| 1 | /* |
| 2 | * Copyright (C) 2007 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | #include "config.h" |
| 26 | #include "ProtectionSpaceBase.h" |
| 27 | |
| 28 | #include "ProtectionSpace.h" |
| 29 | |
| 30 | #if USE(CFURLCONNECTION) |
| 31 | #include "AuthenticationCF.h" |
| 32 | #include <CFNetwork/CFURLProtectionSpacePriv.h> |
| 33 | #endif |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | // Need to enforce empty, non-null strings due to the pickiness of the String == String operator |
| 38 | // combined with the semantics of the String(NSString*) constructor |
| 39 | ProtectionSpaceBase::ProtectionSpaceBase() |
| 40 | : m_host(emptyString()) |
| 41 | , m_port(0) |
| 42 | , m_serverType(ProtectionSpaceServerHTTP) |
| 43 | , m_realm(emptyString()) |
| 44 | , m_authenticationScheme(ProtectionSpaceAuthenticationSchemeDefault) |
| 45 | , m_isHashTableDeletedValue(false) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | // Need to enforce empty, non-null strings due to the pickiness of the String == String operator |
| 50 | // combined with the semantics of the String(NSString*) constructor |
| 51 | ProtectionSpaceBase::ProtectionSpaceBase(const String& host, int port, ProtectionSpaceServerType serverType, const String& realm, ProtectionSpaceAuthenticationScheme authenticationScheme) |
| 52 | : m_host(host.length() ? host : emptyString()) |
| 53 | , m_port(port) |
| 54 | , m_serverType(serverType) |
| 55 | , m_realm(realm.length() ? realm : emptyString()) |
| 56 | , m_authenticationScheme(authenticationScheme) |
| 57 | , m_isHashTableDeletedValue(false) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | const String& ProtectionSpaceBase::host() const |
| 62 | { |
| 63 | return m_host; |
| 64 | } |
| 65 | |
| 66 | int ProtectionSpaceBase::port() const |
| 67 | { |
| 68 | return m_port; |
| 69 | } |
| 70 | |
| 71 | ProtectionSpaceServerType ProtectionSpaceBase::serverType() const |
| 72 | { |
| 73 | return m_serverType; |
| 74 | } |
| 75 | |
| 76 | bool ProtectionSpaceBase::isProxy() const |
| 77 | { |
| 78 | return (m_serverType == ProtectionSpaceProxyHTTP || |
| 79 | m_serverType == ProtectionSpaceProxyHTTPS || |
| 80 | m_serverType == ProtectionSpaceProxyFTP || |
| 81 | m_serverType == ProtectionSpaceProxySOCKS); |
| 82 | } |
| 83 | |
| 84 | const String& ProtectionSpaceBase::realm() const |
| 85 | { |
| 86 | return m_realm; |
| 87 | } |
| 88 | |
| 89 | ProtectionSpaceAuthenticationScheme ProtectionSpaceBase::authenticationScheme() const |
| 90 | { |
| 91 | return m_authenticationScheme; |
| 92 | } |
| 93 | |
| 94 | bool ProtectionSpaceBase::receivesCredentialSecurely() const |
| 95 | { |
| 96 | return (m_serverType == ProtectionSpaceServerHTTPS || |
| 97 | m_serverType == ProtectionSpaceServerFTPS || |
| 98 | m_serverType == ProtectionSpaceProxyHTTPS || |
| 99 | m_authenticationScheme == ProtectionSpaceAuthenticationSchemeHTTPDigest); |
| 100 | } |
| 101 | |
| 102 | bool ProtectionSpaceBase::isPasswordBased() const |
| 103 | { |
| 104 | switch (m_authenticationScheme) { |
| 105 | case ProtectionSpaceAuthenticationSchemeDefault: |
| 106 | case ProtectionSpaceAuthenticationSchemeHTTPBasic: |
| 107 | case ProtectionSpaceAuthenticationSchemeHTTPDigest: |
| 108 | case ProtectionSpaceAuthenticationSchemeHTMLForm: |
| 109 | case ProtectionSpaceAuthenticationSchemeNTLM: |
| 110 | case ProtectionSpaceAuthenticationSchemeNegotiate: |
| 111 | case ProtectionSpaceAuthenticationSchemeOAuth: |
| 112 | return true; |
| 113 | case ProtectionSpaceAuthenticationSchemeClientCertificateRequested: |
| 114 | case ProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested: |
| 115 | case ProtectionSpaceAuthenticationSchemeUnknown: |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool ProtectionSpaceBase::compare(const ProtectionSpace& a, const ProtectionSpace& b) |
| 123 | { |
| 124 | if (a.host() != b.host()) |
| 125 | return false; |
| 126 | if (a.port() != b.port()) |
| 127 | return false; |
| 128 | if (a.serverType() != b.serverType()) |
| 129 | return false; |
| 130 | // Ignore realm for proxies |
| 131 | if (!a.isProxy() && a.realm() != b.realm()) |
| 132 | return false; |
| 133 | if (a.authenticationScheme() != b.authenticationScheme()) |
| 134 | return false; |
| 135 | |
| 136 | return ProtectionSpace::platformCompare(a, b); |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |