Add Adreno GPU to deep memory profiler Android policies
[chromium-blink-merge.git] / net / http / http_auth_handler_basic.cc
blobaf5f188b156223e5a419b5ec7546c883e673f7fd
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_auth_handler_basic.h"
7 #include <string>
9 #include "base/base64.h"
10 #include "base/i18n/icu_string_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "net/base/net_errors.h"
14 #include "net/http/http_auth.h"
16 namespace net {
18 namespace {
20 // Parses a realm from an auth challenge, and converts to UTF8-encoding.
21 // Returns whether the realm is invalid or the parameters are invalid.
23 // Note that if a realm was not specified, we will default it to "";
24 // so specifying 'Basic realm=""' is equivalent to 'Basic'.
26 // This is more generous than RFC 2617, which is pretty clear in the
27 // production of challenge that realm is required.
29 // We allow it to be compatibility with certain embedded webservers that don't
30 // include a realm (see http://crbug.com/20984.)
32 // The over-the-wire realm is encoded as ISO-8859-1 (aka Latin-1).
34 // TODO(cbentzel): Realm may need to be decoded using RFC 2047 rules as
35 // well, see http://crbug.com/25790.
36 bool ParseRealm(const HttpAuth::ChallengeTokenizer& tokenizer,
37 std::string* realm) {
38 CHECK(realm);
39 realm->clear();
40 HttpUtil::NameValuePairsIterator parameters = tokenizer.param_pairs();
41 while (parameters.GetNext()) {
42 if (!LowerCaseEqualsASCII(parameters.name(), "realm"))
43 continue;
45 if (!base::ConvertToUtf8AndNormalize(
46 parameters.value(), base::kCodepageLatin1, realm))
47 return false;
49 return parameters.valid();
52 } // namespace
54 bool HttpAuthHandlerBasic::Init(HttpAuth::ChallengeTokenizer* challenge) {
55 auth_scheme_ = HttpAuth::AUTH_SCHEME_BASIC;
56 score_ = 1;
57 properties_ = 0;
58 return ParseChallenge(challenge);
61 bool HttpAuthHandlerBasic::ParseChallenge(
62 HttpAuth::ChallengeTokenizer* challenge) {
63 // Verify the challenge's auth-scheme.
64 if (!LowerCaseEqualsASCII(challenge->scheme(), "basic"))
65 return false;
67 std::string realm;
68 if (!ParseRealm(*challenge, &realm))
69 return false;
71 realm_ = realm;
72 return true;
75 HttpAuth::AuthorizationResult HttpAuthHandlerBasic::HandleAnotherChallenge(
76 HttpAuth::ChallengeTokenizer* challenge) {
77 // Basic authentication is always a single round, so any responses
78 // should be treated as a rejection. However, if the new challenge
79 // is for a different realm, then indicate the realm change.
80 std::string realm;
81 if (!ParseRealm(*challenge, &realm))
82 return HttpAuth::AUTHORIZATION_RESULT_INVALID;
83 return (realm_ != realm)?
84 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM:
85 HttpAuth::AUTHORIZATION_RESULT_REJECT;
88 int HttpAuthHandlerBasic::GenerateAuthTokenImpl(
89 const AuthCredentials* credentials, const HttpRequestInfo*,
90 const CompletionCallback&, std::string* auth_token) {
91 DCHECK(credentials);
92 // TODO(eroman): is this the right encoding of username/password?
93 std::string base64_username_password;
94 if (!base::Base64Encode(
95 UTF16ToUTF8(credentials->username()) + ":" +
96 UTF16ToUTF8(credentials->password()),
97 &base64_username_password)) {
98 LOG(ERROR) << "Unexpected problem Base64 encoding.";
99 return ERR_UNEXPECTED;
101 *auth_token = "Basic " + base64_username_password;
102 return OK;
105 HttpAuthHandlerBasic::Factory::Factory() {
108 HttpAuthHandlerBasic::Factory::~Factory() {
111 int HttpAuthHandlerBasic::Factory::CreateAuthHandler(
112 HttpAuth::ChallengeTokenizer* challenge,
113 HttpAuth::Target target,
114 const GURL& origin,
115 CreateReason reason,
116 int digest_nonce_count,
117 const BoundNetLog& net_log,
118 scoped_ptr<HttpAuthHandler>* handler) {
119 // TODO(cbentzel): Move towards model of parsing in the factory
120 // method and only constructing when valid.
121 scoped_ptr<HttpAuthHandler> tmp_handler(new HttpAuthHandlerBasic());
122 if (!tmp_handler->InitFromChallenge(challenge, target, origin, net_log))
123 return ERR_INVALID_RESPONSE;
124 handler->swap(tmp_handler);
125 return OK;
128 } // namespace net