summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOpenGnSys Support Team <soporte-og@soleta.eu>2024-12-01 17:21:34 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-12-05 17:12:30 +0100
commit8f52b8911edfd3f4e2dd1c459f7779eb79492046 (patch)
tree92f253476a93924ef62921f12534dc05eadb6deb
parentef3bad6e14eeded9832f3d24ae60bc6303ff015e (diff)
rest: extend og_rest_ctx to provide http error
this is to improve error reporting for ogcp and ogcli
-rw-r--r--src/rest.c287
1 files changed, 182 insertions, 105 deletions
diff --git a/src/rest.c b/src/rest.c
index 2f3acc0..ae19386 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -84,6 +84,18 @@ struct ev_loop *og_loop;
#define OG_REST_PARAM_GATEWAY (1UL << 46)
#define OG_REST_PARAM_FOLDER (1UL << 47)
+enum og_http_error {
+ OG_HTTP_000_UNSPEC = 0,
+ OG_HTTP_400_BAD_REQUEST, /* malformed request */
+ OG_HTTP_405_METHOD_NOT_ALLOWED, /* unsupported method */
+ OG_HTTP_404_NOT_FOUND, /* not found */
+ OG_HTTP_409_CONFLICT, /* already exists */
+ OG_HTTP_423_LOCKED, /* in use */
+ OG_HTTP_500_INTERNAL_SERVER_ERROR, /* request has failed */
+ OG_HTTP_501_SERVICE_UNAVAILABLE, /* cannot reach database */
+ OG_HTTP_507_INSUFFICIENT_STORAGE, /* no disk space in server */
+};
+
static LIST_HEAD(client_list);
static LIST_HEAD(client_wol_list);
@@ -438,6 +450,7 @@ struct og_rest_ctx {
json_t *json;
struct og_msg_params params;
char buf_reply[OG_MSG_RESPONSE_MAXLEN];
+ uint32_t http_error;
};
static int og_cmd_get_clients(struct og_rest_ctx *ctx)
@@ -5961,6 +5974,24 @@ static int og_client_not_found(struct og_client *cli)
return -1;
}
+static int og_client_conflict(struct og_client *cli)
+{
+ char buf[] = "HTTP/1.1 409 Conflict\r\nContent-Length: 0\r\n\r\n";
+
+ send(og_client_socket(cli), buf, strlen(buf), 0);
+
+ return -1;
+}
+
+static int og_client_locked(struct og_client *cli)
+{
+ char buf[] = "HTTP/1.1 423 Locked\r\nContent-Length: 0\r\n\r\n";
+
+ send(og_client_socket(cli), buf, strlen(buf), 0);
+
+ return -1;
+}
+
static int og_client_not_authorized(struct og_client *cli)
{
char buf[] = "HTTP/1.1 401 Unauthorized\r\n"
@@ -5982,6 +6013,26 @@ static int og_server_internal_error(struct og_client *cli)
return -1;
}
+static int og_server_service_unavailable(struct og_client *cli)
+{
+ char buf[] = "HTTP/1.1 501 Service Unavailable\r\n"
+ "Content-Length: 0\r\n\r\n";
+
+ send(og_client_socket(cli), buf, strlen(buf), 0);
+
+ return -1;
+}
+
+static int og_server_insufficient_storage(struct og_client *cli)
+{
+ char buf[] = "HTTP/1.1 507 Insufficient Storage\r\n"
+ "Content-Length: 0\r\n\r\n";
+
+ send(og_client_socket(cli), buf, strlen(buf), 0);
+
+ return -1;
+}
+
static int og_client_ok(struct og_client *cli, char *buf_reply)
{
char buf[OG_MSG_RESPONSE_MAXLEN] = {};
@@ -6137,6 +6188,32 @@ static void og_rest_log(const struct og_client *cli, enum og_rest_method method,
syslog(LOG_INFO, "%s", log_buf);
}
+static int og_client_error(struct og_client *cli, enum og_http_error error)
+{
+ switch (error) {
+ case OG_HTTP_400_BAD_REQUEST:
+ return og_client_bad_request(cli);
+ case OG_HTTP_404_NOT_FOUND:
+ return og_client_not_found(cli);
+ case OG_HTTP_405_METHOD_NOT_ALLOWED:
+ return og_client_method_not_found(cli);
+ case OG_HTTP_409_CONFLICT:
+ return og_client_conflict(cli);
+ case OG_HTTP_423_LOCKED:
+ return og_client_locked(cli);
+ case OG_HTTP_500_INTERNAL_SERVER_ERROR:
+ return og_server_internal_error(cli);
+ case OG_HTTP_501_SERVICE_UNAVAILABLE:
+ return og_server_service_unavailable(cli);
+ case OG_HTTP_507_INSUFFICIENT_STORAGE:
+ return og_server_insufficient_storage(cli);
+ default:
+ break;
+ }
+
+ return og_client_bad_request(cli);
+}
+
int og_client_state_process_payload_rest(struct og_client *cli)
{
struct og_rest_ctx ctx = {};
@@ -6180,7 +6257,7 @@ int og_client_state_process_payload_rest(struct og_client *cli)
if (!strncmp(cmd, "clients", strlen("clients"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
@@ -6189,7 +6266,7 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_get_clients(&ctx);
break;
default:
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
} else if (!strncmp(cmd, "client/repo", strlen("client/repo"))) {
@@ -6197,26 +6274,26 @@ int og_client_state_process_payload_rest(struct og_client *cli)
case OG_METHOD_POST:
if (!root) {
syslog(LOG_ERR, "client post repo command with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_client_repo(&ctx);
break;
default:
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
} else if (!strncmp(cmd, "client/setup",
strlen("client/setup"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client partitions with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6224,83 +6301,83 @@ int og_client_state_process_payload_rest(struct og_client *cli)
} else if (!strncmp(cmd, "client/info",
strlen("client/info"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client info with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_get_client_info(&ctx);
} else if (!strncmp(cmd, "client/add", strlen("client/add"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client info with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_client_add(&ctx);
} else if (!strncmp(cmd, "client/update", strlen("client/update"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client info with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_client_update(&ctx);
} else if (!strncmp(cmd, "client/delete", strlen("client/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_client_delete(&ctx);
} else if (!strncmp(cmd, "client/move", strlen("client/move"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client move with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_client_move(&ctx);
} else if (!strncmp(cmd, "folder/add", strlen("folder/add"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command folder add with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6308,14 +6385,14 @@ int og_client_state_process_payload_rest(struct og_client *cli)
} else if (!strncmp(cmd, "folder/update", strlen("folder/update"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command folder update with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6323,14 +6400,14 @@ int og_client_state_process_payload_rest(struct og_client *cli)
} else if (!strncmp(cmd, "folder/delete", strlen("folder/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command folder delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6338,57 +6415,57 @@ int og_client_state_process_payload_rest(struct og_client *cli)
} else if (!strncmp(cmd, "wol", strlen("wol"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command wol with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_wol(&ctx);
} else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command run with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_run_post(&ctx);
} else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) {
if (method != OG_METHOD_POST && method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command output with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_run_get(&ctx);
} else if (!strncmp(cmd, "shell/list", strlen("shell/list"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
err = og_cmd_shell_list(&ctx);
} else if (!strncmp(cmd, "session", strlen("session"))) {
if (method != OG_METHOD_POST && method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command session with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6398,50 +6475,50 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_get_session(&ctx);
} else if (!strncmp(cmd, "scopes", strlen("scopes"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (root) {
syslog(LOG_ERR, "command scopes with payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_scope_get(&ctx);
} else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command poweroff with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_poweroff(&ctx);
} else if (!strncmp(cmd, "reboot", strlen("reboot"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command reboot with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_reboot(&ctx);
} else if (!strncmp(cmd, "mode", strlen("mode"))) {
if (method != OG_METHOD_GET && method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (method == OG_METHOD_POST && !root) {
syslog(LOG_ERR, "command mode with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6451,37 +6528,37 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_post_modes(&ctx);
} else if (!strncmp(cmd, "stop", strlen("stop"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command stop with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_stop(&ctx);
} else if (!strncmp(cmd, "refresh", strlen("refresh"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command refresh with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_refresh(&ctx);
} else if (!strncmp(cmd, "hardware", strlen("hardware"))) {
if (method != OG_METHOD_GET && method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command hardware with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6491,13 +6568,13 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_hardware(&ctx);
} else if (!strncmp(cmd, "software", strlen("software"))) {
if (method != OG_METHOD_POST && method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command software with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6507,120 +6584,120 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_get_software(&ctx);
} else if (!strncmp(cmd, "repositories", strlen("repositories"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (root) {
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_get_repositories(&ctx);
} else if (!strncmp(cmd, "repository/add", strlen("repository/add"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command repository add with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_repository_add(&ctx);
} else if (!strncmp(cmd, "repository/update", strlen("repository/update"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command repository add with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_repository_update(&ctx);
} else if (!strncmp(cmd, "repository/delete",
strlen("repository/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command repository delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_repository_delete(&ctx);
} else if (!strncmp(cmd, "images", strlen("images"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (root) {
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_images(&ctx);
} else if (!strncmp(cmd, "image/create", strlen("image/create"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command create with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_create_image(&ctx);
} else if (!strncmp(cmd, "image/update", strlen("image/update"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command create with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_update_image(&ctx);
} else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command create with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_restore_image(&ctx);
} else if (!strncmp(cmd, "image/delete", strlen("image/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command image delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_delete_image(&ctx);
} else if (!strncmp(cmd, "image/restrict", strlen("image/restrict"))) {
if (!root) {
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6632,217 +6709,217 @@ int og_client_state_process_payload_rest(struct og_client *cli)
err = og_cmd_image_scope_update(&ctx);
break;
default:
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
} else if (!strncmp(cmd, "cache/list", strlen("cache/list"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command cache list with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_cache_list(&ctx);
} else if (!strncmp(cmd, "efi", strlen("efi"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command efi with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_efi_list(&ctx);
} else if (!strncmp(cmd, "cache/delete", strlen("cache/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command cache list with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_cache_delete(&ctx);
} else if (!strncmp(cmd, "cache/fetch", strlen("cache/fetch"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command cache fetch with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_cache_fetch(&ctx);
} else if (!strncmp(cmd, "setup", strlen("setup"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command create with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_setup(&ctx);
} else if (!strncmp(cmd, "oglive/list",
strlen("oglive/list"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
err = og_cmd_oglive_list(&ctx);
} else if (!strncmp(cmd, "oglive/add", strlen("oglive/add"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command oglive add with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_oglive_add(&ctx);
} else if (!strncmp(cmd, "oglive/delete", strlen("oglive/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command oglive delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_oglive_delete(&ctx);
} else if (!strncmp(cmd, "oglive/set", strlen("oglive/set"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command oglive set with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_oglive_set(&ctx);
} else if (!strncmp(cmd, "center/add",
strlen("center/add"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
err = og_cmd_post_center_add(&ctx);
} else if (!strncmp(cmd, "center/delete", strlen("center/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command center delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_center_delete(&ctx);
} else if (!strncmp(cmd, "center/info",
strlen("center/info"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command client info with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_get_center_info(&ctx);
} else if (!strncmp(cmd, "center/update", strlen("center/update"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command center update with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_center_update(&ctx);
} else if (!strncmp(cmd, "room/add",
strlen("room/add"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command task with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_room_add(&ctx);
} else if (!strncmp(cmd, "room/update",
strlen("room/update"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR, "command task with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_room_update(&ctx);
} else if (!strncmp(cmd, "room/delete", strlen("room/delete"))) {
if (method != OG_METHOD_POST) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command room delete with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_room_delete(&ctx);
} else if (!strncmp(cmd, "room/info", strlen("room/info"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
if (!root) {
syslog(LOG_ERR,
"command room info with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
@@ -6858,25 +6935,25 @@ int og_client_state_process_payload_rest(struct og_client *cli)
case OG_METHOD_POST:
if (!root) {
syslog(LOG_ERR, "address add command with no payload\n");
- err = og_client_bad_request(cli);
+ ctx.http_error = OG_HTTP_400_BAD_REQUEST;
goto err_process_rest_payload;
}
err = og_cmd_post_server(&ctx);
break;
default:
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
} else if (!strncmp(cmd, "stats", strlen("stats"))) {
if (method != OG_METHOD_GET) {
- err = og_client_method_not_found(cli);
+ ctx.http_error = OG_HTTP_405_METHOD_NOT_ALLOWED;
goto err_process_rest_payload;
}
err = og_cmd_get_server_stats(&ctx);
} else {
syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd);
- err = og_client_not_found(cli);
+ ctx.http_error = OG_HTTP_404_NOT_FOUND;
}
og_rest_log(cli, method, uri, &ctx.params);
@@ -6884,7 +6961,7 @@ int og_client_state_process_payload_rest(struct og_client *cli)
json_decref(root);
if (err < 0)
- return og_client_bad_request(cli);
+ return og_client_error(cli, ctx.http_error);
return og_client_ok(cli, ctx.buf_reply);
@@ -6894,5 +6971,5 @@ err_process_rest_payload:
ntohs(cli->addr.sin_port), cli->buf);
json_decref(root);
- return err;
+ return og_client_error(cli, ctx.http_error);
}