This commit is contained in:
thosakwe 2017-01-11 19:19:15 -05:00
parent 3dcc17c5aa
commit 2613f1db0a
3 changed files with 15 additions and 13 deletions

View file

@ -22,8 +22,9 @@ String _pathify(String path) {
/// Copies HTTP headers ;) /// Copies HTTP headers ;)
void copyHeaders(HttpHeaders from, HttpHeaders to) { void copyHeaders(HttpHeaders from, HttpHeaders to) {
from.forEach(to.add); from.forEach(to.set);
to
/*to
..chunkedTransferEncoding = from.chunkedTransferEncoding ..chunkedTransferEncoding = from.chunkedTransferEncoding
..contentLength = from.contentLength ..contentLength = from.contentLength
..contentType = from.contentType ..contentType = from.contentType
@ -32,7 +33,7 @@ void copyHeaders(HttpHeaders from, HttpHeaders to) {
..host = from.host ..host = from.host
..ifModifiedSince = from.ifModifiedSince ..ifModifiedSince = from.ifModifiedSince
..persistentConnection = from.persistentConnection ..persistentConnection = from.persistentConnection
..port = from.port; ..port = from.port;*/
} }
class ProxyLayer { class ProxyLayer {
@ -91,17 +92,18 @@ class ProxyLayer {
final rq = await _client.open(req.method, host, port, mapping); final rq = await _client.open(req.method, host, port, mapping);
_printDebug('Opened client request'); _printDebug('Opened client request');
rq.headers
..set('X-Forwarded-For', req.connectionInfo.remoteAddress.address)
..set('X-Forwarded-Port', req.connectionInfo.remotePort.toString())
..set('X-Forwarded-Host',
req.headers.host ?? req.headers.value(HttpHeaders.HOST) ?? 'none')
..set('X-Forwarded-Proto', protocol);
_printDebug('Added X-Forwarded headers');
copyHeaders(req.headers, rq.headers); copyHeaders(req.headers, rq.headers);
_printDebug('Copied headers'); _printDebug('Copied headers');
rq.cookies.addAll(req.cookies ?? []); rq.cookies.addAll(req.cookies ?? []);
_printDebug('Added cookies'); _printDebug('Added cookies');
rq.headers
.set('X-Forwarded-For', req.io.connectionInfo.remoteAddress.address);
rq.headers
..set('X-Forwarded-Port', req.io.connectionInfo.remotePort.toString())
..set('X-Forwarded-Host',
req.headers.host ?? req.headers.value(HttpHeaders.HOST) ?? 'none')
..set('X-Forwarded-Proto', protocol);
_printDebug('Added X-Forwarded headers');
await rq.addStream(req.io); await rq.addStream(req.io);
final HttpClientResponse rs = await rq.close(); final HttpClientResponse rs = await rq.close();

View file

@ -1,6 +1,6 @@
name: angel_proxy name: angel_proxy
description: Angel middleware to forward requests to another server (i.e. pub serve). description: Angel middleware to forward requests to another server (i.e. pub serve).
version: 1.0.0-dev+4 version: 1.0.0-dev+5
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/proxy homepage: https://github.com/angel-dart/proxy
environment: environment:

View file

@ -15,10 +15,10 @@ main() {
app = new Angel(); app = new Angel();
testServer = await testApp().startServer(); testServer = await testApp().startServer();
await app.configure(new ProxyLayer( await app.configure(new ProxyLayer(
testServer.address.address, testServer.port, testServer.address.address, testServer.port,
publicPath: '/proxy', debug: true)); publicPath: '/proxy'));
await app.configure(new ProxyLayer( await app.configure(new ProxyLayer(
testServer.address.address, testServer.port, testServer.address.address, testServer.port,
mapTo: '/foo')); mapTo: '/foo'));