Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
URLRequest.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
26#include "MutableDictionary.h"
27#include "URLRequest.h"
28
29#define _Class _URLRequest
30
31#pragma mark - Object
32
36static Object *copy(const Object *self) {
37
38 URLRequest *this = (URLRequest *) self;
39
40 URLRequest *that = $(alloc(URLRequest), initWithURL, this->url);
41
42 if (this->httpBody) {
43 that->httpBody = (Data *) $((Object *) this->httpBody, copy);
44 }
45
46 if (this->httpHeaders) {
47 that->httpHeaders = (Dictionary *) $((Object *) this->httpHeaders, copy);
48 }
49
50 return (Object *) that;
51}
52
56static void dealloc(Object *self) {
57
58 URLRequest *this = (URLRequest *) self;
59
60 release(this->httpBody);
61 release(this->httpHeaders);
62 release(this->url);
63
64 super(Object, self, dealloc);
65}
66
67#pragma mark - URLRequest
68
73static URLRequest *initWithURL(URLRequest *self, URL *url) {
74
75 assert(url);
76
77 self = (URLRequest *) super(Object, self, init);
78 if (self) {
79 self->url = retain(url);
80 }
81
82 return self;
83}
84
89static void setValueForHTTPHeaderField(URLRequest *self, const char *value, const char *field) {
90
91 if (self->httpHeaders == NULL) {
93 }
94
95 String *object = str(value);
96 String *key = str(field);
97
98 $((MutableDictionary *) self->httpHeaders, setObjectForKey, object, key);
99
100 release(object);
101 release(key);
102}
103
104#pragma mark - Class lifecycle
105
109static void initialize(Class *clazz) {
110
111 ((ObjectInterface *) clazz->interface)->copy = copy;
112 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
113
114 ((URLRequestInterface *) clazz->interface)->initWithURL = initWithURL;
115 ((URLRequestInterface *) clazz->interface)->setValueForHTTPHeaderField = setValueForHTTPHeaderField;
116}
117
123 static Class *clazz;
124 static Once once;
125
126 do_once(&once, {
127 clazz = _initialize(&(const ClassDef) {
128 .name = "URLRequest",
129 .superclass = _Object(),
130 .instanceSize = sizeof(URLRequest),
131 .interfaceOffset = offsetof(URLRequest, interface),
132 .interfaceSize = sizeof(URLRequestInterface),
134 });
135 });
136
137 return clazz;
138}
139
140#undef _Class
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
Mutable key-value stores.
static void initialize(Class *clazz)
Definition: URLRequest.c:109
A protocol-agnostic abstraction for requesting resources via URLs.
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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Immutable data buffers.
Definition: Data.h:50
Immutable key-value stores.
Definition: Dictionary.h:60
Mutable key-value stores.
void setObjectForKey(MutableDictionary *self, const ident obj, const ident key)
Sets a pair in this MutableDictionary.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Immutable UTF-8 strings.
Definition: String.h:69
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739
Uniform Resource Locators (RFC 3986).
Definition: URL.h:44
A protocol-agnostic abstraction for requesting resources via URLs.
Definition: URLRequest.h:56
Dictionary * httpHeaders
The HTTP request headers.
Definition: URLRequest.h:77
URLRequest * initWithURL(URLRequest *self, URL *url)
Initializes this URLRequest with the specified URL.
Definition: URLRequest.c:73
URL * url
The URL.
Definition: URLRequest.h:87
void setValueForHTTPHeaderField(URLREquest *self, const char *value, const char *field)
Data * httpBody
The HTTP request body, sent as POST or PUT data.
Definition: URLRequest.h:72
Class * _URLRequest(void)
The URLRequest archetype.
Definition: URLRequest.c:122