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, int bytes_len, |
willchan no longer on Chromium
2011/03/03 19:47:48
http://dev.chromium.org/developers/coding-style#Co
| |
32 std::string chunk_length = StringPrintf("%X\r\n", bytes_len); | 32 bool is_last_chunk) { |
33 bytes_.clear(); | 33 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); | 34 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; | 35 type_ = TYPE_CHUNK; |
40 is_last_chunk_ = (bytes_len == 0); | 36 is_last_chunk_ = is_last_chunk; |
41 } | 37 } |
42 | 38 |
43 uint64 UploadData::Element::GetContentLength() { | 39 uint64 UploadData::Element::GetContentLength() { |
44 if (override_content_length_ || content_length_computed_) | 40 if (override_content_length_ || content_length_computed_) |
45 return content_length_; | 41 return content_length_; |
46 | 42 |
47 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) | 43 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) |
48 return static_cast<uint64>(bytes_.size()); | 44 return static_cast<uint64>(bytes_.size()); |
49 else if (type_ == TYPE_BLOB) | 45 else if (type_ == TYPE_BLOB) |
50 // The blob reference will be resolved later. | 46 // 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, | 135 elements_.back().SetToFilePathRange(file_path, offset, length, |
140 expected_modification_time); | 136 expected_modification_time); |
141 } | 137 } |
142 | 138 |
143 void UploadData::AppendBlob(const GURL& blob_url) { | 139 void UploadData::AppendBlob(const GURL& blob_url) { |
144 DCHECK(!is_chunked_); | 140 DCHECK(!is_chunked_); |
145 elements_.push_back(Element()); | 141 elements_.push_back(Element()); |
146 elements_.back().SetToBlobUrl(blob_url); | 142 elements_.back().SetToBlobUrl(blob_url); |
147 } | 143 } |
148 | 144 |
149 void UploadData::AppendChunk(const char* bytes, int bytes_len) { | 145 void UploadData::AppendChunk(const char* bytes, int bytes_len, |
willchan no longer on Chromium
2011/03/03 19:47:48
http://dev.chromium.org/developers/coding-style#Co
| |
146 bool is_last_chunk) { | |
150 DCHECK(is_chunked_); | 147 DCHECK(is_chunked_); |
151 elements_.push_back(Element()); | 148 elements_.push_back(Element()); |
152 elements_.back().SetToChunk(bytes, bytes_len); | 149 elements_.back().SetToChunk(bytes, bytes_len, is_last_chunk); |
153 if (chunk_callback_) | 150 if (chunk_callback_) |
154 chunk_callback_->OnChunkAvailable(); | 151 chunk_callback_->OnChunkAvailable(); |
155 } | 152 } |
156 | 153 |
157 void UploadData::set_chunk_callback(ChunkCallback* callback) { | 154 void UploadData::set_chunk_callback(ChunkCallback* callback) { |
158 chunk_callback_ = callback; | 155 chunk_callback_ = callback; |
159 } | 156 } |
160 | 157 |
161 uint64 UploadData::GetContentLength() { | 158 uint64 UploadData::GetContentLength() { |
162 uint64 len = 0; | 159 uint64 len = 0; |
163 std::vector<Element>::iterator it = elements_.begin(); | 160 std::vector<Element>::iterator it = elements_.begin(); |
164 for (; it != elements_.end(); ++it) | 161 for (; it != elements_.end(); ++it) |
165 len += (*it).GetContentLength(); | 162 len += (*it).GetContentLength(); |
166 return len; | 163 return len; |
167 } | 164 } |
168 | 165 |
169 void UploadData::SetElements(const std::vector<Element>& elements) { | 166 void UploadData::SetElements(const std::vector<Element>& elements) { |
170 elements_ = elements; | 167 elements_ = elements; |
171 } | 168 } |
172 | 169 |
173 UploadData::~UploadData() { | 170 UploadData::~UploadData() { |
174 } | 171 } |
175 | 172 |
176 } // namespace net | 173 } // namespace net |
OLD | NEW |