2021-10-02 08:07:01 +00:00
|
|
|
import http, { RequestOptions } from 'http';
|
2020-11-28 19:08:23 +00:00
|
|
|
import https from 'https';
|
|
|
|
import fs from 'fs-extra';
|
2021-10-02 08:07:01 +00:00
|
|
|
import { URL } from 'url';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an HTTP GET request.
|
|
|
|
* @param link Request URL
|
|
|
|
* @param headers Request headers
|
|
|
|
* @param restrictToText Only allow textual responses
|
|
|
|
* @param saveTo Save to a file
|
|
|
|
* @returns Response data
|
|
|
|
*/
|
2020-11-28 19:08:23 +00:00
|
|
|
export function httpGET(
|
|
|
|
link: string,
|
|
|
|
headers: any = {},
|
|
|
|
restrictToText = true,
|
|
|
|
saveTo?: string,
|
2021-10-02 08:07:01 +00:00
|
|
|
lback?: number
|
|
|
|
): Promise<any> {
|
|
|
|
const parsed = new URL(link);
|
2020-11-28 19:08:23 +00:00
|
|
|
const opts = {
|
|
|
|
headers: {
|
|
|
|
Accept: '*/*',
|
|
|
|
'Accept-Language': 'en-US',
|
|
|
|
'User-Agent': 'Squeebot/Commons-3.0.0',
|
|
|
|
},
|
|
|
|
host: parsed.hostname,
|
2021-10-02 08:07:01 +00:00
|
|
|
path: `${parsed.pathname}${parsed.search}`,
|
|
|
|
port: parsed.port || null,
|
2020-11-28 19:08:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (headers) {
|
|
|
|
opts.headers = Object.assign(opts.headers, headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
let reqTimeOut: any;
|
|
|
|
let data: string | null = '';
|
|
|
|
|
|
|
|
const httpModule = parsed.protocol === 'https:' ? https : http;
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-18 13:21:53 +00:00
|
|
|
if (lback && lback >= 5) {
|
|
|
|
return reject(new Error('infinite loop!'));
|
|
|
|
}
|
|
|
|
|
2020-11-28 19:08:23 +00:00
|
|
|
const req = httpModule.get(opts, (res) => {
|
|
|
|
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
|
|
if (!lback) {
|
|
|
|
lback = 1;
|
|
|
|
} else {
|
|
|
|
lback += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return httpGET(res.headers.location as string,
|
|
|
|
headers,
|
|
|
|
restrictToText,
|
|
|
|
saveTo,
|
|
|
|
lback,
|
|
|
|
).then(resolve, reject);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (saveTo) {
|
|
|
|
const file = fs.createWriteStream(saveTo);
|
|
|
|
res.pipe(file);
|
|
|
|
file.on('close', () => resolve(saveTo));
|
|
|
|
file.on('error', (e) => reject(e));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restrictToText) {
|
|
|
|
const cType = res.headers['content-type'];
|
|
|
|
if (cType && cType.indexOf('text') === -1 && cType.indexOf('json') === -1) {
|
2021-10-02 08:07:01 +00:00
|
|
|
req.destroy();
|
2020-11-28 19:08:23 +00:00
|
|
|
return reject(new Error('Response type is not supported by httpGET!'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reqTimeOut = setTimeout(() => {
|
2021-10-02 08:07:01 +00:00
|
|
|
req.destroy();
|
2020-11-28 19:08:23 +00:00
|
|
|
data = null;
|
|
|
|
reject(new Error('Request took too long!'));
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
res.on('data', (chunk) => {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on('end', () => {
|
|
|
|
clearTimeout(reqTimeOut);
|
|
|
|
|
|
|
|
resolve(data || saveTo);
|
|
|
|
});
|
|
|
|
}).on('error', (e) => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
req.setTimeout(10000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:07:01 +00:00
|
|
|
/**
|
|
|
|
* Create an HTTP POST request.
|
|
|
|
*
|
|
|
|
* Note: Content-Type defaults to `application/x-www-form-urlencoded`.
|
|
|
|
* Set the `Content-Type` to `application/json` to post and parse JSON.
|
|
|
|
* @param link Request URL
|
|
|
|
* @param headers Request headers
|
|
|
|
* @param data Submit data
|
|
|
|
* @returns Response data
|
|
|
|
*/
|
2020-11-28 19:08:23 +00:00
|
|
|
export function httpPOST(
|
|
|
|
link: string,
|
|
|
|
headers: any = {},
|
2021-10-02 08:07:01 +00:00
|
|
|
data: any
|
|
|
|
): Promise<any> {
|
|
|
|
const parsed = new URL(link);
|
|
|
|
let postData: string | URLSearchParams = new URLSearchParams(data);
|
2020-11-28 19:08:23 +00:00
|
|
|
|
2021-10-02 08:07:01 +00:00
|
|
|
const opts: RequestOptions = {
|
2020-11-28 19:08:23 +00:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'User-Agent': 'Squeebot/Commons-3.0.0',
|
|
|
|
},
|
|
|
|
host: parsed.host,
|
|
|
|
method: 'POST',
|
2021-10-02 08:07:01 +00:00
|
|
|
path: `${parsed.pathname}${parsed.search}`,
|
|
|
|
port: parsed.port || null,
|
2020-11-28 19:08:23 +00:00
|
|
|
};
|
|
|
|
|
2021-10-02 08:07:01 +00:00
|
|
|
// Assign provided headers
|
2020-11-28 19:08:23 +00:00
|
|
|
if (headers) {
|
2021-10-02 08:07:01 +00:00
|
|
|
opts.headers = Object.assign({}, opts.headers, headers);
|
2020-11-28 19:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 08:07:01 +00:00
|
|
|
// Ensure headers list exists
|
|
|
|
if (!opts.headers) {
|
|
|
|
opts.headers = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If content type is JSON, add it to body
|
2020-11-28 19:08:23 +00:00
|
|
|
if (opts.headers['Content-Type'] === 'application/json') {
|
|
|
|
postData = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:07:01 +00:00
|
|
|
// Set content length accordingly
|
|
|
|
opts.headers['Content-Length'] = Buffer.byteLength(postData.toString());
|
|
|
|
|
2020-11-28 19:08:23 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const httpModule = parsed.protocol === 'https:' ? https : http;
|
|
|
|
const req = httpModule.request(opts, (res) => {
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
let resp = '';
|
|
|
|
|
|
|
|
res.on('data', (chunk) => {
|
|
|
|
resp += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on('end', () => {
|
|
|
|
resolve(resp);
|
|
|
|
});
|
|
|
|
}).on('error', (e) => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
req.write(postData);
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
}
|