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 COMPUTER, 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 COMPUTER, 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 "ProtectionSpace.h"
27
28#if USE(CFNETWORK) && !PLATFORM(MAC)
29#include "AuthenticationCF.h"
30#include <CFNetwork/CFURLProtectionSpacePriv.h>
31#include <wtf/RetainPtr.h>
32#endif
33
34namespace WebCore {
35
36// Need to enforce empty, non-null strings due to the pickiness of the String == String operator
37// combined with the semantics of the String(NSString*) constructor
38ProtectionSpace::ProtectionSpace()
39    : m_host("")
40    , m_port(0)
41    , m_serverType(ProtectionSpaceServerHTTP)
42    , m_realm("")
43    , m_authenticationScheme(ProtectionSpaceAuthenticationSchemeDefault)
44    , m_isHashTableDeletedValue(false)
45{
46}
47
48// Need to enforce empty, non-null strings due to the pickiness of the String == String operator
49// combined with the semantics of the String(NSString*) constructor
50ProtectionSpace::ProtectionSpace(const String& host, int port, ProtectionSpaceServerType serverType, const String& realm, ProtectionSpaceAuthenticationScheme authenticationScheme)
51    : m_host(host.length() ? host : "")
52    , m_port(port)
53    , m_serverType(serverType)
54    , m_realm(realm.length() ? realm : "")
55    , m_authenticationScheme(authenticationScheme)
56    , m_isHashTableDeletedValue(false)
57{
58}
59
60const String& ProtectionSpace::host() const
61{
62    return m_host;
63}
64
65int ProtectionSpace::port() const
66{
67    return m_port;
68}
69
70ProtectionSpaceServerType ProtectionSpace::serverType() const
71{
72    return m_serverType;
73}
74
75bool ProtectionSpace::isProxy() const
76{
77    return (m_serverType == ProtectionSpaceProxyHTTP ||
78            m_serverType == ProtectionSpaceProxyHTTPS ||
79            m_serverType == ProtectionSpaceProxyFTP ||
80            m_serverType == ProtectionSpaceProxySOCKS);
81}
82
83const String& ProtectionSpace::realm() const
84{
85    return m_realm;
86}
87
88ProtectionSpaceAuthenticationScheme ProtectionSpace::authenticationScheme() const
89{
90    return m_authenticationScheme;
91}
92
93bool ProtectionSpace::receivesCredentialSecurely() const
94{
95#if USE(CFNETWORK) && !PLATFORM(MAC)
96    RetainPtr<CFURLProtectionSpaceRef> cfSpace = adoptCF(createCF(*this));
97    return cfSpace && CFURLProtectionSpaceReceivesCredentialSecurely(cfSpace.get());
98#else
99    return (m_serverType == ProtectionSpaceServerHTTPS ||
100            m_serverType == ProtectionSpaceServerFTPS ||
101            m_serverType == ProtectionSpaceProxyHTTPS ||
102            m_authenticationScheme == ProtectionSpaceAuthenticationSchemeHTTPDigest);
103#endif
104}
105
106bool operator==(const ProtectionSpace& a, const ProtectionSpace& b)
107{
108    if (a.host() != b.host())
109        return false;
110    if (a.port() != b.port())
111        return false;
112    if (a.serverType() != b.serverType())
113        return false;
114    // Ignore realm for proxies
115    if (!a.isProxy() && a.realm() != b.realm())
116        return false;
117    if (a.authenticationScheme() != b.authenticationScheme())
118        return false;
119
120    return true;
121}
122
123}
124
125
126