-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.js
executable file
·91 lines (69 loc) · 2.11 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
// Inspired by https://github.com/clintandrewhall/node-jsonp-proxy
'use strict';
var sys = require("util"), http = require("http"),
url = require("url"), https = require('https');
var argv = optimist();
console.log(argv.p) ;
console.log(argv.u) ;
console.log(argv.c) ;
http.createServer(function(req, res) {
// read the config file and/or commmand line parameters
var from_uri = url.parse(req.url, true);
var to_uri = url.parse(argv.u, true);
var protocol = http;
var options = {
path: from_uri.path,
port: to_uri.port,
method: "GET",
hostname : to_uri.hostname,
headers: {
'Accept' : 'application/sparql-results+json'
}
};
var r = protocol.request(options);
r.addListener('response', function(response) {
var body = '';
response.setEncoding("utf8");
response.addListener("data", function(chunk) {
body += chunk;
});
response.addListener('end', function() {
wrap(body);
});
});
r.end();
function wrap(contents) {
res.write(argv.c + '(' + contents + ')');
return res.end() ;
}
}).listen(argv.p);
function optimist() {
var usage = [
'Simple proxy to wrap json responses in a callback',
'',
'',
'Usage:',
' json-proxy [-u uri] [-p port] [-c callback]',
'',
'Examples:',
' proxy -p 8080 -u "http://server:9900 -c "callb_" ',
''
].join('\n');
var argv = require('optimist')
.usage(usage)
.alias('p', 'port')
.describe('p', 'The TCP port to run on')
.alias('u', 'uri')
.describe('g', 'URL for a LAN HTTP proxy to use for forwarding requests')
.alias('c', 'callback')
.describe('c', 'The callback to wrap the response in, e.g. "callback" ')
.describe('html5mode', 'support AngularJS HTML5 mode by catching 404s')
.alias('?', 'help')
.argv;
if (argv.help === true) {
require('optimist').showHelp();
process.exit(-1);
}
return argv;
}