]> granicus.if.org Git - libevent/commitdiff
add a http default content type option
authorNicolas Martyanoff <khaelin@gmail.com>
Sat, 28 Sep 2013 18:03:28 +0000 (20:03 +0200)
committerNicolas Martyanoff <khaelin@gmail.com>
Mon, 30 Sep 2013 16:11:26 +0000 (18:11 +0200)
http-internal.h
http.c
include/event2/http.h

index fe14a230096b8f80931b3049b74e03558d37dce8..d25753bbf96ed35e3ec045611f653e940d715b27 100644 (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. */
diff --git a/http.c b/http.c
index 4452d31b2f33f8d797b6ec2fbd98b7c842585fc6..5eb91dfe989134d89912bd9532048ab20db316e6 100644 (file)
--- a/http.c
+++ b/http.c
@@ -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)
 {
index 0db75ed4b97ec23d2adc718b58e1dfdf3dac4f98..cf44941de8078b7b468997e8b2200ce118dbef2d 100644 (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.