Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
URLSessionUploadTask.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <string.h>
26
27#include <curl/curl.h>
28
30
31#define _Class _URLSessionUploadTask
32
33#pragma mark - URLSessionTask
34
38static size_t readFunction(char *data, size_t size, size_t count, ident self) {
39
41
42 const size_t bytesRead = fread(data, size, count, this->file);
43 this->urlSessionTask.bytesSent += bytesRead;
44
45 return bytesRead;
46}
47
51static void setup(URLSessionTask *self) {
52
54
56
57 assert(this->file);
58
59 int err = fseek(this->file, 0, SEEK_END);
60 assert(err == 0);
61
62 self->bytesExpectedToSend = ftell(this->file);
63
64 err = fseek(this->file, 0, SEEK_SET);
65 assert(err == 0);
66
67 curl_easy_setopt(self->locals.handle, CURLOPT_INFILESIZE_LARGE, self->bytesExpectedToSend);
68
69 curl_easy_setopt(self->locals.handle, CURLOPT_READFUNCTION, readFunction);
70 curl_easy_setopt(self->locals.handle, CURLOPT_READDATA, self);
71}
72
73#pragma mark - Class lifecycle
74
78static void initialize(Class *clazz) {
79
80 ((URLSessionTaskInterface *) clazz->interface)->setup = setup;
81}
82
88 static Class *clazz;
89 static Once once;
90
91 do_once(&once, {
92 clazz = _initialize(&(const ClassDef) {
93 .name = "URLSessionUploadTask",
94 .superclass = _URLSessionTask(),
95 .instanceSize = sizeof(URLSessionUploadTask),
96 .interfaceOffset = offsetof(URLSessionUploadTask, interface),
97 .interfaceSize = sizeof(URLSessionUploadTaskInterface),
99 });
100 });
101
102 return clazz;
103}
104
105#undef _Class
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define super(type, obj, method,...)
Immutable UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
static size_t readFunction(char *data, size_t size, size_t count, ident self)
The CURLOPT_READFUNCTION callback.
static void initialize(Class *clazz)
Use upload tasks to send files directly from disk.
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
ident interface
The interface of the Class.
Definition: Class.h:105
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
URL session tasks are handles to pending URL operations.
ident handle
The backing libcurl handle.
void setup(URLSessionTask *)
Sets up this task.
size_t bytesExpectedToSend
The count of bytes this task expects to send.
Class * _URLSessionTask(void)
The URLSessionTask archetype.
Use upload tasks to send files directly from disk.
Class * _URLSessionUploadTask(void)
The URLSessionUploadTask archetype.