-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.class.ts
127 lines (110 loc) · 4.69 KB
/
http.class.ts
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
const fs = require('fs');
const net = require('net');
const URL = require('url').URL;
export class HttpLibrary {
public get(verbose: boolean, redirect: boolean, headers: string[], hostStr: string, output?: string): void {
// Create connection
let urlObj = new URL(hostStr);
let socket = net.createConnection(urlObj.port ? urlObj.port : 80, urlObj.hostname);
// Parse headers into a string
let headerObj = '';
for (let i = 0; i < headers.length; ++i) {
headerObj = headerObj + headers[i];
if (i + 1 < headers.length) {
headerObj = headerObj + '\r\n';
}
}
// Overwrite host/connection headers
let requestLine = 'GET ' + hostStr + ' HTTP/1.1\r\n' +
'Host: ' + urlObj.hostname + '\r\n' +
'Connection: close\r\n' +
headerObj + '\r\n\r\n';
// Socket listeners
socket.on('data', (data) => {
// Extract status code
const statusCode = parseInt(data.toString().split('\r\n')[0].split(' ')[1]);
// Check for redirect
if (redirect && statusCode < 303 && statusCode > 299) {
for (const line of data.toString().split('\r\n').slice(1)) {
const i = line.indexOf(': ');
const k = line.substr(0, i).toLowerCase();
const v = line.substr(i + 2);
if (k.toLocaleLowerCase() === 'location') {
this.get(verbose, redirect, headers, v, output);
return;
}
}
}
// Standard output operations
if (output) {
fs.writeFileSync(output, verbose ? data.toString() : data.toString().split('\r\n\r\n')[1]);
console.log('');
console.log('Output sent to file: ' + output + '.');
} else {
console.log('');
console.log(verbose ? data.toString() : data.toString().split('\r\n\r\n')[1]);
}
}).on('connect', function() {
socket.write(requestLine);
}).on('end', function() {
socket.destroy();
}).on('error',function(error){
console.log('Error : ' + error);
});
}
public post(verbose: boolean, redirect: boolean, headers: string[], hostStr: string, file?: string, data: string = '', output?: string): void {
// Create connection
let urlObj = new URL(hostStr);
let socket = net.createConnection(urlObj.port ? urlObj.port : 80, urlObj.hostname);
// Parse headers into a string
let headerObj = '';
for (let i = 0; i < headers.length; ++i) {
headerObj = headerObj + headers[i];
if (i + 1 < headers.length) {
headerObj = headerObj + '\r\n';
}
}
// Parse file if exists
if (file) {
data = fs.readFileSync(file, "utf8");
}
// Overwrite host/connection headers
let requestLine = 'POST ' + hostStr + ' HTTP/1.1\r\n' +
'Host: ' + urlObj.hostname + '\r\n' +
'Connection: close\r\n' +
'Content-Length: ' + data.length + '\r\n' +
headerObj + '\r\n\r\n' + data;
// Socket listeners
socket.on('data', (data) => {
// Extract status code
const statusCode = parseInt(data.toString().split('\r\n')[0].split(' ')[1]);
// Check for redirect
if (redirect && statusCode < 303 && statusCode > 299) {
for (const line of data.toString().split('\r\n').slice(1)) {
const i = line.indexOf(': ');
const k = line.substr(0, i).toLowerCase();
const v = line.substr(i + 2);
if (k.toLocaleLowerCase() === 'location') {
this.post(verbose, redirect, headers, v, file, data, output);
return;
}
}
}
// Standard output operations
if (output) {
fs.writeFileSync(output, verbose ? data.toString() : data.toString().split('\r\n\r\n')[1]);
console.log('');
console.log('Output sent to file: ' + output + '.');
} else {
console.log('');
console.log(verbose ? data.toString() : data.toString().split('\r\n\r\n')[1]);
}
}).on('connect', function() {
socket.write(requestLine);
}).on('end', function() {
socket.destroy();
}).on('error',function(error){
console.log('Error : ' + error);
});
}
}