| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/upload_data_stream.h" | 5 #include "net/base/upload_data_stream.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 9 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 10 | 11 |
| 11 namespace net { | 12 namespace net { |
| 12 | 13 |
| 14 UploadDataStream* UploadDataStream::Create(const UploadData* data, |
| 15 int* error_code) { |
| 16 scoped_ptr<UploadDataStream> stream(new UploadDataStream(data)); |
| 17 int rv = stream->FillBuf(); |
| 18 if (error_code) |
| 19 *error_code = rv; |
| 20 if (rv != OK) |
| 21 return NULL; |
| 22 |
| 23 return stream.release(); |
| 24 } |
| 25 |
| 13 UploadDataStream::UploadDataStream(const UploadData* data) | 26 UploadDataStream::UploadDataStream(const UploadData* data) |
| 14 : data_(data), | 27 : data_(data), |
| 15 buf_(new IOBuffer(kBufSize)), | 28 buf_(new IOBuffer(kBufSize)), |
| 16 buf_len_(0), | 29 buf_len_(0), |
| 17 next_element_(data->elements().begin()), | 30 next_element_(data->elements().begin()), |
| 18 next_element_offset_(0), | 31 next_element_offset_(0), |
| 19 next_element_remaining_(0), | 32 next_element_remaining_(0), |
| 20 total_size_(data->GetContentLength()), | 33 total_size_(data->GetContentLength()), |
| 21 current_position_(0), | 34 current_position_(0), |
| 22 eof_(false) { | 35 eof_(false) { |
| 23 FillBuf(); | |
| 24 } | 36 } |
| 25 | 37 |
| 26 UploadDataStream::~UploadDataStream() { | 38 UploadDataStream::~UploadDataStream() { |
| 27 } | 39 } |
| 28 | 40 |
| 29 void UploadDataStream::DidConsume(size_t num_bytes) { | 41 void UploadDataStream::DidConsume(size_t num_bytes) { |
| 30 DCHECK_LE(num_bytes, buf_len_); | 42 DCHECK_LE(num_bytes, buf_len_); |
| 31 DCHECK(!eof_); | 43 DCHECK(!eof_); |
| 32 | 44 |
| 33 buf_len_ -= num_bytes; | 45 buf_len_ -= num_bytes; |
| 34 if (buf_len_) | 46 if (buf_len_) |
| 35 memmove(buf_->data(), buf_->data() + num_bytes, buf_len_); | 47 memmove(buf_->data(), buf_->data() + num_bytes, buf_len_); |
| 36 | 48 |
| 37 FillBuf(); | 49 FillBuf(); |
| 38 | 50 |
| 39 current_position_ += num_bytes; | 51 current_position_ += num_bytes; |
| 40 } | 52 } |
| 41 | 53 |
| 42 void UploadDataStream::FillBuf() { | 54 int UploadDataStream::FillBuf() { |
| 43 std::vector<UploadData::Element>::const_iterator end = | 55 std::vector<UploadData::Element>::const_iterator end = |
| 44 data_->elements().end(); | 56 data_->elements().end(); |
| 45 | 57 |
| 46 while (buf_len_ < kBufSize && next_element_ != end) { | 58 while (buf_len_ < kBufSize && next_element_ != end) { |
| 47 bool advance_to_next_element = false; | 59 bool advance_to_next_element = false; |
| 48 | 60 |
| 49 const UploadData::Element& element = *next_element_; | 61 const UploadData::Element& element = *next_element_; |
| 50 | 62 |
| 51 size_t size_remaining = kBufSize - buf_len_; | 63 size_t size_remaining = kBufSize - buf_len_; |
| 52 if (element.type() == UploadData::TYPE_BYTES) { | 64 if (element.type() == UploadData::TYPE_BYTES) { |
| 53 const std::vector<char>& d = element.bytes(); | 65 const std::vector<char>& d = element.bytes(); |
| 54 size_t count = d.size() - next_element_offset_; | 66 size_t count = d.size() - next_element_offset_; |
| 55 | 67 |
| 56 size_t bytes_copied = std::min(count, size_remaining); | 68 size_t bytes_copied = std::min(count, size_remaining); |
| 57 | 69 |
| 58 memcpy(buf_->data() + buf_len_, &d[next_element_offset_], bytes_copied); | 70 memcpy(buf_->data() + buf_len_, &d[next_element_offset_], bytes_copied); |
| 59 buf_len_ += bytes_copied; | 71 buf_len_ += bytes_copied; |
| 60 | 72 |
| 61 if (bytes_copied == count) { | 73 if (bytes_copied == count) { |
| 62 advance_to_next_element = true; | 74 advance_to_next_element = true; |
| 63 } else { | 75 } else { |
| 64 next_element_offset_ += bytes_copied; | 76 next_element_offset_ += bytes_copied; |
| 65 } | 77 } |
| 66 } else { | 78 } else { |
| 67 DCHECK(element.type() == UploadData::TYPE_FILE); | 79 DCHECK(element.type() == UploadData::TYPE_FILE); |
| 68 | 80 |
| 81 // If the underlying file has been changed, treat it as error. |
| 82 // Note that the expected modification time from WebKit is based on |
| 83 // time_t precision. So we have to convert both to time_t to compare. |
| 84 if (!element.expected_file_modification_time().is_null()) { |
| 85 file_util::FileInfo info; |
| 86 if (file_util::GetFileInfo(element.file_path(), &info) && |
| 87 element.expected_file_modification_time().ToTimeT() != |
| 88 info.last_modified.ToTimeT()) { |
| 89 return ERR_UPLOAD_FILE_CHANGED; |
| 90 } |
| 91 } |
| 92 |
| 69 if (!next_element_stream_.IsOpen()) { | 93 if (!next_element_stream_.IsOpen()) { |
| 70 int flags = base::PLATFORM_FILE_OPEN | | 94 int flags = base::PLATFORM_FILE_OPEN | |
| 71 base::PLATFORM_FILE_READ; | 95 base::PLATFORM_FILE_READ; |
| 72 int rv = next_element_stream_.Open(element.file_path(), flags); | 96 int rv = next_element_stream_.Open(element.file_path(), flags); |
| 73 // If the file does not exist, that's technically okay.. we'll just | 97 // If the file does not exist, that's technically okay.. we'll just |
| 74 // upload an empty file. This is for consistency with Mozilla. | 98 // upload an empty file. This is for consistency with Mozilla. |
| 75 DLOG_IF(WARNING, rv != OK) << "Failed to open \"" | 99 DLOG_IF(WARNING, rv != OK) << "Failed to open \"" |
| 76 << element.file_path().value() | 100 << element.file_path().value() |
| 77 << "\" for reading: " << rv; | 101 << "\" for reading: " << rv; |
| 78 | 102 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 103 | 127 |
| 104 if (advance_to_next_element) { | 128 if (advance_to_next_element) { |
| 105 ++next_element_; | 129 ++next_element_; |
| 106 next_element_offset_ = 0; | 130 next_element_offset_ = 0; |
| 107 next_element_stream_.Close(); | 131 next_element_stream_.Close(); |
| 108 } | 132 } |
| 109 } | 133 } |
| 110 | 134 |
| 111 if (next_element_ == end && !buf_len_) | 135 if (next_element_ == end && !buf_len_) |
| 112 eof_ = true; | 136 eof_ = true; |
| 137 |
| 138 return OK; |
| 113 } | 139 } |
| 114 | 140 |
| 115 } // namespace net | 141 } // namespace net |
| OLD | NEW |