From 3c3b3b7b7349ef459bca14a344b03cfb85c7ad02 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sun, 8 Jan 2017 21:11:20 -0500 Subject: [PATCH] Yay --- lib/src/proxy_layer.dart | 38 ++++++++++++++++++++++++++++++++++++ lib/src/pub_serve_layer.dart | 7 ++++++- pubspec.yaml | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/src/proxy_layer.dart b/lib/src/proxy_layer.dart index 9c4975c0..540f1282 100644 --- a/lib/src/proxy_layer.dart +++ b/lib/src/proxy_layer.dart @@ -20,17 +20,37 @@ String _pathify(String path) { return p; } +/// Copies HTTP headers ;) +void copyHeaders(HttpHeaders from, HttpHeaders to) { + to + ..chunkedTransferEncoding = from.chunkedTransferEncoding + ..contentLength = from.contentLength + ..contentType = from.contentType + ..date = from.date + ..expires = from.expires + ..host = from.host + ..ifModifiedSince = from.ifModifiedSince + ..persistentConnection = from.persistentConnection + ..port = from.port; + + from.forEach((header, values) { + to.set(header, values); + }); +} + class ProxyLayer { HttpClient _client; String _prefix; final bool debug; final String host, mapTo, publicPath; final int port; + final String protocol; ProxyLayer(this.host, this.port, {this.debug: false, this.mapTo: '/', this.publicPath: '/', + this.protocol: 'http', SecurityContext securityContext}) { _client = new HttpClient(context: securityContext); _prefix = publicPath.replaceAll(_straySlashes, ''); @@ -70,6 +90,24 @@ class ProxyLayer { // Create mapping final mapping = '$mapTo/$_path'.replaceAll(_straySlashes, ''); final rq = await _client.open(req.method, host, port, mapping); + + 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 + ..add('X-Forwarded-For', req.connectionInfo.remoteAddress.address) + ..add('X-Forwarded-Port', req.connectionInfo.remotePort.toString()) + ..add('X-Forwarded-Host', + req.headers.host ?? req.headers.value(HttpHeaders.HOST) ?? 'none') + ..add('X-Forwarded-Proto', protocol); + await rq.addStream(req.io); final HttpClientResponse rs = await rq.close(); final HttpResponse r = res.io; diff --git a/lib/src/pub_serve_layer.dart b/lib/src/pub_serve_layer.dart index 40fb4a89..dd8d1789 100644 --- a/lib/src/pub_serve_layer.dart +++ b/lib/src/pub_serve_layer.dart @@ -8,8 +8,13 @@ class PubServeLayer extends ProxyLayer { String host: 'localhost', String mapTo: '/', int port: 8080, + String protocol: 'http', String publicPath: '/'}) - : super(host, port, debug: debug, mapTo: mapTo, publicPath: publicPath); + : super(host, port, + debug: debug, + mapTo: mapTo, + protocol: protocol, + publicPath: publicPath); @override void serve(Router router) { diff --git a/pubspec.yaml b/pubspec.yaml index 3f768f5f..7e6f1ee3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: angel_proxy description: Angel middleware to forward requests to another server (i.e. pub serve). -version: 1.0.0-dev+3 +version: 1.0.0-dev+4 author: Tobe O homepage: https://github.com/angel-dart/proxy environment: