Add XML support to HTTP monitors

This commit is contained in:
Berkay Bayram 2022-12-28 22:32:07 +03:00
parent 692103bb2b
commit 589487d575
6 changed files with 36 additions and 3 deletions

View file

@ -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;

View file

@ -66,6 +66,7 @@ class Database {
"patch-add-radius-monitor.sql": true, "patch-add-radius-monitor.sql": true,
"patch-monitor-add-resend-interval.sql": true, "patch-monitor-add-resend-interval.sql": true,
"patch-maintenance-table2.sql": true, "patch-maintenance-table2.sql": true,
"patch-http-monitor-add-body-encoding.sql": true,
}; };
/** /**

View file

@ -107,6 +107,7 @@ class Monitor extends BeanModel {
grpcEnableTls: this.getGrpcEnableTls(), grpcEnableTls: this.getGrpcEnableTls(),
radiusCalledStationId: this.radiusCalledStationId, radiusCalledStationId: this.radiusCalledStationId,
radiusCallingStationId: this.radiusCallingStationId, radiusCallingStationId: this.radiusCallingStationId,
httpBodyEncoding: this.httpBodyEncoding,
}; };
if (includeSensitiveData) { if (includeSensitiveData) {
@ -268,17 +269,29 @@ class Monitor extends BeanModel {
log.debug("monitor", `[${this.name}] Prepare Options for axios`); 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 // Axios Options
const options = { const options = {
url: this.url, url: this.url,
method: (this.method || "get").toLowerCase(), method: (this.method || "get").toLowerCase(),
...(this.body ? { data: JSON.parse(this.body) } : {}), ...(bodyValue ? { data: bodyValue } : {}),
timeout: this.interval * 1000 * 0.8, timeout: this.interval * 1000 * 0.8,
headers: { 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", "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, "User-Agent": "Uptime-Kuma/" + version,
...(this.headers ? JSON.parse(this.headers) : {}), ...(this.headers ? JSON.parse(this.headers) : {}),
...(basicAuthHeader), ...(basicAuthHeader),
...(contentType ? { "Content-Type": contentType } : {})
}, },
maxRedirects: this.maxredirects, maxRedirects: this.maxredirects,
validateStatus: (status) => { validateStatus: (status) => {

View file

@ -723,6 +723,7 @@ let needSetup = false;
bean.radiusCalledStationId = monitor.radiusCalledStationId; bean.radiusCalledStationId = monitor.radiusCalledStationId;
bean.radiusCallingStationId = monitor.radiusCallingStationId; bean.radiusCallingStationId = monitor.radiusCallingStationId;
bean.radiusSecret = monitor.radiusSecret; bean.radiusSecret = monitor.radiusSecret;
bean.httpBodyEncoding = monitor.httpBodyEncoding;
bean.validate(); bean.validate();

View file

@ -669,4 +669,5 @@ export default {
"General Monitor Type": "General Monitor Type", "General Monitor Type": "General Monitor Type",
"Passive Monitor Type": "Passive Monitor Type", "Passive Monitor Type": "Passive Monitor Type",
"Specific Monitor Type": "Specific Monitor Type", "Specific Monitor Type": "Specific Monitor Type",
"Body Encoding": "Body Encoding",
}; };

View file

@ -453,6 +453,15 @@
</select> </select>
</div> </div>
<!-- Encoding -->
<div class="my-3">
<label for="httpBodyEncoding" class="form-label">{{ $t("Body Encoding") }}</label>
<select id="httpBodyEncoding" v-model="monitor.httpBodyEncoding" class="form-select">
<option value="json">JSON</option>
<option value="xml">XML</option>
</select>
</div>
<!-- Body --> <!-- Body -->
<div class="my-3"> <div class="my-3">
<label for="body" class="form-label">{{ $t("Body") }}</label> <label for="body" class="form-label">{{ $t("Body") }}</label>
@ -789,6 +798,7 @@ message HealthCheckResponse {
mqttTopic: "", mqttTopic: "",
mqttSuccessMessage: "", mqttSuccessMessage: "",
authMethod: null, authMethod: null,
httpBodyEncoding: "json",
}; };
if (this.$root.proxyList && !this.monitor.proxyId) { if (this.$root.proxyList && !this.monitor.proxyId) {
@ -826,7 +836,7 @@ message HealthCheckResponse {
* @returns {boolean} Is the form input valid? * @returns {boolean} Is the form input valid?
*/ */
isInputValid() { isInputValid() {
if (this.monitor.body) { if (this.monitor.body && (!this.monitor.httpBodyEncoding || this.monitor.httpBodyEncoding === "json")) {
try { try {
JSON.parse(this.monitor.body); JSON.parse(this.monitor.body);
} catch (err) { } catch (err) {
@ -858,7 +868,7 @@ message HealthCheckResponse {
} }
// Beautify the JSON format // 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); this.monitor.body = JSON.stringify(JSON.parse(this.monitor.body), null, 4);
} }