diff options
| author | Dan Engelbrecht <[email protected]> | 2026-04-09 10:34:30 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-04-09 10:34:30 +0200 |
| commit | 4598f710da2d0e387c53eb97d983ff520c211a8f (patch) | |
| tree | b731277ec552a5ad978e1caa104a082ce25ecf2b /src/zenserver/proxy | |
| parent | 5.8.3 (diff) | |
| download | zen-4598f710da2d0e387c53eb97d983ff520c211a8f.tar.xz zen-4598f710da2d0e387c53eb97d983ff520c211a8f.zip | |
migrate from http_parser to llhttp (#929)
Diffstat (limited to 'src/zenserver/proxy')
| -rw-r--r-- | src/zenserver/proxy/httptrafficinspector.cpp | 73 | ||||
| -rw-r--r-- | src/zenserver/proxy/httptrafficinspector.h | 10 |
2 files changed, 40 insertions, 43 deletions
diff --git a/src/zenserver/proxy/httptrafficinspector.cpp b/src/zenserver/proxy/httptrafficinspector.cpp index 74ecbfd48..913bd2c28 100644 --- a/src/zenserver/proxy/httptrafficinspector.cpp +++ b/src/zenserver/proxy/httptrafficinspector.cpp @@ -10,29 +10,33 @@ namespace zen { // clang-format off -http_parser_settings HttpTrafficInspector::s_RequestSettings{ - .on_message_begin = [](http_parser*) { return 0; }, - .on_url = [](http_parser* p, const char* Data, size_t Len) { return GetThis(p)->OnUrl(Data, Len); }, - .on_status = [](http_parser*, const char*, size_t) { return 0; }, - .on_header_field = [](http_parser* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderField(Data, Len); }, - .on_header_value = [](http_parser* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderValue(Data, Len); }, - .on_headers_complete = [](http_parser* p) { return GetThis(p)->OnHeadersComplete(); }, - .on_body = [](http_parser*, const char*, size_t) { return 0; }, - .on_message_complete = [](http_parser* p) { return GetThis(p)->OnMessageComplete(); }, - .on_chunk_header{}, - .on_chunk_complete{}}; - -http_parser_settings HttpTrafficInspector::s_ResponseSettings{ - .on_message_begin = [](http_parser*) { return 0; }, - .on_url = [](http_parser*, const char*, size_t) { return 0; }, - .on_status = [](http_parser*, const char*, size_t) { return 0; }, - .on_header_field = [](http_parser* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderField(Data, Len); }, - .on_header_value = [](http_parser* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderValue(Data, Len); }, - .on_headers_complete = [](http_parser* p) { return GetThis(p)->OnHeadersComplete(); }, - .on_body = [](http_parser*, const char*, size_t) { return 0; }, - .on_message_complete = [](http_parser* p) { return GetThis(p)->OnMessageComplete(); }, - .on_chunk_header{}, - .on_chunk_complete{}}; +llhttp_settings_t HttpTrafficInspector::s_RequestSettings = []() { + llhttp_settings_t S; + llhttp_settings_init(&S); + S.on_message_begin = [](llhttp_t*) { return 0; }; + S.on_url = [](llhttp_t* p, const char* Data, size_t Len) { return GetThis(p)->OnUrl(Data, Len); }; + S.on_status = [](llhttp_t*, const char*, size_t) { return 0; }; + S.on_header_field = [](llhttp_t* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderField(Data, Len); }; + S.on_header_value = [](llhttp_t* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderValue(Data, Len); }; + S.on_headers_complete = [](llhttp_t* p) { return GetThis(p)->OnHeadersComplete(); }; + S.on_body = [](llhttp_t*, const char*, size_t) { return 0; }; + S.on_message_complete = [](llhttp_t* p) { return GetThis(p)->OnMessageComplete(); }; + return S; +}(); + +llhttp_settings_t HttpTrafficInspector::s_ResponseSettings = []() { + llhttp_settings_t S; + llhttp_settings_init(&S); + S.on_message_begin = [](llhttp_t*) { return 0; }; + S.on_url = [](llhttp_t*, const char*, size_t) { return 0; }; + S.on_status = [](llhttp_t*, const char*, size_t) { return 0; }; + S.on_header_field = [](llhttp_t* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderField(Data, Len); }; + S.on_header_value = [](llhttp_t* p, const char* Data, size_t Len) { return GetThis(p)->OnHeaderValue(Data, Len); }; + S.on_headers_complete = [](llhttp_t* p) { return GetThis(p)->OnHeadersComplete(); }; + S.on_body = [](llhttp_t*, const char*, size_t) { return 0; }; + S.on_message_complete = [](llhttp_t* p) { return GetThis(p)->OnMessageComplete(); }; + return S; +}(); // clang-format on HttpTrafficInspector::HttpTrafficInspector(Direction Dir, std::string_view SessionLabel) @@ -40,7 +44,8 @@ HttpTrafficInspector::HttpTrafficInspector(Direction Dir, std::string_view Sessi , m_Direction(Dir) , m_SessionLabel(SessionLabel) { - http_parser_init(&m_Parser, Dir == Direction::Request ? HTTP_REQUEST : HTTP_RESPONSE); + llhttp_settings_t* Settings = (Dir == Direction::Request) ? &s_RequestSettings : &s_ResponseSettings; + llhttp_init(&m_Parser, Dir == Direction::Request ? HTTP_REQUEST : HTTP_RESPONSE, Settings); m_Parser.data = this; } @@ -52,11 +57,9 @@ HttpTrafficInspector::Inspect(const char* Data, size_t Length) return; } - http_parser_settings* Settings = (m_Direction == Direction::Request) ? &s_RequestSettings : &s_ResponseSettings; + llhttp_errno_t Err = llhttp_execute(&m_Parser, Data, Length); - size_t Parsed = http_parser_execute(&m_Parser, Settings, Data, Length); - - if (m_Parser.upgrade) + if (Err == HPE_PAUSED_UPGRADE) { if (m_Direction == Direction::Request) { @@ -72,15 +75,9 @@ HttpTrafficInspector::Inspect(const char* Data, size_t Length) return; } - http_errno Error = HTTP_PARSER_ERRNO(&m_Parser); - if (Error != HPE_OK) - { - ZEN_DEBUG("[{}] non-HTTP traffic detected ({}), disabling inspection", m_SessionLabel, http_errno_name(Error)); - m_Disabled = true; - } - else if (Parsed != Length) + if (Err != HPE_OK) { - ZEN_DEBUG("[{}] parser consumed {}/{} bytes, disabling inspection", m_SessionLabel, Parsed, Length); + ZEN_DEBUG("[{}] non-HTTP traffic detected ({}), disabling inspection", m_SessionLabel, llhttp_errno_name(Err)); m_Disabled = true; } } @@ -127,11 +124,11 @@ HttpTrafficInspector::OnHeadersComplete() { if (m_Direction == Direction::Request) { - m_Method = http_method_str(static_cast<http_method>(m_Parser.method)); + m_Method = llhttp_method_name(static_cast<llhttp_method_t>(llhttp_get_method(&m_Parser))); } else { - m_StatusCode = m_Parser.status_code; + m_StatusCode = static_cast<uint16_t>(llhttp_get_status_code(&m_Parser)); } return 0; } diff --git a/src/zenserver/proxy/httptrafficinspector.h b/src/zenserver/proxy/httptrafficinspector.h index f4af0e77e..8192632ba 100644 --- a/src/zenserver/proxy/httptrafficinspector.h +++ b/src/zenserver/proxy/httptrafficinspector.h @@ -6,7 +6,7 @@ #include <zencore/uid.h> ZEN_THIRD_PARTY_INCLUDES_START -#include <http_parser.h> +#include <llhttp.h> ZEN_THIRD_PARTY_INCLUDES_END #include <atomic> @@ -45,15 +45,15 @@ private: void ResetMessageState(); - static HttpTrafficInspector* GetThis(http_parser* Parser) { return static_cast<HttpTrafficInspector*>(Parser->data); } + static HttpTrafficInspector* GetThis(llhttp_t* Parser) { return static_cast<HttpTrafficInspector*>(Parser->data); } - static http_parser_settings s_RequestSettings; - static http_parser_settings s_ResponseSettings; + static llhttp_settings_t s_RequestSettings; + static llhttp_settings_t s_ResponseSettings; LoggerRef Log() { return m_Log; } LoggerRef m_Log; - http_parser m_Parser; + llhttp_t m_Parser; Direction m_Direction; std::string m_SessionLabel; bool m_Disabled = false; |