Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: util/net/http_body.cc

Issue 791493007: Make http_body cross-platform (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@file-writer-redux
Patch Set: fixes Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/net/http_body.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "util/net/http_body.h" 15 #include "util/net/http_body.h"
16 16
17 #include <fcntl.h>
18 #include <string.h> 17 #include <string.h>
19 #include <unistd.h>
20 18
21 #include <algorithm> 19 #include <algorithm>
22 #include <limits> 20 #include <limits>
23 21
24 #include "base/logging.h" 22 #include "base/logging.h"
25 #include "base/posix/eintr_wrapper.h"
26 #include "base/stl_util.h" 23 #include "base/stl_util.h"
27 #include "util/file/file_io.h"
28 24
29 namespace crashpad { 25 namespace crashpad {
30 26
31 StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string) 27 StringHTTPBodyStream::StringHTTPBodyStream(const std::string& string)
32 : HTTPBodyStream(), string_(string), bytes_read_() { 28 : HTTPBodyStream(), string_(string), bytes_read_() {
33 } 29 }
34 30
35 StringHTTPBodyStream::~StringHTTPBodyStream() { 31 StringHTTPBodyStream::~StringHTTPBodyStream() {
36 } 32 }
37 33
38 ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { 34 ssize_t StringHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
39 size_t num_bytes_remaining = string_.length() - bytes_read_; 35 size_t num_bytes_remaining = string_.length() - bytes_read_;
40 if (num_bytes_remaining == 0) { 36 if (num_bytes_remaining == 0) {
41 return num_bytes_remaining; 37 return num_bytes_remaining;
42 } 38 }
43 39
44 size_t num_bytes_returned = 40 size_t num_bytes_returned =
45 std::min(std::min(num_bytes_remaining, max_len), 41 std::min(std::min(num_bytes_remaining, max_len),
46 implicit_cast<size_t>(std::numeric_limits<ssize_t>::max())); 42 implicit_cast<size_t>(std::numeric_limits<ssize_t>::max()));
47 memcpy(buffer, &string_[bytes_read_], num_bytes_returned); 43 memcpy(buffer, &string_[bytes_read_], num_bytes_returned);
48 bytes_read_ += num_bytes_returned; 44 bytes_read_ += num_bytes_returned;
49 return num_bytes_returned; 45 return num_bytes_returned;
50 } 46 }
51 47
52 FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path) 48 FileHTTPBodyStream::FileHTTPBodyStream(const base::FilePath& path)
53 : HTTPBodyStream(), path_(path), fd_(kUnopenedFile) { 49 : HTTPBodyStream(), path_(path), file_(), file_state_(kUnopenedFile) {
54 } 50 }
55 51
56 FileHTTPBodyStream::~FileHTTPBodyStream() { 52 FileHTTPBodyStream::~FileHTTPBodyStream() {
57 if (fd_ >= 0) {
58 LoggingCloseFile(fd_);
59 }
60 } 53 }
61 54
62 ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) { 55 ssize_t FileHTTPBodyStream::GetBytesBuffer(uint8_t* buffer, size_t max_len) {
63 switch (fd_) { 56 switch (file_state_) {
64 case kUnopenedFile: 57 case kUnopenedFile:
65 fd_ = HANDLE_EINTR(open(path_.value().c_str(), O_RDONLY)); 58 file_.reset(LoggingOpenFileForRead(path_));
66 if (fd_ < 0) { 59 if (!file_.is_valid()) {
67 fd_ = kFileOpenError; 60 file_state_ = kFileOpenError;
68 PLOG(ERROR) << "Cannot open " << path_.value();
69 return -1; 61 return -1;
70 } 62 }
63 file_state_ = kReading;
71 break; 64 break;
72 case kFileOpenError: 65 case kFileOpenError:
73 return -1; 66 return -1;
74 case kClosedAtEOF: 67 case kClosedAtEOF:
75 return 0; 68 return 0;
76 default: 69 case kReading:
77 break; 70 break;
78 } 71 }
79 72
80 ssize_t rv = ReadFile(fd_, buffer, max_len); 73 ssize_t rv = ReadFile(file_.get(), buffer, max_len);
81 if (rv == 0) { 74 if (rv == 0) {
82 LoggingCloseFile(fd_); 75 file_.reset();
83 fd_ = kClosedAtEOF; 76 file_state_ = kClosedAtEOF;
84 } else if (rv < 0) { 77 } else if (rv < 0) {
85 PLOG(ERROR) << "read"; 78 PLOG(ERROR) << "read";
86 } 79 }
87 return rv; 80 return rv;
88 } 81 }
89 82
90 CompositeHTTPBodyStream::CompositeHTTPBodyStream( 83 CompositeHTTPBodyStream::CompositeHTTPBodyStream(
91 const CompositeHTTPBodyStream::PartsList& parts) 84 const CompositeHTTPBodyStream::PartsList& parts)
92 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) { 85 : HTTPBodyStream(), parts_(parts), current_part_(parts_.begin()) {
93 } 86 }
(...skipping 18 matching lines...) Expand all
112 } else if (this_read < 0) { 105 } else if (this_read < 0) {
113 return this_read; 106 return this_read;
114 } 107 }
115 bytes_copied += this_read; 108 bytes_copied += this_read;
116 } 109 }
117 110
118 return bytes_copied; 111 return bytes_copied;
119 } 112 }
120 113
121 } // namespace crashpad 114 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/net/http_body.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698