ObjFW  Diff

Differences From Artifact [afa9ff2eb3]:

To Artifact [26ed4a79d0]:


26
27
28
29
30
31
32
33
34


35
36
37
38
39
40
41
42
43
44
45


46
47
48
49
50
51
52
53


54
55
56
57
58
59


60
61
62
63



64
65
66
67
68
69
70


71
72
73
74
75
76



77
78
79
80
81
82
83


84
85
86
87
88
89
90
91
92
93
94
95
96
97



98
99
100
101
102
103
104


105
106
107
108
109
110
111
26
27
28
29
30
31
32


33
34
35
36
37
38
39
40
41
42
43


44
45
46
47
48
49
50
51


52
53
54
55
56
57


58
59
60



61
62
63
64
65
66
67
68


69
70
71
72
73



74
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90
91
92
93
94



95
96
97
98
99
100
101
102


103
104
105
106
107
108
109
110
111







-
-
+
+









-
-
+
+






-
-
+
+




-
-
+
+

-
-
-
+
+
+





-
-
+
+



-
-
-
+
+
+





-
-
+
+











-
-
-
+
+
+





-
-
+
+








typedef enum of_http_request_type_t {
	OF_HTTP_REQUEST_TYPE_GET,
	OF_HTTP_REQUEST_TYPE_POST,
	OF_HTTP_REQUEST_TYPE_HEAD
} of_http_request_type_t;

/**
 * \brief A delegate for OFHTTPRequests.
/*!
 * @brief A delegate for OFHTTPRequests.
 */
#ifndef OF_HTTP_REQUEST_M
@protocol OFHTTPRequestDelegate <OFObject>
#else
@protocol OFHTTPRequestDelegate
#endif
#ifdef OF_HAVE_OPTIONAL_PROTOCOLS
@optional
#endif
/**
 * \brief A callback which is called when an OFHTTPRequest creates a socket.
/*!
 * @brief A callback which is called when an OFHTTPRequest creates a socket.
 *
 * This is useful if the connection is using HTTPS and the server requires a
 * client certificate. This callback can then be used to tell the TLS socket
 * about the certificate. Another use case is to tell the socket about a SOCKS5
 * proxy it should use for this connection.
 *
 * \param request The OFHTTPRequest that created a socket
 * \param socket The socket created by the OFHTTPRequest
 * @param request The OFHTTPRequest that created a socket
 * @param socket The socket created by the OFHTTPRequest
 */
-   (void)request: (OFHTTPRequest*)request
  didCreateSocket: (OFTCPSocket*)socket;

/**
 * \brief A callback which is called when an OFHTTPRequest received headers.
/*!
 * @brief A callback which is called when an OFHTTPRequest received headers.
 *
 * \param request The OFHTTPRequest which received the headers
 * \param headers The headers received
 * \param statusCode The status code received
 * @param request The OFHTTPRequest which received the headers
 * @param headers The headers received
 * @param statusCode The status code received
 */
-     (void)request: (OFHTTPRequest*)request
  didReceiveHeaders: (OFDictionary*)headers
     withStatusCode: (int)statusCode;

/**
 * \brief A callback which is called when an OFHTTPRequest received data.
/*!
 * @brief A callback which is called when an OFHTTPRequest received data.
 *
 * This is useful for example if you want to update a status display.
 *
 * \param request The OFHTTPRequest which received data
 * \param data The data the OFHTTPRequest received
 * \param length The length of the data received, in bytes
 * @param request The OFHTTPRequest which received data
 * @param data The data the OFHTTPRequest received
 * @param length The length of the data received, in bytes
 */
-  (void)request: (OFHTTPRequest*)request
  didReceiveData: (const char*)data
      withLength: (size_t)length;

/**
 * \brief A callback which is called when an OFHTTPRequest will follow a
/*!
 * @brief A callback which is called when an OFHTTPRequest will follow a
 *	  redirect.
 *
 * If you want to get the headers and data for each redirect, set the number of
 * redirects to 0 and perform a new OFHTTPRequest for each redirect. However,
 * this callback will not be called then and you have to look at the status code
 * to detect a redirect.
 *
 * This callback will only be called if the OFHTTPRequest will follow a
 * redirect. If the maximum number of redirects has been reached already, this
 * callback will not be called.
 *
 * \param request The OFHTTPRequest which will follow a redirect
 * \param URL The URL to which it will follow a redirect
 * \return A boolean whether the OFHTTPRequest should follow the redirect
 * @param request The OFHTTPRequest which will follow a redirect
 * @param URL The URL to which it will follow a redirect
 * @return A boolean whether the OFHTTPRequest should follow the redirect
 */
-	 (BOOL)request: (OFHTTPRequest*)request
  willFollowRedirectTo: (OFURL*)URL;
@end

/**
 * \brief A class for storing and performing HTTP requests.
/*!
 * @brief A class for storing and performing HTTP requests.
 */
