Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
URLSessionDataTask.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "URLSession.h"
#include "URLCache.h"
#include "Data.h"
#include "URLSessionDataTask.h"

Go to the source code of this file.

Macros

#define _Class   _URLSessionDataTask
 

Functions

Class * _URLSessionDataTask (void)
 
static void cacheResponse (URLSessionDataTask *self)
 
static void cancel (URLSessionTask *self)
 
static void dealloc (Object *self)
 
static void execute (URLSessionTask *self)
 
static void initialize (Class *clazz)
 
static void materializeCachedResponse (URLSessionDataTask *self)
 Copies a cached response into this task's live response and data fields.
 
static void resume (URLSessionTask *self)
 
static void setup (URLSessionTask *self)
 
static size_t writeFunction (char *data, size_t size, size_t count, ident self)
 The CURLOPT_WRITEFUNCTION callback.
 

Macro Definition Documentation

◆ _Class

#define _Class   _URLSessionDataTask

Definition at line 35 of file URLSessionDataTask.c.

Function Documentation

◆ _URLSessionDataTask()

Class * _URLSessionDataTask ( void  )

Definition at line 227 of file URLSessionDataTask.c.

227 {
228 static Class *clazz;
229 static Once once;
230
231 do_once(&once, {
232 clazz = _initialize(&(const ClassDef) {
233 .name = "URLSessionDataTask",
234 .superclass = _URLSessionTask(),
235 .instanceSize = sizeof(URLSessionDataTask),
236 .interfaceSize = sizeof(URLSessionDataTaskInterface),
237 .initialize = initialize,
238 });
239 });
240
241 return clazz;
242}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
static void initialize(Class *clazz)
Class * _URLSessionTask(void)
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:90
Use data tasks to send and receive Data in-memory.

◆ cacheResponse()

static void cacheResponse ( URLSessionDataTask *  self)
static

Definition at line 82 of file URLSessionDataTask.c.

82 {
83
84 if (self->cachedResponse) {
85 return;
86 }
87
89 if (cache) {
91 }
92}
static void storeCachedResponseForRequest(URLCache *self, const URLRequest *request, const URLResponse *response, const Data *data)
Definition URLCache.c:356
A cache for HTTP responses.
Definition URLCache.h:44
URLCache * urlCache
The cache for URL responses, or NULL to disable caching.
URLCachedResponse * cachedResponse
A cached response, if this task was fulfilled from URLCache.
Data * data
The data received.
URLSessionTask urlSessionTask
The superclass.
URLSessionConfiguration * configuration
The session configuration.
Definition URLSession.h:81
struct URLResponse * response
The response.
struct URLRequest * request
The request.
struct URLSession * session
The session, which is not retained.

◆ cancel()

static void cancel ( URLSessionTask *  self)
static
See also
URLSessionTask::cancel(URLSessionTask *)

Definition at line 97 of file URLSessionDataTask.c.

97 {
98
100
101 if (this->cachedResponse) {
102 if (this->urlSessionTask.state != URLSESSIONTASK_COMPLETED) {
103 release(this->cachedResponse);
104 this->cachedResponse = NULL;
105 this->urlSessionTask.state = URLSESSIONTASK_CANCELED;
106 }
107
108 return;
109 }
110
112}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
#define super(type, obj, method,...)
static void cancel(URLSessionTask *self)
@ URLSESSIONTASK_CANCELED
@ URLSESSIONTASK_COMPLETED
URL session tasks are handles to pending URL operations.

◆ dealloc()

static void dealloc ( Object *  self)
static
See also
Object::dealloc(Object *)

Definition at line 42 of file URLSessionDataTask.c.

42 {
43
45
46 release(this->cachedResponse);
47 release(this->data);
48
49 super(Object, self, dealloc);
50}
static Data * data(void)
Definition Data.c:286
static void dealloc(Object *self)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ execute()

static void execute ( URLSessionTask *  self)
static
See also
URLSessionTask::execute(URLSessionTask *)

Definition at line 117 of file URLSessionDataTask.c.

