1/*
2 * Copyright (C) 2004, 2006 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
26#pragma once
27
28#include "AuthenticationChallenge.h"
29#include "NetworkingContext.h"
30#include "ResourceHandle.h"
31#include "ResourceHandleClient.h"
32#include "ResourceRequest.h"
33#include "Timer.h"
34
35#if USE(CFURLCONNECTION)
36#include "ResourceHandleCFURLConnectionDelegate.h"
37#include <pal/spi/cf/CFNetworkSPI.h>
38#endif
39
40#if USE(CURL)
41#include "CurlRequest.h"
42#include <wtf/MessageQueue.h>
43#include <wtf/MonotonicTime.h>
44#endif
45
46#if PLATFORM(COCOA)
47OBJC_CLASS NSURLAuthenticationChallenge;
48OBJC_CLASS NSURLConnection;
49#endif
50
51#if PLATFORM(COCOA) || USE(CFURLCONNECTION)
52typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
53#endif
54
55// The allocations and releases in ResourceHandleInternal are
56// Cocoa-exception-free (either simple Foundation classes or
57// WebCoreResourceLoaderImp which avoids doing work in dealloc).
58
59namespace WebCore {
60
61class ResourceHandleInternal {
62 WTF_MAKE_NONCOPYABLE(ResourceHandleInternal); WTF_MAKE_FAST_ALLOCATED;
63public:
64 ResourceHandleInternal(ResourceHandle* loader, NetworkingContext* context, const ResourceRequest& request, ResourceHandleClient* client, bool defersLoading, bool shouldContentSniff, bool shouldContentEncodingSniff)
65 : m_context(context)
66 , m_client(client)
67 , m_firstRequest(request)
68 , m_lastHTTPMethod(request.httpMethod())
69 , m_partition(request.cachePartition())
70 , m_defersLoading(defersLoading)
71 , m_shouldContentSniff(shouldContentSniff)
72 , m_shouldContentEncodingSniff(shouldContentEncodingSniff)
73#if USE(CFURLCONNECTION)
74 , m_currentRequest(request)
75#endif
76 , m_failureTimer(*loader, &ResourceHandle::failureTimerFired)
77 {
78 const URL& url = m_firstRequest.url();
79 m_user = url.user();
80 m_pass = url.pass();
81 m_firstRequest.removeCredentials();
82 }
83
84 ~ResourceHandleInternal();
85
86 ResourceHandleClient* client() { return m_client; }
87
88 RefPtr<NetworkingContext> m_context;
89 ResourceHandleClient* m_client;
90 ResourceRequest m_firstRequest;
91 String m_lastHTTPMethod;
92 String m_partition;
93
94 // Suggested credentials for the current redirection step.
95 String m_user;
96 String m_pass;
97
98 Credential m_initialCredential;
99
100 int status { 0 };
101
102 bool m_defersLoading;
103 bool m_shouldContentSniff;
104 bool m_shouldContentEncodingSniff;
105#if USE(CFURLCONNECTION)
106 RetainPtr<CFURLConnectionRef> m_connection;
107 ResourceRequest m_currentRequest;
108 RefPtr<ResourceHandleCFURLConnectionDelegate> m_connectionDelegate;
109#endif
110#if PLATFORM(COCOA)
111 RetainPtr<NSURLConnection> m_connection;
112 RetainPtr<id> m_delegate;
113#endif
114#if PLATFORM(COCOA)
115 bool m_startWhenScheduled { false };
116#endif
117#if PLATFORM(COCOA) || USE(CFURLCONNECTION)
118 RetainPtr<CFURLStorageSessionRef> m_storageSession;
119#endif
120#if USE(CURL)
121 std::unique_ptr<CurlResourceHandleDelegate> m_delegate;
122
123 bool m_cancelled { false };
124 unsigned m_redirectCount { 0 };
125 unsigned m_authFailureCount { 0 };
126 bool m_addedCacheValidationHeaders { false };
127 RefPtr<CurlRequest> m_curlRequest;
128 MessageQueue<WTF::Function<void()>>* m_messageQueue { };
129 MonotonicTime m_startTime;
130#endif
131
132#if PLATFORM(COCOA)
133 // We need to keep a reference to the original challenge to be able to cancel it.
134 // It is almost identical to m_currentWebChallenge.nsURLAuthenticationChallenge(), but has a different sender.
135 NSURLAuthenticationChallenge *m_currentMacChallenge { nil };
136#endif
137
138 AuthenticationChallenge m_currentWebChallenge;
139 ResourceHandle::FailureType m_scheduledFailureType { ResourceHandle::NoFailure };
140 Timer m_failureTimer;
141};
142
143} // namespace WebCore
144