LCOV - code coverage report
Current view: top level - include/hpactor/net - http_connection.hpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 100.0 % 9 9
Test Date: 2026-05-20 02:24:49 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             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/adt/stream_buffer.hpp>
      18                 :             : #include <hpactor/net/event_loop.hpp>
      19                 :             : #include <hpactor/net/http_parser.hpp>
      20                 :             : #include <hpactor/net/http_types.hpp>
      21                 :             : #include <hpactor/net/transport.hpp>
      22                 :             : 
      23                 :             : #include <functional>
      24                 :             : #include <memory>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :             : namespace hpactor {
      28                 :             : namespace net {
      29                 :             : 
      30                 :             : enum class HTTPConnectionMode { Server, Client };
      31                 :             : 
      32                 :             : class HTTPConnection;
      33                 :             : using HTTPConnectionPtr = std::shared_ptr<HTTPConnection>;
      34                 :             : 
      35                 :             : // HTTPConnection — TCP connection with HTTP/1.1 framing via llhttp.
      36                 :             : //
      37                 :             : // Mirrors the WireFrameConnection pattern: registers its fd with EventLoop,
      38                 :             : // uses a read handler to accumulate bytes and feed them to HttpParser.
      39                 :             : // Supports two modes:
      40                 :             : //   - Server: parses incoming HTTP requests, fires request_handler
      41                 :             : //   - Client: parses incoming HTTP responses, fires response_handler
      42                 :             : //
      43                 :             : // Send operations build HTTP/1.1 wire bytes and use async_send through
      44                 :             : // the EventLoop's backend, with write buffering and flush-on-completion.
      45                 :             : class HTTPConnection : public Connection,
      46                 :             :                        public std::enable_shared_from_this<HTTPConnection> {
      47                 :             :   public:
      48                 :             :     // Create a connection in the given mode.
      49                 :             :     // Registers fd with the EventLoop for read events.
      50                 :             :     static HTTPConnectionPtr
      51                 :             :     create(int fd, EndPoint local_endpoint, EndPoint remote_endpoint,
      52                 :             :            EventLoop* loop, HTTPConnectionMode mode);
      53                 :             : 
      54                 :             :     ~HTTPConnection();
      55                 :             : 
      56                 :             :     // Non-copyable
      57                 :             :     HTTPConnection(const HTTPConnection&) = delete;
      58                 :             :     HTTPConnection& operator=(const HTTPConnection&) = delete;
      59                 :             : 
      60                 :             :     // ---- Callbacks ----
      61                 :             : 
      62                 :             :     // Server mode: called when a complete HTTP request is parsed.
      63                 :             :     // First argument is the connection itself (for send_response).
      64                 :             :     using RequestHandler = std::function<void(HTTPConnection*, HttpRequest&&)>;
      65                 :           3 :     void set_request_handler(RequestHandler handler) {
      66                 :           3 :         request_handler_ = std::move(handler);
      67                 :           3 :     }
      68                 :             : 
      69                 :             :     // Client mode: called when a complete HTTP response is parsed.
      70                 :             :     using ResponseHandler =
      71                 :             :         std::function<void(HTTPConnection*, int status_code,
      72                 :             :                            std::vector<HttpHeader> headers, StreamBuffer body)>;
      73                 :           1 :     void set_response_handler(ResponseHandler handler) {
      74                 :           1 :         response_handler_ = std::move(handler);
      75                 :           1 :     }
      76                 :             : 
      77                 :             :     // Called on HTTP parse errors.
      78                 :             :     using ErrorHandler = std::function<void(HTTPConnection*, const error&)>;
      79                 :           1 :     void set_error_handler(ErrorHandler handler) {
      80                 :           1 :         error_handler_ = std::move(handler);
      81                 :           1 :     }
      82                 :             : 
      83                 :             :     // ---- Send ----
      84                 :             : 
      85                 :             :     // Build an HTTP/1.1 response and send it on the wire.
      86                 :             :     void send_response(HttpStatusCode code,
      87                 :             :                        std::vector<HttpHeader> headers = {},
      88                 :             :                        StreamBuffer body = {});
      89                 :             : 
      90                 :             :     // Send raw bytes (appends to write buffer, flushes asynchronously).
      91                 :             :     void send_raw(const StreamBuffer& data);
      92                 :             : 
      93                 :             :     // Send interface inherited from Connection.
      94                 :             :     void send(const StreamBuffer& data) override;
      95                 :             : 
      96                 :             :     // ---- Connection lifecycle ----
      97                 :             : 
      98                 :             :     void close() override;
      99                 :             : 
     100                 :             :     // Called by the event loop when fd is readable.
     101                 :             :     void handle_read() override;
     102                 :             : 
     103                 :             :     // Handle send completion (called by EventLoop).
     104                 :             :     void handle_send_completion(int result) override;
     105                 :             : 
     106                 :             :     // Delegate keep-alive decision to the parser.
     107                 :             :     bool should_keep_alive() const;
     108                 :             : 
     109                 :             :     // Return the connection mode.
     110                 :             :     HTTPConnectionMode mode() const { return mode_; }
     111                 :             : 
     112                 :             :   private:
     113                 :             :     HTTPConnection(int fd, EndPoint local_endpoint, EndPoint remote_endpoint,
     114                 :             :                    EventLoop* loop, HTTPConnectionMode mode);
     115                 :             : 
     116                 :             :     // Flush the write buffer via async_send.
     117                 :             :     void flush_write_buffer();
     118                 :             : 
     119                 :             :     // Write buffer initial capacity.
     120                 :             :     static constexpr size_t kWriteChunkSize = 65536;
     121                 :             : 
     122                 :             :     // HTTP/1.1 parser (owns llhttp instance).
     123                 :             :     std::unique_ptr<HttpParser> parser_;
     124                 :             : 
     125                 :             :     // Accumulation buffer for incoming bytes.
     126                 :             :     adt::StreamBuffer read_buffer_;
     127                 :             : 
     128                 :             :     // Write buffer.
     129                 :             :     adt::StreamBuffer write_buffer_;
     130                 :             : 
     131                 :             :     // True while async send is in progress.
     132                 :             :     bool is_sending_ = false;
     133                 :             : 
     134                 :             :     // Connection mode.
     135                 :             :     HTTPConnectionMode mode_;
     136                 :             : 
     137                 :             :     // Callbacks.
     138                 :             :     RequestHandler request_handler_;
     139                 :             :     ResponseHandler response_handler_;
     140                 :             :     ErrorHandler error_handler_;
     141                 :             : };
     142                 :             : 
     143                 :             : } // namespace net
     144                 :             : } // namespace hpactor
        

Generated by: LCOV version 2.0-1