Fix check supported content type.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2026-05-04 09:46:26 +02:00
parent 0cc24a9637
commit 869ef09f34
3 changed files with 15 additions and 5 deletions

View File

@@ -293,11 +293,21 @@ const char *rest_method_to_string(rest_http_method_t method) {
}
}
bool rest_content_type_is_json(const char *content_type) {
bool rest_supported_content_type(const char *content_type) {
if (content_type == NULL) {
return false;
}
return strncasecmp(content_type, "application/json", 16) == 0;
const char *supported_types[] = {
"application/json",
"application/x-pem-file"
};
printf("[rest] Checking content type: %s\n", content_type);
for (size_t i = 0; i < sizeof(supported_types) / sizeof(supported_types[0]); i++) {
if (strncasecmp(content_type, supported_types[i], strlen(supported_types[i])) == 0) {
return true;
}
}
return false;
}
#ifndef _MSC_VER

View File

@@ -167,7 +167,7 @@ extern int rest_execute_route_handler(const rest_request_t *request, rest_route_
extern int rest_response_set_error(rest_response_t *response, int status_code, const char *message);
const char *rest_status_text_from_code(uint16_t code);
const char *rest_method_to_string(rest_http_method_t method);
bool rest_content_type_is_json(const char *content_type);
bool rest_supported_content_type(const char *content_type);
const rest_route_t *rest_get_routes(size_t *count);

View File

@@ -754,8 +754,8 @@ void rest_handle_request(rest_conn_t *conn) {
rest_debug_dump_payload("request-body", request->body, request->body_len);
if (request->method == REST_HTTP_POST || request->method == REST_HTTP_PUT) {
if (!rest_content_type_is_json(request->content_type) && strncasecmp(request->content_type, "application/x-pem-file", 22) != 0) {
send_json_error(conn, 415, "content_type_must_be_application_json");
if (!rest_supported_content_type(request->content_type)) {
send_json_error(conn, 415, "content_type_not_supported");
return;
}
}