| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | #include "gate/net/httptypes.hpp" | ||
| 29 | #include "gate/strings.hpp" | ||
| 30 | #include "gate/exceptions.hpp" | ||
| 31 | #include "gate/utilities.h" | ||
| 32 | |||
| 33 | namespace gate | ||
| 34 | { | ||
| 35 | namespace net | ||
| 36 | { | ||
| 37 | |||
| 38 | 6 | HttpRequest::HttpRequest(String const& method, String const& host, String const& path) | |
| 39 | { | ||
| 40 | 6 | const result_t result = gate_http_request_init(this->c_impl(), method.c_impl(), host.c_impl(), path.c_impl()); | |
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | GATEXX_CHECK_ERROR(result); |
| 42 | 6 | } | |
| 43 | |||
| 44 | 1 | HttpRequest::HttpRequest(String const& method, Uri const& uri) | |
| 45 | { | ||
| 46 | 1 | const result_t result = gate_http_request_init_uri(this->c_impl(), method.c_impl(), uri.c_impl()); | |
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
| 48 | 1 | } | |
| 49 | |||
| 50 | |||
| 51 | 1 | HttpRequest::HttpRequest(HttpRequest const& src) | |
| 52 | { | ||
| 53 | 1 | const result_t result = gate_http_request_copy(this->c_impl(), src.c_impl()); | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
| 55 | 1 | } | |
| 56 | 4 | HttpRequest::HttpRequest(gate_http_request_t const& src) | |
| 57 | { | ||
| 58 | 4 | const result_t result = gate_http_request_copy(this->c_impl(), &src); | |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | GATEXX_CHECK_ERROR(result); |
| 60 | 4 | } | |
| 61 | |||
| 62 | 1 | HttpRequest& HttpRequest::operator=(HttpRequest const& src) | |
| 63 | { | ||
| 64 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 65 | { | ||
| 66 | 1 | gate_http_request_t tmp = GATE_INIT_EMPTY; | |
| 67 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_http_request_copy(&tmp, src.c_impl()); |
| 68 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
| 69 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_http_request_release(this->c_impl()); |
| 70 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_mem_copy(this->c_impl(), &tmp, sizeof(tmp)); |
| 71 | } | ||
| 72 | 1 | return *this; | |
| 73 | } | ||
| 74 | 24 | HttpRequest::~HttpRequest() noexcept | |
| 75 | { | ||
| 76 | 12 | gate_http_request_release(this->c_impl()); | |
| 77 | 12 | } | |
| 78 | |||
| 79 | 27 | gate_http_request_t* HttpRequest::c_impl() noexcept | |
| 80 | { | ||
| 81 | 27 | return static_cast<gate_http_request_t*>(this); | |
| 82 | } | ||
| 83 | 2 | gate_http_request_t const* HttpRequest::c_impl() const noexcept | |
| 84 | { | ||
| 85 | 2 | return static_cast<gate_http_request_t const*>(this); | |
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | 7 | static void replace_string(gate_string_t& target, String const& src, char const* funcName) | |
| 90 | { | ||
| 91 | gate_string_t tmp; | ||
| 92 |
2/4✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
|
7 | if (NULL == gate_string_clone(&tmp, src.c_impl())) |
| 93 | { | ||
| 94 | ✗ | gate::raiseError(results::OutOfMemory, funcName); | |
| 95 | } | ||
| 96 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | gate_string_release(&target); |
| 97 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | gate_mem_copy(&target, &tmp, sizeof(tmp)); |
| 98 | 7 | } | |
| 99 | |||
| 100 | 4 | String HttpRequest::getMethod() const | |
| 101 | { | ||
| 102 | 4 | return String::duplicate(this->method); | |
| 103 | } | ||
| 104 | ✗ | void HttpRequest::setMethod(String const& value) | |
| 105 | { | ||
| 106 | ✗ | replace_string(this->method, value, "HttpRequest::setMethod"); | |
| 107 | ✗ | } | |
| 108 | |||
| 109 | 4 | String HttpRequest::getPath() const | |
| 110 | { | ||
| 111 | 4 | return String::duplicate(this->path); | |
| 112 | } | ||
| 113 | ✗ | void HttpRequest::setPath(String const& value) | |
| 114 | { | ||
| 115 | ✗ | replace_string(this->path, value, "HttpRequest::setPath"); | |
| 116 | ✗ | } | |
| 117 | |||
| 118 | 1 | String HttpRequest::getHost() const | |
| 119 | { | ||
| 120 | 1 | return String::duplicate(this->host); | |
| 121 | } | ||
| 122 | 1 | void HttpRequest::setHost(String const& value) | |
| 123 | { | ||
| 124 | 1 | replace_string(this->host, value, "HttpRequest::setHost"); | |
| 125 | 1 | } | |
| 126 | |||
| 127 | |||
| 128 | 1 | void HttpRequest::setAuthentication(String const& user, String const& password) | |
| 129 | { | ||
| 130 | 1 | replace_string(this->auth_user, user, "HttpRequest::setAuthentication"); | |
| 131 | 1 | replace_string(this->auth_pass, password, "HttpRequest::setAuthentication"); | |
| 132 | 1 | } | |
| 133 | 1 | String HttpRequest::getAuthUser() const | |
| 134 | { | ||
| 135 | 1 | return String::duplicate(this->auth_user); | |
| 136 | } | ||
| 137 | 1 | String HttpRequest::getAuthPass() const | |
| 138 | { | ||
| 139 | 1 | return String::duplicate(this->auth_pass); | |
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | 1 | String HttpRequest::getVersion() const | |
| 144 | { | ||
| 145 | 1 | return String::duplicate(this->version); | |
| 146 | } | ||
| 147 | 1 | void HttpRequest::setVersion(String const& value) | |
| 148 | { | ||
| 149 | 1 | replace_string(this->version, value, "HttpRequest::setVersion"); | |
| 150 | 1 | } | |
| 151 | |||
| 152 | 4 | String HttpRequest::getHeader(String const& headerKey) const | |
| 153 | { | ||
| 154 | 4 | gate_string_t const* str = gate_util_stringmap_get_string((gate_map_t*)&this->headers, headerKey.c_impl()); | |
| 155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (str == NULL) |
| 156 | { | ||
| 157 | ✗ | GATEXX_RAISE_EXCEPTION(results::NoMatch, "Header entry not found", 0); | |
| 158 | ✗ | return String(); | |
| 159 | } | ||
| 160 | 4 | return String::duplicate(*str); | |
| 161 | } | ||
| 162 | 7 | void HttpRequest::setHeader(String const& headerKey, String const& headerValue) | |
| 163 | { | ||
| 164 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
|
7 | if (NULL == gate_util_stringmap_add_string(&this->headers, headerKey.c_impl(), headerValue.c_impl())) |
| 165 | { | ||
| 166 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 167 | } | ||
| 168 | 7 | } | |
| 169 | ✗ | void HttpRequest::clearHeaders() | |
| 170 | { | ||
| 171 | ✗ | gate_map_clear(&this->headers); | |
| 172 | ✗ | } | |
| 173 | |||
| 174 | 2 | void HttpRequest::addAcceptType(String const& acceptType) | |
| 175 | { | ||
| 176 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!gate_util_stringset_add_string(&this->accept_types, acceptType.c_impl())) |
| 177 | { | ||
| 178 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 179 | } | ||
| 180 | 2 | } | |
| 181 | 2 | void HttpRequest::removeAcceptType(String const& acceptType) | |
| 182 | { | ||
| 183 | 2 | gate_util_stringset_remove_string(&this->accept_types, acceptType.c_impl()); | |
| 184 | 2 | } | |
| 185 | 1 | void HttpRequest::clearAcceptTypes() | |
| 186 | { | ||
| 187 | 1 | gate_set_clear(&this->accept_types); | |
| 188 | 1 | } | |
| 189 | |||
| 190 | 1 | uint32_t HttpRequest::getConnectTimeout() const | |
| 191 | { | ||
| 192 | 1 | return this->connect_timeout_ms; | |
| 193 | } | ||
| 194 | 5 | void HttpRequest::setConnectTimeout(uint32_t valueMS) | |
| 195 | { | ||
| 196 | 5 | this->connect_timeout_ms = valueMS; | |
| 197 | 5 | } | |
| 198 | |||
| 199 | 1 | uint32_t HttpRequest::getSendTimeout() const | |
| 200 | { | ||
| 201 | 1 | return this->send_timeout_ms; | |
| 202 | } | ||
| 203 | 5 | void HttpRequest::setSendTimeout(uint32_t valueMS) | |
| 204 | { | ||
| 205 | 5 | this->send_timeout_ms = valueMS; | |
| 206 | 5 | } | |
| 207 | |||
| 208 | 1 | uint32_t HttpRequest::getReceiveTimeout() const | |
| 209 | { | ||
| 210 | 1 | return this->receive_timeout_ms; | |
| 211 | } | ||
| 212 | 5 | void HttpRequest::setReceiveTimeout(uint32_t valueMS) | |
| 213 | { | ||
| 214 | 5 | this->receive_timeout_ms = valueMS; | |
| 215 | 5 | } | |
| 216 | |||
| 217 | 1 | void HttpRequest::setProxy(String const& proxyServer, uint16_t proxyPort, String const& user, String const& pass) | |
| 218 | { | ||
| 219 | static char const* const funcName = "HttpRequest::setProxy"; | ||
| 220 | 1 | replace_string(this->proxy_server, proxyServer, funcName); | |
| 221 | 1 | replace_string(this->proxy_user, user, funcName); | |
| 222 | 1 | replace_string(this->proxy_pass, pass, funcName); | |
| 223 | 1 | this->proxy_port = proxyPort; | |
| 224 | 1 | } | |
| 225 | ✗ | void HttpRequest::noProxy() | |
| 226 | { | ||
| 227 | ✗ | gate_string_release(&this->proxy_server); | |
| 228 | ✗ | gate_string_release(&this->proxy_user); | |
| 229 | ✗ | gate_string_release(&this->proxy_pass); | |
| 230 | ✗ | this->proxy_port = 0; | |
| 231 | ✗ | } | |
| 232 | 1 | String HttpRequest::getProxyServer() const | |
| 233 | { | ||
| 234 | 1 | return String::duplicate(this->proxy_server); | |
| 235 | } | ||
| 236 | 1 | uint16_t HttpRequest::getProxyPort() const | |
| 237 | { | ||
| 238 | 1 | return this->proxy_port; | |
| 239 | } | ||
| 240 | 1 | String HttpRequest::getProxyUser() const | |
| 241 | { | ||
| 242 | 1 | return String::duplicate(this->proxy_user); | |
| 243 | } | ||
| 244 | 1 | String HttpRequest::getProxyPass() const | |
| 245 | { | ||
| 246 | 1 | return String::duplicate(this->proxy_pass); | |
| 247 | } | ||
| 248 | |||
| 249 | 2 | Stream HttpRequest::getUploadStream() const | |
| 250 | { | ||
| 251 | 2 | Stream strm(this->upload_stream); | |
| 252 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | gate_object_retain(this->upload_stream); |
| 253 | 2 | return strm; | |
| 254 | } | ||
| 255 | |||
| 256 | 2 | void HttpRequest::setUploadStream(Stream& strm) | |
| 257 | { | ||
| 258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (this->upload_stream != NULL) |
| 259 | { | ||
| 260 | ✗ | gate_object_release(this->upload_stream); | |
| 261 | } | ||
| 262 | 2 | this->upload_stream = strm.c_impl(); | |
| 263 | 2 | gate_object_retain(this->upload_stream); | |
| 264 | 2 | } | |
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | 6 | HttpResponse::HttpResponse() | |
| 280 | { | ||
| 281 | 6 | result_t result = gate_http_response_init(this); | |
| 282 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | GATEXX_CHECK_ERROR(result); |
| 283 | 6 | } | |
| 284 | 1 | HttpResponse::HttpResponse(HttpResponse const& src) | |
| 285 | { | ||
| 286 | 1 | result_t result = gate_http_response_copy(this, &src); | |
| 287 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
| 288 | 1 | } | |
| 289 | 1 | HttpResponse& HttpResponse::operator=(HttpResponse const& src) | |
| 290 | { | ||
| 291 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 292 | { | ||
| 293 | 1 | gate_http_response_t tmp = GATE_INIT_EMPTY; | |
| 294 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_http_response_copy(&tmp, &src); |
| 295 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(result); |
| 296 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_http_response_release(this); |
| 297 | 1 | *static_cast<gate_http_response_t*>(this) = tmp; | |
| 298 | } | ||
| 299 | 1 | return *this; | |
| 300 | } | ||
| 301 | 14 | HttpResponse::~HttpResponse() noexcept | |
| 302 | { | ||
| 303 | 7 | gate_http_response_release(this); | |
| 304 | 7 | } | |
| 305 | 5 | uint32_t HttpResponse::getStatus() const | |
| 306 | { | ||
| 307 | 5 | return this->status_code; | |
| 308 | } | ||
| 309 | 4 | Map<String, String> HttpResponse::getHeaders() const | |
| 310 | { | ||
| 311 | 4 | Map<String, String> ret; | |
| 312 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | gate_map_iterator_t iter = gate_map_first(&this->headers); |
| 313 |
3/4✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 4 times.
|
16 | while (gate_map_iterator_valid(iter)) |
| 314 | { | ||
| 315 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | gate_string_t const* ptr_key = (gate_string_t const*)gate_map_iterator_key(iter); |
| 316 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | gate_string_t const* ptr_value = (gate_string_t const*)gate_map_iterator_value(iter); |
| 317 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | if (ptr_key && ptr_value) |
| 318 | { | ||
| 319 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
24 | String key(*ptr_key); |
| 320 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
24 | String value(*ptr_value); |
| 321 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | ret.add(key, value); |
| 322 | } | ||
| 323 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | iter = gate_map_iterator_next(iter); |
| 324 | } | ||
| 325 | 4 | return ret; | |
| 326 | } | ||
| 327 | 4 | Stream HttpResponse::getResponseStream() const | |
| 328 | { | ||
| 329 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (this->response_stream) |
| 330 | { | ||
| 331 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | Stream strm(this->response_stream); |
| 332 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | gate_object_retain(this->response_stream); |
| 333 | 4 | return strm; | |
| 334 | } | ||
| 335 | else | ||
| 336 | { | ||
| 337 | ✗ | return NullStream(); | |
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | 65 | String HttpResponse::getStatusText(uint32_t statusCode) | |
| 342 | { | ||
| 343 | 65 | char const* txt = gate_http_response_status_text(statusCode); | |
| 344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (NULL == txt) |
| 345 | { | ||
| 346 | ✗ | return String(); | |
| 347 | } | ||
| 348 | else | ||
| 349 | { | ||
| 350 | 65 | return String::createStatic(txt); | |
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | 2 | String MimeTypes::fromFileExt(String const& fileExt) | |
| 357 | { | ||
| 358 | gate_string_t mime; | ||
| 359 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | result_t res = gate_http_resolve_mime_type(fileExt.c_impl(), &mime); |
| 360 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (GATE_SUCCEEDED(res)) |
| 361 | { | ||
| 362 | 2 | return String::createFrom(mime); | |
| 363 | } | ||
| 364 | ✗ | return String(); | |
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | 1 | String HttpUrlCodec::build(KeyValueMap const& keyValueMap) | |
| 370 | { | ||
| 371 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | StringBuilder builder; |
| 372 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | result_t res = gate_http_build_urlencoded_content(keyValueMap.c_impl(), builder.c_impl()); |
| 373 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_ERROR(res); |
| 374 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return builder.toString(); |
| 375 | } | ||
| 376 | 1 | HttpUrlCodec::KeyValueMap HttpUrlCodec::parse(String const& text) | |
| 377 | { | ||
| 378 | 1 | KeyValueMap ret; | |
| 379 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | result_t res = gate_http_parse_urlencoded_content(text.c_impl(), ret.c_impl()); |
| 380 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
| 381 | 1 | return ret; | |
| 382 | } | ||
| 383 | |||
| 384 | } // end of namespace net | ||
| 385 | } // end of namespace gate | ||
| 386 | |||
| 387 |