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 proxyRequest = _uriMap[path];
final originRequest = await HttpClient().getUrl(proxyRequest.uri);
// Rewrite request headers
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);
});
for (var name in proxyRequest.headers.keys) {
final value = proxyRequest.headers[name];
if (value != null) {
originRequest.headers.set(name, value);
} else {
originRequest.headers.removeAll(name);
}
originRequest.headers.set(name, proxyRequest.headers[name]);
}
originRequest.headers.set('host', host);
// Make request
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);
}
});