Skip to content

Commit e1e1bee

Browse files
Make llhttp method array size dynamic (#13174) (#13196)
(cherry picked from commit b3a1c67)
1 parent aa4cf29 commit e1e1bee

4 files changed

Lines changed: 42 additions & 87 deletions

File tree

‎CHANGES/13174.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed the C parser reporting newer HTTP methods such as ``QUERY`` as ``<unknown>``; the method table is now derived from the vendored llhttp instead of a hand-maintained count -- by :user:`Dreamsorcerer`.

‎aiohttp/_cparser.pxd‎

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
from libc.stdint cimport int32_t, uint8_t, uint16_t, uint64_t
1+
# This file contains just the definitions needed in our Cython code.
2+
3+
from libc.stdint cimport uint8_t, uint16_t, uint64_t
24

35

46
cdef extern from "llhttp.h":
57

68
struct llhttp__internal_s:
7-
int32_t _index
8-
void* _span_pos0
9-
void* _span_cb0
10-
int32_t error
11-
const char* reason
12-
const char* error_pos
139
void* data
14-
void* _current
1510
uint64_t content_length
1611
uint8_t type
1712
uint8_t method
1813
uint8_t http_major
1914
uint8_t http_minor
20-
uint8_t header_state
21-
uint8_t lenient_flags
2215
uint8_t upgrade
23-
uint8_t finish
2416
uint16_t flags
2517
uint16_t status_code
26-
void* settings
2718

2819
ctypedef llhttp__internal_s llhttp__internal_t
2920
ctypedef llhttp__internal_t llhttp_t
@@ -43,20 +34,10 @@ cdef extern from "llhttp.h":
4334
llhttp_cb on_chunk_header
4435
llhttp_cb on_chunk_complete
4536

46-
llhttp_cb on_url_complete
47-
llhttp_cb on_status_complete
48-
llhttp_cb on_header_field_complete
49-
llhttp_cb on_header_value_complete
50-
5137
ctypedef llhttp_settings_s llhttp_settings_t
5238

5339
enum llhttp_errno:
5440
HPE_OK,
55-
HPE_INTERNAL,
56-
HPE_STRICT,
57-
HPE_LF_EXPECTED,
58-
HPE_UNEXPECTED_CONTENT_LENGTH,
59-
HPE_CLOSED_CONNECTION,
6041
HPE_INVALID_METHOD,
6142
HPE_INVALID_URL,
6243
HPE_INVALID_CONSTANT,
@@ -73,8 +54,7 @@ cdef extern from "llhttp.h":
7354
HPE_CB_CHUNK_HEADER,
7455
HPE_CB_CHUNK_COMPLETE,
7556
HPE_PAUSED,
76-
HPE_PAUSED_UPGRADE,
77-
HPE_USER
57+
HPE_PAUSED_UPGRADE
7858

7959
ctypedef llhttp_errno llhttp_errno_t
8060

@@ -84,58 +64,10 @@ cdef extern from "llhttp.h":
8464

8565
enum llhttp_type:
8666
HTTP_REQUEST,
87-
HTTP_RESPONSE,
88-
HTTP_BOTH
67+
HTTP_RESPONSE
8968

9069
enum llhttp_method:
91-
HTTP_DELETE,
92-
HTTP_GET,
93-
HTTP_HEAD,
94-
HTTP_POST,
95-
HTTP_PUT,
96-
HTTP_CONNECT,
97-
HTTP_OPTIONS,
98-
HTTP_TRACE,
99-
HTTP_COPY,
100-
HTTP_LOCK,
101-
HTTP_MKCOL,
102-
HTTP_MOVE,
103-
HTTP_PROPFIND,
104-
HTTP_PROPPATCH,
105-
HTTP_SEARCH,
106-
HTTP_UNLOCK,
107-
HTTP_BIND,
108-
HTTP_REBIND,
109-
HTTP_UNBIND,
110-
HTTP_ACL,
111-
HTTP_REPORT,
112-
HTTP_MKACTIVITY,
113-
HTTP_CHECKOUT,
114-
HTTP_MERGE,
115-
HTTP_MSEARCH,
116-
HTTP_NOTIFY,
117-
HTTP_SUBSCRIBE,
118-
HTTP_UNSUBSCRIBE,
119-
HTTP_PATCH,
120-
HTTP_PURGE,
121-
HTTP_MKCALENDAR,
122-
HTTP_LINK,
123-
HTTP_UNLINK,
124-
HTTP_SOURCE,
125-
HTTP_PRI,
126-
HTTP_DESCRIBE,
127-
HTTP_ANNOUNCE,
128-
HTTP_SETUP,
129-
HTTP_PLAY,
130-
HTTP_PAUSE,
131-
HTTP_TEARDOWN,
132-
HTTP_GET_PARAMETER,
133-
HTTP_SET_PARAMETER,
134-
HTTP_REDIRECT,
135-
HTTP_RECORD,
136-
HTTP_FLUSH
137-
138-
ctypedef llhttp_method llhttp_method_t;
70+
HTTP_CONNECT
13971

14072
void llhttp_settings_init(llhttp_settings_t* settings)
14173
void llhttp_init(llhttp_t* parser, llhttp_type type,
@@ -152,8 +84,6 @@ cdef extern from "llhttp.h":
15284
const char* llhttp_get_error_reason(const llhttp_t* parser)
15385
const char* llhttp_get_error_pos(const llhttp_t* parser)
15486

155-
const char* llhttp_method_name(llhttp_method_t method)
156-
15787
void llhttp_set_lenient_headers(llhttp_t* parser, int enabled)
15888
void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled)
15989
void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled)

