diff options
| author | RblSb <msrblsb@gmail.com> | 2020-05-23 04:39:42 +0300 |
|---|---|---|
| committer | RblSb <msrblsb@gmail.com> | 2020-05-23 04:39:42 +0300 |
| commit | e9ae58d8e97c4f0b492948268436078439cb6e36 (patch) | |
| tree | 97708295cfab80c06c18ef54a2f1a2b5a22adf01 | |
| parent | fc8fbc601c05e1caa671dfd94a48fe402bc39723 (diff) | |
Support single proxy redirect
| -rw-r--r-- | build/server.js | 43 | ||||
| -rw-r--r-- | src/server/HttpServer.hx | 40 |
2 files changed, 63 insertions, 20 deletions
diff --git a/build/server.js b/build/server.js index 737e47b..09fa596 100644 --- a/build/server.js +++ b/build/server.js @@ -1,4 +1,4 @@ -// Generated by Haxe 4.1.0 +// Generated by Haxe 4.1.1 (function ($global) { "use strict"; var $estr = function() { return js_Boot.__string_rec(this,''); },$hxEnums = $hxEnums || {},$_; function $extend(from, fields) { @@ -3268,26 +3268,49 @@ server_HttpServer.localizeHtml = function(data,lang) { return data; }; server_HttpServer.proxyUrl = function(req,res) { - var url = StringTools.replace(req.url,"/proxy?url=",""); + var proxy = server_HttpServer.proxyRequest(StringTools.replace(req.url,"/proxy?url=",""),req,res,function(proxyReq) { + var url = proxyReq.headers["location"]; + if(url == null) { + return false; + } + var proxy2 = server_HttpServer.proxyRequest(url,req,res,function(proxyReq) { + return false; + }); + if(proxy2 == null) { + res.end("Proxy error for redirected " + url); + return true; + } + req.pipe(proxy2,{ end : true}); + return true; + }); + if(proxy == null) { + return false; + } + req.pipe(proxy,{ end : true}); + return true; +}; +server_HttpServer.proxyRequest = function(url,req,res,fn) { var url1; try { url1 = new js_node_url_URL(global.decodeURI(url)); } catch( _g ) { - return false; + return null; } if(url1.host == req.headers["host"]) { - return false; + return null; } - var proxy = (url1.protocol == "https:" ? js_node_Https.request : js_node_Http.request)({ host : url1.host, port : Std.parseInt(url1.port), path : url1.pathname + url1.search, method : req.method},function(proxyRes) { - proxyRes.headers["Content-Type"] = "application/octet-stream"; - res.writeHead(proxyRes.statusCode,proxyRes.headers); - proxyRes.pipe(res,{ end : true}); + var proxy = (url1.protocol == "https:" ? js_node_Https.request : js_node_Http.request)({ host : url1.host, port : Std.parseInt(url1.port), path : url1.pathname + url1.search, method : req.method},function(proxyReq) { + if(fn(proxyReq)) { + return; + } + proxyReq.headers["Content-Type"] = "application/octet-stream"; + res.writeHead(proxyReq.statusCode,proxyReq.headers); + proxyReq.pipe(res,{ end : true}); }); proxy.on("error",function(err) { res.end("Proxy error for " + url1.href); }); - req.pipe(proxy,{ end : true}); - return true; + return proxy; }; server_HttpServer.isChildOf = function(parent,child) { var rel = js_node_Path.relative(parent,child); diff --git a/src/server/HttpServer.hx b/src/server/HttpServer.hx index c6abe30..5a7c6ee 100644 --- a/src/server/HttpServer.hx +++ b/src/server/HttpServer.hx @@ -9,6 +9,7 @@ import js.node.Http; import js.node.url.URL; import js.node.http.IncomingMessage; import js.node.http.ServerResponse; +import js.node.http.ClientRequest; import js.node.Path as JsPath; using StringTools; @@ -138,28 +139,47 @@ class HttpServer { static function proxyUrl(req:IncomingMessage, res:ServerResponse):Bool { final url = req.url.replace("/proxy?url=", ""); + final proxy = proxyRequest(url, req, res, proxyReq -> { + final url = proxyReq.headers["location"]; + if (url == null) return false; + final proxy2 = proxyRequest(url, req, res, proxyReq -> false); + if (proxy2 == null) { + res.end('Proxy error for redirected $url'); + return true; + } + req.pipe(proxy2, {end: true}); + return true; + }); + if (proxy == null) return false; + req.pipe(proxy, {end: true}); + return true; + } + + static function proxyRequest( + url:String, req:IncomingMessage, res:ServerResponse, + fn:(req:IncomingMessage)->Bool + ):Null<ClientRequest> { final url = try { new URL(js.Node.global.decodeURI(url)); - } catch(e) return false; - if (url.host == req.headers["host"]) return false; + } catch (e) return null; + if (url.host == req.headers["host"]) return null; final options = { host: url.host, port: Std.parseInt(url.port), path: url.pathname + url.search, - method: req.method, - // headers: req.headers + method: req.method }; final request = url.protocol == "https:" ? Https.request : Http.request; - final proxy = request(options, proxyRes -> { - proxyRes.headers["Content-Type"] = "application/octet-stream"; - res.writeHead(proxyRes.statusCode, proxyRes.headers); - proxyRes.pipe(res, {end: true}); + final proxy = request(options, proxyReq -> { + if (fn(proxyReq)) return; + proxyReq.headers["Content-Type"] = "application/octet-stream"; + res.writeHead(proxyReq.statusCode, proxyReq.headers); + proxyReq.pipe(res, {end: true}); }); proxy.on("error", err -> { res.end('Proxy error for ${url.href}'); }); - req.pipe(proxy, {end: true}); - return true; + return proxy; } static function isChildOf(parent:String, child:String):Bool { |
