hermes

HTTP GET/HEAD-only web server for static content.
git clone git://git.francescosaccone.com/hermes
Log | Files | Refs | README | LICENSE

commit 97aa1785bdba9e221f54433c146f72931342ce2c
parent af604046704aefa22efa536a3a8217ac99b9b3f2
Author: Francesco Saccone <francesco@francescosaccone.com>
Date:   Mon, 31 Mar 2025 19:41:08 +0200

refactor: make compose_http_response actually return the string

Signed-off-by: Francesco Saccone <francesco@francescosaccone.com>

Diffstat:
Mhttp.c | 11+++++++++--
Mhttp.h | 6+++---
2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/http.c b/http.c @@ -47,8 +47,8 @@ get_length_of_integer(unsigned int integer) { return result; } -void -compose_http_response(struct http_response response, char *buffer) { +char * +compose_http_response(struct http_response response) { size_t size; unsigned int status_code = status_map[response.status].code; const char *template = "HTTP/1.1 %u %s\r\n" @@ -59,6 +59,7 @@ compose_http_response(struct http_response response, char *buffer) { *status_message = status_map[response.status].message, *content_type = response.content_type, *body = response.body; + char *buffer; /* * This is actually a bit inelegant: it adds the length of 'template' @@ -74,10 +75,16 @@ compose_http_response(struct http_response response, char *buffer) { + strlen(body) - 2 * 5; + buffer = malloc(size); + snprintf(buffer, size, template, status_code, status_message, content_type, strlen(body), body); + + buffer[size] = '\0'; + + return buffer; } diff --git a/http.h b/http.h @@ -84,9 +84,9 @@ struct http_request * parse_http_request(char *request); /* - * Writes a raw HTTP response to buffer, given a http_response. + * Returns the raw HTTP response from a given a http_response. */ -void -compose_http_response(struct http_response response, char *buffer); +char * +compose_http_response(struct http_response response); #endif