-
Notifications
You must be signed in to change notification settings - Fork 0
/
httptrail.coffee
executable file
·92 lines (74 loc) · 2.52 KB
/
httptrail.coffee
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
#!/usr/bin/env coffee
http = require 'http'
stream = require 'stream'
urllib = require 'url'
exports.HttpStream = class HttpStream extends stream.Transform
constructor: (options) ->
super
this.prefix = options.prefix or ''
this.buffer = ''
this.on 'pipe', this.headers
headers: (request) ->
for header, value of request.headers
this.write "#{header}: #{value}\n"
this.write '\n'
format: (line) => "#{this.prefix}#{line}\n"
_transform: (chunk, encoding, done) ->
this.buffer += chunk.toString(encoding unless encoding == 'buffer')
if this.buffer.indexOf('\n') > -1
[lines..., this.buffer] = this.buffer.split '\n'
this.push lines.map(this.format).join('')
done()
_flush: (done) ->
this.push this.format this.buffer if this.buffer
done()
exports.proxy = proxy = (host) ->
host = urllib.parse cleanHost host
if host.protocol not in ['http:', 'https:']
throw new Error 'httptrail only supports http'
client = if host.protocol == 'https:' then require 'https' else http
return http.createServer (request, response) ->
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
proxied.once 'response', (upstream) ->
response.writeHead upstream.statusCode, upstream.headers
upstream.pipe response
upstream.pipe log.response
cleanHost = (host) ->
host = host.trim()
if /^\d+$/.test host
host = "http://localhost:#{host}/"
else if not /:\/\//.test host
host = host.replace /^(\/\/)?/, 'http://'
{protocol, hostname, port} = urllib.parse host
protocol ||= 'http:'
hostname ||= 'localhost'
port = if port then ":#{port}" else ''
return "#{protocol}//#{hostname}#{port}/"
if require.main == module
[upstream, port] = process.argv[2..]
if upstream and port
try
upstream = cleanHost upstream
proxy(upstream).listen(port)
catch error
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