Skip to content

Commit

Permalink
net: Remove UploadElement::TYPE_CHUNK
Browse files Browse the repository at this point in the history
All chunk related information is handled by UploadData.
Now UploadElement has no idea about chunks.

BUG=None
TEST=net_unittests


Review URL: https://chromiumcodereview.appspot.com/10861036

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153120 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hashimoto@chromium.org committed Aug 24, 2012
1 parent ecd2fd7 commit b2f3107
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 78 deletions.
25 changes: 25 additions & 0 deletions chrome_frame/urlmon_url_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,31 @@ void UrlmonUrlRequestManager::StartRequestHelper(
new_request = created_request;
}

// Format upload data if it's chunked.
if (request_info.upload_data && request_info.upload_data->is_chunked()) {
std::vector<net::UploadElement>* elements =
request_info.upload_data->elements_mutable();
for (size_t i = 0; i < elements->size(); ++i) {
net::UploadElement* element = &(*elements)[i];
DCHECK(element->type() == net::UploadElement::TYPE_BYTES);
std::string chunk_length = StringPrintf(
"%X\r\n", static_cast<unsigned int>(element->bytes_length()));
std::vector<char> bytes;
bytes.insert(bytes.end(), chunk_length.data(),
chunk_length.data() + chunk_length.length());
const char* data = element->bytes();
bytes.insert(bytes.end(), data, data + element->bytes_length());
const char* crlf = "\r\n";
bytes.insert(bytes.end(), crlf, crlf + strlen(crlf));
if (i == elements->size() - 1) {
const char* end_of_data = "0\r\n\r\n";
bytes.insert(bytes.end(), end_of_data,
end_of_data + strlen(end_of_data));
}
element->SetToBytes(&bytes[0], static_cast<int>(bytes.size()));
}
}

