| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_auth.h" | 5 #include "net/http/http_auth.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/http/http_auth_handler.h" | 12 #include "net/http/http_auth_handler.h" |
| 13 #include "net/http/http_auth_handler_factory.h" | 13 #include "net/http/http_auth_handler_factory.h" |
| 14 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 15 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 16 #include "net/http/http_util.h" | 16 #include "net/http/http_util.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 HttpAuth::Identity::Identity() : source(IDENT_SRC_NONE), invalid(true) {} | 20 HttpAuth::Identity::Identity() : source(IDENT_SRC_NONE), invalid(true) {} |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 void HttpAuth::ChooseBestChallenge( | 23 void HttpAuth::ChooseBestChallenge( |
| 24 HttpAuthHandlerFactory* http_auth_handler_factory, | 24 HttpAuthHandlerFactory* http_auth_handler_factory, |
| 25 const HttpResponseHeaders* headers, | 25 const HttpResponseHeaders* headers, |
| 26 Target target, | 26 Target target, |
| 27 const GURL& origin, | 27 const GURL& origin, |
| 28 const std::set<Scheme>& disabled_schemes, | 28 const std::set<std::string>& disabled_schemes, |
| 29 const BoundNetLog& net_log, | 29 const BoundNetLog& net_log, |
| 30 scoped_ptr<HttpAuthHandler>* handler) { | 30 scoped_ptr<HttpAuthHandler>* handler) { |
| 31 DCHECK(http_auth_handler_factory); | 31 DCHECK(http_auth_handler_factory); |
| 32 DCHECK(handler->get() == NULL); | 32 DCHECK(handler->get() == NULL); |
| 33 | 33 |
| 34 // Choose the challenge whose authentication handler gives the maximum score. | 34 // Choose the challenge whose authentication handler gives the maximum score. |
| 35 scoped_ptr<HttpAuthHandler> best; | 35 scoped_ptr<HttpAuthHandler> best; |
| 36 const std::string header_name = GetChallengeHeaderName(target); | 36 const std::string header_name = GetChallengeHeaderName(target); |
| 37 std::string cur_challenge; | 37 std::string cur_challenge; |
| 38 void* iter = NULL; | 38 void* iter = NULL; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 best.swap(cur); | 50 best.swap(cur); |
| 51 } | 51 } |
| 52 handler->swap(best); | 52 handler->swap(best); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // static | 55 // static |
| 56 HttpAuth::AuthorizationResult HttpAuth::HandleChallengeResponse( | 56 HttpAuth::AuthorizationResult HttpAuth::HandleChallengeResponse( |
| 57 HttpAuthHandler* handler, | 57 HttpAuthHandler* handler, |
| 58 const HttpResponseHeaders* headers, | 58 const HttpResponseHeaders* headers, |
| 59 Target target, | 59 Target target, |
| 60 const std::set<Scheme>& disabled_schemes, | 60 const std::set<std::string>& disabled_schemes, |
| 61 std::string* challenge_used) { | 61 std::string* challenge_used) { |
| 62 DCHECK(handler); | 62 DCHECK(handler); |
| 63 DCHECK(headers); | 63 DCHECK(headers); |
| 64 DCHECK(challenge_used); | 64 DCHECK(challenge_used); |
| 65 challenge_used->clear(); | 65 challenge_used->clear(); |
| 66 HttpAuth::Scheme current_scheme = handler->auth_scheme(); | 66 std::string current_scheme = handler->auth_scheme(); |
| 67 if (disabled_schemes.find(current_scheme) != disabled_schemes.end()) | 67 if (disabled_schemes.find(current_scheme) != disabled_schemes.end()) |
| 68 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 68 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 69 std::string current_scheme_name = SchemeToString(current_scheme); | |
| 70 const std::string header_name = GetChallengeHeaderName(target); | 69 const std::string header_name = GetChallengeHeaderName(target); |
| 71 void* iter = NULL; | 70 void* iter = NULL; |
| 72 std::string challenge; | 71 std::string challenge; |
| 73 HttpAuth::AuthorizationResult authorization_result = | 72 HttpAuth::AuthorizationResult authorization_result = |
| 74 HttpAuth::AUTHORIZATION_RESULT_INVALID; | 73 HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 75 while (headers->EnumerateHeader(&iter, header_name, &challenge)) { | 74 while (headers->EnumerateHeader(&iter, header_name, &challenge)) { |
| 76 HttpAuth::ChallengeTokenizer props(challenge.begin(), challenge.end()); | 75 HttpAuth::ChallengeTokenizer props(challenge.begin(), challenge.end()); |
| 77 if (!LowerCaseEqualsASCII(props.scheme(), current_scheme_name.c_str())) | 76 if (!LowerCaseEqualsASCII(props.scheme(), current_scheme.c_str())) |
| 78 continue; | 77 continue; |
| 79 authorization_result = handler->HandleAnotherChallenge(&props); | 78 authorization_result = handler->HandleAnotherChallenge(&props); |
| 80 if (authorization_result != HttpAuth::AUTHORIZATION_RESULT_INVALID) { | 79 if (authorization_result != HttpAuth::AUTHORIZATION_RESULT_INVALID) { |
| 81 *challenge_used = challenge; | 80 *challenge_used = challenge; |
| 82 return authorization_result; | 81 return authorization_result; |
| 83 } | 82 } |
| 84 } | 83 } |
| 85 // Finding no matches is equivalent to rejection. | 84 // Finding no matches is equivalent to rejection. |
| 86 return HttpAuth::AUTHORIZATION_RESULT_REJECT; | 85 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 87 } | 86 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 case AUTH_PROXY: | 167 case AUTH_PROXY: |
| 169 return "proxy"; | 168 return "proxy"; |
| 170 case AUTH_SERVER: | 169 case AUTH_SERVER: |
| 171 return "server"; | 170 return "server"; |
| 172 default: | 171 default: |
| 173 NOTREACHED(); | 172 NOTREACHED(); |
| 174 return ""; | 173 return ""; |
| 175 } | 174 } |
| 176 } | 175 } |
| 177 | 176 |
| 178 // static | |
| 179 const char* HttpAuth::SchemeToString(Scheme scheme) { | |
| 180 static const char* const kSchemeNames[] = { | |
| 181 "basic", | |
| 182 "digest", | |
| 183 "ntlm", | |
| 184 "negotiate", | |
| 185 "mock", | |
| 186 }; | |
| 187 COMPILE_ASSERT(arraysize(kSchemeNames) == AUTH_SCHEME_MAX, | |
| 188 http_auth_scheme_names_incorrect_size); | |
| 189 if (scheme < AUTH_SCHEME_BASIC || scheme >= AUTH_SCHEME_MAX) { | |
| 190 NOTREACHED(); | |
| 191 return "invalid_scheme"; | |
| 192 } | |
| 193 return kSchemeNames[scheme]; | |
| 194 } | |
| 195 | |
| 196 } // namespace net | 177 } // namespace net |
| OLD | NEW |