From 19985261b2f288563c27ba6a9f88603baa275c50 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Tue, 10 Dec 2019 00:38:47 -0500 Subject: [PATCH] [core] convenience macros to check req methods --- src/http-header-glue.c | 7 ++----- src/http_kv.h | 2 ++ src/mod_deflate.c | 3 +-- src/mod_dirlisting.c | 13 ++----------- src/mod_expire.c | 3 +-- src/mod_proxy.c | 3 +-- src/mod_staticfile.c | 9 +-------- src/request.c | 3 +-- 8 files changed, 11 insertions(+), 32 deletions(-) diff --git a/src/http-header-glue.c b/src/http-header-glue.c index f4735b40..5574c8ff 100644 --- a/src/http-header-glue.c +++ b/src/http-header-glue.c @@ -156,9 +156,6 @@ const buffer * strftime_cache_get(const time_t last_mod) { int http_response_handle_cachable(connection *con, const buffer *mtime) { const buffer *vb; - int head_or_get = - ( HTTP_METHOD_GET == con->request.http_method - || HTTP_METHOD_HEAD == con->request.http_method); /* * 14.26 If-None-Match @@ -177,7 +174,7 @@ int http_response_handle_cachable(connection *con, const buffer *mtime) { && (200 == con->http_status || 0 == con->http_status) && NULL != http_header_request_get(con, HTTP_HEADER_RANGE, CONST_STR_LEN("Range"))); if (etag_is_equal(con->physical.etag, vb->ptr, !range_request)) { - if (head_or_get) { + if (http_method_get_or_head(con->request.http_method)) { con->http_status = 304; return HANDLER_FINISHED; } else { @@ -186,7 +183,7 @@ int http_response_handle_cachable(connection *con, const buffer *mtime) { return HANDLER_FINISHED; } } - } else if (head_or_get + } else if (http_method_get_or_head(con->request.http_method) && (vb = http_header_request_get(con, HTTP_HEADER_IF_MODIFIED_SINCE, CONST_STR_LEN("If-Modified-Since")))) { /* last-modified handling */ size_t used_len; diff --git a/src/http_kv.h b/src/http_kv.h index 8b73d7b2..73d0e750 100644 --- a/src/http_kv.h +++ b/src/http_kv.h @@ -65,5 +65,7 @@ int get_http_version_key(const char *s, size_t slen); http_method_t get_http_method_key(const char *s, size_t slen); void http_status_append(buffer *b, int status); void http_method_append(buffer *b, http_method_t method); +#define http_method_get_or_head(method) ((method) <= HTTP_METHOD_HEAD) +#define http_method_get_head_post(method) ((method) <= HTTP_METHOD_POST) #endif diff --git a/src/mod_deflate.c b/src/mod_deflate.c index ee3cd841..f6576e4a 100644 --- a/src/mod_deflate.c +++ b/src/mod_deflate.c @@ -1147,8 +1147,7 @@ CONNECTION_FUNC(mod_deflate_handle_response_start) { && if_none_match->ptr[etaglen-1] == '-' && 0 == strncmp(if_none_match->ptr+etaglen, label, strlen(label))) { - if ( HTTP_METHOD_GET == con->request.http_method - || HTTP_METHOD_HEAD == con->request.http_method) { + if (http_method_get_or_head(con->request.http_method)) { /* modify ETag response header in-place to remove '"' and append '-label"' */ vb->ptr[etaglen-1] = '-'; /*(overwrite end '"')*/ buffer_append_string(vb, label); diff --git a/src/mod_dirlisting.c b/src/mod_dirlisting.c index d5dd7341..687dc360 100644 --- a/src/mod_dirlisting.c +++ b/src/mod_dirlisting.c @@ -1026,20 +1026,11 @@ URIHANDLER_FUNC(mod_dirlisting_subrequest) { plugin_data *p = p_d; stat_cache_entry *sce = NULL; - /* we only handle GET and HEAD */ - switch(con->request.http_method) { - case HTTP_METHOD_GET: - case HTTP_METHOD_HEAD: - break; - default: - return HANDLER_GO_ON; - } - if (con->mode != DIRECT) return HANDLER_GO_ON; - - if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON; if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON; if (con->uri.path->ptr[buffer_string_length(con->uri.path) - 1] != '/') return HANDLER_GO_ON; + if (!http_method_get_or_head(con->request.http_method)) return HANDLER_GO_ON; + if (buffer_is_empty(con->physical.path)) return HANDLER_GO_ON; mod_dirlisting_patch_config(con, p); diff --git a/src/mod_expire.c b/src/mod_expire.c index c80cc81b..efb58888 100644 --- a/src/mod_expire.c +++ b/src/mod_expire.c @@ -275,8 +275,7 @@ CONNECTION_FUNC(mod_expire_handler) { /* Add caching headers only to http_status 200 OK or 206 Partial Content */ if (con->http_status != 200 && con->http_status != 206) return HANDLER_GO_ON; /* Add caching headers only to GET or HEAD requests */ - if ( con->request.http_method != HTTP_METHOD_GET - && con->request.http_method != HTTP_METHOD_HEAD) return HANDLER_GO_ON; + if (!http_method_get_or_head(con->request.http_method)) return HANDLER_GO_ON; /* Add caching headers only if not already present */ vb = http_header_response_get(con, HTTP_HEADER_CACHE_CONTROL, CONST_STR_LEN("Cache-Control")); if (NULL != vb) return HANDLER_GO_ON; diff --git a/src/mod_proxy.c b/src/mod_proxy.c index 20e1b0ea..ff57c32d 100644 --- a/src/mod_proxy.c +++ b/src/mod_proxy.c @@ -859,8 +859,7 @@ static handler_t proxy_create_env(gw_handler_ctx *gwhctx) { if (con->request.content_length > 0 || (0 == con->request.content_length - && HTTP_METHOD_GET != con->request.http_method - && HTTP_METHOD_HEAD != con->request.http_method)) { + && !http_method_get_or_head(con->request.http_method))) { /* set Content-Length if client sent Transfer-Encoding: chunked * and not streaming to backend (request body has been fully received) */ const buffer *vb = http_header_request_get(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length")); diff --git a/src/mod_staticfile.c b/src/mod_staticfile.c index d3310525..b14ff826 100644 --- a/src/mod_staticfile.c +++ b/src/mod_staticfile.c @@ -106,14 +106,7 @@ URIHANDLER_FUNC(mod_staticfile_subrequest) { if (con->mode != DIRECT) return HANDLER_GO_ON; /* we only handle GET, POST and HEAD */ - switch(con->request.http_method) { - case HTTP_METHOD_GET: - case HTTP_METHOD_POST: - case HTTP_METHOD_HEAD: - break; - default: - return HANDLER_GO_ON; - } + if (!http_method_get_head_post(con->request.http_method)) return HANDLER_GO_ON; mod_staticfile_patch_config(con, p); diff --git a/src/request.c b/src/request.c index 7058f749..cde84d4a 100644 --- a/src/request.c +++ b/src/request.c @@ -855,8 +855,7 @@ int http_request_parse(connection * const con, char * const hdrs, const unsigned http_header_request_unset(con, HTTP_HEADER_CONTENT_LENGTH, CONST_STR_LEN("Content-Length")); } } - if ((HTTP_METHOD_GET == con->request.http_method - || HTTP_METHOD_HEAD == con->request.http_method) + if (http_method_get_or_head(con->request.http_method) && !(con->conf.http_parseopts & HTTP_PARSEOPT_METHOD_GET_BODY)) { return http_request_header_line_invalid(con, 400, "GET/HEAD with content-length -> 400"); }