Make files closer to gnustep coding style (automated fixups using uncrustify)

This commit is contained in:
rfm 2024-10-28 11:21:47 +00:00
parent f6c0c5a5c0
commit 0ad7e3f8ef
5 changed files with 1313 additions and 1181 deletions

View file

@ -1,31 +1,31 @@
/**
NSURLSession.m
Copyright (C) 2017-2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: May 2024
Author: Hugo Melder <hugo@algoriddim.com>
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
* NSURLSession.m
*
* Copyright (C) 2017-2024 Free Software Foundation, Inc.
*
* Written by: Hugo Melder <hugo@algoriddim.com>
* Date: May 2024
* Author: Hugo Melder <hugo@algoriddim.com>
*
* This file is part of GNUStep-base
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* If you are interested in a warranty or support for this source code,
* contact Scott Christley <scottc@net-community.com> for more information.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110 USA.
*/
#import "NSURLSessionPrivate.h"
@ -69,9 +69,12 @@ timer_callback(CURLM *multi, /* multi handle */
{
NSURLSession * session = (NSURLSession *)clientp;
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Timer Callback for Session %@: multi=%p timeout_ms=%ld",
session, multi, timeout_ms);
session,
multi,
timeout_ms);
/*
* if timeout_ms is -1, just delete the timer
@ -98,9 +101,13 @@ socket_callback(CURL *easy, /* easy handle */
NSURLSession * session = clientp;
const char * whatstr[] = { "none", "IN", "OUT", "INOUT", "REMOVE" };
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Socket Callback for Session %@: socket=%d easy:%p what=%s",
session, s, easy, whatstr[what]);
session,
s,
easy,
whatstr[what]);
if (NULL == socketp)
{
@ -117,7 +124,7 @@ socket_callback(CURL *easy, /* easy handle */
sources: (struct SourceInfo *)socketp
what: what];
}
}
} /* socket_callback */
#pragma mark - NSURLSession Implementation
@ -188,7 +195,9 @@ socket_callback(CURL *easy, /* easy handle */
static NSURLSession * session = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
dispatch_once(
&predicate,
^{
NSURLSessionConfiguration * configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
session = [[NSURLSession alloc] initWithConfiguration: configuration
@ -226,7 +235,8 @@ socket_callback(CURL *easy, /* easy handle */
return AUTORELEASE(session);
}
- (instancetype)initWithConfiguration:(NSURLSessionConfiguration *)configuration
- (instancetype) initWithConfiguration: (NSURLSessionConfiguration *)
configuration
delegate: (id<NSURLSessionDelegate>)delegate
delegateQueue: (NSOperationQueue *)queue
{
@ -266,14 +276,21 @@ socket_callback(CURL *easy, /* easy handle */
return nil;
}
dispatch_source_set_cancel_handler(_timer, ^{
dispatch_source_set_cancel_handler(
_timer,
^{
dispatch_release(this->_timer);
});
// Called after timeout set by libcurl is reached
dispatch_source_set_event_handler(_timer, ^{
dispatch_source_set_event_handler(
_timer,
^{
// TODO: Check for return values
curl_multi_socket_action(this->_multiHandle, CURL_SOCKET_TIMEOUT, 0,
curl_multi_socket_action(
this->_multiHandle,
CURL_SOCKET_TIMEOUT,
0,
&this->_stillRunning);
[this _checkForCompletion];
});
@ -304,7 +321,9 @@ socket_callback(CURL *easy, /* easy handle */
curl_multi_setopt(_multiHandle, CURLMOPT_TIMERDATA, self);
// Configure Multi Handle
curl_multi_setopt(_multiHandle, CURLMOPT_MAX_HOST_CONNECTIONS,
curl_multi_setopt(
_multiHandle,
CURLMOPT_MAX_HOST_CONNECTIONS,
[_configuration HTTPMaximumConnectionsPerHost]);
/* Check if GSCACertificateFilePath is set */
@ -320,7 +339,8 @@ socket_callback(CURL *easy, /* easy handle */
_certificateBlob = [[NSData alloc] initWithContentsOfFile: caPath];
if (!_certificateBlob)
{
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Could not open file at GSCACertificateFilePath=%@",
caPath);
}
@ -332,7 +352,7 @@ socket_callback(CURL *easy, /* easy handle */
}
return self;
}
} /* initWithConfiguration */
#pragma mark - Private Methods
@ -365,15 +385,21 @@ socket_callback(CURL *easy, /* easy handle */
- (void) _resumeTask: (NSURLSessionTask *)task
{
dispatch_async(_workQueue, ^{
dispatch_async(
_workQueue,
^{
CURLMcode code;
CURLM * multiHandle = _multiHandle;
code = curl_multi_add_handle(multiHandle, [task _easyHandle]);
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Added task=%@ easy=%p to multi=%p with return value %d",
task, [task _easyHandle], multiHandle, code);
task,
[task _easyHandle],
multiHandle,
code);
});
}
@ -388,8 +414,10 @@ socket_callback(CURL *easy, /* easy handle */
- (void) _setTimer: (NSInteger)timeoutMs
{
dispatch_source_set_timer(_timer,
dispatch_time(DISPATCH_TIME_NOW,
dispatch_source_set_timer(
_timer,
dispatch_time(
DISPATCH_TIME_NOW,
timeoutMs * NSEC_PER_MSEC),
DISPATCH_TIME_FOREVER, // don't repeat
timeoutMs * 0.05); // 5% leeway
@ -421,7 +449,9 @@ socket_callback(CURL *easy, /* easy handle */
*/
- (void) _removeSocket: (struct SourceInfo *)sources
{
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY, @"Remove socket with SourceInfo: %p",
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Remove socket with SourceInfo: %p",
sources);
if (sources->readSocket)
@ -442,17 +472,22 @@ socket_callback(CURL *easy, /* easy handle */
* (socketp) in socket_callback is NULL, meaning we first need to
* allocate our SourceInfo structure.
*/
- (int)_addSocket:(curl_socket_t)socket easyHandle:(CURL *)easy what:(int)what
- (int) _addSocket: (curl_socket_t)socket easyHandle: (CURL *)easy what: (int)
what
{
struct SourceInfo * info;
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY, @"Add Socket: %d easy: %p", socket,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Add Socket: %d easy: %p",
socket,
easy);
/* Allocate a new SourceInfo structure on the heap */
if (!(info = calloc(1, sizeof(struct SourceInfo))))
{
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Failed to allocate SourceInfo structure!");
return -1;
}
@ -466,7 +501,7 @@ socket_callback(CURL *easy, /* easy handle */
/* Assign the SourceInfo for access in subsequent socket_callback calls */
curl_multi_assign(_multiHandle, socket, info);
return 0;
}
} /* _addSocket */
- (int) _setSocket: (curl_socket_t)socket
sources: (struct SourceInfo *)sources
@ -486,17 +521,25 @@ socket_callback(CURL *easy, /* easy handle */
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Creating a reading dispatch source: socket=%d sources=%p what=%d",
socket, sources, what);
socket,
sources,
what);
sources->readSocket = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
socket, 0, _workQueue);
sources->readSocket = dispatch_source_create(
DISPATCH_SOURCE_TYPE_READ,
socket,
0,
_workQueue);
if (!sources->readSocket)
{
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Unable to create dispatch source for read socket!");
return -1;
}
dispatch_source_set_event_handler(sources->readSocket, ^{
dispatch_source_set_event_handler(
sources->readSocket,
^{
int action;
action = CURL_CSELECT_IN;
@ -528,18 +571,26 @@ socket_callback(CURL *easy, /* easy handle */
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Creating a writing dispatch source: socket=%d sources=%p what=%d",
socket, sources, what);
socket,
sources,
what);
sources->writeSocket = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE,
socket, 0, _workQueue);
sources->writeSocket = dispatch_source_create(
DISPATCH_SOURCE_TYPE_WRITE,
socket,
0,
_workQueue);
if (!sources->writeSocket)
{
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Unable to create dispatch source for write socket!");
return -1;
}
dispatch_source_set_event_handler(sources->writeSocket, ^{
dispatch_source_set_event_handler(
sources->writeSocket,
^{
int action;
action = CURL_CSELECT_OUT;
@ -559,7 +610,7 @@ socket_callback(CURL *easy, /* easy handle */
}
return 0;
}
} /* _setSocket */
/* Called by a socket event handler or by a firing timer set by timer_callback.
*
@ -592,7 +643,8 @@ socket_callback(CURL *easy, /* easy handle */
rc = curl_easy_getinfo(easyHandle, CURLINFO_PRIVATE, &task);
if (CURLE_OK != rc)
{
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Failed to retrieve task from easy handle %p using "
@"CURLINFO_PRIVATE",
easyHandle);
@ -607,10 +659,13 @@ socket_callback(CURL *easy, /* easy handle */
easyHandle);
}
NSDebugMLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugMLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"Transfer finished for Task %@ with effective url %s "
@"and CURLcode: %s",
task, eff_url, curl_easy_strerror(res));
task,
eff_url,
curl_easy_strerror(res));
curl_multi_remove_handle(_multiHandle, easyHandle);
@ -627,7 +682,8 @@ socket_callback(CURL *easy, /* easy handle */
* session was invalidated */
if (_invalidated && [_tasks count] == 0 &&
[_delegate respondsToSelector: @selector(URLSession:
didBecomeInvalidWithError:)])
didBecomeInvalidWithError
:)])
{
[_delegateQueue addOperationWithBlock:^{
/* We only support explicit Invalidation for now. Error is set
@ -639,12 +695,14 @@ socket_callback(CURL *easy, /* easy handle */
RELEASE(self);
}
}
}
} /* _checkForCompletion */
/* Adds task to _tasks and updates the delegate */
- (void) _didCreateTask: (NSURLSessionTask *)task
{
dispatch_async(_workQueue, ^{
dispatch_async(
_workQueue,
^{
[_tasks addObject: task];
});
@ -666,7 +724,9 @@ socket_callback(CURL *easy, /* easy handle */
return;
}
dispatch_async(_workQueue, ^{
dispatch_async(
_workQueue,
^{
_invalidated = YES;
});
}
@ -678,7 +738,9 @@ socket_callback(CURL *easy, /* easy handle */
return;
}
dispatch_async(_workQueue, ^{
dispatch_async(
_workQueue,
^{
_invalidated = YES;
/* Cancel all tasks */
@ -744,7 +806,7 @@ socket_callback(CURL *easy, /* easy handle */
[self _didCreateTask: task];
return AUTORELEASE(task);
}
} /* uploadTaskWithRequest */
- (NSURLSessionUploadTask *) uploadTaskWithRequest: (NSURLRequest *)request
fromData: (NSData *)bodyData
@ -829,12 +891,15 @@ socket_callback(CURL *easy, /* easy handle */
}
- (void) getTasksWithCompletionHandler:
(void (^)(NSArray<NSURLSessionDataTask *> *dataTasks,
(void (^)(
NSArray<NSURLSessionDataTask *> * dataTasks,
NSArray<NSURLSessionUploadTask *> * uploadTasks,
NSArray<NSURLSessionDownloadTask *> * downloadTasks))
completionHandler
{
dispatch_async(_workQueue, ^{
dispatch_async(
_workQueue,
^{
NSMutableArray<NSURLSessionDataTask *> * dataTasks;
NSMutableArray<NSURLSessionUploadTask *> * uploadTasks;
NSMutableArray<NSURLSessionDownloadTask *> * downloadTasks;
@ -871,12 +936,14 @@ socket_callback(CURL *easy, /* easy handle */
completionHandler(dataTasks, uploadTasks, downloadTasks);
});
}
} /* getTasksWithCompletionHandler */
- (void) getAllTasksWithCompletionHandler:
(void (^)(NSArray<__kindof NSURLSessionTask *> * tasks))completionHandler
{
dispatch_async(_workQueue, ^{
dispatch_async(
_workQueue,
^{
completionHandler(_tasks);
});
}
@ -992,7 +1059,7 @@ NSURLSession (NSURLSessionAsynchronousConvenience)
[self _didCreateTask: task];
return AUTORELEASE(task);
}
} /* uploadTaskWithRequest */
- (NSURLSessionUploadTask *)
uploadTaskWithRequest: (NSURLRequest *)request

View file

@ -1,31 +1,31 @@
/**
NSURLSessionConfiguration.m
Copyright (C) 2017-2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: May 2024
Author: Hugo Melder <hugo@algoriddim.com>
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
* NSURLSessionConfiguration.m
*
* Copyright (C) 2017-2024 Free Software Foundation, Inc.
*
* Written by: Hugo Melder <hugo@algoriddim.com>
* Date: May 2024
* Author: Hugo Melder <hugo@algoriddim.com>
*
* This file is part of GNUStep-base
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* If you are interested in a warranty or support for this source code,
* contact Scott Christley <scottc@net-community.com> for more information.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110 USA.
*/
#import "Foundation/NSURLSession.h"
@ -60,6 +60,7 @@ static NSURLSessionConfiguration *def = nil;
(NSString *)identifier
{
NSURLSessionConfiguration * configuration = [def copy];
configuration->_identifier = [identifier copy];
return AUTORELEASE(configuration);
}
@ -250,7 +251,7 @@ static NSURLSessionConfiguration *def = nil;
}
return AUTORELEASE([r copy]);
}
} /* setCookiesOnRequest */
- (NSURLCredentialStorage *) URLCredentialStorage
{
@ -272,12 +273,13 @@ static NSURLSessionConfiguration *def = nil;
copy->_HTTPCookieAcceptPolicy = _HTTPCookieAcceptPolicy;
copy->_HTTPCookieStorage = [_HTTPCookieStorage retain];
copy->_HTTPShouldSetCookies = _HTTPShouldSetCookies;
copy->_HTTPAdditionalHeaders = [_HTTPAdditionalHeaders copyWithZone:zone];
copy->_HTTPAdditionalHeaders =
[_HTTPAdditionalHeaders copyWithZone: zone];
copy->_timeoutIntervalForRequest = _timeoutIntervalForRequest;
copy->_timeoutIntervalForResource = _timeoutIntervalForResource;
}
return copy;
}
} /* copyWithZone */
@end

View file

@ -1,31 +1,31 @@
/**
NSURLSessionPrivate.h
Copyright (C) 2017-2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: May 2024
Author: Hugo Melder <hugo@algoriddim.com>
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
* NSURLSessionPrivate.h
*
* Copyright (C) 2017-2024 Free Software Foundation, Inc.
*
* Written by: Hugo Melder <hugo@algoriddim.com>
* Date: May 2024
* Author: Hugo Melder <hugo@algoriddim.com>
*
* This file is part of GNUStep-base
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* If you are interested in a warranty or support for this source code,
* contact Scott Christley <scottc@net-community.com> for more information.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110 USA.
*/
#import "common.h"
@ -50,7 +50,8 @@ struct SourceInfo
dispatch_source_t writeSocket;
};
typedef NS_ENUM(NSInteger, GSURLSessionProperties) {
typedef NS_ENUM(NSInteger, GSURLSessionProperties)
{
GSURLSessionStoresDataInMemory = (1 << 0),
GSURLSessionWritesDataToFile = (1 << 1),
GSURLSessionUpdatesDelegate = (1 << 2),

View file

@ -1,30 +1,30 @@
/**
NSURLSessionTask.m
Copyright (C) 2017-2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: May 2024
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
* NSURLSessionTask.m
*
* Copyright (C) 2017-2024 Free Software Foundation, Inc.
*
* Written by: Hugo Melder <hugo@algoriddim.com>
* Date: May 2024
*
* This file is part of GNUStep-base
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* If you are interested in a warranty or support for this source code,
* contact Scott Christley <scottc@net-community.com> for more information.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110 USA.
*/
#import "NSURLSessionPrivate.h"
@ -118,14 +118,15 @@ translateWinSockToPOSIXError(NSInteger err)
break;
default:
break;
}
} /* switch */
return err;
}
#endif
} /* translateWinSockToPOSIXError */
#endif /* ifdef _WIN32 */
static inline NSError *
errorForCURLcode(CURL *handle, CURLcode code, char errorBuffer[CURL_ERROR_SIZE])
errorForCURLcode(CURL * handle, CURLcode code,
char errorBuffer[CURL_ERROR_SIZE])
{
NSString * curlErrorString;
NSString * errorString;
@ -141,7 +142,8 @@ errorForCURLcode(CURL *handle, CURLcode code, char errorBuffer[CURL_ERROR_SIZE])
}
errorString = [[NSString alloc] initWithCString: errorBuffer];
curlErrorString = [[NSString alloc] initWithCString:curl_easy_strerror(code)];
curlErrorString =
[[NSString alloc] initWithCString: curl_easy_strerror(code)];
/* Get errno number from the last connect failure.
*
@ -227,7 +229,7 @@ errorForCURLcode(CURL *handle, CURLcode code, char errorBuffer[CURL_ERROR_SIZE])
default:
urlError = NSURLErrorUnknown;
break;
}
} /* switch */
/* Adjust error based on underlying OS error if available */
if (code == CURLE_COULDNT_CONNECT || code == CURLE_RECV_ERROR
@ -271,7 +273,7 @@ errorForCURLcode(CURL *handle, CURLcode code, char errorBuffer[CURL_ERROR_SIZE])
[errorString release];
return error;
}
} /* errorForCURLcode */
/* CURLOPT_PROGRESSFUNCTION: progress reports by libcurl */
static int
@ -324,8 +326,10 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
if (nil == headerFields)
{
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ Could not find 'headers' key in taskData", task);
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ Could not find 'headers' key in taskData",
task);
return 0;
}
@ -364,7 +368,8 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
NSString * errorDescription;
errorDescription = [NSString
stringWithFormat:@"Header is line folded but previous header "
stringWithFormat:
@"Header is line folded but previous header "
@"key '%@' does not have an entry",
key];
error =
@ -448,9 +453,13 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
version = @"HTTP/1.0";
}
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ version=%@ status=%ld found %ld headers", task,
version, statusCode, [headerFields count]);
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ version=%@ status=%ld found %ld headers",
task,
version,
statusCode,
[headerFields count]);
urlString = [[NSString alloc] initWithCString: effURL];
url = [NSURL URLWithString: urlString];
@ -494,14 +503,18 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
newRequest = [[task originalRequest] mutableCopy];
[newRequest setURL: redirectURL];
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ status=%ld has Location header. Prepare "
@"for redirection with url=%@",
task, statusCode, redirectURL);
task,
statusCode,
redirectURL);
if ([delegate respondsToSelector: willPerformHTTPRedirectionSel])
{
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ ask delegate for redirection "
@"permission. Pausing handle.",
task);
@ -512,12 +525,15 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
void (^completionHandler)(NSURLRequest *) = ^(
NSURLRequest * userRequest) {
/* Changes are dispatched onto workqueue */
dispatch_async([session _workQueue], ^{
dispatch_async(
[session _workQueue],
^{
if (NULL == userRequest)
{
curl_easy_pause(handle, CURLPAUSE_CONT);
[task _setShouldStopTransfer: YES];
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ willPerformHTTPRedirection "
@"completionHandler called with nil "
@"request",
@ -529,11 +545,13 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
newURLString = [[userRequest URL] absoluteString];
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ willPerformHTTPRedirection "
@"delegate completionHandler called "
@"with new URL %@",
task, newURLString);
task,
newURLString);
/* Remove handle for reconfiguration */
[session _removeHandle: handle];
@ -547,7 +565,9 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
[task _setCurrentRequest: userRequest];
/* Update URL in easy handle */
curl_easy_setopt(handle, CURLOPT_URL,
curl_easy_setopt(
handle,
CURLOPT_URL,
[newURLString UTF8String]);
curl_easy_pause(handle, CURLPAUSE_CONT);
@ -573,12 +593,16 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
@"task=%@ status=%ld has Location header but "
@"delegate does not respond to "
@"willPerformHTTPRedirection:. Redirecting to Location %@",
task, statusCode, redirectURL);
task,
statusCode,
redirectURL);
/* Remove handle for reconfiguration */
[session _removeHandle: handle];
curl_easy_setopt(handle, CURLOPT_URL,
curl_easy_setopt(
handle,
CURLOPT_URL,
[[redirectURL absoluteString] UTF8String]);
/* Reset statistics */
@ -602,12 +626,14 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
NSString * errorString;
errorString = [NSString
stringWithFormat:@"task=%@ status=%ld has no Location header",
stringWithFormat:
@"task=%@ status=%ld has no Location header",
task, statusCode];
error = [NSError
errorWithDomain: NSURLErrorDomain
code: NSURLErrorBadServerResponse
userInfo:@{NSLocalizedDescriptionKey : errorString}];
userInfo: @{ NSLocalizedDescriptionKey:
errorString }];
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY, @"%@", errorString);
@ -647,7 +673,9 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
}
/* Unpause easy handle */
dispatch_async(queue, ^{
dispatch_async(
queue,
^{
curl_easy_pause(handle, CURLPAUSE_CONT);
});
}];
@ -659,7 +687,7 @@ header_callback(char *ptr, size_t size, size_t nitems, void *userdata)
}
return size * nitems;
}
} /* header_callback */
/* CURLOPT_READFUNCTION: read callback for data uploads */
size_t
@ -680,8 +708,10 @@ read_callback(char *buffer, size_t size, size_t nitems, void *userdata)
{
id<NSURLSessionTaskDelegate> delegate = [task delegate];
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ requesting new body stream from delegate", task);
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ requesting new body stream from delegate",
task);
if ([delegate respondsToSelector: needNewBodyStreamSel])
{
@ -700,7 +730,8 @@ read_callback(char *buffer, size_t size, size_t nitems, void *userdata)
}
else
{
NSDebugLLog(GS_NSURLSESSION_DEBUG_KEY,
NSDebugLLog(
GS_NSURLSESSION_DEBUG_KEY,
@"task=%@ no input stream was given and delegate does "
@"not respond to URLSession:task:needNewBodyStream:",
task);
@ -729,7 +760,7 @@ read_callback(char *buffer, size_t size, size_t nitems, void *userdata)
}
return bytesWritten;
}
} /* read_callback */
/* CURLOPT_WRITEFUNCTION: callback for writing received data from easy handle */
static size_t
@ -829,7 +860,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
[dataFragment release];
return size * nmemb;
}
} /* write_callback */
@implementation NSURLSessionTask
{
@ -932,7 +963,9 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
NSData * body = [_originalRequest HTTPBody];
curl_easy_setopt(_easyHandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(_easyHandle, CURLOPT_POSTFIELDSIZE_LARGE,
curl_easy_setopt(
_easyHandle,
CURLOPT_POSTFIELDSIZE_LARGE,
[body length]);
curl_easy_setopt(_easyHandle, CURLOPT_POSTFIELDS, [body bytes]);
}
@ -950,10 +983,14 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
}
/* Configure HTTP method and URL */
curl_easy_setopt(_easyHandle, CURLOPT_CUSTOMREQUEST,
curl_easy_setopt(
_easyHandle,
CURLOPT_CUSTOMREQUEST,
[[_originalRequest HTTPMethod] UTF8String]);
curl_easy_setopt(_easyHandle, CURLOPT_URL,
curl_easy_setopt(
_easyHandle,
CURLOPT_URL,
[[url absoluteString] UTF8String]);
/* This callback function gets called by libcurl as soon as there is data
@ -987,7 +1024,9 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
/* Specifiy our own progress function with the user pointer being the
* current object
*/
curl_easy_setopt(_easyHandle, CURLOPT_XFERINFOFUNCTION,
curl_easy_setopt(
_easyHandle,
CURLOPT_XFERINFOFUNCTION,
progress_callback);
curl_easy_setopt(_easyHandle, CURLOPT_XFERINFODATA, self);
@ -1000,17 +1039,23 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
curl_easy_setopt(_easyHandle, CURLOPT_FOLLOWLOCATION, 0L);
/* Set timeout in connect phase */
curl_easy_setopt(_easyHandle, CURLOPT_CONNECTTIMEOUT,
curl_easy_setopt(
_easyHandle,
CURLOPT_CONNECTTIMEOUT,
(NSInteger)[request timeoutInterval]);
/* Set overall timeout */
curl_easy_setopt(_easyHandle, CURLOPT_TIMEOUT,
curl_easy_setopt(
_easyHandle,
CURLOPT_TIMEOUT,
[configuration timeoutIntervalForResource]);
/* Set to HTTP/3 if requested */
if ([request assumesHTTP3Capable])
{
curl_easy_setopt(_easyHandle, CURLOPT_HTTP_VERSION,
curl_easy_setopt(
_easyHandle,
CURLOPT_HTTP_VERSION,
CURL_HTTP_VERSION_3);
}
@ -1029,7 +1074,9 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
curl_easy_setopt(_easyHandle, CURLOPT_CAINFO_BLOB, &blob);
#else
curl_easy_setopt(_easyHandle, CURLOPT_CAINFO,
curl_easy_setopt(
_easyHandle,
CURLOPT_CAINFO,
[_session _certificatePath]);
#endif
}
@ -1092,7 +1139,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
}
return self;
}
} /* initWithSession */
- (void) _enableAutomaticRedirects: (BOOL)flag
{
@ -1110,7 +1157,9 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
curl_easy_setopt(_easyHandle, CURLOPT_POSTFIELDS, [data bytes]);
/* The method is overwritten by CURLOPT_UPLOAD. Change it back. */
curl_easy_setopt(_easyHandle, CURLOPT_CUSTOMREQUEST,
curl_easy_setopt(
_easyHandle,
CURLOPT_CUSTOMREQUEST,
[[_originalRequest HTTPMethod] UTF8String]);
}
@ -1131,9 +1180,11 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
}
/* The method is overwritten by CURLOPT_UPLOAD. Change it back. */
curl_easy_setopt(_easyHandle, CURLOPT_CUSTOMREQUEST,
curl_easy_setopt(
_easyHandle,
CURLOPT_CUSTOMREQUEST,
[[_originalRequest HTTPMethod] UTF8String]);
}
} /* _enableUploadWithSize */
- (CURL *) _easyHandle
{
@ -1142,7 +1193,9 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
- (void) _setVerbose: (BOOL)flag
{
dispatch_async([_session _workQueue], ^{
dispatch_async(
[_session _workQueue],
^{
curl_easy_setopt(_easyHandle, CURLOPT_VERBOSE, flag ? 1L : 0L);
});
}
@ -1165,6 +1218,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
- (void) _setResponse: (NSURLResponse *)response
{
NSURLResponse * oldResponse = _response;
_response = [response retain];
[oldResponse release];
}
@ -1253,13 +1307,15 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
if (error)
{
NSString * errorDescription = [NSString
stringWithFormat:@"Failed to create temporary file at path %@",
stringWithFormat:
@"Failed to create temporary file at path %@",
path];
*error = [NSError
errorWithDomain: NSCocoaErrorDomain
code: NSURLErrorCannotCreateFile
userInfo:@{NSLocalizedDescriptionKey : errorDescription}];
userInfo: @{ NSLocalizedDescriptionKey:
errorDescription }];
}
return nil;
@ -1269,7 +1325,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
[_taskData setObject: handle forKey: taskTemporaryFileHandleKey];
return handle;
}
} /* _createTemporaryFileHandleWithError */
/* Called in _checkForCompletion */
- (void) _transferFinishedWithCode: (CURLcode)code
@ -1280,7 +1336,8 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
NSFileHandle * handle;
if (nil != (handle = [_taskData objectForKey:taskTemporaryFileHandleKey]))
if (nil !=
(handle = [_taskData objectForKey: taskTemporaryFileHandleKey]))
{
[handle closeFile];
}
@ -1344,7 +1401,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
}
RELEASE(_session);
}
} /* _transferFinishedWithCode */
/* Called in header_callback */
- (void) _setCookiesFromHeaders: (NSDictionary *)headers
@ -1369,7 +1426,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
mainDocumentURL: nil];
}
}
}
} /* _setCookiesFromHeaders */
#pragma mark - Public Methods
@ -1413,7 +1470,9 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
* URLSession:task:didCompleteWithError: is called after receiving
* CURLMSG_DONE in -[NSURLSessionTask _checkForCompletion].
*/
dispatch_async([_session _workQueue], ^{
dispatch_async(
[_session _workQueue],
^{
/* Unpause the easy handle if previously paused */
curl_easy_pause(_easyHandle, CURLPAUSE_CONT);
@ -1496,6 +1555,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
- (void) setDelegate: (id<NSURLSessionTaskDelegate>)delegate
{
id<NSURLSessionTaskDelegate> oldDelegate = _delegate;
_delegate = RETAIN(delegate);
RELEASE(oldDelegate);
}
@ -1508,6 +1568,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
- (void) setEarliestBeginDate: (NSDate *)date
{
NSDate * oldDate = _earliestBeginDate;
_earliestBeginDate = RETAIN(date);
RELEASE(oldDate);
}
@ -1545,6 +1606,7 @@ write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
- (void) setTaskDescription: (NSString *)description
{
NSString * oldDescription = _taskDescription;
_taskDescription = [description copy];
RELEASE(oldDescription);
}

View file

@ -1,31 +1,31 @@
/**
NSURLSessionTaskPrivate.h
Copyright (C) 2017-2024 Free Software Foundation, Inc.
Written by: Hugo Melder <hugo@algoriddim.com>
Date: May 2024
Author: Hugo Melder <hugo@algoriddim.com>
This file is part of GNUStep-base
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
If you are interested in a warranty or support for this source code,
contact Scott Christley <scottc@net-community.com> for more information.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
* NSURLSessionTaskPrivate.h
*
* Copyright (C) 2017-2024 Free Software Foundation, Inc.
*
* Written by: Hugo Melder <hugo@algoriddim.com>
* Date: May 2024
* Author: Hugo Melder <hugo@algoriddim.com>
*
* This file is part of GNUStep-base
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* If you are interested in a warranty or support for this source code,
* contact Scott Christley <scottc@net-community.com> for more information.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110 USA.
*/
#import "Foundation/NSDictionary.h"