Commit 6cfc6964 authored by Sergey Lyubka's avatar Sergey Lyubka Committed by rojer

Add helper function mg_send_head()

    PUBLISHED_FROM=5c9d11d21d34fa72974a021f0342068d87159634
parent a93b3a06
...@@ -4503,6 +4503,17 @@ void mg_send_response_line(struct mg_connection *nc, int status_code, ...@@ -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, static void send_http_error(struct mg_connection *nc, int code,
const char *reason) { const char *reason) {
if (reason == NULL) { if (reason == NULL) {
......
...@@ -1534,7 +1534,7 @@ void mg_printf_http_chunk(struct mg_connection *, const char *, ...); ...@@ -1534,7 +1534,7 @@ void mg_printf_http_chunk(struct mg_connection *, const char *, ...);
/* /*
* Send response status line. * Send response status line.
* If `extra_headers` is not NULL, then `extra_headers` are also sent * 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: * Example:
* *
* mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); * mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *");
...@@ -1544,9 +1544,25 @@ void mg_printf_http_chunk(struct mg_connection *, const char *, ...); ...@@ -1544,9 +1544,25 @@ void mg_printf_http_chunk(struct mg_connection *, const char *, ...);
* HTTP/1.1 200 OK\r\n * HTTP/1.1 200 OK\r\n
* Access-Control-Allow-Origin: *\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); 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. * Send printf-formatted HTTP chunk, escaping HTML tags.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment