| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/http/http_auth_handler_basic.h" | 11 #include "net/http/http_auth_handler_basic.h" |
| 12 #include "net/http/http_auth_handler_digest.h" | 12 #include "net/http/http_auth_handler_digest.h" |
| 13 #include "net/http/http_auth_handler_ntlm.h" | 13 #include "net/http/http_auth_handler_ntlm.h" |
| 14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/http/http_util.h" | 15 #include "net/http/http_util.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 // static | 19 // static |
| 20 void HttpAuth::ChooseBestChallenge(const HttpResponseHeaders* headers, | 20 void HttpAuth::ChooseBestChallenge(const HttpResponseHeaders* headers, |
| 21 Target target, | 21 Target target, |
| 22 const GURL& origin, |
| 22 scoped_refptr<HttpAuthHandler>* handler) { | 23 scoped_refptr<HttpAuthHandler>* handler) { |
| 23 // A connection-based authentication scheme must continue to use the | 24 // A connection-based authentication scheme must continue to use the |
| 24 // existing handler object in |*handler|. | 25 // existing handler object in |*handler|. |
| 25 if (*handler && (*handler)->is_connection_based()) { | 26 if (*handler && (*handler)->is_connection_based()) { |
| 26 const std::string header_name = GetChallengeHeaderName(target); | 27 const std::string header_name = GetChallengeHeaderName(target); |
| 27 std::string challenge; | 28 std::string challenge; |
| 28 void* iter = NULL; | 29 void* iter = NULL; |
| 29 while (headers->EnumerateHeader(&iter, header_name, &challenge)) { | 30 while (headers->EnumerateHeader(&iter, header_name, &challenge)) { |
| 30 ChallengeTokenizer props(challenge.begin(), challenge.end()); | 31 ChallengeTokenizer props(challenge.begin(), challenge.end()); |
| 31 if (LowerCaseEqualsASCII(props.scheme(), (*handler)->scheme().c_str()) && | 32 if (LowerCaseEqualsASCII(props.scheme(), (*handler)->scheme().c_str()) && |
| 32 (*handler)->InitFromChallenge(challenge.begin(), challenge.end(), | 33 (*handler)->InitFromChallenge(challenge.begin(), challenge.end(), |
| 33 target)) | 34 target, origin)) |
| 34 return; | 35 return; |
| 35 } | 36 } |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Choose the challenge whose authentication handler gives the maximum score. | 39 // Choose the challenge whose authentication handler gives the maximum score. |
| 39 scoped_refptr<HttpAuthHandler> best; | 40 scoped_refptr<HttpAuthHandler> best; |
| 40 const std::string header_name = GetChallengeHeaderName(target); | 41 const std::string header_name = GetChallengeHeaderName(target); |
| 41 std::string cur_challenge; | 42 std::string cur_challenge; |
| 42 void* iter = NULL; | 43 void* iter = NULL; |
| 43 while (headers->EnumerateHeader(&iter, header_name, &cur_challenge)) { | 44 while (headers->EnumerateHeader(&iter, header_name, &cur_challenge)) { |
| 44 scoped_refptr<HttpAuthHandler> cur; | 45 scoped_refptr<HttpAuthHandler> cur; |
| 45 CreateAuthHandler(cur_challenge, target, &cur); | 46 CreateAuthHandler(cur_challenge, target, origin, &cur); |
| 46 if (cur && (!best || best->score() < cur->score())) | 47 if (cur && (!best || best->score() < cur->score())) |
| 47 best.swap(cur); | 48 best.swap(cur); |
| 48 } | 49 } |
| 49 handler->swap(best); | 50 handler->swap(best); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // static | 53 // static |
| 53 void HttpAuth::CreateAuthHandler(const std::string& challenge, | 54 void HttpAuth::CreateAuthHandler(const std::string& challenge, |
| 54 Target target, | 55 Target target, |
| 56 const GURL& origin, |
| 55 scoped_refptr<HttpAuthHandler>* handler) { | 57 scoped_refptr<HttpAuthHandler>* handler) { |
| 56 // Find the right auth handler for the challenge's scheme. | 58 // Find the right auth handler for the challenge's scheme. |
| 57 ChallengeTokenizer props(challenge.begin(), challenge.end()); | 59 ChallengeTokenizer props(challenge.begin(), challenge.end()); |
| 58 if (!props.valid()) { | 60 if (!props.valid()) { |
| 59 *handler = NULL; | 61 *handler = NULL; |
| 60 return; | 62 return; |
| 61 } | 63 } |
| 62 | 64 |
| 63 scoped_refptr<HttpAuthHandler> tmp_handler; | 65 scoped_refptr<HttpAuthHandler> tmp_handler; |
| 64 if (LowerCaseEqualsASCII(props.scheme(), "basic")) { | 66 if (LowerCaseEqualsASCII(props.scheme(), "basic")) { |
| 65 tmp_handler = new HttpAuthHandlerBasic(); | 67 tmp_handler = new HttpAuthHandlerBasic(); |
| 66 } else if (LowerCaseEqualsASCII(props.scheme(), "digest")) { | 68 } else if (LowerCaseEqualsASCII(props.scheme(), "digest")) { |
| 67 tmp_handler = new HttpAuthHandlerDigest(); | 69 tmp_handler = new HttpAuthHandlerDigest(); |
| 68 } else if (LowerCaseEqualsASCII(props.scheme(), "ntlm")) { | 70 } else if (LowerCaseEqualsASCII(props.scheme(), "ntlm")) { |
| 69 tmp_handler = new HttpAuthHandlerNTLM(); | 71 tmp_handler = new HttpAuthHandlerNTLM(); |
| 70 } | 72 } |
| 71 if (tmp_handler) { | 73 if (tmp_handler) { |
| 72 if (!tmp_handler->InitFromChallenge(challenge.begin(), challenge.end(), | 74 if (!tmp_handler->InitFromChallenge(challenge.begin(), challenge.end(), |
| 73 target)) { | 75 target, origin)) { |
| 74 // Invalid/unsupported challenge. | 76 // Invalid/unsupported challenge. |
| 75 tmp_handler = NULL; | 77 tmp_handler = NULL; |
| 76 } | 78 } |
| 77 } | 79 } |
| 78 handler->swap(tmp_handler); | 80 handler->swap(tmp_handler); |
| 79 } | 81 } |
| 80 | 82 |
| 81 void HttpAuth::ChallengeTokenizer::Init(std::string::const_iterator begin, | 83 void HttpAuth::ChallengeTokenizer::Init(std::string::const_iterator begin, |
| 82 std::string::const_iterator end) { | 84 std::string::const_iterator end) { |
| 83 // The first space-separated token is the auth-scheme. | 85 // The first space-separated token is the auth-scheme. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 return "Proxy-Authorization"; | 163 return "Proxy-Authorization"; |
| 162 case AUTH_SERVER: | 164 case AUTH_SERVER: |
| 163 return "Authorization"; | 165 return "Authorization"; |
| 164 default: | 166 default: |
| 165 NOTREACHED(); | 167 NOTREACHED(); |
| 166 return ""; | 168 return ""; |
| 167 } | 169 } |
| 168 } | 170 } |
| 169 | 171 |
| 170 } // namespace net | 172 } // namespace net |
| OLD | NEW |