@@ -5,6 +5,7 @@ var cors = require('cors')
55var http = require ( 'http' )
66var https = require ( 'https' )
77var debug = require ( '../debug' )
8+ var url = require ( 'url' )
89
910function addProxy ( app , path ) {
1011 debug . settings ( 'XSS Proxy listening to ' + path )
@@ -13,7 +14,6 @@ function addProxy (app, path) {
1314 cors ( {
1415 methods : [ 'GET' ] ,
1516 exposedHeaders : 'User, Location, Link, Vary, Last-Modified, Content-Length' ,
16- credentials : true ,
1717 maxAge : 1728000 ,
1818 origin : true
1919 } ) ,
@@ -37,11 +37,28 @@ function addProxy (app, path) {
3737 return res . send ( 400 )
3838 }
3939
40- var _req = request ( uri , function ( _res ) {
40+ // Set the headers and uri of the proxied request
41+ var opts = url . parse ( uri )
42+ opts . headers = req . headers
43+ // See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
44+ delete opts . headers . connection
45+ delete opts . headers . host
46+
47+ var _req = request ( opts , function ( _res ) {
4148 res . status ( _res . statusCode )
49+ // Set the response with the same header of proxied response
50+ Object . keys ( _res . headers ) . forEach ( function ( header ) {
51+ if ( ! res . get ( header ) ) {
52+ res . setHeader ( header . trim ( ) , _res . headers [ header ] )
53+ }
54+ } )
4255 _res . pipe ( res )
4356 } )
4457
58+ _req . on ( 'error' , function ( e ) {
59+ res . send ( 500 , 'Cannot proxy' )
60+ } )
61+
4562 _req . end ( )
4663 } )
4764}
0 commit comments