Pass headers through proxy
This commit is contained in:
parent
58c39d4ffe
commit
f0ad4cdd50
|
@ -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);
|
||||||
|
|
||||||
|
// 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) {
|
for (var name in proxyRequest.headers.keys) {
|
||||||
final value = proxyRequest.headers[name];
|
originRequest.headers.set(name, proxyRequest.headers[name]);
|
||||||
if (value != null) {
|
|
||||||
originRequest.headers.set(name, value);
|
|
||||||
} else {
|
|
||||||
originRequest.headers.removeAll(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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue