Skip to content

Commit

Permalink
fix for #302
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Feb 11, 2024
1 parent be12e91 commit ec91479
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 24 deletions.
12 changes: 12 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
2024-02-11 Richard Frith-Macdonald <[email protected]>

* Headers/Foundation/NSURLProtocol.h:
* Source/GSEasyHandle.m:
* Source/GSHTTPURLProtocol.m:
* Source/GSNativeProtocol.m:
* Source/NSURLProtocol.m:
* Source/NSURLSession.m:
Add +canInitWithTask: and altered code to differentiate between
protocols which can be used with NSURLConnection and those which
can be used with NSURLSession to fix #302

2024-02-11 Richard Frith-Macdonald <[email protected]>

* Source/NSLocale.m: (+canonicalLocaleIdentifierFromString:)
Expand Down
5 changes: 5 additions & 0 deletions Headers/Foundation/NSURLProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ GS_EXPORT_CLASS
*/
+ (BOOL) canInitWithRequest: (NSURLRequest *)request;

/** This method is called to decide whether a class can deal with
* the specified task. The abstract class implementation return NO.
*/
+ (BOOL) canInitWithTask: (NSURLSessionTask*)task;

/** <override-subclass />
* Returns the 'canonical' version of the request.<br />
* The canonical form is used to look up requests in the cache by
Expand Down
44 changes: 30 additions & 14 deletions Source/GSEasyHandle.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ - (int) seekInputStreamWithOffset: (int64_t)offset
static size_t
curl_write_function(char *data, size_t size, size_t nmemb, void *userdata)
{
GSEasyHandle *handle;

if (!userdata)
{
return 0;
}

GSEasyHandle *handle = (GSEasyHandle*)userdata;
handle = (GSEasyHandle*)userdata;

[handle resetTimer]; //FIXME should be deffered after the function returns?

Expand All @@ -66,12 +68,14 @@ - (int) seekInputStreamWithOffset: (int64_t)offset
static size_t
curl_read_function(char *data, size_t size, size_t nmemb, void *userdata)
{
GSEasyHandle *handle;

if (!userdata)
{
return 0;
}

GSEasyHandle *handle = (GSEasyHandle*)userdata;
handle = (GSEasyHandle*)userdata;

[handle resetTimer]; //FIXME should be deffered after the function returns?

Expand All @@ -81,13 +85,15 @@ - (int) seekInputStreamWithOffset: (int64_t)offset
size_t
curl_header_function(char *data, size_t size, size_t nmemb, void *userdata)
{
GSEasyHandle *handle;
double length;

if (!userdata)
{
return 0;
}

GSEasyHandle *handle = (GSEasyHandle*)userdata;
double length;
handle = (GSEasyHandle*)userdata;

[handle resetTimer]; //FIXME should be deffered after the function returns?

Expand All @@ -103,12 +109,14 @@ - (int) seekInputStreamWithOffset: (int64_t)offset
static int
curl_seek_function(void *userdata, curl_off_t offset, int origin)
{
GSEasyHandle *handle;

if (!userdata)
{
return CURL_SEEKFUNC_FAIL;
}

GSEasyHandle *handle = (GSEasyHandle*)userdata;
handle = (GSEasyHandle*)userdata;

return [handle seekInputStreamWithOffset: offset origin: origin];
}
Expand All @@ -117,6 +125,12 @@ - (int) seekInputStreamWithOffset: (int64_t)offset
curl_debug_function(CURL *handle, curl_infotype type, char *data,
size_t size, void *userptr)
{
NSURLSessionTask *task;
NSString *text;
NSURLRequest *o;
NSURLRequest *r;
id<GSLogDelegate> d;

if (!userptr)
{
return 0;
Expand All @@ -127,11 +141,11 @@ - (int) seekInputStreamWithOffset: (int64_t)offset
return 0; // Don't log encrypted data here
}

NSURLSessionTask *task = (NSURLSessionTask*)userptr;
NSString *text = @"";
NSURLRequest *o = [task originalRequest];
NSURLRequest *r = [task currentRequest];
id<GSLogDelegate> d = [(nil == r ? o : r) _debugLogDelegate];
task = (NSURLSessionTask*)userptr;
text = @"";
o = [task originalRequest];
r = [task currentRequest];
d = [(nil == r ? o : r) _debugLogDelegate];

if (d != nil)
{
Expand Down Expand Up @@ -177,10 +191,12 @@ - (instancetype) initWithDelegate: (id<GSEasyHandleDelegate>)delegate
{
if (nil != (self = [super init]))
{
char *eb;

_rawHandle = curl_easy_init();
_delegate = delegate;

char *eb = (char *)malloc(sizeof(char) * (CURL_ERROR_SIZE + 1));
eb = (char *)malloc(sizeof(char) * (CURL_ERROR_SIZE + 1));
_errorBuffer = memset(eb, 0, sizeof(char) * (CURL_ERROR_SIZE + 1));

[self setupCallbacks];
Expand Down Expand Up @@ -395,8 +411,9 @@ - (void) setConnectToHost: (NSString*)host port: (NSInteger)port
{
if (nil != host)
{
NSString *originHost = [_URL host];
NSString *value = nil;
NSString *originHost = [_URL host];
NSString *value;
struct curl_slist *connect_to;

if (0 == port)
{
Expand All @@ -408,7 +425,6 @@ - (void) setConnectToHost: (NSString*)host port: (NSInteger)port
originHost, (unsigned long)port, host];
}

struct curl_slist *connect_to = NULL;
connect_to = curl_slist_append(NULL, [value UTF8String]);
handleEasyCode(
curl_easy_setopt(_rawHandle, CURLOPT_CONNECT_TO, connect_to));
Expand Down
17 changes: 10 additions & 7 deletions Source/GSHTTPURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,10 @@ + (void) getCacheControlDeirectivesFromHeaderValue: (NSString*)headerValue

@implementation GSHTTPURLProtocol

+ (BOOL) canInitWithRequest: (NSURLRequest*)request
+ (BOOL) canInitWithTask: (NSURLSessionTask*)task
{
NSURL *url;
NSURLRequest *request = [task currentRequest];
NSURL *url;

if (nil != (url = [request URL])
&& ([[url scheme] isEqualToString: @"http"]
Expand Down Expand Up @@ -431,6 +432,7 @@ - (void) configureEasyHandleForRequest: (NSURLRequest*)request
NSURLSessionTask *task = [self task];
NSURLSession *session = [task session];
NSURLSessionConfiguration *config = [session configuration];
BOOL debugLibcurl;

if ([[request HTTPMethod] isEqualToString:@"GET"])
{
Expand All @@ -452,7 +454,7 @@ - (void) configureEasyHandleForRequest: (NSURLRequest*)request
}
}

BOOL debugLibcurl = [[[NSProcessInfo processInfo] environment]
debugLibcurl = [[[NSProcessInfo processInfo] environment]
objectForKey: @"URLSessionDebugLibcurl"] ? YES : NO;

/* Programatically turning debug on in the request supercedes any
Expand Down Expand Up @@ -636,9 +638,10 @@ - (GSCompletionAction*) completeActionForCompletedRequest: (NSURLRequest*)reques
- (NSURLRequest*) redirectRequestForResponse: (NSHTTPURLResponse*)response
fromRequest: (NSURLRequest*)fromRequest
{
NSString *method = nil;
NSURL *targetURL;
NSString *location;
NSString *method = nil;
NSURL *targetURL;
NSString *location;
NSMutableURLRequest *request;

if (nil == [response allHeaderFields])
{
Expand Down Expand Up @@ -673,7 +676,7 @@ - (NSURLRequest*) redirectRequestForResponse: (NSHTTPURLResponse*)response
return nil;
}

NSMutableURLRequest *request = AUTORELEASE([fromRequest mutableCopy]);
request = AUTORELEASE([fromRequest mutableCopy]);
[request setHTTPMethod: method];

if (nil != [targetURL scheme] && nil != [targetURL host])
Expand Down
9 changes: 8 additions & 1 deletion Source/GSNativeProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ - (NSUInteger) suspendCount

- (void) getBodyWithCompletion: (void (^)(GSURLSessionTaskBody *body))completion
{
GSURLSessionTaskBody *body;

if (nil != _knownBody)
{
completion(_knownBody);
return;
};

GSURLSessionTaskBody *body = AUTORELEASE([[GSURLSessionTaskBody alloc] init]);
body = AUTORELEASE([[GSURLSessionTaskBody alloc] init]);
completion(body);
}

Expand Down Expand Up @@ -198,6 +200,11 @@ - (void) setRedirectRequest: (NSURLRequest*)request

@implementation GSNativeProtocol

+ (BOOL) canInitWithRequest: (NSURLRequest*)request
{
return NO;
}

- (instancetype) initWithTask: (NSURLSessionTask*)_task
cachedResponse: (NSCachedURLResponse*)_cachedResponse
client: (id<NSURLProtocolClient>)_client
Expand Down
5 changes: 5 additions & 0 deletions Source/NSURLProtocol.m
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ + (BOOL) canInitWithRequest: (NSURLRequest *)request
return NO;
}

+ (BOOL) canInitWithTask: (NSURLSessionTask*)task
{
return NO;
}

+ (NSURLRequest *) canonicalRequestForRequest: (NSURLRequest *)request
{
return request;
Expand Down
5 changes: 3 additions & 2 deletions Source/NSURLSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@ - (void) URLProtocol: (NSURLProtocol *)protocol
{
NSURLSessionTask *task = [protocol task];
NSURLSession *session;
id<NSURLSessionDelegate> delegate;

NSAssert(nil != task, @"Missing task");

Expand Down Expand Up @@ -712,7 +713,7 @@ - (void) URLProtocol: (NSURLProtocol *)protocol
}
}

id<NSURLSessionDelegate> delegate = [session delegate];
delegate = [session delegate];
if (nil != delegate)
{
[[session delegateQueue] addOperationWithBlock:
Expand Down Expand Up @@ -1008,7 +1009,7 @@ - (instancetype) initWithSession: (NSURLSession*)session
e = [[[session configuration] protocolClasses] objectEnumerator];
while (nil != (protocolClass = [e nextObject]))
{
if ([protocolClass canInitWithRequest: request])
if ([protocolClass canInitWithTask: self])
{
_protocolClass = protocolClass;
break;
Expand Down

0 comments on commit ec91479

Please sign in to comment.