diff options
author | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-25 07:17:11 +0000 |
---|---|---|
committer | satish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-25 07:17:11 +0000 |
commit | 699efe606ab0e0995e3613f5fb880babbc66e8fc (patch) | |
tree | 7a06b83d08b940a4c04c96ee994a600399c7a5ed /net/url_request | |
parent | 2d6a4beb5df1d9377a141b12dbb2bf6225cb3d5d (diff) | |
download | chromium_src-699efe606ab0e0995e3613f5fb880babbc66e8fc.zip chromium_src-699efe606ab0e0995e3613f5fb880babbc66e8fc.tar.gz chromium_src-699efe606ab0e0995e3613f5fb880babbc66e8fc.tar.bz2 |
Prototype of chunked transfer encoded POST.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6134003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72471 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request')
-rw-r--r-- | net/url_request/url_request.cc | 21 | ||||
-rw-r--r-- | net/url_request/url_request.h | 14 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 62 |
3 files changed, 97 insertions, 0 deletions
diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc index d901d83..300c9c6 100644 --- a/net/url_request/url_request.cc +++ b/net/url_request/url_request.cc @@ -160,6 +160,27 @@ void URLRequest::AppendFileRangeToUpload( expected_modification_time); } +void URLRequest::EnableChunkedUpload() { + DCHECK(!upload_ || upload_->is_chunked()); + if (!upload_) { + upload_ = new UploadData(); + upload_->set_is_chunked(true); + } +} + +void URLRequest::AppendChunkToUpload(const char* bytes, int bytes_len) { + DCHECK(upload_); + DCHECK(upload_->is_chunked()); + DCHECK_GT(bytes_len, 0); + upload_->AppendChunk(bytes, bytes_len); +} + +void URLRequest::MarkEndOfChunks() { + DCHECK(upload_); + DCHECK(upload_->is_chunked()); + upload_->AppendChunk(NULL, 0); +} + void URLRequest::set_upload(net::UploadData* upload) { upload_ = upload; } diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h index f257ed6..3f399a6 100644 --- a/net/url_request/url_request.h +++ b/net/url_request/url_request.h @@ -320,6 +320,20 @@ class URLRequest : public base::NonThreadSafe { AppendFileRangeToUpload(file_path, 0, kuint64max, base::Time()); } + // Indicates that the request body should be sent using chunked transfer + // encoding. This method may only be called before Start() is called. + void EnableChunkedUpload(); + + // Appends the given bytes to the request's upload data to be sent + // immediately via chunked transfer encoding. When all data has been sent, + // call MarkEndOfChunks() to indicate the end of upload data. + // + // This method may be called only after calling EnableChunkedUpload(). + void AppendChunkToUpload(const char* bytes, int bytes_len); + + // Indicates the end of a chunked transfer encoded request body. + void MarkEndOfChunks(); + // Set the upload data directly. void set_upload(net::UploadData* upload); diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index f920fa4..ce02eed 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -177,6 +177,31 @@ class URLRequestTestHTTP : public URLRequestTest { delete[] uploadBytes; } + void AddChunksToUpload(TestURLRequest* r) { + r->AppendChunkToUpload("a", 1); + r->AppendChunkToUpload("bcd", 3); + r->AppendChunkToUpload("this is a longer chunk than before.", 35); + r->AppendChunkToUpload("\r\n\r\n", 4); + r->AppendChunkToUpload("0", 1); + r->AppendChunkToUpload("2323", 4); + r->MarkEndOfChunks(); + } + + void VerifyReceivedDataMatchesChunks(TestURLRequest* r, TestDelegate* d) { + // This should match the chunks sent by AddChunksToUpload(). + const char* expected_data = + "abcdthis is a longer chunk than before.\r\n\r\n02323"; + + ASSERT_EQ(1, d->response_started_count()) << "request failed: " << + (int) r->status().status() << ", os error: " << r->status().os_error(); + + EXPECT_FALSE(d->received_data_before_response()); + + ASSERT_EQ(strlen(expected_data), static_cast<size_t>(d->bytes_received())); + EXPECT_EQ(0, memcmp(d->data_received().c_str(), expected_data, + strlen(expected_data))); + } + net::TestServer test_server_; }; @@ -639,6 +664,43 @@ TEST_F(URLRequestTestHTTP, PostFileTest) { } } +TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { + ASSERT_TRUE(test_server_.Start()); + + TestDelegate d; + { + TestURLRequest r(test_server_.GetURL("echo"), &d); + r.EnableChunkedUpload(); + r.set_method("POST"); + AddChunksToUpload(&r); + r.Start(); + EXPECT_TRUE(r.is_pending()); + + MessageLoop::current()->Run(); + + VerifyReceivedDataMatchesChunks(&r, &d); + } +} + +TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { + ASSERT_TRUE(test_server_.Start()); + + TestDelegate d; + { + TestURLRequest r(test_server_.GetURL("echo"), &d); + r.EnableChunkedUpload(); + r.set_method("POST"); + r.Start(); + EXPECT_TRUE(r.is_pending()); + + MessageLoop::current()->RunAllPending(); + AddChunksToUpload(&r); + MessageLoop::current()->Run(); + + VerifyReceivedDataMatchesChunks(&r, &d); + } +} + TEST_F(URLRequestTest, AboutBlankTest) { TestDelegate d; { |