Skip to content

Commit

Permalink
Merge pull request #2272 from pi-hole/fix/delete
Browse files Browse the repository at this point in the history
We should not send any content for HTTP codes 1xx, 204 and 304
  • Loading branch information
DL6ER authored Feb 24, 2025
2 parents 45d0c8a + 6ffd43b commit 1fcb2bb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/api/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static int send_api_auth_status(struct ftl_conn *api, const int user_id, const t
const int code = delete_session(user_id) ? 204 : 404;

// Send empty reply with appropriate HTTP status code
send_http_code(api, "application/json; charset=utf-8", code, "");
send_http_code(api, NULL, code, "");
return code;
}
else
Expand Down
16 changes: 14 additions & 2 deletions src/api/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,7 @@ static int api_config_put_delete(struct ftl_conn *api)
if(!new_item->c(&new_item->v, new_item->k, errbuf))
{
free_config(&newconf);
free_config_path(requested_path);
return send_json_error(api, 400,
"bad_request",
"Invalid value",
Expand All @@ -1032,13 +1033,15 @@ static int api_config_put_delete(struct ftl_conn *api)
// Error 404 if config element not found
if(!found)
{
free_config(&newconf);
cJSON *json = JSON_NEW_OBJECT();
JSON_SEND_OBJECT_CODE(json, 404);
}

// Error 400 if unique item already present
if(message != NULL)
{
free_config(&newconf);
return send_json_error(api, 400,
"bad_request",
message,
Expand Down Expand Up @@ -1081,8 +1084,17 @@ static int api_config_put_delete(struct ftl_conn *api)

// Send empty reply with matching HTTP status code
// 201 - Created or 204 - No content
cJSON *json = JSON_NEW_OBJECT();
JSON_SEND_OBJECT_CODE(json, api->method == HTTP_PUT ? 201 : 204);
if(api->method == HTTP_PUT)
{
cJSON *json = JSON_NEW_OBJECT();
JSON_SEND_OBJECT_CODE(json, 201);
}
else
{
// 204 - No content
send_http_code(api, NULL, 204, "");
return 204;
}
}

// Endpoint /api/config router
Expand Down
13 changes: 3 additions & 10 deletions src/api/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -974,16 +974,9 @@ static int api_info_messages_DELETE(struct ftl_conn *api)
char *endptr = NULL;
long int idval = strtol(token, &endptr, 10);
if(errno != 0 || endptr == token || *endptr != '\0' || idval < 0)
{
// Send error reply
free(id);
return send_json_error(api, 400, // 400 Bad Request
"uri_error",
"Invalid ID in path",
api->action_path);
}

cJSON_AddNumberToObject(ids, "id", idval);
log_warn("API: URI error - skipping invalid ID in path (%s): %s", api->action_path, token);
else
cJSON_AddNumberToArray(ids, idval);

// Get next token
token = strtok_r(NULL, ",", &saveptr);
Expand Down
25 changes: 22 additions & 3 deletions src/webserver/http-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ int send_http_code(struct ftl_conn *api, const char *mime_type,
mime_type,
strlen(msg));

// Codes 1xx, 204 and 304 are not allowed to have a body
if(code < 200 || code == 204 || code == 304)
return 0;

return mg_write(api->conn, msg, strlen(msg));
}

Expand All @@ -88,18 +92,33 @@ int send_json_error_free(struct ftl_conn *api, const int code,
const char *key, const char* message,
char *hint, const bool free_hint, const bool log)
{
// Codes 1xx, 204, and 304 are not allowed to have a body
// Log to the logfile instead
if(code < 200 || code == 204 || code == 304)
{
if(hint != NULL)
log_warn("API: %s (key: %s, hint: %s)", message, key, hint);
else
log_warn("API: %s (key: %s)", message, key);

return send_http_code(api, NULL, code, "");
}

if(log)
{
if(hint != NULL)
log_warn("API: %s (%s)", message, hint);
log_warn("API: %s (key: %s, hint: %s)", message, key, hint);
else
log_warn("API: %s", message);
log_warn("API: %s (key: %s)", message, key);
}

cJSON *error = JSON_NEW_OBJECT();
JSON_REF_STR_IN_OBJECT(error, "key", key);
JSON_REF_STR_IN_OBJECT(error, "message", message);
JSON_COPY_STR_TO_OBJECT(error, "hint", hint);
if(hint != NULL)
JSON_COPY_STR_TO_OBJECT(error, "hint", hint);
else
JSON_ADD_NULL_TO_OBJECT(error, "hint");
if(free_hint && hint != NULL)
free(hint);

Expand Down
8 changes: 6 additions & 2 deletions src/webserver/json_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,14 @@
})

#define JSON_SEND_OBJECT_CODE(object, code)({ \
if((code) != 204) \
if((code) < 100 || (code) == 204 || (code) == 304) \
{ \
cJSON_AddNumberToObject(object, "took", double_time() - api->now); \
/* HTTP codes 1xx, 204 and 304 must not have a body */ \
send_http_code(api, NULL, code, ""); \
cJSON_Delete(object); \
return code; \
} \
cJSON_AddNumberToObject(object, "took", double_time() - api->now); \
char *json_string = json_formatter(object); \
if(json_string == NULL) \
{ \
Expand Down

0 comments on commit 1fcb2bb

Please sign in to comment.