|
12 | 12 | #include <optional> |
13 | 13 | #include <stdexcept> |
14 | 14 | #include <string> |
| 15 | +#include <string_view> |
| 16 | +#include <type_traits> |
15 | 17 | #include <utility> |
16 | 18 | #include <variant> |
17 | 19 | #include <vector> |
|
26 | 28 | #include "cpr/auth.h" |
27 | 29 | #include "cpr/bearer.h" |
28 | 30 | #include "cpr/body.h" |
| 31 | +#include "cpr/body_view.h" |
29 | 32 | #include "cpr/callback.h" |
30 | 33 | #include "cpr/connect_timeout.h" |
31 | 34 | #include "cpr/cookies.h" |
@@ -289,6 +292,10 @@ void Session::RemoveContent() { |
289 | 292 | curl_mime_free(curl_->multipart); |
290 | 293 | curl_->multipart = nullptr; |
291 | 294 | } |
| 295 | + } else if (std::holds_alternative<cpr::BodyView>(content_)) { |
| 296 | + // set default values, so curl does not send a body in subsequent requests |
| 297 | + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, -1); |
| 298 | + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDS, nullptr); |
292 | 299 | } |
293 | 300 | content_ = std::monostate{}; |
294 | 301 | } |
@@ -488,6 +495,12 @@ void Session::SetBody(Body&& body) { |
488 | 495 | content_ = std::move(body); |
489 | 496 | } |
490 | 497 |
|
| 498 | +// cppcheck-suppress passedByValue |
| 499 | +void Session::SetBodyView(BodyView body) { |
| 500 | + static_assert(std::is_trivially_copyable_v<BodyView>, "BodyView expected to be trivially copyable otherwise will need some std::move across codebase"); |
| 501 | + content_ = body; |
| 502 | +} |
| 503 | + |
491 | 504 | void Session::SetLowSpeed(const LowSpeed& low_speed) { |
492 | 505 | curl_easy_setopt(curl_->handle, CURLOPT_LOW_SPEED_LIMIT, static_cast<long>(low_speed.limit)); |
493 | 506 | curl_easy_setopt(curl_->handle, CURLOPT_LOW_SPEED_TIME, static_cast<long>(low_speed.time.count())); // cppcheck-suppress y2038-unsafe-call |
@@ -971,6 +984,11 @@ void Session::prepareBodyPayloadOrMultipart() const { |
971 | 984 | const std::string& body = std::get<cpr::Body>(content_).str(); |
972 | 985 | curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(body.length())); |
973 | 986 | curl_easy_setopt(curl_->handle, CURLOPT_COPYPOSTFIELDS, body.c_str()); |
| 987 | + } else if (std::holds_alternative<cpr::BodyView>(content_)) { |
| 988 | + const std::string_view body = std::get<cpr::BodyView>(content_).str(); |
| 989 | + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(body.length())); |
| 990 | + // NOLINTNEXTLINE (bugprone-suspicious-stringview-data-usage) |
| 991 | + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDS, body.data()); |
974 | 992 | } else if (std::holds_alternative<cpr::Multipart>(content_)) { |
975 | 993 | // Make sure, we have a empty multipart to start with: |
976 | 994 | if (curl_->multipart) { |
@@ -1020,7 +1038,7 @@ void Session::prepareBodyPayloadOrMultipart() const { |
1020 | 1038 | } |
1021 | 1039 |
|
1022 | 1040 | [[nodiscard]] bool Session::hasBodyOrPayload() const { |
1023 | | - return std::holds_alternative<cpr::Body>(content_) || std::holds_alternative<cpr::Payload>(content_); |
| 1041 | + return std::holds_alternative<cpr::Body>(content_) || std::holds_alternative<cpr::BodyView>(content_) || std::holds_alternative<cpr::Payload>(content_); |
1024 | 1042 | } |
1025 | 1043 |
|
1026 | 1044 | // clang-format off |
@@ -1057,6 +1075,8 @@ void Session::SetOption(const Redirect& redirect) { SetRedirect(redirect); } |
1057 | 1075 | void Session::SetOption(const Cookies& cookies) { SetCookies(cookies); } |
1058 | 1076 | void Session::SetOption(const Body& body) { SetBody(body); } |
1059 | 1077 | void Session::SetOption(Body&& body) { SetBody(std::move(body)); } |
| 1078 | +// cppcheck-suppress passedByValue |
| 1079 | +void Session::SetOption(BodyView body) { SetBodyView(body); } |
1060 | 1080 | void Session::SetOption(const LowSpeed& low_speed) { SetLowSpeed(low_speed); } |
1061 | 1081 | void Session::SetOption(const VerifySsl& verify) { SetVerifySsl(verify); } |
1062 | 1082 | void Session::SetOption(const Verbose& verbose) { SetVerbose(verbose); } |
|
0 commit comments