This commit is contained in:
thosakwe 2017-04-26 08:20:14 -04:00
parent 918b20086f
commit 5c8f3e28b1
6 changed files with 46 additions and 30 deletions

View file

@ -12,7 +12,8 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart Packages" level="project" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Dart SDK" level="application" />
<orderEntry type="library" name="Dart Packages" level="project" />
</component>
</module>

View file

@ -1,6 +1,6 @@
# angel_proxy
[![version 1.0.6](https://img.shields.io/badge/version-1.0.6-brightgreen.svg)](https://pub.dartlang.org/packages/angel_proxy)
[![version 1.0.7](https://img.shields.io/badge/version-1.0.7-brightgreen.svg)](https://pub.dartlang.org/packages/angel_proxy)
[![build status](https://travis-ci.org/angel-dart/proxy.svg)](https://travis-ci.org/angel-dart/proxy)
Angel middleware to forward requests to another server (i.e. pub serve).

View file

@ -44,6 +44,9 @@ class ProxyLayer {
Angel app;
HttpClient _client;
String _prefix;
/// If `true` (default), then the plug-in will ignore failures to connect to the proxy, and allow other handlers to run.
final bool recoverFromDead;
final bool debug, recoverFrom404, streamToIO;
final String host, mapTo, publicPath;
final int port;
@ -54,6 +57,7 @@ class ProxyLayer {
this.mapTo: '/',
this.publicPath: '/',
this.protocol: 'http',
this.recoverFromDead: true,
this.recoverFrom404: true,
this.streamToIO: false,
SecurityContext securityContext}) {
@ -82,6 +86,7 @@ class ProxyLayer {
serveFile(String path, RequestContext req, ResponseContext res) async {
var _path = path;
HttpClientResponse rs;
if (_prefix.isNotEmpty) {
_path = path.replaceAll(new RegExp('^' + _pathify(_prefix)), '');
@ -91,29 +96,38 @@ class ProxyLayer {
_printDebug('Serving path $_path via proxy');
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);
_printDebug('Opened client request');
copyHeaders(req.headers, rq.headers);
_printDebug('Copied headers');
rq.cookies.addAll(req.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');
try {
final rq = await _client.open(req.method, host, port, mapping);
_printDebug('Opened client request');
if (app.storeOriginalBuffer == true) {
await req.parse();
if (req.originalBuffer?.isNotEmpty == true) rq.add(req.originalBuffer);
copyHeaders(req.headers, rq.headers);
_printDebug('Copied headers');
rq.cookies.addAll(req.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');
if (app.storeOriginalBuffer == true) {
await req.parse();
if (req.originalBuffer?.isNotEmpty == true) rq.add(req.originalBuffer);
}
await rq.flush();
rs = await rq.close();
} catch (e) {
if (recoverFromDead != false)
return true;
else
rethrow;
}
await rq.flush();
final HttpClientResponse rs = await rq.close();
_printDebug(
'Proxy responded to $mapping with status code ${rs.statusCode}');

View file

@ -4,6 +4,7 @@ import 'proxy_layer.dart';
class PubServeLayer extends ProxyLayer {
PubServeLayer(
{bool debug: false,
bool recoverFromDead: true,
bool recoverFrom404: true,
bool streamToIO: true,
String host: 'localhost',
@ -16,6 +17,7 @@ class PubServeLayer extends ProxyLayer {
mapTo: mapTo,
protocol: protocol,
publicPath: publicPath,
recoverFromDead: recoverFromDead != false,
recoverFrom404: recoverFrom404 != false,
streamToIO: streamToIO != false);

View file

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

View file

@ -20,7 +20,10 @@ main() {
app = new Angel();
app.get('/bar', (req, res) => res.write('normal'));
var layer = new PubServeLayer(
debug: true, publicPath: '/proxy', host: server.address.address, port: server.port);
debug: true,
publicPath: '/proxy',
host: server.address.address,
port: server.port);
print('streamToIO: ${layer.streamToIO}');
await app.configure(layer);
@ -51,7 +54,7 @@ main() {
// Should have gzipped body
//
// We have to decode it, because `mock_request` does not auto-decode.
expect(UTF8.decode(GZIP.decode(response.bodyBytes)), 'pub serve');
expect(response, hasBody('pub serve'));
});
test('empty', () async {
@ -61,9 +64,7 @@ main() {
expect(response, hasHeader('content-encoding', 'gzip'));
// Should have gzipped body
//
// We have to decode it, because `mock_request` does not auto-decode.
expect(UTF8.decode(GZIP.decode(response.bodyBytes)), isEmpty);
expect(response.body, isEmpty);
});
test('normal', () async {
@ -73,8 +74,6 @@ main() {
expect(response, hasHeader('content-encoding', 'gzip'));
// Should have normal body
//
// We have to decode it, because `mock_request` does not auto-decode.
expect(UTF8.decode(GZIP.decode(response.bodyBytes)), 'normal');
expect(response, hasBody('normal'));
});
}