Pass headers through proxy

This commit is contained in:
Ryan Heise 2020-06-10 00:43:07 +10:00
parent 58c39d4ffe
commit f0ad4cdd50
1 changed files with 20 additions and 6 deletions

View File

@ -651,15 +651,29 @@ class _ProxyHttpServer {
final path = _requestKey(request.uri); final path = _requestKey(request.uri);
final proxyRequest = _uriMap[path]; final proxyRequest = _uriMap[path];
final originRequest = await HttpClient().getUrl(proxyRequest.uri); final originRequest = await HttpClient().getUrl(proxyRequest.uri);
for (var name in proxyRequest.headers.keys) {
final value = proxyRequest.headers[name]; // Rewrite request headers
if (value != null) { final host = originRequest.headers.value('host');
print("saved host from request headers: $host");
originRequest.headers.clear();
request.headers.forEach((name, value) {
originRequest.headers.set(name, value); originRequest.headers.set(name, value);
} else { });
originRequest.headers.removeAll(name); for (var name in proxyRequest.headers.keys) {
} originRequest.headers.set(name, proxyRequest.headers[name]);
} }
originRequest.headers.set('host', host);
// Make request
final originResponse = await originRequest.close(); final originResponse = await originRequest.close();
// Rewrite response headers
request.response.headers.clear();
originResponse.headers.forEach((name, value) {
request.response.headers.set(name, value);
});
// Pipe response
await originResponse.pipe(request.response); await originResponse.pipe(request.response);
} }
}); });