2011-02-23 Patrick Gansterer <paroga@webkit.org>
[WebKit-https.git] / Source / WebCore / platform / network / ResourceHandle.h
1 /*
2  * Copyright (C) 2004, 2006 Apple Computer, 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
26 #ifndef ResourceHandle_h
27 #define ResourceHandle_h
28
29 #include "AuthenticationChallenge.h"
30 #include "AuthenticationClient.h"
31 #include "HTTPHeaderMap.h"
32 #include "NetworkingContext.h"
33 #include "ThreadableLoader.h"
34 #include <wtf/OwnPtr.h>
35
36 #if USE(SOUP)
37 typedef struct _SoupSession SoupSession;
38 #endif
39
40 #if USE(CF)
41 typedef const struct __CFData * CFDataRef;
42 #endif
43
44 #if USE(WININET)
45 typedef unsigned long DWORD;
46 typedef unsigned long DWORD_PTR;
47 typedef void* LPVOID;
48 typedef LPVOID HINTERNET;
49 #endif
50
51
52 #if PLATFORM(MAC)
53 #include <wtf/RetainPtr.h>
54 #ifdef __OBJC__
55 @class NSData;
56 @class NSError;
57 @class NSURLConnection;
58 @class WebCoreResourceHandleAsDelegate;
59 #else
60 class NSData;
61 class NSError;
62 class NSURLConnection;
63 class WebCoreResourceHandleAsDelegate;
64 typedef struct objc_object *id;
65 #endif
66 #endif
67
68 #if USE(CFNETWORK)
69 typedef struct _CFURLConnection* CFURLConnectionRef;
70 typedef int CFHTTPCookieStorageAcceptPolicy;
71 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
72 #endif
73
74 namespace WebCore {
75
76 class AuthenticationChallenge;
77 class Credential;
78 class FormData;
79 class Frame;
80 class KURL;
81 class ProtectionSpace;
82 class ResourceError;
83 class ResourceHandleClient;
84 class ResourceHandleInternal;
85 class ResourceRequest;
86 class ResourceResponse;
87 class SchedulePair;
88 class SharedBuffer;
89
90 template <typename T> class Timer;
91
92 class ResourceHandle : public RefCounted<ResourceHandle>
93 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
94     , public AuthenticationClient
95 #endif
96     {
97 public:
98     static PassRefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
99     static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
100
101     static void prepareForURL(const KURL&);
102     static bool willLoadFromCache(ResourceRequest&, Frame*);
103     static void cacheMetadata(const ResourceResponse&, const Vector<char>&);
104 #if PLATFORM(MAC)
105     static bool didSendBodyDataDelegateExists();
106 #endif
107
108     virtual ~ResourceHandle();
109
110 #if PLATFORM(MAC) || USE(CFNETWORK)
111     void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
112     bool shouldUseCredentialStorage();
113 #endif
114 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
115     void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
116     virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
117     virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
118     virtual void receivedCancellation(const AuthenticationChallenge&);
119 #endif
120
121 #if PLATFORM(MAC)
122     void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
123 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
124     bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
125 #endif
126     NSURLConnection *connection() const;
127     WebCoreResourceHandleAsDelegate *delegate();
128     void releaseDelegate();
129     id releaseProxy();
130
131     void schedule(SchedulePair*);
132     void unschedule(SchedulePair*);
133 #elif USE(CFNETWORK)
134     CFURLConnectionRef connection() const;
135     CFURLConnectionRef releaseConnectionForDownload();
136     static void setHostAllowsAnyHTTPSCertificate(const String&);
137     static void setClientCertificate(const String& host, CFDataRef);
138 #endif
139
140 #if PLATFORM(WIN) && USE(CURL)
141     static void setHostAllowsAnyHTTPSCertificate(const String&);
142 #endif
143 #if PLATFORM(WIN) && USE(CURL) && USE(CF)
144     static void setClientCertificate(const String& host, CFDataRef);
145 #endif
146
147     PassRefPtr<SharedBuffer> bufferedData();
148     static bool supportsBufferedData();
149
150     bool shouldContentSniff() const;
151     static bool shouldContentSniffURL(const KURL&);
152
153     static void forceContentSniffing();
154
155 #if USE(WININET)
156     void setSynchronousInternetHandle(HINTERNET);
157     void fileLoadTimer(Timer<ResourceHandle>*);
158     void onRedirect();
159     bool onRequestComplete();
160     static void CALLBACK internetStatusCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
161 #endif
162
163 #if PLATFORM(QT) || USE(CURL) || USE(SOUP) || PLATFORM(ANDROID)
164     ResourceHandleInternal* getInternal() { return d.get(); }
165 #endif
166
167 #if USE(SOUP)
168     static SoupSession* defaultSession();
169 #endif
170
171     // Used to work around the fact that you don't get any more NSURLConnection callbacks until you return from the one you're in.
172     static bool loadsBlocked();    
173
174     bool hasAuthenticationChallenge() const;
175     void clearAuthentication();
176     virtual void cancel();
177
178     // The client may be 0, in which case no callbacks will be made.
179     ResourceHandleClient* client() const;
180     void setClient(ResourceHandleClient*);
181
182     void setDefersLoading(bool);
183       
184     ResourceRequest& firstRequest();
185     const String& lastHTTPMethod() const;
186
187     void fireFailure(Timer<ResourceHandle>*);
188
189     using RefCounted<ResourceHandle>::ref;
190     using RefCounted<ResourceHandle>::deref;
191
192 protected:
193     ResourceHandle(const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
194
195 private:
196     enum FailureType {
197         NoFailure,
198         BlockedFailure,
199         InvalidURLFailure
200     };
201
202     void platformSetDefersLoading(bool);
203
204     void scheduleFailure(FailureType);
205
206     bool start(NetworkingContext*);
207
208     virtual void refAuthenticationClient() { ref(); }
209     virtual void derefAuthenticationClient() { deref(); }
210
211 #if PLATFORM(MAC)
212     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff);
213 #elif USE(CF)
214     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff);
215 #endif
216
217     friend class ResourceHandleInternal;
218     OwnPtr<ResourceHandleInternal> d;
219 };
220
221 }
222
223 #endif // ResourceHandle_h