1
0
mirror of synced 2025-01-31 20:05:20 +01:00

Added error handling and CORS support

This commit is contained in:
n1474335 2017-06-08 15:03:55 +00:00
parent a5f1c430a3
commit cbab995c6d
3 changed files with 76 additions and 15 deletions

View File

@ -126,6 +126,7 @@ const Categories = [
{ {
name: "Networking", name: "Networking",
ops: [ ops: [
"HTTP request",
"Strip HTTP headers", "Strip HTTP headers",
"Parse User Agent", "Parse User Agent",
"Parse IP range", "Parse IP range",
@ -288,6 +289,8 @@ const Categories = [
"Scan for Embedded Files", "Scan for Embedded Files",
"Generate UUID", "Generate UUID",
"Render Image", "Render Image",
"Remove EXIF",
"Extract EXIF",
"Numberwang", "Numberwang",
] ]
}, },

View File

@ -3370,7 +3370,7 @@ const OperationConfig = {
"<br><br>", "<br><br>",
"EXIF data from photos usually contains information about the image file itself as well as the device used to create it.", "EXIF data from photos usually contains information about the image file itself as well as the device used to create it.",
].join("\n"), ].join("\n"),
run: Image.runEXIF, run: Image.runExtractEXIF,
inputType: "byteArray", inputType: "byteArray",
outputType: "string", outputType: "string",
args: [], args: [],
@ -3388,9 +3388,20 @@ const OperationConfig = {
} }
] ]
}, },
"Remove EXIF": {
description: [
"Removes EXIF data from a JPEG image.",
"<br><br>",
"EXIF data embedded in photos usually contains information about the image file itself as well as the device used to create it.",
].join("\n"),
run: Image.runRemoveEXIF,
inputType: "byteArray",
outputType: "byteArray",
args: []
},
"HTTP request": { "HTTP request": {
description: [ description: [
"Makes a HTTP request and returns the response body.", "Makes an HTTP request and returns the response.",
"<br><br>", "<br><br>",
"This operation supports different HTTP verbs like GET, POST, PUT, etc.", "This operation supports different HTTP verbs like GET, POST, PUT, etc.",
"<br><br>", "<br><br>",
@ -3401,24 +3412,33 @@ const OperationConfig = {
run: HTTP.runHTTPRequest, run: HTTP.runHTTPRequest,
inputType: "string", inputType: "string",
outputType: "string", outputType: "string",
manualBake: true,
args: [ args: [
{ {
name: "Method", name: "Method",
type: "option", type: "option",
value: HTTP.METHODS, value: HTTP.METHODS,
}, { },
{
name: "URL", name: "URL",
type: "string", type: "string",
value: "", value: "",
}, { },
{
name: "Headers", name: "Headers",
type: "text", type: "text",
value: "", value: "",
}, { },
name: "Ignore status code", {
name: "Mode",
type: "option",
value: HTTP.MODE,
},
{
name: "Show response metadata",
type: "boolean", type: "boolean",
value: false, value: false,
}, }
] ]
}, },
}; };

View File

@ -11,6 +11,7 @@ import {UAS_parser as UAParser} from "../lib/uas_parser.js";
* @namespace * @namespace
*/ */
const HTTP = { const HTTP = {
/** /**
* @constant * @constant
* @default * @default
@ -18,8 +19,10 @@ const HTTP = {
METHODS: [ METHODS: [
"GET", "POST", "HEAD", "GET", "POST", "HEAD",
"PUT", "PATCH", "DELETE", "PUT", "PATCH", "DELETE",
"CONNECT", "TRACE", "OPTIONS"
], ],
/** /**
* Strip HTTP headers operation. * Strip HTTP headers operation.
* *
@ -60,9 +63,31 @@ const HTTP = {
}, },
/**
* @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",
},
/** /**
* HTTP request operation. * HTTP request operation.
* *
* @author tlwr [toby@toby.codes]
* @author n1474335 [n1474335@gmail.com]
* @param {string} input * @param {string} input
* @param {Object[]} args * @param {Object[]} args
* @returns {string} * @returns {string}
@ -71,7 +96,8 @@ const HTTP = {
const method = args[0], const method = args[0],
url = args[1], url = args[1],
headersText = args[2], headersText = args[2],
ignoreStatusCode = args[3]; mode = args[3],
showResponseMetadata = args[4];
if (url.length === 0) return ""; if (url.length === 0) return "";
@ -88,9 +114,9 @@ const HTTP = {
}); });
let config = { let config = {
method, method: method,
headers, headers: headers,
mode: "cors", mode: HTTP._modeLookup[mode],
cache: "no-cache", cache: "no-cache",
}; };
@ -100,11 +126,23 @@ const HTTP = {
return fetch(url, config) return fetch(url, config)
.then(r => { .then(r => {
if (ignoreStatusCode || r.status === 200) { if (showResponseMetadata) {
return r.text(); let headers = "";
} else { for (let pair of r.headers.entries()) {
throw `HTTP response code was ${r.status}.`; 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 cross-origin request to a server which does not support CORS\n";
}); });
}, },