new_request->Initialize(static_cast<PluginUrlRequestDelegate*>(this),
request_id,
request_info.url,
Expand Down
41 changes: 5 additions & 36 deletions content/public/common/common_param_traits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,6 @@ struct ParamTraits<net::UploadElement> {
m->WriteData(p.bytes(), static_cast<int>(p.bytes_length()));
break;
}
case net::UploadElement::TYPE_CHUNK: {
std::string chunk_length = StringPrintf(
"%X\r\n", static_cast<unsigned int>(p.bytes_length()));
std::vector<char> bytes;
bytes.insert(bytes.end(), chunk_length.data(),
chunk_length.data() + chunk_length.length());
const char* data = p.bytes();
bytes.insert(bytes.end(), data, data + p.bytes_length());
const char* crlf = "\r\n";
bytes.insert(bytes.end(), crlf, crlf + strlen(crlf));
if (p.is_last_chunk()) {
const char* end_of_data = "0\r\n\r\n";
bytes.insert(bytes.end(), end_of_data,
end_of_data + strlen(end_of_data));
}
m->WriteData(&bytes[0], static_cast<int>(bytes.size()));
// If this element is part of a chunk upload then send over information
// indicating if this is the last chunk.
WriteParam(m, p.is_last_chunk());
break;
}
default: {
DCHECK(p.type() == net::UploadElement::TYPE_FILE);
WriteParam(m, p.file_path());
Expand All @@ -181,21 +160,6 @@ struct ParamTraits<net::UploadElement> {
r->SetToBytes(data, len);
break;
}
case net::UploadElement::TYPE_CHUNK: {
const char* data;
int len;
if (!m->ReadData(iter, &data, &len))
return false;
r->SetToBytes(data, len);
// If this element is part of a chunk upload then we need to explicitly
// set the type of the element and whether it is the last chunk.
bool is_last_chunk = false;
if (!ReadParam(m, iter, &is_last_chunk))
return false;
r->set_type(net::UploadElement::TYPE_CHUNK);
r->set_is_last_chunk(is_last_chunk);
break;
}
default: {
DCHECK(type == net::UploadElement::TYPE_FILE);
FilePath file_path;
Expand Down Expand Up @@ -228,6 +192,7 @@ void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m,
WriteParam(m, *p->elements());
WriteParam(m, p->identifier());
WriteParam(m, p->is_chunked());
WriteParam(m, p->last_chunk_appended());
}
}

Expand All @@ -248,10 +213,14 @@ bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m,
bool is_chunked = false;
if (!ReadParam(m, iter, &is_chunked))
return false;
bool last_chunk_appended = false;
if (!ReadParam(m, iter, &last_chunk_appended))
return false;
*r = new net::UploadData;
(*r)->swap_elements(&elements);
(*r)->set_identifier(identifier);
(*r)->set_is_chunked(is_chunked);
(*r)->set_last_chunk_appended(last_chunk_appended);
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions net/base/upload_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ void OnGetContentLengthComplete(
UploadData::UploadData()
: identifier_(0),
chunk_callback_(NULL),
is_chunked_(false) {
is_chunked_(false),
last_chunk_appended_(false) {
}

void UploadData::AppendBytes(const char* bytes, int bytes_len) {
Expand All @@ -49,8 +50,10 @@ void UploadData::AppendChunk(const char* bytes,
int bytes_len,
bool is_last_chunk) {
DCHECK(is_chunked_);
DCHECK(!last_chunk_appended_);
elements_.push_back(UploadElement());
elements_.back().SetToChunk(bytes, bytes_len, is_last_chunk);
elements_.back().SetToBytes(bytes, bytes_len);
last_chunk_appended_ = is_last_chunk;
if (chunk_callback_)
chunk_callback_->OnChunkAvailable();
}
Expand Down
4 changes: 4 additions & 0 deletions net/base/upload_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class NET_EXPORT UploadData
void set_is_chunked(bool set) { is_chunked_ = set; }
bool is_chunked() const { return is_chunked_; }

void set_last_chunk_appended(bool set) { last_chunk_appended_ = set; }
bool last_chunk_appended() const { return last_chunk_appended_; }

// Gets the total size in bytes of the data to upload. Computing the
// content length can result in performing file IO hence the operation is
// done asynchronously. Runs the callback with the content length once the
Expand Down Expand Up @@ -119,6 +122,7 @@ class NET_EXPORT UploadData
int64 identifier_;
ChunkCallback* chunk_callback_;
bool is_chunked_;
bool last_chunk_appended_;

DISALLOW_COPY_AND_ASSIGN(UploadData);
};
Expand Down
9 changes: 3 additions & 6 deletions net/base/upload_data_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int UploadDataStream::Read(IOBuffer* buf, int buf_len) {
buf_len - bytes_copied);

if (element.BytesRemaining() == 0)
++element_index_;
++element_index_;

if (is_chunked() && !merge_chunks_)
break;
Expand All @@ -99,12 +99,9 @@ bool UploadDataStream::IsEOF() const {

// Check if all elements are consumed.
if (element_index_ == elements.size()) {
// If the upload data is chunked, check if the last element is the
// last chunk.
if (!upload_data_->is_chunked() ||
(!elements.empty() && elements.back().is_last_chunk())) {
// If the upload data is chunked, check if the last chunk is appended.
if (!upload_data_->is_chunked() || upload_data_->last_chunk_appended())
return true;
}
}
return false;
}
Expand Down
15 changes: 3 additions & 12 deletions net/base/upload_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ UploadElement::UploadElement()
bytes_length_(0),
file_range_offset_(0),
file_range_length_(kuint64max),
is_last_chunk_(false),
override_content_length_(false),
content_length_computed_(false),
content_length_(-1),
Expand All @@ -37,19 +36,11 @@ UploadElement::~UploadElement() {
}
}

void UploadElement::SetToChunk(const char* bytes,
int bytes_len,
bool is_last_chunk) {
type_ = TYPE_CHUNK;
buf_.assign(bytes, bytes + bytes_len);
is_last_chunk_ = is_last_chunk;
}

uint64 UploadElement::GetContentLength() {
if (override_content_length_ || content_length_computed_)
return content_length_;

if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK)
if (type_ == TYPE_BYTES)
return bytes_length();

DCHECK_EQ(TYPE_FILE, type_);
Expand Down Expand Up @@ -82,7 +73,7 @@ uint64 UploadElement::GetContentLength() {
}

int UploadElement::ReadSync(char* buf, int buf_len) {
if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) {
if (type_ == TYPE_BYTES) {
return ReadFromMemorySync(buf, buf_len);
} else if (type_ == TYPE_FILE) {
return ReadFromFileSync(buf, buf_len);
Expand Down Expand Up @@ -136,7 +127,7 @@ FileStream* UploadElement::OpenFileStream() {

int UploadElement::ReadFromMemorySync(char* buf, int buf_len) {
DCHECK_LT(0, buf_len);
DCHECK(type_ == TYPE_BYTES || type_ == TYPE_CHUNK);
DCHECK(type_ == TYPE_BYTES);

const size_t num_bytes_to_read = std::min(BytesRemaining(),
static_cast<uint64>(buf_len));
Expand Down
23 changes: 1 addition & 22 deletions net/base/upload_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,12 @@ class NET_EXPORT UploadElement {
enum Type {
TYPE_BYTES,
TYPE_FILE,

// A block of bytes to be sent in chunked encoding immediately, without
// waiting for rest of the data.
TYPE_CHUNK,
};

UploadElement();
~UploadElement();

Type type() const { return type_; }
// Explicitly sets the type of this UploadElement. Used during IPC
// marshalling.
void set_type(Type type) {
type_ = type;
}

const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; }
uint64 bytes_length() const { return buf_.size() + bytes_length_; }
Expand Down Expand Up @@ -81,17 +72,6 @@ class NET_EXPORT UploadElement {
expected_file_modification_time_ = expected_modification_time;
}

// Though similar to bytes, a chunk indicates that the element is sent via
// chunked transfer encoding and not buffered until the full upload data
// is available.
void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk);

bool is_last_chunk() const { return is_last_chunk_; }
// Sets whether this is the last chunk. Used during IPC marshalling.
void set_is_last_chunk(bool is_last_chunk) {
is_last_chunk_ = is_last_chunk;
}

// Returns the byte-length of the element. For files that do not exist, 0
// is returned. This is done for consistency with Mozilla.
uint64 GetContentLength();
Expand All @@ -115,7 +95,7 @@ class NET_EXPORT UploadElement {
FileStream* OpenFileStream();

// Reads up to |buf_len| bytes synchronously from memory (i.e. type_ is
// TYPE_BYTES or TYPE_CHUNK).
// TYPE_BYTES).
int ReadFromMemorySync(char* buf, int buf_len);

// Reads up to |buf_len| bytes synchronously from a file (i.e. type_ is
Expand All @@ -136,7 +116,6 @@ class NET_EXPORT UploadElement {
uint64 file_range_offset_;
uint64 file_range_length_;
base::Time expected_file_modification_time_;
bool is_last_chunk_;
bool override_content_length_;
bool content_length_computed_;
uint64 content_length_;
Expand Down

0 comments on commit b2f3107

Please sign in to comment.