Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
URLRequest.c File Reference
#include <assert.h>
#include "Hash.h"
#include "Dictionary.h"
#include "URLRequest.h"

Go to the source code of this file.

Macros

#define _Class   _URLRequest
 

Functions

Class_URLRequest (void)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static URLRequestinitWithURL (URLRequest *self, URL *url)
 
static bool isEqual (const Object *self, const Object *other)
 
static bool objectEquals (const Object *self, const Object *other)
 Tests equality of two Objects, accounting for NULL.
 
static void setValueForHTTPHeaderField (URLRequest *self, const char *value, const char *field)
 

Macro Definition Documentation

◆ _Class

#define _Class   _URLRequest

Definition at line 30 of file URLRequest.c.

Function Documentation

◆ _URLRequest()

Class * _URLRequest ( void  )

Definition at line 204 of file URLRequest.c.

204 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
static void initialize(Class *clazz)
Definition URLRequest.c:189
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
A protocol-agnostic abstraction for requesting resources via URLs.
Definition URLRequest.h:58

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 37 of file URLRequest.c.

37 {
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}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static URLRequest * initWithURL(URLRequest *self, URL *url)
Definition URLRequest.c:150
static Object * copy(const Object *self)
Definition URLRequest.c:37
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
HTTPMethod httpMethod
The HTTP request method.
Definition URLRequest.h:84
Dictionary * httpHeaders
The HTTP request headers.
Definition URLRequest.h:79
Data * httpBody
The HTTP request body, sent as POST or PUT data.
Definition URLRequest.h:74

◆ dealloc()

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

Definition at line 59 of file URLRequest.c.

59 {
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}
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 dealloc(Object *self)
Definition URLRequest.c:59

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 73 of file URLRequest.c.

73 {
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}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static Array * allKeys(const Dictionary *self)
Definition Dictionary.c:193
static ident objectForKey(const Dictionary *self, const ident key)
Definition Dictionary.c:439
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
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
static int hash(const Object *self)
Definition URLRequest.c:73
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Array.h:72

◆ initialize()

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

Definition at line 189 of file URLRequest.c.

189 {
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}
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
ident interface
The interface of the Class.
Definition Class.h:105
int hash(const Object *self)
Definition Array.c:129
void setValueForHTTPHeaderField(URLREquest *self, const char *value, const char *field)

◆ initWithURL()

static URLRequest * initWithURL ( URLRequest self,
URL url 
)
static

Definition at line 150 of file URLRequest.c.

150 {
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}
static Array * init(Array *self)
Definition Array.c:420
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
@ HTTP_GET
Definition URLRequest.h:46
URL * url
The URL.
Definition URLRequest.h:89

◆ isEqual()

static bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 124 of file URLRequest.c.

124 {
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}
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
static bool objectEquals(const Object *self, const Object *other)
Tests equality of two Objects, accounting for NULL.
Definition URLRequest.c:112
Class * _URLRequest(void)
Definition URLRequest.c:204

◆ objectEquals()

static bool objectEquals ( const Object self,
const Object other 
)
static

Tests equality of two Objects, accounting for NULL.

Definition at line 112 of file URLRequest.c.

112 {
113
114 if (self) {
115 return other && $(self, isEqual, other);
116 }
117
118 return other == NULL;
119}

◆ setValueForHTTPHeaderField()

static void setValueForHTTPHeaderField ( URLRequest self,
const char *  value,
const char *  field 
)
static

Definition at line 169 of file URLRequest.c.

169 {
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}
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
Definition Dictionary.c:634
String * str(const char *fmt,...)
Definition String.c:1084
UTF-8 strings.
Definition String.h:69