| 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.h" | 5 #include "net/base/upload_data.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/file_stream.h" | 10 #include "net/base/file_stream.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 content_length_computed_(false), | 21 content_length_computed_(false), |
| 22 content_length_(-1), | 22 content_length_(-1), |
| 23 file_stream_(NULL) { | 23 file_stream_(NULL) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 UploadData::Element::~Element() { | 26 UploadData::Element::~Element() { |
| 27 // In the common case |file__stream_| will be null. | 27 // In the common case |file__stream_| will be null. |
| 28 delete file_stream_; | 28 delete file_stream_; |
| 29 } | 29 } |
| 30 | 30 |
| 31 void UploadData::Element::SetToChunk(const char* bytes, int bytes_len) { | 31 void UploadData::Element::SetToChunk(const char* bytes, |
| 32 std::string chunk_length = StringPrintf("%X\r\n", bytes_len); | 32 int bytes_len, |
| 33 bool is_last_chunk) { |
| 33 bytes_.clear(); | 34 bytes_.clear(); |
| 34 bytes_.insert(bytes_.end(), chunk_length.data(), | |
| 35 chunk_length.data() + chunk_length.length()); | |
| 36 bytes_.insert(bytes_.end(), bytes, bytes + bytes_len); | 35 bytes_.insert(bytes_.end(), bytes, bytes + bytes_len); |
| 37 const char* crlf = "\r\n"; | |
| 38 bytes_.insert(bytes_.end(), crlf, crlf + 2); | |
| 39 type_ = TYPE_CHUNK; | 36 type_ = TYPE_CHUNK; |
| 40 is_last_chunk_ = (bytes_len == 0); | 37 is_last_chunk_ = is_last_chunk; |
| 41 } | 38 } |
| 42 | 39 |
| 43 uint64 UploadData::Element::GetContentLength() { | 40 uint64 UploadData::Element::GetContentLength() { |
| 44 if (override_content_length_ || content_length_computed_) | 41 if (override_content_length_ || content_length_computed_) |
| 45 return content_length_; | 42 return content_length_; |
| 46 | 43 |
| 47 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) | 44 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) |
| 48 return static_cast<uint64>(bytes_.size()); | 45 return static_cast<uint64>(bytes_.size()); |
| 49 else if (type_ == TYPE_BLOB) | 46 else if (type_ == TYPE_BLOB) |
| 50 // The blob reference will be resolved later. | 47 // The blob reference will be resolved later. |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 elements_.back().SetToFilePathRange(file_path, offset, length, | 136 elements_.back().SetToFilePathRange(file_path, offset, length, |
| 140 expected_modification_time); | 137 expected_modification_time); |
| 141 } | 138 } |
| 142 | 139 |
| 143 void UploadData::AppendBlob(const GURL& blob_url) { | 140 void UploadData::AppendBlob(const GURL& blob_url) { |
| 144 DCHECK(!is_chunked_); | 141 DCHECK(!is_chunked_); |
| 145 elements_.push_back(Element()); | 142 elements_.push_back(Element()); |
| 146 elements_.back().SetToBlobUrl(blob_url); | 143 elements_.back().SetToBlobUrl(blob_url); |
| 147 } | 144 } |
| 148 | 145 |
| 149 void UploadData::AppendChunk(const char* bytes, int bytes_len) { | 146 void UploadData::AppendChunk(const char* bytes, |
| 147 int bytes_len, |
| 148 bool is_last_chunk) { |
| 150 DCHECK(is_chunked_); | 149 DCHECK(is_chunked_); |
| 151 elements_.push_back(Element()); | 150 elements_.push_back(Element()); |
| 152 elements_.back().SetToChunk(bytes, bytes_len); | 151 elements_.back().SetToChunk(bytes, bytes_len, is_last_chunk); |
| 153 if (chunk_callback_) | 152 if (chunk_callback_) |
| 154 chunk_callback_->OnChunkAvailable(); | 153 chunk_callback_->OnChunkAvailable(); |
| 155 } | 154 } |
| 156 | 155 |
| 157 void UploadData::set_chunk_callback(ChunkCallback* callback) { | 156 void UploadData::set_chunk_callback(ChunkCallback* callback) { |
| 158 chunk_callback_ = callback; | 157 chunk_callback_ = callback; |
| 159 } | 158 } |
| 160 | 159 |
| 161 uint64 UploadData::GetContentLength() { | 160 uint64 UploadData::GetContentLength() { |
| 162 uint64 len = 0; | 161 uint64 len = 0; |
| 163 std::vector<Element>::iterator it = elements_.begin(); | 162 std::vector<Element>::iterator it = elements_.begin(); |
| 164 for (; it != elements_.end(); ++it) | 163 for (; it != elements_.end(); ++it) |
| 165 len += (*it).GetContentLength(); | 164 len += (*it).GetContentLength(); |
| 166 return len; | 165 return len; |
| 167 } | 166 } |
| 168 | 167 |
| 169 void UploadData::SetElements(const std::vector<Element>& elements) { | 168 void UploadData::SetElements(const std::vector<Element>& elements) { |
| 170 elements_ = elements; | 169 elements_ = elements; |
| 171 } | 170 } |
| 172 | 171 |
| 173 UploadData::~UploadData() { | 172 UploadData::~UploadData() { |
| 174 } | 173 } |
| 175 | 174 |
| 176 } // namespace net | 175 } // namespace net |
| OLD | NEW |