Skip to content

Commit 8e31c31

Browse files
author
Andrei
committed
Merge pull request #257 from linkeddata/fix-proxy-issues
Fixing issues with proxy headers
2 parents 4e5ff9b + 0f71020 commit 8e31c31

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- "0.12"
4+
- "4.0"
5+
- "5.0"
56
cache:
67
directories:
78
- node_modules
89
addons:
910
hosts:
1011
- nic.localhost
1112
- tim.localhost
12-
- nicola.localhost
13+
- nicola.localhost

lib/handlers/proxy.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var cors = require('cors')
55
var http = require('http')
66
var https = require('https')
77
var debug = require('../debug')
8+
var url = require('url')
89

910
function 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
}

test/params.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ describe('LDNODE params', function () {
2525
.expect(200, done)
2626
})
2727

28+
it('should return the same headers of proxied request', function (done) {
29+
nock('https://amazingwebsite.tld')
30+
.get('/')
31+
.reply(function (uri, req) {
32+
if (this.req.headers['accept'] !== 'text/turtle') {
33+
throw Error('Accept is received on the header')
34+
}
35+
if (this.req.headers['test'] && this.req.headers['test'] === 'test1') {
36+
return [200, 'YES']
37+
} else {
38+
return [500, 'empty']
39+
}
40+
})
41+
42+
server.get('/proxy?uri=https://amazingwebsite.tld/')
43+
.set('test', 'test1')
44+
.set('accept', 'text/turtle')
45+
.expect(200)
46+
.end(function (err, data) {
47+
if (err) return done(err)
48+
done(err)
49+
})
50+
})
51+
2852
it('should also work on /proxy/ ?uri', function (done) {
2953
nock('https://amazingwebsite.tld').get('/').reply(200)
3054
server.get('/proxy/?uri=https://amazingwebsite.tld/')

0 commit comments

Comments
 (0)