1/*
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef SocketStreamHandle_h
33#define SocketStreamHandle_h
34
35#include "AuthenticationClient.h"
36#include "SocketStreamHandleBase.h"
37#include <wtf/RetainPtr.h>
38
39typedef struct __CFHTTPMessage* CFHTTPMessageRef;
40
41namespace WebCore {
42
43class AuthenticationChallenge;
44class Credential;
45class SocketStreamHandleClient;
46
47class SocketStreamHandle : public ThreadSafeRefCounted<SocketStreamHandle>, public SocketStreamHandleBase, public AuthenticationClient {
48public:
49    static PassRefPtr<SocketStreamHandle> create(const KURL& url, SocketStreamHandleClient* client) { return adoptRef(new SocketStreamHandle(url, client)); }
50
51    virtual ~SocketStreamHandle();
52
53    using ThreadSafeRefCounted<SocketStreamHandle>::ref;
54    using ThreadSafeRefCounted<SocketStreamHandle>::deref;
55
56private:
57    virtual int platformSend(const char* data, int length);
58    virtual void platformClose();
59
60    SocketStreamHandle(const KURL&, SocketStreamHandleClient*);
61    void createStreams();
62    void scheduleStreams();
63    void chooseProxy();
64#ifndef BUILDING_ON_TIGER
65    void chooseProxyFromArray(CFArrayRef);
66    void executePACFileURL(CFURLRef);
67    void removePACRunLoopSource();
68    RetainPtr<CFRunLoopSourceRef> m_pacRunLoopSource;
69    static void pacExecutionCallback(void* client, CFArrayRef proxyList, CFErrorRef error);
70    static void pacExecutionCallbackMainThread(void*);
71    static CFStringRef copyPACExecutionDescription(void*);
72#endif
73
74    bool shouldUseSSL() const { return m_url.protocolIs("wss"); }
75    unsigned short port() const;
76
77    void addCONNECTCredentials(CFHTTPMessageRef response);
78
79    static CFStringRef copyCFStreamDescription(void* );
80    static void readStreamCallback(CFReadStreamRef, CFStreamEventType, void*);
81    static void writeStreamCallback(CFWriteStreamRef, CFStreamEventType, void*);
82#if PLATFORM(WIN)
83    static void readStreamCallbackMainThread(void*);
84    static void writeStreamCallbackMainThread(void*);
85#endif
86    void readStreamCallback(CFStreamEventType);
87    void writeStreamCallback(CFStreamEventType);
88
89#ifndef BUILDING_ON_TIGER
90    void reportErrorToClient(CFErrorRef);
91#endif
92
93    // No authentication for streams per se, but proxy may ask for credentials.
94    virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
95    virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
96    virtual void receivedCancellation(const AuthenticationChallenge&);
97
98    virtual void refAuthenticationClient() { ref(); }
99    virtual void derefAuthenticationClient() { deref(); }
100
101    enum ConnectingSubstate { New, ExecutingPACFile, WaitingForCredentials, WaitingForConnect, Connected };
102    ConnectingSubstate m_connectingSubstate;
103
104    enum ConnectionType { Unknown, Direct, SOCKSProxy, CONNECTProxy };
105    ConnectionType m_connectionType;
106    RetainPtr<CFStringRef> m_proxyHost;
107    RetainPtr<CFNumberRef> m_proxyPort;
108
109    RetainPtr<CFHTTPMessageRef> m_proxyResponseMessage;
110    bool m_sentStoredCredentials;
111    RetainPtr<CFReadStreamRef> m_readStream;
112    RetainPtr<CFWriteStreamRef> m_writeStream;
113
114    RetainPtr<CFURLRef> m_httpsURL; // ws(s): replaced with https:
115};
116
117}  // namespace WebCore
118
119#endif  // SocketStreamHandle_h
120