oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
net_http.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/net_http.h>
17#include <ove/types.hpp>
18#include <ove/error.hpp>
19
20#ifdef CONFIG_OVE_NET_HTTP
21
22namespace ove::http
23{
24
45{
46 public:
50 Response() = default;
51
55 ~Response() noexcept
56 {
57 ove_http_response_free(&raw_);
58 }
59
60 Response(const Response &) = delete;
61 Response &operator=(const Response &) = delete;
62
67 Response(Response &&other) noexcept : raw_(other.raw_)
68 {
69 other.raw_ = {};
70 }
71
77 Response &operator=(Response &&other) noexcept
78 {
79 if (this != &other) {
80 ove_http_response_free(&raw_);
81 raw_ = other.raw_;
82 other.raw_ = {};
83 }
84 return *this;
85 }
86
88 int status() const
89 {
90 return raw_.status;
91 }
92
94 const char *body() const
95 {
96 return raw_.body;
97 }
98
100 size_t body_len() const
101 {
102 return raw_.body_len;
103 }
104
106 const char *headers() const
107 {
108 return raw_.headers;
109 }
110
112 size_t headers_len() const
113 {
114 return raw_.headers_len;
115 }
116
121 ove_http_response_t *raw()
122 {
123 return &raw_;
124 }
125
126 ove_http_response_t raw_{};
127};
128
141{
142 public:
150 {
151#ifdef CONFIG_OVE_ZERO_HEAP
152 int err = ove_http_client_init(&handle_, &storage_);
153#else
154 int err = ove_http_client_create(&handle_);
155#endif
156 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
157 }
158
164 ~Client() noexcept
165 {
166 if (!handle_)
167 return;
168#ifdef CONFIG_OVE_ZERO_HEAP
169 ove_http_client_deinit(handle_);
170#else
171 ove_http_client_destroy(handle_);
172#endif
173 }
174
175 Client(const Client &) = delete;
176 Client &operator=(const Client &) = delete;
177
178#ifdef CONFIG_OVE_ZERO_HEAP
179 Client(Client &&) = delete;
180 Client &operator=(Client &&) = delete;
181#else
186 Client(Client &&other) noexcept : handle_(other.handle_)
187 {
188 other.handle_ = nullptr;
189 }
190
196 Client &operator=(Client &&other) noexcept
197 {
198 if (this != &other) {
199 if (handle_)
200 ove_http_client_destroy(handle_);
201 handle_ = other.handle_;
202 other.handle_ = nullptr;
203 }
204 return *this;
205 }
206#endif
207
215 [[nodiscard]] Result<Response> get(const char *url) noexcept
216 {
217 Response resp;
218 const int rc = ove_http_get(handle_, url, &resp.raw_);
219 return from_rc(rc, std::move(resp));
220 }
221
231 [[nodiscard]] Result<Response> post(const char *url, const char *content_type,
232 const void *body, size_t body_len) noexcept
233 {
234 Response resp;
235 const int rc =
236 ove_http_post(handle_, url, content_type, body, body_len, &resp.raw_);
237 return from_rc(rc, std::move(resp));
238 }
239
250 [[nodiscard]] Result<Response> request(ove_http_method_t method, const char *url,
251 const char *content_type, const void *body,
252 size_t body_len) noexcept
253 {
254 Response resp;
255 const int rc = ove_http_request(handle_, method, url, content_type, body, body_len,
256 &resp.raw_);
257 return from_rc(rc, std::move(resp));
258 }
259
272 [[nodiscard]] Result<Response> request(ove_http_method_t method, const char *url,
273 const char *content_type, const void *body,
274 size_t body_len, const ove_http_header_t *headers,
275 size_t header_count) noexcept
276 {
277 Response resp;
278 const int rc = ove_http_request_ex(handle_, method, url, content_type, body,
279 body_len, headers, header_count, &resp.raw_);
280 return from_rc(rc, std::move(resp));
281 }
282
287 bool valid() const
288 {
289 return handle_ != nullptr;
290 }
291
296 ove_http_client_t handle() const
297 {
298 return handle_;
299 }
300
301 private:
302 ove_http_client_t handle_ = nullptr;
303#ifdef CONFIG_OVE_ZERO_HEAP
304 ove_http_client_storage_t storage_ = {};
305#endif
306};
307
308} /* namespace ove::http */
309
310#endif /* CONFIG_OVE_NET_HTTP */
RAII wrapper around an oveRTOS HTTP client handle.
Definition net_http.hpp:141
Result< Response > request(ove_http_method_t method, const char *url, const char *content_type, const void *body, size_t body_len, const ove_http_header_t *headers, size_t header_count) noexcept
Performs an HTTP request with custom headers.
Definition net_http.hpp:272
~Client() noexcept
Destroys the HTTP client, releasing the underlying resource.
Definition net_http.hpp:164
Result< Response > post(const char *url, const char *content_type, const void *body, size_t body_len) noexcept
Performs an HTTP POST request.
Definition net_http.hpp:231
ove_http_client_t handle() const
Returns the raw oveRTOS HTTP client handle.
Definition net_http.hpp:296
Client & operator=(Client &&other) noexcept
Move-assignment operator – transfers ownership of the client handle.
Definition net_http.hpp:196
bool valid() const
Returns true if the underlying client handle is non-null.
Definition net_http.hpp:287
Client()
Constructs and initialises the HTTP client.
Definition net_http.hpp:149
Result< Response > request(ove_http_method_t method, const char *url, const char *content_type, const void *body, size_t body_len) noexcept
Performs a generic HTTP request.
Definition net_http.hpp:250
Result< Response > get(const char *url) noexcept
Performs an HTTP GET request.
Definition net_http.hpp:215
Client(Client &&other) noexcept
Move constructor – transfers ownership of the client handle.
Definition net_http.hpp:186
RAII wrapper around an oveRTOS HTTP response.
Definition net_http.hpp:45
int status() const
Returns the HTTP status code (e.g. 200, 404).
Definition net_http.hpp:88
const char * body() const
Returns the response body (NUL-terminated).
Definition net_http.hpp:94
~Response() noexcept
Destroys the response, freeing body and header buffers.
Definition net_http.hpp:55
ove_http_response_t raw_
Definition net_http.hpp:126
size_t headers_len() const
Returns the response headers length in bytes.
Definition net_http.hpp:112
Response()=default
Constructs an empty response.
Response & operator=(Response &&other) noexcept
Move-assignment operator – frees current buffers and takes ownership.
Definition net_http.hpp:77
const char * headers() const
Returns the raw response headers.
Definition net_http.hpp:106
size_t body_len() const
Returns the response body length in bytes.
Definition net_http.hpp:100
Response(Response &&other) noexcept
Move constructor – transfers ownership of response buffers.
Definition net_http.hpp:67
ove_http_response_t * raw()
Returns a pointer to the underlying C response struct.
Definition net_http.hpp:121
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
C++ wrappers around the oveRTOS HTTP client API.
Definition net_http.hpp:23
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
Common type definitions and concepts for the C++ wrapper layer.