-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.coffee
39 lines (35 loc) · 1.1 KB
/
net.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
# Network shim.
# Original by tcr: https://gist.github.com/1306996
https = require 'https'
qs = require 'querystring'
module.exports =
get: (host, path, query, headers, data, cb) ->
headers ||= {}
headers['Content-Length'] = data.length if data
options =
method: if data then 'POST' else 'GET'
host: host
headers: headers
path: if query then path + '?' + qs.stringify query else path
req = https.request options, (res) ->
statusCode = Number(res.statusCode)
data = ''
res.on 'data', (d) -> data += d
res.on 'end', ->
cb statusCode, data
req.end()
post: (host, path, query, headers, data, cb) ->
headers ||= {}
headers['Content-Length'] = data.length if data
options =
method: if data then 'POST' else 'GET'
host: host
headers: headers
path: if query then path + '?' + qs.stringify query else path
req = https.request options, (res) ->
statusCode = Number(res.statusCode)
data = ''
res.on 'data', (d) -> data += d
res.on 'end', ->
cb statusCode, data
req.end data