Skip to content

Commit

Permalink
Changed response.headers to a StringMultiValueMap
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlalis committed Jan 11, 2025
1 parent 3a43b35 commit 233b920
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion source/handy_httpd/components/parse_utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public Optional!HttpRequestContext receiveRequest(InputStream, OutputStream)(
ctx.request.remoteAddress = clientSocket.remoteAddress;

ctx.response.outputStream = outputStreamObjectFor(outputStream);
ctx.response.headers["Connection"] = "close";
ctx.response.addHeader("Connection", "close");
foreach (header, value; server.config.defaultHeaders) {
ctx.response.addHeader(header, value);
}
Expand Down
10 changes: 6 additions & 4 deletions source/handy_httpd/components/response.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
module handy_httpd.components.response;

import handy_httpd.components.multivalue_map;

import std.array : appender;
import std.conv : to;
import std.socket : Socket;
Expand All @@ -19,9 +21,9 @@ struct HttpResponse {
public StatusInfo status = HttpStatus.OK;

/**
* An associative array of headers.
* A multi-valued map of response headers.
*/
public string[string] headers;
public StringMultiValueMap headers;

/**
* Internal flag used to determine if we've already flushed the headers.
Expand Down Expand Up @@ -60,7 +62,7 @@ struct HttpResponse {
if (flushed) {
warnF!"Attempted to set header \"%s\" to \"%s\" after the response has already been flushed."(name, value);
}
this.headers[name] = value;
this.headers.add(name, value);
return this;
}

Expand All @@ -76,7 +78,7 @@ struct HttpResponse {
warn("Attempted to set response body as chunked-encoded after headers have been flushed.");
return this;
}
this.headers["Transfer-Encoding"] = "chunked";
this.addHeader("Transfer-Encoding", "chunked");
this.outputStream = outputStreamObjectFor(ChunkedEncodingOutputStream!(OutputStream!ubyte)(this.outputStream));
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions source/handy_httpd/util/builders.d
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class HttpResponseBuilder {
private HttpRequestContextBuilder ctxBuilder;

private StatusInfo status = HttpStatus.OK;
private string[string] headers;
private StringMultiValueMap headers;
private OutputStream!ubyte outputStream = new ResponseCachingOutputStream();

this() {}
Expand All @@ -301,13 +301,13 @@ class HttpResponseBuilder {
}

HttpResponseBuilder withHeader(string name, string value) {
this.headers[name] = value;
this.headers.add(name, value);
return this;
}

HttpResponseBuilder withHeader(V)(string name, V value) {
import std.conv : to;
this.headers[name] = value.to!string;
this.headers.add(name, value.to!string);
return this;
}

Expand Down

0 comments on commit 233b920

Please sign in to comment.