diff --git a/mongoose.c b/mongoose.c index a94ed7fa..367f6193 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4503,6 +4503,17 @@ void mg_send_response_line(struct mg_connection *nc, int status_code, } } +void mg_send_head(struct mg_connection *c, int status_code, + int64_t content_length, const char *extra_headers) { + mg_send_response_line(c, status_code, extra_headers); + if (content_length < 0) { + mg_printf(c, "%s", "Transfer-Encoding: chunked\r\n"); + } else { + mg_printf(c, "Content-Length: %" INT64_FMT "\r\n", content_length); + } + mg_send(c, "\r\n", 2); +} + static void send_http_error(struct mg_connection *nc, int code, const char *reason) { if (reason == NULL) { diff --git a/mongoose.h b/mongoose.h index c9e2ce5b..7d37254b 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1534,7 +1534,7 @@ void mg_printf_http_chunk(struct mg_connection *, const char *, ...); /* * Send response status line. * If `extra_headers` is not NULL, then `extra_headers` are also sent - * after the reponse line, followed by a new line. + * after the reponse line. `extra_headers` must NOT end end with new line. * Example: * * mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); @@ -1544,9 +1544,25 @@ void mg_printf_http_chunk(struct mg_connection *, const char *, ...); * HTTP/1.1 200 OK\r\n * Access-Control-Allow-Origin: *\r\n */ -void mg_send_response_line(struct mg_connection *nc, int status_code, +void mg_send_response_line(struct mg_connection *c, int status_code, const char *extra_headers); +/* + * Send response line and headers. + * This function sends response line with the `status_code`, and automatically + * sends one header: either "Content-Length", or "Transfer-Encoding". + * If `content_length` is negative, then "Transfer-Encoding: chunked" header + * is sent, otherwise, "Content-Length" header is sent. + * + * NOTE: If `Transfer-Encoding` is `chunked`, then message body must be sent + * using `mg_send_http_chunk()` or `mg_printf_http_chunk()` functions. + * Otherwise, `mg_send()` or `mg_printf()` must be used. + * Extra headers could be set through `extra_headers` - and note `extra_headers` + * must NOT be terminated by a new line. + */ +void mg_send_head(struct mg_connection *n, int status_code, + int64_t content_length, const char *extra_headers); + /* * Send printf-formatted HTTP chunk, escaping HTML tags. */