Broken still

This commit is contained in:
thosakwe 2017-01-11 19:12:23 -05:00
parent 3c3b3b7b73
commit 3dcc17c5aa
2 changed files with 24 additions and 21 deletions

View file

@ -22,6 +22,7 @@ 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);
to to
..chunkedTransferEncoding = from.chunkedTransferEncoding ..chunkedTransferEncoding = from.chunkedTransferEncoding
..contentLength = from.contentLength ..contentLength = from.contentLength
@ -32,10 +33,6 @@ void copyHeaders(HttpHeaders from, HttpHeaders to) {
..ifModifiedSince = from.ifModifiedSince ..ifModifiedSince = from.ifModifiedSince
..persistentConnection = from.persistentConnection ..persistentConnection = from.persistentConnection
..port = from.port; ..port = from.port;
from.forEach((header, values) {
to.set(header, values);
});
} }
class ProxyLayer { class ProxyLayer {
@ -88,31 +85,32 @@ class ProxyLayer {
..end(); ..end();
// Create mapping // Create mapping
_printDebug('Serving path $_path via proxy');
final mapping = '$mapTo/$_path'.replaceAll(_straySlashes, ''); final mapping = '$mapTo/$_path'.replaceAll(_straySlashes, '');
_printDebug('Mapped path $_path to path $mapping on proxy $host:$port');
final rq = await _client.open(req.method, host, port, mapping); final rq = await _client.open(req.method, host, port, mapping);
_printDebug('Opened client request');
if (req.headers.contentType != null)
rq.headers.contentType = req.headers.contentType;
rq.cookies.addAll(req.cookies);
copyHeaders(req.headers, rq.headers);
if (req.headers[HttpHeaders.ACCEPT] == null) {
req.headers.set(HttpHeaders.ACCEPT, '*/*');
}
rq.headers rq.headers
..add('X-Forwarded-For', req.connectionInfo.remoteAddress.address) ..set('X-Forwarded-For', req.connectionInfo.remoteAddress.address)
..add('X-Forwarded-Port', req.connectionInfo.remotePort.toString()) ..set('X-Forwarded-Port', req.connectionInfo.remotePort.toString())
..add('X-Forwarded-Host', ..set('X-Forwarded-Host',
req.headers.host ?? req.headers.value(HttpHeaders.HOST) ?? 'none') req.headers.host ?? req.headers.value(HttpHeaders.HOST) ?? 'none')
..add('X-Forwarded-Proto', protocol); ..set('X-Forwarded-Proto', protocol);
_printDebug('Added X-Forwarded headers');
copyHeaders(req.headers, rq.headers);
_printDebug('Copied headers');
rq.cookies.addAll(req.cookies ?? []);
_printDebug('Added cookies');
await rq.addStream(req.io); await rq.addStream(req.io);
final HttpClientResponse rs = await rq.close(); final HttpClientResponse rs = await rq.close();
final HttpResponse r = res.io; final HttpResponse r = res.io;
_printDebug(
'Proxy responded to $mapping with status code ${rs.statusCode}');
r.statusCode = rs.statusCode; r.statusCode = rs.statusCode;
r.headers.contentType = rs.headers.contentType; r.headers.contentType = rs.headers.contentType;
copyHeaders(rs.headers, r.headers);
await r.addStream(rs); await r.addStream(rs);
await r.flush(); await r.flush();
await r.close(); await r.close();

View file

@ -15,8 +15,13 @@ main() {
app = new Angel(); app = new Angel();
testServer = await testApp().startServer(); testServer = await testApp().startServer();
await app.configure(new ProxyLayer(testServer.address.address, testServer.port, publicPath: '/proxy'));
await app.configure(new ProxyLayer(testServer.address.address, testServer.port, mapTo: '/foo')); await app.configure(new ProxyLayer(
testServer.address.address, testServer.port,
publicPath: '/proxy', debug: true));
await app.configure(new ProxyLayer(
testServer.address.address, testServer.port,
mapTo: '/foo'));
server = await app.startServer(); server = await app.startServer();
url = 'http://${server.address.address}:${server.port}'; url = 'http://${server.address.address}:${server.port}';
@ -40,4 +45,4 @@ main() {
print('Response: ${response.body}'); print('Response: ${response.body}');
expect(response.body, equals('"baz"')); expect(response.body, equals('"baz"'));
}); });
} }