LCOV - code coverage report
Current view: top level - include/hpactor/net - http_gateway.hpp (source / functions) Coverage Total Hit
Test: HPActor Coverage Lines: 100.0 % 2 2
Test Date: 2026-05-20 02:24:49 Functions: 100.0 % 2 2
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/net/acceptor.hpp>
      18                 :             : #include <hpactor/net/event_loop.hpp>
      19                 :             : #include <hpactor/net/http_connection.hpp>
      20                 :             : #include <hpactor/net/http_types.hpp>
      21                 :             : #include <hpactor/ref/actor_address.hpp>
      22                 :             : #include <hpactor/types/types.hpp>
      23                 :             : 
      24                 :             : #include <functional>
      25                 :             : #include <memory>
      26                 :             : #include <mutex>
      27                 :             : #include <string>
      28                 :             : #include <vector>
      29                 :             : 
      30                 :             : namespace hpactor {
      31                 :             : namespace net {
      32                 :             : 
      33                 :             : // ---------------------------------------------------------------------------
      34                 :             : // RouteRegistry — URL pattern matcher for HTTP routing
      35                 :             : // ---------------------------------------------------------------------------
      36                 :             : 
      37                 :             : enum class PatternSegmentType { Literal, NamedParam, SingleWildcard, MultiWildcard };
      38                 :             : 
      39                 :             : struct PatternSegment {
      40                 :             :     PatternSegmentType type;
      41                 :             :     std::string name;
      42                 :             : };
      43                 :             : 
      44                 :             : class RouteRegistry {
      45                 :             :   public:
      46                 :             :     using MessageBuilder =
      47                 :             :         std::function<std::pair<ActorAddress, TypedMessage>(const HttpRequest&)>;
      48                 :             : 
      49                 :           1 :     RouteRegistry() = default;
      50                 :             : 
      51                 :             :     void add(HttpMethod method, std::string pattern,
      52                 :             :              MessageBuilder builder, int priority = 0);
      53                 :             : 
      54                 :             :     // Returns the builder for the matching route, or nullptr
      55                 :             :     const MessageBuilder* match(HttpMethod method, const std::string& path,
      56                 :             :                                 HttpRequest& req) const;
      57                 :             : 
      58                 :             :     // Returns true if any route is registered
      59                 :             :     bool empty() const { return routes_.empty(); }
      60                 :             : 
      61                 :             :   private:
      62                 :             :     struct Route {
      63                 :             :         HttpMethod method;
      64                 :             :         std::vector<PatternSegment> segments;
      65                 :             :         MessageBuilder builder;
      66                 :             :         int priority;
      67                 :             :     };
      68                 :             :     std::vector<Route> routes_;
      69                 :             : };
      70                 :             : 
      71                 :             : // ---------------------------------------------------------------------------
      72                 :             : // HTTPGateway — reusable HTTP server (I/O and protocol, no actor coupling)
      73                 :             : // ---------------------------------------------------------------------------
      74                 :             : class HTTPGateway {
      75                 :             :   public:
      76                 :             :     using RequestHandler =
      77                 :             :         std::function<void(HTTPConnection*, HttpRequest&&)>;
      78                 :             :     using ErrorHandler =
      79                 :             :         std::function<void(HTTPConnection*, const error&)>;
      80                 :             : 
      81                 :             :     HTTPGateway();
      82                 :             :     ~HTTPGateway();
      83                 :             : 
      84                 :             :     HTTPGateway(const HTTPGateway&) = delete;
      85                 :             :     HTTPGateway& operator=(const HTTPGateway&) = delete;
      86                 :             : 
      87                 :             :     // Lifecycle — call listen() before run_once()
      88                 :             :     bool listen(uint16_t port, const std::string& bind_host = "0.0.0.0");
      89                 :             :     void stop();
      90                 :             :     bool is_listening() const;
      91                 :             :     uint16_t port() const;
      92                 :             : 
      93                 :             :     // Main loop tick — call from daemon thread
      94                 :             :     void run_once();
      95                 :             : 
      96                 :             :     // Handlers
      97                 :             :     void set_request_handler(RequestHandler handler);
      98                 :             :     void set_error_handler(ErrorHandler handler);
      99                 :             : 
     100                 :             :     // Send HTTP response on a connection
     101                 :             :     void send_response(HTTPConnection* conn, HttpStatusCode code,
     102                 :             :                        std::vector<HttpHeader> headers, StreamBuffer body);
     103                 :             : 
     104                 :             :     // Connection management
     105                 :             :     void close_connection(HTTPConnection* conn);
     106                 :             :     void set_max_connections(size_t max);
     107                 :             :     void set_max_request_size(size_t max);
     108                 :             : 
     109                 :             :     // Access to event loop for scheduling timers (used by actor for timeouts)
     110                 :           1 :     EventLoop& event_loop() { return loop_; }
     111                 :             : 
     112                 :             :   private:
     113                 :             :     void on_accept(int client_fd, EndPoint remote_endpoint);
     114                 :             : 
     115                 :             :     EventLoop loop_;
     116                 :             :     std::unique_ptr<TcpAcceptor> acceptor_;
     117                 :             :     std::vector<HTTPConnectionPtr> connections_;
     118                 :             :     std::mutex conn_mutex_;
     119                 :             :     RequestHandler request_handler_;
     120                 :             :     ErrorHandler error_handler_;
     121                 :             :     std::string bind_host_;
     122                 :             :     uint16_t port_{0};
     123                 :             :     size_t max_connections_{1000};
     124                 :             :     size_t max_request_size_{1048576};
     125                 :             : };
     126                 :             : 
     127                 :             : } // namespace net
     128                 :             : } // namespace hpactor
        

Generated by: LCOV version 2.0-1