Yay
This commit is contained in:
parent
fc05e85bfc
commit
3c3b3b7b73
3 changed files with 45 additions and 2 deletions
|
@ -20,17 +20,37 @@ String _pathify(String path) {
|
||||||
return p;
|
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 {
|
class ProxyLayer {
|
||||||
HttpClient _client;
|
HttpClient _client;
|
||||||
String _prefix;
|
String _prefix;
|
||||||
final bool debug;
|
final bool debug;
|
||||||
final String host, mapTo, publicPath;
|
final String host, mapTo, publicPath;
|
||||||
final int port;
|
final int port;
|
||||||
|
final String protocol;
|
||||||
|
|
||||||
ProxyLayer(this.host, this.port,
|
ProxyLayer(this.host, this.port,
|
||||||
{this.debug: false,
|
{this.debug: false,
|
||||||
this.mapTo: '/',
|
this.mapTo: '/',
|
||||||
this.publicPath: '/',
|
this.publicPath: '/',
|
||||||
|
this.protocol: 'http',
|
||||||
SecurityContext securityContext}) {
|
SecurityContext securityContext}) {
|
||||||
_client = new HttpClient(context: securityContext);
|
_client = new HttpClient(context: securityContext);
|
||||||
_prefix = publicPath.replaceAll(_straySlashes, '');
|
_prefix = publicPath.replaceAll(_straySlashes, '');
|
||||||
|
@ -70,6 +90,24 @@ class ProxyLayer {
|
||||||
// Create mapping
|
// Create mapping
|
||||||
final mapping = '$mapTo/$_path'.replaceAll(_straySlashes, '');
|
final mapping = '$mapTo/$_path'.replaceAll(_straySlashes, '');
|
||||||
final rq = await _client.open(req.method, host, port, mapping);
|
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);
|
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;
|
||||||
|
|
|
@ -8,8 +8,13 @@ class PubServeLayer extends ProxyLayer {
|
||||||
String host: 'localhost',
|
String host: 'localhost',
|
||||||
String mapTo: '/',
|
String mapTo: '/',
|
||||||
int port: 8080,
|
int port: 8080,
|
||||||
|
String protocol: 'http',
|
||||||
String publicPath: '/'})
|
String publicPath: '/'})
|
||||||
: super(host, port, debug: debug, mapTo: mapTo, publicPath: publicPath);
|
: super(host, port,
|
||||||
|
debug: debug,
|
||||||
|
mapTo: mapTo,
|
||||||
|
protocol: protocol,
|
||||||
|
publicPath: publicPath);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void serve(Router router) {
|
void serve(Router router) {
|
||||||
|
|
|
@ -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+3
|
version: 1.0.0-dev+4
|
||||||
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:
|
||||||
|
|
Loading…
Reference in a new issue