@interface OFHTTPRequest: OFObject
{
	OFURL *URL;
	of_http_request_type_t requestType;
	OFString *queryString;
	OFDictionary *headers;
120
121
122
123
124
125
126
127
128


129
130

131
132
133
134
135


136
137
138


139
140
141
142
143


144
145
146


147
148
149
150
151


152
153

154
155
156
157
158


159
160

161
162
163
164
165


166
167

168
169
170
171
172


173
174

175
176
177
178
179


180
181

182
183
184
185
186


187
188

189
190
191
192
193


194
195

196
197
198
199
200


201
202

203
204
205
206
207


208
209

210
211
212
213
214


215
216

217
218
219
220
221


222
223

224
225
226
227
228


229
230

231
232
233
234
235


236
237
238
239
240

241
242
243
244
245


246
247

248
249
250
251
252


253
254

255
256
257
258
259


260
261

262
263
264

265
266
267
268
269
270


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290


291
292

293
294
295
296
297


298
299

300
301
302
303
304


305
306
307
308

309
310
311
312
313
314
315
316
120
121
122
123
124
125
126


127
128
129

130
131
132
133


134
135
136


137
138
139
140
141


142
143
144


145
146
147
148
149


150
151
152

153
154
155
156


157
158
159

160
161
162
163


164
165
166

167
168
169
170


171
172
173

174
175
176
177


178
179
180

181
182
183
184


185
186
187

188
189
190
191


192
193
194

195
196
197
198


199
200
201

202
203
204
205


206
207
208

209
210
211
212


213
214
215

216
217
218
219


220
221
222

223
224
225
226


227
228
229

230
231
232
233


234
235
236
237
238
239

240
241
242
243


244
245
246

247
248
249
250


251
252
253

254
255
256
257


258
259
260

261
262
263

264
265
266
267
268


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288


289
290
291

292
293
294
295


296
297
298

299
300
301
302


303
304
305
306
307

308
309
310
311
312
313
314
315
316







-
-
+
+

-
+



-
-
+
+

-
-
+
+



-
-
+
+

-
-
+
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+




-
+



-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+

-
+


-
+




-
-
+
+


















-
-
+
+

-
+



-
-
+
+

-
+



-
-
+
+



-
+








@property (copy) OFString *queryString;
@property (copy) OFDictionary *headers;
@property BOOL redirectsFromHTTPSToHTTPAllowed;
@property (assign) id <OFHTTPRequestDelegate> delegate;
@property BOOL storesData;
#endif

/**
 * \brief Creates a new OFHTTPRequest.
/*!
 * @brief Creates a new OFHTTPRequest.
 *
 * \return A new, autoreleased OFHTTPRequest
 * @return A new, autoreleased OFHTTPRequest
 */
+ (instancetype)request;

/**
 * \brief Creates a new OFHTTPRequest with the specified URL.
/*!
 * @brief Creates a new OFHTTPRequest with the specified URL.
 *
 * \param URL The URL for the request
 * \return A new, autoreleased OFHTTPRequest
 * @param URL The URL for the request
 * @return A new, autoreleased OFHTTPRequest
 */
+ (instancetype)requestWithURL: (OFURL*)URL;

/**
 * \brief Initializes an already allocated OFHTTPRequest with the specified URL.
/*!
 * @brief Initializes an already allocated OFHTTPRequest with the specified URL.
 *
 * \param URL The URL for the request
 * \return An initialized OFHTTPRequest
 * @param URL The URL for the request
 * @return An initialized OFHTTPRequest
 */
- initWithURL: (OFURL*)URL;

/**
 * \brief Sets the URL of the HTTP request.
/*!
 * @brief Sets the URL of the HTTP request.
 *
 * \param URL The URL of the HTTP request
 * @param URL The URL of the HTTP request
 */
- (void)setURL: (OFURL*)URL;

/**
 * \brief Returns the URL of the HTTP request.
/*!
 * @brief Returns the URL of the HTTP request.
 *
 * \return The URL of the HTTP request
 * @return The URL of the HTTP request
 */
- (OFURL*)URL;

/**
 * \brief Sets the request type of the HTTP request.
/*!
 * @brief Sets the request type of the HTTP request.
 *
 * \param requestType The request type of the HTTP request
 * @param requestType The request type of the HTTP request
 */
- (void)setRequestType: (of_http_request_type_t)requestType;

/**
 * \brief Returns the request type of the HTTP request.
/*!
 * @brief Returns the request type of the HTTP request.
 *
 * \return The request type of the HTTP request
 * @return The request type of the HTTP request
 */
- (of_http_request_type_t)requestType;

/**
 * \brief Sets the query string of the HTTP request.
/*!
 * @brief Sets the query string of the HTTP request.
 *
 * \param queryString The query string of the HTTP request
 * @param queryString The query string of the HTTP request
 */
- (void)setQueryString: (OFString*)queryString;

/**
 * \brief Returns the query string of the HTTP request.
/*!
 * @brief Returns the query string of the HTTP request.
 *
 * \return The query string of the HTTP request
 * @return The query string of the HTTP request
 */
- (OFString*)queryString;

/**
 * \brief Sets a dictionary with headers for the HTTP request.
/*!
 * @brief Sets a dictionary with headers for the HTTP request.
 *
 * \param headers A dictionary with headers for the HTTP request
 * @param headers A dictionary with headers for the HTTP request
 */
- (void)setHeaders: (OFDictionary*)headers;

/**
 * \brief Retrusn a dictionary with headers for the HTTP request.
/*!
 * @brief Retrusn a dictionary with headers for the HTTP request.
 *
 * \return A dictionary with headers for the HTTP request.
 * @return A dictionary with headers for the HTTP request.
 */
- (OFDictionary*)headers;

/**
 * \brief Sets whether redirects from HTTPS to HTTP are allowed.
/*!
 * @brief Sets whether redirects from HTTPS to HTTP are allowed.
 *
 * \param allowed Whether redirects from HTTPS to HTTP are allowed
 * @param allowed Whether redirects from HTTPS to HTTP are allowed
 */
- (void)setRedirectsFromHTTPSToHTTPAllowed: (BOOL)allowed;

/**
 * \brief Returns whether redirects from HTTPS to HTTP will be allowed
/*!
 * @brief Returns whether redirects from HTTPS to HTTP will be allowed
 *
 * \return Whether redirects from HTTPS to HTTP will be allowed
 * @return Whether redirects from HTTPS to HTTP will be allowed
 */
- (BOOL)redirectsFromHTTPSToHTTPAllowed;

/**
 * \brief Sets the delegate of the HTTP request.
/*!
 * @brief Sets the delegate of the HTTP request.
 *
 * \param delegate The delegate of the HTTP request
 * @param delegate The delegate of the HTTP request
 */
- (void)setDelegate: (id <OFHTTPRequestDelegate>)delegate;

/**
 * \brief Returns the delegate of the HTTP reqeust.
/*!
 * @brief Returns the delegate of the HTTP reqeust.
 *
 * \return The delegate of the HTTP request
 * @return The delegate of the HTTP request
 */
- (id <OFHTTPRequestDelegate>)delegate;

/**
 * \brief Sets whether an OFDataArray with the data should be created.
/*!
 * @brief Sets whether an OFDataArray with the data should be created.
 *
 * Setting this to NO is only useful if you are using the delegate to handle the
 * data.
 *
 * \param storesData Whether to store the data in an OFDataArray
 * @param storesData Whether to store the data in an OFDataArray
 */
- (void)setStoresData: (BOOL)storesData;

/**
 * \brief Returns whether an OFDataArray with the date should be created.
/*!
 * @brief Returns whether an OFDataArray with the date should be created.
 *
 * \return Whether an OFDataArray with the data should be created
 * @return Whether an OFDataArray with the data should be created
 */
- (BOOL)storesData;

/**
 * \brief Performs the HTTP request and returns an OFHTTPRequestResult.
/*!
 * @brief Performs the HTTP request and returns an OFHTTPRequestResult.
 *
 * \return An OFHTTPRequestResult with the result of the HTTP request
 * @return An OFHTTPRequestResult with the result of the HTTP request
 */
- (OFHTTPRequestResult*)perform;

/**
 * \brief Performs the HTTP request and returns an OFHTTPRequestResult.
/*!
 * @brief Performs the HTTP request and returns an OFHTTPRequestResult.
 *
 * \param redirects The maximum number of redirects after which no further
 * @param redirects The maximum number of redirects after which no further
 *		    attempt is done to follow the redirect, but instead the
 *		    redirect is returned as an OFHTTPRequest
 * \return An OFHTTPRequestResult with the result of the HTTP request
 * @return An OFHTTPRequestResult with the result of the HTTP request
 */
- (OFHTTPRequestResult*)performWithRedirects: (size_t)redirects;
@end

/**
 * \brief A class for storing the result of an HTTP request.
/*!
 * @brief A class for storing the result of an HTTP request.
 */
@interface OFHTTPRequestResult: OFObject
{
	short statusCode;
	OFDataArray *data;
	OFDictionary *headers;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly) short statusCode;
@property (readonly, copy) OFDictionary *headers;
@property (readonly, retain) OFDataArray *data;
#endif

- initWithStatusCode: (short)status
	     headers: (OFDictionary*)headers
		data: (OFDataArray*)data;

/**
 * \brief Returns the state code of the result of the HTTP request.
/*!
 * @brief Returns the state code of the result of the HTTP request.
 *
 * \return The status code of the result of the HTTP request
 * @return The status code of the result of the HTTP request
 */
- (short)statusCode;

/**
 * \brief Returns the headers of the result of the HTTP request.
/*!
 * @brief Returns the headers of the result of the HTTP request.
 *
 * \return The headers of the result of the HTTP request
 * @return The headers of the result of the HTTP request
 */
- (OFDictionary*)headers;

/**
 * \brief Returns the data received for the HTTP request.
/*!
 * @brief Returns the data received for the HTTP request.
 *
 * Returns nil if storesData was set to NO.
 *
 * \return The data received for the HTTP request
 * @return The data received for the HTTP request
 */
- (OFDataArray*)data;
@end

@interface OFObject (OFHTTPRequestDelegate) <OFHTTPRequestDelegate>
@end

extern Class of_http_request_tls_socket_class;