LCOV - code coverage report
Current view: top level - http/common - http_request.hpp (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 92.9 % 14 13
Test Date: 2026-02-20 15:38:22 Functions: 83.3 % 6 5

            Line data    Source code
       1              : #ifndef HTTP_REQUEST_HPP
       2              : #define HTTP_REQUEST_HPP
       3              : 
       4              : #include <boost/asio.hpp>
       5              : #include <regex>
       6              : #include <utility>
       7              : #include "headers.hpp"
       8              : #include "../client/cookie_store.hpp"
       9              : #include "../util/url.hpp"
      10              : 
      11              : namespace thinger::http {
      12              : 
      13              :     enum class method{
      14              :         GET,
      15              :         HEAD,
      16              :         POST,
      17              :         PUT,
      18              :         DELETE,
      19              :         TRACE,
      20              :         OPTIONS,
      21              :         CONNECT,
      22              :         PATCH,
      23              :         UNKNOWN
      24              :     };
      25              : 
      26              :     method get_method(const std::string& method);
      27              :     const std::string& get_method(method);
      28              :     static const std::string https = "https";
      29              :     static const std::string wss = "wss";
      30              :     static const std::string ws = "ws";
      31              :     static const std::string https_port = "443";
      32              :     static const std::string http = "http";
      33              :     static const std::string http_port = "80";
      34              : 
      35              : class http_request : public headers{
      36              : 
      37              : public:
      38              : 
      39              :     // constructor
      40         2048 :     http_request() = default;
      41         2048 :     ~http_request() override = default;
      42              : 
      43              :     // setters
      44              :     bool set_url(const std::string& url);
      45              :     void set_host(std::string host);
      46              :     void set_port(const std::string &port);
      47              :     void set_protocol(std::string protocol);
      48              :     void set_ssl(bool ssl);
      49              :     void set_unix_socket(const std::string& unix_socket);
      50              :     void set_uri(const std::string &uri);
      51              :     void set_method(const std::string &method);
      52              :     void set_method(http::method method);
      53              :     void set_content(std::string content);
      54              :     void set_content(std::string content, std::string content_type);
      55              :     void add_uri_parameter(const std::string &key, const std::string &value);
      56              :     void set_resource(const std::string& resource);
      57              : 
      58              :     // getters
      59              :     std::string get_url() const;
      60              :     std::string get_base_path() const;
      61              :     const std::string& get_body() const;
      62              :     bool has_resource() const;
      63              :     bool has_query_parameters() const;
      64              :     bool has_uri_parameter(const std::string& key) const;
      65              :     bool has_uri_value(const std::string& key, const std::string& value) const;
      66              :     bool has_uri_parameters() const;
      67              :     const std::string& get_uri_parameter(const std::string& key) const;
      68              :     const std::multimap<std::string, std::string>& get_uri_parameters() const;
      69              :     bool has_content() const;
      70              :     method get_method() const;
      71              :     const std::string& get_method_string() const;
      72              :     std::string get_query_string() const;
      73              :     std::string get_path() const;
      74              :     std::string& get_uri();
      75              :     const std::string& get_uri() const;
      76              :     const std::string& get_unix_socket();
      77              :     const std::string& get_protocol() const;
      78              :     bool is_default_port() const;
      79              :     const std::string &get_port() const;
      80              :     cookie_store& get_cookie_store();
      81              :     const std::string &get_host() const;
      82              :     bool is_ssl() const;
      83              :     const std::string& get_resource() const;
      84              :     std::string& get_resource();
      85              :     std::string& get_body();
      86              : 
      87              :     template<class T>
      88            9 :     T get_uri_parameter(const std::string& key, T default_value) const{
      89            9 :         if(has_uri_parameter(key)){
      90            6 :             const std::string& ts_str = get_uri_parameter(key);
      91            6 :             if(!ts_str.empty()){
      92              :                 try
      93              :                 {
      94            6 :                     T value = boost::lexical_cast<T>(ts_str);
      95            6 :                     return value;
      96            0 :                 }catch(const boost::bad_lexical_cast &){
      97              : 
      98              :                 }
      99              :             }
     100              :         }
     101            3 :         return default_value;
     102              :     }
     103              : 
     104              :     // chunked callbacks
     105              :     std::function<void(int, const std::string&)> get_chunked_callback();
     106              :     void set_chunked_callback(std::function<void(int, const std::string&)> callback);
     107              : 
     108              :     // stream related
     109              :     bool end_stream() override;
     110              :     size_t get_size() override;
     111              :     void to_buffer(std::vector<boost::asio::const_buffer> &buffer) const override;
     112              :     void process_header(std::string key, std::string value) override;
     113              : 
     114              :     // logs
     115              :     void log(const char* scope, int level) const override;
     116              : 
     117              :     // factory methods
     118              :     static std::shared_ptr<http_request> create_http_request(const std::string& method, const std::string& url);
     119              :     static std::shared_ptr<http_request> create_http_request(http::method method, const std::string& url, const std::string& unix_socket="");
     120              : 
     121              :     // body status (infrastructure for deferred body reading)
     122          869 :     bool has_pending_body() const {
     123          969 :         return (get_content_length() > 0 && content_.size() < get_content_length())
     124          969 :             || chunked_transfer_;
     125              :     }
     126              : 
     127              :     size_t pending_body_size() const {
     128              :         return get_content_length() > content_.size() ? get_content_length() - content_.size() : 0;
     129              :     }
     130              : 
     131          606 :     bool is_chunked_transfer() const { return chunked_transfer_; }
     132              : 
     133              :     // other
     134              :     void refresh_uri();
     135              : 
     136              : private:
     137              :     bool ssl_ = false;
     138              :     method method_ = method::UNKNOWN;
     139              :     std::string uri_;
     140              :     std::string resource_;
     141              :     std::multimap<std::string, std::string> uri_params_;
     142              :     std::string content_;
     143              :     std::string host_;
     144              :     std::string port_;
     145              :     std::string protocol_;
     146              :     std::string unix_socket_;
     147              :     bool chunked_transfer_ = false;
     148              :     cookie_store cookie_store_;
     149              :     std::function<void(int, const std::string&)> on_chunked_;
     150              : 
     151              : };
     152              : 
     153              : 
     154              : }
     155              : 
     156              : #endif
        

Generated by: LCOV version 2.0-1