fix(edit-monitor): store headers as JSON

This commit is contained in:
Bert Verhelst 2021-10-09 12:42:32 +02:00
parent fba4f86552
commit d71d27220b
4 changed files with 9 additions and 32 deletions

View File

@ -99,31 +99,6 @@ class Monitor extends BeanModel {
return JSON.parse(this.accepted_statuscodes_json);
}
/**
* Convert header string into an object:
* eg:
*
* Authorization: Basic <credentials>
* Content-Type: application/json
*
* into
*
* {
* "Authorization": "Basic <credentials>",
* "Content-Type": "application/json"
* }
**/
getParsedHeaders() {
if (!this.headers || !this.headers.includes(":")) {
return {};
}
return Object.fromEntries(this.headers.split("\n").map(header => {
const trimmedHeader = header.trim();
const firstColonIndex = trimmedHeader.indexOf(":");
return [trimmedHeader.slice(0, firstColonIndex), trimmedHeader.slice(firstColonIndex + 1) || ""];
}).filter(arr => !!arr[0] && !!arr[1]));
}
start(io) {
let previousBeat = null;
let retries = 0;
@ -173,7 +148,7 @@ class Monitor extends BeanModel {
headers: {
"Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version,
...this.getParsedHeaders(),
...JSON.parse(this.headers),
},
httpsAgent: new https.Agent({
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940)

View File

@ -285,8 +285,8 @@ export default {
Body: "Body",
Headers: "Headers",
PushUrl: "Push URL",
HeadersInvalidFormat: "Headers do not have a valid format: \"key: value <new line> key: value <new line>...\"",
HeadersInvalidFormat: "The request headers are not valid JSON: ",
BodyInvalidFormat: "The request body is not valid JSON: ",
BodyPlaceholder: "&lbrace;\n\t\"id\": 124357,\n\t\"username\": \"admin\",\n\t\"password\": \"myAdminPassword\"\n&rbrace;",
HeadersPlaceholder: "Authorization: Bearer abc123\nContent-Type: application/json",
HeadersPlaceholder: "&lbrace;\n\t\"Authorization\": \"Bearer abc123\",\n\t\"Content-Type\": \"application/json\"\n&rbrace;",
};

View File

@ -202,8 +202,8 @@ export default {
Body: "Body",
Headers: "Headers",
PushUrl: "Push URL",
HeadersInvalidFormat: "Headers hebben een incorrect formaat: \"key: waarde <new line> key: waarde <new line>...\"",
HeadersInvalidFormat: "The request headers is geen geldige JSON: ",
BodyInvalidFormat: "De request body is geen geldige JSON: ",
BodyPlaceholder: "&lbrace;\n\t\"id\": 124357,\n\t\"gebruikersnaam\": \"admin\",\n\t\"wachtwoord\": \"mijnAdminWachtwoord\"\n&rbrace;",
HeadersPlaceholder: "Authorization: Bearer abc123\nContent-Type: application/json",
HeadersPlaceholder: "&lbrace;\n\t\"Authorization\": \"Bearer abc123\",\n\t\"Content-Type\": \"application/json\"\n&rbrace;",
};

View File

@ -442,8 +442,10 @@ export default {
}
}
if (this.monitor.headers) {
if (!/^([^:]+:.*)([\s]+[^:]+:.*)*$/g.test(this.monitor.headers)) {
toast.error(this.$t("HeadersInvalidFormat"));
try {
JSON.parse(this.monitor.headers);
} catch (err) {
toast.error(this.$t("HeadersInvalidFormat") + err.message);
return false;
}
}