117 {
118
119 URLSessionDataTask *this = (URLSessionDataTask *) self;
120
121 if (this->urlSessionTask.state == URLSESSIONTASK_CANCELED
122 || this->urlSessionTask.state == URLSESSIONTASK_COMPLETED) {
123 return;
124 }
125
126 if (this->cachedResponse) {
128 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
129 return;
130 }
131
132 URLCache *cache = self->session->configuration->urlCache;
133 if (cache) {
134 retain(cache);
135 }
136
138
139 if (cache && self->state == URLSESSIONTASK_COMPLETED && self->result == CURLE_OK) {
140 $(cache, storeCachedResponseForRequest, self->request, self->response, this->data);
141 }
142
143 release(cache);
144}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:372
static void materializeCachedResponse(URLSessionDataTask *self)
Copies a cached response into this task's live response and data fields.
static void execute(URLSessionTask *self)
int result
The libcurl result code (CURLcode) of the transfer, 0 on success.
URLSessionTaskState state
The state.

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 211 of file URLSessionDataTask.c.

211 {
212
213 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
214
215 ((URLSessionTaskInterface *) clazz->interface)->cancel = cancel;
216 ((URLSessionTaskInterface *) clazz->interface)->execute = execute;
217 ((URLSessionTaskInterface *) clazz->interface)->setup = setup;
218 ((URLSessionTaskInterface *) clazz->interface)->resume = resume;
219
220 ((URLSessionDataTaskInterface *) clazz->interface)->cacheResponse = cacheResponse;
221}
static void setup(void)
Called when initializing Object to setup Objectively.
Definition Class.c:113
static void resume(URLSessionTask *self)
static void cacheResponse(URLSessionDataTask *self)
ident interface
The interface of the Class.
Definition Class.h:100

◆ materializeCachedResponse()

static void materializeCachedResponse ( URLSessionDataTask *  self)
static

Copies a cached response into this task's live response and data fields.

Definition at line 57 of file URLSessionDataTask.c.

57 {
58
59 if (self->cachedResponse == NULL) {
60 return;
61 }
62
65
66 release(self->data);
67 self->data = (Data *) $((Object *) self->cachedResponse->data, copy);
68
72 self->urlSessionTask.bytesSent = 0;
73
75 self->cachedResponse = NULL;
76}
static Object * copy(const Object *self)
Definition Array.c:84
Data buffers.
Definition Data.h:50
URLResponse * response
The HTTP response.
Data * data
The response body.
size_t size
The cached body size.
A protocol-agnostic abstraction for URLSessionTask responses.
Definition URLResponse.h:42
size_t bytesExpectedToSend
The count of bytes this task expects to send.
size_t bytesReceived
The count of bytes received.
size_t bytesSent
The count of bytes sent.
size_t bytesExpectedToReceive
The count of bytes this task expects to receive.

◆ resume()

static void resume ( URLSessionTask *  self)
static
See also
URLSessionTask::resume(URLSessionTask *)

Definition at line 149 of file URLSessionDataTask.c.

149 {
150
151 URLSessionDataTask *this = (URLSessionDataTask *) self;
152
153 if (this->cachedResponse) {
154 switch (this->urlSessionTask.state) {
159 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
160
161 if (this->urlSessionTask.completion) {
162 this->urlSessionTask.completion((URLSessionTask *) this, true);
163 }
164 break;
165 default:
166 break;
167 }
168
169 return;
170 }
171
173}
@ URLSESSIONTASK_RESUMING
@ URLSESSIONTASK_SUSPENDING
@ URLSESSIONTASK_SUSPENDED

◆ setup()

static void setup ( URLSessionTask *  self)
static
See also
URLSessionTask::setup(URLSessionTask *)

Definition at line 198 of file URLSessionDataTask.c.

198 {
199
200 super(URLSessionTask, self, setup);
201
202 curl_easy_setopt(self->handle, CURLOPT_WRITEFUNCTION, writeFunction);
203 curl_easy_setopt(self->handle, CURLOPT_WRITEDATA, self);
204}
static size_t writeFunction(char *data, size_t size, size_t count, ident self)
The CURLOPT_WRITEFUNCTION callback.

◆ writeFunction()

static size_t writeFunction ( char *  data,
size_t  size,
size_t  count,
ident  self 
)
static

The CURLOPT_WRITEFUNCTION callback.

Definition at line 178 of file URLSessionDataTask.c.

178 {
179
180 URLSessionDataTask *this = (URLSessionDataTask *) self;
181
182 const uint8_t *bytes = (uint8_t *) data;
183 const size_t bytesReceived = size * count;
184
185 if (this->data == NULL) {
186 this->data = (Data *) $(alloc(Data), init);
187 }
188
189 $((Data *) this->data, appendBytes, bytes, bytesReceived);
190
191 this->urlSessionTask.bytesReceived += bytesReceived;
192 return bytesReceived;
193}
static Array * init(Array *self)
Definition Array.c:420
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
static void appendBytes(Data *self, const uint8_t *bytes, size_t length)
Definition Data.c:262