mirror of https://git.48k.eu/ogserver
				
				
				
			#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.master
							parent
							
								
									55edb404b7
								
							
						
					
					
						commit
						c1c89e196c
					
				|  | @ -3933,6 +3933,15 @@ static int og_client_method_not_found(struct og_client *cli) | ||||||
| 	return -1; | 	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) | 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"; | 	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) { | 		if (method == OG_METHOD_POST && !root) { | ||||||
| 			syslog(LOG_ERR, "command clients with no payload\n"); | 			syslog(LOG_ERR, "command clients with no payload\n"); | ||||||
| 			return og_client_not_found(cli); | 			return og_client_bad_request(cli); | ||||||
| 		} | 		} | ||||||
| 		switch (method) { | 		switch (method) { | ||||||
| 		case OG_METHOD_POST: | 		case OG_METHOD_POST: | ||||||
|  | @ -4048,7 +4057,7 @@ static int og_client_state_process_payload_rest(struct og_client *cli) | ||||||
| 
 | 
 | ||||||
| 		if (!root) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command wol with no payload\n"); | 			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); | 		err = og_cmd_wol(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command run with no payload\n"); | 			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); | 		err = og_cmd_run_post(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command output with no payload\n"); | 			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); | 		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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command session with no payload\n"); | 			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); | 		err = og_cmd_session(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command poweroff with no payload\n"); | 			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); | 		err = og_cmd_poweroff(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "reboot", strlen("reboot"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command reboot with no payload\n"); | 			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); | 		err = og_cmd_reboot(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "stop", strlen("stop"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command stop with no payload\n"); | 			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); | 		err = og_cmd_stop(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "refresh", strlen("refresh"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command refresh with no payload\n"); | 			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); | 		err = og_cmd_refresh(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "hardware", strlen("hardware"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command hardware with no payload\n"); | 			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); | 		err = og_cmd_hardware(root, ¶ms); | ||||||
| 	} else if (!strncmp(cmd, "software", strlen("software"))) { | 	} 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) { | 		if (!root) { | ||||||
| 			syslog(LOG_ERR, "command software with no payload\n"); | 			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); | 		err = og_cmd_software(root, ¶ms); | ||||||
| 	} else { | 	} else { | ||||||
|  |  | ||||||
|  | @ -13,7 +13,7 @@ class TestGetClientsMethods(unittest.TestCase): | ||||||
| 
 | 
 | ||||||
|     def test_post_without_data(self): |     def test_post_without_data(self): | ||||||
|         returned = requests.post(self.url, headers=self.headers) |         returned = requests.post(self.url, headers=self.headers) | ||||||
|         self.assertEqual(returned.status_code, 404) |         self.assertEqual(returned.status_code, 400) | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue