From 7f98000e9b53e09ddbf4e664d98a9562d6e732d3 Mon Sep 17 00:00:00 2001 From: Alexibu Date: Wed, 21 Feb 2024 03:43:07 +0000 Subject: [PATCH 1/5] failing test --- tests/vibe.http.proxy.copyHeaders/dub.sdl | 4 ++ .../vibe.http.proxy.copyHeaders/source/app.d | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/vibe.http.proxy.copyHeaders/dub.sdl create mode 100644 tests/vibe.http.proxy.copyHeaders/source/app.d diff --git a/tests/vibe.http.proxy.copyHeaders/dub.sdl b/tests/vibe.http.proxy.copyHeaders/dub.sdl new file mode 100644 index 0000000..3a53d2b --- /dev/null +++ b/tests/vibe.http.proxy.copyHeaders/dub.sdl @@ -0,0 +1,4 @@ +name "tests" +description "proxy tests" +dependency "vibe-http" path="../../" +versions "VibeDefaultMain" 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..258e632 --- /dev/null +++ b/tests/vibe.http.proxy.copyHeaders/source/app.d @@ -0,0 +1,54 @@ +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; + +shared static this() +{ + { + auto settings = new HTTPServerSettings; + settings.port = 80; + settings.bindAddresses = ["::1", "0.0.0.0"]; + + 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("0.0.0.0",80)); + auto settings = new HTTPServerSettings; + settings.port = 8080; + settings.bindAddresses = ["::1", "0.0.0.0"]; + listenHTTP(settings, router); + } + + runTask({ + scope (exit) exitEventLoop(); + try { + auto res = requestHTTP("http://0.0.0.0:8080"); + 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."); + } catch (Exception e) assert(false, e.msg); + logInfo("All web tests succeeded."); + }); + runApplication(); +} From 1223a6bbfdc526089b340c8b910baa1352fb2725 Mon Sep 17 00:00:00 2001 From: Alexibu Date: Wed, 21 Feb 2024 03:48:22 +0000 Subject: [PATCH 2/5] test passing --- source/vibe/http/proxy.d | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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); } From be522ab3c88699cc3b616eef7686b5e0cb5e503a Mon Sep 17 00:00:00 2001 From: Alexibu Date: Wed, 21 Feb 2024 08:19:58 +0000 Subject: [PATCH 3/5] make test use random ports so it can cooperate with other parellel tests --- .../vibe.http.proxy.copyHeaders/source/app.d | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/tests/vibe.http.proxy.copyHeaders/source/app.d b/tests/vibe.http.proxy.copyHeaders/source/app.d index 258e632..4360e52 100644 --- a/tests/vibe.http.proxy.copyHeaders/source/app.d +++ b/tests/vibe.http.proxy.copyHeaders/source/app.d @@ -12,30 +12,24 @@ import std.stdio; shared static this() { - { - auto settings = new HTTPServerSettings; - settings.port = 80; - settings.bindAddresses = ["::1", "0.0.0.0"]; + 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("0.0.0.0",80)); - auto settings = new HTTPServerSettings; - settings.port = 8080; - settings.bindAddresses = ["::1", "0.0.0.0"]; - listenHTTP(settings, router); - } + 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; runTask({ scope (exit) exitEventLoop(); try { - auto res = requestHTTP("http://0.0.0.0:8080"); + auto res = requestHTTP("http://" ~ proxyAddr.toString); assert(res.statusCode == HTTPStatus.ok); bool hadY; bool hadZ; @@ -48,7 +42,6 @@ shared static this() assert(hadY); assert(res.bodyReader.readAllUTF8 == "Hello world."); } catch (Exception e) assert(false, e.msg); - logInfo("All web tests succeeded."); }); runApplication(); } From 3606114cbecbe53e16fcecca38cbf098188b4ed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 23 Feb 2024 11:48:38 +0100 Subject: [PATCH 4/5] Adjust test case to work without default main and task. --- .../vibe.http.proxy.copyHeaders/source/app.d | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/tests/vibe.http.proxy.copyHeaders/source/app.d b/tests/vibe.http.proxy.copyHeaders/source/app.d index 4360e52..91452fb 100644 --- a/tests/vibe.http.proxy.copyHeaders/source/app.d +++ b/tests/vibe.http.proxy.copyHeaders/source/app.d @@ -10,7 +10,7 @@ import std.range.primitives : front; import std.socket : AddressFamily; import std.stdio; -shared static this() +void main() { auto settings = new HTTPServerSettings; settings.port = 0; @@ -26,22 +26,15 @@ shared static this() router.get("/", reverseProxyRequest(serverAddr.toAddressString,serverAddr.port)); immutable proxyAddr = listenHTTP(settings, router).bindAddresses.find!(addr => addr.family == AddressFamily.INET).front; - runTask({ - scope (exit) exitEventLoop(); - try { - 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."); - } catch (Exception e) assert(false, e.msg); - }); - runApplication(); + 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."); } From 2b7d35de641f6e1f49fbca3107025554cf592f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 23 Feb 2024 11:48:53 +0100 Subject: [PATCH 5/5] Remove default main --- tests/vibe.http.proxy.copyHeaders/dub.sdl | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/vibe.http.proxy.copyHeaders/dub.sdl b/tests/vibe.http.proxy.copyHeaders/dub.sdl index 3a53d2b..ee99ce9 100644 --- a/tests/vibe.http.proxy.copyHeaders/dub.sdl +++ b/tests/vibe.http.proxy.copyHeaders/dub.sdl @@ -1,4 +1,3 @@ name "tests" description "proxy tests" dependency "vibe-http" path="../../" -versions "VibeDefaultMain"