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_ > kUnopenedFile) { | |
Mark Mentovai
2014/10/24 17:46:58
This is sensitive to the ordering of the enum. I’d
Robert Sesek
2014/10/24 18:45:31
Done.
| |
58 CloseFD(fd_); | |
59 } | |
60 } | |
61 | |
62 ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { | |
63 if (fd_ == kUnopenedFile) { | |
Mark Mentovai
2014/10/24 17:46:58
switch?
Robert Sesek
2014/10/24 18:45:31
Done.
| |
64 fd_ = HANDLE_EINTR(open(path_.value().c_str(), O_RDONLY)); | |
65 if (fd_ < 0) { | |
66 fd_ = kFileOpenError; | |
67 PLOG(ERROR) << "Cannot open " << path_.value(); | |
68 return -1; | |
69 } | |
70 } else if (fd_ == kFileOpenError) { | |
71 return -2; | |
72 } else if (fd_ == kClosedAtEOF) { | |
73 return 0; | |
74 } | |
75 | |
76 ssize_t rv = ReadFD(fd_, buffer, max_len); | |
77 if (rv == 0) { | |
78 CloseFD(fd_); | |
79 fd_ = kClosedAtEOF; | |
80 } else if (rv < 0) { | |
81 PLOG(ERROR) << "read"; | |
82 } | |
83 return rv; | |
84 } | |
85 | |
86 CompositeHTTPBodyStream::CompositeHTTPBodyStream( | |
87 const CompositeHTTPBodyStream::PartsList& parts) | |
88 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) { | |
89 } | |
90 | |
91 CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { | |
92 STLDeleteContainerPointers(parts_.begin(), parts_.end()); | |
93 } | |
94 | |
95 ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, | |
96 size_t max_len) { | |
97 if (current_part_ == parts_.end()) | |
98 return 0; | |
99 | |
100 ssize_t rv = (*current_part_)->GetBytesBuffer(buffer, max_len); | |
101 | |
102 if (rv == 0) { | |
103 // If the current part has returned 0 indicating EOF, advance the current | |
104 // part and call recursively to try again. | |
105 ++current_part_; | |
106 return GetBytesBuffer(buffer, max_len); | |
107 } | |
108 | |
109 return rv; | |
110 } | |
111 | |
112 } // namespace crashpad | |
OLD | NEW |