Add helper function mg_send_head()

PUBLISHED_FROM=5c9d11d21d34fa72974a021f0342068d87159634
This commit is contained in:
Sergey Lyubka 2015-11-21 17:35:14 +03:00 committed by rojer
parent a93b3a06dd
commit 6cfc696439
2 changed files with 29 additions and 2 deletions

View File

@ -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) {

View File

@ -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.
*/