forked from HeiSir2014/M3U8-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPPlus.js
140 lines (126 loc) · 3.83 KB
/
HTTPPlus.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var http = require('http'),
https = require('https'),
fs = require('fs'),
url = require('url');
function url_to_option(urlstr,method,postData,headerSend) {
// body...
if (urlstr === null) {
urlstr = '';
}
var Url = url.parse(urlstr);
var retOpt = {hostname:Url.hostname,port:Url.port,path:Url.path,method:(method === null ? 'GET':method)};
if ( method === 'POST' ) {
var header = retOpt['headers'];
if ( typeof(header) === 'undefined' ) {
header = {};
}
header['Content-Type'] = 'application/x-www-form-urlencoded';
header['Content-Length'] = Buffer.byteLength(postData);
retOpt['headers'] = header;
}
if ( headerSend != null ) {
if( typeof(retOpt['headers']) === 'undefined' ){
retOpt['headers'] = {};
}
for (var key in headerSend) {
if ('host' != key) {
retOpt['headers'][key] = headerSend[key];
}
}
}
return retOpt;
}
function httpSend(urlstr,method,postData,header,callback) {
// body...
var http__ = http;
if(urlstr.indexOf('https') == 0)
{
http__ = https;
}
var req = http__.request(url_to_option(urlstr,method,postData,header), function(res){
var responseContent = '';
res.setEncoding('utf8');
res.on('data', function(chunk){
responseContent += chunk;
});
res.on('end', function() {
//console.log('response = ' + responseContent);
if(callback != null){
callback(responseContent,null);
}
});
res.on('error',function(err){
if(callback != null){
callback(null,err);
}
})
});
req.on('error', function(e) {
if(callback != null){
callback(null,e.message);
}
});
if (method === 'POST' && postData != null && postData !='') {
req.write(postData);
}
req.end();
}
function downloadFile(urlstr,filePath,method,postData,header,callback,process_callback) {
// body...
var http__ = http;
if(urlstr.indexOf('https') == 0)
{
http__ = https;
}
var req = http__.request(url_to_option(urlstr,method,postData,header), function(res){
let stream = fs.createWriteStream(filePath);
res.pipe(stream);
res.on('close',function(){
if(callback != null){
callback('success',null);
}
});
});
req.on('error', function(e) {
if(callback != null){
callback(null,e.message);
}
});
if (method === 'POST' && postData != null && postData !='') {
req.write(postData);
}
req.end();
}
function downloadFileSync(urlstr,filePath,method,postData,header,callback,process_callback) {
return new Promise(function(resolve, reject){
// body...
var http__ = http;
if(urlstr.indexOf('https') == 0)
{
http__ = https;
}
var req = http__.request(url_to_option(urlstr,method,postData,header), function(res){
let stream = fs.createWriteStream(filePath);
res.pipe(stream);
res.on('close',function(){
if(callback != null){
callback('success',null);
resolve('success');
}
});
});
req.on('error', function(e) {
if(callback != null){
callback(null,e.message);
reject(e.message);
}
});
if (method === 'POST' && postData != null && postData !='') {
req.write(postData);
}
req.end();
});
}
exports.httpSend = httpSend;
exports.downloadFile = downloadFile;
exports.downloadFileSync = downloadFileSync;