forked from olegp/ak-httpclient
/
index.js
118 lines (103 loc) · 3.08 KB
/
index.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
ak = require('ak');
exports.HttpClient = HttpClient;
// {method, url, headers, body}
// return value is {status:status, headers:{..}, body:[..]}
function HttpClient (settings) {
if (!(this instanceof HttpClient)) return new HttpClient(settings);
this.guts = {};
if (settings) this.setOptions(settings);
};
HttpClient.prototype = {
create : function () {
this.setOptions({
"method" : "GET",
"headers" : {},
"body" : []
});
return this;
},
setOptions : function (settings) {
for (var key in settings) if (settings.hasOwnProperty(key)) {
this.setOption(key, settings[key]);
}
return this;
},
setOption : function (key, val) {
switch (key) {
case "headers":
if (typeof val !== 'object') throw new Error(
"HttpClient: headers must be a simple object."
);
return this.setHeaders(val);
case "body":
if (typeof val.forEach !== 'function') throw new Error(
"HttpClient: body must be iterable."
);
// fallthrough
default:
this.guts[key] = val;
}
return this;
},
setHeaders : function (headers) {
for (var h in headers) if (headers.hasOwnProperty(h)) {
this.setHeader(h, headers[h]);
}
return this;
},
setHeader : function (key, val) {
if (!this.guts.hasOwnProperty("headers")) this.guts.headers = {};
this.guts.headers[key] = val;
return this;
},
write : function (data) {
var len = this.guts.headers["Content-Length"] || 0;
len += data.length;
this.guts.headers["Content-Length"] = len;
this.guts.body.push(data);
return this;
},
connect : function (decode) {
//if (decode) HttpClient.decode(resp);
return this;
},
read : function() {
// {status:Integer, headers:Objecct, body:Iterable<ByteString>}
// NOTE only works with http atm
var response = ak.requestHost(this.guts.url.substr("http://".length),
{method: this.guts.method, data: (this.guts.body || []).join('')});
return {status: response.status,
headers: response.headers,
body: response.content.toString()};
},
finish : function() {
return this.connect().read();
}
};
/*
HttpClient.decode = function HttpClient_decode (resp, encoding) {
encoding = encoding || HashP.get(resp.headers, "Content-Encoding");
if (!encoding) {
var contentType = HashP.get(resp.headers, "Content-Type");
if (contentType) {
encoding = /charset=([^;\s]+)/.exec(contentType);
if (encoding) encoding = encoding[1];
}
}
// fall back on UTF-8. It's almost always a good choice.
if (!encoding) encoding = 'UTF-8';
var raw = resp.body;
resp._rawBody = raw;
resp.body = {forEach : function (block) {
raw.forEach(function (i) {
block(i.decodeToString(encoding));
});
}};
return resp;
};
HttpClient.undecode = function HttpClient_undecode (resp) {
if ("_rawBody" in resp) resp.body = resp._rawBody;
delete resp._rawBody;
return resp;
};
*/