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 } | |
Mark Mentovai
2014/10/24 18:58:21
default:
break;
It’s required by the style guid
Robert Sesek
2014/10/24 19:02:47
Done.
| |
77 | |
78 ssize_t rv = ReadFD(fd_, buffer, max_len); | |
79 if (rv == 0) { | |
80 LoggingCloseFD(fd_); | |
81 fd_ = kClosedAtEOF; | |
82 } else if (rv < 0) { | |
83 PLOG(ERROR) << "read"; | |
84 } | |
85 return rv; | |
86 } | |
87 | |
88 CompositeHTTPBodyStream::CompositeHTTPBodyStream( | |
89 const CompositeHTTPBodyStream::PartsList& parts) | |
90 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) { | |
91 } | |
92 | |
93 CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { | |
94 STLDeleteContainerPointers(parts_.begin(), parts_.end()); | |
95 } | |
96 | |
97 ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, | |
98 size_t max_len) { | |
99 if (current_part_ == parts_.end()) | |
100 return 0; | |
101 | |
102 ssize_t rv = (*current_part_)->GetBytesBuffer(buffer, max_len); | |
103 | |
104 if (rv == 0) { | |
105 // If the current part has returned 0 indicating EOF, advance the current | |
106 // part and call recursively to try again. | |
107 ++current_part_; | |
108 return GetBytesBuffer(buffer, max_len); | |
109 } | |
110 | |
111 return rv; | |
112 } | |
113 | |
114 } // namespace crashpad | |
OLD | NEW |