From d71d27220be2bc9832f8a5860172bce32ca9191c Mon Sep 17 00:00:00 2001 From: Bert Verhelst Date: Sat, 9 Oct 2021 12:42:32 +0200 Subject: [PATCH] fix(edit-monitor): store headers as JSON --- server/model/monitor.js | 27 +-------------------------- src/languages/en.js | 4 ++-- src/languages/nl-NL.js | 4 ++-- src/pages/EditMonitor.vue | 6 ++++-- 4 files changed, 9 insertions(+), 32 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 33ad82f0b..2738898fd 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -99,31 +99,6 @@ class Monitor extends BeanModel { return JSON.parse(this.accepted_statuscodes_json); } - /** - * Convert header string into an object: - * eg: - * - * Authorization: Basic - * Content-Type: application/json - * - * into - * - * { - * "Authorization": "Basic ", - * "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) diff --git a/src/languages/en.js b/src/languages/en.js index 7871f0fe7..a5ad24e05 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -285,8 +285,8 @@ export default { Body: "Body", Headers: "Headers", PushUrl: "Push URL", - HeadersInvalidFormat: "Headers do not have a valid format: \"key: value key: value ...\"", + HeadersInvalidFormat: "The request headers are not valid JSON: ", BodyInvalidFormat: "The request body is not valid JSON: ", BodyPlaceholder: "{\n\t\"id\": 124357,\n\t\"username\": \"admin\",\n\t\"password\": \"myAdminPassword\"\n}", - HeadersPlaceholder: "Authorization: Bearer abc123\nContent-Type: application/json", + HeadersPlaceholder: "{\n\t\"Authorization\": \"Bearer abc123\",\n\t\"Content-Type\": \"application/json\"\n}", }; diff --git a/src/languages/nl-NL.js b/src/languages/nl-NL.js index 33d84cd5a..170b4a3ce 100644 --- a/src/languages/nl-NL.js +++ b/src/languages/nl-NL.js @@ -202,8 +202,8 @@ export default { Body: "Body", Headers: "Headers", PushUrl: "Push URL", - HeadersInvalidFormat: "Headers hebben een incorrect formaat: \"key: waarde key: waarde ...\"", + HeadersInvalidFormat: "The request headers is geen geldige JSON: ", BodyInvalidFormat: "De request body is geen geldige JSON: ", BodyPlaceholder: "{\n\t\"id\": 124357,\n\t\"gebruikersnaam\": \"admin\",\n\t\"wachtwoord\": \"mijnAdminWachtwoord\"\n}", - HeadersPlaceholder: "Authorization: Bearer abc123\nContent-Type: application/json", + HeadersPlaceholder: "{\n\t\"Authorization\": \"Bearer abc123\",\n\t\"Content-Type\": \"application/json\"\n}", }; diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 5074dab95..ed8b359e4 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -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; } }