Skip to content

Commit cbab995

Browse files
committed
Added error handling and CORS support
1 parent a5f1c43 commit cbab995

File tree

3 files changed

+76
-15
lines changed

3 files changed

+76
-15
lines changed

src/core/config/Categories.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const Categories = [
126126
{
127127
name: "Networking",
128128
ops: [
129+
"HTTP request",
129130
"Strip HTTP headers",
130131
"Parse User Agent",
131132
"Parse IP range",
@@ -288,6 +289,8 @@ const Categories = [
288289
"Scan for Embedded Files",
289290
"Generate UUID",
290291
"Render Image",
292+
"Remove EXIF",
293+
"Extract EXIF",
291294
"Numberwang",
292295
]
293296
},

src/core/config/OperationConfig.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3370,7 +3370,7 @@ const OperationConfig = {
33703370
"<br><br>",
33713371
"EXIF data from photos usually contains information about the image file itself as well as the device used to create it.",
33723372
].join("\n"),
3373-
run: Image.runEXIF,
3373+
run: Image.runExtractEXIF,
33743374
inputType: "byteArray",
33753375
outputType: "string",
33763376
args: [],
@@ -3388,9 +3388,20 @@ const OperationConfig = {
33883388
}
33893389
]
33903390
},
3391+
"Remove EXIF": {
3392+
description: [
3393+
"Removes EXIF data from a JPEG image.",
3394+
"<br><br>",
3395+
"EXIF data embedded in photos usually contains information about the image file itself as well as the device used to create it.",
3396+
].join("\n"),
3397+
run: Image.runRemoveEXIF,
3398+
inputType: "byteArray",
3399+
outputType: "byteArray",
3400+
args: []
3401+
},
33913402
"HTTP request": {
33923403
description: [
3393-
"Makes a HTTP request and returns the response body.",
3404+
"Makes an HTTP request and returns the response.",
33943405
"<br><br>",
33953406
"This operation supports different HTTP verbs like GET, POST, PUT, etc.",
33963407
"<br><br>",
@@ -3401,24 +3412,33 @@ const OperationConfig = {
34013412
run: HTTP.runHTTPRequest,
34023413
inputType: "string",
34033414
outputType: "string",
3415+
manualBake: true,
34043416
args: [
34053417
{
34063418
name: "Method",
34073419
type: "option",
34083420
value: HTTP.METHODS,
3409-
}, {
3421+
},
3422+
{
34103423
name: "URL",
34113424
type: "string",
34123425
value: "",
3413-
}, {
3426+
},
3427+
{
34143428
name: "Headers",
34153429
type: "text",
34163430
value: "",
3417-
}, {
3418-
name: "Ignore status code",
3431+
},
3432+
{
3433+
name: "Mode",
3434+
type: "option",
3435+
value: HTTP.MODE,
3436+
},
3437+
{
3438+
name: "Show response metadata",
34193439
type: "boolean",
34203440
value: false,
3421-
},
3441+
}
34223442
]
34233443
},
34243444
};

src/core/operations/HTTP.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ import {UAS_parser as UAParser} from "../lib/uas_parser.js";
1111
* @namespace
1212
*/
1313
const HTTP = {
14+
1415
/**
1516
* @constant
1617
* @default
1718
*/
1819
METHODS: [
1920
"GET", "POST", "HEAD",
2021
"PUT", "PATCH", "DELETE",
22+
"CONNECT", "TRACE", "OPTIONS"
2123
],
2224

25+
2326
/**
2427
* Strip HTTP headers operation.
2528
*
@@ -60,9 +63,31 @@ const HTTP = {
6063
},
6164

6265

66+
/**
67+
* @constant
68+
* @default
69+
*/
70+
MODE: [
71+
"Cross-Origin Resource Sharing",
72+
"No CORS (limited to HEAD, GET or POST)",
73+
],
74+
75+
/**
76+
* Lookup table for HTTP modes
77+
*
78+
* @private
79+
* @constant
80+
*/
81+
_modeLookup: {
82+
"Cross-Origin Resource Sharing": "cors",
83+
"No CORS (limited to HEAD, GET or POST)": "no-cors",
84+
},
85+
6386
/**
6487
* HTTP request operation.
6588
*
89+
* @author tlwr [[email protected]]
90+
* @author n1474335 [[email protected]]
6691
* @param {string} input
6792
* @param {Object[]} args
6893
* @returns {string}
@@ -71,7 +96,8 @@ const HTTP = {
7196
const method = args[0],
7297
url = args[1],
7398
headersText = args[2],
74-
ignoreStatusCode = args[3];
99+
mode = args[3],
100+
showResponseMetadata = args[4];
75101

76102
if (url.length === 0) return "";
77103

@@ -88,9 +114,9 @@ const HTTP = {
88114
});
89115

90116
let config = {
91-
method,
92-
headers,
93-
mode: "cors",
117+
method: method,
118+
headers: headers,
119+
mode: HTTP._modeLookup[mode],
94120
cache: "no-cache",
95121
};
96122

@@ -100,11 +126,23 @@ const HTTP = {
100126

101127
return fetch(url, config)
102128
.then(r => {
103-
if (ignoreStatusCode || r.status === 200) {
104-
return r.text();
105-
} else {
106-
throw `HTTP response code was ${r.status}.`;
129+
if (showResponseMetadata) {
130+
let headers = "";
131+
for (let pair of r.headers.entries()) {
132+
headers += " " + pair[0] + ": " + pair[1] + "\n";
133+
}
134+
return r.text().then(b => {
135+
return "####\n Status: " + r.status + " " + r.statusText +
136+
"\n Exposed headers:\n" + headers + "####\n\n" + b;
137+
});
107138
}
139+
return r.text();
140+
})
141+
.catch(e => {
142+
return e.toString() +
143+
"\n\nThis error could be caused by one of the following:\n" +
144+
" - An invalid URL\n" +
145+
" - Making a cross-origin request to a server which does not support CORS\n";
108146
});
109147
},
110148

0 commit comments

Comments
 (0)