‎aiohttp/_http_parser.pyx‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,29 @@ cdef inline object extend(object buf, const char* at, size_t length):
9898
memcpy(ptr + s, at, length)
9999

100100

101-
DEF METHODS_COUNT = 46;
101+
# The method-name table and its length come straight from llhttp's canonical
102+
# HTTP_ALL_METHOD_MAP, so they track the vendored llhttp version automatically
103+
# instead of relying on a hand-maintained method count.
104+
cdef extern from *:
105+
"""
106+
#include "llhttp.h"
107+
108+
#define _AIOHTTP_METHOD_NAME(NUM, NAME, STRING) [NUM] = #STRING,
109+
static const char* const _aiohttp_method_names[] = {
110+
HTTP_ALL_METHOD_MAP(_AIOHTTP_METHOD_NAME)
111+
};
112+
#undef _AIOHTTP_METHOD_NAME
113+
"""
114+
const char* _aiohttp_method_names[]
115+
const int METHODS_COUNT "((int)(sizeof(_aiohttp_method_names) / sizeof(_aiohttp_method_names[0])))"
116+
102117

103118
cdef list _http_method = []
104119

105120
for i in range(METHODS_COUNT):
106-
_http_method.append(
107-
cparser.llhttp_method_name(<cparser.llhttp_method_t> i).decode('ascii'))
108-
121+
assert _aiohttp_method_names[i] is not NULL
122+
_http_method.append(_aiohttp_method_names[i].decode('ascii'))
109123

110-
cdef inline str http_method_str(int i):
111-
if i < METHODS_COUNT:
112-
return <str>_http_method[i]
113-
else:
114-
return "<unknown>"
115124

116125
cdef inline object find_header(bytes raw_header):
117126
cdef Py_ssize_t size
@@ -504,7 +513,7 @@ cdef class HttpParser:
504513
encoding = enc
505514

506515
if self._cparser.type == cparser.HTTP_REQUEST:
507-
method = http_method_str(self._cparser.method)
516+
method = <str>_http_method[self._cparser.method]
508517
msg = _new_request_message(
509518
method, self._path,
510519
http_version, headers, raw_headers,

‎tests/test_http_parser.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,22 @@ def test_parse(parser: HttpRequestParser) -> None:
647647
assert msg.headers["Host"] == "a"
648648

649649

650-
async def test_parse_body(parser) -> None:
650+
def test_parse_query_method(parser: HttpRequestParser) -> None:
651+
"""QUERY has the highest llhttp method id; both parsers must decode its name.
652+
653+
Regression test for a stale hand-maintained method count in the C parser
654+
that mapped the last method(s) to "<unknown>".
655+
"""
656+
text = b"QUERY /test HTTP/1.1\r\nHost: a\r\n\r\n"
657+
messages, upgrade, tail = parser.feed_data(text)
658+
assert len(messages) == 1
659+
msg = messages[0][0]
660+
assert msg.method == "QUERY"
661+
assert msg.path == "/test"
662+
assert msg.version == (1, 1)
663+
664+
665+
async def test_parse_body(parser: HttpRequestParser) -> None:
651666
text = b"GET /test HTTP/1.1\r\nHost: a\r\nContent-Length: 4\r\n\r\nbody"
652667
messages, upgrade, tail = parser.feed_data(text)
653668
assert len(messages) == 1

0 commit comments

Comments
 (0)