2017-03-23 17:52:20 +00:00
|
|
|
import {UAS_parser as UAParser} from "../lib/uas_parser.js";
|
2017-03-06 12:45:51 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP operations.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2017-03-23 17:52:20 +00:00
|
|
|
const HTTP = {
|
2017-06-08 15:03:55 +00:00
|
|
|
|
2017-06-07 22:42:37 -04:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
METHODS: [
|
|
|
|
"GET", "POST", "HEAD",
|
|
|
|
"PUT", "PATCH", "DELETE",
|
2017-06-08 15:03:55 +00:00
|
|
|
"CONNECT", "TRACE", "OPTIONS"
|
2017-06-07 22:42:37 -04:00
|
|
|
],
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-06-08 15:03:55 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Strip HTTP headers operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runStripHeaders: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
let headerEnd = input.indexOf("\r\n\r\n");
|
2017-03-09 17:16:47 +00:00
|
|
|
headerEnd = (headerEnd < 0) ? input.indexOf("\n\n") + 2 : headerEnd + 4;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
return (headerEnd < 2) ? input : input.slice(headerEnd, input.length);
|
2016-11-28 10:42:58 +00:00
|
|
|
},
|
2017-02-09 15:09:33 +00:00
|
|
|
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Parse User Agent operation.
|
|
|
|
*
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
runParseUserAgent: function(input, args) {
|
2017-04-13 18:08:50 +01:00
|
|
|
const ua = UAParser.parse(input);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
return "Type: " + ua.type + "\n" +
|
|
|
|
"Family: " + ua.uaFamily + "\n" +
|
|
|
|
"Name: " + ua.uaName + "\n" +
|
|
|
|
"URL: " + ua.uaUrl + "\n" +
|
|
|
|
"Company: " + ua.uaCompany + "\n" +
|
|
|
|
"Company URL: " + ua.uaCompanyUrl + "\n\n" +
|
|
|
|
"OS Family: " + ua.osFamily + "\n" +
|
|
|
|
"OS Name: " + ua.osName + "\n" +
|
|
|
|
"OS URL: " + ua.osUrl + "\n" +
|
|
|
|
"OS Company: " + ua.osCompany + "\n" +
|
|
|
|
"OS Company URL: " + ua.osCompanyUrl + "\n" +
|
|
|
|
"Device Type: " + ua.deviceType + "\n";
|
|
|
|
},
|
|
|
|
|
2017-06-07 22:42:37 -04:00
|
|
|
|
2017-06-08 15:03:55 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
MODE: [
|
|
|
|
"Cross-Origin Resource Sharing",
|
|
|
|
"No CORS (limited to HEAD, GET or POST)",
|
|
|
|
],
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lookup table for HTTP modes
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @constant
|
|
|
|
*/
|
|
|
|
_modeLookup: {
|
|
|
|
"Cross-Origin Resource Sharing": "cors",
|
|
|
|
"No CORS (limited to HEAD, GET or POST)": "no-cors",
|
|
|
|
},
|
|
|
|
|
2017-06-07 22:42:37 -04:00
|
|
|
/**
|
|
|
|
* HTTP request operation.
|
|
|
|
*
|
2017-06-08 15:03:55 +00:00
|
|
|
* @author tlwr [toby@toby.codes]
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
2017-06-07 22:42:37 -04:00
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
runHTTPRequest(input, args) {
|
|
|
|
const method = args[0],
|
|
|
|
url = args[1],
|
|
|
|
headersText = args[2],
|
2017-06-08 15:03:55 +00:00
|
|
|
mode = args[3],
|
|
|
|
showResponseMetadata = args[4];
|
2017-06-07 22:42:37 -04:00
|
|
|
|
|
|
|
if (url.length === 0) return "";
|
|
|
|
|
|
|
|
let headers = new Headers();
|
|
|
|
headersText.split(/\r?\n/).forEach(line => {
|
|
|
|
line = line.trim();
|
|
|
|
|
|
|
|
if (line.length === 0) return;
|
|
|
|
|
|
|
|
let split = line.split(":");
|
|
|
|
if (split.length !== 2) throw `Could not parse header in line: ${line}`;
|
|
|
|
|
|
|
|
headers.set(split[0].trim(), split[1].trim());
|
|
|
|
});
|
|
|
|
|
|
|
|
let config = {
|
2017-06-08 15:03:55 +00:00
|
|
|
method: method,
|
|
|
|
headers: headers,
|
|
|
|
mode: HTTP._modeLookup[mode],
|
2017-06-07 22:42:37 -04:00
|
|
|
cache: "no-cache",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (method !== "GET" && method !== "HEAD") {
|
|
|
|
config.body = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetch(url, config)
|
2017-07-24 13:49:16 +00:00
|
|
|
.then(r => {
|
|
|
|
if (r.status === 0 && r.type === "opaque") {
|
|
|
|
return "Error: Null response. Try setting the connection mode to CORS.";
|
2017-06-08 15:03:55 +00:00
|
|
|
}
|
2017-07-24 13:49:16 +00:00
|
|
|
|
|
|
|
if (showResponseMetadata) {
|
|
|
|
let headers = "";
|
|
|
|
for (let pair of r.headers.entries()) {
|
|
|
|
headers += " " + pair[0] + ": " + pair[1] + "\n";
|
|
|
|
}
|
|
|
|
return r.text().then(b => {
|
|
|
|
return "####\n Status: " + r.status + " " + r.statusText +
|
|
|
|
"\n Exposed headers:\n" + headers + "####\n\n" + b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return r.text();
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return e.toString() +
|
|
|
|
"\n\nThis error could be caused by one of the following:\n" +
|
|
|
|
" - An invalid URL\n" +
|
|
|
|
" - Making a request to an insecure resource (HTTP) from a secure source (HTTPS)\n" +
|
|
|
|
" - Making a cross-origin request to a server which does not support CORS\n";
|
|
|
|
});
|
2017-06-07 22:42:37 -04:00
|
|
|
},
|
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
|
|
|
export default HTTP;
|