2 * Copyright (C) 2006, 2007, 2008 Apple, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "ResourceRequest.h"
31 #import "WebCoreSystemInterface.h"
33 #import "FormDataStreamMac.h"
34 #import "ResourceRequestCFNet.h"
35 #import "WebCoreSystemInterface.h"
37 #import <Foundation/Foundation.h>
39 #ifdef BUILDING_ON_TIGER
40 typedef unsigned NSUInteger;
43 @interface NSURLRequest (WebNSURLRequestDetails)
44 - (NSArray *)contentDispositionEncodingFallbackArray;
45 + (void)setDefaultTimeoutInterval:(NSTimeInterval)seconds;
48 @interface NSMutableURLRequest (WebMutableNSURLRequestDetails)
49 - (void)setContentDispositionEncodingFallbackArray:(NSArray *)theEncodingFallbackArray;
54 NSURLRequest* ResourceRequest::nsURLRequest() const
56 updatePlatformRequest();
58 return [[m_nsRequest.get() retain] autorelease];
61 void ResourceRequest::doUpdateResourceRequest()
63 m_url = [m_nsRequest.get() URL];
64 m_cachePolicy = (ResourceRequestCachePolicy)[m_nsRequest.get() cachePolicy];
65 m_timeoutInterval = [m_nsRequest.get() timeoutInterval];
66 m_firstPartyForCookies = [m_nsRequest.get() mainDocumentURL];
68 if (NSString* method = [m_nsRequest.get() HTTPMethod])
69 m_httpMethod = method;
70 m_allowCookies = [m_nsRequest.get() HTTPShouldHandleCookies];
72 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
73 if (isHTTPPipeliningEnabled() && !shouldForceHTTPPipeliningPriorityHigh())
74 m_priority = mapHTTPPipeliningPriorityToResourceLoadPriority(wkGetHTTPPipeliningPriority(m_nsRequest.get()));
77 NSDictionary *headers = [m_nsRequest.get() allHTTPHeaderFields];
78 NSEnumerator *e = [headers keyEnumerator];
80 m_httpHeaderFields.clear();
81 while ((name = [e nextObject]))
82 m_httpHeaderFields.set(name, [headers objectForKey:name]);
84 // The below check can be removed once we require a version of Foundation with -[NSURLRequest contentDispositionEncodingFallbackArray] method.
85 static bool supportsContentDispositionEncodingFallbackArray = [NSURLRequest instancesRespondToSelector:@selector(contentDispositionEncodingFallbackArray)];
86 if (supportsContentDispositionEncodingFallbackArray) {
87 m_responseContentDispositionEncodingFallbackArray.clear();
88 NSArray *encodingFallbacks = [m_nsRequest.get() contentDispositionEncodingFallbackArray];
89 NSUInteger count = [encodingFallbacks count];
90 for (NSUInteger i = 0; i < count; ++i) {
91 CFStringEncoding encoding = CFStringConvertNSStringEncodingToEncoding([(NSNumber *)[encodingFallbacks objectAtIndex:i] unsignedLongValue]);
92 if (encoding != kCFStringEncodingInvalidId)
93 m_responseContentDispositionEncodingFallbackArray.append(CFStringConvertEncodingToIANACharSetName(encoding));
97 if (NSData* bodyData = [m_nsRequest.get() HTTPBody])
98 m_httpBody = FormData::create([bodyData bytes], [bodyData length]);
99 else if (NSInputStream* bodyStream = [m_nsRequest.get() HTTPBodyStream])
100 if (FormData* formData = httpBodyFromStream(bodyStream))
101 m_httpBody = formData;
104 void ResourceRequest::doUpdatePlatformRequest()
111 NSMutableURLRequest* nsRequest = [m_nsRequest.get() mutableCopy];
114 [nsRequest setURL:url()];
116 nsRequest = [[NSMutableURLRequest alloc] initWithURL:url()];
118 #ifdef BUILDING_ON_TIGER
119 wkSupportsMultipartXMixedReplace(nsRequest);
122 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
123 if (isHTTPPipeliningEnabled()) {
124 int priority = mapResourceLoadPriorityToHTTPPipeliningPriority(m_priority);
125 wkSetHTTPPipeliningPriority(nsRequest, shouldForceHTTPPipeliningPriorityHigh() ? 2 : priority);
129 [nsRequest setCachePolicy:(NSURLRequestCachePolicy)cachePolicy()];
131 double timeoutInterval = ResourceRequestBase::timeoutInterval();
133 [nsRequest setTimeoutInterval:timeoutInterval];
134 // Otherwise, respect NSURLRequest default timeout.
136 [nsRequest setMainDocumentURL:firstPartyForCookies()];
137 if (!httpMethod().isEmpty())
138 [nsRequest setHTTPMethod:httpMethod()];
139 [nsRequest setHTTPShouldHandleCookies:allowCookies()];
141 // Cannot just use setAllHTTPHeaderFields here, because it does not remove headers.
142 NSArray *oldHeaderFieldNames = [[nsRequest allHTTPHeaderFields] allKeys];
143 for (unsigned i = [oldHeaderFieldNames count]; i != 0; --i)
144 [nsRequest setValue:nil forHTTPHeaderField:[oldHeaderFieldNames objectAtIndex:i - 1]];
145 HTTPHeaderMap::const_iterator end = httpHeaderFields().end();
146 for (HTTPHeaderMap::const_iterator it = httpHeaderFields().begin(); it != end; ++it)
147 [nsRequest setValue:it->second forHTTPHeaderField:it->first];
149 // The below check can be removed once we require a version of Foundation with -[NSMutableURLRequest setContentDispositionEncodingFallbackArray:] method.
150 static bool supportsContentDispositionEncodingFallbackArray = [NSMutableURLRequest instancesRespondToSelector:@selector(setContentDispositionEncodingFallbackArray:)];
151 if (supportsContentDispositionEncodingFallbackArray) {
152 NSMutableArray *encodingFallbacks = [NSMutableArray array];
153 unsigned count = m_responseContentDispositionEncodingFallbackArray.size();
154 for (unsigned i = 0; i != count; ++i) {
155 CFStringRef encodingName = m_responseContentDispositionEncodingFallbackArray[i].createCFString();
156 unsigned long nsEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName));
157 CFRelease(encodingName);
158 if (nsEncoding != kCFStringEncodingInvalidId)
159 [encodingFallbacks addObject:[NSNumber numberWithUnsignedLong:nsEncoding]];
161 [nsRequest setContentDispositionEncodingFallbackArray:encodingFallbacks];
164 RefPtr<FormData> formData = httpBody();
165 if (formData && !formData->isEmpty())
166 WebCore::setHTTPBody(nsRequest, formData);
168 m_nsRequest.adoptNS(nsRequest);
171 void ResourceRequest::applyWebArchiveHackForMail()
173 // Hack because Mail checks for this property to detect data / archive loads
174 [NSURLProtocol setProperty:@"" forKey:@"WebDataRequest" inRequest:(NSMutableURLRequest *)nsURLRequest()];
177 #if USE(CFURLSTORAGESESSIONS)
179 void ResourceRequest::setStorageSession(CFURLStorageSessionRef storageSession)
181 m_nsRequest = wkCopyRequestWithStorageSession(storageSession, m_nsRequest.get());
186 } // namespace WebCore
188 #endif // !USE(CFNETWORK)