LCOV - code coverage report
Current view: top level - http/client - stream_types.hpp (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 100.0 % 7 7
Test Date: 2026-11-10 10:38:07 Functions: 100.0 % 5 5

            Line data    Source code
       1              : #ifndef THINGER_HTTP_CLIENT_STREAM_TYPES_HPP
       2              : #define THINGER_HTTP_CLIENT_STREAM_TYPES_HPP
       3              : 
       4              : #include <string>
       5              : #include <string_view>
       6              : #include <functional>
       7              : #include <map>
       8              : #include "../../util/types.hpp"
       9              : 
      10              : namespace thinger::http {
      11              : 
      12              : /**
      13              :  * Information passed to stream callbacks for each chunk of data.
      14              :  */
      15              : struct stream_info {
      16              :     std::string_view data;      // Current chunk data
      17              :     size_t downloaded;          // Total bytes downloaded so far
      18              :     size_t total;               // Total expected size (0 if unknown, e.g., chunked)
      19              :     int status_code;            // HTTP status code
      20              : };
      21              : 
      22              : /**
      23              :  * Result of a streaming operation.
      24              :  */
      25              : struct stream_result {
      26              :     int status_code = 0;
      27              :     std::string error;                              // Empty if no connection error
      28              :     // Response headers, as Node gives them: names in lowercase, and the values of a header
      29              :     // repeated joined with ", ". Set once the head arrives (also when the stream callback
      30              :     // stops the download), and for a proxy refusing the tunnel.
      31              :     std::map<std::string, std::string> headers;
      32              :     size_t bytes_transferred = 0;
      33              : 
      34              :     /**
      35              :      * Returns true if the request succeeded (no error and 2xx status).
      36              :      */
      37          102 :     bool ok() const {
      38          102 :         return error.empty() && status_code >= 200 && status_code < 300;
      39              :     }
      40              : 
      41              :     /**
      42              :      * Conversion to bool for if(result) checks.
      43              :      */
      44            8 :     explicit operator bool() const { return ok(); }
      45              : 
      46              :     /**
      47              :      * Returns true if the request completed (even if status is not 2xx).
      48              :      * Use this to distinguish between network errors and HTTP errors.
      49              :      */
      50            8 :     bool completed() const { return error.empty() && status_code > 0; }
      51              : 
      52              :     /**
      53              :      * Returns true if there was a network/connection error.
      54              :      */
      55            6 :     bool has_network_error() const { return !error.empty(); }
      56              : 
      57              :     /**
      58              :      * Returns true if the server returned an error status (4xx or 5xx).
      59              :      */
      60            6 :     bool has_http_error() const {
      61            6 :         return error.empty() && status_code >= 400;
      62              :     }
      63              : };
      64              : 
      65              : /**
      66              :  * Callback for streaming data.
      67              :  * Called for each chunk of data received.
      68              :  * Return true to continue, false to abort the download.
      69              :  */
      70              : using stream_callback = std::function<bool(const stream_info&)>;
      71              : 
      72              : /**
      73              :  * Callback for download progress.
      74              :  * @param downloaded Bytes downloaded so far
      75              :  * @param total Total bytes expected (0 if unknown)
      76              :  */
      77              : using progress_callback = sync_callback<void(size_t downloaded, size_t total)>;
      78              : 
      79              : } // namespace thinger::http
      80              : 
      81              : #endif // THINGER_HTTP_CLIENT_STREAM_TYPES_HPP
        

Generated by: LCOV version 2.0-1