From f89ed0a3a44b7d8dad2b98c9e7a0163bbd93d7ca Mon Sep 17 00:00:00 2001
From: Stefan Ottosson <stefan@ottosson.io>
Date: Fri, 12 Aug 2022 18:43:24 +0200
Subject: [PATCH] feat: added HTTP method option for push monitor

---
 server/routers/api-router.js |  7 ++-
 src/pages/EditMonitor.vue    | 84 ++++++++++++++++++------------------
 2 files changed, 48 insertions(+), 43 deletions(-)

diff --git a/server/routers/api-router.js b/server/routers/api-router.js
index 201efc41f..bba789ac5 100644
--- a/server/routers/api-router.js
+++ b/server/routers/api-router.js
@@ -31,9 +31,8 @@ router.get("/api/entry-page", async (request, response) => {
     response.json(result);
 });
 
-router.get("/api/push/:pushToken", async (request, response) => {
+router.all("/api/push/:pushToken", async (request, response) => {
     try {
-
         let pushToken = request.params.pushToken;
         let msg = request.query.msg || "OK";
         let ping = request.query.ping || null;
@@ -48,6 +47,10 @@ router.get("/api/push/:pushToken", async (request, response) => {
             throw new Error("Monitor not found or not active.");
         }
 
+        if (monitor.method !== request.method) {
+            throw new Error("Monitor HTTP method (" + monitor.method + ") does not match request (" + request.method + ").");
+        }
+
         const previousHeartbeat = await Monitor.getPreviousHeartbeat(monitor.id);
 
         if (monitor.isUpsideDown()) {
diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue
index 82859a4ff..d82aa2800 100644
--- a/src/pages/EditMonitor.vue
+++ b/src/pages/EditMonitor.vue
@@ -398,7 +398,7 @@
                             </div>
 
                             <!-- HTTP Options -->
-                            <template v-if="monitor.type === 'http' || monitor.type === 'keyword' ">
+                            <template v-if="monitor.type === 'http' || monitor.type === 'keyword' || monitor.type === 'push'">
                                 <h2 class="mt-5 mb-2">{{ $t("HTTP Options") }}</h2>
 
                                 <!-- Method -->
@@ -429,56 +429,58 @@
                                     </select>
                                 </div>
 
-                                <!-- Body -->
-                                <div class="my-3">
-                                    <label for="body" class="form-label">{{ $t("Body") }}</label>
-                                    <textarea id="body" v-model="monitor.body" class="form-control" :placeholder="bodyPlaceholder"></textarea>
-                                </div>
-
-                                <!-- Headers -->
-                                <div class="my-3">
-                                    <label for="headers" class="form-label">{{ $t("Headers") }}</label>
-                                    <textarea id="headers" v-model="monitor.headers" class="form-control" :placeholder="headersPlaceholder"></textarea>
-                                </div>
-
-                                <!-- HTTP Auth -->
-                                <h4 class="mt-5 mb-2">{{ $t("Authentication") }}</h4>
-
-                                <!-- Method -->
-                                <div class="my-3">
-                                    <label for="method" class="form-label">{{ $t("Method") }}</label>
-                                    <select id="method" v-model="monitor.authMethod" class="form-select">
-                                        <option :value="null">
-                                            {{ $t("None") }}
-                                        </option>
-                                        <option value="basic">
-                                            {{ $t("HTTP Basic Auth") }}
-                                        </option>
-                                        <option value="ntlm">
-                                            NTLM
-                                        </option>
-                                    </select>
-                                </div>
-                                <template v-if="monitor.authMethod && monitor.authMethod !== null ">
+                                <template v-if="monitor.type !== 'push'">
+                                    <!-- Body -->
                                     <div class="my-3">
-                                        <label for="basicauth" class="form-label">{{ $t("Username") }}</label>
-                                        <input id="basicauth-user" v-model="monitor.basic_auth_user" type="text" class="form-control" :placeholder="$t('Username')">
+                                        <label for="body" class="form-label">{{ $t("Body") }}</label>
+                                        <textarea id="body" v-model="monitor.body" class="form-control" :placeholder="bodyPlaceholder"></textarea>
                                     </div>
 
+                                    <!-- Headers -->
                                     <div class="my-3">
-                                        <label for="basicauth" class="form-label">{{ $t("Password") }}</label>
-                                        <input id="basicauth-pass" v-model="monitor.basic_auth_pass" type="password" autocomplete="new-password" class="form-control" :placeholder="$t('Password')">
+                                        <label for="headers" class="form-label">{{ $t("Headers") }}</label>
+                                        <textarea id="headers" v-model="monitor.headers" class="form-control" :placeholder="headersPlaceholder"></textarea>
                                     </div>
-                                    <template v-if="monitor.authMethod === 'ntlm' ">
+
+                                    <!-- HTTP Auth -->
+                                    <h4 class="mt-5 mb-2">{{ $t("Authentication") }}</h4>
+
+                                    <!-- Method -->
+                                    <div class="my-3">
+                                        <label for="method" class="form-label">{{ $t("Method") }}</label>
+                                        <select id="method" v-model="monitor.authMethod" class="form-select">
+                                            <option :value="null">
+                                                {{ $t("None") }}
+                                            </option>
+                                            <option value="basic">
+                                                {{ $t("HTTP Basic Auth") }}
+                                            </option>
+                                            <option value="ntlm">
+                                                NTLM
+                                            </option>
+                                        </select>
+                                    </div>
+                                    <template v-if="monitor.authMethod && monitor.authMethod !== null ">
                                         <div class="my-3">
-                                            <label for="basicauth" class="form-label">{{ $t("Domain") }}</label>
-                                            <input id="basicauth-domain" v-model="monitor.authDomain" type="text" class="form-control" :placeholder="$t('Domain')">
+                                            <label for="basicauth" class="form-label">{{ $t("Username") }}</label>
+                                            <input id="basicauth-user" v-model="monitor.basic_auth_user" type="text" class="form-control" :placeholder="$t('Username')">
                                         </div>
 
                                         <div class="my-3">
-                                            <label for="basicauth" class="form-label">{{ $t("Workstation") }}</label>
-                                            <input id="basicauth-workstation" v-model="monitor.authWorkstation" type="text" class="form-control" :placeholder="$t('Workstation')">
+                                            <label for="basicauth" class="form-label">{{ $t("Password") }}</label>
+                                            <input id="basicauth-pass" v-model="monitor.basic_auth_pass" type="password" autocomplete="new-password" class="form-control" :placeholder="$t('Password')">
                                         </div>
+                                        <template v-if="monitor.authMethod === 'ntlm' ">
+                                            <div class="my-3">
+                                                <label for="basicauth" class="form-label">{{ $t("Domain") }}</label>
+                                                <input id="basicauth-domain" v-model="monitor.authDomain" type="text" class="form-control" :placeholder="$t('Domain')">
+                                            </div>
+
+                                            <div class="my-3">
+                                                <label for="basicauth" class="form-label">{{ $t("Workstation") }}</label>
+                                                <input id="basicauth-workstation" v-model="monitor.authWorkstation" type="text" class="form-control" :placeholder="$t('Workstation')">
+                                            </div>
+                                        </template>
                                     </template>
                                 </template>
                             </template>