-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
40 lines (36 loc) · 1.36 KB
/
request.js
File metadata and controls
40 lines (36 loc) · 1.36 KB
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
const Util = require('./util');
const fs = require('fs');
exports.getRequestObject = (url, args) => {
const url_args = Util.getURLProperties(url);
const request = {
method: args.method,
h: args.h,
args: url_args
};
if (args.hasOwnProperty('d')) request.d = args.d;
else if (args.hasOwnProperty('f')) request.f = args.f;
return request;
};
exports.createHTTPRequest = (request) => {
let http_request;
if (request.method === GET_CONSTANT) {
http_request = 'GET ';
} else if (request.method === POST_CONSTANT) {
http_request = 'POST ';
}
http_request += (request.args.hasOwnProperty('pathname') ? request.args.pathname : '/')
+ (!Util.isEmpty(request.args.query) ? ('?' + request.args.href.toString().split('?')[1]) : '')
+ ' HTTP/1.0\r\nHost: ' + request.args.host + `\r\nUser-Agent: ${USER_AGENT}\r\n`;
if (request.h.length > 0) {
request.h.forEach(value => {
http_request += (value + '\r\n');
})
}
if (request.hasOwnProperty('d') || request.hasOwnProperty('f')) {
const body = (request.hasOwnProperty('d') ? request.d : fs.readFileSync(request.f, 'utf8').trim());
http_request += ('Content-Length: ' + body.length + '\r\n\r\n');
http_request += (body + '\r\n');
}
http_request += '\r\n';
return http_request;
};