Skip to content

Commit

Permalink
Merge pull request #30 from Alexibu/master
Browse files Browse the repository at this point in the history
Proxy supports multiple headers with same name
  • Loading branch information
s-ludwig authored Feb 23, 2024
2 parents 6c9e346 + 2b7d35d commit af625ad
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
8 changes: 4 additions & 4 deletions source/vibe/http/proxy.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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); });
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/vibe.http.proxy.copyHeaders/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name "tests"
description "proxy tests"
dependency "vibe-http" path="../../"
40 changes: 40 additions & 0 deletions tests/vibe.http.proxy.copyHeaders/source/app.d
Original file line number Diff line number Diff line change
@@ -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.");
}

0 comments on commit af625ad

Please sign in to comment.