From 589487d57534ee08593bcc3081d77b420976fe75 Mon Sep 17 00:00:00 2001 From: Berkay Bayram Date: Wed, 28 Dec 2022 22:32:07 +0300 Subject: [PATCH] Add XML support to HTTP monitors --- db/patch-http-monitor-add-body-encoding.sql | 7 +++++++ server/database.js | 1 + server/model/monitor.js | 15 ++++++++++++++- server/server.js | 1 + src/languages/en.js | 1 + src/pages/EditMonitor.vue | 14 ++++++++++++-- 6 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 db/patch-http-monitor-add-body-encoding.sql diff --git a/db/patch-http-monitor-add-body-encoding.sql b/db/patch-http-monitor-add-body-encoding.sql new file mode 100644 index 000000000..287db3e96 --- /dev/null +++ b/db/patch-http-monitor-add-body-encoding.sql @@ -0,0 +1,7 @@ +-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. +BEGIN TRANSACTION; + +ALTER TABLE monitor + ADD http_body_encoding TEXT default null; + +COMMIT; diff --git a/server/database.js b/server/database.js index 2544f1972..ff41f8351 100644 --- a/server/database.js +++ b/server/database.js @@ -66,6 +66,7 @@ class Database { "patch-add-radius-monitor.sql": true, "patch-monitor-add-resend-interval.sql": true, "patch-maintenance-table2.sql": true, + "patch-http-monitor-add-body-encoding.sql": true, }; /** diff --git a/server/model/monitor.js b/server/model/monitor.js index 186962b08..a0a692dc2 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -107,6 +107,7 @@ class Monitor extends BeanModel { grpcEnableTls: this.getGrpcEnableTls(), radiusCalledStationId: this.radiusCalledStationId, radiusCallingStationId: this.radiusCallingStationId, + httpBodyEncoding: this.httpBodyEncoding, }; if (includeSensitiveData) { @@ -268,17 +269,29 @@ class Monitor extends BeanModel { log.debug("monitor", `[${this.name}] Prepare Options for axios`); + let contentType = null; + let bodyValue = null; + + if (this.body && !this.httpBodyEncoding || this.httpBodyEncoding === "json") { + bodyValue = JSON.parse(this.body); + contentType = "application/json"; + } else if (this.body && (this.httpBodyEncoding === "xml")) { + bodyValue = this.body; + contentType = "text/xml; charset=utf-8"; + } + // Axios Options const options = { url: this.url, method: (this.method || "get").toLowerCase(), - ...(this.body ? { data: JSON.parse(this.body) } : {}), + ...(bodyValue ? { data: bodyValue } : {}), timeout: this.interval * 1000 * 0.8, headers: { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "User-Agent": "Uptime-Kuma/" + version, ...(this.headers ? JSON.parse(this.headers) : {}), ...(basicAuthHeader), + ...(contentType ? { "Content-Type": contentType } : {}) }, maxRedirects: this.maxredirects, validateStatus: (status) => { diff --git a/server/server.js b/server/server.js index 594c29b31..e482aed75 100644 --- a/server/server.js +++ b/server/server.js @@ -723,6 +723,7 @@ let needSetup = false; bean.radiusCalledStationId = monitor.radiusCalledStationId; bean.radiusCallingStationId = monitor.radiusCallingStationId; bean.radiusSecret = monitor.radiusSecret; + bean.httpBodyEncoding = monitor.httpBodyEncoding; bean.validate(); diff --git a/src/languages/en.js b/src/languages/en.js index e760f92ea..fa914db66 100644 --- a/src/languages/en.js +++ b/src/languages/en.js @@ -669,4 +669,5 @@ export default { "General Monitor Type": "General Monitor Type", "Passive Monitor Type": "Passive Monitor Type", "Specific Monitor Type": "Specific Monitor Type", + "Body Encoding": "Body Encoding", }; diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index c9d5ad2f1..bcb416642 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -453,6 +453,15 @@ + +
+ + +
+
@@ -789,6 +798,7 @@ message HealthCheckResponse { mqttTopic: "", mqttSuccessMessage: "", authMethod: null, + httpBodyEncoding: "json", }; if (this.$root.proxyList && !this.monitor.proxyId) { @@ -826,7 +836,7 @@ message HealthCheckResponse { * @returns {boolean} Is the form input valid? */ isInputValid() { - if (this.monitor.body) { + if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) { try { JSON.parse(this.monitor.body); } catch (err) { @@ -858,7 +868,7 @@ message HealthCheckResponse { } // Beautify the JSON format - if (this.monitor.body) { + if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) { this.monitor.body = JSON.stringify(JSON.parse(this.monitor.body), null, 4); }