OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 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 "util/net/http_body.h" |
| 16 |
| 17 #include <fcntl.h> |
| 18 #include <string.h> |
| 19 #include <unistd.h> |
| 20 |
| 21 #include <algorithm> |
| 22 #include <limits> |
| 23 |
| 24 #include "base/logging.h" |
| 25 #include "base/posix/eintr_wrapper.h" |
| 26 #include "base/stl_util.h" |
| 27 #include "util/file/fd_io.h" |
| 28 |
| 29 namespace crashpad { |
| 30 |
| 31 StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string) |
| 32 : HTTPBodyStream(), string_(string), bytes_read_() { |
| 33 } |
| 34 |
| 35 StringHTTPBodyStream::~StringHTTPBodyStream() { |
| 36 } |
| 37 |
| 38 ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { |
| 39 size_t num_bytes_remaining = string_.length() - bytes_read_; |
| 40 if (num_bytes_remaining == 0) { |
| 41 return num_bytes_remaining; |
| 42 } |
| 43 |
| 44 size_t num_bytes_returned = |
| 45 std::min(std::min(num_bytes_remaining, max_len), |
| 46 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); |
| 47 memcpy(buffer, &string_[bytes_read_], num_bytes_returned); |
| 48 bytes_read_ += num_bytes_returned; |
| 49 return num_bytes_returned; |
| 50 } |
| 51 |
| 52 FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) |
| 53 : HTTPBodyStream(), path_(path), fd_(kUnopenedFile) { |
| 54 } |
| 55 |
| 56 FileHTTPBodyStream::~FileHTTPBodyStream() { |
| 57 if (fd_ >= 0) { |
| 58 LoggingCloseFD(fd_); |
| 59 } |
| 60 } |
| 61 |
| 62 ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { |
| 63 switch (fd_) { |
| 64 case kUnopenedFile: |
| 65 fd_ = HANDLE_EINTR(open(path_.value().c_str(), O_RDONLY)); |
| 66 if (fd_ < 0) { |
| 67 fd_ = kFileOpenError; |
| 68 PLOG(ERROR) << "Cannot open " << path_.value(); |
| 69 return -1; |
| 70 } |
| 71 break; |
| 72 case kFileOpenError: |
| 73 return -1; |
| 74 case kClosedAtEOF: |
| 75 return 0; |
| 76 default: |
| 77 break; |
| 78 } |
| 79 |
| 80 ssize_t rv = ReadFD(fd_, buffer, max_len); |
| 81 if (rv == 0) { |
| 82 LoggingCloseFD(fd_); |
| 83 fd_ = kClosedAtEOF; |
| 84 } else if (rv < 0) { |
| 85 PLOG(ERROR) << "read"; |
| 86 } |
| 87 return rv; |
| 88 } |
| 89 |
| 90 CompositeHTTPBodyStream::CompositeHTTPBodyStream( |
| 91 const CompositeHTTPBodyStream::PartsList& parts) |
| 92 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) { |
| 93 } |
| 94 |
| 95 CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { |
| 96 STLDeleteContainerPointers(parts_.begin(), parts_.end()); |
| 97 } |
| 98 |
| 99 ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, |
| 100 size_t max_len) { |
| 101 if (current_part_ == parts_.end()) |
| 102 return 0; |
| 103 |
| 104 ssize_t rv = (*current_part_)->GetBytesBuffer(buffer, max_len); |
| 105 |
| 106 if (rv == 0) { |
| 107 // If the current part has returned 0 indicating EOF, advance the current |
| 108 // part and call recursively to try again. |
| 109 ++current_part_; |
| 110 return GetBytesBuffer(buffer, max_len); |
| 111 } |
| 112 |
| 113 return rv; |
| 114 } |
| 115 |
| 116 } // namespace crashpad |
OLD | NEW |