Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: more efficient access to HttpRequest data #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/site/http_form_data/http_form_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FormDataDelimiter {

} // namespace

gcf::HttpResponse http_form_data(gcf::HttpRequest request) { // NOLINT
gcf::HttpResponse http_form_data(gcf::HttpRequest request) {
if (request.verb() != "POST") {
return gcf::HttpResponse{}.set_result(gcf::HttpResponse::kMethodNotAllowed);
}
Expand All @@ -72,8 +72,10 @@ gcf::HttpResponse http_form_data(gcf::HttpRequest request) { // NOLINT
return gcf::HttpResponse{}.set_result(gcf::HttpResponse::kBadRequest);
}
auto delimiter = FormDataDelimiter::FromHeader(header->second);

auto payload = std::move(request).payload();
std::vector<absl::string_view> parts =
absl::StrSplit(request.payload(), delimiter, absl::SkipEmpty{});
absl::StrSplit(payload, delimiter, absl::SkipEmpty{});
nlohmann::json result{{"parts", std::vector<nlohmann::json>{}}};
for (auto& p : parts) {
std::vector<absl::string_view> components =
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/functions/http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class HttpRequest {
[[nodiscard]] std::string const& target() const { return target_; }

/// The request payload
[[nodiscard]] std::string const& payload() const { return payload_; }
[[nodiscard]] std::string const& payload() const& { return payload_; }
[[nodiscard]] std::string&& payload() && { return std::move(payload_); }

/// The request HTTP headers
[[nodiscard]] HeadersType const& headers() const { return headers_; }
Expand Down