Commit c359e685 authored by Chris Mumford's avatar Chris Mumford Committed by Commit Bot

Added offset/length to SimpleURLLoader::AttachFileForUpload.

Previously SimpleURLLoader would always upload the entire file. This
allows the caller to specify a portion of the file to be uploaded.

Bug: 844979
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I8c2d6267fb65a5012f49c9f3ce943c44dc4837db
Reviewed-on: https://chromium-review.googlesource.com/1180072
Commit-Queue: Chris Mumford <cmumford@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584472}
parent d5b9a79d
......@@ -7,7 +7,6 @@
#include <stdint.h>
#include <algorithm>
#include <limits>
#include "base/bind.h"
#include "base/files/file_path.h"
......@@ -214,8 +213,11 @@ class SimpleURLLoaderImpl : public SimpleURLLoader,
void SetAllowHttpErrorResults(bool allow_http_error_results) override;
void AttachStringForUpload(const std::string& upload_data,
const std::string& upload_content_type) override;
void AttachFileForUpload(const base::FilePath& upload_file_path,
const std::string& upload_content_type) override;
void AttachFileForUpload(
const base::FilePath& upload_file_path,
const std::string& upload_content_type,
uint64_t offset = 0,
uint64_t length = std::numeric_limits<uint64_t>::max()) override;
void SetRetryOptions(int max_retries, int retry_mode) override;
int NetError() const override;
const ResourceResponseHead* ResponseInfo() const override;
......@@ -1273,7 +1275,9 @@ void SimpleURLLoaderImpl::AttachStringForUpload(
void SimpleURLLoaderImpl::AttachFileForUpload(
const base::FilePath& upload_file_path,
const std::string& upload_content_type) {
const std::string& upload_content_type,
uint64_t offset,
uint64_t length) {
DCHECK(!upload_file_path.empty());
// Currently only allow a single file to be attached.
......@@ -1286,8 +1290,8 @@ void SimpleURLLoaderImpl::AttachFileForUpload(
resource_request_->request_body = new ResourceRequestBody();
// TODO(mmenke): Open the file in the current process and append the file
// handle instead of the file path.
resource_request_->request_body->AppendFileRange(
upload_file_path, 0, std::numeric_limits<uint64_t>::max(), base::Time());
resource_request_->request_body->AppendFileRange(upload_file_path, offset,
length, base::Time());
resource_request_->headers.SetHeader(net::HttpRequestHeaders::kContentType,
upload_content_type);
......
......@@ -279,10 +279,16 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
// open the file itself off-thread. May only be called once, and only if the
// ResourceRequest passed to the constructor had a null |request_body|.
//
// The |offset| and |length| can optionally be set to specify the desired
// range of the file to be uploaded. By default the entire file is uploaded.
//
// |content_type| will overwrite any Content-Type header in the
// ResourceRequest passed to Create().
virtual void AttachFileForUpload(const base::FilePath& upload_file_path,
const std::string& upload_content_type) = 0;
virtual void AttachFileForUpload(
const base::FilePath& upload_file_path,
const std::string& upload_content_type,
uint64_t offset = 0,
uint64_t length = std::numeric_limits<uint64_t>::max()) = 0;
// Sets the when to try and the max number of times to retry a request, if
// any. |max_retries| is the number of times to retry the request, not
......
......@@ -1555,6 +1555,24 @@ TEST_P(SimpleURLLoaderTest, UploadFile) {
}
}
TEST_P(SimpleURLLoaderTest, UploadFileRange) {
std::unique_ptr<SimpleLoaderTestHelper> test_helper =
CreateHelperForURL(test_server_.GetURL("/echo"), "POST");
// These two values should return the second line of the test file.
const uint64_t kOffset = 7;
const uint64_t kLength = 13;
test_helper->simple_url_loader()->AttachFileForUpload(
GetTestFilePath(), "text/plain", kOffset, kLength);
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents().substr(kOffset, kLength),
*test_helper->response_body());
}
}
TEST_P(SimpleURLLoaderTest, UploadFileWithPut) {
std::unique_ptr<SimpleLoaderTestHelper> test_helper =
CreateHelperForURL(test_server_.GetURL("/echo"), "PUT");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment