diff --git a/source/vibe/http/proxy.d b/source/vibe/http/proxy.d index 6178711..274aaad 100644 --- a/source/vibe/http/proxy.d +++ b/source/vibe/http/proxy.d @@ -181,7 +181,7 @@ HTTPServerRequestDelegateS proxyRequest(HTTPProxySettings settings) if ("Content-Length" !in cres.headers && "Transfer-Encoding" !in cres.headers || req.method == HTTPMethod.HEAD) { foreach (key, ref value; cres.headers.byKeyValue) if (icmp2(key, "Connection") != 0) - res.headers[key] = value; + res.headers.addField(key,value); res.writeVoidBody(); return; } @@ -192,7 +192,7 @@ HTTPServerRequestDelegateS proxyRequest(HTTPProxySettings settings) // copy all headers that may pass from upstream to client foreach (n, ref v; cres.headers.byKeyValue) if (n !in non_forward_headers_map) - res.headers[n] = v; + res.headers.addField(n,v); if ("Transfer-Encoding" in res.headers) res.headers.remove("Transfer-Encoding"); auto content = cres.bodyReader.readAll(1024*1024); @@ -207,7 +207,7 @@ HTTPServerRequestDelegateS proxyRequest(HTTPProxySettings settings) if ("Content-Encoding" in res.headers) res.headers.remove("Content-Encoding"); foreach (key, ref value; cres.headers.byKeyValue) if (icmp2(key, "Connection") != 0) - res.headers[key] = value; + res.headers.addField(key,value); auto size = cres.headers["Content-Length"].to!size_t(); if (res.isHeadResponse) res.writeVoidBody(); else cres.readRawBody((scope InterfaceProxy!InputStream reader) { res.writeRawBody(reader, size); }); @@ -219,7 +219,7 @@ HTTPServerRequestDelegateS proxyRequest(HTTPProxySettings settings) // copy all headers that may pass from upstream to client foreach (n, ref v; cres.headers.byKeyValue) if (n !in non_forward_headers_map) - res.headers[n] = v; + res.headers.addField(n,v); if (res.isHeadResponse) res.writeVoidBody(); else cres.bodyReader.pipe(res.bodyWriter); } diff --git a/tests/vibe.http.proxy.copyHeaders/dub.sdl b/tests/vibe.http.proxy.copyHeaders/dub.sdl new file mode 100644 index 0000000..ee99ce9 --- /dev/null +++ b/tests/vibe.http.proxy.copyHeaders/dub.sdl @@ -0,0 +1,3 @@ +name "tests" +description "proxy tests" +dependency "vibe-http" path="../../" diff --git a/tests/vibe.http.proxy.copyHeaders/source/app.d b/tests/vibe.http.proxy.copyHeaders/source/app.d new file mode 100644 index 0000000..91452fb --- /dev/null +++ b/tests/vibe.http.proxy.copyHeaders/source/app.d @@ -0,0 +1,40 @@ +import vibe.core.core; +import vibe.core.log; +import vibe.http.client; +import vibe.http.server; +import vibe.http.proxy; +import vibe.http.router; +import vibe.stream.operations : readAllUTF8; +import std.algorithm : find; +import std.range.primitives : front; +import std.socket : AddressFamily; +import std.stdio; + +void main() +{ + auto settings = new HTTPServerSettings; + settings.port = 0; + settings.bindAddresses = ["127.0.0.1"]; + + immutable serverAddr = listenHTTP(settings, (scope req, scope res) { + res.headers.addField("X","Y"); + res.headers.addField("X","Z"); + res.writeBody("Hello world."); + }).bindAddresses.find!(addr => addr.family == AddressFamily.INET).front; + + auto router = new URLRouter; + router.get("/", reverseProxyRequest(serverAddr.toAddressString,serverAddr.port)); + immutable proxyAddr = listenHTTP(settings, router).bindAddresses.find!(addr => addr.family == AddressFamily.INET).front; + + auto res = requestHTTP("http://" ~ proxyAddr.toString); + assert(res.statusCode == HTTPStatus.ok); + bool hadY; + bool hadZ; + foreach (k, v; res.headers.byKeyValue) { + if ((k == "X") && (v == "Y")) hadY = true; + if ((k == "X") && (v == "Z")) hadZ = true; + } + assert(hadZ); + assert(hadY); + assert(res.bodyReader.readAllUTF8 == "Hello world."); +}