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

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

Issue 9242018: Factor out chunk encoding logic into HttpStreamParser::EncodeChunk(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_stream.h" 5 #include "net/base/upload_data_stream.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/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
10 #include "net/base/file_stream.h" 10 #include "net/base/file_stream.h"
11 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
13 13
14 namespace net { 14 namespace net {
15 15
16 const size_t UploadDataStream::kBufferSize = 16384;
16 bool UploadDataStream::merge_chunks_ = true; 17 bool UploadDataStream::merge_chunks_ = true;
17 18
18 UploadDataStream::~UploadDataStream() { 19 UploadDataStream::~UploadDataStream() {
19 } 20 }
20 21
21 UploadDataStream* UploadDataStream::Create(UploadData* data, int* error_code) { 22 UploadDataStream* UploadDataStream::Create(UploadData* data, int* error_code) {
22 scoped_ptr<UploadDataStream> stream(new UploadDataStream(data)); 23 scoped_ptr<UploadDataStream> stream(new UploadDataStream(data));
23 int rv = stream->FillBuf(); 24 int rv = stream->FillBuf();
24 if (error_code) 25 if (error_code)
25 *error_code = rv; 26 *error_code = rv;
(...skipping 13 matching lines...) Expand all
39 memmove(buf_->data(), buf_->data() + num_bytes, buf_len_); 40 memmove(buf_->data(), buf_->data() + num_bytes, buf_len_);
40 } 41 }
41 42
42 FillBuf(); 43 FillBuf();
43 44
44 current_position_ += num_bytes; 45 current_position_ += num_bytes;
45 } 46 }
46 47
47 UploadDataStream::UploadDataStream(UploadData* data) 48 UploadDataStream::UploadDataStream(UploadData* data)
48 : data_(data), 49 : data_(data),
49 buf_(new IOBuffer(kBufSize)), 50 buf_(new IOBuffer(kBufferSize)),
50 buf_len_(0), 51 buf_len_(0),
51 next_element_(0), 52 next_element_(0),
52 next_element_offset_(0), 53 next_element_offset_(0),
53 next_element_remaining_(0), 54 next_element_remaining_(0),
54 total_size_(data->GetContentLength()), 55 total_size_(data->GetContentLength()),
55 current_position_(0), 56 current_position_(0),
56 eof_(false) { 57 eof_(false) {
57 } 58 }
58 59
59 int UploadDataStream::FillBuf() { 60 int UploadDataStream::FillBuf() {
60 std::vector<UploadData::Element>& elements = *data_->elements(); 61 std::vector<UploadData::Element>& elements = *data_->elements();
61 62
62 while (buf_len_ < kBufSize && next_element_ < elements.size()) { 63 while (buf_len_ < kBufferSize && next_element_ < elements.size()) {
63 bool advance_to_next_element = false; 64 bool advance_to_next_element = false;
64 65
65 UploadData::Element& element = elements[next_element_]; 66 UploadData::Element& element = elements[next_element_];
66 67
67 size_t size_remaining = kBufSize - buf_len_; 68 size_t size_remaining = kBufferSize - buf_len_;
68 if (element.type() == UploadData::TYPE_BYTES || 69 if (element.type() == UploadData::TYPE_BYTES ||
69 element.type() == UploadData::TYPE_CHUNK) { 70 element.type() == UploadData::TYPE_CHUNK) {
70 const std::vector<char>& d = element.bytes(); 71 const std::vector<char>& d = element.bytes();
71 size_t count = d.size() - next_element_offset_; 72 size_t count = d.size() - next_element_offset_;
72 73
73 size_t bytes_copied = std::min(count, size_remaining); 74 size_t bytes_copied = std::min(count, size_remaining);
74 75
75 // Check if we have anything to copy first, because we are getting the 76 // Check if we have anything to copy first, because we are getting the
76 // address of an element in |d| and that will throw an exception if |d| 77 // address of an element in |d| and that will throw an exception if |d|
77 // is an empty vector. 78 // is an empty vector.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 bool UploadDataStream::IsOnLastChunk() const { 156 bool UploadDataStream::IsOnLastChunk() const {
156 const std::vector<UploadData::Element>& elements = *data_->elements(); 157 const std::vector<UploadData::Element>& elements = *data_->elements();
157 DCHECK(data_->is_chunked()); 158 DCHECK(data_->is_chunked());
158 return (eof_ || 159 return (eof_ ||
159 (!elements.empty() && 160 (!elements.empty() &&
160 next_element_ == elements.size() && 161 next_element_ == elements.size() &&
161 elements.back().is_last_chunk())); 162 elements.back().is_last_chunk()));
162 } 163 }
163 164
164 } // namespace net 165 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698