Chromium Code Reviews| 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 "net/base/file_stream.h" | 9 #include "net/base/file_stream.h" |
| 10 #include "base/string_util.h" | |
| 10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 UploadData::Element::Element() | 15 UploadData::Element::Element() |
| 15 : type_(TYPE_BYTES), | 16 : type_(TYPE_BYTES), |
| 16 file_range_offset_(0), | 17 file_range_offset_(0), |
| 17 file_range_length_(kuint64max), | 18 file_range_length_(kuint64max), |
| 18 override_content_length_(false), | 19 override_content_length_(false), |
| 19 content_length_computed_(false), | 20 content_length_computed_(false), |
| 20 content_length_(-1), | 21 content_length_(-1), |
| 21 file_stream_(NULL) { | 22 file_stream_(NULL) { |
| 22 } | 23 } |
| 23 | 24 |
| 24 UploadData::Element::~Element() { | 25 UploadData::Element::~Element() { |
| 25 // In the common case |file__stream_| will be null. | 26 // In the common case |file__stream_| will be null. |
| 26 delete file_stream_; | 27 delete file_stream_; |
| 27 } | 28 } |
| 28 | 29 |
| 29 uint64 UploadData::Element::GetContentLength() { | 30 uint64 UploadData::Element::GetContentLength() { |
| 30 if (override_content_length_ || content_length_computed_) | 31 if (override_content_length_ || content_length_computed_) |
| 31 return content_length_; | 32 return content_length_; |
| 32 | 33 |
| 33 if (type_ == TYPE_BYTES) | 34 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) |
| 34 return static_cast<uint64>(bytes_.size()); | 35 return static_cast<uint64>(bytes_.size()); |
| 35 else if (type_ == TYPE_BLOB) | 36 else if (type_ == TYPE_BLOB) |
| 36 // The blob reference will be resolved later. | 37 // The blob reference will be resolved later. |
| 37 return 0; | 38 return 0; |
| 38 | 39 |
| 39 DCHECK_EQ(TYPE_FILE, type_); | 40 DCHECK_EQ(TYPE_FILE, type_); |
| 40 DCHECK(!file_stream_); | 41 DCHECK(!file_stream_); |
| 41 | 42 |
| 42 // TODO(darin): This size calculation could be out of sync with the state of | 43 // TODO(darin): This size calculation could be out of sync with the state of |
| 43 // the file when we get around to reading it. We should probably find a way | 44 // the file when we get around to reading it. We should probably find a way |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 58 return 0; | 59 return 0; |
| 59 | 60 |
| 60 if (file_range_offset_ >= static_cast<uint64>(length)) | 61 if (file_range_offset_ >= static_cast<uint64>(length)) |
| 61 return 0; // range is beyond eof | 62 return 0; // range is beyond eof |
| 62 | 63 |
| 63 // compensate for the offset and clip file_range_length_ to eof | 64 // compensate for the offset and clip file_range_length_ to eof |
| 64 content_length_ = std::min(length - file_range_offset_, file_range_length_); | 65 content_length_ = std::min(length - file_range_offset_, file_range_length_); |
| 65 return content_length_; | 66 return content_length_; |
| 66 } | 67 } |
| 67 | 68 |
| 69 void UploadData::Element::SetToChunk(const char* bytes, int bytes_len) { | |
| 70 std::string chunk_length = StringPrintf("%X\r\n", bytes_len); | |
| 71 bytes_.clear(); | |
| 72 bytes_.insert(bytes_.end(), chunk_length.c_str(), | |
| 73 chunk_length.c_str() + chunk_length.length()); | |
| 74 bytes_.insert(bytes_.end(), bytes, bytes + bytes_len); | |
| 75 const char* crlf = "\r\n"; | |
| 76 bytes_.insert(bytes_.end(), crlf, crlf + 2); | |
| 77 | |
| 78 // The last chunk (of length 0 bytes) is of type BYTES to indicate that | |
| 79 // we have no more. This is used by UploadDataStream. | |
|
wtc
2011/01/12 02:39:02
Why don't you just use the 0 chunk size to indicat
Satish
2011/01/13 17:43:27
Done, I now add a "0\r\n\r\n" chunk followed by a
| |
| 80 type_ = (bytes_len ? TYPE_CHUNK : TYPE_BYTES); | |
| 81 } | |
| 82 | |
| 68 FileStream* UploadData::Element::NewFileStreamForReading() { | 83 FileStream* UploadData::Element::NewFileStreamForReading() { |
| 69 // In common usage GetContentLength() will call this first and store the | 84 // In common usage GetContentLength() will call this first and store the |
| 70 // result into |file_| and a subsequent call (from UploadDataStream) will | 85 // result into |file_| and a subsequent call (from UploadDataStream) will |
| 71 // get the cached open FileStream. | 86 // get the cached open FileStream. |
| 72 if (file_stream_) { | 87 if (file_stream_) { |
| 73 FileStream* file = file_stream_; | 88 FileStream* file = file_stream_; |
| 74 file_stream_ = NULL; | 89 file_stream_ = NULL; |
| 75 return file; | 90 return file; |
| 76 } | 91 } |
| 77 | 92 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 90 DLOG(WARNING) << "Failed to seek \"" << file_path_.value() | 105 DLOG(WARNING) << "Failed to seek \"" << file_path_.value() |
| 91 << "\" to offset: " << file_range_offset_ << " (" << rv | 106 << "\" to offset: " << file_range_offset_ << " (" << rv |
| 92 << ")"; | 107 << ")"; |
| 93 return NULL; | 108 return NULL; |
| 94 } | 109 } |
| 95 } | 110 } |
| 96 | 111 |
| 97 return file.release(); | 112 return file.release(); |
| 98 } | 113 } |
| 99 | 114 |
| 100 UploadData::UploadData() : identifier_(0) { | 115 UploadData::UploadData() : identifier_(0), data_callback_(NULL) { |
| 101 } | 116 } |
| 102 | 117 |
| 103 void UploadData::AppendBytes(const char* bytes, int bytes_len) { | 118 void UploadData::AppendBytes(const char* bytes, int bytes_len) { |
| 119 DCHECK(!is_chunked_); | |
| 104 if (bytes_len > 0) { | 120 if (bytes_len > 0) { |
| 105 elements_.push_back(Element()); | 121 elements_.push_back(Element()); |
| 106 elements_.back().SetToBytes(bytes, bytes_len); | 122 elements_.back().SetToBytes(bytes, bytes_len); |
| 107 } | 123 } |
| 108 } | 124 } |
| 109 | 125 |
| 110 void UploadData::AppendFile(const FilePath& file_path) { | 126 void UploadData::AppendFile(const FilePath& file_path) { |
| 127 DCHECK(!is_chunked_); | |
| 111 elements_.push_back(Element()); | 128 elements_.push_back(Element()); |
| 112 elements_.back().SetToFilePath(file_path); | 129 elements_.back().SetToFilePath(file_path); |
| 113 } | 130 } |
| 114 | 131 |
| 115 void UploadData::AppendFileRange(const FilePath& file_path, | 132 void UploadData::AppendFileRange(const FilePath& file_path, |
| 116 uint64 offset, uint64 length, | 133 uint64 offset, uint64 length, |
| 117 const base::Time& expected_modification_time) { | 134 const base::Time& expected_modification_time) { |
| 135 DCHECK(!is_chunked_); | |
| 118 elements_.push_back(Element()); | 136 elements_.push_back(Element()); |
| 119 elements_.back().SetToFilePathRange(file_path, offset, length, | 137 elements_.back().SetToFilePathRange(file_path, offset, length, |
| 120 expected_modification_time); | 138 expected_modification_time); |
| 121 } | 139 } |
| 122 | 140 |
| 123 void UploadData::AppendBlob(const GURL& blob_url) { | 141 void UploadData::AppendBlob(const GURL& blob_url) { |
| 142 DCHECK(!is_chunked_); | |
| 124 elements_.push_back(Element()); | 143 elements_.push_back(Element()); |
| 125 elements_.back().SetToBlobUrl(blob_url); | 144 elements_.back().SetToBlobUrl(blob_url); |
| 126 } | 145 } |
| 127 | 146 |
| 147 void UploadData::AppendChunk(const char* bytes, int bytes_len) { | |
| 148 DCHECK(is_chunked_); | |
| 149 elements_.push_back(Element()); | |
| 150 elements_.back().SetToChunk(bytes, bytes_len); | |
| 151 if (data_callback_) { | |
| 152 CompletionCallback* c = data_callback_; | |
| 153 //data_callback_ = NULL; | |
|
wtc
2011/01/12 02:39:02
This is a sign that CompletionCallback is the wron
Satish
2011/01/13 17:43:27
Changed to an interface/method call
| |
| 154 c->Run(0); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void UploadData::set_data_callback(CompletionCallback* callback) { | |
| 159 DCHECK(!data_callback_); | |
| 160 data_callback_ = callback; | |
| 161 } | |
| 162 | |
| 128 uint64 UploadData::GetContentLength() { | 163 uint64 UploadData::GetContentLength() { |
| 129 uint64 len = 0; | 164 uint64 len = 0; |
| 130 std::vector<Element>::iterator it = elements_.begin(); | 165 std::vector<Element>::iterator it = elements_.begin(); |
| 131 for (; it != elements_.end(); ++it) | 166 for (; it != elements_.end(); ++it) |
| 132 len += (*it).GetContentLength(); | 167 len += (*it).GetContentLength(); |
| 133 return len; | 168 return len; |
| 134 } | 169 } |
| 135 | 170 |
| 136 void UploadData::SetElements(const std::vector<Element>& elements) { | 171 void UploadData::SetElements(const std::vector<Element>& elements) { |
| 137 elements_ = elements; | 172 elements_ = elements; |
| 138 } | 173 } |
| 139 | 174 |
| 140 UploadData::~UploadData() { | 175 UploadData::~UploadData() { |
| 141 } | 176 } |
| 142 | 177 |
| 143 } // namespace net | 178 } // namespace net |
| OLD | NEW |