CyberChef/src/core/operations/HTTP.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

import {UAS_parser as UAParser} from "../lib/uas_parser.js";
2016-11-28 11:42:58 +01:00
/**
* HTTP operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const HTTP = {
2017-06-08 04:42:37 +02:00
/**
* @constant
* @default
*/
METHODS: [
"GET", "POST", "HEAD",
"PUT", "PATCH", "DELETE",
],
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Strip HTTP headers operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runStripHeaders: function(input, args) {
2017-04-13 19:08:50 +02:00
let headerEnd = input.indexOf("\r\n\r\n");
headerEnd = (headerEnd < 0) ? input.indexOf("\n\n") + 2 : headerEnd + 4;
2017-02-09 16:09:33 +01:00
return (headerEnd < 2) ? input : input.slice(headerEnd, input.length);
2016-11-28 11:42:58 +01:00
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Parse User Agent operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runParseUserAgent: function(input, args) {
2017-04-13 19:08:50 +02:00
const ua = UAParser.parse(input);
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01: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-08 04:42:37 +02:00
/**
* HTTP request operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runHTTPRequest(input, args) {
const method = args[0],
url = args[1],
headersText = args[2],
ignoreStatusCode = args[3];
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 = {
method,
headers,
mode: "cors",
cache: "no-cache",
};
if (method !== "GET" && method !== "HEAD") {
config.body = input;
}
return fetch(url, config)
.then(r => {
if (ignoreStatusCode || r.status === 200) {
return r.text();
} else {
throw `HTTP response code was ${r.status}.`;
}
});
},
2016-11-28 11:42:58 +01:00
};
export default HTTP;