|
1 | 1 | /***************************************************************************** |
2 | 2 | BSD 3-Clause License |
3 | 3 |
|
4 | | -Copyright (c) 2024, 🍀☀🌕🌥 🌊 |
| 4 | +Copyright (c) 2024, kcenon |
5 | 5 | All rights reserved. |
6 | 6 |
|
7 | 7 | Redistribution and use in source and binary forms, with or without |
@@ -32,235 +32,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | 32 |
|
33 | 33 | #pragma once |
34 | 34 |
|
35 | | -#include "kcenon/network/core/messaging_client.h" |
36 | | -#include "internal/http/http_types.h" |
37 | | -#include "internal/http/http_parser.h" |
38 | | -#include "kcenon/network/utils/result_types.h" |
39 | | -#include <string> |
40 | | -#include <map> |
41 | | -#include <vector> |
42 | | -#include <memory> |
43 | | -#include <chrono> |
44 | | -#include <optional> |
45 | | - |
46 | | -namespace kcenon::network::core |
47 | | -{ |
48 | | - /*! |
49 | | - * \struct http_url |
50 | | - * \brief Parsed URL components |
51 | | - */ |
52 | | - struct http_url |
53 | | - { |
54 | | - std::string scheme; // http or https |
55 | | - std::string host; // hostname or IP |
56 | | - unsigned short port = 0; // port number (0 = default) |
57 | | - std::string path; // URI path |
58 | | - std::map<std::string, std::string> query; // query parameters |
59 | | - |
60 | | - /*! |
61 | | - * \brief Parse URL string into components |
62 | | - * \param url URL string (e.g., "http://example.com:8080/path?key=value") |
63 | | - * \return Result containing parsed URL or error |
64 | | - */ |
65 | | - static auto parse(const std::string& url) -> Result<http_url>; |
66 | | - |
67 | | - /*! |
68 | | - * \brief Get default port for scheme |
69 | | - * \return Default port (80 for http, 443 for https) |
70 | | - */ |
71 | | - auto get_default_port() const -> unsigned short; |
72 | | - }; |
73 | | - |
74 | | - /*! |
75 | | - * \class http_client |
76 | | - * \brief HTTP/1.1 client built on top of messaging_client |
77 | | - * |
78 | | - * ### Thread Safety |
79 | | - * - All public methods are thread-safe |
80 | | - * - Multiple requests can be made concurrently from different threads |
81 | | - * - Each request uses its own messaging_client instance |
82 | | - * |
83 | | - * ### Features |
84 | | - * - HTTP/1.1 protocol support |
85 | | - * - GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH methods |
86 | | - * - Custom headers support |
87 | | - * - Query parameters support |
88 | | - * - Request/response body handling |
89 | | - * - Timeout configuration |
90 | | - * - Automatic Content-Length calculation |
91 | | - * - Connection: close header for proper cleanup |
92 | | - * |
93 | | - * ### Usage Example |
94 | | - * \code |
95 | | - * auto client = std::make_shared<http_client>(); |
96 | | - * |
97 | | - * // Simple GET request |
98 | | - * auto response = client->get("http://example.com/api/users"); |
99 | | - * if (response) { |
100 | | - * std::cout << "Status: " << response->status_code << "\n"; |
101 | | - * std::cout << "Body: " << response->get_body_string() << "\n"; |
102 | | - * } |
103 | | - * |
104 | | - * // POST with JSON body |
105 | | - * std::map<std::string, std::string> headers = { |
106 | | - * {"Content-Type", "application/json"} |
107 | | - * }; |
108 | | - * std::string json_body = R"({"name": "John", "age": 30})"; |
109 | | - * auto post_response = client->post("http://example.com/api/users", |
110 | | - * json_body, headers); |
111 | | - * \endcode |
112 | | - * |
113 | | - * ### Limitations |
114 | | - * - Only HTTP/1.1 supported (no HTTP/2) |
115 | | - * - No persistent connections (Connection: close) |
116 | | - * - No chunked transfer encoding |
117 | | - * - No automatic redirect following |
118 | | - * - No cookie management |
119 | | - * - No compression support |
120 | | - */ |
121 | | - class http_client : public std::enable_shared_from_this<http_client> |
122 | | - { |
123 | | - public: |
124 | | - /*! |
125 | | - * \brief Construct HTTP client with default timeout |
126 | | - */ |
127 | | - http_client(); |
128 | | - |
129 | | - /*! |
130 | | - * \brief Construct HTTP client with custom timeout |
131 | | - * \param timeout_ms Timeout in milliseconds |
132 | | - */ |
133 | | - explicit http_client(std::chrono::milliseconds timeout_ms); |
134 | | - |
135 | | - /*! |
136 | | - * \brief Destructor |
137 | | - */ |
138 | | - ~http_client() = default; |
139 | | - |
140 | | - /*! |
141 | | - * \brief Perform HTTP GET request |
142 | | - * \param url Full URL (e.g., "http://example.com/path") |
143 | | - * \param query Query parameters (optional) |
144 | | - * \param headers Custom headers (optional) |
145 | | - * \return Result containing HTTP response or error |
146 | | - */ |
147 | | - auto get(const std::string& url, |
148 | | - const std::map<std::string, std::string>& query = {}, |
149 | | - const std::map<std::string, std::string>& headers = {}) |
150 | | - -> Result<internal::http_response>; |
151 | | - |
152 | | - /*! |
153 | | - * \brief Perform HTTP POST request |
154 | | - * \param url Full URL |
155 | | - * \param body Request body |
156 | | - * \param headers Custom headers (optional) |
157 | | - * \return Result containing HTTP response or error |
158 | | - */ |
159 | | - auto post(const std::string& url, |
160 | | - const std::string& body, |
161 | | - const std::map<std::string, std::string>& headers = {}) |
162 | | - -> Result<internal::http_response>; |
163 | | - |
164 | | - /*! |
165 | | - * \brief Perform HTTP POST request with binary body |
166 | | - * \param url Full URL |
167 | | - * \param body Request body as bytes |
168 | | - * \param headers Custom headers (optional) |
169 | | - * \return Result containing HTTP response or error |
170 | | - */ |
171 | | - auto post(const std::string& url, |
172 | | - const std::vector<uint8_t>& body, |
173 | | - const std::map<std::string, std::string>& headers = {}) |
174 | | - -> Result<internal::http_response>; |
175 | | - |
176 | | - /*! |
177 | | - * \brief Perform HTTP PUT request |
178 | | - * \param url Full URL |
179 | | - * \param body Request body |
180 | | - * \param headers Custom headers (optional) |
181 | | - * \return Result containing HTTP response or error |
182 | | - */ |
183 | | - auto put(const std::string& url, |
184 | | - const std::string& body, |
185 | | - const std::map<std::string, std::string>& headers = {}) |
186 | | - -> Result<internal::http_response>; |
187 | | - |
188 | | - /*! |
189 | | - * \brief Perform HTTP DELETE request |
190 | | - * \param url Full URL |
191 | | - * \param headers Custom headers (optional) |
192 | | - * \return Result containing HTTP response or error |
193 | | - */ |
194 | | - auto del(const std::string& url, |
195 | | - const std::map<std::string, std::string>& headers = {}) |
196 | | - -> Result<internal::http_response>; |
197 | | - |
198 | | - /*! |
199 | | - * \brief Perform HTTP HEAD request |
200 | | - * \param url Full URL |
201 | | - * \param headers Custom headers (optional) |
202 | | - * \return Result containing HTTP response or error |
203 | | - */ |
204 | | - auto head(const std::string& url, |
205 | | - const std::map<std::string, std::string>& headers = {}) |
206 | | - -> Result<internal::http_response>; |
207 | | - |
208 | | - /*! |
209 | | - * \brief Perform HTTP PATCH request |
210 | | - * \param url Full URL |
211 | | - * \param body Request body |
212 | | - * \param headers Custom headers (optional) |
213 | | - * \return Result containing HTTP response or error |
214 | | - */ |
215 | | - auto patch(const std::string& url, |
216 | | - const std::string& body, |
217 | | - const std::map<std::string, std::string>& headers = {}) |
218 | | - -> Result<internal::http_response>; |
219 | | - |
220 | | - /*! |
221 | | - * \brief Set request timeout |
222 | | - * \param timeout_ms Timeout in milliseconds |
223 | | - */ |
224 | | - auto set_timeout(std::chrono::milliseconds timeout_ms) -> void; |
225 | | - |
226 | | - /*! |
227 | | - * \brief Get current timeout setting |
228 | | - * \return Timeout in milliseconds |
229 | | - */ |
230 | | - auto get_timeout() const -> std::chrono::milliseconds; |
231 | | - |
232 | | - private: |
233 | | - /*! |
234 | | - * \brief Perform generic HTTP request |
235 | | - * \param method HTTP method |
236 | | - * \param url Full URL |
237 | | - * \param body Request body (optional) |
238 | | - * \param headers Custom headers |
239 | | - * \param query Query parameters |
240 | | - * \return Result containing HTTP response or error |
241 | | - */ |
242 | | - auto request(internal::http_method method, |
243 | | - const std::string& url, |
244 | | - const std::vector<uint8_t>& body, |
245 | | - const std::map<std::string, std::string>& headers, |
246 | | - const std::map<std::string, std::string>& query) |
247 | | - -> Result<internal::http_response>; |
248 | | - |
249 | | - /*! |
250 | | - * \brief Build HTTP request from components |
251 | | - * \param method HTTP method |
252 | | - * \param url_info Parsed URL |
253 | | - * \param body Request body |
254 | | - * \param headers Custom headers |
255 | | - * \return HTTP request object |
256 | | - */ |
257 | | - auto build_request(internal::http_method method, |
258 | | - const http_url& url_info, |
259 | | - const std::vector<uint8_t>& body, |
260 | | - const std::map<std::string, std::string>& headers) |
261 | | - -> internal::http_request; |
262 | | - |
263 | | - std::chrono::milliseconds timeout_; |
264 | | - }; |
265 | | - |
266 | | -} // namespace kcenon::network::core |
| 35 | +// DEPRECATED: This header location is deprecated. |
| 36 | +// Please use facade headers for new code. |
| 37 | +#ifndef NETWORK_SYSTEM_SUPPRESS_DEPRECATION_WARNINGS |
| 38 | +#if defined(__GNUC__) || defined(__clang__) |
| 39 | +#pragma message("DEPRECATED: include path 'kcenon/network/http/http_client.h' is deprecated. " \ |
| 40 | + "Use facade headers instead.") |
| 41 | +#elif defined(_MSC_VER) |
| 42 | +#pragma message("DEPRECATED: include path 'kcenon/network/http/http_client.h' is deprecated. " \ |
| 43 | + "Use facade headers instead.") |
| 44 | +#endif |
| 45 | +#endif |
| 46 | + |
| 47 | +// Include internal header for backward compatibility |
| 48 | +#include "internal/http/http_client.h" |
0 commit comments