src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 2
0.0%
Branches:
-
-%
Functions:
-
-%

utils/curl_utils.hpp
Line Branch Exec Source
1 #pragma once
2
3 #include "http_utils.hpp"
4 #include "interface/progress_spinner.hpp"
5 #include "interface/user_interface.hpp"
6 #include <curl/curl.h>
7 #include <iostream>
8 #include <map>
9 #include <optional>
10 #include <string>
11
12 /**
13 * Web content fetch result.
14 */
15 struct WebContent {
16 std::string read_buffer;
17 char error_buffer[CURL_ERROR_SIZE] = {};
18 long status = 0;
19 };
20
21 /**
22 * Web content stream result.
23 */
24 struct WebContentStream {
25 bool success;
26 char error_buffer[CURL_ERROR_SIZE];
27 std::string error_message;
28 int status = 0;
29 };
30
31 /**
32 * CURL HTTP client wrapper for making web requests to the Ollama server.
33 *
34 * Provides a simplified interface for HTTP operations by encapsulating CURL
35 * initialization, request execution, and resource cleanup. Manages CURL easy
36 * handles and HTTP headers, with support for both GET and POST requests via
37 * optional POST data parameter. Integrates with the UserInterface for logging
38 * and progress feedback during network operations. Follows RAII pattern with
39 * automatic resource cleanup in the destructor. Uses a progress spinner to
40 * provide visual feedback during potentially long-running HTTP operations.
41 */
42 class CurlUtils {
43 private:
44 /**
45 * CURL handle for HTTP requests. Initialized in init_curl().
46 */
47 ✗ std::unique_ptr<CURL, decltype(&curl_easy_cleanup)> curl{nullptr, curl_easy_cleanup};
48
49 /**
50 * CURL headers list for HTTP requests (Content-Type, etc.).
51 * Allocated in query methods and freed after each request.
52 */
53 ✗ std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)> headers{nullptr, curl_slist_free_all};
54
55 /**
56 * Pointer to the UserInterface for displaying messages and errors.
57 * Used for both CLI and interactive UI output.
58 */
59 std::shared_ptr<UserInterface> ui;
60
61 /**
62 * Progress spinner for visual feedback during network operations.
63 * Managed via std::unique_ptr for automatic cleanup.
64 */
65 std::unique_ptr<ProgressSpinner> spinner;
66
67 void initialize(void);
68
69 void cleanup(void);
70
71 void add_header(curl_slist*& headers, const std::string& line);
72
73 long check_service_status(const std::string& url, const std::map<std::string, std::string>& extra_headers = {},
74 long timeout_seconds = 5L);
75
76 static size_t curl_write_callback(char* contents, size_t size, size_t nmemb, std::string* userp);
77
78 // Streaming methods.
79 std::string stream_buffer;
80 static size_t curl_streaming_write_callback(char* contents, size_t size, size_t nmemb, void* userp);
81 void process_chunk(const std::string& chunk);
82 std::function<void(const std::string&)> stream_callback;
83
84 public:
85 explicit CurlUtils(std::shared_ptr<UserInterface> ui);
86
87 ~CurlUtils();
88
89 long wait_for_service(const std::string& url, const std::map<std::string, std::string>& extra_headers = {},
90 int max_retries = 3, int initial_delay_ms = 500);
91
92 // Non-streaming version (buffers entire response)
93 std::string fetch_web_content(const std::string& url, std::optional<std::string> post_data = std::nullopt,
94 std::optional<std::string> custom_request_type = std::nullopt,
95 std::optional<std::map<std::string, std::string>> extra_headers = std::nullopt,
96 bool fail_on_http_error = true);
97
98 // Streaming version (processes chunks as they arrive)
99 WebContentStream fetch_web_content_stream(
100 const std::string& url, std::optional<std::string> post_data, std::function<void(const std::string&)> on_chunk,
101 std::optional<std::string> custom_request_type = std::nullopt,
102 std::optional<std::map<std::string, std::string>> extra_headers = std::nullopt, bool fail_on_http_error = true);
103 };
104