Skip to content

Commit 5688f58

Browse files
rluboskartben
authored andcommitted
net: http_server: Add helper functions for HTTP1 error replies
Add helper functions for HTTP/1 error replies to reduce and avoid further code duplication. Signed-off-by: Robert Lubos <[email protected]>
1 parent f5a126e commit 5688f58

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

subsys/net/lib/http/http_server_http1.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,40 @@ static const char conflict_response[] = "HTTP/1.1 409 Conflict\r\n\r\n";
3737
static const char final_chunk[] = "0\r\n\r\n";
3838
static const char *crlf = &final_chunk[3];
3939

40+
static int send_http1_error_common(struct http_client_ctx *client,
41+
const char *response, size_t len)
42+
{
43+
int ret;
44+
45+
ret = http_server_sendall(client, response, len);
46+
if (ret < 0) {
47+
LOG_DBG("Cannot write to socket (%d)", ret);
48+
return ret;
49+
}
50+
51+
client->http1_headers_sent = true;
52+
53+
return 0;
54+
}
55+
56+
static int send_http1_404(struct http_client_ctx *client)
57+
{
58+
return send_http1_error_common(client, not_found_response,
59+
sizeof(not_found_response) - 1);
60+
}
61+
62+
static int send_http1_405(struct http_client_ctx *client)
63+
{
64+
return send_http1_error_common(client, not_allowed_response,
65+
sizeof(not_allowed_response) - 1);
66+
}
67+
68+
static int send_http1_409(struct http_client_ctx *client)
69+
{
70+
return send_http1_error_common(client, conflict_response,
71+
sizeof(conflict_response) - 1);
72+
}
73+
4074
static int handle_http1_static_resource(
4175
struct http_resource_detail_static *static_detail,
4276
struct http_client_ctx *client)
@@ -408,12 +442,7 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
408442
sizeof(CONTENT_ENCODING_GZIP)];
409443

410444
if (!(static_fs_detail->common.bitmask_of_supported_http_methods & BIT(HTTP_GET))) {
411-
ret = http_server_sendall(client, not_allowed_response,
412-
sizeof(not_allowed_response) - 1);
413-
if (ret < 0) {
414-
LOG_DBG("Cannot write to socket (%d)", ret);
415-
}
416-
return ret;
445+
return send_http1_405(client);
417446
}
418447

419448
/* get filename and content-type from url */
@@ -432,12 +461,7 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
432461
ret = http_server_find_file(fname, sizeof(fname), &file_size, &gzipped);
433462
if (ret < 0) {
434463
LOG_ERR("fs_stat %s: %d", fname, ret);
435-
ret = http_server_sendall(client, not_found_response,
436-
sizeof(not_found_response) - 1);
437-
if (ret < 0) {
438-
LOG_DBG("Cannot write to socket (%d)", ret);
439-
}
440-
return ret;
464+
return send_http1_404(client);
441465
}
442466
fs_file_t_init(&file);
443467
ret = fs_open(&file, fname, FS_O_READ);
@@ -501,10 +525,8 @@ static int handle_http1_dynamic_resource(
501525
}
502526

503527
if (dynamic_detail->holder != NULL && dynamic_detail->holder != client) {
504-
ret = http_server_sendall(client, conflict_response,
505-
sizeof(conflict_response) - 1);
528+
ret = send_http1_409(client);
506529
if (ret < 0) {
507-
LOG_DBG("Cannot write to socket (%d)", ret);
508530
return ret;
509531
}
510532

@@ -931,10 +953,8 @@ int handle_http1_request(struct http_client_ctx *client)
931953
}
932954
} else {
933955
not_found: ; /* Add extra semicolon to make clang to compile when using label */
934-
ret = http_server_sendall(client, not_found_response,
935-
sizeof(not_found_response) - 1);
956+
ret = send_http1_404(client);
936957
if (ret < 0) {
937-
LOG_DBG("Cannot write to socket (%d)", ret);
938958
return ret;
939959
}
940960
}

0 commit comments

Comments
 (0)