diff options
author | Roberto Hueso Gómez <rhueso@soleta.eu> | 2019-09-19 15:49:39 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2019-09-19 16:06:03 +0200 |
commit | 391f9befcb48ae99cf89a067d27e6221339677b9 (patch) | |
tree | df93044f4f16f8e7d14d66ce6c3ff9d170707b11 | |
parent | b96fd6754455812aff43556438265accb3d8e9b3 (diff) |
#915: Return 400 status code in POST methods when no payload is attached
If no payload is attached to method that requires a payload, then the API
returns a 400 status code (following RFC 7231) instead of the previous 404.
test_0001_get_clients.py is also modified to fit the new status code.
-rw-r--r-- | admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp | 31 | ||||
-rw-r--r-- | admin/Sources/Services/ogAdmServer/tests/units/test_0001_get_clients.py | 2 |
2 files changed, 21 insertions, 12 deletions
diff --git a/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp b/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp index 357bf013..6401fe10 100644 --- a/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp +++ b/admin/Sources/Services/ogAdmServer/sources/ogAdmServer.cpp @@ -3933,6 +3933,15 @@ static int og_client_method_not_found(struct og_client *cli) return -1; } +static int og_client_bad_request(struct og_client *cli) +{ + char buf[] = "HTTP/1.1 400 Bad Request\r\nContent-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"; @@ -4032,7 +4041,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (method == OG_METHOD_POST && !root) { syslog(LOG_ERR, "command clients with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } switch (method) { case OG_METHOD_POST: @@ -4048,7 +4057,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command wol with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_wol(root, ¶ms); } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { @@ -4057,7 +4066,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command run with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_run_post(root, ¶ms); } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { @@ -4066,7 +4075,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command output with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_run_get(root, ¶ms, buf_reply); @@ -4076,7 +4085,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command session with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_session(root, ¶ms); } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { @@ -4085,7 +4094,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command poweroff with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_poweroff(root, ¶ms); } else if (!strncmp(cmd, "reboot", strlen("reboot"))) { @@ -4094,7 +4103,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command reboot with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_reboot(root, ¶ms); } else if (!strncmp(cmd, "stop", strlen("stop"))) { @@ -4103,7 +4112,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command stop with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_stop(root, ¶ms); } else if (!strncmp(cmd, "refresh", strlen("refresh"))) { @@ -4112,7 +4121,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command refresh with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_refresh(root, ¶ms); } else if (!strncmp(cmd, "hardware", strlen("hardware"))) { @@ -4121,7 +4130,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command hardware with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_hardware(root, ¶ms); } else if (!strncmp(cmd, "software", strlen("software"))) { @@ -4130,7 +4139,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) if (!root) { syslog(LOG_ERR, "command software with no payload\n"); - return og_client_not_found(cli); + return og_client_bad_request(cli); } err = og_cmd_software(root, ¶ms); } else { diff --git a/admin/Sources/Services/ogAdmServer/tests/units/test_0001_get_clients.py b/admin/Sources/Services/ogAdmServer/tests/units/test_0001_get_clients.py index 8d52bb8c..218b41aa 100644 --- a/admin/Sources/Services/ogAdmServer/tests/units/test_0001_get_clients.py +++ b/admin/Sources/Services/ogAdmServer/tests/units/test_0001_get_clients.py @@ -13,7 +13,7 @@ class TestGetClientsMethods(unittest.TestCase): def test_post_without_data(self): returned = requests.post(self.url, headers=self.headers) - self.assertEqual(returned.status_code, 404) + self.assertEqual(returned.status_code, 400) if __name__ == '__main__': unittest.main() |