Index: net/base/upload_data.cc |
diff --git a/net/base/upload_data.cc b/net/base/upload_data.cc |
index 664285dca6b3b7b874a93ba1f1798ec87b91ff28..17c59825ce8d2826074e01ed35cdcb5f1af9e3aa 100644 |
--- a/net/base/upload_data.cc |
+++ b/net/base/upload_data.cc |
@@ -7,6 +7,7 @@ |
#include "base/file_util.h" |
#include "base/logging.h" |
#include "net/base/file_stream.h" |
+#include "base/string_util.h" |
wtc
2011/01/20 00:29:47
Nit: list this header in alphabetical order.
|
#include "net/base/net_errors.h" |
namespace net { |
@@ -30,7 +31,7 @@ uint64 UploadData::Element::GetContentLength() { |
if (override_content_length_ || content_length_computed_) |
return content_length_; |
- if (type_ == TYPE_BYTES) |
+ if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) |
return static_cast<uint64>(bytes_.size()); |
else if (type_ == TYPE_BLOB) |
// The blob reference will be resolved later. |
@@ -65,6 +66,22 @@ uint64 UploadData::Element::GetContentLength() { |
return content_length_; |
} |
+void UploadData::Element::SetToChunk(const char* bytes, int bytes_len) { |
+ std::string chunk_length = StringPrintf("%X\r\n", bytes_len); |
+ bytes_.clear(); |
+ bytes_.insert(bytes_.end(), chunk_length.c_str(), |
+ chunk_length.c_str() + chunk_length.length()); |
wtc
2011/01/20 00:29:47
Nit: c_str() => data()
|
+ bytes_.insert(bytes_.end(), bytes, bytes + bytes_len); |
+ const char* crlf = "\r\n"; |
+ bytes_.insert(bytes_.end(), crlf, crlf + 2); |
+ type_ = TYPE_CHUNK; |
+} |
+ |
+void UploadData::Element::SetToEndOfChunksMarker() { |
+ bytes_.clear(); |
+ type_ = TYPE_CHUNK; |
+} |
+ |
FileStream* UploadData::Element::NewFileStreamForReading() { |
// In common usage GetContentLength() will call this first and store the |
// result into |file_| and a subsequent call (from UploadDataStream) will |
@@ -97,10 +114,14 @@ FileStream* UploadData::Element::NewFileStreamForReading() { |
return file.release(); |
} |
-UploadData::UploadData() : identifier_(0) { |
+UploadData::UploadData() |
+ : identifier_(0), |
+ chunk_callback_(NULL), |
+ is_chunked_(false) { |
} |
void UploadData::AppendBytes(const char* bytes, int bytes_len) { |
+ DCHECK(!is_chunked_); |
if (bytes_len > 0) { |
elements_.push_back(Element()); |
elements_.back().SetToBytes(bytes, bytes_len); |
@@ -108,6 +129,7 @@ void UploadData::AppendBytes(const char* bytes, int bytes_len) { |
} |
void UploadData::AppendFile(const FilePath& file_path) { |
+ DCHECK(!is_chunked_); |
elements_.push_back(Element()); |
elements_.back().SetToFilePath(file_path); |
} |
@@ -115,17 +137,38 @@ void UploadData::AppendFile(const FilePath& file_path) { |
void UploadData::AppendFileRange(const FilePath& file_path, |
uint64 offset, uint64 length, |
const base::Time& expected_modification_time) { |
+ DCHECK(!is_chunked_); |
elements_.push_back(Element()); |
elements_.back().SetToFilePathRange(file_path, offset, length, |
expected_modification_time); |
} |
void UploadData::AppendBlob(const GURL& blob_url) { |
+ DCHECK(!is_chunked_); |
elements_.push_back(Element()); |
elements_.back().SetToBlobUrl(blob_url); |
} |
+void UploadData::AppendChunk(const char* bytes, int bytes_len) { |
+ DCHECK(is_chunked_); |
+ elements_.push_back(Element()); |
+ elements_.back().SetToChunk(bytes, bytes_len); |
+ if (!bytes_len) { |
vandebo (ex-Chrome)
2011/01/18 21:51:17
nit: This uses two Elements to represent the last
Satish
2011/01/20 18:02:36
I avoided doing that because it would take up unwa
vandebo (ex-Chrome)
2011/01/20 20:48:45
There are already several bools in the Element cla
|
+ // End of chunked stream is marked by a chunk with an empty |bytes_|. |
wtc
2011/01/20 00:29:47
Nit: chunked stream => chunks
|
+ elements_.push_back(Element()); |
+ elements_.back().SetToEndOfChunksMarker(); |
+ } |
+ if (chunk_callback_) |
+ chunk_callback_->OnChunkAvailable(); |
+} |
+ |
+void UploadData::set_chunk_callback(ChunkCallback* callback) { |
+ DCHECK(!chunk_callback_); |
wtc
2011/01/20 00:29:47
IMPORTANT: I believe this DCHECK is too strong. W
|
+ chunk_callback_ = callback; |
+} |
+ |
uint64 UploadData::GetContentLength() { |
+ DCHECK(!is_chunked_); |
uint64 len = 0; |
std::vector<Element>::iterator it = elements_.begin(); |
for (; it != elements_.end(); ++it) |