OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/upload_element_reader.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "net/base/file_stream.h" |
| 9 #include "net/base/net_errors.h" |
| 10 |
| 11 namespace net { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // In tests, this value is used to override the return value of |
| 16 // UploadFileElementReader::GetContentLength() when set to non-zero. |
| 17 uint64 overriding_content_length = 0; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 bool UploadElementReader::IsInMemory() const { |
| 22 return false; |
| 23 } |
| 24 |
| 25 UploadBytesElementReader::UploadBytesElementReader(const char* bytes, |
| 26 int bytes_length) |
| 27 : bytes_(bytes), |
| 28 bytes_length_(bytes_length), |
| 29 offset_(0) { |
| 30 } |
| 31 |
| 32 UploadBytesElementReader::~UploadBytesElementReader() { |
| 33 } |
| 34 |
| 35 int UploadBytesElementReader::InitSync() { |
| 36 return OK; |
| 37 } |
| 38 |
| 39 uint64 UploadBytesElementReader::GetContentLength() const { |
| 40 return bytes_length_; |
| 41 } |
| 42 |
| 43 uint64 UploadBytesElementReader::BytesRemaining() const { |
| 44 return bytes_length_ - offset_; |
| 45 } |
| 46 |
| 47 int UploadBytesElementReader::ReadSync(char* buf, int buf_length) { |
| 48 DCHECK_LT(0, buf_length); |
| 49 |
| 50 const size_t num_bytes_to_read = |
| 51 std::min(BytesRemaining(), static_cast<uint64>(buf_length)); |
| 52 |
| 53 // Check if we have anything to copy first, because we are getting |
| 54 // the address of an element in |bytes_| and that will throw an |
| 55 // exception if |bytes_| is an empty vector. |
| 56 if (num_bytes_to_read > 0) |
| 57 memcpy(buf, bytes_ + offset_, num_bytes_to_read); |
| 58 |
| 59 offset_ += num_bytes_to_read; |
| 60 return num_bytes_to_read; |
| 61 } |
| 62 |
| 63 bool UploadBytesElementReader::IsInMemory() const { |
| 64 return true; |
| 65 } |
| 66 |
| 67 UploadFileElementReader::UploadFileElementReader( |
| 68 const FilePath& path, |
| 69 uint64 range_offset, |
| 70 uint64 range_length, |
| 71 const base::Time& expected_modification_time) |
| 72 : path_(path), |
| 73 range_offset_(range_offset), |
| 74 range_length_(range_length), |
| 75 expected_modification_time_(expected_modification_time), |
| 76 content_length_(0), |
| 77 bytes_remaining_(0) { |
| 78 } |
| 79 |
| 80 UploadFileElementReader::~UploadFileElementReader() { |
| 81 if (file_stream_.get()) |
| 82 file_stream_->CloseSync(); |
| 83 } |
| 84 |
| 85 int UploadFileElementReader::InitSync() { |
| 86 scoped_ptr<FileStream> file_stream(new FileStream(NULL)); |
| 87 int64 rv = file_stream->OpenSync( |
| 88 path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| 89 if (rv != OK) { |
| 90 // If the file can't be opened, we'll just upload an empty file. |
| 91 DLOG(WARNING) << "Failed to open \"" << path_.value() |
| 92 << "\" for reading: " << rv; |
| 93 file_stream.reset(); |
| 94 } |
| 95 if (file_stream.get() && range_offset_) { |
| 96 rv = file_stream->SeekSync(FROM_BEGIN, range_offset_); |
| 97 if (rv < 0) { |
| 98 DLOG(WARNING) << "Failed to seek \"" << path_.value() |
| 99 << "\" to offset: " << range_offset_ << " (" << rv |
| 100 << ")"; |
| 101 file_stream->CloseSync(); |
| 102 file_stream.reset(); |
| 103 } |
| 104 } |
| 105 file_stream_.reset(file_stream.release()); |
| 106 |
| 107 int64 length = 0; |
| 108 if (file_stream_.get() && |
| 109 file_util::GetFileSize(path_, &length) && |
| 110 range_offset_ < static_cast<uint64>(length)) { |
| 111 // Compensate for the offset. |
| 112 length = std::min(length - range_offset_, range_length_); |
| 113 } |
| 114 content_length_ = length; |
| 115 bytes_remaining_ = GetContentLength(); |
| 116 |
| 117 // If the underlying file has been changed and the expected file |
| 118 // modification time is set, treat it as error. Note that the expected |
| 119 // modification time from WebKit is based on time_t precision. So we |
| 120 // have to convert both to time_t to compare. This check is used for |
| 121 // sliced files. |
| 122 if (!expected_modification_time_.is_null()) { |
| 123 base::PlatformFileInfo info; |
| 124 if (file_util::GetFileInfo(path_, &info) && |
| 125 expected_modification_time_.ToTimeT() != |
| 126 info.last_modified.ToTimeT()) { |
| 127 return ERR_UPLOAD_FILE_CHANGED; |
| 128 } |
| 129 } |
| 130 |
| 131 return OK; |
| 132 } |
| 133 |
| 134 uint64 UploadFileElementReader::GetContentLength() const { |
| 135 if (overriding_content_length) |
| 136 return overriding_content_length; |
| 137 return content_length_; |
| 138 } |
| 139 |
| 140 uint64 UploadFileElementReader::BytesRemaining() const { |
| 141 return bytes_remaining_; |
| 142 } |
| 143 |
| 144 int UploadFileElementReader::ReadSync(char* buf, int buf_length) { |
| 145 DCHECK_LT(0, buf_length); |
| 146 |
| 147 const uint64 num_bytes_to_read = |
| 148 static_cast<int>(std::min(BytesRemaining(), |
| 149 static_cast<uint64>(buf_length))); |
| 150 if (num_bytes_to_read > 0) { |
| 151 int num_bytes_consumed = 0; |
| 152 // file_stream_ is NULL if the target file is |
| 153 // missing or not readable. |
| 154 if (file_stream_.get()) { |
| 155 num_bytes_consumed = |
| 156 file_stream_->ReadSync(buf, num_bytes_to_read); |
| 157 } |
| 158 if (num_bytes_consumed <= 0) { |
| 159 // If there's less data to read than we initially observed, then |
| 160 // pad with zero. Otherwise the server will hang waiting for the |
| 161 // rest of the data. |
| 162 memset(buf, 0, num_bytes_to_read); |
| 163 } |
| 164 } |
| 165 DCHECK_GE(bytes_remaining_, num_bytes_to_read); |
| 166 bytes_remaining_ -= num_bytes_to_read; |
| 167 return num_bytes_to_read; |
| 168 } |
| 169 |
| 170 UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 171 ScopedOverridingContentLengthForTests(uint64 value) { |
| 172 overriding_content_length = value; |
| 173 } |
| 174 |
| 175 UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 176 ~ScopedOverridingContentLengthForTests() { |
| 177 overriding_content_length = 0; |
| 178 } |
| 179 |
| 180 } // namespace net |
OLD | NEW |