Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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 "Hash.h"
27#include "Dictionary.h"
28#include "URLRequest.h"
29
30#define _Class _URLRequest
31
32#pragma mark - Object
33
37static Object *copy(const Object *self) {
38
39 URLRequest *this = (URLRequest *) self;
40
41 URLRequest *that = $(alloc(URLRequest), initWithURL, this->url);
42
43 if (this->httpBody) {
44 that->httpBody = (Data *) $((Object *) this->httpBody, copy);
45 }
46
47 if (this->httpHeaders) {
48 that->httpHeaders = (Dictionary *) $((Object *) this->httpHeaders, copy);
49 }
50
51 that->httpMethod = this->httpMethod;
52
53 return (Object *) that;
54}
55
59static void dealloc(Object *self) {
60
61 URLRequest *this = (URLRequest *) self;
62
63 release(this->httpBody);
64 release(this->httpHeaders);
65 release(this->url);
66
67 super(Object, self, dealloc);
68}
69
73static int hash(const Object *self) {
74
75 const URLRequest *this = (URLRequest *) self;
76
77 int hash = HASH_SEED;
78 hash = HashForObject(hash, this->url);
79 hash = HashForInteger(hash, this->httpMethod);
80 hash = HashForObject(hash, this->httpBody);
81
82 if (this->httpHeaders) {
83
84 const Dictionary *headers = (Dictionary *) this->httpHeaders;
85 Array *keys = $(headers, allKeys);
86
87 int headersHash = 0;
88 for (size_t i = 0; i < keys->count; i++) {
89
90 const Object *key = $(keys, objectAtIndex, i);
91 const Object *value = $(headers, objectForKey, (ident) key);
92
93 int pairHash = HASH_SEED;
94 pairHash = HashForObject(pairHash, (ident) key);
95 pairHash = HashForObject(pairHash, (ident) value);
96
97 headersHash += pairHash;
98 }
99
100 release(keys);
101
102 hash = HashForInteger(hash, headersHash);
103 hash = HashForInteger(hash, this->httpHeaders->count);
104 }
105
106 return hash;
107}
108
112static bool objectEquals(const Object *self, const Object *other) {
113
114 if (self) {
115 return other && $(self, isEqual, other);
116 }
117
118 return other == NULL;
119}
120
124static bool isEqual(const Object *self, const Object *other) {
125
126 if (super(Object, self, isEqual, other)) {
127 return true;
128 }
129
130 if (other && $(other, isKindOfClass, _URLRequest())) {
131
132 const URLRequest *this = (URLRequest *) self;
133 const URLRequest *that = (URLRequest *) other;
134
135 return this->httpMethod == that->httpMethod
136 && objectEquals((Object *) this->url, (Object *) that->url)
137 && objectEquals((Object *) this->httpBody, (Object *) that->httpBody)
138 && objectEquals((Object *) this->httpHeaders, (Object *) that->httpHeaders);
139 }
140
141 return false;
142}
143
144#pragma mark - URLRequest
145
150static URLRequest *initWithURL(URLRequest *self, URL *url) {
151
152 assert(url);
153
154 self = (URLRequest *) super(Object, self, init);
155 if (self) {
156 self->url = retain(url);
157 assert(self->url);
158
159 self->httpMethod = HTTP_GET;
160 }
161
162 return self;
163}
164
169static void setValueForHTTPHeaderField(URLRequest *self, const char *value, const char *field) {
170
171 if (self->httpHeaders == NULL) {
172 self->httpHeaders = (Dictionary *) $(alloc(Dictionary), init);
173 }
174
175 String *object = str(value);
176 String *key = str(field);
177
178 $((Dictionary *) self->httpHeaders, setObjectForKey, object, key);
179
180 release(object);
181 release(key);
182}
183
184#pragma mark - Class lifecycle
185
189static void initialize(Class *clazz) {
190
191 ((ObjectInterface *) clazz->interface)->copy = copy;
192 ((ObjectInterface *) clazz->interface)->hash = hash;
193 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
194 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
195
196 ((URLRequestInterface *) clazz->interface)->initWithURL = initWithURL;
197 ((URLRequestInterface *) clazz->interface)->setValueForHTTPHeaderField = setValueForHTTPHeaderField;
198}
199
205 static Class *clazz;
206 static Once once;
207
208 do_once(&once, {
209 clazz = _initialize(&(const ClassDef) {
210 .name = "URLRequest",
211 .superclass = _Object(),
212 .instanceSize = sizeof(URLRequest),
213 .interfaceOffset = offsetof(URLRequest, interface),
214 .interfaceSize = sizeof(URLRequestInterface),
216 });
217 });
218
219 return clazz;
220}
221
222#undef _Class
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static Array * init(Array *self)
Definition Array.c:420
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
Definition Dictionary.c:634
static Array * allKeys(const Dictionary *self)
Definition Dictionary.c:193
static ident objectForKey(const Dictionary *self, const ident key)
Definition Dictionary.c:439
Key-value stores.
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition Hash.c:72
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Class * _Object(void)
Definition Object.c:136
String * str(const char *fmt,...)
Definition String.c:1084
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
static URLRequest * initWithURL(URLRequest *self, URL *url)
Definition URLRequest.c:150
static void setValueForHTTPHeaderField(URLRequest *self, const char *value, const char *field)
Definition URLRequest.c:169
static bool isEqual(const Object *self, const Object *other)
Definition URLRequest.c:124
static bool objectEquals(const Object *self, const Object *other)
Tests equality of two Objects, accounting for NULL.
Definition URLRequest.c:112
static void dealloc(Object *self)
Definition URLRequest.c:59
static Object * copy(const Object *self)
Definition URLRequest.c:37
static void initialize(Class *clazz)
Definition URLRequest.c:189
Class * _URLRequest(void)
Definition URLRequest.c:204
static int hash(const Object *self)
Definition URLRequest.c:73
A protocol-agnostic abstraction for requesting resources via URLs.
@ HTTP_GET
Definition URLRequest.h:46
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
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72
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
Data buffers.
Definition Data.h:50
Key-value stores.
Definition Dictionary.h:60
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
int hash(const Object *self)
Definition Array.c:129
UTF-8 strings.
Definition String.h:69
Uniform Resource Locators (RFC 3986).
Definition URL.h:44
A protocol-agnostic abstraction for requesting resources via URLs.
Definition URLRequest.h:58
HTTPMethod httpMethod
The HTTP request method.
Definition URLRequest.h:84
Dictionary * httpHeaders
The HTTP request headers.
Definition URLRequest.h:79
URL * url
The URL.
Definition URLRequest.h:89
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:74