1 /* 2 * Copyright (C) 2006, 2007, 2008 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 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 #import "config.h" 27 #import "ResourceRequest.h" 28 #import "WebCoreSystemInterface.h" 29 30 #import "FormDataStreamMac.h" 31 32 #import <Foundation/Foundation.h> 33 34 #ifdef BUILDING_ON_TIGER 35 typedef unsigned NSUInteger; 36 #endif 37 38 @interface NSURLRequest (WebCoreContentDispositionEncoding) 39 - (NSArray *)contentDispositionEncodingFallbackArray; 40 @end 41 42 @interface NSMutableURLRequest (WebCoreContentDispositionEncoding) 43 - (void)setContentDispositionEncodingFallbackArray:(NSArray *)theEncodingFallbackArray; 44 @end 45 46 namespace WebCore { 47 48 NSURLRequest* ResourceRequest::nsURLRequest() const 49 { 50 updatePlatformRequest(); 51 52 return [[m_nsRequest.get() retain] autorelease]; 53 } 54 55 void ResourceRequest::doUpdateResourceRequest() 56 { 57 m_url = [m_nsRequest.get() URL]; 58 m_cachePolicy = (ResourceRequestCachePolicy)[m_nsRequest.get() cachePolicy]; 59 m_timeoutInterval = [m_nsRequest.get() timeoutInterval]; 60 m_firstPartyForCookies = [m_nsRequest.get() mainDocumentURL]; 61 62 if (NSString* method = [m_nsRequest.get() HTTPMethod]) 63 m_httpMethod = method; 64 m_allowCookies = [m_nsRequest.get() HTTPShouldHandleCookies]; 65 66 NSDictionary *headers = [m_nsRequest.get() allHTTPHeaderFields]; 67 NSEnumerator *e = [headers keyEnumerator]; 68 NSString *name; 69 m_httpHeaderFields.clear(); 70 while ((name = [e nextObject])) 71 m_httpHeaderFields.set(name, [headers objectForKey:name]); 72 73 // The below check can be removed once we require a version of Foundation with -[NSURLRequest contentDispositionEncodingFallbackArray] method. 74 static bool supportsContentDispositionEncodingFallbackArray = [NSURLRequest instancesRespondToSelector:@selector(contentDispositionEncodingFallbackArray)]; 75 if (supportsContentDispositionEncodingFallbackArray) { 76 m_responseContentDispositionEncodingFallbackArray.clear(); 77 NSArray *encodingFallbacks = [m_nsRequest.get() contentDispositionEncodingFallbackArray]; 78 NSUInteger count = [encodingFallbacks count]; 79 for (NSUInteger i = 0; i < count; ++i) { 80 CFStringEncoding encoding = CFStringConvertNSStringEncodingToEncoding([(NSNumber *)[encodingFallbacks objectAtIndex:i] unsignedLongValue]); 81 if (encoding != kCFStringEncodingInvalidId) 82 m_responseContentDispositionEncodingFallbackArray.append(CFStringConvertEncodingToIANACharSetName(encoding)); 83 } 84 } 85 86 if (NSData* bodyData = [m_nsRequest.get() HTTPBody]) 87 m_httpBody = FormData::create([bodyData bytes], [bodyData length]); 88 else if (NSInputStream* bodyStream = [m_nsRequest.get() HTTPBodyStream]) 89 if (FormData* formData = httpBodyFromStream(bodyStream)) 90 m_httpBody = formData; 91 } 92 93 void ResourceRequest::doUpdatePlatformRequest() 94 { 95 if (isNull()) { 96 m_nsRequest = nil; 97 return; 98 } 99 100 NSMutableURLRequest* nsRequest = [m_nsRequest.get() mutableCopy]; 101 102 if (nsRequest) 103 [nsRequest setURL:url()]; 104 else 105 nsRequest = [[NSMutableURLRequest alloc] initWithURL:url()]; 106 107 #ifdef BUILDING_ON_TIGER 108 wkSupportsMultipartXMixedReplace(nsRequest); 109 #endif 110 111 [nsRequest setCachePolicy:(NSURLRequestCachePolicy)cachePolicy()]; 112 if (timeoutInterval() != unspecifiedTimeoutInterval) 113 [nsRequest setTimeoutInterval:timeoutInterval()]; 114 [nsRequest setMainDocumentURL:firstPartyForCookies()]; 115 if (!httpMethod().isEmpty()) 116 [nsRequest setHTTPMethod:httpMethod()]; 117 [nsRequest setHTTPShouldHandleCookies:allowCookies()]; 118 119 // Cannot just use setAllHTTPHeaderFields here, because it does not remove headers. 120 NSArray *oldHeaderFieldNames = [[nsRequest allHTTPHeaderFields] allKeys]; 121 for (unsigned i = [oldHeaderFieldNames count]; i != 0; --i) 122 [nsRequest setValue:nil forHTTPHeaderField:[oldHeaderFieldNames objectAtIndex:i - 1]]; 123 HTTPHeaderMap::const_iterator end = httpHeaderFields().end(); 124 for (HTTPHeaderMap::const_iterator it = httpHeaderFields().begin(); it != end; ++it) 125 [nsRequest setValue:it->second forHTTPHeaderField:it->first]; 126 127 // The below check can be removed once we require a version of Foundation with -[NSMutableURLRequest setContentDispositionEncodingFallbackArray:] method. 128 static bool supportsContentDispositionEncodingFallbackArray = [NSMutableURLRequest instancesRespondToSelector:@selector(setContentDispositionEncodingFallbackArray:)]; 129 if (supportsContentDispositionEncodingFallbackArray) { 130 NSMutableArray *encodingFallbacks = [NSMutableArray array]; 131 unsigned count = m_responseContentDispositionEncodingFallbackArray.size(); 132 for (unsigned i = 0; i != count; ++i) { 133 CFStringRef encodingName = m_responseContentDispositionEncodingFallbackArray[i].createCFString(); 134 unsigned long nsEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName)); 135 CFRelease(encodingName); 136 if (nsEncoding != kCFStringEncodingInvalidId) 137 [encodingFallbacks addObject:[NSNumber numberWithUnsignedLong:nsEncoding]]; 138 } 139 [nsRequest setContentDispositionEncodingFallbackArray:encodingFallbacks]; 140 } 141 142 RefPtr<FormData> formData = httpBody(); 143 if (formData && !formData->isEmpty()) 144 WebCore::setHTTPBody(nsRequest, formData); 145 146 m_nsRequest.adoptNS(nsRequest); 147 } 148 149 void ResourceRequest::applyWebArchiveHackForMail() 150 { 151 // Hack because Mail checks for this property to detect data / archive loads 152 [NSURLProtocol setProperty:@"" forKey:@"WebDataRequest" inRequest:(NSMutableURLRequest *)nsURLRequest()]; 153 } 154 155 unsigned initializeMaximumHTTPConnectionCountPerHost() 156 { 157 static const unsigned preferredConnectionCount = 6; 158 return wkInitializeMaximumHTTPConnectionCountPerHost(preferredConnectionCount); 159 } 160 161 } 162