Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: net/base/upload_data.cc

Issue 6134003: Prototype of chunked transfer encoded POST. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "net/base/file_stream.h" 10 #include "net/base/file_stream.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),
19 is_last_chunk_(false),
18 override_content_length_(false), 20 override_content_length_(false),
19 content_length_computed_(false), 21 content_length_computed_(false),
20 content_length_(-1), 22 content_length_(-1),
21 file_stream_(NULL) { 23 file_stream_(NULL) {
22 } 24 }
23 25
24 UploadData::Element::~Element() { 26 UploadData::Element::~Element() {
25 // In the common case |file__stream_| will be null. 27 // In the common case |file__stream_| will be null.
26 delete file_stream_; 28 delete file_stream_;
27 } 29 }
28 30
29 uint64 UploadData::Element::GetContentLength() { 31 uint64 UploadData::Element::GetContentLength() {
30 if (override_content_length_ || content_length_computed_) 32 if (override_content_length_ || content_length_computed_)
31 return content_length_; 33 return content_length_;
32 34
33 if (type_ == TYPE_BYTES) 35 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK)
34 return static_cast<uint64>(bytes_.size()); 36 return static_cast<uint64>(bytes_.size());
35 else if (type_ == TYPE_BLOB) 37 else if (type_ == TYPE_BLOB)
36 // The blob reference will be resolved later. 38 // The blob reference will be resolved later.
37 return 0; 39 return 0;
38 40
39 DCHECK_EQ(TYPE_FILE, type_); 41 DCHECK_EQ(TYPE_FILE, type_);
40 DCHECK(!file_stream_); 42 DCHECK(!file_stream_);
41 43
42 // TODO(darin): This size calculation could be out of sync with the state of 44 // 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 45 // the file when we get around to reading it. We should probably find a way
(...skipping 14 matching lines...) Expand all
58 return 0; 60 return 0;
59 61
60 if (file_range_offset_ >= static_cast<uint64>(length)) 62 if (file_range_offset_ >= static_cast<uint64>(length))
61 return 0; // range is beyond eof 63 return 0; // range is beyond eof
62 64
63 // compensate for the offset and clip file_range_length_ to eof 65 // compensate for the offset and clip file_range_length_ to eof
64 content_length_ = std::min(length - file_range_offset_, file_range_length_); 66 content_length_ = std::min(length - file_range_offset_, file_range_length_);
65 return content_length_; 67 return content_length_;
66 } 68 }
67 69
70 void UploadData::Element::SetToChunk(const char* bytes, int bytes_len) {
71 std::string chunk_length = StringPrintf("%X\r\n", bytes_len);
72 bytes_.clear();
73 bytes_.insert(bytes_.end(), chunk_length.data(),
74 chunk_length.data() + chunk_length.length());
75 bytes_.insert(bytes_.end(), bytes, bytes + bytes_len);
76 const char* crlf = "\r\n";
77 bytes_.insert(bytes_.end(), crlf, crlf + 2);
78 type_ = TYPE_CHUNK;
79 is_last_chunk_ = (bytes_len == 0);
80 }
81
68 FileStream* UploadData::Element::NewFileStreamForReading() { 82 FileStream* UploadData::Element::NewFileStreamForReading() {
69 // In common usage GetContentLength() will call this first and store the 83 // In common usage GetContentLength() will call this first and store the
70 // result into |file_| and a subsequent call (from UploadDataStream) will 84 // result into |file_| and a subsequent call (from UploadDataStream) will
71 // get the cached open FileStream. 85 // get the cached open FileStream.
72 if (file_stream_) { 86 if (file_stream_) {
73 FileStream* file = file_stream_; 87 FileStream* file = file_stream_;
74 file_stream_ = NULL; 88 file_stream_ = NULL;
75 return file; 89 return file;
76 } 90 }
77 91
(...skipping 12 matching lines...) Expand all
90 DLOG(WARNING) << "Failed to seek \"" << file_path_.value() 104 DLOG(WARNING) << "Failed to seek \"" << file_path_.value()
91 << "\" to offset: " << file_range_offset_ << " (" << rv 105 << "\" to offset: " << file_range_offset_ << " (" << rv
92 << ")"; 106 << ")";
93 return NULL; 107 return NULL;
94 } 108 }
95 } 109 }
96 110
97 return file.release(); 111 return file.release();
98 } 112 }
99 113
100 UploadData::UploadData() : identifier_(0) { 114 UploadData::UploadData()
115 : identifier_(0),
116 chunk_callback_(NULL),
117 is_chunked_(false) {
101 } 118 }
102 119
103 void UploadData::AppendBytes(const char* bytes, int bytes_len) { 120 void UploadData::AppendBytes(const char* bytes, int bytes_len) {
121 DCHECK(!is_chunked_);
104 if (bytes_len > 0) { 122 if (bytes_len > 0) {
105 elements_.push_back(Element()); 123 elements_.push_back(Element());
106 elements_.back().SetToBytes(bytes, bytes_len); 124 elements_.back().SetToBytes(bytes, bytes_len);
107 } 125 }
108 } 126 }
109 127
110 void UploadData::AppendFile(const FilePath& file_path) { 128 void UploadData::AppendFile(const FilePath& file_path) {
129 DCHECK(!is_chunked_);
111 elements_.push_back(Element()); 130 elements_.push_back(Element());
112 elements_.back().SetToFilePath(file_path); 131 elements_.back().SetToFilePath(file_path);
113 } 132 }
114 133
115 void UploadData::AppendFileRange(const FilePath& file_path, 134 void UploadData::AppendFileRange(const FilePath& file_path,
116 uint64 offset, uint64 length, 135 uint64 offset, uint64 length,
117 const base::Time& expected_modification_time) { 136 const base::Time& expected_modification_time) {
137 DCHECK(!is_chunked_);
118 elements_.push_back(Element()); 138 elements_.push_back(Element());
119 elements_.back().SetToFilePathRange(file_path, offset, length, 139 elements_.back().SetToFilePathRange(file_path, offset, length,
120 expected_modification_time); 140 expected_modification_time);
121 } 141 }
122 142
123 void UploadData::AppendBlob(const GURL& blob_url) { 143 void UploadData::AppendBlob(const GURL& blob_url) {
144 DCHECK(!is_chunked_);
124 elements_.push_back(Element()); 145 elements_.push_back(Element());
125 elements_.back().SetToBlobUrl(blob_url); 146 elements_.back().SetToBlobUrl(blob_url);
126 } 147 }
127 148
149 void UploadData::AppendChunk(const char* bytes, int bytes_len) {
150 DCHECK(is_chunked_);
151 elements_.push_back(Element());
152 elements_.back().SetToChunk(bytes, bytes_len);
153 if (chunk_callback_)
154 chunk_callback_->OnChunkAvailable();
155 }
156
157 void UploadData::set_chunk_callback(ChunkCallback* callback) {
158 chunk_callback_ = callback;
159 }
160
128 uint64 UploadData::GetContentLength() { 161 uint64 UploadData::GetContentLength() {
162 DCHECK(!is_chunked_);
129 uint64 len = 0; 163 uint64 len = 0;
130 std::vector<Element>::iterator it = elements_.begin(); 164 std::vector<Element>::iterator it = elements_.begin();
131 for (; it != elements_.end(); ++it) 165 for (; it != elements_.end(); ++it)
132 len += (*it).GetContentLength(); 166 len += (*it).GetContentLength();
133 return len; 167 return len;
134 } 168 }
135 169
136 void UploadData::SetElements(const std::vector<Element>& elements) { 170 void UploadData::SetElements(const std::vector<Element>& elements) {
137 elements_ = elements; 171 elements_ = elements;
138 } 172 }
139 173
140 UploadData::~UploadData() { 174 UploadData::~UploadData() {
141 } 175 }
142 176
143 } // namespace net 177 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698