-
Notifications
You must be signed in to change notification settings - Fork 385
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
fix(rest): support rewinds in libcurl #11709
Merged
coryan
merged 2 commits into
googleapis:main
from
coryan:fix-rest-support-rewinds-in-libcurl-take2
May 23, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/internal/curl_writev.h" | ||
#include <cstdio> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace rest_internal { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
||
bool WriteVector::Seek(std::size_t offset, int origin) { | ||
// libcurl claims to only req | ||
if (origin != SEEK_SET) return false; | ||
writev_.assign(original_.begin(), original_.end()); | ||
while (!writev_.empty()) { | ||
auto& src = writev_.front(); | ||
if (src.size() >= offset) { | ||
src.remove_prefix(offset); | ||
offset = 0; | ||
break; | ||
} | ||
offset -= src.size(); | ||
writev_.pop_front(); | ||
} | ||
return offset == 0; | ||
} | ||
|
||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace rest_internal | ||
} // namespace cloud | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_WRITEV_H | ||
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_WRITEV_H | ||
|
||
#include "google/cloud/version.h" | ||
#include "absl/types/span.h" | ||
#include <deque> | ||
#include <vector> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace rest_internal { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
||
// Vector of data chunks to satisfy requests from libcurl. | ||
class WriteVector { | ||
public: | ||
explicit WriteVector(std::vector<absl::Span<char const>> v) | ||
: original_(std::move(v)), writev_(original_.begin(), original_.end()) {} | ||
|
||
std::size_t size() const { | ||
std::size_t size = 0; | ||
for (auto const& s : writev_) { | ||
size += s.size(); | ||
} | ||
return size; | ||
} | ||
|
||
std::size_t MoveTo(absl::Span<char> dst) { | ||
auto const avail = dst.size(); | ||
while (!writev_.empty()) { | ||
auto& src = writev_.front(); | ||
if (src.size() > dst.size()) { | ||
std::copy(src.begin(), src.begin() + dst.size(), dst.begin()); | ||
src.remove_prefix(dst.size()); | ||
dst.remove_prefix(dst.size()); | ||
break; | ||
} | ||
std::copy(src.begin(), src.end(), dst.begin()); | ||
dst.remove_prefix(src.size()); | ||
writev_.pop_front(); | ||
} | ||
return avail - dst.size(); | ||
} | ||
|
||
/// Implements a CURLOPT_SEEKFUNCTION callback. | ||
/// | ||
/// @see https://curl.se/libcurl/c/CURLOPT_SEEKFUNCTION.html | ||
/// @returns true if the seek operation was successful. | ||
bool Seek(std::size_t offset, int origin); | ||
|
||
private: | ||
std::vector<absl::Span<char const>> original_; | ||
std::deque<absl::Span<char const>> writev_; | ||
}; | ||
|
||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace rest_internal | ||
} // namespace cloud | ||
} // namespace google | ||
|
||
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_WRITEV_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "google/cloud/internal/curl_writev.h" | ||
#include <gmock/gmock.h> | ||
#include <cstdio> | ||
#include <vector> | ||
|
||
namespace google { | ||
namespace cloud { | ||
namespace rest_internal { | ||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN | ||
|
||
TEST(WriteVector, Simple) { | ||
auto const a = std::string(32, 'a'); | ||
auto const b = std::string(32, 'b'); | ||
auto tested = | ||
WriteVector({absl::Span<char const>(a), absl::Span<char const>(b)}); | ||
|
||
auto buffer = std::vector<char>(a.size() + b.size(), 'c'); | ||
auto offset = std::size_t{0}; | ||
ASSERT_EQ(4, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 4))); | ||
offset += 4; | ||
ASSERT_EQ(32, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 32))); | ||
offset += 32; | ||
ASSERT_EQ(28, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 64))); | ||
offset += 28; | ||
EXPECT_EQ(std::string(buffer.begin(), buffer.end()), a + b); | ||
} | ||
|
||
TEST(WriteVector, Rewind) { | ||
auto const a = std::string(32, 'a'); | ||
auto const b = std::string(32, 'b'); | ||
auto tested = | ||
WriteVector({absl::Span<char const>(a), absl::Span<char const>(b)}); | ||
|
||
auto buffer = std::vector<char>(a.size() + b.size(), 'c'); | ||
auto offset = std::size_t{0}; | ||
ASSERT_EQ(4, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 4))); | ||
offset += 4; | ||
ASSERT_EQ(32, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 32))); | ||
offset += 32; | ||
tested.Seek(16, SEEK_SET); | ||
offset = 16; | ||
ASSERT_EQ(32, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 32))); | ||
offset += 32; | ||
ASSERT_EQ(16, tested.MoveTo(absl::MakeSpan(buffer.data() + offset, 32))); | ||
EXPECT_EQ(std::string(buffer.begin(), buffer.end()), a + b); | ||
} | ||
|
||
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END | ||
} // namespace rest_internal | ||
} // namespace cloud | ||
} // namespace google |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incomplete comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed