From cc3814db536cd3f6e3701d0949f1f29337fd8424 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Wed, 26 Apr 2023 00:50:02 -0400 Subject: [core] rename http_kv funcs, reorder http_versions rename http_kv funcs for consistency ("http_" prefix) reorder http_versions[] --- src/http_cgi.c | 5 ++-- src/http_kv.c | 69 +++++++++++++++++++--------------------------------- src/http_kv.h | 36 ++++++++++----------------- src/mod_ajp13.c | 4 +-- src/request.c | 4 +-- src/t/test_http_kv.c | 7 +++--- 6 files changed, 48 insertions(+), 77 deletions(-) (limited to 'src') diff --git a/src/http_cgi.c b/src/http_cgi.c index 2f1a5b89..c38b9a19 100644 --- a/src/http_cgi.c +++ b/src/http_cgi.c @@ -224,9 +224,8 @@ http_cgi_headers (request_st * const r, http_cgi_opts * const opts, http_cgi_hea const buffer * const m = http_method_buf(r->http_method); rc |= cb(vdata, CONST_STR_LEN("REQUEST_METHOD"), BUF_PTR_LEN(m)); - s = get_http_version_name(r->http_version); - force_assert(s); - rc |= cb(vdata, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s)); + const buffer * const v = http_version_buf(r->http_version); + rc |= cb(vdata, CONST_STR_LEN("SERVER_PROTOCOL"), BUF_PTR_LEN(v)); if (r->conf.server_tag) { s = r->conf.server_tag->ptr; diff --git a/src/http_kv.c b/src/http_kv.c index 2ad848ef..90270be0 100644 --- a/src/http_kv.c +++ b/src/http_kv.c @@ -18,12 +18,13 @@ typedef struct { const char *value; } keyvalue; -static const keyvalue http_versions[] = { - { HTTP_VERSION_2, CONST_LEN_STR("HTTP/2.0") }, /* SERVER_PROTOCOL */ - { HTTP_VERSION_3, CONST_LEN_STR("HTTP/3.0") }, - { HTTP_VERSION_1_1, CONST_LEN_STR("HTTP/1.1") }, - { HTTP_VERSION_1_0, CONST_LEN_STR("HTTP/1.0") }, - { HTTP_VERSION_UNSET, 0, NULL } +/* HTTP version string as SERVER_PROTOCOL string */ +static const buffer http_versions[] = { /*(must by ordered by enum)*/ + { CONST_STR_LEN("HTTP/1.0")+1, 0 } /* HTTP_VERSION_1_0 */ + ,{ CONST_STR_LEN("HTTP/1.1")+1, 0 } /* HTTP_VERSION_1_1 */ + ,{ CONST_STR_LEN("HTTP/2.0")+1, 0 } /* HTTP_VERSION_2 */ + ,{ CONST_STR_LEN("HTTP/3.0")+1, 0 } /* HTTP_VERSION_3 */ + ,{ "", 0, 0 } }; static const buffer http_methods[] = { @@ -140,7 +141,17 @@ static const keyvalue http_status[] = { }; -const buffer *http_method_buf (http_method_t i) +const buffer * +http_version_buf (http_version_t i) +{ + return ((unsigned int)i < sizeof(http_versions)/sizeof(*http_versions)) + ? http_versions+i + : http_versions+sizeof(http_versions)/sizeof(*http_versions)-1; +} + + +const buffer * +http_method_buf (http_method_t i) { return ((unsigned int)i < sizeof(http_methods)/sizeof(*http_methods)-2) ? http_methods+i @@ -149,9 +160,9 @@ const buffer *http_method_buf (http_method_t i) } -__attribute_noinline__ __attribute_pure__ -static const keyvalue * keyvalue_from_key (const keyvalue *kv, const int k) +static const keyvalue * +keyvalue_from_key (const keyvalue *kv, const int k) { /*(expects sentinel to have key == -1 and value == NULL)*/ while (kv->key != k && kv->key != -1) ++kv; @@ -159,35 +170,9 @@ static const keyvalue * keyvalue_from_key (const keyvalue *kv, const int k) } -#if 0 /*(unused)*/ -__attribute_pure__ -static int keyvalue_get_key(const keyvalue *kv, const char * const s, const unsigned int slen) +http_method_t +http_method_key_get (const char *s, const size_t slen) { - /*(expects sentinel to have key == -1 and vlen == 0)*/ - while (kv->vlen && (kv->vlen != slen || 0 != memcmp(kv->value, s, slen))) - ++kv; - return kv->key; -} -#endif - - -const char *get_http_version_name(int i) { - return keyvalue_from_key(http_versions, i)->value; -} - -#if 0 /*(unused)*/ -const char *get_http_status_name(int i) { - return keyvalue_from_key(http_status, i)->value; -} -#endif - -#if 0 /*(unused)*/ -int get_http_version_key(const char *s, size_t slen) { - return keyvalue_get_key(http_versions, s, (unsigned int)slen); -} -#endif - -http_method_t get_http_method_key(const char *s, const size_t slen) { if (slen == 3 && s[0] == 'G' && s[1] == 'E' && s[2] == 'T') return HTTP_METHOD_GET; const buffer *kv = http_methods+1; /*(step over http_methods[0] ("GET"))*/ @@ -203,7 +188,9 @@ http_method_t get_http_method_key(const char *s, const size_t slen) { } -void http_status_append(buffer * const b, const int status) { +void +http_status_append (buffer * const b, const int status) +{ if (200 == status) { /*(short-circuit common case)*/ buffer_append_string_len(b, CONST_STR_LEN("200 OK")); return; @@ -217,9 +204,3 @@ void http_status_append(buffer * const b, const int status) { buffer_append_char(b, ' '); } } - -void http_version_append(buffer * const b, const http_version_t version) { - const keyvalue * const kv = keyvalue_from_key(http_versions, version); - if (__builtin_expect( (0 != kv->vlen), 1)) - buffer_append_string_len(b, kv->value, kv->vlen); -} diff --git a/src/http_kv.h b/src/http_kv.h index 377a7fd2..e4a00e18 100644 --- a/src/http_kv.h +++ b/src/http_kv.h @@ -66,45 +66,35 @@ typedef enum { ,HTTP_VERSION_3 } http_version_t; -#if 0 /*(unused)*/ __attribute_pure__ -const char *get_http_status_name(int i); -#endif - -__attribute_pure__ -const char *get_http_version_name(int i); - -/*(deprecated)*/ -#define get_http_method_name(i) http_method_buf(i)->ptr - -#if 0 /*(unused)*/ -__attribute_nonnull__() -__attribute_pure__ -int get_http_version_key(const char *s, size_t slen); -#endif +const buffer *http_version_buf (http_version_t i); __attribute_pure__ const buffer *http_method_buf (http_method_t i); __attribute_nonnull__() __attribute_pure__ -http_method_t get_http_method_key(const char *s, size_t slen); - -__attribute_nonnull__() -void http_status_append(buffer *b, int status); +http_method_t http_method_key_get (const char *s, size_t slen); __attribute_nonnull__() -void http_version_append(buffer *b, http_version_t version); +void http_status_append (buffer *b, int status); #define http_method_get_or_head(method) ((method) <= HTTP_METHOD_HEAD) #define http_method_get_head_query(method) ((method) <= HTTP_METHOD_QUERY) #define http_method_get_head_query_post(method) ((method) <= HTTP_METHOD_POST) +__attribute_nonnull__() +static inline void http_version_append (buffer * const b, const http_version_t version); +static inline void http_version_append (buffer * const b, const http_version_t version) +{ + buffer_append_buffer(b, http_version_buf(version)); +} + __attribute_nonnull__() static inline void http_method_append (buffer * const b, const http_method_t method); -static inline void http_method_append (buffer * const b, const http_method_t method) { - const buffer * const kv = http_method_buf(method); - buffer_append_string_len(b, BUF_PTR_LEN(kv)); +static inline void http_method_append (buffer * const b, const http_method_t method) +{ + buffer_append_buffer(b, http_method_buf(method)); } diff --git a/src/mod_ajp13.c b/src/mod_ajp13.c index 8bed8df7..65dc1905 100644 --- a/src/mod_ajp13.c +++ b/src/mod_ajp13.c @@ -583,8 +583,8 @@ ajp13_create_env (handler_ctx * const hctx) if (0 == method_byte) break; x[5] = method_byte; /* protocol */ - const char * const proto = get_http_version_name(r->http_version); - n = ajp13_enc_string(x, n, proto, strlen(proto)); + const buffer * const proto = http_version_buf(r->http_version); + n = ajp13_enc_string(x, n, BUF_PTR_LEN(proto)); if (0 == n) break; /* req_uri */ n = ajp13_enc_string(x, n, BUF_PTR_LEN(&r->uri.path)); diff --git a/src/request.c b/src/request.c index 3dc12a3b..b53db2be 100644 --- a/src/request.c +++ b/src/request.c @@ -716,7 +716,7 @@ http_request_parse_header (request_st * const restrict r, http_header_parse_ctx case HTTP_HEADER_H2_METHOD: if (__builtin_expect( (HTTP_METHOD_UNSET != r->http_method), 0)) break; - r->http_method = get_http_method_key(v, vlen); + r->http_method = http_method_key_get(v, vlen); if (HTTP_METHOD_UNSET >= r->http_method) return http_request_header_line_invalid(r, 501, "unknown http-method -> 501"); @@ -914,7 +914,7 @@ static int http_request_parse_reqline(request_st * const restrict r, const char return http_request_header_line_invalid(r, 400, "incomplete request line -> 400"); #endif - r->http_method = get_http_method_key(ptr, i); + r->http_method = http_method_key_get(ptr, i); if (HTTP_METHOD_UNSET >= r->http_method) return http_request_header_line_invalid(r, 501, "unknown http-method -> 501"); diff --git a/src/t/test_http_kv.c b/src/t/test_http_kv.c index 37eebca6..1c323d2c 100644 --- a/src/t/test_http_kv.c +++ b/src/t/test_http_kv.c @@ -8,9 +8,10 @@ #include "http_kv.c" static void test_http_kv_tables (void) { - assert(0 == strcmp(get_http_version_name(HTTP_VERSION_2), "HTTP/2.0")); - assert(0 == strcmp(get_http_version_name(HTTP_VERSION_1_1), "HTTP/1.1")); - assert(0 == strcmp(get_http_version_name(HTTP_VERSION_1_0), "HTTP/1.0")); + assert(0 == strcmp(http_version_buf(HTTP_VERSION_3)->ptr, "HTTP/3.0")); + assert(0 == strcmp(http_version_buf(HTTP_VERSION_2)->ptr, "HTTP/2.0")); + assert(0 == strcmp(http_version_buf(HTTP_VERSION_1_1)->ptr, "HTTP/1.1")); + assert(0 == strcmp(http_version_buf(HTTP_VERSION_1_0)->ptr, "HTTP/1.0")); /* TODO (more) */ } -- cgit v1.2.1