diff --git a/http-internal.h b/http-internal.h index 5b3f6397..b320e2d6 100644 --- a/http-internal.h +++ b/http-internal.h @@ -145,8 +145,9 @@ struct evhttp { size_t default_max_headers_size; ev_uint64_t default_max_body_size; - /* bitmask of all allowed methods */ - short allowed_methods; + /* Bitmask of all HTTP methods that we accept and pass to user + * callbacks. */ + ev_uint16_t allowed_methods; /* Fallback callback if all the other callbacks for this connection don't match. */ diff --git a/http.c b/http.c index 70e0d13d..3aeecc9d 100644 --- a/http.c +++ b/http.c @@ -1396,7 +1396,7 @@ evhttp_parse_request_line(struct evhttp_request *req, char *line) } else if (strcmp(method, "TRACE") == 0) { req->type = EVHTTP_REQ_TRACE; } else if (strcmp(method, "PATCH") == 0) { - req->type = EVHTTP_REQ_PATCH; + req->type = EVHTTP_REQ_PATCH; } else { event_debug(("%s: bad method %s on request %p from %s", __func__, method, req, req->remote_host)); @@ -2692,8 +2692,9 @@ evhttp_handle_request(struct evhttp_request *req, void *arg) } if ((http->allowed_methods & req->type) == 0) { - event_debug(("Rejecting disallowed method %d (allowed: %d)\n", req->type, http->allowed_methods)); - evhttp_send_error(req, HTTP_BADMETHOD, NULL); + event_debug(("Rejecting disallowed method %x (allowed: %x)\n", + (unsigned)req->type, (unsigned)http->allowed_methods)); + evhttp_send_error(req, HTTP_NOTIMPLEMENTED, NULL); return; } @@ -2883,9 +2884,9 @@ evhttp_new_object(void) evhttp_set_max_headers_size(http, EV_SIZE_MAX); evhttp_set_max_body_size(http, EV_SIZE_MAX); evhttp_set_allowed_methods(http, EVHTTP_REQ_GET | - EVHTTP_REQ_POST | - EVHTTP_REQ_HEAD | - EVHTTP_REQ_PUT | + EVHTTP_REQ_POST | + EVHTTP_REQ_HEAD | + EVHTTP_REQ_PUT | EVHTTP_REQ_DELETE); TAILQ_INIT(&http->sockets); @@ -3020,7 +3021,7 @@ evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size) } void -evhttp_set_allowed_methods(struct evhttp* http, short methods) +evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods) { http->allowed_methods = methods; } diff --git a/include/event2/http.h b/include/event2/http.h index 45d3b245..27dd7811 100644 --- a/include/event2/http.h +++ b/include/event2/http.h @@ -57,7 +57,9 @@ struct event_base; #define HTTP_NOTMODIFIED 304 /**< page was not modified from last */ #define HTTP_BADREQUEST 400 /**< invalid http request was made */ #define HTTP_NOTFOUND 404 /**< could not find content for uri */ -#define HTTP_BADMETHOD 405 /**< method not allowed */ +#define HTTP_BADMETHOD 405 /**< method not allowed for this uri */ +#define HTTP_INTERNAL 500 /**< internal error */ +#define HTTP_NOTIMPLEMENTED 501 /**< not implemented */ #define HTTP_SERVUNAVAIL 503 /**< the server is not available */ struct evhttp; @@ -185,7 +187,9 @@ void evhttp_set_max_headers_size(struct evhttp* http, ev_ssize_t max_headers_siz void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size); /** - Sets the what HTTP methods are supported in requests accepted by this server. + Sets the what HTTP methods are supported in requests accepted by this + server, and passed to user callbacks. + If not supported they will generate a "405 Method not allowed" response. By default this includes the following methods: GET, POST, HEAD, PUT, DELETE @@ -193,7 +197,7 @@ void evhttp_set_max_body_size(struct evhttp* http, ev_ssize_t max_body_size); @param http the http server on which to set the methods @param methods bit mask constructed from evhttp_cmd_type values */ -void evhttp_set_allowed_methods(struct evhttp* http, short methods); +void evhttp_set_allowed_methods(struct evhttp* http, ev_uint16_t methods); /** Set a callback for a specified URI @@ -339,7 +343,13 @@ void evhttp_send_reply_end(struct evhttp_request *req); * Interfaces for making requests */ -/** the different request types supported by evhttp */ +/** The different request types supported by evhttp. These are as specified + * in RFC2616, except for PATCH which is specified by RFC5789. + * + * By default, only some of these methods are accepted and passed to user + * callbacks; use evhttp_set_allowed_methods() to change which methods + * are allowed. + */ enum evhttp_cmd_type { EVHTTP_REQ_GET = 1 << 0, EVHTTP_REQ_POST = 1 << 1,