Skip to content

Commit 11eb043

Browse files
rluboskartben
authored andcommitted
net: http_server: Support HTTP1 405 error reply for all resource types
Reply with 405 Method not allowed not only for static FS resource types, but also for others. Also, the method checking for for static resources was messed up - those resource types only support GET by design, so no need to compare resource method bitmask - it should be checked that the request was actually GET instead. Signed-off-by: Robert Lubos <[email protected]>
1 parent 5688f58 commit 11eb043

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

subsys/net/lib/http/http_server_http1.c

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -90,37 +90,38 @@ static int handle_http1_static_resource(
9090
int len;
9191
int ret;
9292

93-
if (static_detail->common.bitmask_of_supported_http_methods & BIT(HTTP_GET)) {
94-
data = static_detail->static_data;
95-
len = static_detail->static_data_len;
96-
97-
if (static_detail->common.content_encoding != NULL &&
98-
static_detail->common.content_encoding[0] != '\0') {
99-
snprintk(http_response, sizeof(http_response),
100-
RESPONSE_TEMPLATE "Content-Encoding: %s\r\n\r\n",
101-
"Content-Type: ",
102-
static_detail->common.content_type == NULL ?
103-
"text/html" : static_detail->common.content_type,
104-
len, static_detail->common.content_encoding);
105-
} else {
106-
snprintk(http_response, sizeof(http_response),
107-
RESPONSE_TEMPLATE "\r\n",
108-
"Content-Type: ",
109-
static_detail->common.content_type == NULL ?
110-
"text/html" : static_detail->common.content_type,
111-
len);
112-
}
93+
if (client->method != HTTP_GET) {
94+
return send_http1_405(client);
95+
}
11396

114-
ret = http_server_sendall(client, http_response,
115-
strlen(http_response));
116-
if (ret < 0) {
117-
return ret;
118-
}
97+
data = static_detail->static_data;
98+
len = static_detail->static_data_len;
11999

120-
ret = http_server_sendall(client, data, len);
121-
if (ret < 0) {
122-
return ret;
123-
}
100+
if (static_detail->common.content_encoding != NULL &&
101+
static_detail->common.content_encoding[0] != '\0') {
102+
snprintk(http_response, sizeof(http_response),
103+
RESPONSE_TEMPLATE "Content-Encoding: %s\r\n\r\n",
104+
"Content-Type: ",
105+
static_detail->common.content_type == NULL ?
106+
"text/html" : static_detail->common.content_type,
107+
len, static_detail->common.content_encoding);
108+
} else {
109+
snprintk(http_response, sizeof(http_response),
110+
RESPONSE_TEMPLATE "\r\n",
111+
"Content-Type: ",
112+
static_detail->common.content_type == NULL ?
113+
"text/html" : static_detail->common.content_type,
114+
len);
115+
}
116+
117+
ret = http_server_sendall(client, http_response, strlen(http_response));
118+
if (ret < 0) {
119+
return ret;
120+
}
121+
122+
ret = http_server_sendall(client, data, len);
123+
if (ret < 0) {
124+
return ret;
124125
}
125126

126127
return 0;
@@ -441,7 +442,7 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
441442
sizeof("Content-Length: 01234567890123456789\r\n") +
442443
sizeof(CONTENT_ENCODING_GZIP)];
443444

444-
if (!(static_fs_detail->common.bitmask_of_supported_http_methods & BIT(HTTP_GET))) {
445+
if (client->method != HTTP_GET) {
445446
return send_http1_405(client);
446447
}
447448

@@ -521,7 +522,7 @@ static int handle_http1_dynamic_resource(
521522
user_method = dynamic_detail->common.bitmask_of_supported_http_methods;
522523

523524
if (!(BIT(client->method) & user_method)) {
524-
return -ENOPROTOOPT;
525+
return send_http1_405(client);
525526
}
526527

527528
if (dynamic_detail->holder != NULL && dynamic_detail->holder != client) {

0 commit comments

Comments
 (0)