Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <hpactor/net/http_types.hpp>
18 : : #include <hpactor/types/types.hpp>
19 : :
20 : : #include <functional>
21 : : #include <string>
22 : : #include <vector>
23 : :
24 : : #include "llhttp.h"
25 : :
26 : : namespace hpactor {
27 : : namespace net {
28 : :
29 : : // ---------------------------------------------------------------------------
30 : : // HttpParseState
31 : : // ---------------------------------------------------------------------------
32 : : enum class HttpParseState {
33 : : Idle,
34 : : ParsingHeaders,
35 : : ParsingBody,
36 : : Complete,
37 : : Error,
38 : : };
39 : :
40 : : // ---------------------------------------------------------------------------
41 : : // HttpParserMode — selects between HTTP request and response parsing
42 : : // ---------------------------------------------------------------------------
43 : : enum class HttpParserMode { Request, Response };
44 : :
45 : : // ---------------------------------------------------------------------------
46 : : // ResponseCallback — fired in Response mode with status, headers, body
47 : : // ---------------------------------------------------------------------------
48 : : using ResponseCallback = std::function<void(int status_code,
49 : : const std::vector<HttpHeader>& headers, const StreamBuffer& body)>;
50 : :
51 : : // ---------------------------------------------------------------------------
52 : : // HttpParser — wraps llhttp for use with StreamBuffer and HttpRequest
53 : : // ---------------------------------------------------------------------------
54 : : class HttpParser {
55 : : public:
56 : : using MessageCallback = std::function<void(HttpRequest&&)>;
57 : : using ErrorCallback = std::function<void(llhttp_errno_t, const char*)>;
58 : :
59 : : explicit HttpParser(HttpParserMode mode = HttpParserMode::Request);
60 : : ~HttpParser();
61 : :
62 : : HttpParser(const HttpParser&) = delete;
63 : : HttpParser& operator=(const HttpParser&) = delete;
64 : :
65 : : size_t execute(const StreamBuffer& data);
66 : :
67 : 8 : void set_on_message(MessageCallback cb) { on_message_ = std::move(cb); }
68 : 2 : void set_on_response(ResponseCallback cb) { on_response_ = std::move(cb); }
69 : 5 : void set_on_error(ErrorCallback cb) { on_error_ = std::move(cb); }
70 : :
71 : : void reset();
72 : :
73 : 2 : HttpParseState state() const { return state_; }
74 : 1 : bool upgrade_requested() const { return upgrade_; }
75 : : bool should_keep_alive() const;
76 : :
77 : : private:
78 : : static int on_message_begin_cb(llhttp_t* parser);
79 : : static int on_url_cb(llhttp_t* parser, const char* data, size_t len);
80 : : static int on_method_cb(llhttp_t* parser, const char* data, size_t len);
81 : : static int on_header_field_cb(llhttp_t* parser, const char* data, size_t len);
82 : : static int on_header_value_cb(llhttp_t* parser, const char* data, size_t len);
83 : : static int on_headers_complete_cb(llhttp_t* parser);
84 : : static int on_body_cb(llhttp_t* parser, const char* data, size_t len);
85 : : static int on_message_complete_cb(llhttp_t* parser);
86 : :
87 : : void finish_header();
88 : :
89 : : llhttp_t parser_;
90 : : llhttp_settings_t settings_;
91 : : HttpParserMode mode_ = HttpParserMode::Request;
92 : : HttpParseState state_ = HttpParseState::Idle;
93 : : bool upgrade_ = false;
94 : :
95 : : // Accumulation buffers
96 : : std::string url_buf_;
97 : : std::string header_name_buf_;
98 : : std::string header_value_buf_;
99 : : std::vector<HttpHeader> headers_;
100 : : HttpMethod method_ = HttpMethod::GET;
101 : : int http_major_ = 1;
102 : : int http_minor_ = 1;
103 : : StreamBuffer body_buf_;
104 : :
105 : : MessageCallback on_message_;
106 : : ResponseCallback on_response_;
107 : : ErrorCallback on_error_;
108 : : };
109 : :
110 : : } // namespace net
111 : : } // namespace hpactor
|