Commit c2e91393 authored by Sorin Jianu's avatar Sorin Jianu Committed by Commit Bot

RequestSender sets the content type header as application/json.

This is done uniformly for all clients of update_client including
the chrome/updater.

Unfortunately, the content-type is still done by changing each
network stack, since it can't be done cleanly with a "Content-Type"
header.

Bug: 1095704

Change-Id: I16a7523e16cd846075fee8d28852b8ddb7eaf265
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2248721Reviewed-by: default avatarS. Ganesh <ganesh@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Commit-Queue: Sorin Jianu <sorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779590}
parent d723c139
...@@ -33,6 +33,7 @@ class NetworkFetcher : public update_client::NetworkFetcher { ...@@ -33,6 +33,7 @@ class NetworkFetcher : public update_client::NetworkFetcher {
void PostRequest( void PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
update_client::NetworkFetcher::ResponseStartedCallback update_client::NetworkFetcher::ResponseStartedCallback
response_started_callback, response_started_callback,
......
...@@ -272,6 +272,7 @@ NetworkFetcher::~NetworkFetcher() = default; ...@@ -272,6 +272,7 @@ NetworkFetcher::~NetworkFetcher() = default;
void NetworkFetcher::PostRequest( void NetworkFetcher::PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback, ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback, ProgressCallback progress_callback,
...@@ -296,6 +297,8 @@ void NetworkFetcher::PostRequest( ...@@ -296,6 +297,8 @@ void NetworkFetcher::PostRequest(
[urlRequest setHTTPMethod:@"POST"]; [urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[base::SysUTF8ToNSString(post_data) [urlRequest setHTTPBody:[base::SysUTF8ToNSString(post_data)
dataUsingEncoding:NSUTF8StringEncoding]]; dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest addValue:base::SysUTF8ToNSString(content_type)
forHTTPHeaderField:@"Content-Type"];
VLOG(1) << "Posting data: " << post_data.c_str(); VLOG(1) << "Posting data: " << post_data.c_str();
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:urlRequest]; NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:urlRequest];
......
...@@ -108,7 +108,7 @@ TEST_F(ChromeUpdaterNetworkMacTest, NetworkFetcherMacPostRequest) { ...@@ -108,7 +108,7 @@ TEST_F(ChromeUpdaterNetworkMacTest, NetworkFetcherMacPostRequest) {
const GURL url = test_server.GetURL("/echo"); const GURL url = test_server.GetURL("/echo");
fetcher->PostRequest( fetcher->PostRequest(
url, "", {}, url, {}, {}, {},
base::BindOnce(&ChromeUpdaterNetworkMacTest::StartedCallback, base::BindOnce(&ChromeUpdaterNetworkMacTest::StartedCallback,
base::Unretained(this)), base::Unretained(this)),
base::BindRepeating(&ChromeUpdaterNetworkMacTest::ProgressCallback, base::BindRepeating(&ChromeUpdaterNetworkMacTest::ProgressCallback,
......
...@@ -31,6 +31,7 @@ NetworkFetcher::~NetworkFetcher() { ...@@ -31,6 +31,7 @@ NetworkFetcher::~NetworkFetcher() {
void NetworkFetcher::PostRequest( void NetworkFetcher::PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback, ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback, ProgressCallback progress_callback,
...@@ -38,7 +39,7 @@ void NetworkFetcher::PostRequest( ...@@ -38,7 +39,7 @@ void NetworkFetcher::PostRequest(
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
post_request_complete_callback_ = std::move(post_request_complete_callback); post_request_complete_callback_ = std::move(post_request_complete_callback);
network_fetcher_->PostRequest( network_fetcher_->PostRequest(
url, post_data, post_additional_headers, url, post_data, content_type, post_additional_headers,
std::move(response_started_callback), std::move(progress_callback), std::move(response_started_callback), std::move(progress_callback),
base::BindOnce(&NetworkFetcher::PostRequestComplete, base::BindOnce(&NetworkFetcher::PostRequestComplete,
base::Unretained(this))); base::Unretained(this)));
......
...@@ -46,6 +46,7 @@ class NetworkFetcher : public update_client::NetworkFetcher { ...@@ -46,6 +46,7 @@ class NetworkFetcher : public update_client::NetworkFetcher {
void PostRequest( void PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback, ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback, ProgressCallback progress_callback,
......
...@@ -113,6 +113,7 @@ int64_t NetworkFetcherWinHTTP::GetContentSize() const { ...@@ -113,6 +113,7 @@ int64_t NetworkFetcherWinHTTP::GetContentSize() const {
void NetworkFetcherWinHTTP::PostRequest( void NetworkFetcherWinHTTP::PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
FetchStartedCallback fetch_started_callback, FetchStartedCallback fetch_started_callback,
FetchProgressCallback fetch_progress_callback, FetchProgressCallback fetch_progress_callback,
...@@ -128,7 +129,7 @@ void NetworkFetcherWinHTTP::PostRequest( ...@@ -128,7 +129,7 @@ void NetworkFetcherWinHTTP::PostRequest(
CrackUrl(url, &is_https_, &host_, &port_, &path_for_request_); CrackUrl(url, &is_https_, &host_, &port_, &path_for_request_);
verb_ = L"POST"; verb_ = L"POST";
content_type_ = L"Content-Type: application/json\r\n"; content_type_ = base::SysUTF8ToWide(content_type);
write_data_callback_ = write_data_callback_ =
base::BindRepeating(&NetworkFetcherWinHTTP::WriteDataToMemory, this); base::BindRepeating(&NetworkFetcherWinHTTP::WriteDataToMemory, this);
......
...@@ -48,6 +48,7 @@ class NetworkFetcherWinHTTP ...@@ -48,6 +48,7 @@ class NetworkFetcherWinHTTP
void PostRequest( void PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
FetchStartedCallback fetch_started_callback, FetchStartedCallback fetch_started_callback,
FetchProgressCallback fetch_progress_callback, FetchProgressCallback fetch_progress_callback,
...@@ -129,7 +130,7 @@ class NetworkFetcherWinHTTP ...@@ -129,7 +130,7 @@ class NetworkFetcherWinHTTP
std::string path_for_request_; std::string path_for_request_;
base::StringPiece16 verb_; base::StringPiece16 verb_;
base::StringPiece16 content_type_; base::string16 content_type_;
WriteDataCallback write_data_callback_; WriteDataCallback write_data_callback_;
HRESULT net_error_ = S_OK; HRESULT net_error_ = S_OK;
std::string etag_; std::string etag_;
......
...@@ -102,6 +102,7 @@ NetworkFetcherImpl::~NetworkFetcherImpl() = default; ...@@ -102,6 +102,7 @@ NetworkFetcherImpl::~NetworkFetcherImpl() = default;
void NetworkFetcherImpl::PostRequest( void NetworkFetcherImpl::PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback, ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback, ProgressCallback progress_callback,
...@@ -119,7 +120,9 @@ void NetworkFetcherImpl::PostRequest( ...@@ -119,7 +120,9 @@ void NetworkFetcherImpl::PostRequest(
simple_url_loader_->SetRetryOptions( simple_url_loader_->SetRetryOptions(
kMaxRetriesOnNetworkChange, kMaxRetriesOnNetworkChange,
network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE); network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
simple_url_loader_->AttachStringForUpload(post_data, "application/json"); // The `Content-Type` header set by |AttachStringForUpload| overwrites any
// `Content-Type` header present in the |ResourceRequest| above.
simple_url_loader_->AttachStringForUpload(post_data, content_type);
simple_url_loader_->SetOnResponseStartedCallback(base::BindOnce( simple_url_loader_->SetOnResponseStartedCallback(base::BindOnce(
&NetworkFetcherImpl::OnResponseStartedCallback, base::Unretained(this), &NetworkFetcherImpl::OnResponseStartedCallback, base::Unretained(this),
std::move(response_started_callback))); std::move(response_started_callback)));
......
...@@ -34,6 +34,7 @@ class NetworkFetcherImpl : public NetworkFetcher { ...@@ -34,6 +34,7 @@ class NetworkFetcherImpl : public NetworkFetcher {
void PostRequest( void PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback, ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback, ProgressCallback progress_callback,
......
...@@ -55,6 +55,7 @@ class NetworkFetcher { ...@@ -55,6 +55,7 @@ class NetworkFetcher {
virtual void PostRequest( virtual void PostRequest(
const GURL& url, const GURL& url,
const std::string& post_data, const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers, const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback, ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback, ProgressCallback progress_callback,
......
...@@ -25,10 +25,13 @@ namespace { ...@@ -25,10 +25,13 @@ namespace {
// This is an ECDSA prime256v1 named-curve key. // This is an ECDSA prime256v1 named-curve key.
constexpr int kKeyVersion = 10; constexpr int kKeyVersion = 10;
const char kKeyPubBytesBase64[] = constexpr char kKeyPubBytesBase64[] =
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzOqC8cKNUYIi0UkNu0smZKDW8w5/" "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzOqC8cKNUYIi0UkNu0smZKDW8w5/"
"0EmEw1KQ6Aj/5JWBMdUVm13EIVwFwPlkO/U6vXa+iu4wyUB89GFaldJ7Bg=="; "0EmEw1KQ6Aj/5JWBMdUVm13EIVwFwPlkO/U6vXa+iu4wyUB89GFaldJ7Bg==";
// The content type for all protocol requests.
constexpr char kContentType[] = "application/json";
} // namespace } // namespace
RequestSender::RequestSender(scoped_refptr<Configurator> config) RequestSender::RequestSender(scoped_refptr<Configurator> config)
...@@ -96,7 +99,7 @@ void RequestSender::SendInternal() { ...@@ -96,7 +99,7 @@ void RequestSender::SendInternal() {
std::string(), std::string(), 0)); std::string(), std::string(), 0));
} }
network_fetcher_->PostRequest( network_fetcher_->PostRequest(
url, request_body_, request_extra_headers_, url, request_body_, kContentType, request_extra_headers_,
base::BindOnce(&RequestSender::OnResponseStarted, base::Unretained(this)), base::BindOnce(&RequestSender::OnResponseStarted, base::Unretained(this)),
base::BindRepeating([](int64_t current) {}), base::BindRepeating([](int64_t current) {}),
base::BindOnce(&RequestSender::OnNetworkFetcherComplete, base::BindOnce(&RequestSender::OnNetworkFetcherComplete,
......
...@@ -159,9 +159,12 @@ TEST_P(RequestSenderTest, RequestSendSuccess) { ...@@ -159,9 +159,12 @@ TEST_P(RequestSenderTest, RequestSendSuccess) {
const auto extra_request_headers = const auto extra_request_headers =
std::get<1>(post_interceptor_->GetRequests()[0]); std::get<1>(post_interceptor_->GetRequests()[0]);
EXPECT_TRUE(extra_request_headers.HasHeader("X-Goog-Update-Interactivity")); EXPECT_TRUE(extra_request_headers.HasHeader("X-Goog-Update-Interactivity"));
EXPECT_TRUE(extra_request_headers.HasHeader("Content-Type"));
std::string header; std::string header;
extra_request_headers.GetHeader("X-Goog-Update-Interactivity", &header); extra_request_headers.GetHeader("X-Goog-Update-Interactivity", &header);
EXPECT_STREQ(is_foreground ? "fg" : "bg", header.c_str()); EXPECT_STREQ(is_foreground ? "fg" : "bg", header.c_str());
extra_request_headers.GetHeader("Content-Type", &header);
EXPECT_STREQ("application/json", header.c_str());
} }
// Tests that the request succeeds using the second url after the first url // Tests that the request succeeds using the second url after the first url
......
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