Skip to content

Commit c53f050

Browse files
kfirousfabiobaltieri
authored andcommitted
net: http_server: Support reason phrase for chunked encoded response
Add HTTP reason phrases in chunked transfer-encoded responses Signed-off-by: Kfir Bracha <[email protected]>
1 parent 764e210 commit c53f050

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

subsys/net/lib/http/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ config HTTP_SERVER_STATIC_FS_RESPONSE_SIZE
232232
Please note that it is allocated on the stack of the HTTP server thread,
233233
so CONFIG_HTTP_SERVER_STACK_SIZE has to be sufficiently large.
234234

235+
config HTTP_SERVER_COMPLETE_STATUS_PHRASES
236+
bool "Complete HTTP status reason phrases"
237+
help
238+
Include reason phrases for HTTP status codes in server responses.
239+
When disabled, HTTP responses will not include reason phrases,
240+
reducing code size. Note that reason phrases are optional per
241+
HTTP specification and their absence does not affect protocol
242+
compliance or client behavior.
243+
235244
endif
236245

237246
# Hidden option to avoid having multiple individual options that are ORed together

subsys/net/lib/http/http_server_http1.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,52 @@ static int handle_http1_static_resource(
196196
ret; })
197197

198198
#define RESPONSE_TEMPLATE_DYNAMIC_PART1 \
199-
"HTTP/1.1 %d\r\n" \
199+
"HTTP/1.1 %d%s%s\r\n" \
200200
"Transfer-Encoding: chunked\r\n"
201201

202+
static const char *http_status_str(enum http_status status)
203+
{
204+
#if defined(CONFIG_HTTP_SERVER_COMPLETE_STATUS_PHRASES)
205+
switch (status) {
206+
case HTTP_200_OK:
207+
return "OK";
208+
case HTTP_201_CREATED:
209+
return "Created";
210+
case HTTP_204_NO_CONTENT:
211+
return "No Content";
212+
case HTTP_400_BAD_REQUEST:
213+
return "Bad Request";
214+
case HTTP_401_UNAUTHORIZED:
215+
return "Unauthorized";
216+
case HTTP_403_FORBIDDEN:
217+
return "Forbidden";
218+
case HTTP_404_NOT_FOUND:
219+
return "Not Found";
220+
case HTTP_405_METHOD_NOT_ALLOWED:
221+
return "Method Not Allowed";
222+
case HTTP_500_INTERNAL_SERVER_ERROR:
223+
return "Internal Server Error";
224+
default:
225+
return "";
226+
}
227+
#endif
228+
return "";
229+
}
230+
231+
#if defined(CONFIG_HTTP_SERVER_COMPLETE_STATUS_PHRASES)
232+
#define REASON_PHRASE_MAX_LENGTH sizeof("Internal Server Error")
233+
#else
234+
#define REASON_PHRASE_MAX_LENGTH 0
235+
#endif
236+
202237
static int http1_send_headers(struct http_client_ctx *client, enum http_status status,
203238
const struct http_header *headers, size_t header_count,
204239
struct http_resource_detail_dynamic *dynamic_detail)
205240
{
206241
int ret;
207242
bool content_type_sent = false;
208-
char http_response[MAX(sizeof(RESPONSE_TEMPLATE_DYNAMIC_PART1) + sizeof("xxx"),
243+
char http_response[MAX(sizeof(RESPONSE_TEMPLATE_DYNAMIC_PART1) + sizeof("xxx") +
244+
REASON_PHRASE_MAX_LENGTH,
209245
CONFIG_HTTP_SERVER_MAX_HEADER_LEN + 2)];
210246

211247
if (status < HTTP_100_CONTINUE || status > HTTP_511_NETWORK_AUTHENTICATION_REQUIRED) {
@@ -219,7 +255,9 @@ static int http1_send_headers(struct http_client_ctx *client, enum http_status s
219255
}
220256

221257
/* Send response code and transfer encoding */
222-
snprintk(http_response, sizeof(http_response), RESPONSE_TEMPLATE_DYNAMIC_PART1, status);
258+
snprintk(http_response, sizeof(http_response), RESPONSE_TEMPLATE_DYNAMIC_PART1, status,
259+
IS_ENABLED(CONFIG_HTTP_SERVER_COMPLETE_STATUS_PHRASES) ? " " : "",
260+
http_status_str(status));
223261

224262
ret = http_server_sendall(client, http_response,
225263
strnlen(http_response, sizeof(http_response) - 1));

0 commit comments

Comments
 (0)