add a http default content type option

This commit is contained in:
Nicolas Martyanoff 2013-09-28 20:03:28 +02:00
parent a093137809
commit 5a5acd9a70
3 changed files with 23 additions and 2 deletions

View File

@ -151,6 +151,7 @@ struct evhttp {
size_t default_max_headers_size;
ev_uint64_t default_max_body_size;
const char *default_content_type;
/* Bitmask of all HTTP methods that we accept and pass to user
* callbacks. */

13
http.c
View File

@ -552,9 +552,11 @@ evhttp_make_header_response(struct evhttp_connection *evcon,
/* Potentially add headers for unidentified content. */
if (evhttp_response_needs_body(req)) {
if (evhttp_find_header(req->output_headers,
"Content-Type") == NULL) {
"Content-Type") == NULL
&& evcon->http_server->default_content_type) {
evhttp_add_header(req->output_headers,
"Content-Type", "text/html; charset=ISO-8859-1");
"Content-Type",
evcon->http_server->default_content_type);
}
}
@ -3389,6 +3391,7 @@ evhttp_new_object(void)
evutil_timerclear(&http->timeout);
evhttp_set_max_headers_size(http, EV_SIZE_MAX);
evhttp_set_max_body_size(http, EV_SIZE_MAX);
evhttp_set_default_content_type(http, "text/html; charset=ISO-8859-1");
evhttp_set_allowed_methods(http,
EVHTTP_REQ_GET |
EVHTTP_REQ_POST |
@ -3595,6 +3598,12 @@ evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size)
http->default_max_body_size = max_body_size;
}
void
evhttp_set_default_content_type(struct evhttp *http,
const char *content_type) {
http->default_content_type = content_type;
}
void
evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods)
{

View File

@ -206,6 +206,17 @@ void evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_siz
/** XXX Document. */
void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size);
/**
Set the value to use for the Content-Type header when none was provided. If
the content type string is NULL, the Content-Type header will not be
automatically added.
@param http the http server on which to set the default content type
@param content_type the value for the Content-Type header
*/
void evhttp_set_default_content_type(struct evhttp *http,
const char *content_type);
/**
Sets the what HTTP methods are supported in requests accepted by this
server, and passed to user callbacks.