Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy supports multiple headers with same name #30

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
}
Loading