-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttptrail.js
executable file
·131 lines (117 loc) · 4.03 KB
/
httptrail.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
// Generated by CoffeeScript 1.12.5
var HttpStream, cleanHost, error, http, port, proxy, ref, stream, upstream, urllib,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
slice = [].slice;
http = require('http');
stream = require('stream');
urllib = require('url');
exports.HttpStream = HttpStream = (function(superClass) {
extend(HttpStream, superClass);
function HttpStream(options) {
this.format = bind(this.format, this);
HttpStream.__super__.constructor.apply(this, arguments);
this.prefix = options.prefix || '';
this.buffer = '';
this.on('pipe', this.headers);
}
HttpStream.prototype.headers = function(request) {
var header, ref, value;
ref = request.headers;
for (header in ref) {
value = ref[header];
this.write(header + ": " + value + "\n");
}
return this.write('\n');
};
HttpStream.prototype.format = function(line) {
return "" + this.prefix + line + "\n";
};
HttpStream.prototype._transform = function(chunk, encoding, done) {
var i, lines, ref;
this.buffer += chunk.toString(encoding !== 'buffer' ? encoding : void 0);
if (this.buffer.indexOf('\n') > -1) {
ref = this.buffer.split('\n'), lines = 2 <= ref.length ? slice.call(ref, 0, i = ref.length - 1) : (i = 0, []), this.buffer = ref[i++];
this.push(lines.map(this.format).join(''));
}
return done();
};
HttpStream.prototype._flush = function(done) {
if (this.buffer) {
this.push(this.format(this.buffer));
}
return done();
};
return HttpStream;
})(stream.Transform);
exports.proxy = proxy = function(host) {
var client, ref;
host = urllib.parse(cleanHost(host));
if ((ref = host.protocol) !== 'http:' && ref !== 'https:') {
throw new Error('httptrail only supports http');
}
client = host.protocol === 'https:' ? require('https') : http;
return http.createServer(function(request, response) {
var log, proxied, url;
log = {
request: new HttpStream({
prefix: '> '
}),
response: new HttpStream({
prefix: '< '
})
};
log.request.pipe(process.stdout);
log.response.pipe(process.stdout);
request.headers.host = host.hostname;
url = urllib.parse(request.url);
proxied = client.request({
hostname: host.hostname,
port: host.port,
method: request.method,
path: url.path,
auth: url.auth,
headers: request.headers
});
request.pipe(proxied);
request.pipe(log.request);
return proxied.once('response', function(upstream) {
response.writeHead(upstream.statusCode, upstream.headers);
upstream.pipe(response);
return upstream.pipe(log.response);
});
});
};
cleanHost = function(host) {
var hostname, port, protocol, ref;
host = host.trim();
if (/^\d+$/.test(host)) {
host = "http://localhost:" + host + "/";
} else if (!/:\/\//.test(host)) {
host = host.replace(/^(\/\/)?/, 'http://');
}
ref = urllib.parse(host), protocol = ref.protocol, hostname = ref.hostname, port = ref.port;
protocol || (protocol = 'http:');
hostname || (hostname = 'localhost');
port = port ? ":" + port : '';
return protocol + "//" + hostname + port + "/";
};
if (require.main === module) {
ref = process.argv.slice(2), upstream = ref[0], port = ref[1];
if (upstream && port) {
try {
upstream = cleanHost(upstream);
proxy(upstream).listen(port);
} catch (error1) {
error = error1;
process.stderr.write(error + "\n");
process.exit(1);
}
process.stderr.write("Proxying port " + port + " to upstream server at " + upstream + "\n");
} else {
process.stderr.write('Usage: httptrail <upstream host> <proxy port>\n');
process.exit(1);
}
}