|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/storage/internal/curl_streambuf.h" |
| 16 | + |
| 17 | +namespace google { |
| 18 | +namespace cloud { |
| 19 | +namespace storage { |
| 20 | +inline namespace STORAGE_CLIENT_NS { |
| 21 | +namespace internal { |
| 22 | + |
| 23 | +CurlStreambuf::CurlStreambuf(CurlUploadRequest&& upload, |
| 24 | + std::size_t max_buffer_size) |
| 25 | + : upload_(std::move(upload)), max_buffer_size_(max_buffer_size) { |
| 26 | + current_ios_buffer_.reserve(max_buffer_size); |
| 27 | +} |
| 28 | + |
| 29 | +bool CurlStreambuf::IsOpen() const { return upload_.IsOpen(); } |
| 30 | + |
| 31 | +CurlStreambuf::int_type CurlStreambuf::overflow(int_type ch) { |
| 32 | + Validate(__func__); |
| 33 | + SwapBuffers(); |
| 34 | + if (not traits_type::eq_int_type(ch, traits_type::eof())) { |
| 35 | + current_ios_buffer_.push_back(traits_type::to_char_type(ch)); |
| 36 | + pbump(1); |
| 37 | + } |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +int CurlStreambuf::sync() { |
| 42 | + // The default destructor calls sync(), for already closed streams this should |
| 43 | + // be a no-op. |
| 44 | + if (not IsOpen()) { |
| 45 | + return 0; |
| 46 | + } |
| 47 | + SwapBuffers(); |
| 48 | + upload_.Flush(); |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +std::streamsize CurlStreambuf::xsputn(char const* s, std::streamsize count) { |
| 53 | + Validate(__func__); |
| 54 | + current_ios_buffer_.append(s, static_cast<std::size_t>(count)); |
| 55 | + pbump(static_cast<int>(count)); |
| 56 | + if (current_ios_buffer_.size() > max_buffer_size_) { |
| 57 | + SwapBuffers(); |
| 58 | + } |
| 59 | + return count; |
| 60 | +} |
| 61 | + |
| 62 | +HttpResponse CurlStreambuf::DoClose() { |
| 63 | + GCP_LOG(INFO) << __func__ << "()"; |
| 64 | + Validate(__func__); |
| 65 | + SwapBuffers(); |
| 66 | + return upload_.Close(); |
| 67 | +} |
| 68 | + |
| 69 | +void CurlStreambuf::Validate(char const* where) const { |
| 70 | + if (upload_.IsOpen()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + std::string msg = "Attempting to use closed CurlStream in "; |
| 74 | + msg += where; |
| 75 | + google::cloud::internal::RaiseRuntimeError(msg); |
| 76 | +} |
| 77 | + |
| 78 | +void CurlStreambuf::SwapBuffers() { |
| 79 | + // Shorten the buffer to the actual used size. |
| 80 | + current_ios_buffer_.resize(pptr() - pbase()); |
| 81 | + // Push the buffer to the libcurl wrapper to be written as needed |
| 82 | + upload_.NextBuffer(current_ios_buffer_); |
| 83 | + // Make the buffer big enough to receive more data before needing a flush. |
| 84 | + current_ios_buffer_.clear(); |
| 85 | + current_ios_buffer_.reserve(max_buffer_size_); |
| 86 | + setp(¤t_ios_buffer_[0], ¤t_ios_buffer_[0] + max_buffer_size_); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace internal |
| 90 | +} // namespace STORAGE_CLIENT_NS |
| 91 | +} // namespace storage |
| 92 | +} // namespace cloud |
| 93 | +} // namespace google |
0 commit comments