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 <vector> | |
Mark Mentovai
2014/10/23 19:31:03
You don’t need this, it’s already been #included b
Robert Sesek
2014/10/24 16:53:32
Done.
| |
22 | |
23 #include "base/logging.h" | |
24 #include "base/stl_util.h" | |
25 #include "base/strings/stringprintf.h" | |
26 #include "util/file/fd_io.h" | |
27 | |
28 namespace crashpad { | |
29 | |
30 StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string) | |
31 : string_(string), bytes_read_() { | |
Mark Mentovai
2014/10/23 19:31:03
I’ve been explicitly initializing the base classes
Robert Sesek
2014/10/24 16:53:32
Done.
| |
32 } | |
33 | |
34 StringHTTPBodyStream::~StringHTTPBodyStream() { | |
35 } | |
36 | |
37 ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { | |
38 size_t num_bytes_remaining = string_.length() - bytes_read_; | |
39 if (num_bytes_remaining == 0) { | |
40 return num_bytes_remaining; | |
41 } | |
42 | |
43 size_t num_bytes_returned = std::min(num_bytes_remaining, max_len); | |
Mark Mentovai
2014/10/23 19:31:03
#include <algorithm>
Mark Mentovai
2014/10/23 19:31:03
You should also make sure that this doesn’t overfl
Robert Sesek
2014/10/24 16:53:32
Done.
Robert Sesek
2014/10/24 16:53:32
Done. I tried to write a test for this, but you ca
| |
44 memcpy(buffer, &string_[bytes_read_], num_bytes_returned); | |
45 bytes_read_ += num_bytes_returned; | |
46 return num_bytes_returned; | |
47 } | |
48 | |
49 bool StringHTTPBodyStream::HasBytesRemaining() { | |
50 return bytes_read_ < string_.length(); | |
51 } | |
52 | |
53 FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) | |
54 : path_(path), fd_(0), at_eof_(false) { | |
55 } | |
56 | |
57 FileHTTPBodyStream::~FileHTTPBodyStream() { | |
58 if (fd_ > 0) { | |
Mark Mentovai
2014/10/23 19:31:03
You could have used a ScopedFD from base/files/sco
Robert Sesek
2014/10/24 16:53:32
ScopedFD does not check if fd<0 before trying to c
Mark Mentovai
2014/10/24 16:59:43
Robert Sesek wrote:
Robert Sesek
2014/10/24 17:20:37
Per offline discussion, fd_ now stores multiple ne
| |
59 close(fd_); | |
60 } | |
61 } | |
62 | |
63 ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { | |
64 if (fd_ == 0) { | |
Mark Mentovai
2014/10/23 19:31:02
If someone calls this again after GetBytesBuffer()
Robert Sesek
2014/10/24 16:53:32
Done. Added a test.
| |
65 fd_ = open(path_.value().c_str(), O_RDONLY); | |
Mark Mentovai
2014/10/23 19:31:03
open() needs to be wrapped in HANDLE_EINTR() (base
Robert Sesek
2014/10/24 16:53:32
Done.
| |
66 if (fd_ < 0) { | |
67 PLOG(ERROR) << "Cannot open " << path_.value(); | |
68 return -1; | |
69 } | |
70 } else if (fd_ < 0) { | |
71 return -2; | |
72 } | |
73 | |
74 ssize_t rv = ReadFD(fd_, buffer, max_len); | |
75 if (rv == 0) { | |
76 close(fd_); | |
77 fd_ = 0; | |
78 at_eof_ = true; | |
79 } | |
Mark Mentovai
2014/10/23 19:31:03
You haven’t handled the ReadFD() error case.
Robert Sesek
2014/10/24 16:53:32
The semantics are the same as GetBytesBuffer (% er
Mark Mentovai
2014/10/24 16:59:43
Robert Sesek wrote:
Robert Sesek
2014/10/24 17:20:37
Done.
| |
80 return rv; | |
81 } | |
82 | |
83 bool FileHTTPBodyStream::HasBytesRemaining() { | |
84 return fd_ >= 0 && !at_eof_; | |
85 } | |
86 | |
87 CompositeHTTPBodyStream::CompositeHTTPBodyStream( | |
88 const CompositeHTTPBodyStream::PartsList& parts) | |
89 : parts_(parts), current_part_(parts_.begin()) { | |
90 } | |
91 | |
92 CompositeHTTPBodyStream::~CompositeHTTPBodyStream() { | |
93 STLDeleteContainerPointers(parts_.begin(), parts_.end()); | |
94 } | |
95 | |
96 ssize_t CompositeHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, | |
Mark Mentovai
2014/10/23 19:31:03
This is good. The only thing I could think of that
Robert Sesek
2014/10/24 16:53:32
Right, this is something I considered, and I decid
| |
97 size_t max_len) { | |
98 // The call to HasBytesRemaining() will advance to the next part if the | |
99 // current part no longer has bytes remaining. | |
100 if (!HasBytesRemaining()) | |
101 return 0; | |
102 | |
103 ssize_t rv = (*current_part_)->GetBytesBuffer(buffer, max_len); | |
104 | |
105 if (rv == 0) { | |
106 // If the current part has returned 0 indicating EOF, call recursively to | |
107 // advance to the next part and try that. | |
108 return GetBytesBuffer(buffer, max_len); | |
109 } | |
110 | |
111 return rv; | |
112 } | |
113 | |
114 bool CompositeHTTPBodyStream::HasBytesRemaining() { | |
115 while (current_part_ != parts_.end()) { | |
116 if ((*current_part_)->HasBytesRemaining()) | |
117 return true; | |
118 ++current_part_; | |
119 } | |
120 | |
121 return false; | |
122 } | |
123 | |
124 } // namespace crashpad | |
OLD | NEW |