hermes

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

commit 1aa821c8f4c48e1bf9655b00986fa0c6b95e888d
parent 2e94c46e2ce16cb6330461dc241094c435e046bb
Author: Francesco Saccone <francesco@francescosaccone.com>
Date:   Mon, 31 Mar 2025 17:38:22 +0200

feat: add compose_http_response function

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

Diffstat:
Mhttp.c | 17+++++++++++++++++
Mhttp.h | 8++++++++
2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/http.c b/http.c @@ -1,5 +1,6 @@ #include "http.h" +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -33,3 +34,19 @@ parse_http_request(char *request) { return result; } + +void +compose_http_response(struct http_response *response, + char *buffer, + unsigned int buffer_size) { + snprintf(buffer, buffer_size, "HTTP/1.1 %u %s\r\n" + "Content-Type: %s; charset=UTF-8\r\n" + "Content-Length: %u\r\n" + "\r\n" + "%s", + status_map[response->status].code, + status_map[response->status].message, + response->content_type, + response->body_length, + response->body); +} diff --git a/http.h b/http.h @@ -84,4 +84,12 @@ struct http_response { struct http_request * parse_http_request(char *request); +/* + * Writes a raw HTTP response to buffer, given a http_response. + */ +void +compose_http_response(struct http_response *response, + char *buffer, + unsigned int buffer_size); + #endif