From ae3fb0d61fcf57680108bb607c123cb4e59dc2f6 Mon Sep 17 00:00:00 2001 From: rfm Date: Fri, 16 Jun 2006 15:21:39 +0000 Subject: [PATCH] Skeletal implementation of new URL laoding scheme git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@23065 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 43 ++ Headers/Foundation/Foundation.h | 30 +- Headers/Foundation/NSHTTPCookie.h | 246 +++++++++ Headers/Foundation/NSHTTPCookieStorage.h | 118 ++++ .../Foundation/NSURLAuthenticationChallenge.h | 134 +++++ Headers/Foundation/NSURLCache.h | 193 +++++++ Headers/Foundation/NSURLConnection.h | 228 ++++++++ Headers/Foundation/NSURLCredential.h | 98 ++++ Headers/Foundation/NSURLCredentialStorage.h | 103 ++++ Headers/Foundation/NSURLDownload.h | 206 +++++++ Headers/Foundation/NSURLError.h | 88 +++ Headers/Foundation/NSURLProtectionSpace.h | 129 +++++ Headers/Foundation/NSURLProtocol.h | 222 ++++++++ Headers/Foundation/NSURLRequest.h | 275 ++++++++++ Headers/Foundation/NSURLResponse.h | 120 ++++ Source/DocMakefile | 25 +- Source/GNUmakefile | 34 +- Source/NSHTTPCookie.m | 173 ++++++ Source/NSHTTPCookieStorage.m | 138 +++++ Source/NSMapTable.m | 3 +- Source/NSSet.m | 3 +- Source/NSURL.m | 5 +- Source/NSURLAuthenticationChallenge.m | 131 +++++ Source/NSURLCache.m | 216 ++++++++ Source/NSURLConnection.m | 252 +++++++++ Source/NSURLCredential.m | 133 +++++ Source/NSURLCredentialStorage.m | 193 +++++++ Source/NSURLDownload.m | 279 ++++++++++ Source/NSURLHandle.m | 12 +- Source/NSURLProtectionSpace.m | 198 +++++++ Source/NSURLProtocol.m | 174 ++++++ Source/NSURLRequest.m | 512 ++++++++++++++++++ Source/NSURLResponse.m | 338 ++++++++++++ 33 files changed, 5024 insertions(+), 28 deletions(-) create mode 100644 Headers/Foundation/NSHTTPCookie.h create mode 100644 Headers/Foundation/NSHTTPCookieStorage.h create mode 100644 Headers/Foundation/NSURLAuthenticationChallenge.h create mode 100644 Headers/Foundation/NSURLCache.h create mode 100644 Headers/Foundation/NSURLConnection.h create mode 100644 Headers/Foundation/NSURLCredential.h create mode 100644 Headers/Foundation/NSURLCredentialStorage.h create mode 100644 Headers/Foundation/NSURLDownload.h create mode 100644 Headers/Foundation/NSURLError.h create mode 100644 Headers/Foundation/NSURLProtectionSpace.h create mode 100644 Headers/Foundation/NSURLProtocol.h create mode 100644 Headers/Foundation/NSURLRequest.h create mode 100644 Headers/Foundation/NSURLResponse.h create mode 100644 Source/NSHTTPCookie.m create mode 100644 Source/NSHTTPCookieStorage.m create mode 100644 Source/NSURLAuthenticationChallenge.m create mode 100644 Source/NSURLCache.m create mode 100644 Source/NSURLConnection.m create mode 100644 Source/NSURLCredential.m create mode 100644 Source/NSURLCredentialStorage.m create mode 100644 Source/NSURLDownload.m create mode 100644 Source/NSURLProtectionSpace.m create mode 100644 Source/NSURLProtocol.m create mode 100644 Source/NSURLRequest.m create mode 100644 Source/NSURLResponse.m diff --git a/ChangeLog b/ChangeLog index 1dbc6e6cb..2765d2717 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,46 @@ +2006-06-16 Richard Frith-Macdonald + + * Headers/Foundation/NSURLCredential.h: + * Headers/Foundation/NSHTTPCookieStorage.h: + * Headers/Foundation/NSURLAuthenticationChallenge.h: + * Headers/Foundation/NSHTTPCookie.h: + * Headers/Foundation/NSURLError.h: + * Headers/Foundation/NSURLProtectionSpace.h: + * Headers/Foundation/NSURLDownload.h: + * Headers/Foundation/NSURLRequest.h: + * Headers/Foundation/NSURLCredentialStorage.h: + * Headers/Foundation/Foundation.h: + * Headers/Foundation/NSURLResponse.h: + * Headers/Foundation/NSURLConnection.h: + * Headers/Foundation/NSURLProtocol.h: + * Headers/Foundation/NSURLCache.h: + Headers for Apple's new URL loading scheme (including documentation). + * Source/NSMapTable.m: + * Source/NSURLCredential.m: + * Source/NSURLAuthenticationChallenge.m: + * Source/NSURLHandle.m: + * Source/GNUmakefile: + * Source/NSURLDownload.m: + * Source/NSURLCredentialStorage.m: + * Source/NSURLResponse.m: + * Source/NSURLProtocol.m: + * Source/NSURLCache.m: + * Source/NSHTTPCookieStorage.m: + * Source/NSURL.m: + * Source/DocMakefile: + * Source/NSHTTPCookie.m: + * Source/NSURLProtectionSpace.m: + * Source/NSURLRequest.m: + * Source/NSSet.m: + * Source/NSURLConnection.m: + Skeleton code for Apple's new URL loading scheme (plus minor + tweaks to comments in other files). This code is not expected + to work yet. Major lacks are ... + 1. concrete implementations of protocols + 2. on disk cache/storage management + 3. thread safety (locking) for shared objects + However, I hope it will provide a good starting framework. + 2006-06-15 Richard Frith-Macdonald * Additions/GNUstepBase/GSXML.h: New XMLRPC settings to control diff --git a/Headers/Foundation/Foundation.h b/Headers/Foundation/Foundation.h index d60c4ffe3..74d5258f8 100644 --- a/Headers/Foundation/Foundation.h +++ b/Headers/Foundation/Foundation.h @@ -20,7 +20,8 @@ You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA. + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. */ #ifndef __Foundation_h_GNUSTEP_BASE_INCLUDE @@ -44,11 +45,11 @@ #include #include #include -#include -#include #include -#include +#include +#include #include +#include #include #include #include @@ -59,14 +60,16 @@ #include #include #include -#include #include +#include #include +#include +#include #include #include #include -#include #include +#include #include #include #include @@ -92,11 +95,22 @@ #include #include #include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include +#include +#include +#include +#include #include #include #include diff --git a/Headers/Foundation/NSHTTPCookie.h b/Headers/Foundation/NSHTTPCookie.h new file mode 100644 index 000000000..2ecf850ec --- /dev/null +++ b/Headers/Foundation/NSHTTPCookie.h @@ -0,0 +1,246 @@ +/* Interface for NSHTTPCookie for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSHTTPCookie_h_GNUSTEP_BASE_INCLUDE +#define __NSHTTPCookie_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSArray; +@class NSDate; +@class NSDictionary; +@class NSString; +@class NSURL; + +extern NSString * const NSHTTPCookieComment; /** Obtain text of the comment */ +extern NSString * const NSHTTPCookieCommentURL; /** Obtain the comment URL */ +extern NSString * const NSHTTPCookieDiscard; /** Obtain the sessions discard setting */ +extern NSString * const NSHTTPCookieDomain; /** Obrain cookie domain */ +extern NSString * const NSHTTPCookieExpires; /** Obrain cookie expiry date */ +extern NSString * const NSHTTPCookieMaximumAge; /** Obtain maximum age (expiry) */ +extern NSString * const NSHTTPCookieName; /** Obtain name of cookie */ +extern NSString * const NSHTTPCookieOriginURL; /** Obtain cookie origin URL */ +extern NSString * const NSHTTPCookiePath; /** Obtain cookie path */ +extern NSString * const NSHTTPCookiePort; /** Obtain cookie ports */ +extern NSString * const NSHTTPCookieSecure; /** Obtain cookie security */ +extern NSString * const NSHTTPCookieValue; /** Obtain value of cookie */ +extern NSString * const NSHTTPCookieVersion; /** Obtain cookie version */ + + +/** + * An instance of the NSHTTPCookie class is a single, immutable http cookie. + * It can be initialised with properties from a dictionary and has accessor + * methods to obtain the cookie values.
+ * The class supports unversioned cookies (sometimes referred to as version 0) + * as originally produced by netscape, as well as more recent standardised + * and versioned cookies. + */ +@interface NSHTTPCookie : NSObject +{ +@private + void *_NSHTTPCookieInternal; +} + +/** + * Allocates and returns an autoreleasd instance using -initWithProperties: + * to initialise it from properties. + */ ++ (id) cookieWithProperties: (NSDictionary *)properties; + +/** + * Returns an array of cookies parsed from the headerFields and URL + * (assuming that the headerFields came from a response to a request + * sent to the URL).
+ * The headerFields dictionary must contain at least all the headers + * relevant to cookie setting ... other headers are ignored. + */ ++ (NSArray *) cookiesWithResponseHeaderFields: (NSDictionary *)headerFields + forURL: (NSURL *)URL; + +/** + * Returns a dictionary of header fields that can be used to add the + * specified cookies to a request. + */ ++ (NSDictionary *) requestHeaderFieldsWithCookies: (NSArray *)cookies; + +/** + * Returns a string which may be used to describe the cookie to the + * user, or nil if no comment is set. + */ +- (NSString *) comment; + +/** + * Returns a URL where the user can find out about the cookie, or nil + * if no comment URL is set. + */ +- (NSURL *) commentURL; + +/** + * Returns the domain to which the cookie should be sent.
+ * If there is a leading dot then subdomains should also receive the + * coockie as specified in RFC 2965. + */ +- (NSString *) domain; + +/** + * Returns the expiry date of the receiver or nil if there is no + * such date. + */ +- (NSDate *) expiresDate; + +/** + * Initialises the receiver with a dictionary of properties.
+ * Unrecognised keys are ignored.
+ * Returns nil if a required key is missing or if an illegal + * value is specified for a key. + * + * NSHTTPCookieComment + * + * The [NSString] comment for the cookie (if any).
+ * This is nil by default and for unversioned cookies. + *
+ * NSHTTPCookieCommentURL + * + * The [NSString] or [NSURL] URL to get the comment for the cookie.
+ * This is nil by default and for unversioned cookies. + *
+ * NSHTTPCookieDomain + * + * The [NSString] specified the domain to which the cookie applies.
+ * This is extracted from NSHTTPCookieOriginURL if not specified. + *
+ * NSHTTPCookieDiscard + * + * A [NSString] (either TRUE or FALSE) saying whether the cookie + * is to be discarded when the session ends.
+ * Defaults to FALSE except for versioned cookies where + * NSHTTPCookieMaximumAge is unspecified. + *
+ * NSHTTPCookieExpires + * + * The [NSDate] or [NSString] (format Wdy, DD-Mon-YYYY HH:MM:SS GMT) + * specifying when an unversioned cookie expires and ignored for + * versioned cookies. + * + * NSHTTPCookieMaximumAge + * + * An [NSString] containing an integer value specifying the longest time + * (in seconds) for which the cookie is valid.
+ * This defaults to zero and is only meaningful for versioned cookies. + *
+ * NSHTTPCookieName + * + * An [NSString] ... obvious ... no default value. + * + * NSHTTPCookieOriginURL + * + * An [NSString] or [NSURL] specifying the URL which set the cookie.
+ * Must be supplied if NSHTTPCookieDomain is not. + *
+ * NSHTTPCookiePath + * + * An [NSString] specifying the path from the cookie.
+ * If unspecified this value is determined from NSHTTPCookieOriginURL + * or defaults to '/'. + *
+ * NSHTTPCookiePort + * + * An [NSString] containing a comma separated list of integer port + * numbers. This is valid for versioned cookies and defaults to + * an empty string. + * + * NSHTTPCookieSecure + * + * An [NSString] saying whether the cookie may be sent over + * insecure connections.
+ * The default is FALSE meaning that it may be sent insecurely. + *
+ * NSHTTPCookieValue + * + * An [NSString] containing the whole value of the cooke.
+ * This parameter must be provided. + *
+ * NSHTTPCookieVersion + * + * An [NSString] specifying the cookie version ... for an + * unversioned cookie (the default) this is '0'.
+ * Also supports version '1'. + *
+ *
+ */ +- (id) initWithProperties: (NSDictionary *)properties; + +/** + * Returns whether the receiver should only be sent over + * secure connections. + */ +- (BOOL) isSecure; + +/** + * Returns whether the receiver should be destroyed at the end of the + * session. + */ +- (BOOL) isSessionOnly; + +/** + * Returns the name of the receiver. + */ +- (NSString *) name; + +/** + * Returns the URL path within the cookie's domain for which + * this cookie must be sent. + */ +- (NSString *) path; + +/** + * Returns the list of ports to which the receiver should be sent, + * or nil if the cookie can be used for any port. + */ +- (NSArray *) portList; + +/** + * Returns a dictionary representation of the receiver which could be + * used as the argument for -initWithProperties: to recreate a copy + * of the receiver. + */ +- (NSDictionary *) properties; + +/** + * Returns the value of the receiver. + */ +- (NSString *) value; + +/** + * Returns 0 for an unversioned Netscape style cookie or a + * positive integer for a versioned cookie. + */ +- (unsigned) version; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSHTTPCookieStorage.h b/Headers/Foundation/NSHTTPCookieStorage.h new file mode 100644 index 000000000..6aa87a5d8 --- /dev/null +++ b/Headers/Foundation/NSHTTPCookieStorage.h @@ -0,0 +1,118 @@ +/* Interface for NSHTTPCookieStorage for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSHTTPCookieStorage_h_GNUSTEP_BASE_INCLUDE +#define __NSHTTPCookieStorage_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSArray; +@class NSHTTPCookie; +@class NSURL; + +/** + * NSHTTPCookieAcceptPolicyAlways Accept all cookies + * NSHTTPCookieAcceptPolicyNever Reject all cookies + * NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain Accept cookies + * only from the main document domain + */ +typedef enum { + NSHTTPCookieAcceptPolicyAlways, + NSHTTPCookieAcceptPolicyNever, + NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain +} NSHTTPCookieAcceptPolicy; + +/** + * Posted to the distributed notification center when the cookie + * accept policy is changed. + */ +extern NSString * const NSHTTPCookieManagerAcceptPolicyChangedNotification; + +/** + * Posted when the set of cookies changes + */ +extern NSString * const NSHTTPCookieManagerCookiesChangedNotification; + + +/** + * The NSHTTPCookieStorage class provides a shared instance which handles + * the shared cookie store.
+ */ + +@interface NSHTTPCookieStorage : NSObject +{ +@private + void *_NSHTTPCookieStorageInternal; +} + +/** + * Returns the shared instance. + */ ++ (NSHTTPCookieStorage *) sharedHTTPCookieStorage; + +/** + * Returns the current cookie accept policy. + */ +- (NSHTTPCookieAcceptPolicy) cookieAcceptPolicy; + +/** + * Returns an array of all managed cookies. + */ +- (NSArray *) cookies; + +/** + * Returns an array of all known cookies to send to URL. + */ +- (NSArray *) cookiesForURL: (NSURL *)URL; + +/** + * Deletes cookie from the shared store. + */ +- (void) deleteCookie: (NSHTTPCookie *)cookie; + +/** + * Sets a cookie in the store, replacing any existing cookie with the + * same name, domain and path. + */ +- (void) setCookie: (NSHTTPCookie *)cookie; + +/** + * Sets the current cookie accept policy. + */ +- (void) setCookieAcceptPolicy: (NSHTTPCookieAcceptPolicy)cookieAcceptPolicy; + +/** + * Adds to the shared store following the policy for + * NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain + */ +- (void) setCookies: (NSArray *)cookies + forURL: (NSURL *)URL + mainDocumentURL: (NSURL *)mainDocumentURL; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLAuthenticationChallenge.h b/Headers/Foundation/NSURLAuthenticationChallenge.h new file mode 100644 index 000000000..ab55a3f69 --- /dev/null +++ b/Headers/Foundation/NSURLAuthenticationChallenge.h @@ -0,0 +1,134 @@ +/* Interface for NSURLAuthenticationChallenge for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLAuthenticationChallenge_h_GNUSTEP_BASE_INCLUDE +#define __NSURLAuthenticationChallenge_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSError; +@class NSURLAuthenticationChallenge; +@class NSURLCredential; +@class NSURLProtectionSpace; +@class NSURLResponse; + +/** + */ +@protocol NSURLAuthenticationChallengeSender + +/** + */ +- (void) cancelAuthenticationChallenge: + (NSURLAuthenticationChallenge *)challenge; + +/** + */ +- (void) continueWithoutCredentialForAuthenticationChallenge: + (NSURLAuthenticationChallenge *)challenge; + +/** + */ +- (void) useCredential: (NSURLCredential *)credential + forAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +@end + + +/** + * Class to represent an authentication challenge and indicate when the + * challenge is complete. + */ +@interface NSURLAuthenticationChallenge : NSObject +{ +@private + void *_NSURLAuthenticationChallengeInternal; +} + +/** + * Returns the error with which the reciver was initialised or nil + * if it was not initialised with an error.
+ * The error may indicate the nature of the authentication failure. + */ +- (NSError *) error; + +/** + * Returns the response with which the receiver was initialised.
+ * This response contains the authentication failure corresponding to + * this challenge object.
+ * If there was no failure or response, returns nil. + */ +- (NSURLResponse *) failureResponse; + +/** + * Initialises a new challenge by copying information from an old one. + */ +- (id) initWithAuthenticationChallenge: + (NSURLAuthenticationChallenge *)challenge + sender: + (id)sender; + +/** + * Returns the receiver initialised in the specified protection space and + * with the specified credential. The previousFailureCount says how many + * requests have failed the challenge and response provide information + * about the last failure (which caused this challenge to be created).
+ * The error provides information about the authentication failure and + * the sender is the object to receive callbacks. + */ +- (id) initWithProtectionSpace: (NSURLProtectionSpace *)space + proposedCredential: (NSURLCredential *)credential + previousFailureCount: (int)previousFailureCount + failureResponse: (NSURLResponse *)response + error: (NSError *)error + sender: (id)sender; + +/** + * Returns the count of failed authentication attempts. + */ +- (int) previousFailureCount; + +/** + * Returns a proposed credential to answer the challenge.
+ * If there is no credential available, this returns nil.
+ * The credential may come from storage or from the URL itsself. + */ +- (NSURLCredential *) proposedCredential; + +/** + * Returns the protection space needing authentication. + */ +- (NSURLProtectionSpace *) protectionSpace; + +/** + * Returns the sender of the challenge ... the object to which callbacks + * should be made while processing the challenge. + */ +- (id) sender; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLCache.h b/Headers/Foundation/NSURLCache.h new file mode 100644 index 000000000..be021900e --- /dev/null +++ b/Headers/Foundation/NSURLCache.h @@ -0,0 +1,193 @@ +/* Interface for NSURLCache for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLCache_h_GNUSTEP_BASE_INCLUDE +#define __NSURLCache_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSData; +@class NSDictionary; +@class NSURLRequest; +@class NSURLRequest; +@class NSURLResponse; + +/** + * Specifies the cache storage policy. + */ +typedef enum +{ + NSURLCacheStorageAllowed, /** Unrestricted caching */ + NSURLCacheStorageAllowedInMemoryOnly, /** In memory caching only */ + NSURLCacheStorageNotAllowed, /** No caching allowed */ +} NSURLCacheStoragePolicy; + + +/** + * Encapsulates a cached response to a URL load request. + */ +@interface NSCachedURLResponse : NSObject +{ +@private + void *_NSCachedURLResponseInternal; +} + +/** + * Returns the data with which the receiver was initialised. + */ +- (NSData *) data; + +/** + * Uses the NSURLCacheStorageAllowed policy to cache the specified + * response and data.
+ * Returns the cached response. + */ +- (id) initWithResponse: (NSURLResponse *)response data: (NSData *)data; + +/** + * Returns the receiver initialized with the provided parameters. + */ +- (id) initWithResponse: (NSURLResponse *)response + data: (NSData *)data + userInfo: (NSDictionary *)userInfo + storagePolicy: (NSURLCacheStoragePolicy)storagePolicy; + +/** + * Returns the response with which the receiver was initialised. + */ +- (NSURLResponse *) response; + +/** + * Returns the storage policy with which the receiver was initialised. + */ +- (NSURLCacheStoragePolicy) storagePolicy; + +/** + * Returns the user info dictionary with which the receiver was initialised + * (if any). + */ +- (NSDictionary *) userInfo; + +@end + + +@interface NSURLCache : NSObject +{ +@private + void *_NSURLCacheInternal; +} + +/** + * Sets the shared [NSURLCache] used throughout the process.
+ * If you are going to call this method to specify an alternative to + * the default cache, you should do so before the shared cache is used + * in order to avoid loss of data that was in the old cache. + */ ++ (void) setSharedURLCache: (NSURLCache *)cache; + +/** + * Returns the shared cache instance set by +setSharedURLCache: or, + * if none has been set, returns an instance initialised with
+ * + * Memory capacity + * 4 megabytes + * Disk capacity + * 20 megabytes + * Disk path + * user-library-path/Caches/current-app-name + * + */ ++ (NSURLCache *) sharedURLCache; + +/** + * Returns the [NSCachedURLResponse] cached for the specified request + * or nil if there is no matching response in tthe cache. + */ +- (NSCachedURLResponse *) cachedResponseForRequest: (NSURLRequest *)request; + +/** + * Returns the current size (butes) of the data stored in the on-disk + * cache. + */ +- (unsigned) currentDiskUsage; + +/** + * Returns the current size (butes) of the data stored in the in-memory + * cache. + */ +- (unsigned) currentMemoryUsage; + +/** + * Returns the disk capacity (in bytes) of the cache. + */ +- (unsigned) diskCapacity; + +/** + * Returns the receiver initialised with the specified capacities + * (in bytes) and using the specified location on disk for persistent + * storage. + */ +- (id) initWithMemoryCapacity: (unsigned)memoryCapacity + diskCapacity: (unsigned)diskCapacity + diskPath: (NSString *)path; + +/** + * Returns the memory capacity (in bytes) of the cache. + */ +- (unsigned) memoryCapacity; + +/** + * Empties the cache. + */ +- (void) removeAllCachedResponses; + +/** + * Removes from the cache (if present) the [NSCachedURLResponse] + * which was stored using the specified request. + */ +- (void) removeCachedResponseForRequest: (NSURLRequest *)request; + +/** + * Sets the disk capacity (in bytes) truncating cache contents if necessary. + */ +- (void) setDiskCapacity: (unsigned)diskCapacity; + +/** + * Sets the memory capacity (in bytes) truncating cache contents if necessary. + */ +- (void) setMemoryCapacity: (unsigned)memoryCapacity; + +/** + * Stores cachedResponse in the cache, keyed on request.
+ * Replaces any existing response with the same key. + */ +- (void) storeCachedResponse: (NSCachedURLResponse *)cachedResponse + forRequest: (NSURLRequest *)request; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLConnection.h b/Headers/Foundation/NSURLConnection.h new file mode 100644 index 000000000..5e8fcc8f6 --- /dev/null +++ b/Headers/Foundation/NSURLConnection.h @@ -0,0 +1,228 @@ +/* Interface for NSURLConnection for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLConnection_h_GNUSTEP_BASE_INCLUDE +#define __NSURLConnection_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSCachedURLResponse; +@class NSData; +@class NSError; +@class NSURLAuthenticationChallenge; +@class NSURLRequest; +@class NSURLResponse; + +/** + */ +@interface NSURLConnection : NSObject +{ +@private + void *_NSURLConnectionInternal; +} + +/** + * Performs a preliminary check to see if a load of the specified + * request can be handled by an instance of this class.
+ * The results of this method may be invalidated by subsequent + * changes to the request or changes to the registered protocols + * etc. + */ ++ (BOOL) canHandleRequest: (NSURLRequest *)request; + +/** + * Allocates and returns the autoreleased instance which it initialises + * using the -initWithRequest:delegate: method. + */ ++ (NSURLConnection *) connectionWithRequest: (NSURLRequest *)request + delegate: (id)delegate; + +/** + * Cancel the asynchronous load in progress (if any) for this connection. + */ +- (void) cancel; + +/** + * Initialises the receiver with the specified request (performing + * a deep copy so that ithe request does not change during loading) + * and delegate.
+ * This automatically initiates an asynchronous load for the request.
+ * Processing of the request is done in the thread which calls this + * method, so the thread must run its current run loop + * (in NSDefaultRunLoopMode) for processing to continue/complete.
+ * The delegate will receive callbacks informing it of the progress + * of the load. + */ +- (id) initWithRequest: (NSURLRequest *)request delegate: (id)delegate; + +@end + + + +/** + * This category is an informal protocol specifying how an NSURLConnection + * instance will communicate with its delegate to inform it of (and allow + * it to manage) the progress of a load request.
+ * A load operation is performed by asynchronous I/O using the + * run loop of the thread in which it was initiated, so all callbacks + * will occur in that thread.
+ * The process of loading a resource occurs as follows -
+ * + * + * Any number of -connection:willSendRequest:redirectResponse: + * messages may be sent to the delegate before any other messages + * in this list are sent. This permits a chain of redirects to + * be followed before eventual loading of 'real' data. + * + * + * A -connection:didReceiveAuthenticationChallenge: message may be + * sent to the delegate (where authentication is required) before + * response data can be downloaded. + * + * + * Any number of -connection:didReceiveResponse: messages + * may be be sent to the delegate before a + * -connection:didReceiveData: message. Usually there is exactly one + * of these, but for multipart/x-mixed-replace there may be multiple + * responses for each part, and if an error occurs in the download + * the delegate may not receive a response at all.
+ * Delegates should discard previously handled data when they + * receive a new response. + *
+ * + * Any number of -connection:didReceiveData: messages may + * be sent before the load completes as described below. + * + * + * A single -connection:willCacheResponse: message may + * be sent to the delegate after any -connection:didReceiveData: + * messages are sent but before a -connectionDidFinishLoading: message + * is sent. + * + * + * Unless the NSURLConnection receives a -cancel message, + * the delegate will receive one and only one of + * -connectionDidFinishLoading:, or + * -connection:didFailWithError: message, but never + * both.
+ * Once either of these terminal messages is sent the + * delegate will receive no further messages from the + * NSURLConnection. + *
+ *
+ */ +@interface NSObject (NSURLConnectionDelegate) + +/** + * Instructs the delegate that authentication for challenge has + * been cancelled for the request loading on connection. + */ +- (void) connection: (NSURLConnection *)connection + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +/* + * Called when an NSURLConnection has failed to load successfully. + */ +- (void) connection: (NSURLConnection *)connection + didFailWithError: (NSError *)error; + +/** + * Called when an NSURLConnection has finished loading successfully. + */ +- (void) connectionDidFinishLoading: (NSURLConnection *)connection; + +/** + * Called when an authentication challenge is received ... the delegate + * should send -useCredential:forAuthenticationChallenge: or + * -continueWithoutCredentialForAuthenticationChallenge: or + * -cancelAuthenticationChallenge: to the challenge sender when done. + */ +- (void) connection: (NSURLConnection *)connection + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +/** + * Called when content data arrives during a load operations ... this + * may be incremental or may be the compolete data for the load. + */ +- (void) connection: (NSURLConnection *)connection + didReceiveData: (NSData *)data; + +/** + * Called when enough information to build a NSURLResponse object has + * been received. + */ +- (void) connection: (NSURLConnection *)connection + didReceiveResponse: (NSURLResponse *)response; + +/** + * Called with the cachedResponse to be stored in the cache. + * The delegate can inspect the cachedResponse and return a modified + * copy if if wants changed to what whill be stored.
+ * If it returns nil, nothing will be stored in the cache. + */ +- (NSCachedURLResponse *) connection: (NSURLConnection *)connection + willCacheResponse: (NSCachedURLResponse *)cachedResponse; + +/** + * Informs the delegate that the connection must change the URL of + * the request in order to continue with the load operation.
+ * This allows the delegate to ionspect and/or modify a copy of the request + * before the connection continues loading it. Normally the delegate + * can return the request unmodifield.
+ * The redirection can be rejectected by the delegate calling -cancel + * or returning nil.
+ * Cancelling the load will simply stop it, but returning nil will + * cause it to complete with a redirection failure.
+ * As a special case, this method may be called with a nil response, + * indicating a change of URL made internally by the system rather than + * due to a response from the server. + */ +- (NSURLRequest *) connection: (NSURLConnection *)connection + willSendRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)response; + +@end + + + +/** + * An interface to perform synchronous loading of URL requests. + */ +@interface NSURLConnection (NSURLConnectionSynchronousLoading) + +/** + * Performs a synchronous load of request and returns the + * [NSURLResponse] in response.
+ * Returns the result of the load or nil if the load failed. + */ ++ (NSData *) sendSynchronousRequest: (NSURLRequest *)request + returningResponse: (NSURLResponse **)response + error: (NSError **)error; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLCredential.h b/Headers/Foundation/NSURLCredential.h new file mode 100644 index 000000000..3506edf51 --- /dev/null +++ b/Headers/Foundation/NSURLCredential.h @@ -0,0 +1,98 @@ +/* Interface for NSURLCredential for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLCredential_h_GNUSTEP_BASE_INCLUDE +#define __NSURLCredential_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSString; + +/** + * Controls how long a credential is retained. + */ +typedef enum { + NSURLCredentialPersistenceNone, /** Don't save at all */ + NSURLCredentialPersistenceForSession, /** Save for current session */ + NSURLCredentialPersistencePermanent /** Save forever (on disk) */ +} NSURLCredentialPersistence; + + +/** + * Represents a user/password credential + */ +@interface NSURLCredential : NSObject +{ +@private + void *_NSURLCredentialInternal; +} + +/** + * Returns an autoreleased instance initialised using the + * -initWithUser:password:persistence: method. + */ ++ (NSURLCredential *) credentialWithUser: (NSString *)user + password: (NSString *)password + persistence: (NSURLCredentialPersistence)persistence; + +/** + * Determine whether the credential has a password. + */ +- (BOOL) hasPassword; + +/** + * Initialises and returns the receiver with a user name and password.
+ * The user identifies the credential and must be specified but the + * password may be nil. + */ +- (id) initWithUser: (NSString *)user + password: (NSString *)password + persistence: (NSURLCredentialPersistence)persistence; + +/** + * Returns the password for the receiver.
+ * May require prompting of the user to authorize retrieval.
+ * May return nil if retrieval of the password fails (eg authorization + * failure) even if the credential actually has a password. Call the + * -hasPassword method to determine whether the credential has a + * password + */ +- (NSString *) password; + +/** + * Return the presistence of this credential. + */ +- (NSURLCredentialPersistence) persistence; + +/** + * Returns the user string for the receiver + */ +- (NSString *) user; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLCredentialStorage.h b/Headers/Foundation/NSURLCredentialStorage.h new file mode 100644 index 000000000..142568f53 --- /dev/null +++ b/Headers/Foundation/NSURLCredentialStorage.h @@ -0,0 +1,103 @@ +/* Interface for NSURLCredentialStorage for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLCredentialStorage_h_GNUSTEP_BASE_INCLUDE +#define __NSURLCredentialStorage_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSDictionary; +@class NSString; +@class NSURLCredential; +@class NSURLProtectionSpace; + +/** + * Notification sent when the set of stored credentials changes. + */ +extern NSString *const NSURLCredentialStorageChangedNotification; + +/** + * Provides shared storage of credentials. + */ +@interface NSURLCredentialStorage : NSObject +{ +@private + void *_NSURLCredentialStorageInternal; +} + +/** + * Return the shared credential storage object. + */ ++ (NSURLCredentialStorage *) sharedCredentialStorage; + +/** + * Returns a dictionary of dictionaries ... with [NSURLProtectionSpace] + * objects as the keys in the outer dictionary, and values which are + * dictionaries of the credentails within each protection space. + */ +- (NSDictionary *) allCredentials; + +/** + * Returns a dictionary mapping usernames to credentials + * for the specified protection space.
+ * Each username is a unique identifier for a credential + * within a protection space. + */ +- (NSDictionary *) credentialsForProtectionSpace: (NSURLProtectionSpace *)space; + +/** + * Returns the default credential for the specified protection space, or + * nil if none is set. + */ +- (NSURLCredential *) defaultCredentialForProtectionSpace: + (NSURLProtectionSpace *)space; + +/** + * Removes the credential from both in-memory and persistent storage + * for the specified protection space. + */ +- (void) removeCredential: (NSURLCredential *)credential + forProtectionSpace: (NSURLProtectionSpace *)space; + +/** + * Sets credential in the storage for the protection space specified.
+ * This replaces any old value with the same username. + */ +- (void) setCredential: (NSURLCredential *)credential + forProtectionSpace: (NSURLProtectionSpace *)space; + +/** + * Sets the default credential for the protection space. Also calls + * -setCredential:forProtectionSpace: if the credential has not already + * been set in space. + */ +- (void) setDefaultCredential: (NSURLCredential *)credential + forProtectionSpace: (NSURLProtectionSpace *)space; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLDownload.h b/Headers/Foundation/NSURLDownload.h new file mode 100644 index 000000000..f60b1abaa --- /dev/null +++ b/Headers/Foundation/NSURLDownload.h @@ -0,0 +1,206 @@ +/* Interface for NSURLDownload for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLDownload_h_GNUSTEP_BASE_INCLUDE +#define __NSURLDownload_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSData; +@class NSError; +@class NSString; +@class NSURLAuthenticationChallenge; +@class NSURLRequest; +@class NSURLResponse; + +/** + * Handles download to file. + */ +@interface NSURLDownload : NSObject +{ +@private + void *_NSURLDownloadInternal; +} + +/** + * Returns a flag saying whether the class can resume a download + * which was decoded with MIMEType.
+ */ ++ (BOOL) canResumeDownloadDecodedWithEncodingMIMEType: (NSString *)MIMEType; + +/** + * Cancels the download and deletes any downloaded file. + */ +- (void) cancel; + +/** + * Returns a flag saying whether a partially downloaded file should be + * deleted on failure ... YES by default. + */ +- (BOOL) deletesFileUponFailure; + +/** + * Initialises the receiver and start the download process. + */ +- (id) initWithRequest: (NSURLRequest *)request delegate: (id)delegate; + +/** + * Initialises the receiver with data from a previous partial + * download and resumes (or restarts) the downloading process. + */ +- (id) initWithResumeData: (NSData *)resumeData + delegate: (id)delegate + path: (NSString *)path; + +/** + * Returns the receiver's request. + */ +- (NSURLRequest *) request; + +/** + * Returns state data of an incomplete download ... this data should be + * sufficient to resume/restart the download using the + * -initWithResumeData:delegate:path: method.
+ * Returns nil if a resume is probably impossible.
+ * NB. You need to call -setDeletesFileUponFailure: to turn off deletion + * if you wish to be able to resume an incomplete download. + */ +- (NSData *) resumeData; + +/** + * Sets a flag to determine if downloaded file is be deleted upon failure. + * This is YES by default and needs to be set to NO if you want to be able + * to attempt to resume a failed download. + */ +- (void) setDeletesFileUponFailure: (BOOL)deletesFileUponFailure; + +/** + * Sets the path to which the file is downloaded.
+ * May be called (once only) after initialisation of the receiver or when the + * delegate receives a -decideDestinationWithSuggestedFilename: message.
+ * Appends a number to the filename if allowOverwrite is NO and a file + * already exists at path.
+ * See -didCreateDestination: also. + */ +- (void) setDestination: (NSString *)path allowOverwrite: (BOOL)allowOverwrite; + +@end + +/** + * Protocol for delegate used to report the progress of the download. + */ +@interface NSObject (NSURLDownloadDelegate) + +/** + * Called immediately once the download has started. + */ +- (void) downloadDidBegin: (NSURLDownload *)download; + +/** + * Called when the download completes after having received all data. + */ +- (void) downloadDidFinish: (NSURLDownload *)download; + +/** + * Called when it's time to establish a name for the downloaded file ... + * the delegate may decide a name by inspecting the response.
+ * The delegate should call -setDestination:allowOverwrite: to set the + * filename to be used. + */ +- (void) download: (NSURLDownload *)download + decideDestinationWithSuggestedFilename: (NSString *)filename; + +/** + * Called when authentication of a request is cancelled. + */ +- (void) download: (NSURLDownload *)download + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +/** + * Called when the download has created the downloaded file. + */ +- (void) download: (NSURLDownload *)download + didCreateDestination: (NSString *)path; + +/** + * Called when the download fails. + */ +- (void) download: (NSURLDownload *)download didFailWithError: (NSError *)error; + +/** + * Called when an authentication challenge is received.
+ * The delegate should send -useCredential:forAuthenticationChallenge: or + * -continueWithoutCredentialForAuthenticationChallenge: or -cancel to + * the connection sender when done. + */ +- (void) download: (NSURLDownload *)download + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +/** + * Called when some data has been received. + */ +- (void) download: (NSURLDownload *)download + didReceiveDataOfLength: (unsigned)length; + +/** + * Called when a response is received.
+ * Multiple responses may be received on the same download (eg with server push) + * and the delegate should be prepared to treat each separately. + */ +- (void) download: (NSURLDownload *)download + didReceiveResponse: (NSURLResponse *)response; + +/** + * Called if the download file is encoded ... the delegate should return + * YES if the downloaded data is to be decoded, NO otherwise. + */ +- (BOOL) download: (NSURLDownload *)download + shouldDecodeSourceDataOfMIMEType: (NSString *)encodingType; + +/** + * Called when a download is resuming from previously stored data and + * a response has been received from the server.
+ * The startingBytes is the offset from which the downloaded data + * will actually commence ... and may be zero if the entire download + * must be redone. + */ +- (void) download: (NSURLDownload *)download + willResumeWithResponse: (NSURLResponse *)response + fromByte: (long long)startingByte; + +/** + * Called if a new request has to be sent due to redirection.
+ * Must return the request argument (or a modified copy of it) + * to have the process continue. + */ +- (NSURLRequest *) download: (NSURLDownload *)download + willSendRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)redirectResponse; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLError.h b/Headers/Foundation/NSURLError.h new file mode 100644 index 000000000..9f3c28899 --- /dev/null +++ b/Headers/Foundation/NSURLError.h @@ -0,0 +1,88 @@ +/* Interface for NSURLError for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLError_h_GNUSTEP_BASE_INCLUDE +#define __NSURLError_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSString; + +/** + * The domain for a URL error. + */ +extern NSString * const NSURLErrorDomain; + +/** + * Obtain the URL which caused the failure + */ +extern NSString * const NSErrorFailingURLStringKey; + +/** + * Error codes for URL errors + */ +enum +{ + NSURLErrorUnknown = -1, + NSURLErrorCancelled = -999, + NSURLErrorBadURL = -1000, + NSURLErrorTimedOut = -1001, + NSURLErrorUnsupportedURL = -1002, + NSURLErrorCannotFindHost = -1003, + NSURLErrorCannotConnectToHost = -1004, + NSURLErrorNetworkConnectionLost = -1005, + NSURLErrorDNSLookupFailed = -1006, + NSURLErrorHTTPTooManyRedirects = -1007, + NSURLErrorResourceUnavailable = -1008, + NSURLErrorNotConnectedToInternet = -1009, + NSURLErrorRedirectToNonExistentLocation = -1010, + NSURLErrorBadServerResponse = -1011, + NSURLErrorUserCancelledAuthentication = -1012, + NSURLErrorUserAuthenticationRequired = -1013, + NSURLErrorZeroByteResource = -1014, + NSURLErrorFileDoesNotExist = -1100, + NSURLErrorFileIsDirectory = -1101, + NSURLErrorNoPermissionsToReadFile = -1102, + NSURLErrorSecureConnectionFailed = -1200, + NSURLErrorServerCertificateHasBadDate = -1201, + NSURLErrorServerCertificateUntrusted = -1202, + NSURLErrorServerCertificateHasUnknownRoot = -1203, + NSURLErrorServerCertificateNotYetValid = -1204, + NSURLErrorClientCertificateRejected = -1205, + NSURLErrorCannotLoadFromNetwork = -2000, + + NSURLErrorCannotCreateFile = -3000, + NSURLErrorCannotOpenFile = -3001, + NSURLErrorCannotCloseFile = -3002, + NSURLErrorCannotWriteToFile = -3003, + NSURLErrorCannotRemoveFile = -3004, + NSURLErrorCannotMoveFile = -3005, + NSURLErrorDownloadDecodingFailedMidStream = -3006, + NSURLErrorDownloadDecodingFailedToComplete = -3007, +}; + +#endif +#endif diff --git a/Headers/Foundation/NSURLProtectionSpace.h b/Headers/Foundation/NSURLProtectionSpace.h new file mode 100644 index 000000000..347b25fb6 --- /dev/null +++ b/Headers/Foundation/NSURLProtectionSpace.h @@ -0,0 +1,129 @@ +/* Interface for NSURLProtectionSpace for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLProtectionSpace_h_GNUSTEP_BASE_INCLUDE +#define __NSURLProtectionSpace_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSString; + +extern NSString * const NSURLProtectionSpaceFTPProxy; /** An FTP proxy */ +extern NSString * const NSURLProtectionSpaceHTTPProxy; /** An HTTP proxy */ +extern NSString * const NSURLProtectionSpaceHTTPSProxy; /** An HTTPS proxy */ +extern NSString * const NSURLProtectionSpaceSOCKSProxy; /** A SOCKS proxy */ + +/** Default authentication */ +extern NSString * const NSURLAuthenticationMethodDefault; + +/** HTML form authentication */ +extern NSString * const NSURLAuthenticationMethodHTMLForm; + +/** HTTP Basic authentication */ +extern NSString * const NSURLAuthenticationMethodHTTPBasic; + +/** HTTP Digest authentication */ +extern NSString * const NSURLAuthenticationMethodHTTPDigest; + + +/** + * Class to encapsulate a protection space ... where authentication is + * required. + */ +@interface NSURLProtectionSpace : NSObject +{ +@private + void *_NSURLProtectionSpaceInternal; +} + +/** + * Returns the authentication method used for this protection space. + */ +- (NSString *) authenticationMethod; + +/** + * Returns the host (or proxy host) set in the receiver. + */ +- (NSString *) host; + +/** + * Initialises the receiver with host, port, and protocol identifying the + * protection space. For some protocols the realm identifies a space + * within the host, for others it may be nil. + */ +- (id) initWithHost: (NSString *)host + port: (int)port + protocol: (NSString *)protocol + realm: (NSString *)realm +authenticationMethod: (NSString *)authenticationMethod; + +/** + * This is like -initWithHost:port:protocol:realm:authenticationMethod: + * except that it uses a proxy host and proxy type rather than an actual + * host and a protocol. + */ +- (id) initWithProxyHost: (NSString *)host + port: (int)port + type: (NSString *)type + realm: (NSString *)realm + authenticationMethod: (NSString *)authenticationMethod; + +/** + * Returns a flag to indicate whether this protection space is on a proxy + * server or not. + */ +- (BOOL) isProxy; + +/** + * Returns the port set for this receiver or zero if none was set. + */ +- (int) port; + +/** + * Returns the protocol of the receiver or nil if it is a proxy. + */ +- (NSString *) protocol; + +/** + * Returns the proxy type set for the receiver or nil if it's not a proxy. + */ +- (NSString *) proxyType; + +/** + * Returns the realm (or nil) which was set in the receiver upon initialisation. + */ +- (NSString *) realm; + +/** + * Returns a flag to indicate whether the password for this protection space + * will be sent over a secure mechanism. + */ +- (BOOL) receivesCredentialSecurely; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLProtocol.h b/Headers/Foundation/NSURLProtocol.h new file mode 100644 index 000000000..8a8c2756b --- /dev/null +++ b/Headers/Foundation/NSURLProtocol.h @@ -0,0 +1,222 @@ +/* Interface for NSURLProtocol for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLProtocol_h_GNUSTEP_BASE_INCLUDE +#define __NSURLProtocol_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +#include + +@class NSCachedURLResponse; +@class NSError; +@class NSMutableURLRequest; +@class NSURLAuthenticationChallenge; +@class NSURLConnection; +@class NSURLProtocol; +@class NSURLRequest; +@class NSURLResponse; + + +/** + * Defines the API for NSURLProtocol loading + */ +@protocol NSURLProtocolClient + +/** + * Informs a client that a cached response is valid. + */ +- (void) URLProtocol: (NSURLProtocol *)protocol + cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse; + +/** + * Informs a client that loading of a request has failed. + */ +- (void) URLProtocol: (NSURLProtocol *)protocol + didFailWithError: (NSError *)error; + +/** + * Informs a client that data has been loaded. Only new data since the + * last call to this method must be provided. + */ +- (void) URLProtocol: (NSURLProtocol *)protocol + didLoadData: (NSData *)data; + +/** + * Informs a client that an authentication challenge has been received. + */ +- (void) URLProtocol: (NSURLProtocol *)protocol + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +/** + * Informs a client that a response for the current load has been created.
+ * Also supplies the policy to be used for caching the response. + */ +- (void) URLProtocol: (NSURLProtocol *)protocol + didReceiveResponse: (NSURLResponse *)response + cacheStoragePolicy: (NSURLCacheStoragePolicy)policy; + +/** + * Informs a client that a redirect has occurred.
+ */ +- (void) URLProtocol: (NSURLProtocol *)protocol + wasRedirectedToRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)redirectResponse; + + +/** + * Informs a client that loading of a request has successfully finished. + */ +- (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol; + +/** + * Informs a client that an authentication challenge has been cancelled. + */ +- (void) URLProtocol: (NSURLProtocol *)protocol + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge; + +@end + + +/** + *

Subclasses of NSURLProtocol implement basic handling of URL + * loading for specific protocols. The NSURLProtocol class + * itsself is a semi-abstract class giving the essential + * structure for the subclasses. + *

+ *

You never instantiate NSURLProtocol yourself ... it should only + * ever be done by other classes within mthe URL loading system. + *

+ */ +@interface NSURLProtocol : NSObject +{ +@private + void *_NSURLProtocolInternal; +} + +/** + * Allows subclasses to provide access to proptocol specific + * properties, returning the property of request stored by the + * name key or nil if no property had been stored using that + * key in the request. + */ ++ (id) propertyForKey: (NSString *)key inRequest: (NSURLRequest *)request; + +/** + * Registers the specified class so that it can be used to load requests.
+ * When the system is determining which class to use to handle a + * request it examines them in a most recently registered first order.
+ * The +canInitWithRequest: method is used to determine whether a class + * may be used to handle a particular request or not. + * Returns YES if registered (ie the class is an NSURLProtocol subclass), + * NO otherwise. + */ ++ (BOOL) registerClass: (Class)protocolClass; + +/** + * Allows subclasses to provide a way to set protocol specific properties, + * setting the property named key to value in the request. + */ ++ (void) setProperty: (id)value + forKey: (NSString *)key + inRequest: (NSMutableURLRequest *)request; + +/** + * Unregisters a class which was previously registered using the + * +registerClass: method. + */ ++ (void) unregisterClass: (Class)protocolClass; + +/** + * Returns the cachedResponse of the receiver. + */ +- (NSCachedURLResponse *) cachedResponse; + +/** + * Returns the client associated with the receiver. + */ +- (id ) client; + +/** + * Initialises the receiver with request, cachedResponse and client.
+ * The cachedResponse may be the result of a previous load of the + * request (in which case the protocol may validate and use it).
+ * The client is the object which receives messages about the progress + * of the load. + */ +- (id) initWithRequest: (NSURLRequest *)request + cachedResponse: (NSCachedURLResponse *)cachedResponse + client: (id )client; + +/** + * Returns the request handled by the receiver. + */ +- (NSURLRequest *) request; + +@end + +/** + * This category lists the methods which a subclass must implement + * in order to produce a working protocol. + */ +@interface NSURLProtocol (Subclassing) + +/** + * This method is called to decide whether a class can deal with + * the specified request. The abstract class implementation + * raises an exception. + */ ++ (BOOL) canInitWithRequest: (NSURLRequest *)request; + +/** + * Returns the 'canonical' version of the request.
+ * The canonical form is used to look up requests in the cache by + * checking for equality.
+ * The abnstract class implementation simply returns request. + */ ++ (NSURLRequest *) canonicalRequestForRequest: (NSURLRequest *)request; + +/** + * Compares two requests for equivalence for caching purposes.
+ * The abstract class implementaton just uses [NSObject-isEqual:] + */ ++ (BOOL) requestIsCacheEquivalent: (NSURLRequest *)a + toRequest: (NSURLRequest *)b; + +/** + * Starts loading of a request. + */ +- (void) startLoading; + +/** + * Stops loading of a request (eg when the load is cancelled). + */ +- (void) stopLoading; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLRequest.h b/Headers/Foundation/NSURLRequest.h new file mode 100644 index 000000000..e2addf86b --- /dev/null +++ b/Headers/Foundation/NSURLRequest.h @@ -0,0 +1,275 @@ +/* Interface for NSURLRequest for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLRequest_h_GNUSTEP_BASE_INCLUDE +#define __NSURLRequest_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + +@class NSData; +@class NSDate; +@class NSDictionary; +@class NSInputStream; +@class NSString; +@class NSURL; + +/** + * + * NSURLRequestUseProtocolCachePolicy + * + * Says that any protocol specific cache policy should be + * used ... this is the default. + * + * NSURLRequestReloadIgnoringCacheData + * + * Says the data should be re-loaded from source rather + * than any cached data being used, irrespective of any + * protocol standard. + * + * NSURLRequestReturnCacheDataElseLoad + * + * Says to use cached data if any is available, but to + * load from source if the cache is empty. Ignores any + * protocol specific logic (like cache aging). + * + * NSURLRequestReturnCacheDataDontLoad + * + * Says to use cached data if any is available, but to + * return nil without loading if the cache is empty. + * + * + */ +typedef enum +{ + NSURLRequestUseProtocolCachePolicy, + NSURLRequestReloadIgnoringCacheData, + NSURLRequestReturnCacheDataElseLoad, + NSURLRequestReturnCacheDataDontLoad, +} NSURLRequestCachePolicy; + + +/** + * This class encapsulates information about a request to load a + * URL, how to cache the results, and when to deal with a slow/hung + * load process by timing out. + */ +@interface NSURLRequest : NSObject +{ +@private + void *_NSURLRequestInternal; +} + +/* + * Returns an autoreleased instance initialised with the specified URL + * and with the default cache policy (NSURLRequestUseProtocolCachePolicy) + * and a sixty second timeout. + */ ++ (id) requestWithURL: (NSURL *)URL; + +/** + * Returns an autoreleased instance initialised with the specified URL, + * cachePolicy, and timeoutInterval. + */ ++ (id) requestWithURL: (NSURL *)URL + cachePolicy: (NSURLRequestCachePolicy)cachePolicy + timeoutInterval: (NSTimeInterval)timeoutInterval; + +/** + * Returns the cache policy associated with the receiver. + */ +- (NSURLRequestCachePolicy) cachePolicy; + +/** + * Initialises the reveiver with the specified URL + * and with the default cache policy (NSURLRequestUseProtocolCachePolicy) + * and a sixty second timeout. + */ +- (id) initWithURL: (NSURL *)URL; + +/** + * Initialises the receiver with the specified URL, + * cachePolicy, and timeoutInterval. + */ +- (id) initWithURL: (NSURL *)URL + cachePolicy: (NSURLRequestCachePolicy)cachePolicy + timeoutInterval: (NSTimeInterval)timeoutInterval; + +/** + * Returns the main document URL for the receiver.
+ * Currently unused.
+ * This is intended for use with frames and similar situations where + * a main document has a large number of subsidiary documents. + */ +- (NSURL *) mainDocumentURL; + +/** + * Returns the timeout interval associated with the receiver.
+ * This is a value in seconds specifying how long the load process + * may be inactive (waiting for data to arrive from the server) + * before the load is mconsidered to have failed due to a timeout. + */ +- (NSTimeInterval) timeoutInterval; + +/** + * Returns the URL associated with the receiver. + */ +- (NSURL *) URL; + +@end + + +/** + */ +@interface NSMutableURLRequest : NSURLRequest + +/** + * Sets the receiver's cache policy. + */ +- (void) setCachePolicy: (NSURLRequestCachePolicy)cachePolicy; + +/** + * Sets the receiver's main document. + */ +- (void) setMainDocumentURL: (NSURL *)URL; + +/** + * Sets the receiver's timeout policy. + */ +- (void) setTimeoutInterval: (NSTimeInterval)seconds; + +/** + * Sets the receiver's URL + */ +- (void) setURL: (NSURL *)URL; + +@end + + + +/** + * HTTP specific additions to NSURLRequest + */ +@interface NSURLRequest (NSHTTPURLRequest) + +/** + * Returns a dictionary of the HTTP header fields associated with the + * receiver. + */ +- (NSDictionary *) allHTTPHeaderFields; + +/** + * Returns the body of the reques ... this is the data sent in a POST + * request. + */ +- (NSData *) HTTPBody; + +#if OS_API_VERSION(100400,GS_API_LATEST) +/** + * Returns the currently set stream (if any) to be used to provide data + * to send as the request body.
+ * Of course, any attempt to modify this stream may mess up the load + * operation in progress. + */ +- (NSInputStream *) HTTPBodyStream; +#endif + +/** + * Returns the HTTP method assiciated with the receiver. + */ +- (NSString *) HTTPMethod; + +/** + * Returns a flag indicating whether this request should use standard + * cookie handling (sending of cookies with the request and storing + * any cookies returned in the response. + */ +- (BOOL) HTTPShouldHandleCookies; + +/** + * Returns the value for a particular HTTP header field (by case + * insensitive comparison) or nil if no such header is set. + */ +- (NSString *) valueForHTTPHeaderField: (NSString *)field; + +@end + + + +/** + */ +@interface NSMutableURLRequest (NSMutableHTTPURLRequest) + +/** + * Appends the value to the specified header field, automatically inserting + * a comman field delimiter if necessary. + */ +- (void) addValue: (NSString *)value forHTTPHeaderField: (NSString *)field; + +/** + * Sets all the string values in the supplied headerFields + * dictionary as header values in the receiver.
+ * Non-string values are ignored. + */ +- (void) setAllHTTPHeaderFields: (NSDictionary *)headerFields; + +#if OS_API_VERSION(100400,GS_API_LATEST) +/** + * Sets the request body to be the contents of the given stream.
+ * The stream should be unopened when it is set, and the load process + * for the request will open the stream and read its entire content + * forwarding it to the remote server.
+ * Clears any value previously set by -setHTTPBody: or -setHTTPBodyStream: + */ +- (void) setHTTPBodyStream: (NSInputStream *)inputStream; +#endif + +/** + * Sets the data to be sent as the body of the HTTP request.
+ * Clears any value previously set by -setHTTPBodyStream: or -setHTTPBody: + */ +- (void) setHTTPBody: (NSData *)data; + +/** + * Sets the method of the receiver. + */ +- (void) setHTTPMethod: (NSString *)method; + +/** + * Sets a flag to say whether cookies should automatically be added + * to the request and whether cookies in the response should be used. + */ +- (void) setHTTPShouldHandleCookies: (BOOL)should; + +/** + * Sets the value for the sapecified header field, replacing any + * previously set value. + */ +- (void) setValue: (NSString *)value forHTTPHeaderField: (NSString *)field; + +@end + +#endif +#endif diff --git a/Headers/Foundation/NSURLResponse.h b/Headers/Foundation/NSURLResponse.h new file mode 100644 index 000000000..43fd161f4 --- /dev/null +++ b/Headers/Foundation/NSURLResponse.h @@ -0,0 +1,120 @@ +/* Interface for NSURLResponse for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#ifndef __NSURLResponse_h_GNUSTEP_BASE_INCLUDE +#define __NSURLResponse_h_GNUSTEP_BASE_INCLUDE + +#include + +#if OS_API_VERSION(100200,GS_API_LATEST) && GS_API_VERSION(011300,GS_API_LATEST) + + +@class NSDictionary; +@class NSString; +@class NSURL; + +#define NSURLResponseUnknownLength ((long long)-1) + +/** + * The response to an NSURLRequest + */ +@interface NSURLResponse : NSObject +{ +@private + void *_NSURLResponseInternal; +} + +/** + * Returns the expected content length of the receiver or -1 if + * there is no idea of what the content length might be.
+ * This value is advisory, not a definitive length. + */ +- (long long) expectedContentLength; + +/** + * Initialises the receiver with the URL, MIMEType, expected length and + * text encoding name provided. + */ +- (id) initWithURL: (NSURL *)URL + MIMEType: (NSString *)MIMEType + expectedContentLength: (int)length + textEncodingName: (NSString *)name; + +/** + * Returns the receiver's MIME type. + */ +- (NSString *) MIMEType; + +/** + * Returns a suggested file name for storing the response data, with + * suggested names being found in the following order:
+ * + * content-disposition header + * last path component of URL + * host name from URL + * 'unknown' + * + * If possible, an extension based on the MIME type of the response + * is also appended.
+ * The result should always be a valid file name. + */ +- (NSString *) suggestedFilename; + +/** + * Returns the name of the character set used where response data is text + */ +- (NSString *) textEncodingName; + +/** + * Returns the receiver's URL. + */ +- (NSURL *) URL; + +@end + + +/** + * HTTP specific additions to an NSURLResponse + */ +@interface NSHTTPURLResponse : NSURLResponse + +/** + * Returns a string representation of a status code. + */ ++ (NSString *) localizedStringForStatusCode: (int)statusCode; + +/** + * Returns a dictionary containing all the HTTP header fields. + */ +- (NSDictionary *) allHeaderFields; + +/** + * Returns the HTTP status code for the response. + */ +- (int) statusCode; + +@end + +#endif +#endif diff --git a/Source/DocMakefile b/Source/DocMakefile index 4f2d08245..5dc814719 100644 --- a/Source/DocMakefile +++ b/Source/DocMakefile @@ -46,8 +46,8 @@ NSClassDescription.h \ NSCoder.h \ NSConnection.h \ NSData.h \ -NSDate.h \ NSDateFormatter.h \ +NSDate.h \ NSDebug.h \ NSDecimal.h \ NSDecimalNumber.h \ @@ -64,6 +64,8 @@ NSFormatter.h \ NSGeometry.h \ NSHashTable.h \ NSHost.h \ +NSHTTPCookie.h \ +NSHTTPCookieStorage.h \ NSIndexPath.h \ NSIndexSet.h \ NSInvocation.h \ @@ -80,12 +82,12 @@ NSNumberFormatter.h \ NSObjCRuntime.h \ NSObject.h \ NSPathUtilities.h \ -NSPort.h \ NSPortCoder.h \ +NSPort.h \ NSPortMessage.h \ NSPortNameServer.h \ -NSPropertyList.h \ NSProcessInfo.h \ +NSPropertyList.h \ NSProtocolChecker.h \ NSProxy.h \ NSRange.h \ @@ -97,16 +99,29 @@ NSStream.h \ NSString.h \ NSTask.h \ NSThread.h \ -NSTimeZone.h \ NSTimer.h \ +NSTimeZone.h \ +NSUndoManager.h \ +NSURLAuthenticationChallenge.h \ +NSURLCache.h \ +NSURLConnection.h \ +NSURLCredential.h \ +NSURLCredentialStorage.h \ +NSURLDownload.h \ +NSURLError.h \ NSURL.h \ NSURLHandle.h \ -NSUndoManager.h \ +NSURLProtectionSpace.h \ +NSURLProtocol.h \ +NSURLRequest.h \ +NSURLResponse.h \ NSUserDefaults.h \ NSValue.h \ NSXMLParser.h \ NSZone.h + + BaseAdditions_AGSDOC_FILES = \ ../Documentation/BaseAdditions.gsdoc \ GSCategories.h \ diff --git a/Source/GNUmakefile b/Source/GNUmakefile index 0a6174886..d5b7c21b6 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -150,6 +150,7 @@ NSArray.m \ NSAssertionHandler.m \ NSAutoreleasePool.m \ NSBundle.m \ +NSCachedURLResponse.m \ NSCalendarDate.m \ NSCallBacks.m \ NSCharacterSet.m \ @@ -178,6 +179,8 @@ NSFormatter.m \ NSGeometry.m \ NSHashTable.m \ NSHost.m \ +NSHTTPCookie.m \ +NSHTTPCookieStorage.m \ NSIndexPath.m \ NSIndexSet.m \ NSInvocation.m \ @@ -225,6 +228,16 @@ NSTimeZone.m \ NSUnarchiver.m \ NSUndoManager.m \ NSURL.m \ +NSURLAuthenticationChallenge.m \ +NSURLCache.m \ +NSURLCredential.m \ +NSURLConnection.m \ +NSURLCredentialStorage.m \ +NSURLDownload.m \ +NSURLProtectionSpace.m \ +NSURLProtocol.m \ +NSURLRequest.m \ +NSURLResponse.m \ NSURLHandle.m \ NSUserDefaults.m \ NSValue.m \ @@ -278,8 +291,8 @@ NSComparisonPredicate.h \ NSCompoundPredicate.h \ NSConnection.h \ NSData.h \ -NSDate.h \ NSDateFormatter.h \ +NSDate.h \ NSDebug.h \ NSDecimal.h \ NSDecimalNumber.h \ @@ -297,6 +310,8 @@ NSFormatter.h \ NSGeometry.h \ NSHashTable.h \ NSHost.h \ +NSHTTPCookie.h \ +NSHTTPCookieStorage.h \ NSIndexPath.h \ NSIndexSet.h \ NSInvocation.h \ @@ -313,8 +328,8 @@ NSNumberFormatter.h \ NSObjCRuntime.h \ NSObject.h \ NSPathUtilities.h \ -NSPort.h \ NSPortCoder.h \ +NSPort.h \ NSPortMessage.h \ NSPortNameServer.h \ NSPredicate.h \ @@ -332,11 +347,22 @@ NSStream.h \ NSString.h \ NSTask.h \ NSThread.h \ -NSTimeZone.h \ NSTimer.h \ +NSTimeZone.h \ +NSUndoManager.h \ +NSURLAuthenticationChallenge.h \ +NSURLCache.h \ +NSURLConnection.h \ +NSURLCredential.h \ +NSURLCredentialStorage.h \ +NSURLDownload.h \ +NSURLError.h \ NSURL.h \ NSURLHandle.h \ -NSUndoManager.h \ +NSURLProtectionSpace.h \ +NSURLProtocol.h \ +NSURLRequest.h \ +NSURLResponse.h \ NSUserDefaults.h \ NSUtilities.h \ NSValue.h \ diff --git a/Source/NSHTTPCookie.m b/Source/NSHTTPCookie.m new file mode 100644 index 000000000..95367c86c --- /dev/null +++ b/Source/NSHTTPCookie.m @@ -0,0 +1,173 @@ +/* Implementation for NSHTTPCookie for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" +#include "Foundation/NSSet.h" + +NSString * const NSHTTPCookieComment = @"NSHTTPCookieComment"; +NSString * const NSHTTPCookieCommentURL = @"NSHTTPCookieCommentURL"; +NSString * const NSHTTPCookieDiscard = @"NSHTTPCookieDiscard"; +NSString * const NSHTTPCookieDomain = @"NSHTTPCookieDomain"; +NSString * const NSHTTPCookieExpires = @"NSHTTPCookieExpires"; +NSString * const NSHTTPCookieMaximumAge = @"NSHTTPCookieMaximumAge"; +NSString * const NSHTTPCookieName = @"NSHTTPCookieName"; +NSString * const NSHTTPCookieOriginURL = @"NSHTTPCookieOriginURL"; +NSString * const NSHTTPCookiePath = @"NSHTTPCookiePath"; +NSString * const NSHTTPCookiePort = @"NSHTTPCookiePort"; +NSString * const NSHTTPCookieSecure = @"NSHTTPCookieSecure"; +NSString * const NSHTTPCookieValue = @"NSHTTPCookieValue"; +NSString * const NSHTTPCookieVersion = @"NSHTTPCookieVersion"; + +// Internal data storage +typedef struct { + NSDictionary *_properties; +} Internal; + +typedef struct { + @defs(NSHTTPCookie) +} priv; +#define this ((Internal*)(((priv*)self)->_NSHTTPCookieInternal)) +#define inst ((Internal*)(((priv*)o)->_NSHTTPCookieInternal)) + +@implementation NSHTTPCookie + ++ (id) allocWithZone: (NSZone*)z +{ + NSHTTPCookie *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSHTTPCookieInternal = NSZoneMalloc(z, sizeof(Internal)); + } + return o; +} + ++ (id) cookieWithProperties: (NSDictionary *)properties +{ + NSHTTPCookie *o; + + o = [[self alloc] initWithProperties: properties]; + return AUTORELEASE(o); +} + ++ (NSArray *) cookiesWithResponseHeaderFields: (NSDictionary *)headerFields + forURL: (NSURL *)URL +{ + NSMutableArray *a = nil; + + [self notImplemented: _cmd]; // FIXME + return a; +} + ++ (NSDictionary *) requestHeaderFieldsWithCookies: (NSArray *)cookies +{ + NSMutableDictionary *d = nil; + + [self notImplemented: _cmd]; // FIXME + return d; +} + +- (NSString *) comment +{ + return [this->_properties objectForKey: NSHTTPCookieComment]; +} + +- (NSURL *) commentURL +{ + return [this->_properties objectForKey: NSHTTPCookieCommentURL]; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->_properties); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (NSString *) domain +{ + return [this->_properties objectForKey: NSHTTPCookieDomain]; +} + +- (NSDate *) expiresDate +{ + return [this->_properties objectForKey: NSHTTPCookieExpires]; +} + +- (id) initWithProperties: (NSDictionary *)properties +{ + if ((self = [super init]) != nil) + { + this->_properties = [properties copy]; + // FIXME ... parse and validate + } + return self; +} + +- (BOOL) isSecure +{ + return [[this->_properties objectForKey: NSHTTPCookieSecure] boolValue]; +} + +- (BOOL) isSessionOnly +{ + return [[this->_properties objectForKey: NSHTTPCookieDiscard] boolValue]; +} + +- (NSString *) name +{ + return [this->_properties objectForKey: NSHTTPCookieName]; +} + +- (NSString *) path +{ + return [this->_properties objectForKey: NSHTTPCookiePath]; +} + +- (NSArray *) portList +{ + return [[this->_properties objectForKey: NSHTTPCookiePort] + componentsSeparatedByString: @","]; +} + +- (NSDictionary *) properties +{ + return this->_properties; +} + +- (NSString *) value +{ + return [this->_properties objectForKey: NSHTTPCookieValue]; +} + +- (unsigned) version +{ + return [[this->_properties objectForKey: NSHTTPCookieVersion] intValue]; +} + +@end + diff --git a/Source/NSHTTPCookieStorage.m b/Source/NSHTTPCookieStorage.m new file mode 100644 index 000000000..7a13e52f3 --- /dev/null +++ b/Source/NSHTTPCookieStorage.m @@ -0,0 +1,138 @@ +/* Implementation for NSHTTPCookieStorage for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" +#include "Foundation/NSSet.h" + +NSString * const NSHTTPCookieManagerAcceptPolicyChangedNotification + = @"NSHTTPCookieManagerAcceptPolicyChangedNotification"; + +NSString * const NSHTTPCookieManagerCookiesChangedNotification + = @"NSHTTPCookieManagerCookiesChangedNotification"; + +// Internal data storage +typedef struct { + NSHTTPCookieAcceptPolicy _policy; + NSMutableSet *_cookies; +} Internal; + +typedef struct { + @defs(NSHTTPCookieStorage) +} priv; +#define this ((Internal*)(((priv*)self)->_NSHTTPCookieStorageInternal)) +#define inst ((Internal*)(((priv*)o)->_NSHTTPCookieStorageInternal)) + + +/* FIXME + * handle persistent storage and use policies. + */ +@implementation NSHTTPCookieStorage + +static NSHTTPCookieStorage *storage = nil; + ++ (id) allocWithZone: (NSZone*)z +{ + return RETAIN([self sharedHTTPCookieStorage]); +} + ++ (NSHTTPCookieStorage *) sharedHTTPCookieStorage +{ + if (storage == nil) + { + [gnustep_global_lock lock]; + if (storage == nil) + { + NSHTTPCookieStorage *o; + + o = (NSHTTPCookieStorage*) + NSAllocateObject(self, 0, NSDefaultMallocZone()); + o->_NSHTTPCookieStorageInternal = (Internal*) + NSZoneMalloc(NSDefaultMallocZone(), sizeof(Internal)); + inst->_policy = NSHTTPCookieAcceptPolicyAlways; + inst->_cookies = [NSMutableSet new]; + storage = o; + } + [gnustep_global_lock unlock]; + } + return storage; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->_cookies); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (NSHTTPCookieAcceptPolicy) cookieAcceptPolicy +{ + return this->_policy; +} + +- (NSArray *) cookies +{ + return [this->_cookies allObjects]; +} + +- (NSArray *) cookiesForURL: (NSURL *)URL +{ + [self notImplemented: _cmd]; // FIXME + return nil; +} + +- (void) deleteCookie: (NSHTTPCookie *)cookie +{ + [this->_cookies removeObject: cookie]; +} + +- (void) setCookie: (NSHTTPCookie *)cookie +{ + [this->_cookies addObject: cookie]; +} + +- (void) setCookieAcceptPolicy: (NSHTTPCookieAcceptPolicy)cookieAcceptPolicy +{ + this->_policy = cookieAcceptPolicy; +} + +- (void) setCookies: (NSArray *)cookies + forURL: (NSURL *)URL + mainDocumentURL: (NSURL *)mainDocumentURL +{ + unsigned count = [cookies count]; + + while (count-- > 0) + { + NSHTTPCookie *c = [cookies objectAtIndex: count]; + + // FIXME check domain matches + [this->_cookies addObject: c]; + } +} + +@end + diff --git a/Source/NSMapTable.m b/Source/NSMapTable.m index 26bd94da5..1653b7553 100644 --- a/Source/NSMapTable.m +++ b/Source/NSMapTable.m @@ -21,7 +21,8 @@ * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free - * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA. + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02111 USA. * * NSMapTable class reference * $Date$ $Revision$ diff --git a/Source/NSSet.m b/Source/NSSet.m index 18c6e1277..e2fc8653e 100644 --- a/Source/NSSet.m +++ b/Source/NSSet.m @@ -18,7 +18,8 @@ You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA. + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. NSSet class reference $Date$ $Revision$ diff --git a/Source/NSURL.m b/Source/NSURL.m index 4011a6092..bd639d778 100644 --- a/Source/NSURL.m +++ b/Source/NSURL.m @@ -1,4 +1,4 @@ -/** NSUrl.m - Class NSURL +/** NSURL.m - Class NSURL Copyright (C) 1999 Free Software Foundation, Inc. Written by: Manuel Guesdon @@ -52,6 +52,9 @@ function may be incorrect #include "Foundation/NSValue.h" #include "Foundation/NSZone.h" +NSString * const NSURLErrorDomain = @"NSURLErrorDomain"; +NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey"; + /* * Structure describing a URL. * All the char* fields may be NULL pointers, except path, which diff --git a/Source/NSURLAuthenticationChallenge.m b/Source/NSURLAuthenticationChallenge.m new file mode 100644 index 000000000..14dcebdf1 --- /dev/null +++ b/Source/NSURLAuthenticationChallenge.m @@ -0,0 +1,131 @@ +/* Implementation for NSURLAuthenticationChallenge for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + + +// Internal data storage +typedef struct { + NSURLProtectionSpace *space; + NSURLCredential *credential; + int previousFailureCount; + NSURLResponse *response; + NSError *error; + id sender; +} Internal; + +#define this ((Internal*)(self->_NSURLAuthenticationChallengeInternal)) + +@implementation NSURLAuthenticationChallenge + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLAuthenticationChallenge *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLAuthenticationChallengeInternal + = NSZoneMalloc(z, sizeof(Internal)); + } + return o; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->space); + RELEASE(this->credential); + RELEASE(this->response); + RELEASE(this->error); + RELEASE(this->sender); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (NSError *) error +{ + return this->error; +} + +- (NSURLResponse *) failureResponse +{ + return this->response; +} + +- (id) initWithAuthenticationChallenge: + (NSURLAuthenticationChallenge *)challenge + sender: + (id)sender +{ + return [self initWithProtectionSpace: [challenge protectionSpace] + proposedCredential: [challenge proposedCredential] + previousFailureCount: [challenge previousFailureCount] + failureResponse: [challenge failureResponse] + error: [challenge error] + sender: sender]; +} + +- (id) initWithProtectionSpace: (NSURLProtectionSpace *)space + proposedCredential: (NSURLCredential *)credential + previousFailureCount: (int)previousFailureCount + failureResponse: (NSURLResponse *)response + error: (NSError *)error + sender: (id)sender +{ + if ((self = [super init]) != nil) + { + this->space = [space copy]; + this->credential = [credential copy]; + this->response = [response copy]; + this->error = [error copy]; + this->sender = RETAIN(sender); + this->previousFailureCount = previousFailureCount; + } + return self; +} + +- (int) previousFailureCount +{ + return this->previousFailureCount; +} + +- (NSURLCredential *) proposedCredential +{ + return this->credential; +} + +- (NSURLProtectionSpace *) protectionSpace +{ + return this->space; +} + +- (id) sender +{ + return this->sender; +} + +@end + diff --git a/Source/NSURLCache.m b/Source/NSURLCache.m new file mode 100644 index 000000000..36a9b5cc7 --- /dev/null +++ b/Source/NSURLCache.m @@ -0,0 +1,216 @@ +/* Implementation for NSURLCache for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +// FIXME ... locking and disk storage needed +typedef struct { + unsigned diskCapacity; + unsigned memoryCapacity; + unsigned diskUsage; + unsigned memoryUsage; + NSString *path; + NSMutableDictionary *memory; +} Internal; + +typedef struct { + @defs(NSURLCache) +} priv; +#define this ((Internal*)(((priv*)self)->_NSURLCacheInternal)) +#define inst ((Internal*)(((priv*)o)->_NSURLCacheInternal)) + + +static NSURLCache *shared = nil; + +@implementation NSURLCache + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLCache *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLCacheInternal = NSZoneMalloc(z, sizeof(Internal)); + memset(o->_NSURLCacheInternal, '\0', sizeof(Internal)); + } + return o; +} + ++ (void) setSharedURLCache: (NSURLCache *)cache +{ + [gnustep_global_lock lock]; + ASSIGN(shared, cache); + [gnustep_global_lock unlock]; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->memory); + RELEASE(this->path); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + ++ (NSURLCache *) sharedURLCache +{ + NSURLCache *c; + + [gnustep_global_lock lock]; + if (shared == nil) + { + NSString *path = nil; + +// FIXME user-library-path/Caches/current-app-name + + shared = [[self alloc] initWithMemoryCapacity: 4 * 1024 * 1024 + diskCapacity: 20 * 1024 * 1024 + diskPath: path]; + + } + c = RETAIN(shared); + [gnustep_global_lock unlock]; + return AUTORELEASE(c); +} + +- (NSCachedURLResponse *) cachedResponseForRequest: (NSURLRequest *)request +{ + // FIXME ... handle disk cache + return [this->memory objectForKey: request]; +} + +- (unsigned) currentDiskUsage +{ + return this->diskUsage; +} + +- (unsigned) currentMemoryUsage +{ + return this->memoryUsage; +} + +- (unsigned) diskCapacity +{ + return this->diskCapacity; +} + +- (id) initWithMemoryCapacity: (unsigned)memoryCapacity + diskCapacity: (unsigned)diskCapacity + diskPath: (NSString *)path +{ + if ((self = [super init]) != nil) + { + this->diskUsage = 0; + this->diskCapacity = diskCapacity; + this->memoryUsage = 0; + this->memoryCapacity = memoryCapacity; + this->path = [path copy]; + this->memory = [NSMutableDictionary new]; + } + return self; +} + +- (unsigned) memoryCapacity +{ + return this->memoryCapacity; +} + +- (void) removeAllCachedResponses +{ + // FIXME ... disk storage + [this->memory removeAllObjects]; + this->diskUsage = 0; + this->memoryUsage = 0; +} + +- (void) removeCachedResponseForRequest: (NSURLRequest *)request +{ + NSCachedURLResponse *item = [self cachedResponseForRequest: request]; + + if (item != nil) + { + // FIXME ... disk storage + this->memoryUsage -= [[item data] length]; + [this->memory removeObjectForKey: request]; + } +} + +- (void) setDiskCapacity: (unsigned)diskCapacity +{ + // FIXME +} + +- (void) setMemoryCapacity: (unsigned)memoryCapacity +{ + // FIXME +} + +- (void) storeCachedResponse: (NSCachedURLResponse *)cachedResponse + forRequest: (NSURLRequest *)request +{ + switch ([cachedResponse storagePolicy]) + { + case NSURLCacheStorageAllowed: +// FIXME ... maybe on disk? + + case NSURLCacheStorageAllowedInMemoryOnly: + { + unsigned size = [[cachedResponse data] length]; + + if (size < this->memoryCapacity) + { + NSCachedURLResponse *old; + + old = [this->memory objectForKey: request]; + if (old != nil) + { + this->memoryUsage -= [[old data] length]; + [this->memory removeObjectForKey: request]; + } + while (this->memoryUsage + size > this->memoryCapacity) + { +// FIXME ... should delete least recently used. + [self removeCachedResponseForRequest: + [[this->memory allKeys] lastObject]]; + } + [this->memory setObject: cachedResponse forKey: request]; + this->memoryUsage += size; + } + } + break; + + case NSURLCacheStorageNotAllowed: + break; + + default: + [NSException raise: NSInternalInconsistencyException + format: @"storing cached response with bad policy (%d)", + [cachedResponse storagePolicy]]; + } +} + +@end + diff --git a/Source/NSURLConnection.m b/Source/NSURLConnection.m new file mode 100644 index 000000000..686b83383 --- /dev/null +++ b/Source/NSURLConnection.m @@ -0,0 +1,252 @@ +/* Implementation for NSURLConnection for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +@interface GSURLConnection : NSObject +{ +@public + NSURLConnection *_parent; // Not retained + NSURLRequest *_request; + NSURLProtocol *_protocol; + id _delegate; +} +@end + +typedef struct { + @defs(NSURLConnection) +} priv; +#define this ((GSURLConnection*)(((priv*)self)->_NSURLConnectionInternal)) +#define inst ((GSURLConnection*)(((priv*)o)->_NSURLConnectionInternal)) + +@implementation NSURLConnection + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLConnection *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLConnectionInternal + = NSAllocateObject([GSURLConnection class], 0, z); + inst->_parent = o; + } + return o; +} + ++ (BOOL) canHandleRequest: (NSURLRequest *)request +{ + // FIXME + return NO; +} + ++ (NSURLConnection *) connectionWithRequest: (NSURLRequest *)request + delegate: (id)delegate +{ + NSURLConnection *o = [self alloc]; + + o = [o initWithRequest: request delegate: delegate]; + return AUTORELEASE(o); +} + +- (void) dealloc +{ + RELEASE(this); + [super dealloc]; +} + +- (void) cancel +{ + [this->_protocol stopLoading]; + DESTROY(this->_protocol); +} + +- (id) initWithRequest: (NSURLRequest *)request delegate: (id)delegate +{ + if ((self = [super init]) != nil) + { + this->_request = [request copy]; + this->_delegate = [delegate retain]; + // FIXME ... start connection + } + return self; +} + +@end + + + +@implementation NSObject (NSURLConnectionDelegate) + +- (void) connection: (NSURLConnection *)connection + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ +} + +- (void) connection: (NSURLConnection *)connection + didFailWithError: (NSError *)error +{ +} + +- (void) connectionDidFinishLoading: (NSURLConnection *)connection +{ +} + +- (void) connection: (NSURLConnection *)connection + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ + [[challenge sender] + continueWithoutCredentialForAuthenticationChallenge: challenge]; +} + +- (void) connection: (NSURLConnection *)connection + didReceiveData: (NSData *)data +{ +} + +- (void) connection: (NSURLConnection *)connection + didReceiveResponse: (NSURLResponse *)response +{ +} + +- (NSCachedURLResponse *) connection: (NSURLConnection *)connection + willCacheResponse: (NSCachedURLResponse *)cachedResponse +{ + return cachedResponse; +} + +- (NSURLRequest *) connection: (NSURLConnection *)connection + willSendRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)response +{ + return request; +} + +@end + + + +@implementation NSURLConnection (NSURLConnectionSynchronousLoading) + ++ (NSData *) sendSynchronousRequest: (NSURLRequest *)request + returningResponse: (NSURLResponse **)response + error: (NSError **)error +{ + NSData *data = nil; + + if ([self canHandleRequest: request] == YES) + { + NSURLConnection *conn = [self alloc]; + + conn = [conn initWithRequest: request delegate: nil]; + // FIXME ... handle load and get results; + } + return data; +} + +@end + + +@implementation GSURLConnection + +- (void) dealloc +{ + RELEASE(_protocol); + RELEASE(_request); + RELEASE(_delegate); + [super dealloc]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse +{ + +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didFailWithError: (NSError *)error +{ + [_delegate connection: _parent didFailWithError: error]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didLoadData: (NSData *)data +{ + [_delegate connection: _parent didReceiveData: data]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ + [_delegate connection: _parent didReceiveAuthenticationChallenge: challenge]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didReceiveResponse: (NSURLResponse *)response + cacheStoragePolicy: (NSURLCacheStoragePolicy)policy +{ + [_delegate connection: _parent didReceiveResponse: response]; + if (policy == NSURLCacheStorageAllowed + || policy == NSURLCacheStorageAllowedInMemoryOnly) + { + + // FIXME ... cache response here + } +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + wasRedirectedToRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)redirectResponse +{ + request = [_delegate connection: _parent + willSendRequest: request + redirectResponse: redirectResponse]; + // If we have been cancelled, our protocol will be nil + if (_protocol != nil) + { + if (request == nil) + { + [_delegate connectionDidFinishLoading: _parent]; + } + else + { + DESTROY(_protocol); + // FIXME start new request loading + } + } +} + +- (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol +{ + [_delegate connectionDidFinishLoading: _parent]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ + [_delegate connection: _parent didCancelAuthenticationChallenge: challenge]; +} + +@end + diff --git a/Source/NSURLCredential.m b/Source/NSURLCredential.m new file mode 100644 index 000000000..03d851fb7 --- /dev/null +++ b/Source/NSURLCredential.m @@ -0,0 +1,133 @@ +/* Implementation for NSURLCredential for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +// Internal data storage +typedef struct { + NSString *user; + NSString *password; + NSURLCredentialPersistence persistence; + BOOL hasPassword; +} Internal; + +#define this ((Internal*)(self->_NSURLCredentialInternal)) + +@implementation NSURLCredential + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLCredential *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLCredentialInternal = NSZoneMalloc(z, sizeof(Internal)); + } + return o; +} + ++ (NSURLCredential *) credentialWithUser: (NSString *)user + password: (NSString *)password + persistence: (NSURLCredentialPersistence)persistence +{ + NSURLCredential *o = [self alloc]; + + o = [o initWithUser: user password: password persistence: persistence]; + return AUTORELEASE(o); +} + +- (id) copyWithZone: (NSZone*)z +{ + NSURLCredential *o; + + if (NSShouldRetainWithZone(self, z) == YES) + { + o = RETAIN(self); + } + else + { + o = [[self class] allocWithZone: z]; + o = [o initWithUser: this->user + password: this->password + persistence: this->persistence]; + } + return o; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->user); + RELEASE(this->password); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (BOOL) hasPassword +{ + return this->hasPassword; +} + +- (id) initWithUser: (NSString *)user + password: (NSString *)password + persistence: (NSURLCredentialPersistence)persistence +{ + if (user == nil) + { + RELEASE(self); + return nil; + } + if ((self = [super init]) != nil) + { + this->user = [user copy]; + this->password = [password copy]; + this->persistence = persistence; + this->hasPassword = (this->password == nil) ? NO : YES; + if (persistence == NSURLCredentialPersistencePermanent) + { + // FIXME ... should check to see if we really have a password + } + } + return self; +} + +- (NSString *) password +{ + return this->password; +} + +- (NSURLCredentialPersistence) persistence +{ + return this->persistence; +} + +- (NSString *) user +{ + return this->user; +} + +@end + diff --git a/Source/NSURLCredentialStorage.m b/Source/NSURLCredentialStorage.m new file mode 100644 index 000000000..b09c6eea0 --- /dev/null +++ b/Source/NSURLCredentialStorage.m @@ -0,0 +1,193 @@ +/* Implementation for NSURLCredentialStorage for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +NSString *const NSURLCredentialStorageChangedNotification + = @"NSURLCredentialStorageChangedNotification"; + +// Internal data storage +typedef struct { + NSMutableDictionary *credentials; + NSMutableDictionary *defaults; +} Internal; + +#define this ((Internal*)(self->_NSURLCredentialStorageInternal)) + +@implementation NSURLCredentialStorage + +static NSURLCredentialStorage *storage = nil; + ++ (id) allocWithZone: (NSZone*)z +{ + return RETAIN([self sharedCredentialStorage]); +} + ++ (NSURLCredentialStorage *) sharedCredentialStorage +{ + if (storage == nil) + { + [gnustep_global_lock lock]; + if (storage == nil) + { + NSURLCredentialStorage *o; + + o = (NSURLCredentialStorage*) + NSAllocateObject(self, 0, NSDefaultMallocZone()); + o->_NSURLCredentialStorageInternal = (Internal*) + NSZoneMalloc(NSDefaultMallocZone(), sizeof(Internal)); + ((Internal*)(o->_NSURLCredentialStorageInternal))->credentials + = [NSMutableDictionary new]; + ((Internal*)(o->_NSURLCredentialStorageInternal))->defaults + = [NSMutableDictionary new]; + storage = o; + } + [gnustep_global_lock unlock]; + } + return storage; +} + +- (NSDictionary *) allCredentials +{ + NSMutableDictionary *all; + NSEnumerator *enumerator; + NSURLProtectionSpace *space; + + all = [NSMutableDictionary dictionaryWithCapacity: [this->credentials count]]; + enumerator = [this->credentials keyEnumerator]; + while ((space = [enumerator nextObject]) != nil) + { + NSDictionary *info = [[this->credentials objectForKey: space] copy]; + + [all setObject: info forKey: space]; + RELEASE(info); + } + return all; +} + +- (NSDictionary *) credentialsForProtectionSpace: (NSURLProtectionSpace *)space +{ + return AUTORELEASE([[this->credentials objectForKey: space] copy]); +} + +- (void) dealloc +{ + [NSException raise: NSInternalInconsistencyException + format: @"Attempt to deallocate credential storage"]; + GSNOSUPERDEALLOC; +} + +- (NSURLCredential *) defaultCredentialForProtectionSpace: + (NSURLProtectionSpace *)space +{ + return [this->defaults objectForKey: space]; +} + +// Should never be called. +- (id) init +{ + RELEASE(self); + return nil; +} + +- (void) removeCredential: (NSURLCredential *)credential + forProtectionSpace: (NSURLProtectionSpace *)space +{ + if (credential == nil || ![credential isKindOfClass: [NSURLCredential class]]) + { + [NSException raise: NSInvalidArgumentException + format: @"[%@-%@] nil or bad class for credential", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + } + if (space == nil || ![space isKindOfClass: [NSURLProtectionSpace class]]) + { + [NSException raise: NSInvalidArgumentException + format: @"[%@-%@] nil or bad class for space", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + } + [[this->credentials objectForKey: space] + removeObjectForKey: [credential user]]; +} + +/** + * Sets credential in the storage for the protection space specified.
+ * This replaces any old value with the same username. + */ +- (void) setCredential: (NSURLCredential *)credential + forProtectionSpace: (NSURLProtectionSpace *)space +{ + NSMutableDictionary *info; + + if (credential == nil || ![credential isKindOfClass: [NSURLCredential class]]) + { + [NSException raise: NSInvalidArgumentException + format: @"[%@-%@] nil or bad class for credential", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + } + if (space == nil || ![space isKindOfClass: [NSURLProtectionSpace class]]) + { + [NSException raise: NSInvalidArgumentException + format: @"[%@-%@] nil or bad class for space", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + } + info = [this->credentials objectForKey: space]; + if (info == nil) + { + info = [NSMutableDictionary new]; + [this->credentials setObject: info forKey: space]; + RELEASE(info); + } + [info setObject: credential forKey: [credential user]]; +} + +/** + * Sets the default credential for the protection space. Also calls + * -setCredential:forProtectionSpace: if the credential has not already + * been set in space. + */ +- (void) setDefaultCredential: (NSURLCredential *)credential + forProtectionSpace: (NSURLProtectionSpace *)space +{ + if (credential == nil || ![credential isKindOfClass: [NSURLCredential class]]) + { + [NSException raise: NSInvalidArgumentException + format: @"[%@-%@] nil or bad class for credential", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + } + if (space == nil || ![space isKindOfClass: [NSURLProtectionSpace class]]) + { + [NSException raise: NSInvalidArgumentException + format: @"[%@-%@] nil or bad class for space", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; + } + [this->defaults setObject: credential forKey: space]; + if ([[this->credentials objectForKey: space] objectForKey: [credential user]] + != credential) + { + [self setCredential: credential forProtectionSpace: space]; + } +} + +@end + diff --git a/Source/NSURLDownload.m b/Source/NSURLDownload.m new file mode 100644 index 000000000..16e8e8e30 --- /dev/null +++ b/Source/NSURLDownload.m @@ -0,0 +1,279 @@ +/* Implementation for NSURLDownload for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + + +#include "GSURLPrivate.h" + +@interface GSURLDownload : NSObject +{ +@public + NSURLDownload *_parent; // Not retained + NSURLRequest *_request; + NSURLProtocol *_protocol; + NSData *_resumeData; + NSString *_path; + id _delegate; + BOOL _deletesFileUponFailure; + BOOL _allowOverwrite; +} +@end + +typedef struct { + @defs(NSURLDownload) +} priv; +#define this ((GSURLDownload*)(((priv*)self)->_NSURLDownloadInternal)) +#define inst ((GSURLDownload*)(((priv*)o)->_NSURLDownloadInternal)) + +@implementation NSURLDownload + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLDownload *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLDownloadInternal + = NSAllocateObject([GSURLDownload class], 0, z); + inst->_parent = o; + } + return o; +} + ++ (BOOL) canResumeDownloadDecodedWithEncodingMIMEType: (NSString *)MIMEType +{ + return NO; // FIXME +} + +- (void) dealloc +{ + RELEASE(this); + [super dealloc]; +} + +- (void) cancel +{ + [this->_protocol stopLoading]; + DESTROY(this->_protocol); + // FIXME +} + +- (BOOL) deletesFileUponFailure +{ + return this->_deletesFileUponFailure; +} + +- (id) initWithRequest: (NSURLRequest *)request delegate: (id)delegate +{ + NSData *resumeData = nil; + +// FIXME ... build resume data from request. + return [self initWithResumeData: resumeData delegate: delegate path: nil]; +} + +- (id) initWithResumeData: (NSData *)resumeData + delegate: (id)delegate + path: (NSString *)path +{ + if ((self = [super init]) != nil) + { + this->_resumeData = [resumeData copy]; + this->_delegate = [delegate retain]; + this->_path = [path copy]; + // FIXME ... start connection + } + return self; +} + +- (NSURLRequest *) request +{ + return this->_request; +} + +- (NSData *) resumeData +{ + return nil; // FIXME +} + +- (void) setDeletesFileUponFailure: (BOOL)deletesFileUponFailure +{ + this->_deletesFileUponFailure = deletesFileUponFailure; +} + +- (void) setDestination: (NSString *)path allowOverwrite: (BOOL)allowOverwrite +{ + ASSIGN(this->_path, path); + this->_allowOverwrite = allowOverwrite; +} + +@end + +@implementation NSObject (NSURLDownloadDelegate) + +- (void) downloadDidBegin: (NSURLDownload *)download +{ +} + +- (void) downloadDidFinish: (NSURLDownload *)download +{ +} + +- (void) download: (NSURLDownload *)download + decideDestinationWithSuggestedFilename: (NSString *)filename +{ +} + +- (void) download: (NSURLDownload *)download + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ +} + +- (void) download: (NSURLDownload *)download + didCreateDestination: (NSString *)path +{ +} + +- (void) download: (NSURLDownload *)download didFailWithError: (NSError *)error +{ +} + +- (void) download: (NSURLDownload *)download + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ +} + +- (void) download: (NSURLDownload *)download + didReceiveDataOfLength: (unsigned)length +{ +} + +- (void) download: (NSURLDownload *)download + didReceiveResponse: (NSURLResponse *)response +{ +} + +- (BOOL) download: (NSURLDownload *)download + shouldDecodeSourceDataOfMIMEType: (NSString *)encodingType +{ + return NO; +} + +- (void) download: (NSURLDownload *)download + willResumeWithResponse: (NSURLResponse *)response + fromByte: (long long)startingByte +{ +} + +- (NSURLRequest *) download: (NSURLDownload *)download + willSendRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)redirectResponse +{ + return request; +} + +@end + + +@implementation GSURLDownload + +- (void) dealloc +{ + RELEASE(_protocol); + RELEASE(_resumeData); + RELEASE(_request); + RELEASE(_delegate); + RELEASE(_path); + [super dealloc]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse +{ + +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didFailWithError: (NSError *)error +{ + [_delegate download: _parent didFailWithError: error]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didLoadData: (NSData *)data +{ +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ + [_delegate download: _parent didReceiveAuthenticationChallenge: challenge]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didReceiveResponse: (NSURLResponse *)response + cacheStoragePolicy: (NSURLCacheStoragePolicy)policy +{ + [_delegate download: _parent didReceiveResponse: response]; + if (policy == NSURLCacheStorageAllowed + || policy == NSURLCacheStorageAllowedInMemoryOnly) + { + + // FIXME ... cache response here + } +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + wasRedirectedToRequest: (NSURLRequest *)request + redirectResponse: (NSURLResponse *)redirectResponse +{ + request = [_delegate download: _parent + willSendRequest: request + redirectResponse: redirectResponse]; + // If we have been cancelled, our protocol will be nil + if (_protocol != nil) + { + if (request == nil) + { + [_delegate downloadDidFinish: _parent]; + } + else + { + DESTROY(_protocol); + // FIXME start new request loading + } + } +} + +- (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol +{ + [_delegate downloadDidFinish: _parent]; +} + +- (void) URLProtocol: (NSURLProtocol *)protocol + didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge +{ + [_delegate download: _parent didCancelAuthenticationChallenge: challenge]; +} + +@end + diff --git a/Source/NSURLHandle.m b/Source/NSURLHandle.m index 9c4b5512b..f06b93be0 100644 --- a/Source/NSURLHandle.m +++ b/Source/NSURLHandle.m @@ -20,21 +20,17 @@ You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA. + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. NSURLHandle class reference $Date$ $Revision$ */ -#include "config.h" -#include "Foundation/NSArray.h" -#include "Foundation/NSData.h" +#include "GSURLPrivate.h" + #include "Foundation/NSEnumerator.h" -#include "Foundation/NSString.h" -#include "Foundation/NSException.h" -#include "Foundation/NSLock.h" #include "Foundation/NSURLHandle.h" -#include "Foundation/NSURL.h" #include "Foundation/NSRunLoop.h" #include "Foundation/NSFileManager.h" diff --git a/Source/NSURLProtectionSpace.m b/Source/NSURLProtectionSpace.m new file mode 100644 index 000000000..ecc2aa0b7 --- /dev/null +++ b/Source/NSURLProtectionSpace.m @@ -0,0 +1,198 @@ +/* Implementation for NSURLProtectionSpace for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +NSString * const NSURLProtectionSpaceFTPProxy = @"ftp"; +NSString * const NSURLProtectionSpaceHTTPProxy = @"http"; +NSString * const NSURLProtectionSpaceHTTPSProxy = @"https"; +NSString * const NSURLProtectionSpaceSOCKSProxy = @"SOCKS"; +NSString * const NSURLAuthenticationMethodDefault + = @"NSURLAuthenticationMethodDefault"; +NSString * const NSURLAuthenticationMethodHTMLForm + = @"NSURLAuthenticationMethodHTMLForm"; +NSString * const NSURLAuthenticationMethodHTTPBasic + = @"NSURLAuthenticationMethodHTTPBasic"; +NSString * const NSURLAuthenticationMethodHTTPDigest + = @"NSURLAuthenticationMethodHTTPDigest"; + +// Internal data storage +typedef struct { + NSString *host; + int port; + NSString *protocol; + NSString *realm; + NSString *proxyType; + NSString *authenticationMethod; + BOOL isProxy; +} Internal; + +typedef struct { + @defs(NSURLProtectionSpace) +} priv; +#define this ((Internal*)(((priv*)self)->_NSURLProtectionSpaceInternal)) +#define inst ((Internal*)(((priv*)o)->_NSURLProtectionSpaceInternal)) + +@implementation NSURLProtectionSpace + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLProtectionSpace *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLProtectionSpaceInternal = NSZoneMalloc(z, sizeof(Internal)); + } + return o; +} + +- (NSString *) authenticationMethod +{ + return this->authenticationMethod; +} + +- (id) copyWithZone: (NSZone*)z +{ + NSURLProtectionSpace *o = [[self class] allocWithZone: z]; + + o = [o initWithHost: this->host + port: this->port + protocol: this->protocol + realm: this->realm + authenticationMethod: this->authenticationMethod]; + if (o != nil) + { + inst->isProxy = this->isProxy; + ASSIGN(inst->proxyType, this->proxyType); + } + return o; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->host); + RELEASE(this->protocol); + RELEASE(this->proxyType); + RELEASE(this->realm); + RELEASE(this->authenticationMethod); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (NSString *) host +{ + return this->host; +} + +- (id) initWithHost: (NSString *)host + port: (int)port + protocol: (NSString *)protocol + realm: (NSString *)realm +authenticationMethod: (NSString *)authenticationMethod +{ + if ((self = [super init]) != nil) + { + this->host = [host copy]; + this->protocol = [protocol copy]; + this->realm = [realm copy]; + this->authenticationMethod = [authenticationMethod copy]; + this->port = port; + this->proxyType = nil; + this->isProxy = NO; + } + return self; +} + +- (id) initWithProxyHost: (NSString *)host + port: (int)port + type: (NSString *)type + realm: (NSString *)realm + authenticationMethod: (NSString *)authenticationMethod +{ + self = [self initWithHost: host + port: port + protocol: nil + realm: realm + authenticationMethod: authenticationMethod]; + if (self != nil) + { + this->isProxy = YES; + ASSIGNCOPY(this->proxyType, type); + } + return NO; +} + +- (BOOL) isProxy +{ + return this->isProxy; +} + +- (int) port +{ + return this->port; +} + +- (NSString *) protocol +{ + return this->protocol; +} + +- (NSString *) proxyType +{ + return this->proxyType; +} + +- (NSString *) realm +{ + return this->realm; +} + +- (BOOL) receivesCredentialSecurely +{ + if ([this->authenticationMethod isEqual: NSURLAuthenticationMethodHTTPDigest]) + { + return YES; + } + if (this->isProxy) + { + if ([this->proxyType isEqual: NSURLProtectionSpaceHTTPSProxy] == YES) + { + return YES; + } + } + else + { + if ([this->protocol isEqual: @"https"] == YES) + { + return YES; + } + } + return NO; +} + +@end + diff --git a/Source/NSURLProtocol.m b/Source/NSURLProtocol.m new file mode 100644 index 000000000..41f475261 --- /dev/null +++ b/Source/NSURLProtocol.m @@ -0,0 +1,174 @@ +/* Implementation for NSURLProtocol for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + + +// Internal data storage +typedef struct { + NSInputStream *input; + NSOutputStream *output; + NSCachedURLResponse *cachedResponse; + id client; + NSURLRequest *request; +} Internal; + +typedef struct { + @defs(NSURLProtocol) +} priv; +#define this ((Internal*)(((priv*)self)->_NSURLProtocolInternal)) +#define inst ((Internal*)(((priv*)o)->_NSURLProtocolInternal)) + +static NSMutableArray *registered = nil; +static NSLock *regLock = nil; + +@implementation NSURLProtocol + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLProtocol *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLProtocolInternal = NSZoneMalloc(z, sizeof(Internal)); + } + return o; +} + ++ (void) initialize +{ + if (registered == nil) + { + registered = [NSMutableArray new]; + regLock = [NSLock new]; + } +} + ++ (BOOL) registerClass: (Class)protocolClass +{ + if ([protocolClass isSubclassOfClass: [NSURLProtocol class]] == YES) + { + [regLock lock]; + [registered addObject: protocolClass]; + [regLock unlock]; + return YES; + } + return NO; +} + ++ (id) propertyForKey: (NSString *)key inRequest: (NSURLRequest *)request +{ + return [request _propertyForKey: key]; +} + ++ (void) setProperty: (id)value + forKey: (NSString *)key + inRequest: (NSMutableURLRequest *)request +{ + [request _setProperty: value forKey: key]; +} + ++ (void) unregisterClass: (Class)protocolClass +{ + [regLock lock]; + [registered removeObjectIdenticalTo: protocolClass]; + [regLock unlock]; +} + +- (NSCachedURLResponse *) cachedResponse +{ + return this->cachedResponse; +} + +- (id ) client +{ + return this->client; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->input); + RELEASE(this->output); + RELEASE(this->cachedResponse); + RELEASE(this->client); + RELEASE(this->request); + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (id) initWithRequest: (NSURLRequest *)request + cachedResponse: (NSCachedURLResponse *)cachedResponse + client: (id )client +{ + if ((self = [super init]) != nil) + { + this->request = [request copy]; + this->cachedResponse = RETAIN(cachedResponse); + this->client = RETAIN(client); + } + return self; +} + +- (NSURLRequest *) request +{ + return this->request; +} + +@end + + +@implementation NSURLProtocol (Subclassing) + ++ (BOOL) canInitWithRequest: (NSURLRequest *)request +{ + [self subclassResponsibility: _cmd]; + return NO; +} + ++ (NSURLRequest *) canonicalRequestForRequest: (NSURLRequest *)request +{ + return request; +} + ++ (BOOL) requestIsCacheEquivalent: (NSURLRequest *)a + toRequest: (NSURLRequest *)b +{ + return [a isEqual: b]; +} + +- (void) startLoading +{ + [self subclassResponsibility: _cmd]; +} + +- (void) stopLoading +{ + [self subclassResponsibility: _cmd]; +} + +@end + diff --git a/Source/NSURLRequest.m b/Source/NSURLRequest.m new file mode 100644 index 000000000..080fba26a --- /dev/null +++ b/Source/NSURLRequest.m @@ -0,0 +1,512 @@ +/* Implementation for NSURLRequest for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +#include "Foundation/NSMapTable.h" +#include "Foundation/NSCoder.h" +#include "NSCallBacks.h" + + +// Internal data storage +typedef struct { + NSData *body; + NSInputStream *bodyStream; + NSString *method; + NSMapTable *headers; + BOOL shouldHandleCookies; + 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. + */ +typedef struct { + @defs(NSURLRequest) +} priv; +#define this ((Internal*)(((priv*)self)->_NSURLRequestInternal)) +#define inst ((Internal*)(((priv*)o)->_NSURLRequestInternal)) + +@implementation NSURLRequest + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLRequest *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLRequestInternal = NSZoneMalloc(z, sizeof(Internal)); + memset(o->_NSURLRequestInternal, '\0', sizeof(Internal)); + } + 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; + if (this->headers == 0) + { + inst->headers = 0; + } + else + { + inst->headers = NSCopyMapTableWithZone(this->headers, z); + } + } + } + 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); + if (this->headers != 0) + { + NSFreeMapTable(this->headers); + } + 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; +} + +- (unsigned) hash +{ + return [this->URL hash]; +} + +- (id) initWithURL: (NSURL *)URL +{ + return [self initWithURL: URL + cachePolicy: NSURLRequestUseProtocolCachePolicy + timeoutInterval: 60.0]; +} + +- (id) initWithURL: (NSURL *)URL + cachePolicy: (NSURLRequestCachePolicy)cachePolicy + timeoutInterval: (NSTimeInterval)timeoutInterval +{ + if ((self = [super init]) != nil) + { + this->URL = RETAIN(URL); + this->cachePolicy = cachePolicy; + this->timeoutInterval = timeoutInterval; + this->mainDocumentURL = nil; + } + 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; + } + if (this->headers != inst->headers) + { + NSMapEnumerator enumerator; + id k; + id v; + + if (this->headers == 0 || inst->headers == 0) + { + return NO; + } + if (NSCountMapTable(this->headers) != NSCountMapTable(inst->headers)) + { + return NO; + } + enumerator = NSEnumerateMapTable(this->headers); + while (NSNextMapEnumeratorPair(&enumerator, (void **)(&k), (void**)&v)) + { + id ov = (id)NSMapGet(inst->headers, (void*)k); + + if ([v isEqual: ov] == NO) + { + NSEndMapTableEnumeration(&enumerator); + return NO; + } + } + NSEndMapTableEnumeration(&enumerator); + } + 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; + if (this->headers == 0) + { + inst->headers = 0; + } + else + { + inst->headers = NSCopyMapTableWithZone(this->headers, z); + } + } + return o; +} + +- (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 + + + +/* + * Implement map keys for strings with case insensitive comparisons, + * so we can have case insensitive matching of http headers (correct + * behavior), but actually preserve case of headers stored and written + * in case the remote server is buggy and requires particular + * captialisation of headers (some http software is faulty like that). + */ +static unsigned int +_non_retained_id_hash(void *table, NSString* o) +{ + return [[o uppercaseString] hash]; +} + +static BOOL +_non_retained_id_is_equal(void *table, NSString *o, NSString *p) +{ + return ([o caseInsensitiveCompare: p] == NSOrderedSame) ? YES : NO; +} + +typedef unsigned int (*NSMT_hash_func_t)(NSMapTable *, const void *); +typedef BOOL (*NSMT_is_equal_func_t)(NSMapTable *, const void *, const void *); +typedef void (*NSMT_retain_func_t)(NSMapTable *, const void *); +typedef void (*NSMT_release_func_t)(NSMapTable *, void *); +typedef NSString *(*NSMT_describe_func_t)(NSMapTable *, const void *); + +static const NSMapTableKeyCallBacks headerKeyCallBacks = +{ + (NSMT_hash_func_t) _non_retained_id_hash, + (NSMT_is_equal_func_t) _non_retained_id_is_equal, + (NSMT_retain_func_t) _NS_non_retained_id_retain, + (NSMT_release_func_t) _NS_non_retained_id_release, + (NSMT_describe_func_t) _NS_non_retained_id_describe, + NSNotAPointerMapKey +}; + +@implementation NSURLRequest (NSHTTPURLRequest) + +- (NSDictionary *) allHTTPHeaderFields +{ + NSMutableDictionary *fields; + + fields = [NSMutableDictionary dictionaryWithCapacity: 8]; + if (this->headers != 0) + { + NSMapEnumerator enumerator; + NSString *k; + NSString *v; + + enumerator = NSEnumerateMapTable(this->headers); + while (NSNextMapEnumeratorPair(&enumerator, (void **)(&k), (void**)&v)) + { + [fields setObject: v forKey: k]; + } + NSEndMapTableEnumeration(&enumerator); + } + 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 +{ + NSString *value = nil; + + if (this->headers != 0) + { + value = (NSString*)NSMapGet(this->headers, (void*)field); + } + return value; +} + +@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 +{ + ASSIGNCOPY(this->method, method); +} + +- (void) setHTTPShouldHandleCookies: (BOOL)should +{ + this->shouldHandleCookies = should; +} + +- (void) setValue: (NSString *)value forHTTPHeaderField: (NSString *)field +{ + if (this->headers == 0) + { + this->headers = NSCreateMapTable(headerKeyCallBacks, + NSObjectMapValueCallBacks, 8); + } + NSMapInsert(this->headers, (void*)field, (void*)value); +} + +@end + +@implementation NSURLRequest (Private) +- (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 diff --git a/Source/NSURLResponse.m b/Source/NSURLResponse.m new file mode 100644 index 000000000..bcf2af9f3 --- /dev/null +++ b/Source/NSURLResponse.m @@ -0,0 +1,338 @@ +/* Implementation for NSURLResponse for GNUstep + Copyright (C) 2006 Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: 2006 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + 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. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02111 USA. + */ + +#include "GSURLPrivate.h" + +#include "Foundation/NSCoder.h" +#include "Foundation/NSMapTable.h" +#include "Foundation/NSScanner.h" +#include "NSCallBacks.h" +#include "GNUstepBase/GSMime.h" + + +// Internal data storage +typedef struct { + long long expectedContentLength; + NSURL *URL; + NSString *MIMEType; + NSString *textEncodingName; + NSString *statusText; + NSMapTable *headers; + int statusCode; +} Internal; + +typedef struct { + @defs(NSURLResponse) +} priv; +#define this ((Internal*)(((priv*)self)->_NSURLResponseInternal)) +#define inst ((Internal*)(((priv*)o)->_NSURLResponseInternal)) + + +/* + * Implement map keys for strings with case insensitive comparisons, + * so we can have case insensitive matching of http headers (correct + * behavior), but actually preserve case of headers stored and written + * in case the remote server is buggy and requires particular + * captialisation of headers (some http software is faulty like that). + */ +static unsigned int +_non_retained_id_hash(void *table, NSString* o) +{ + return [[o uppercaseString] hash]; +} + +static BOOL +_non_retained_id_is_equal(void *table, NSString *o, NSString *p) +{ + return ([o caseInsensitiveCompare: p] == NSOrderedSame) ? YES : NO; +} + +typedef unsigned int (*NSMT_hash_func_t)(NSMapTable *, const void *); +typedef BOOL (*NSMT_is_equal_func_t)(NSMapTable *, const void *, const void *); +typedef void (*NSMT_retain_func_t)(NSMapTable *, const void *); +typedef void (*NSMT_release_func_t)(NSMapTable *, void *); +typedef NSString *(*NSMT_describe_func_t)(NSMapTable *, const void *); + +static const NSMapTableKeyCallBacks headerKeyCallBacks = +{ + (NSMT_hash_func_t) _non_retained_id_hash, + (NSMT_is_equal_func_t) _non_retained_id_is_equal, + (NSMT_retain_func_t) _NS_non_retained_id_retain, + (NSMT_release_func_t) _NS_non_retained_id_release, + (NSMT_describe_func_t) _NS_non_retained_id_describe, + NSNotAPointerMapKey +}; + +@interface NSURLResponse (Internal) +- (void) setStatusCode: (int)code text: (NSString*)text; +- (void) setValue: (NSString *)value forHTTPHeaderField: (NSString *)field; +- (NSString *) valueForHTTPHeaderField: (NSString *)field; +@end + +@implementation NSURLResponse (Internal) +- (void) setStatusCode: (int)code text: (NSString*)text +{ + this->statusCode = code; + ASSIGNCOPY(this->statusText, text); +} +- (void) setValue: (NSString *)value forHTTPHeaderField: (NSString *)field +{ + if (this->headers == 0) + { + this->headers = NSCreateMapTable(headerKeyCallBacks, + NSObjectMapValueCallBacks, 8); + } + NSMapInsert(this->headers, (void*)field, (void*)value); +} +- (NSString *) valueForHTTPHeaderField: (NSString *)field +{ + NSString *value = nil; + + if (this->headers != 0) + { + value = (NSString*)NSMapGet(this->headers, (void*)field); + } + return value; +} +@end + + +@implementation NSURLResponse + ++ (id) allocWithZone: (NSZone*)z +{ + NSURLResponse *o = [super allocWithZone: z]; + + if (o != nil) + { + o->_NSURLResponseInternal = NSZoneMalloc(z, sizeof(Internal)); + memset(o->_NSURLResponseInternal, '\0', sizeof(Internal)); + } + return o; +} + +- (id) copyWithZone: (NSZone*)z +{ + NSURLResponse *o; + + if (NSShouldRetainWithZone(self, z) == YES) + { + o = RETAIN(self); + } + else + { + o = [[self class] allocWithZone: z]; + o = [o initWithURL: [self URL] + MIMEType: [self MIMEType] + expectedContentLength: [self expectedContentLength] + textEncodingName: [self textEncodingName]]; + if (o != nil) + { + ASSIGN(inst->statusText, this->statusText); + inst->statusCode = this->statusCode; + if (this->headers == 0) + { + inst->headers = 0; + } + else + { + inst->headers = NSCopyMapTableWithZone(this->headers, z); + } + } + } + return o; +} + +- (void) dealloc +{ + if (this != 0) + { + RELEASE(this->URL); + RELEASE(this->MIMEType); + RELEASE(this->textEncodingName); + RELEASE(this->statusText); + if (this->headers != 0) + { + NSFreeMapTable(this->headers); + } + NSZoneFree([self zone], this); + } + [super dealloc]; +} + +- (void) encodeWithCoder: (NSCoder*)aCoder +{ +// FIXME + if ([aCoder allowsKeyedCoding]) + { + } + else + { + } +} + +- (long long) expectedContentLength +{ + return this->expectedContentLength; +} + +- (id) initWithCoder: (NSCoder*)aCoder +{ +// FIXME + if ([aCoder allowsKeyedCoding]) + { + } + else + { + } + return self; +} + +/** + * Initialises the receiver with the URL, MIMEType, expected length and + * text encoding name provided. + */ +- (id) initWithURL: (NSURL *)URL + MIMEType: (NSString *)MIMEType + expectedContentLength: (int)length + textEncodingName: (NSString *)name +{ + if ((self = [super init]) != nil) + { + ASSIGN(this->URL, URL); + ASSIGNCOPY(this->MIMEType, MIMEType); + ASSIGNCOPY(this->textEncodingName, name); + this->expectedContentLength = length; + } + return self; +} + +- (NSString *) MIMEType +{ + return this->MIMEType; +} + +/** + * Returns a suggested file name for storing the response data, with + * suggested names being found in the following order:
+ * + * content-disposition header + * last path component of URL + * host name from URL + * 'unknown' + * + * If possible, an extension based on the MIME type of the response + * is also appended.
+ * The result should always be a valid file name. + */ +- (NSString *) suggestedFilename +{ + NSString *disp = [self valueForHTTPHeaderField: @"content-disposition"]; + NSString *name = nil; + + if (disp != nil) + { + GSMimeParser *p; + GSMimeHeader *h; + NSScanner *sc; + + // Try to get name from content disposition header. + p = AUTORELEASE([GSMimeParser new]); + h = [[GSMimeHeader alloc] initWithName: @"content-displosition" + value: disp]; + AUTORELEASE(h); + sc = [NSScanner scannerWithString: [h value]]; + if ([p scanHeaderBody: sc into: h] == YES) + { + name = [h parameterForKey: @"filename"]; + name = [name stringByDeletingPathExtension]; + } + } + + if ([name length] == 0) + { + name = [[[self URL] absoluteString] lastPathComponent]; + name = [name stringByDeletingPathExtension]; + } + if ([name length] == 0) + { + name = [[self URL] host]; + } + if ([name length] == 0) + { + name = @"unknown"; + } +// FIXME ... add type specific extension + return name; +} + +- (NSString *) textEncodingName +{ + return this->textEncodingName; +} + +- (NSURL *) URL +{ + return this->URL; +} + +@end + + +@implementation NSHTTPURLResponse + ++ (NSString *) localizedStringForStatusCode: (int)statusCode +{ +// FIXME ... put real responses in here + return [NSString stringWithFormat: @"%d", statusCode]; +} + +- (NSDictionary *) allHeaderFields +{ + NSMutableDictionary *fields; + + fields = [NSMutableDictionary dictionaryWithCapacity: 8]; + if (this->headers != 0) + { + NSMapEnumerator enumerator; + NSString *k; + NSString *v; + + enumerator = NSEnumerateMapTable(this->headers); + while (NSNextMapEnumeratorPair(&enumerator, (void **)(&k), (void**)&v)) + { + [fields setObject: v forKey: k]; + } + NSEndMapTableEnumeration(&enumerator); + } + return fields; +} + +- (int) statusCode +{ + return this->statusCode; +} + +@end +