diff options
author | noamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-14 16:52:18 +0000 |
---|---|---|
committer | noamsml@chromium.org <noamsml@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-08-14 16:54:39 +0000 |
commit | 3525eec8b8a5a3604f010a76687bc851d538e7e5 (patch) | |
tree | 5f89d889659b104a28634891cf34f35d9e648841 /chrome/browser/local_discovery | |
parent | 727903e1cdf6cd5752d89b0493c23af9fcd4999d (diff) | |
download | chromium_src-3525eec8b8a5a3604f010a76687bc851d538e7e5.zip chromium_src-3525eec8b8a5a3604f010a76687bc851d538e7e5.tar.gz chromium_src-3525eec8b8a5a3604f010a76687bc851d538e7e5.tar.bz2 |
Send privet auth token in PrivetV3 mode instead of X-Privet token
The URLFetcher will send an X-Privet-Auth token, which it gets from the
delegate, for PrivetV3. It will not send an X-Privet-Token.
BUG=
Review URL: https://codereview.chromium.org/454623002
Cr-Commit-Position: refs/heads/master@{#289614}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289614 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/local_discovery')
4 files changed, 59 insertions, 9 deletions
diff --git a/chrome/browser/local_discovery/privet_http.h b/chrome/browser/local_discovery/privet_http.h index 26f4adf..57b03a4 100644 --- a/chrome/browser/local_discovery/privet_http.h +++ b/chrome/browser/local_discovery/privet_http.h @@ -54,6 +54,7 @@ class PrivetHTTPClient { virtual scoped_ptr<PrivetJSONOperation> CreateInfoOperation( const PrivetJSONOperation::ResultCallback& callback) = 0; + // Creates a URL fetcher for PrivetV1. virtual scoped_ptr<PrivetURLFetcher> CreateURLFetcher( const GURL& url, net::URLFetcher::RequestType request_type, diff --git a/chrome/browser/local_discovery/privet_url_fetcher.cc b/chrome/browser/local_discovery/privet_url_fetcher.cc index bb64b88..ecf7c58 100644 --- a/chrome/browser/local_discovery/privet_url_fetcher.cc +++ b/chrome/browser/local_discovery/privet_url_fetcher.cc @@ -34,8 +34,10 @@ struct TokenMapHolder { }; const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; +const char kXPrivetAuthTokenHeaderPrefix[] = "X-Privet-Auth: "; const char kRangeHeaderFormat[] = "Range: bytes=%d-%d"; const char kXPrivetEmptyToken[] = "\"\""; +const char kPrivetAuthTokenUnknown[] = "Unknown"; const int kPrivetMaxRetries = 20; const int kPrivetTimeoutOnError = 5; const int kHTTPErrorCodeInvalidXPrivetToken = 418; @@ -55,6 +57,10 @@ void PrivetURLFetcher::Delegate::OnNeedPrivetToken( OnError(fetcher, TOKEN_ERROR); } +std::string PrivetURLFetcher::Delegate::GetAuthToken() { + return kPrivetAuthTokenUnknown; +} + bool PrivetURLFetcher::Delegate::OnRawData(PrivetURLFetcher* fetcher, bool response_is_file, const std::string& data_string, @@ -75,10 +81,12 @@ PrivetURLFetcher::PrivetURLFetcher( send_empty_privet_token_(false), has_byte_range_(false), make_response_file_(false), + v3_mode_(false), byte_range_start_(0), byte_range_end_(0), tries_(0), - weak_factory_(this) {} + weak_factory_(this) { +} PrivetURLFetcher::~PrivetURLFetcher() { } @@ -123,6 +131,10 @@ void PrivetURLFetcher::SaveResponseToFile() { make_response_file_ = true; } +void PrivetURLFetcher::V3Mode() { + v3_mode_ = true; +} + void PrivetURLFetcher::SetByteRange(int start, int end) { DCHECK(tries_ == 0); byte_range_start_ = start; @@ -133,15 +145,26 @@ void PrivetURLFetcher::SetByteRange(int start, int end) { void PrivetURLFetcher::Try() { tries_++; if (tries_ < kPrivetMaxRetries) { - std::string token = GetPrivetAccessToken(); - if (token.empty()) - token = kXPrivetEmptyToken; url_fetcher_.reset(net::URLFetcher::Create(url_, request_type_, this)); url_fetcher_->SetRequestContext(request_context_); - url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) + - token); + + if (v3_mode_) { + std::string auth_token = delegate_->GetAuthToken(); + + url_fetcher_->AddExtraRequestHeader( + std::string(kXPrivetAuthTokenHeaderPrefix) + auth_token); + } else { + std::string token = GetPrivetAccessToken(); + + if (token.empty()) + token = kXPrivetEmptyToken; + + url_fetcher_->AddExtraRequestHeader( + std::string(kXPrivetTokenHeaderPrefix) + token); + } + if (has_byte_range_) { url_fetcher_->AddExtraRequestHeader( MakeRangeHeader(byte_range_start_, byte_range_end_)); @@ -177,7 +200,7 @@ void PrivetURLFetcher::Try() { void PrivetURLFetcher::Start() { DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. - if (!send_empty_privet_token_) { + if (!send_empty_privet_token_ && !v3_mode_) { std::string privet_access_token; privet_access_token = GetPrivetAccessToken(); if (privet_access_token.empty()) { diff --git a/chrome/browser/local_discovery/privet_url_fetcher.h b/chrome/browser/local_discovery/privet_url_fetcher.h index 5b2b80c..698ca09 100644 --- a/chrome/browser/local_discovery/privet_url_fetcher.h +++ b/chrome/browser/local_discovery/privet_url_fetcher.h @@ -42,11 +42,15 @@ class PrivetURLFetcher : public net::URLFetcherDelegate { public: virtual ~Delegate() {} - // If you do not implement this method, you will always get a - // TOKEN_ERROR error when your token is invalid. + // If you do not implement this method for PrivetV1 callers, you will always + // get a TOKEN_ERROR error when your token is invalid. virtual void OnNeedPrivetToken( PrivetURLFetcher* fetcher, const TokenCallback& callback); + + // If this returns the empty string, will not send an auth token. + virtual std::string GetAuthToken(); + virtual void OnError(PrivetURLFetcher* fetcher, ErrorType error) = 0; virtual void OnParsedJson(PrivetURLFetcher* fetcher, const base::DictionaryValue& value, @@ -81,6 +85,8 @@ class PrivetURLFetcher : public net::URLFetcherDelegate { void SendEmptyPrivetToken(); + void V3Mode(); + // Set the contents of the Range header. |OnRawData| must return true if this // is called. void SetByteRange(int start, int end); @@ -121,6 +127,7 @@ class PrivetURLFetcher : public net::URLFetcherDelegate { bool send_empty_privet_token_; bool has_byte_range_; bool make_response_file_; + bool v3_mode_; int byte_range_start_; int byte_range_end_; diff --git a/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc b/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc index 1639419d3..45d1edd 100644 --- a/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc +++ b/chrome/browser/local_discovery/privet_url_fetcher_unittest.cc @@ -78,6 +78,8 @@ class MockPrivetURLFetcherDelegate : public PrivetURLFetcher::Delegate { raw_mode_ = raw_mode; } + std::string GetAuthToken() { return "MyAuthToken"; } + private: scoped_ptr<base::DictionaryValue> saved_value_; bool raw_mode_; @@ -296,6 +298,23 @@ TEST_F(PrivetURLFetcherTest, FetcherToFile) { fetcher->delegate()->OnURLFetchComplete(fetcher); } +TEST_F(PrivetURLFetcherTest, V3Mode) { + delegate_.SetRawMode(true); + privet_urlfetcher_->V3Mode(); + privet_urlfetcher_->Start(); + net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); + ASSERT_TRUE(fetcher != NULL); + fetcher->SetResponseFilePath( + base::FilePath(FILE_PATH_LITERAL("sample/file"))); + net::HttpRequestHeaders headers; + fetcher->GetExtraRequestHeaders(&headers); + + std::string header_token; + ASSERT_FALSE(headers.GetHeader("X-Privet-Token", &header_token)); + ASSERT_TRUE(headers.GetHeader("X-Privet-Auth", &header_token)); + ASSERT_EQ("MyAuthToken", header_token); +} + } // namespace } // namespace local_discovery |