Objectively
Ultra-lightweight object oriented framework for GNU 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 228 of file URLSessionDataTask.c.

228 {
229 static Class *clazz;
230 static Once once;
231
232 do_once(&once, {
233 clazz = _initialize(&(const ClassDef) {
234 .name = "URLSessionDataTask",
235 .superclass = _URLSessionTask(),
236 .instanceSize = sizeof(URLSessionDataTask),
237 .interfaceOffset = offsetof(URLSessionDataTask, interface),
238 .interfaceSize = sizeof(URLSessionDataTaskInterface),
240 });
241 });
242
243 return clazz;
244}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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:95
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:98
struct URLResponse * response
The response.
struct URLRequest * request
The request.
struct URLSession * session
The session.

◆ 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:195
#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 $(self, setup);
133
134 CURLcode code = curl_easy_perform(self->locals.handle);
135
136 curl_easy_getinfo(self->locals.handle, CURLINFO_RESPONSE_CODE, (long *) &self->response->httpStatusCode);
137
138 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
139
140 if (code == CURLE_OK) {
141 $(this, cacheResponse);
142 }
143
144 $(self, teardown);
145}
static void teardown(void)
Called atexit to teardown Objectively.
Definition Class.c:46
static void setup(void)
Called when initializing Object to setup Objectively.
Definition Class.c:73
static void materializeCachedResponse(URLSessionDataTask *self)
Copies a cached response into this task's live response and data fields.
static void cacheResponse(URLSessionDataTask *self)
int httpStatusCode
The HTTP response status code.
Definition URLResponse.h:63
ident handle
The backing libcurl handle.

◆ initialize()

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

Definition at line 212 of file URLSessionDataTask.c.

212 {
213
214 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
215
216 ((URLSessionTaskInterface *) clazz->interface)->cancel = cancel;
217 ((URLSessionTaskInterface *) clazz->interface)->execute = execute;
218 ((URLSessionTaskInterface *) clazz->interface)->setup = setup;
219 ((URLSessionTaskInterface *) clazz->interface)->resume = resume;
220
221 ((URLSessionDataTaskInterface *) clazz->interface)->cacheResponse = cacheResponse;
222}
static void resume(URLSessionTask *self)
static void execute(URLSessionTask *self)
ident interface
The interface of the Class.
Definition Class.h:105

◆ 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 150 of file URLSessionDataTask.c.

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

◆ setup()

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

Definition at line 199 of file URLSessionDataTask.c.

199 {
200
201 super(URLSessionTask, self, setup);
202
203 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEFUNCTION, writeFunction);
204 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEDATA, self);
205}
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 179 of file URLSessionDataTask.c.

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