summaryrefslogtreecommitdiffstats
path: root/sources/ogAdmServer.cpp
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2019-07-11 10:18:29 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2019-08-27 14:32:13 +0200
commit7dc8fdbaff2056b8d6220eac21144cd647cd43c4 (patch)
treee1085336024831746eb0c2ace3cbea290f5dd967 /sources/ogAdmServer.cpp
parentfbc30b26690fc6883c9a04d4143fb433c94e598b (diff)
#915 reply with HTTP 405 when the method is incorrect
When a command/request exists but the method is not correct returns 405 error code.
Diffstat (limited to 'sources/ogAdmServer.cpp')
-rw-r--r--sources/ogAdmServer.cpp37
1 files changed, 25 insertions, 12 deletions
diff --git a/sources/ogAdmServer.cpp b/sources/ogAdmServer.cpp
index 5dd42f3..7dea80a 100644
--- a/sources/ogAdmServer.cpp
+++ b/sources/ogAdmServer.cpp
@@ -3918,6 +3918,19 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params)
CLIENTE_OCUPADO);
}
+static int og_client_method_not_found(struct og_client *cli)
+{
+ /* To meet RFC 7231, this function MUST generate an Allow header field
+ * containing the correct methods. For example: "Allow: POST\r\n"
+ */
+ char buf[] = "HTTP/1.1 405 Method Not Allowed\r\n"
+ "Content-Length: 0\r\n\r\n";
+
+ send(og_client_socket(cli), buf, strlen(buf), 0);
+
+ return -1;
+}
+
static int og_client_not_found(struct og_client *cli)
{
char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
@@ -3979,7 +3992,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
method = OG_METHOD_POST;
cmd = cli->buf + strlen("POST") + 2;
} else
- return -1;
+ return og_client_method_not_found(cli);
body = strstr(cli->buf, "\r\n\r\n") + 4;
@@ -4000,7 +4013,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
if (!strncmp(cmd, "clients", strlen("clients"))) {
if (method != OG_METHOD_POST &&
method != OG_METHOD_GET)
- return -1;
+ return og_client_method_not_found(cli);
if (method == OG_METHOD_POST && !root) {
syslog(LOG_ERR, "command clients with no payload\n");
@@ -4016,7 +4029,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
}
} else if (!strncmp(cmd, "wol", strlen("wol"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command wol with no payload\n");
@@ -4025,7 +4038,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_wol(root, &params);
} else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command run with no payload\n");
@@ -4034,7 +4047,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_run_post(root, &params);
} else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command output with no payload\n");
@@ -4044,7 +4057,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_run_get(root, &params, buf_reply);
} else if (!strncmp(cmd, "session", strlen("session"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command session with no payload\n");
@@ -4053,7 +4066,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_session(root, &params);
} else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command poweroff with no payload\n");
@@ -4062,7 +4075,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_poweroff(root, &params);
} else if (!strncmp(cmd, "reboot", strlen("reboot"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command reboot with no payload\n");
@@ -4071,7 +4084,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_reboot(root, &params);
} else if (!strncmp(cmd, "stop", strlen("stop"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command stop with no payload\n");
@@ -4080,7 +4093,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_stop(root, &params);
} else if (!strncmp(cmd, "refresh", strlen("refresh"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command refresh with no payload\n");
@@ -4089,7 +4102,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_refresh(root, &params);
} else if (!strncmp(cmd, "hardware", strlen("hardware"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command hardware with no payload\n");
@@ -4098,7 +4111,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_hardware(root, &params);
} else if (!strncmp(cmd, "software", strlen("software"))) {
if (method != OG_METHOD_POST)
- return -1;
+ return og_client_method_not_found(cli);
if (!root) {
syslog(LOG_ERR, "command software with no payload\n");