2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #ifndef SocketStreamHandle_h
33 #define SocketStreamHandle_h
35 #include "AuthenticationClient.h"
36 #include "SocketStreamHandleBase.h"
37 #include <wtf/RetainPtr.h>
39 typedef struct __CFHTTPMessage* CFHTTPMessageRef;
43 class AuthenticationChallenge;
45 class SocketStreamHandleClient;
47 class SocketStreamHandle : public ThreadSafeRefCounted<SocketStreamHandle>, public SocketStreamHandleBase, public AuthenticationClient {
49 static PassRefPtr<SocketStreamHandle> create(const KURL& url, SocketStreamHandleClient* client) { return adoptRef(new SocketStreamHandle(url, client)); }
51 virtual ~SocketStreamHandle();
53 using ThreadSafeRefCounted<SocketStreamHandle>::ref;
54 using ThreadSafeRefCounted<SocketStreamHandle>::deref;
57 virtual int platformSend(const char* data, int length);
58 virtual void platformClose();
60 SocketStreamHandle(const KURL&, SocketStreamHandleClient*);
62 void scheduleStreams();
64 void chooseProxyFromArray(CFArrayRef);
65 void executePACFileURL(CFURLRef);
66 void removePACRunLoopSource();
67 RetainPtr<CFRunLoopSourceRef> m_pacRunLoopSource;
68 static void pacExecutionCallback(void* client, CFArrayRef proxyList, CFErrorRef error);
69 static void pacExecutionCallbackMainThread(void*);
70 static CFStringRef copyPACExecutionDescription(void*);
72 bool shouldUseSSL() const { return m_url.protocolIs("wss"); }
73 unsigned short port() const;
75 void addCONNECTCredentials(CFHTTPMessageRef response);
77 static void* retainSocketStreamHandle(void*);
78 static void releaseSocketStreamHandle(void*);
79 static CFStringRef copyCFStreamDescription(void*);
80 static void readStreamCallback(CFReadStreamRef, CFStreamEventType, void*);
81 static void writeStreamCallback(CFWriteStreamRef, CFStreamEventType, void*);
83 static void readStreamCallbackMainThread(void*);
84 static void writeStreamCallbackMainThread(void*);
86 void readStreamCallback(CFStreamEventType);
87 void writeStreamCallback(CFStreamEventType);
89 void reportErrorToClient(CFErrorRef);
91 // No authentication for streams per se, but proxy may ask for credentials.
92 virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
93 virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
94 virtual void receivedCancellation(const AuthenticationChallenge&);
96 virtual void refAuthenticationClient() { ref(); }
97 virtual void derefAuthenticationClient() { deref(); }
99 enum ConnectingSubstate { New, ExecutingPACFile, WaitingForCredentials, WaitingForConnect, Connected };
100 ConnectingSubstate m_connectingSubstate;
102 enum ConnectionType { Unknown, Direct, SOCKSProxy, CONNECTProxy };
103 ConnectionType m_connectionType;
104 RetainPtr<CFStringRef> m_proxyHost;
105 RetainPtr<CFNumberRef> m_proxyPort;
107 RetainPtr<CFHTTPMessageRef> m_proxyResponseMessage;
108 bool m_sentStoredCredentials;
109 RetainPtr<CFReadStreamRef> m_readStream;
110 RetainPtr<CFWriteStreamRef> m_writeStream;
112 RetainPtr<CFURLRef> m_httpsURL; // ws(s): replaced with https:
115 } // namespace WebCore
117 #endif // SocketStreamHandle_h