Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/13174.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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`.
82 changes: 6 additions & 76 deletions aiohttp/_cparser.pxd
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
from libc.stdint cimport int32_t, uint8_t, uint16_t, uint64_t
# This file contains just the definitions needed in our Cython code.

from libc.stdint cimport uint8_t, uint16_t, uint64_t


cdef extern from "llhttp.h":

struct llhttp__internal_s:
int32_t _index
void* _span_pos0
void* _span_cb0
int32_t error
const char* reason
const char* error_pos
void* data
void* _current
uint64_t content_length
uint8_t type
uint8_t method
uint8_t http_major
uint8_t http_minor
uint8_t header_state
uint8_t lenient_flags
uint8_t upgrade
uint8_t finish
uint16_t flags
uint16_t status_code
void* settings

ctypedef llhttp__internal_s llhttp__internal_t
ctypedef llhttp__internal_t llhttp_t
Expand All @@ -43,20 +34,10 @@ cdef extern from "llhttp.h":
llhttp_cb on_chunk_header
llhttp_cb on_chunk_complete

llhttp_cb on_url_complete
llhttp_cb on_status_complete
llhttp_cb on_header_field_complete
llhttp_cb on_header_value_complete

ctypedef llhttp_settings_s llhttp_settings_t

enum llhttp_errno:
HPE_OK,
HPE_INTERNAL,
HPE_STRICT,
HPE_LF_EXPECTED,
HPE_UNEXPECTED_CONTENT_LENGTH,
HPE_CLOSED_CONNECTION,
HPE_INVALID_METHOD,
HPE_INVALID_URL,
HPE_INVALID_CONSTANT,
Expand All @@ -73,8 +54,7 @@ cdef extern from "llhttp.h":
HPE_CB_CHUNK_HEADER,
HPE_CB_CHUNK_COMPLETE,
HPE_PAUSED,
HPE_PAUSED_UPGRADE,
HPE_USER
HPE_PAUSED_UPGRADE

ctypedef llhttp_errno llhttp_errno_t

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

enum llhttp_type:
HTTP_REQUEST,
HTTP_RESPONSE,
HTTP_BOTH
HTTP_RESPONSE

enum llhttp_method:
HTTP_DELETE,
HTTP_GET,
HTTP_HEAD,
HTTP_POST,
HTTP_PUT,
HTTP_CONNECT,
HTTP_OPTIONS,
HTTP_TRACE,
HTTP_COPY,
HTTP_LOCK,
HTTP_MKCOL,
HTTP_MOVE,
HTTP_PROPFIND,
HTTP_PROPPATCH,
HTTP_SEARCH,
HTTP_UNLOCK,
HTTP_BIND,
HTTP_REBIND,
HTTP_UNBIND,
HTTP_ACL,
HTTP_REPORT,
HTTP_MKACTIVITY,
HTTP_CHECKOUT,
HTTP_MERGE,
HTTP_MSEARCH,
HTTP_NOTIFY,
HTTP_SUBSCRIBE,
HTTP_UNSUBSCRIBE,
HTTP_PATCH,
HTTP_PURGE,
HTTP_MKCALENDAR,
HTTP_LINK,
HTTP_UNLINK,
HTTP_SOURCE,
HTTP_PRI,
HTTP_DESCRIBE,
HTTP_ANNOUNCE,
HTTP_SETUP,
HTTP_PLAY,
HTTP_PAUSE,
HTTP_TEARDOWN,
HTTP_GET_PARAMETER,
HTTP_SET_PARAMETER,
HTTP_REDIRECT,
HTTP_RECORD,
HTTP_FLUSH

ctypedef llhttp_method llhttp_method_t;
HTTP_CONNECT

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

const char* llhttp_method_name(llhttp_method_t method)

void llhttp_set_lenient_headers(llhttp_t* parser, int enabled)
void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled)
void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled)
29 changes: 19 additions & 10 deletions aiohttp/_http_parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,29 @@ cdef inline object extend(object buf, const char* at, size_t length):
memcpy(ptr + s, at, length)


DEF METHODS_COUNT = 46;
# The method-name table and its length come straight from llhttp's canonical
# HTTP_ALL_METHOD_MAP, so they track the vendored llhttp version automatically
# instead of relying on a hand-maintained method count.
cdef extern from *:
"""
#include "llhttp.h"

#define _AIOHTTP_METHOD_NAME(NUM, NAME, STRING) [NUM] = #STRING,
static const char* const _aiohttp_method_names[] = {
HTTP_ALL_METHOD_MAP(_AIOHTTP_METHOD_NAME)
};
#undef _AIOHTTP_METHOD_NAME
"""
const char* _aiohttp_method_names[]
const int METHODS_COUNT "((int)(sizeof(_aiohttp_method_names) / sizeof(_aiohttp_method_names[0])))"


cdef list _http_method = []

for i in range(METHODS_COUNT):
_http_method.append(
cparser.llhttp_method_name(<cparser.llhttp_method_t> i).decode('ascii'))

assert _aiohttp_method_names[i] is not NULL
_http_method.append(_aiohttp_method_names[i].decode('ascii'))

cdef inline str http_method_str(int i):
if i < METHODS_COUNT:
return <str>_http_method[i]
else:
return "<unknown>"

cdef inline object find_header(bytes raw_header):
cdef Py_ssize_t size
Expand Down Expand Up @@ -506,7 +515,7 @@ cdef class HttpParser:
encoding = enc

if self._cparser.type == cparser.HTTP_REQUEST:
method = http_method_str(self._cparser.method)
method = <str>_http_method[self._cparser.method]
msg = _new_request_message(
method, self._path,
http_version, headers, raw_headers,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,21 @@ def test_parse(parser: HttpRequestParser) -> None:
assert msg.headers["Host"] == "a"


def test_parse_query_method(parser: HttpRequestParser) -> None:
"""QUERY has the highest llhttp method id; both parsers must decode its name.

Regression test for a stale hand-maintained method count in the C parser
that mapped the last method(s) to "<unknown>".
"""
text = b"QUERY /test HTTP/1.1\r\nHost: a\r\n\r\n"
messages, upgrade, tail = parser.feed_data(text)
assert len(messages) == 1
msg = messages[0][0]
assert msg.method == "QUERY"
assert msg.path == "/test"
assert msg.version == (1, 1)


async def test_parse_body(parser: HttpRequestParser) -> None:
text = b"GET /test HTTP/1.1\r\nHost: a\r\nContent-Length: 4\r\n\r\nbody"
messages, upgrade, tail = parser.feed_data(text)
Expand Down
Loading