2006-06-16 15:21:39 +00:00
|
|
|
/* Implementation for NSURLRequest for GNUstep
|
|
|
|
Copyright (C) 2006 Software Foundation, Inc.
|
|
|
|
|
2009-03-20 18:52:59 +00:00
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
2006-06-16 15:21:39 +00:00
|
|
|
Date: 2006
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2006-06-16 15:21:39 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2006-06-16 15:21:39 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-16 15:21:39 +00:00
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
|
|
|
|
2010-02-14 10:48:10 +00:00
|
|
|
#define EXPOSE_NSURLRequest_IVARS 1
|
2007-11-29 20:53:26 +00:00
|
|
|
#import "GSURLPrivate.h"
|
|
|
|
#import "GSPrivate.h"
|
2006-06-16 15:21:39 +00:00
|
|
|
|
2007-11-29 20:53:26 +00:00
|
|
|
#import "Foundation/NSCoder.h"
|
2006-06-16 15:21:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Internal data storage
|
|
|
|
typedef struct {
|
|
|
|
NSData *body;
|
|
|
|
NSInputStream *bodyStream;
|
|
|
|
NSString *method;
|
2007-05-15 10:06:42 +00:00
|
|
|
NSMutableDictionary *headers;
|
2006-06-16 15:21:39 +00:00
|
|
|
BOOL shouldHandleCookies;
|
2015-08-30 15:50:27 +00:00
|
|
|
BOOL debug;
|
2006-06-16 15:21:39 +00:00
|
|
|
NSURL *URL;
|
|
|
|
NSURL *mainDocumentURL;
|
|
|
|
NSURLRequestCachePolicy cachePolicy;
|
|
|
|
NSTimeInterval timeoutInterval;
|
|
|
|
NSMutableDictionary *properties;
|
|
|
|
} Internal;
|
|
|
|
|
|
|
|
/* Defines to get easy access to internals from mutable/immutable
|
|
|
|
* versions of the class and from categories.
|
|
|
|
*/
|
2010-02-14 10:48:10 +00:00
|
|
|
#define this ((Internal*)(self->_NSURLRequestInternal))
|
|
|
|
#define inst ((Internal*)(((NSURLRequest*)o)->_NSURLRequestInternal))
|
2006-06-16 15:21:39 +00:00
|
|
|
|
2010-01-11 12:38:37 +00:00
|
|
|
@interface _GSMutableInsensitiveDictionary : NSMutableDictionary
|
|
|
|
@end
|
|
|
|
|
2006-06-16 15:21:39 +00:00
|
|
|
@implementation NSURLRequest
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
NSURLRequest *o = [super allocWithZone: z];
|
|
|
|
|
|
|
|
if (o != nil)
|
|
|
|
{
|
2007-03-01 17:35:43 +00:00
|
|
|
o->_NSURLRequestInternal = NSZoneCalloc(z, 1, sizeof(Internal));
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) requestWithURL: (NSURL *)URL
|
|
|
|
{
|
|
|
|
return [self requestWithURL: URL
|
|
|
|
cachePolicy: NSURLRequestUseProtocolCachePolicy
|
|
|
|
timeoutInterval: 60.0];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) requestWithURL: (NSURL *)URL
|
|
|
|
cachePolicy: (NSURLRequestCachePolicy)cachePolicy
|
|
|
|
timeoutInterval: (NSTimeInterval)timeoutInterval
|
|
|
|
{
|
|
|
|
NSURLRequest *o = [[self class] allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
|
|
|
o = [o initWithURL: URL
|
|
|
|
cachePolicy: cachePolicy
|
|
|
|
timeoutInterval: timeoutInterval];
|
|
|
|
return AUTORELEASE(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURLRequestCachePolicy) cachePolicy
|
|
|
|
{
|
|
|
|
return this->cachePolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
NSURLRequest *o;
|
|
|
|
|
|
|
|
if (NSShouldRetainWithZone(self, z) == YES
|
|
|
|
&& [self isKindOfClass: [NSMutableURLRequest class]] == NO)
|
|
|
|
{
|
|
|
|
o = RETAIN(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
o = [[self class] allocWithZone: z];
|
|
|
|
o = [o initWithURL: [self URL]
|
|
|
|
cachePolicy: [self cachePolicy]
|
|
|
|
timeoutInterval: [self timeoutInterval]];
|
|
|
|
if (o != nil)
|
|
|
|
{
|
|
|
|
inst->properties = [this->properties mutableCopy];
|
|
|
|
ASSIGN(inst->mainDocumentURL, this->mainDocumentURL);
|
|
|
|
ASSIGN(inst->body, this->body);
|
|
|
|
ASSIGN(inst->bodyStream, this->bodyStream);
|
|
|
|
ASSIGN(inst->method, this->method);
|
|
|
|
inst->shouldHandleCookies = this->shouldHandleCookies;
|
2015-08-30 15:50:27 +00:00
|
|
|
inst->debug = this->debug;
|
2007-05-15 10:06:42 +00:00
|
|
|
inst->headers = [this->headers mutableCopy];
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (this != 0)
|
|
|
|
{
|
|
|
|
RELEASE(this->body);
|
|
|
|
RELEASE(this->bodyStream);
|
|
|
|
RELEASE(this->method);
|
|
|
|
RELEASE(this->URL);
|
|
|
|
RELEASE(this->mainDocumentURL);
|
|
|
|
RELEASE(this->properties);
|
2007-05-15 10:06:42 +00:00
|
|
|
RELEASE(this->headers);
|
2006-06-16 15:21:39 +00:00
|
|
|
NSZoneFree([self zone], this);
|
|
|
|
}
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"<%@ %@>",
|
|
|
|
NSStringFromClass([self class]), [[self URL] absoluteString]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
if ([aCoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-02-23 20:42:32 +00:00
|
|
|
- (NSUInteger) hash
|
2006-06-16 15:21:39 +00:00
|
|
|
{
|
|
|
|
return [this->URL hash];
|
|
|
|
}
|
|
|
|
|
2007-05-11 06:15:05 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
return [self initWithURL: nil];
|
|
|
|
}
|
|
|
|
|
2006-06-16 15:21:39 +00:00
|
|
|
- (id) initWithURL: (NSURL *)URL
|
|
|
|
{
|
|
|
|
return [self initWithURL: URL
|
|
|
|
cachePolicy: NSURLRequestUseProtocolCachePolicy
|
|
|
|
timeoutInterval: 60.0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithURL: (NSURL *)URL
|
|
|
|
cachePolicy: (NSURLRequestCachePolicy)cachePolicy
|
|
|
|
timeoutInterval: (NSTimeInterval)timeoutInterval
|
|
|
|
{
|
2008-01-07 17:51:02 +00:00
|
|
|
if ([URL isKindOfClass: [NSURL class]] == NO)
|
|
|
|
{
|
2012-01-24 09:43:15 +00:00
|
|
|
URL = nil;
|
2008-01-07 17:51:02 +00:00
|
|
|
}
|
2012-01-24 09:43:15 +00:00
|
|
|
if ((self = [super init]) != nil)
|
2006-06-16 15:21:39 +00:00
|
|
|
{
|
|
|
|
this->URL = RETAIN(URL);
|
|
|
|
this->cachePolicy = cachePolicy;
|
|
|
|
this->timeoutInterval = timeoutInterval;
|
|
|
|
this->mainDocumentURL = nil;
|
2007-05-11 06:15:05 +00:00
|
|
|
this->method = @"GET";
|
2012-02-08 17:01:31 +00:00
|
|
|
this->shouldHandleCookies = YES;
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)o
|
|
|
|
{
|
|
|
|
if ([o isKindOfClass: [NSURLRequest class]] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (this->URL != inst->URL
|
|
|
|
&& [this->URL isEqual: inst->URL] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (this->mainDocumentURL != inst->mainDocumentURL
|
|
|
|
&& [this->mainDocumentURL isEqual: inst->mainDocumentURL] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (this->method != inst->method
|
|
|
|
&& [this->method isEqual: inst->method] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (this->body != inst->body
|
|
|
|
&& [this->body isEqual: inst->body] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (this->bodyStream != inst->bodyStream
|
|
|
|
&& [this->bodyStream isEqual: inst->bodyStream] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
if (this->properties != inst->properties
|
|
|
|
&& [this->properties isEqual: inst->properties] == NO)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2007-05-15 10:06:42 +00:00
|
|
|
if (this->headers != inst->headers
|
|
|
|
&& [this->headers isEqual: inst->headers] == NO)
|
2006-06-16 15:21:39 +00:00
|
|
|
{
|
2007-05-15 10:06:42 +00:00
|
|
|
return NO;
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURL *) mainDocumentURL
|
|
|
|
{
|
|
|
|
return this->mainDocumentURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
|
|
|
{
|
|
|
|
NSMutableURLRequest *o;
|
|
|
|
|
|
|
|
o = [NSMutableURLRequest allocWithZone: z];
|
|
|
|
o = [o initWithURL: [self URL]
|
|
|
|
cachePolicy: [self cachePolicy]
|
|
|
|
timeoutInterval: [self timeoutInterval]];
|
|
|
|
if (o != nil)
|
|
|
|
{
|
|
|
|
[o setMainDocumentURL: this->mainDocumentURL];
|
|
|
|
inst->properties = [this->properties mutableCopy];
|
|
|
|
ASSIGN(inst->mainDocumentURL, this->mainDocumentURL);
|
|
|
|
ASSIGN(inst->body, this->body);
|
|
|
|
ASSIGN(inst->bodyStream, this->bodyStream);
|
|
|
|
ASSIGN(inst->method, this->method);
|
|
|
|
inst->shouldHandleCookies = this->shouldHandleCookies;
|
2015-08-30 15:50:27 +00:00
|
|
|
inst->debug = this->debug;
|
2007-05-15 10:06:42 +00:00
|
|
|
inst->headers = [this->headers mutableCopy];
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2016-03-17 08:12:45 +00:00
|
|
|
- (int) setDebug: (int)flag
|
2015-08-30 15:50:27 +00:00
|
|
|
{
|
2016-03-17 08:12:45 +00:00
|
|
|
int old = this->debug;
|
|
|
|
|
|
|
|
this->debug = flag ? YES : NO;
|
|
|
|
return old;
|
2015-08-30 15:50:27 +00:00
|
|
|
}
|
|
|
|
|
2006-06-16 15:21:39 +00:00
|
|
|
- (NSTimeInterval) timeoutInterval
|
|
|
|
{
|
|
|
|
return this->timeoutInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSURL *) URL
|
|
|
|
{
|
|
|
|
return this->URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSMutableURLRequest
|
|
|
|
|
|
|
|
- (void) setCachePolicy: (NSURLRequestCachePolicy)cachePolicy
|
|
|
|
{
|
|
|
|
this->cachePolicy = cachePolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setMainDocumentURL: (NSURL *)URL
|
|
|
|
{
|
|
|
|
ASSIGN(this->mainDocumentURL, URL);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTimeoutInterval: (NSTimeInterval)seconds
|
|
|
|
{
|
|
|
|
this->timeoutInterval = seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setURL: (NSURL *)URL
|
|
|
|
{
|
|
|
|
ASSIGN(this->URL, URL);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSURLRequest (NSHTTPURLRequest)
|
|
|
|
|
|
|
|
- (NSDictionary *) allHTTPHeaderFields
|
|
|
|
{
|
2007-05-15 10:06:42 +00:00
|
|
|
NSDictionary *fields;
|
2006-06-16 15:21:39 +00:00
|
|
|
|
2007-05-15 10:06:42 +00:00
|
|
|
if (this->headers == nil)
|
2006-06-16 15:21:39 +00:00
|
|
|
{
|
2007-05-15 10:06:42 +00:00
|
|
|
fields = [NSDictionary dictionary];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fields = [NSDictionary dictionaryWithDictionary: this->headers];
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *) HTTPBody
|
|
|
|
{
|
|
|
|
return this->body;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInputStream *) HTTPBodyStream
|
|
|
|
{
|
|
|
|
return this->bodyStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) HTTPMethod
|
|
|
|
{
|
|
|
|
return this->method;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) HTTPShouldHandleCookies
|
|
|
|
{
|
|
|
|
return this->shouldHandleCookies;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) valueForHTTPHeaderField: (NSString *)field
|
|
|
|
{
|
2007-05-15 10:06:42 +00:00
|
|
|
return [this->headers objectForKey: field];
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSMutableURLRequest (NSMutableHTTPURLRequest)
|
|
|
|
|
|
|
|
- (void) addValue: (NSString *)value forHTTPHeaderField: (NSString *)field
|
|
|
|
{
|
|
|
|
NSString *old = [self valueForHTTPHeaderField: field];
|
|
|
|
|
|
|
|
if (old != nil)
|
|
|
|
{
|
|
|
|
value = [old stringByAppendingFormat: @",%@", value];
|
|
|
|
}
|
|
|
|
[self setValue: value forHTTPHeaderField: field];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setAllHTTPHeaderFields: (NSDictionary *)headerFields
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator = [headerFields keyEnumerator];
|
|
|
|
NSString *field;
|
|
|
|
|
|
|
|
while ((field = [enumerator nextObject]) != nil)
|
|
|
|
{
|
|
|
|
id value = [headerFields objectForKey: field];
|
|
|
|
|
|
|
|
if ([value isKindOfClass: [NSString class]] == YES)
|
|
|
|
{
|
|
|
|
[self setValue: (NSString*)value forHTTPHeaderField: field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setHTTPBodyStream: (NSInputStream *)inputStream
|
|
|
|
{
|
|
|
|
DESTROY(this->body);
|
|
|
|
ASSIGN(this->bodyStream, inputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setHTTPBody: (NSData *)data
|
|
|
|
{
|
|
|
|
DESTROY(this->bodyStream);
|
|
|
|
ASSIGNCOPY(this->body, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setHTTPMethod: (NSString *)method
|
|
|
|
{
|
2007-05-11 06:15:05 +00:00
|
|
|
/* NB. I checked MacOS-X 4.2, and this method actually lets you set any
|
|
|
|
* copyable value (including non-string classes), but setting nil is
|
|
|
|
* equivalent to resetting to the default value of 'GET'
|
|
|
|
*/
|
|
|
|
if (method == nil)
|
|
|
|
{
|
|
|
|
method = @"GET";
|
|
|
|
}
|
2006-06-16 15:21:39 +00:00
|
|
|
ASSIGNCOPY(this->method, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setHTTPShouldHandleCookies: (BOOL)should
|
|
|
|
{
|
|
|
|
this->shouldHandleCookies = should;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setValue: (NSString *)value forHTTPHeaderField: (NSString *)field
|
|
|
|
{
|
2007-05-15 10:06:42 +00:00
|
|
|
if (this->headers == nil)
|
2006-06-16 15:21:39 +00:00
|
|
|
{
|
2010-01-11 12:38:37 +00:00
|
|
|
this->headers = [_GSMutableInsensitiveDictionary new];
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
2007-05-15 10:06:42 +00:00
|
|
|
[this->headers setObject: value forKey: field];
|
2006-06-16 15:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSURLRequest (Private)
|
2015-08-30 15:50:27 +00:00
|
|
|
|
|
|
|
- (BOOL) _debug
|
|
|
|
{
|
|
|
|
return this->debug;
|
|
|
|
}
|
|
|
|
|
2006-06-16 15:21:39 +00:00
|
|
|
- (id) _propertyForKey: (NSString*)key
|
|
|
|
{
|
|
|
|
return [this->properties objectForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _setProperty: (id)value forKey: (NSString*)key
|
|
|
|
{
|
|
|
|
if (this->properties == nil)
|
|
|
|
{
|
|
|
|
this->properties = [NSMutableDictionary new];
|
|
|
|
[this->properties setObject: value forKey: key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|