JFC
[m6w6/ext-http] / http_request_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_CURL
16 #include "php_http.h"
17
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
19
20 #include "zend_interfaces.h"
21
22 #include "php_http_api.h"
23 #include "php_http_cookie_api.h"
24 #include "php_http_exception_object.h"
25 #include "php_http_message_api.h"
26 #include "php_http_message_object.h"
27 #include "php_http_request_api.h"
28 #include "php_http_request_object.h"
29 #include "php_http_request_pool_api.h"
30 #include "php_http_url_api.h"
31
32 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpRequest, method, 0, req_args)
33 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpRequest, method, 0)
34 #define HTTP_REQUEST_ME(method, visibility) PHP_ME(HttpRequest, method, HTTP_ARGS(HttpRequest, method), visibility)
35 #define HTTP_REQUEST_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpRequest, method))
36 #define HTTP_REQUEST_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpRequest_##al), HTTP_ARGS(HttpRequest, al), vis)
37
38 HTTP_BEGIN_ARGS(__construct, 0)
39 HTTP_ARG_VAL(url, 0)
40 HTTP_ARG_VAL(method, 0)
41 HTTP_ARG_VAL(options, 0)
42 HTTP_END_ARGS;
43
44 HTTP_BEGIN_ARGS(factory, 0)
45 HTTP_ARG_VAL(url, 0)
46 HTTP_ARG_VAL(method, 0)
47 HTTP_ARG_VAL(options, 0)
48 HTTP_ARG_VAL(class_name, 0)
49 HTTP_END_ARGS;
50
51 HTTP_EMPTY_ARGS(getOptions);
52 HTTP_BEGIN_ARGS(setOptions, 0)
53 HTTP_ARG_VAL(options, 0)
54 HTTP_END_ARGS;
55
56 HTTP_EMPTY_ARGS(getSslOptions);
57 HTTP_BEGIN_ARGS(setSslOptions, 0)
58 HTTP_ARG_VAL(ssl_options, 0)
59 HTTP_END_ARGS;
60
61 HTTP_BEGIN_ARGS(addSslOptions, 0)
62 HTTP_ARG_VAL(ssl_optins, 0)
63 HTTP_END_ARGS;
64
65 HTTP_EMPTY_ARGS(getHeaders);
66 HTTP_BEGIN_ARGS(setHeaders, 0)
67 HTTP_ARG_VAL(headers, 0)
68 HTTP_END_ARGS;
69
70 HTTP_BEGIN_ARGS(addHeaders, 1)
71 HTTP_ARG_VAL(headers, 0)
72 HTTP_END_ARGS;
73
74 HTTP_EMPTY_ARGS(getCookies);
75 HTTP_BEGIN_ARGS(setCookies, 0)
76 HTTP_ARG_VAL(cookies, 0)
77 HTTP_END_ARGS;
78
79 HTTP_BEGIN_ARGS(addCookies, 1)
80 HTTP_ARG_VAL(cookies, 0)
81 HTTP_END_ARGS;
82
83 HTTP_EMPTY_ARGS(enableCookies);
84 HTTP_BEGIN_ARGS(resetCookies, 0)
85 HTTP_ARG_VAL(session_only, 0)
86 HTTP_END_ARGS;
87 HTTP_EMPTY_ARGS(flushCookies);
88
89 HTTP_EMPTY_ARGS(getUrl);
90 HTTP_BEGIN_ARGS(setUrl, 1)
91 HTTP_ARG_VAL(url, 0)
92 HTTP_END_ARGS;
93
94 HTTP_EMPTY_ARGS(getMethod);
95 HTTP_BEGIN_ARGS(setMethod, 1)
96 HTTP_ARG_VAL(request_method, 0)
97 HTTP_END_ARGS;
98
99 HTTP_EMPTY_ARGS(getContentType);
100 HTTP_BEGIN_ARGS(setContentType, 1)
101 HTTP_ARG_VAL(content_type, 0)
102 HTTP_END_ARGS;
103
104 HTTP_EMPTY_ARGS(getQueryData);
105 HTTP_BEGIN_ARGS(setQueryData, 0)
106 HTTP_ARG_VAL(query_data, 0)
107 HTTP_END_ARGS;
108
109 HTTP_BEGIN_ARGS(addQueryData, 1)
110 HTTP_ARG_VAL(query_data, 0)
111 HTTP_END_ARGS;
112
113 HTTP_EMPTY_ARGS(getPostFields);
114 HTTP_BEGIN_ARGS(setPostFields, 0)
115 HTTP_ARG_VAL(post_fields, 0)
116 HTTP_END_ARGS;
117
118 HTTP_BEGIN_ARGS(addPostFields, 1)
119 HTTP_ARG_VAL(post_fields, 0)
120 HTTP_END_ARGS;
121
122 HTTP_EMPTY_ARGS(getPostFiles);
123 HTTP_BEGIN_ARGS(setPostFiles, 0)
124 HTTP_ARG_VAL(post_files, 0)
125 HTTP_END_ARGS;
126
127 HTTP_BEGIN_ARGS(addPostFile, 2)
128 HTTP_ARG_VAL(formname, 0)
129 HTTP_ARG_VAL(filename, 0)
130 HTTP_ARG_VAL(content_type, 0)
131 HTTP_END_ARGS;
132
133 HTTP_EMPTY_ARGS(getBody);
134 HTTP_BEGIN_ARGS(setBody, 0)
135 HTTP_ARG_VAL(request_body_data, 0)
136 HTTP_END_ARGS;
137
138 HTTP_BEGIN_ARGS(addBody, 1)
139 HTTP_ARG_VAL(request_body_data, 0)
140 HTTP_END_ARGS;
141
142 HTTP_EMPTY_ARGS(getPutFile);
143 HTTP_BEGIN_ARGS(setPutFile, 0)
144 HTTP_ARG_VAL(filename, 0)
145 HTTP_END_ARGS;
146
147 HTTP_EMPTY_ARGS(getPutData);
148 HTTP_BEGIN_ARGS(setPutData, 0)
149 HTTP_ARG_VAL(put_data, 0)
150 HTTP_END_ARGS;
151
152 HTTP_BEGIN_ARGS(addPutData, 1)
153 HTTP_ARG_VAL(put_data, 0)
154 HTTP_END_ARGS;
155
156 HTTP_EMPTY_ARGS(getResponseData);
157 HTTP_BEGIN_ARGS(getResponseHeader, 0)
158 HTTP_ARG_VAL(name, 0)
159 HTTP_END_ARGS;
160
161 HTTP_BEGIN_ARGS(getResponseCookies, 0)
162 HTTP_ARG_VAL(flags, 0)
163 HTTP_ARG_VAL(allowed_extras, 0)
164 HTTP_END_ARGS;
165
166 HTTP_EMPTY_ARGS(getResponseBody);
167 HTTP_EMPTY_ARGS(getResponseCode);
168 HTTP_EMPTY_ARGS(getResponseStatus);
169 HTTP_BEGIN_ARGS(getResponseInfo, 0)
170 HTTP_ARG_VAL(name, 0)
171 HTTP_END_ARGS;
172
173 HTTP_EMPTY_ARGS(getResponseMessage);
174 HTTP_EMPTY_ARGS(getRawResponseMessage);
175 HTTP_EMPTY_ARGS(getRequestMessage);
176 HTTP_EMPTY_ARGS(getRawRequestMessage);
177 HTTP_EMPTY_ARGS(getHistory);
178 HTTP_EMPTY_ARGS(clearHistory);
179 HTTP_EMPTY_ARGS(send);
180
181 HTTP_BEGIN_ARGS(get, 1)
182 HTTP_ARG_VAL(url, 0)
183 HTTP_ARG_VAL(options, 0)
184 HTTP_ARG_VAL(info, 1)
185 HTTP_END_ARGS;
186
187 HTTP_BEGIN_ARGS(head, 1)
188 HTTP_ARG_VAL(url, 0)
189 HTTP_ARG_VAL(options, 0)
190 HTTP_ARG_VAL(info, 1)
191 HTTP_END_ARGS;
192
193 HTTP_BEGIN_ARGS(postData, 2)
194 HTTP_ARG_VAL(url, 0)
195 HTTP_ARG_VAL(data, 0)
196 HTTP_ARG_VAL(options, 0)
197 HTTP_ARG_VAL(info, 1)
198 HTTP_END_ARGS;
199
200 HTTP_BEGIN_ARGS(postFields, 2)
201 HTTP_ARG_VAL(url, 0)
202 HTTP_ARG_VAL(data, 0)
203 HTTP_ARG_VAL(options, 0)
204 HTTP_ARG_VAL(info, 1)
205 HTTP_END_ARGS;
206
207 HTTP_BEGIN_ARGS(putData, 2)
208 HTTP_ARG_VAL(url, 0)
209 HTTP_ARG_VAL(data, 0)
210 HTTP_ARG_VAL(options, 0)
211 HTTP_ARG_VAL(info, 1)
212 HTTP_END_ARGS;
213
214 HTTP_BEGIN_ARGS(putFile, 2)
215 HTTP_ARG_VAL(url, 0)
216 HTTP_ARG_VAL(file, 0)
217 HTTP_ARG_VAL(options, 0)
218 HTTP_ARG_VAL(info, 1)
219 HTTP_END_ARGS;
220
221 HTTP_BEGIN_ARGS(putStream, 2)
222 HTTP_ARG_VAL(url, 0)
223 HTTP_ARG_VAL(stream, 0)
224 HTTP_ARG_VAL(options, 0)
225 HTTP_ARG_VAL(info, 1)
226 HTTP_END_ARGS;
227
228 HTTP_BEGIN_ARGS(methodRegister, 1)
229 HTTP_ARG_VAL(method_name, 0)
230 HTTP_END_ARGS;
231
232 HTTP_BEGIN_ARGS(methodUnregister, 1)
233 HTTP_ARG_VAL(method, 0)
234 HTTP_END_ARGS;
235
236 HTTP_BEGIN_ARGS(methodName, 1)
237 HTTP_ARG_VAL(method_id, 0)
238 HTTP_END_ARGS;
239
240 HTTP_BEGIN_ARGS(methodExists, 1)
241 HTTP_ARG_VAL(method, 0)
242 HTTP_END_ARGS;
243
244 #ifdef HAVE_CURL_FORMGET
245 HTTP_BEGIN_ARGS(encodeBody, 2)
246 HTTP_ARG_VAL(fields, 0)
247 HTTP_ARG_VAL(files, 0)
248 HTTP_END_ARGS;
249 #endif
250
251 #define THIS_CE http_request_object_ce
252 zend_class_entry *http_request_object_ce;
253 zend_function_entry http_request_object_fe[] = {
254 HTTP_REQUEST_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
255
256 HTTP_REQUEST_ME(setOptions, ZEND_ACC_PUBLIC)
257 HTTP_REQUEST_ME(getOptions, ZEND_ACC_PUBLIC)
258 HTTP_REQUEST_ME(setSslOptions, ZEND_ACC_PUBLIC)
259 HTTP_REQUEST_ME(getSslOptions, ZEND_ACC_PUBLIC)
260 HTTP_REQUEST_ME(addSslOptions, ZEND_ACC_PUBLIC)
261
262 HTTP_REQUEST_ME(addHeaders, ZEND_ACC_PUBLIC)
263 HTTP_REQUEST_ME(getHeaders, ZEND_ACC_PUBLIC)
264 HTTP_REQUEST_ME(setHeaders, ZEND_ACC_PUBLIC)
265
266 HTTP_REQUEST_ME(addCookies, ZEND_ACC_PUBLIC)
267 HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
268 HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
269
270 HTTP_REQUEST_ME(enableCookies, ZEND_ACC_PUBLIC)
271 HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC)
272 HTTP_REQUEST_ME(flushCookies, ZEND_ACC_PUBLIC)
273
274 HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
275 HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
276
277 HTTP_REQUEST_ME(setUrl, ZEND_ACC_PUBLIC)
278 HTTP_REQUEST_ME(getUrl, ZEND_ACC_PUBLIC)
279
280 HTTP_REQUEST_ME(setContentType, ZEND_ACC_PUBLIC)
281 HTTP_REQUEST_ME(getContentType, ZEND_ACC_PUBLIC)
282
283 HTTP_REQUEST_ME(setQueryData, ZEND_ACC_PUBLIC)
284 HTTP_REQUEST_ME(getQueryData, ZEND_ACC_PUBLIC)
285 HTTP_REQUEST_ME(addQueryData, ZEND_ACC_PUBLIC)
286
287 HTTP_REQUEST_ME(setPostFields, ZEND_ACC_PUBLIC)
288 HTTP_REQUEST_ME(getPostFields, ZEND_ACC_PUBLIC)
289 HTTP_REQUEST_ME(addPostFields, ZEND_ACC_PUBLIC)
290
291 HTTP_REQUEST_ME(setBody, ZEND_ACC_PUBLIC)
292 HTTP_REQUEST_ME(getBody, ZEND_ACC_PUBLIC)
293 HTTP_REQUEST_ME(addBody, ZEND_ACC_PUBLIC)
294 HTTP_REQUEST_MALIAS(setRawPostData, setBody, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
295 HTTP_REQUEST_MALIAS(getRawPostData, getBody, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
296 HTTP_REQUEST_MALIAS(addRawPostData, addBody, ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
297
298 HTTP_REQUEST_ME(setPostFiles, ZEND_ACC_PUBLIC)
299 HTTP_REQUEST_ME(addPostFile, ZEND_ACC_PUBLIC)
300 HTTP_REQUEST_ME(getPostFiles, ZEND_ACC_PUBLIC)
301
302 HTTP_REQUEST_ME(setPutFile, ZEND_ACC_PUBLIC)
303 HTTP_REQUEST_ME(getPutFile, ZEND_ACC_PUBLIC)
304
305 HTTP_REQUEST_ME(setPutData, ZEND_ACC_PUBLIC)
306 HTTP_REQUEST_ME(getPutData, ZEND_ACC_PUBLIC)
307 HTTP_REQUEST_ME(addPutData, ZEND_ACC_PUBLIC)
308
309 HTTP_REQUEST_ME(send, ZEND_ACC_PUBLIC)
310
311 HTTP_REQUEST_ME(getResponseData, ZEND_ACC_PUBLIC)
312 HTTP_REQUEST_ME(getResponseHeader, ZEND_ACC_PUBLIC)
313 HTTP_REQUEST_ME(getResponseCookies, ZEND_ACC_PUBLIC)
314 HTTP_REQUEST_ME(getResponseCode, ZEND_ACC_PUBLIC)
315 HTTP_REQUEST_ME(getResponseStatus, ZEND_ACC_PUBLIC)
316 HTTP_REQUEST_ME(getResponseBody, ZEND_ACC_PUBLIC)
317 HTTP_REQUEST_ME(getResponseInfo, ZEND_ACC_PUBLIC)
318 HTTP_REQUEST_ME(getResponseMessage, ZEND_ACC_PUBLIC)
319 HTTP_REQUEST_ME(getRawResponseMessage, ZEND_ACC_PUBLIC)
320 HTTP_REQUEST_ME(getRequestMessage, ZEND_ACC_PUBLIC)
321 HTTP_REQUEST_ME(getRawRequestMessage, ZEND_ACC_PUBLIC)
322 HTTP_REQUEST_ME(getHistory, ZEND_ACC_PUBLIC)
323 HTTP_REQUEST_ME(clearHistory, ZEND_ACC_PUBLIC)
324
325 HTTP_REQUEST_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
326
327 HTTP_REQUEST_ALIAS(get, http_get)
328 HTTP_REQUEST_ALIAS(head, http_head)
329 HTTP_REQUEST_ALIAS(postData, http_post_data)
330 HTTP_REQUEST_ALIAS(postFields, http_post_fields)
331 HTTP_REQUEST_ALIAS(putData, http_put_data)
332 HTTP_REQUEST_ALIAS(putFile, http_put_file)
333 HTTP_REQUEST_ALIAS(putStream, http_put_stream)
334
335 HTTP_REQUEST_ALIAS(methodRegister, http_request_method_register)
336 HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
337 HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
338 HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
339 #ifdef HAVE_CURL_FORMGET
340 HTTP_REQUEST_ALIAS(encodeBody, http_request_body_encode)
341 #endif
342 EMPTY_FUNCTION_ENTRY
343 };
344 static zend_object_handlers http_request_object_handlers;
345
346 PHP_MINIT_FUNCTION(http_request_object)
347 {
348 HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0);
349 http_request_object_handlers.clone_obj = _http_request_object_clone_obj;
350
351 zend_declare_property_null(THIS_CE, ZEND_STRS("options")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
352 zend_declare_property_null(THIS_CE, ZEND_STRS("postFields")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
353 zend_declare_property_null(THIS_CE, ZEND_STRS("postFiles")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
354 zend_declare_property_null(THIS_CE, ZEND_STRS("responseInfo")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
355 zend_declare_property_null(THIS_CE, ZEND_STRS("responseMessage")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
356 zend_declare_property_long(THIS_CE, ZEND_STRS("responseCode")-1, 0, ZEND_ACC_PRIVATE TSRMLS_CC);
357 zend_declare_property_string(THIS_CE, ZEND_STRS("responseStatus")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
358 zend_declare_property_long(THIS_CE, ZEND_STRS("method")-1, HTTP_GET, ZEND_ACC_PRIVATE TSRMLS_CC);
359 zend_declare_property_string(THIS_CE, ZEND_STRS("url")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
360 zend_declare_property_string(THIS_CE, ZEND_STRS("contentType")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
361 zend_declare_property_string(THIS_CE, ZEND_STRS("requestBody")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
362 zend_declare_property_string(THIS_CE, ZEND_STRS("queryData")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
363 zend_declare_property_string(THIS_CE, ZEND_STRS("putFile")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
364 zend_declare_property_string(THIS_CE, ZEND_STRS("putData")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
365 zend_declare_property_null(THIS_CE, ZEND_STRS("history")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
366 zend_declare_property_bool(THIS_CE, ZEND_STRS("recordHistory")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
367
368 #ifndef WONKY
369 /*
370 * Request Method Constants
371 */
372 /* HTTP/1.1 */
373 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_GET")-1, HTTP_GET TSRMLS_CC);
374 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_HEAD")-1, HTTP_HEAD TSRMLS_CC);
375 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_POST")-1, HTTP_POST TSRMLS_CC);
376 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_PUT")-1, HTTP_PUT TSRMLS_CC);
377 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_DELETE")-1, HTTP_DELETE TSRMLS_CC);
378 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_OPTIONS")-1, HTTP_OPTIONS TSRMLS_CC);
379 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_TRACE")-1, HTTP_TRACE TSRMLS_CC);
380 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_CONNECT")-1, HTTP_CONNECT TSRMLS_CC);
381 /* WebDAV - RFC 2518 */
382 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_PROPFIND")-1, HTTP_PROPFIND TSRMLS_CC);
383 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_PROPPATCH")-1, HTTP_PROPPATCH TSRMLS_CC);
384 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_MKCOL")-1, HTTP_MKCOL TSRMLS_CC);
385 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_COPY")-1, HTTP_COPY TSRMLS_CC);
386 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_MOVE")-1, HTTP_MOVE TSRMLS_CC);
387 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_LOCK")-1, HTTP_LOCK TSRMLS_CC);
388 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_UNLOCK")-1, HTTP_UNLOCK TSRMLS_CC);
389 /* WebDAV Versioning - RFC 3253 */
390 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_VERSION_CONTROL")-1, HTTP_VERSION_CONTROL TSRMLS_CC);
391 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_REPORT")-1, HTTP_REPORT TSRMLS_CC);
392 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_CHECKOUT")-1, HTTP_CHECKOUT TSRMLS_CC);
393 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_CHECKIN")-1, HTTP_CHECKIN TSRMLS_CC);
394 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_UNCHECKOUT")-1, HTTP_UNCHECKOUT TSRMLS_CC);
395 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_MKWORKSPACE")-1, HTTP_MKWORKSPACE TSRMLS_CC);
396 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_UPDATE")-1, HTTP_UPDATE TSRMLS_CC);
397 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_LABEL")-1, HTTP_LABEL TSRMLS_CC);
398 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_MERGE")-1, HTTP_MERGE TSRMLS_CC);
399 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_BASELINE_CONTROL")-1, HTTP_BASELINE_CONTROL TSRMLS_CC);
400 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_MKACTIVITY")-1, HTTP_MKACTIVITY TSRMLS_CC);
401 /* WebDAV Access Control - RFC 3744 */
402 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("METH_ACL")-1, HTTP_ACL TSRMLS_CC);
403
404 /*
405 * HTTP Protocol Version Constants
406 */
407 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("VERSION_1_0")-1, CURL_HTTP_VERSION_1_0 TSRMLS_CC);
408 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("VERSION_1_1")-1, CURL_HTTP_VERSION_1_1 TSRMLS_CC);
409 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("VERSION_NONE")-1, CURL_HTTP_VERSION_NONE TSRMLS_CC); /* to be removed */
410 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("VERSION_ANY")-1, CURL_HTTP_VERSION_NONE TSRMLS_CC);
411
412 /*
413 * SSL Version Constants
414 */
415 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("SSL_VERSION_TLSv1")-1, CURL_SSLVERSION_TLSv1 TSRMLS_CC);
416 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("SSL_VERSION_SSLv2")-1, CURL_SSLVERSION_SSLv2 TSRMLS_CC);
417 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("SSL_VERSION_SSLv3")-1, CURL_SSLVERSION_SSLv3 TSRMLS_CC);
418 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("SSL_VERSION_ANY")-1, CURL_SSLVERSION_DEFAULT TSRMLS_CC);
419
420 /*
421 * DNS IPvX resolving
422 */
423 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("IPRESOLVE_V4")-1, CURL_IPRESOLVE_V4 TSRMLS_CC);
424 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("IPRESOLVE_V6")-1, CURL_IPRESOLVE_V6 TSRMLS_CC);
425 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("IPRESOLVE_ANY")-1, CURL_IPRESOLVE_WHATEVER TSRMLS_CC);
426
427 /*
428 * Auth Constants
429 */
430 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("AUTH_BASIC")-1, CURLAUTH_BASIC TSRMLS_CC);
431 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("AUTH_DIGEST")-1, CURLAUTH_DIGEST TSRMLS_CC);
432 #if HTTP_CURL_VERSION(7,19,3)
433 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("AUTH_DIGEST_IE")-1, CURLAUTH_DIGEST_IE TSRMLS_CC);
434 #endif
435 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("AUTH_NTLM")-1, CURLAUTH_NTLM TSRMLS_CC);
436 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("AUTH_GSSNEG")-1, CURLAUTH_GSSNEGOTIATE TSRMLS_CC);
437 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("AUTH_ANY")-1, CURLAUTH_ANY TSRMLS_CC);
438
439 /*
440 * Proxy Type Constants
441 */
442 # if HTTP_CURL_VERSION(7,15,2)
443 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("PROXY_SOCKS4")-1, CURLPROXY_SOCKS4 TSRMLS_CC);
444 # endif
445 #if HTTP_CURL_VERSION(7,18,0)
446 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("PROXY_SOCKS4A")-1, CURLPROXY_SOCKS5 TSRMLS_CC);
447 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("PROXY_SOCKS5_HOSTNAME")-1, CURLPROXY_SOCKS5 TSRMLS_CC);
448 #endif
449 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("PROXY_SOCKS5")-1, CURLPROXY_SOCKS5 TSRMLS_CC);
450 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("PROXY_HTTP")-1, CURLPROXY_HTTP TSRMLS_CC);
451 # if HTTP_CURL_VERSION(7,19,4)
452 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("PROXY_HTTP_1_0")-1, CURLPROXY_HTTP_1_0 TSRMLS_CC);
453 # endif
454 #endif /* WONKY */
455
456 /*
457 * Post Redirection Constants
458 */
459 #if HTTP_CURL_VERSION(7,19,1)
460 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("POSTREDIR_301")-1, CURL_REDIR_POST_301 TSRMLS_CC);
461 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("POSTREDIR_302")-1, CURL_REDIR_POST_302 TSRMLS_CC);
462 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("POSTREDIR_ALL")-1, CURL_REDIR_POST_ALL TSRMLS_CC);
463 #endif
464
465 return SUCCESS;
466 }
467
468 zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
469 {
470 return http_request_object_new_ex(ce, NULL, NULL);
471 }
472
473 zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC)
474 {
475 zend_object_value ov;
476 http_request_object *o;
477
478 o = ecalloc(1, sizeof(http_request_object));
479 o->zo.ce = ce;
480 o->request = http_request_init_ex(NULL, ch, 0, NULL);
481
482 if (ptr) {
483 *ptr = o;
484 }
485
486 ALLOC_HASHTABLE(OBJ_PROP(o));
487 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
488 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
489
490 ov.handle = putObject(http_request_object, o);
491 ov.handlers = &http_request_object_handlers;
492
493 return ov;
494 }
495
496 zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC)
497 {
498 zend_object_value new_ov;
499 http_request_object *new_obj;
500 getObject(http_request_object, old_obj);
501
502 new_ov = http_request_object_new_ex(old_obj->zo.ce, NULL, &new_obj);
503 if (old_obj->request->ch) {
504 http_curl_init_ex(http_curl_copy(old_obj->request->ch), new_obj->request);
505 }
506
507 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
508 phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
509 phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
510
511 return new_ov;
512 }
513
514 void _http_request_object_free(zend_object *object TSRMLS_DC)
515 {
516 http_request_object *o = (http_request_object *) object;
517
518 http_request_free(&o->request);
519 freeObject(o);
520 }
521
522 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
523 static inline void _http_request_object_check_request_content_type(zval *this_ptr TSRMLS_DC)
524 {
525 zval *ctype = zend_read_property(THIS_CE, getThis(), ZEND_STRS("contentType")-1, 0 TSRMLS_CC);
526
527 if (Z_STRLEN_P(ctype)) {
528 zval **headers, *opts = zend_read_property(THIS_CE, getThis(), ZEND_STRS("options")-1, 0 TSRMLS_CC);
529
530 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
531 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void *) &headers)) &&
532 (Z_TYPE_PP(headers) == IS_ARRAY)) {
533 zval **ct_header;
534
535 /* only override if not already set */
536 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void *) &ct_header))) {
537 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
538 } else
539 /* or not a string, zero length string or a string of spaces */
540 if ((Z_TYPE_PP(ct_header) != IS_STRING) || !Z_STRLEN_PP(ct_header)) {
541 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
542 } else {
543 int i, only_space = 1;
544
545 /* check for spaces only */
546 for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) {
547 if (!HTTP_IS_CTYPE(space, Z_STRVAL_PP(ct_header)[i])) {
548 only_space = 0;
549 break;
550 }
551 }
552 if (only_space) {
553 add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
554 }
555 }
556 } else {
557 zval *headers;
558
559 MAKE_STD_ZVAL(headers);
560 array_init(headers);
561 add_assoc_stringl(headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
562 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, headers);
563 zval_ptr_dtor(&headers);
564 }
565 }
566 }
567
568 STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
569 {
570 STATUS status = SUCCESS;
571 char *url = http_absolute_url(Z_STRVAL_P(zend_read_property(THIS_CE, getThis(), ZEND_STRS("url")-1, 0 TSRMLS_CC)));
572
573 if (!url) {
574 return FAILURE;
575 }
576
577 http_request_reset(obj->request);
578 obj->request->url = url;
579 HTTP_CHECK_CURL_INIT(obj->request->ch, http_curl_init(obj->request), return FAILURE);
580
581 switch (obj->request->meth = Z_LVAL_P(zend_read_property(THIS_CE, getThis(), ZEND_STRS("method")-1, 0 TSRMLS_CC)))
582 {
583 case HTTP_GET:
584 case HTTP_HEAD:
585 break;
586
587 case HTTP_PUT:
588 {
589 zval *put_file = zend_read_property(THIS_CE, getThis(), ZEND_STRS("putFile")-1, 0 TSRMLS_CC);
590
591 http_request_object_check_request_content_type(getThis());
592
593 if (Z_STRLEN_P(put_file)) {
594 php_stream_statbuf ssb;
595 php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(put_file), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT);
596
597 if (stream && SUCCESS == php_stream_stat(stream, &ssb)) {
598 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
599 } else {
600 status = FAILURE;
601 }
602 } else {
603 zval *put_data = zend_read_property(THIS_CE, getThis(), ZEND_STRS("putData")-1, 0 TSRMLS_CC);
604 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
605 estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1);
606 }
607 break;
608 }
609
610 case HTTP_POST:
611 default:
612 {
613 /* check for raw request body */
614 zval *raw_data = zend_read_property(THIS_CE, getThis(), ZEND_STRS("requestBody")-1, 0 TSRMLS_CC);
615
616 if (Z_STRLEN_P(raw_data)) {
617 http_request_object_check_request_content_type(getThis());
618 obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
619 estrndup(Z_STRVAL_P(raw_data), Z_STRLEN_P(raw_data)), Z_STRLEN_P(raw_data), 1);
620 } else {
621 zval *zfields = zend_read_property(THIS_CE, getThis(), ZEND_STRS("postFields")-1, 0 TSRMLS_CC), *zfiles = zend_read_property(THIS_CE, getThis(), ZEND_STRS("postFiles")-1, 0 TSRMLS_CC);
622 HashTable *fields;
623 HashTable *files;
624
625 fields = (Z_TYPE_P(zfields) == IS_ARRAY) ? Z_ARRVAL_P(zfields) : NULL;
626 files = (Z_TYPE_P(zfiles) == IS_ARRAY) ? Z_ARRVAL_P(zfiles) : NULL;
627
628 if ((fields && zend_hash_num_elements(fields)) || (files && zend_hash_num_elements(files))) {
629 if (!(obj->request->body = http_request_body_fill(obj->request->body, fields, files))) {
630 status = FAILURE;
631 }
632 }
633 }
634 break;
635 }
636 }
637
638 if (status == SUCCESS) {
639 zval *qdata = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryData")-1, 0 TSRMLS_CC);
640 zval *options = zend_read_property(THIS_CE, getThis(), ZEND_STRS("options")-1, 0 TSRMLS_CC);
641
642 if (Z_STRLEN_P(qdata)) {
643 if (!strchr(obj->request->url, '?')) {
644 strlcat(obj->request->url, "?", HTTP_URL_MAXLEN);
645 } else {
646 strlcat(obj->request->url, "&", HTTP_URL_MAXLEN);
647 }
648 strlcat(obj->request->url, Z_STRVAL_P(qdata), HTTP_URL_MAXLEN);
649 }
650
651 http_request_prepare(obj->request, Z_ARRVAL_P(options));
652
653 /* check if there's a onProgress method and add it as progress callback if one isn't already set */
654 if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onprogress", sizeof("onprogress"))) {
655 zval **entry, *pcb;
656
657 if ( (Z_TYPE_P(options) != IS_ARRAY)
658 || (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry)
659 || (!IS_CALLABLE(*entry, 0, NULL)))) {
660 MAKE_STD_ZVAL(pcb);
661 array_init(pcb);
662 ZVAL_ADDREF(getThis());
663 add_next_index_zval(pcb, getThis());
664 add_next_index_stringl(pcb, "onprogress", lenof("onprogress"), 1);
665 http_request_set_progress_callback(obj->request, pcb);
666 zval_ptr_dtor(&pcb);
667 }
668 }
669 }
670
671 return status;
672 }
673
674 STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this_ptr TSRMLS_DC)
675 {
676 STATUS ret;
677 zval *info;
678 http_message *msg;
679
680 /* always fetch info */
681 MAKE_STD_ZVAL(info);
682 array_init(info);
683 http_request_info(obj->request, Z_ARRVAL_P(info));
684 zend_update_property(THIS_CE, getThis(), ZEND_STRS("responseInfo")-1, info TSRMLS_CC);
685 zval_ptr_dtor(&info);
686
687 /* parse response message */
688 phpstr_fix(&obj->request->conv.request);
689 phpstr_fix(&obj->request->conv.response);
690
691 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) {
692 zval *message;
693
694 if (i_zend_is_true(zend_read_property(THIS_CE, getThis(), ZEND_STRS("recordHistory")-1, 0 TSRMLS_CC))) {
695 zval *hist, *history = zend_read_property(THIS_CE, getThis(), ZEND_STRS("history")-1, 0 TSRMLS_CC);
696 http_message *response = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response));
697 http_message *request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request));
698
699 MAKE_STD_ZVAL(hist);
700 ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, http_message_interconnect(response, request), NULL), 0);
701 if (Z_TYPE_P(history) == IS_OBJECT) {
702 http_message_object_prepend(hist, history);
703 }
704 zend_update_property(THIS_CE, getThis(), ZEND_STRS("history")-1, hist TSRMLS_CC);
705 zval_ptr_dtor(&hist);
706 }
707
708 zend_update_property_long(THIS_CE, getThis(), ZEND_STRS("responseCode")-1, msg->http.info.response.code TSRMLS_CC);
709 zend_update_property_string(THIS_CE, getThis(), ZEND_STRS("responseStatus")-1, STR_PTR(msg->http.info.response.status) TSRMLS_CC);
710
711 MAKE_STD_ZVAL(message);
712 ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
713 zend_update_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, message TSRMLS_CC);
714 zval_ptr_dtor(&message);
715
716 ret = SUCCESS;
717 } else {
718 /* update properties with empty values*/
719 zval *znull;
720
721 MAKE_STD_ZVAL(znull);
722 ZVAL_NULL(znull);
723 zend_update_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, znull TSRMLS_CC);
724 zval_ptr_dtor(&znull);
725
726 zend_update_property_long(THIS_CE, getThis(), ZEND_STRS("responseCode")-1, 0 TSRMLS_CC);
727 zend_update_property_string(THIS_CE, getThis(), ZEND_STRS("responseStatus")-1, "" TSRMLS_CC);
728
729 /* append request message to history */
730 if (i_zend_is_true(zend_read_property(THIS_CE, getThis(), ZEND_STRS("recordHistory")-1, 0 TSRMLS_CC))) {
731 http_message *request;
732
733 if ((request = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
734 zval *hist, *history = zend_read_property(THIS_CE, getThis(), ZEND_STRS("history")-1, 0 TSRMLS_CC);
735
736 MAKE_STD_ZVAL(hist);
737 ZVAL_OBJVAL(hist, http_message_object_new_ex(http_message_object_ce, request, NULL), 0);
738 if (Z_TYPE_P(history) == IS_OBJECT) {
739 http_message_object_prepend(hist, history);
740 }
741 zend_update_property(THIS_CE, getThis(), ZEND_STRS("history")-1, hist TSRMLS_CC);
742 zval_ptr_dtor(&hist);
743 }
744 }
745
746 ret = FAILURE;
747 }
748
749 http_request_set_progress_callback(obj->request, NULL);
750
751 if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
752 zval *param;
753
754 MAKE_STD_ZVAL(param);
755 ZVAL_BOOL(param, ret == SUCCESS);
756 with_error_handling(EH_NORMAL, NULL) {
757 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
758 } end_error_handling();
759 zval_ptr_dtor(&param);
760 }
761
762 return ret;
763 }
764
765 static int apply_pretty_key(void *pDest, int num_args, va_list args, zend_hash_key *hash_key)
766 {
767 if (hash_key->arKey && hash_key->nKeyLength > 1) {
768 hash_key->h = zend_hash_func(pretty_key(hash_key->arKey, hash_key->nKeyLength - 1, 1, 0), hash_key->nKeyLength);
769 }
770 return ZEND_HASH_APPLY_KEEP;
771 }
772
773 #define http_request_object_set_options_subr(key, ow, pk) \
774 _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key), (ow), (pk))
775 static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len, int overwrite, int prettify_keys)
776 {
777 zval *old_opts, *new_opts, *opts = NULL, **entry = NULL;
778
779 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a/!", &opts)) {
780 RETURN_FALSE;
781 }
782
783 MAKE_STD_ZVAL(new_opts);
784 array_init(new_opts);
785 old_opts = zend_read_property(THIS_CE, getThis(), ZEND_STRS("options")-1, 0 TSRMLS_CC);
786 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
787 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
788 }
789
790 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
791 if (overwrite) {
792 zend_hash_clean(Z_ARRVAL_PP(entry));
793 }
794 if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
795 if (overwrite) {
796 array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry));
797 } else {
798 array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0);
799 }
800 }
801 } else if (opts) {
802 if (prettify_keys) {
803 zend_hash_apply_with_arguments(Z_ARRVAL_P(opts) HTTP_ZAPI_HASH_TSRMLS_CC, apply_pretty_key, 0, NULL);
804 }
805 ZVAL_ADDREF(opts);
806 add_assoc_zval_ex(new_opts, key, len, opts);
807 }
808 zend_update_property(THIS_CE, getThis(), ZEND_STRS("options")-1, new_opts TSRMLS_CC);
809 zval_ptr_dtor(&new_opts);
810
811 RETURN_TRUE;
812 }
813
814 #define http_request_object_get_options_subr(key) \
815 _http_request_get_options_subr(INTERNAL_FUNCTION_PARAM_PASSTHRU, (key), sizeof(key))
816 static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS, char *key, size_t len)
817 {
818 NO_ARGS;
819
820 if (return_value_used) {
821 zval *opts, **options;
822
823 opts = zend_read_property(THIS_CE, getThis(), ZEND_STRS("options")-1, 0 TSRMLS_CC);
824 array_init(return_value);
825
826 if ( (Z_TYPE_P(opts) == IS_ARRAY) &&
827 (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
828 convert_to_array(*options);
829 array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value));
830 }
831 }
832 }
833
834
835 /* ### USERLAND ### */
836
837 /* {{{ proto void HttpRequest::__construct([string url[, int request_method = HTTP_METH_GET[, array options]]])
838 Create a new HttpRequest object instance. */
839 PHP_METHOD(HttpRequest, __construct)
840 {
841 char *URL = NULL;
842 int URL_len;
843 long meth = -1;
844 zval *options = NULL;
845
846 SET_EH_THROW_HTTP();
847 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!", &URL, &URL_len, &meth, &options)) {
848 if (URL) {
849 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("url")-1, URL, URL_len TSRMLS_CC);
850 }
851 if (meth > -1) {
852 zend_update_property_long(THIS_CE, getThis(), ZEND_STRS("method")-1, meth TSRMLS_CC);
853 }
854 if (options) {
855 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
856 }
857 }
858 SET_EH_NORMAL();
859 }
860 /* }}} */
861
862 /* {{{ proto HttpRequest HttpRequest::factory([string url[, int request_method HTTP_METH_GET[, array options[, string class_name = "HttpRequest"]]]])
863 Create a new HttpRequest object instance. */
864 PHP_METHOD(HttpRequest, factory)
865 {
866 char *cn = NULL, *URL = NULL;
867 int cl = 0, URL_len = 0;
868 long meth = -1;
869 zval *options = NULL;
870 zend_object_value ov;
871
872 SET_EH_THROW_HTTP();
873 if ( SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!s", &URL, &URL_len, &meth, &options, &cn, &cl) &&
874 SUCCESS == http_object_new(&ov, cn, cl, _http_request_object_new_ex, http_request_object_ce, NULL, NULL)) {
875 RETVAL_OBJVAL(ov, 0);
876 getThis() = return_value;
877 if (URL) {
878 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("url")-1, URL, URL_len TSRMLS_CC);
879 }
880 if (meth > -1) {
881 zend_update_property_long(THIS_CE, getThis(), ZEND_STRS("method")-1, meth TSRMLS_CC);
882 }
883 if (options) {
884 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setoptions", NULL, options);
885 }
886 }
887 SET_EH_NORMAL();
888 }
889 /* }}} */
890
891 /* {{{ proto bool HttpRequest::setOptions([array options])
892 Set the request options to use. See http_get() for a full list of available options. */
893 PHP_METHOD(HttpRequest, setOptions)
894 {
895 HashKey key = initHashKey(0);
896 HashPosition pos;
897 zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
898
899 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
900 RETURN_FALSE;
901 }
902
903 MAKE_STD_ZVAL(new_opts);
904 array_init(new_opts);
905
906 if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
907 zend_update_property(THIS_CE, getThis(), ZEND_STRS("options")-1, new_opts TSRMLS_CC);
908 zval_ptr_dtor(&new_opts);
909 RETURN_TRUE;
910 }
911
912 MAKE_STD_ZVAL(add_opts);
913 array_init(add_opts);
914 /* some options need extra attention -- thus cannot use array_merge() directly */
915 FOREACH_KEYVAL(pos, opts, key, opt) {
916 if (key.type == HASH_KEY_IS_STRING) {
917 #define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s))
918 if (KEYMATCH(key, "headers")) {
919 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
920 } else if (KEYMATCH(key, "cookies")) {
921 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
922 } else if (KEYMATCH(key, "ssl")) {
923 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
924 } else if (KEYMATCH(key, "url") || KEYMATCH(key, "uri")) {
925 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
926 } else if (KEYMATCH(key, "method")) {
927 zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
928 } else if (KEYMATCH(key, "flushcookies")) {
929 getObject(http_request_object, obj);
930 if (i_zend_is_true(*opt)) {
931 http_request_flush_cookies(obj->request);
932 }
933 } else if (KEYMATCH(key, "resetcookies")) {
934 getObject(http_request_object, obj);
935 http_request_reset_cookies(obj->request, (zend_bool) i_zend_is_true(*opt));
936 } else if (KEYMATCH(key, "enablecookies")) {
937 getObject(http_request_object, obj);
938 http_request_enable_cookies(obj->request);
939 } else if (KEYMATCH(key, "recordHistory")) {
940 zend_update_property(THIS_CE, getThis(), ZEND_STRS("recordHistory")-1, *opt TSRMLS_CC);
941 } else if (Z_TYPE_PP(opt) == IS_NULL) {
942 old_opts = zend_read_property(THIS_CE, getThis(), ZEND_STRS("options")-1, 0 TSRMLS_CC);
943 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
944 zend_hash_del(Z_ARRVAL_P(old_opts), key.str, key.len);
945 }
946 } else {
947 ZVAL_ADDREF(*opt);
948 add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
949 }
950 }
951 }
952
953 old_opts = zend_read_property(THIS_CE, getThis(), ZEND_STRS("options")-1, 0 TSRMLS_CC);
954 if (Z_TYPE_P(old_opts) == IS_ARRAY) {
955 array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
956 }
957 array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
958 zend_update_property(THIS_CE, getThis(), ZEND_STRS("options")-1, new_opts TSRMLS_CC);
959 zval_ptr_dtor(&new_opts);
960 zval_ptr_dtor(&add_opts);
961
962 RETURN_TRUE;
963 }
964 /* }}} */
965
966 /* {{{ proto array HttpRequest::getOptions()
967 Get currently set options. */
968 PHP_METHOD(HttpRequest, getOptions)
969 {
970 NO_ARGS;
971
972 if (return_value_used) {
973 RETURN_PROP(options);
974 }
975 }
976 /* }}} */
977
978 /* {{{ proto bool HttpRequest::setSslOptions([array options])
979 Set SSL options. */
980 PHP_METHOD(HttpRequest, setSslOptions)
981 {
982 http_request_object_set_options_subr("ssl", 1, 0);
983 }
984 /* }}} */
985
986 /* {{{ proto bool HttpRequest::addSslOptions(array options)
987 Set additional SSL options. */
988 PHP_METHOD(HttpRequest, addSslOptions)
989 {
990 http_request_object_set_options_subr("ssl", 0, 0);
991 }
992 /* }}} */
993
994 /* {{{ proto array HttpRequest::getSslOtpions()
995 Get previously set SSL options. */
996 PHP_METHOD(HttpRequest, getSslOptions)
997 {
998 http_request_object_get_options_subr("ssl");
999 }
1000 /* }}} */
1001
1002 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1003 Add request header name/value pairs. */
1004 PHP_METHOD(HttpRequest, addHeaders)
1005 {
1006 http_request_object_set_options_subr("headers", 0, 1);
1007 }
1008
1009 /* {{{ proto bool HttpRequest::setHeaders([array headers])
1010 Set request header name/value pairs. */
1011 PHP_METHOD(HttpRequest, setHeaders)
1012 {
1013 http_request_object_set_options_subr("headers", 1, 1);
1014 }
1015 /* }}} */
1016
1017 /* {{{ proto array HttpRequest::getHeaders()
1018 Get previously set request headers. */
1019 PHP_METHOD(HttpRequest, getHeaders)
1020 {
1021 http_request_object_get_options_subr("headers");
1022 }
1023 /* }}} */
1024
1025 /* {{{ proto bool HttpRequest::setCookies([array cookies])
1026 Set cookies. */
1027 PHP_METHOD(HttpRequest, setCookies)
1028 {
1029 http_request_object_set_options_subr("cookies", 1, 0);
1030 }
1031 /* }}} */
1032
1033 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1034 Add cookies. */
1035 PHP_METHOD(HttpRequest, addCookies)
1036 {
1037 http_request_object_set_options_subr("cookies", 0, 0);
1038 }
1039 /* }}} */
1040
1041 /* {{{ proto array HttpRequest::getCookies()
1042 Get previously set cookies. */
1043 PHP_METHOD(HttpRequest, getCookies)
1044 {
1045 http_request_object_get_options_subr("cookies");
1046 }
1047 /* }}} */
1048
1049 /* {{{ proto bool HttpRequest::enableCookies()
1050 Enable automatic sending of received cookies. Note that customly set cookies will be sent anyway. */
1051 PHP_METHOD(HttpRequest, enableCookies)
1052 {
1053 NO_ARGS {
1054 getObject(http_request_object, obj);
1055 RETURN_SUCCESS(http_request_enable_cookies(obj->request));
1056 }
1057
1058 }
1059 /* }}} */
1060
1061 /* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
1062 Reset all automatically received/sent cookies. Note that customly set cookies are not affected. */
1063 PHP_METHOD(HttpRequest, resetCookies)
1064 {
1065 zend_bool session_only = 0;
1066 getObject(http_request_object, obj);
1067
1068 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
1069 RETURN_FALSE;
1070 }
1071 RETURN_SUCCESS(http_request_reset_cookies(obj->request, session_only));
1072 }
1073 /* }}} */
1074
1075 /* {{{ proto bool HttpRequest::flushCookies()
1076 Flush internal cookies to the cookiestore file */
1077 PHP_METHOD(HttpRequest, flushCookies)
1078 {
1079 NO_ARGS {
1080 getObject(http_request_object, obj);
1081 RETURN_SUCCESS(http_request_flush_cookies(obj->request));
1082 }
1083 }
1084 /* }}} */
1085
1086 /* {{{ proto bool HttpRequest::setUrl(string url)
1087 Set the request URL. */
1088 PHP_METHOD(HttpRequest, setUrl)
1089 {
1090 char *URL = NULL;
1091 int URL_len;
1092
1093 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1094 RETURN_FALSE;
1095 }
1096
1097 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("url")-1, URL, URL_len TSRMLS_CC);
1098 RETURN_TRUE;
1099 }
1100 /* }}} */
1101
1102 /* {{{ proto string HttpRequest::getUrl()
1103 Get the previously set request URL. */
1104 PHP_METHOD(HttpRequest, getUrl)
1105 {
1106 NO_ARGS;
1107
1108 if (return_value_used) {
1109 RETURN_PROP(url);
1110 }
1111 }
1112 /* }}} */
1113
1114 /* {{{ proto bool HttpRequest::setMethod(int request_method)
1115 Set the request method. */
1116 PHP_METHOD(HttpRequest, setMethod)
1117 {
1118 long meth;
1119
1120 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1121 RETURN_FALSE;
1122 }
1123
1124 zend_update_property_long(THIS_CE, getThis(), ZEND_STRS("method")-1, meth TSRMLS_CC);
1125 RETURN_TRUE;
1126 }
1127 /* }}} */
1128
1129 /* {{{ proto int HttpRequest::getMethod()
1130 Get the previously set request method. */
1131 PHP_METHOD(HttpRequest, getMethod)
1132 {
1133 NO_ARGS;
1134
1135 if (return_value_used) {
1136 RETURN_PROP(method);
1137 }
1138 }
1139 /* }}} */
1140
1141 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1142 Set the content type the post request should have. */
1143 PHP_METHOD(HttpRequest, setContentType)
1144 {
1145 char *ctype;
1146 int ct_len;
1147
1148 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1149 RETURN_FALSE;
1150 }
1151
1152 if (ct_len) {
1153 HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
1154 }
1155 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("contentType")-1, ctype, ct_len TSRMLS_CC);
1156 RETURN_TRUE;
1157 }
1158 /* }}} */
1159
1160 /* {{{ proto string HttpRequest::getContentType()
1161 Get the previously content type. */
1162 PHP_METHOD(HttpRequest, getContentType)
1163 {
1164 NO_ARGS;
1165
1166 if (return_value_used) {
1167 RETURN_PROP(contentType);
1168 }
1169 }
1170 /* }}} */
1171
1172 /* {{{ proto bool HttpRequest::setQueryData([mixed query_data])
1173 Set the URL query parameters to use, overwriting previously set query parameters. */
1174 PHP_METHOD(HttpRequest, setQueryData)
1175 {
1176 zval *qdata = NULL;
1177
1178 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata)) {
1179 RETURN_FALSE;
1180 }
1181
1182 if ((!qdata) || Z_TYPE_P(qdata) == IS_NULL) {
1183 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("queryData")-1, "", 0 TSRMLS_CC);
1184 } else if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1185 char *query_data = NULL;
1186
1187 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1188 RETURN_FALSE;
1189 }
1190
1191 zend_update_property_string(THIS_CE, getThis(), ZEND_STRS("queryData")-1, query_data TSRMLS_CC);
1192 efree(query_data);
1193 } else {
1194 zval *data = http_zsep(IS_STRING, qdata);
1195
1196 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("queryData")-1, Z_STRVAL_P(data), Z_STRLEN_P(data) TSRMLS_CC);
1197 zval_ptr_dtor(&data);
1198 }
1199 RETURN_TRUE;
1200 }
1201 /* }}} */
1202
1203 /* {{{ proto string HttpRequest::getQueryData()
1204 Get the current query data in form of an urlencoded query string. */
1205 PHP_METHOD(HttpRequest, getQueryData)
1206 {
1207 NO_ARGS;
1208
1209 if (return_value_used) {
1210 RETURN_PROP(queryData);
1211 }
1212 }
1213 /* }}} */
1214
1215 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1216 Add parameters to the query parameter list, leaving previously set unchanged. */
1217 PHP_METHOD(HttpRequest, addQueryData)
1218 {
1219 zval *qdata, *old_qdata;
1220 char *query_data = NULL;
1221 size_t query_data_len = 0;
1222
1223 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &qdata)) {
1224 RETURN_FALSE;
1225 }
1226
1227 old_qdata = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryData")-1, 0 TSRMLS_CC);
1228
1229 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, &query_data_len)) {
1230 RETURN_FALSE;
1231 }
1232
1233 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("queryData")-1, query_data, query_data_len TSRMLS_CC);
1234 efree(query_data);
1235
1236 RETURN_TRUE;
1237 }
1238 /* }}} */
1239
1240 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1241 Adds POST data entries, leaving previously set unchanged, unless a post entry with the same name already exists. */
1242 PHP_METHOD(HttpRequest, addPostFields)
1243 {
1244 zval *post_data, *old_post, *new_post;
1245
1246 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &post_data)) {
1247 RETURN_FALSE;
1248 }
1249
1250 if (zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1251 MAKE_STD_ZVAL(new_post);
1252 array_init(new_post);
1253 old_post = zend_read_property(THIS_CE, getThis(), ZEND_STRS("postFields")-1, 0 TSRMLS_CC);
1254 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1255 array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
1256 }
1257 array_join(Z_ARRVAL_P(post_data), Z_ARRVAL_P(new_post), 0, 0);
1258 zend_update_property(THIS_CE, getThis(), ZEND_STRS("postFields")-1, new_post TSRMLS_CC);
1259 zval_ptr_dtor(&new_post);
1260 }
1261
1262 RETURN_TRUE;
1263 }
1264 /* }}} */
1265
1266 /* {{{ proto bool HttpRequest::setPostFields([array post_data])
1267 Set the POST data entries, overwriting previously set POST data. */
1268 PHP_METHOD(HttpRequest, setPostFields)
1269 {
1270 zval *post, *post_data = NULL;
1271
1272 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &post_data)) {
1273 RETURN_FALSE;
1274 }
1275
1276 MAKE_STD_ZVAL(post);
1277 array_init(post);
1278 if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
1279 array_copy(Z_ARRVAL_P(post_data), Z_ARRVAL_P(post));
1280 }
1281 zend_update_property(THIS_CE, getThis(), ZEND_STRS("postFields")-1, post TSRMLS_CC);
1282 zval_ptr_dtor(&post);
1283
1284 RETURN_TRUE;
1285 }
1286 /* }}}*/
1287
1288 /* {{{ proto array HttpRequest::getPostFields()
1289 Get previously set POST data. */
1290 PHP_METHOD(HttpRequest, getPostFields)
1291 {
1292 NO_ARGS;
1293
1294 if (return_value_used) {
1295 RETURN_PROP(postFields);
1296 }
1297 }
1298 /* }}} */
1299
1300 /* {{{ proto bool HttpRequest::setBody([string request_body_data])
1301 Set request body to send, overwriting previously set request body. Don't forget to specify a content type. */
1302 PHP_METHOD(HttpRequest, setBody)
1303 {
1304 char *raw_data = NULL;
1305 int data_len = 0;
1306
1307 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &raw_data, &data_len)) {
1308 RETURN_FALSE;
1309 }
1310
1311 if (!raw_data) {
1312 raw_data = "";
1313 }
1314
1315 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("requestBody")-1, raw_data, data_len TSRMLS_CC);
1316 RETURN_TRUE;
1317 }
1318 /* }}} */
1319
1320 /* {{{ proto bool HttpRequest::addBody(string request_body_data)
1321 Add request body data, leaving previously set request body data unchanged. */
1322 PHP_METHOD(HttpRequest, addBody)
1323 {
1324 char *raw_data;
1325 int data_len;
1326
1327 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &raw_data, &data_len)) {
1328 RETURN_FALSE;
1329 }
1330
1331 if (data_len) {
1332 zval *data = zend_read_property(THIS_CE, getThis(), ZEND_STRS("requestBody")-1, 0 TSRMLS_CC);
1333
1334 if (Z_STRLEN_P(data)) {
1335 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1336 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1337 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, raw_data, data_len);
1338 } else {
1339 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("requestBody")-1, raw_data, data_len TSRMLS_CC);
1340 }
1341 }
1342
1343 RETURN_TRUE;
1344 }
1345 /* }}} */
1346
1347 /* {{{ proto string HttpRequest::getBody()
1348 Get previously set request body data. */
1349 PHP_METHOD(HttpRequest, getBody)
1350 {
1351 NO_ARGS;
1352
1353 if (return_value_used) {
1354 RETURN_PROP(requestBody);
1355 }
1356 }
1357 /* }}} */
1358
1359 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1360 Add a file to the POST request, leaving previously set files unchanged. */
1361 PHP_METHOD(HttpRequest, addPostFile)
1362 {
1363 zval *entry, *old_post, *new_post;
1364 char *name, *file, *type = NULL;
1365 int name_len, file_len, type_len = 0;
1366
1367 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1368 RETURN_FALSE;
1369 }
1370
1371 if (type_len) {
1372 HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
1373 } else {
1374 type = "application/x-octetstream";
1375 type_len = sizeof("application/x-octetstream") - 1;
1376 }
1377
1378 MAKE_STD_ZVAL(entry);
1379 array_init(entry);
1380
1381 add_assoc_stringl(entry, "name", name, name_len, 1);
1382 add_assoc_stringl(entry, "type", type, type_len, 1);
1383 add_assoc_stringl(entry, "file", file, file_len, 1);
1384
1385 MAKE_STD_ZVAL(new_post);
1386 array_init(new_post);
1387 old_post = zend_read_property(THIS_CE, getThis(), ZEND_STRS("postFiles")-1, 0 TSRMLS_CC);
1388 if (Z_TYPE_P(old_post) == IS_ARRAY) {
1389 array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
1390 }
1391 add_next_index_zval(new_post, entry);
1392 zend_update_property(THIS_CE, getThis(), ZEND_STRS("postFiles")-1, new_post TSRMLS_CC);
1393 zval_ptr_dtor(&new_post);
1394
1395 RETURN_TRUE;
1396 }
1397 /* }}} */
1398
1399 /* {{{ proto bool HttpRequest::setPostFiles([array post_files])
1400 Set files to post, overwriting previously set post files. */
1401 PHP_METHOD(HttpRequest, setPostFiles)
1402 {
1403 zval *files = NULL, *post;
1404
1405 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!/", &files)) {
1406 RETURN_FALSE;
1407 }
1408
1409 MAKE_STD_ZVAL(post);
1410 array_init(post);
1411 if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
1412 array_copy(Z_ARRVAL_P(files), Z_ARRVAL_P(post));
1413 }
1414 zend_update_property(THIS_CE, getThis(), ZEND_STRS("postFiles")-1, post TSRMLS_CC);
1415 zval_ptr_dtor(&post);
1416
1417 RETURN_TRUE;
1418 }
1419 /* }}} */
1420
1421 /* {{{ proto array HttpRequest::getPostFiles()
1422 Get all previously added POST files. */
1423 PHP_METHOD(HttpRequest, getPostFiles)
1424 {
1425 NO_ARGS;
1426
1427 if (return_value_used) {
1428 RETURN_PROP(postFiles);
1429 }
1430 }
1431 /* }}} */
1432
1433 /* {{{ proto bool HttpRequest::setPutFile([string file])
1434 Set file to put. Affects only PUT requests. */
1435 PHP_METHOD(HttpRequest, setPutFile)
1436 {
1437 char *file = "";
1438 int file_len = 0;
1439
1440 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file, &file_len)) {
1441 RETURN_FALSE;
1442 }
1443
1444 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("putFile")-1, file, file_len TSRMLS_CC);
1445 RETURN_TRUE;
1446 }
1447 /* }}} */
1448
1449 /* {{{ proto string HttpRequest::getPutFile()
1450 Get previously set put file. */
1451 PHP_METHOD(HttpRequest, getPutFile)
1452 {
1453 NO_ARGS;
1454
1455 if (return_value_used) {
1456 RETURN_PROP(putFile);
1457 }
1458 }
1459 /* }}} */
1460
1461 /* {{{ proto bool HttpRequest::setPutData([string put_data])
1462 Set PUT data to send, overwriting previously set PUT data. */
1463 PHP_METHOD(HttpRequest, setPutData)
1464 {
1465 char *put_data = NULL;
1466 int data_len = 0;
1467
1468 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &put_data, &data_len)) {
1469 RETURN_FALSE;
1470 }
1471
1472 if (!put_data) {
1473 put_data = "";
1474 }
1475
1476 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("putData")-1, put_data, data_len TSRMLS_CC);
1477 RETURN_TRUE;
1478 }
1479 /* }}} */
1480
1481 /* {{{ proto bool HttpRequest::addPutData(string put_data)
1482 Add PUT data, leaving previously set PUT data unchanged. */
1483 PHP_METHOD(HttpRequest, addPutData)
1484 {
1485 char *put_data;
1486 int data_len;
1487
1488 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &put_data, &data_len)) {
1489 RETURN_FALSE;
1490 }
1491
1492 if (data_len) {
1493 zval *data = zend_read_property(THIS_CE, getThis(), ZEND_STRS("putData")-1, 0 TSRMLS_CC);
1494
1495 if (Z_STRLEN_P(data)) {
1496 Z_STRVAL_P(data) = erealloc(Z_STRVAL_P(data), (Z_STRLEN_P(data) += data_len) + 1);
1497 Z_STRVAL_P(data)[Z_STRLEN_P(data)] = '\0';
1498 memcpy(Z_STRVAL_P(data) + Z_STRLEN_P(data) - data_len, put_data, data_len);
1499 } else {
1500 zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("putData")-1, put_data, data_len TSRMLS_CC);
1501 }
1502 }
1503
1504 RETURN_TRUE;
1505 }
1506 /* }}} */
1507
1508 /* {{{ proto string HttpRequest::getPutData()
1509 Get previously set PUT data. */
1510 PHP_METHOD(HttpRequest, getPutData)
1511 {
1512 NO_ARGS;
1513
1514 if (return_value_used) {
1515 RETURN_PROP(putData);
1516 }
1517 }
1518 /* }}} */
1519
1520 /* {{{ proto array HttpRequest::getResponseData()
1521 Get all response data after the request has been sent. */
1522 PHP_METHOD(HttpRequest, getResponseData)
1523 {
1524 NO_ARGS;
1525
1526 if (return_value_used) {
1527 char *body;
1528 size_t body_len;
1529 zval *headers, *message = zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, 0 TSRMLS_CC);
1530
1531 if (Z_TYPE_P(message) == IS_OBJECT) {
1532 getObjectEx(http_message_object, msg, message);
1533
1534 array_init(return_value);
1535
1536 MAKE_STD_ZVAL(headers);
1537 array_init(headers);
1538 zend_hash_copy(Z_ARRVAL_P(headers), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1539 add_assoc_zval(return_value, "headers", headers);
1540
1541 phpstr_data(PHPSTR(msg->message), &body, &body_len);
1542 add_assoc_stringl(return_value, "body", body, body_len, 0);
1543 }
1544 }
1545 }
1546 /* }}} */
1547
1548 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1549 Get response header(s) after the request has been sent. */
1550 PHP_METHOD(HttpRequest, getResponseHeader)
1551 {
1552 if (return_value_used) {
1553 zval *header;
1554 char *header_name = NULL;
1555 int header_len = 0;
1556
1557 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1558 zval *message = zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, 0 TSRMLS_CC);
1559
1560 if (Z_TYPE_P(message) == IS_OBJECT) {
1561 getObjectEx(http_message_object, msg, message);
1562
1563 if (header_len) {
1564 if ((header = http_message_header_ex(msg->message, pretty_key(header_name, header_len, 1, 1), header_len + 1, 0))) {
1565 RETURN_ZVAL(header, 1, 1);
1566 }
1567 } else {
1568 array_init(return_value);
1569 zend_hash_copy(Z_ARRVAL_P(return_value), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1570 return;
1571 }
1572 }
1573 }
1574 RETURN_FALSE;
1575 }
1576 }
1577 /* }}} */
1578
1579 /* {{{ proto array HttpRequest::getResponseCookies([int flags[, array allowed_extras]])
1580 Get response cookie(s) after the request has been sent. */
1581 PHP_METHOD(HttpRequest, getResponseCookies)
1582 {
1583 if (return_value_used) {
1584 long flags = 0;
1585 zval *allowed_extras_array = NULL;
1586
1587 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
1588 int i = 0;
1589 HashKey key = initHashKey(0);
1590 char **allowed_extras = NULL;
1591 zval **header = NULL, **entry = NULL, *message = zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, 0 TSRMLS_CC);
1592 HashPosition pos, pos1, pos2;
1593
1594 if (Z_TYPE_P(message) == IS_OBJECT) {
1595 getObjectEx(http_message_object, msg, message);
1596
1597 array_init(return_value);
1598
1599 if (allowed_extras_array) {
1600 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1601 FOREACH_VAL(pos, allowed_extras_array, entry) {
1602 zval *data = http_zsep(IS_STRING, *entry);
1603 allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
1604 zval_ptr_dtor(&data);
1605 }
1606 }
1607
1608 FOREACH_HASH_KEYVAL(pos1, &msg->message->hdrs, key, header) {
1609 if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "Set-Cookie")) {
1610 http_cookie_list list;
1611
1612 if (Z_TYPE_PP(header) == IS_ARRAY) {
1613 zval **single_header;
1614
1615 FOREACH_VAL(pos2, *header, single_header) {
1616 zval *data = http_zsep(IS_STRING, *single_header);
1617
1618 if (http_parse_cookie_ex(&list, Z_STRVAL_P(data), flags, allowed_extras)) {
1619 zval *cookie;
1620
1621 MAKE_STD_ZVAL(cookie);
1622 object_init(cookie);
1623 http_cookie_list_tostruct(&list, cookie);
1624 add_next_index_zval(return_value, cookie);
1625 http_cookie_list_dtor(&list);
1626 }
1627 zval_ptr_dtor(&data);
1628 }
1629 } else {
1630 zval *data = http_zsep(IS_STRING, *header);
1631 if (http_parse_cookie_ex(&list, Z_STRVAL_P(data), flags, allowed_extras)) {
1632 zval *cookie;
1633
1634 MAKE_STD_ZVAL(cookie);
1635 object_init(cookie);
1636 http_cookie_list_tostruct(&list, cookie);
1637 add_next_index_zval(return_value, cookie);
1638 http_cookie_list_dtor(&list);
1639 }
1640 zval_ptr_dtor(&data);
1641 }
1642 }
1643 }
1644
1645 if (allowed_extras) {
1646 for (i = 0; allowed_extras[i]; ++i) {
1647 efree(allowed_extras[i]);
1648 }
1649 efree(allowed_extras);
1650 }
1651
1652 return;
1653 }
1654 }
1655 RETURN_FALSE;
1656 }
1657 }
1658 /* }}} */
1659
1660 /* {{{ proto string HttpRequest::getResponseBody()
1661 Get the response body after the request has been sent. */
1662 PHP_METHOD(HttpRequest, getResponseBody)
1663 {
1664 NO_ARGS;
1665
1666 if (return_value_used) {
1667 zval *message = zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, 0 TSRMLS_CC);
1668
1669 if (Z_TYPE_P(message) == IS_OBJECT) {
1670 getObjectEx(http_message_object, msg, message);
1671 RETURN_PHPSTR_DUP(&msg->message->body);
1672 } else {
1673 RETURN_FALSE;
1674 }
1675 }
1676 }
1677 /* }}} */
1678
1679 /* {{{ proto int HttpRequest::getResponseCode()
1680 Get the response code after the request has been sent. */
1681 PHP_METHOD(HttpRequest, getResponseCode)
1682 {
1683 NO_ARGS;
1684
1685 if (return_value_used) {
1686 RETURN_PROP(responseCode);
1687 }
1688 }
1689 /* }}} */
1690
1691 /* {{{ proto string HttpRequest::getResponseStatus()
1692 Get the response status (i.e. the string after the response code) after the message has been sent. */
1693 PHP_METHOD(HttpRequest, getResponseStatus)
1694 {
1695 NO_ARGS;
1696
1697 if (return_value_used) {
1698 RETURN_PROP(responseStatus);
1699 }
1700 }
1701 /* }}} */
1702
1703 /* {{{ proto mixed HttpRequest::getResponseInfo([string name])
1704 Get response info after the request has been sent. */
1705 PHP_METHOD(HttpRequest, getResponseInfo)
1706 {
1707 if (return_value_used) {
1708 zval *info, **infop;
1709 char *info_name = NULL;
1710 int info_len = 0;
1711
1712 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1713 RETURN_FALSE;
1714 }
1715
1716 info = zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseInfo")-1, 0 TSRMLS_CC);
1717
1718 if (Z_TYPE_P(info) != IS_ARRAY) {
1719 RETURN_FALSE;
1720 }
1721
1722 if (info_len && info_name) {
1723 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
1724 RETURN_ZVAL(*infop, 1, 0);
1725 } else {
1726 http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
1727 RETURN_FALSE;
1728 }
1729 } else {
1730 RETURN_ZVAL(info, 1, 0);
1731 }
1732 }
1733 }
1734 /* }}}*/
1735
1736 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1737 Get the full response as HttpMessage object after the request has been sent. */
1738 PHP_METHOD(HttpRequest, getResponseMessage)
1739 {
1740 NO_ARGS {
1741 zval *message;
1742
1743 SET_EH_THROW_HTTP();
1744 message = zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, 0 TSRMLS_CC);
1745 if (Z_TYPE_P(message) == IS_OBJECT) {
1746 RETVAL_OBJECT(message, 1);
1747 } else {
1748 http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
1749 }
1750 SET_EH_NORMAL();
1751 }
1752 }
1753 /* }}} */
1754
1755 /* {{{ proto HttpMessage HttpRequest::getRequestMessage()
1756 Get sent HTTP message. */
1757 PHP_METHOD(HttpRequest, getRequestMessage)
1758 {
1759 NO_ARGS;
1760
1761 if (return_value_used) {
1762 http_message *msg;
1763 getObject(http_request_object, obj);
1764
1765 SET_EH_THROW_HTTP();
1766 if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.request), PHPSTR_LEN(&obj->request->conv.request)))) {
1767 RETVAL_OBJVAL(http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
1768 }
1769 SET_EH_NORMAL();
1770 }
1771 }
1772 /* }}} */
1773
1774 /* {{{ proto string HttpRequest::getRawRequestMessage()
1775 Get sent HTTP message. */
1776 PHP_METHOD(HttpRequest, getRawRequestMessage)
1777 {
1778 NO_ARGS;
1779
1780 if (return_value_used) {
1781 getObject(http_request_object, obj);
1782
1783 RETURN_PHPSTR_DUP(&obj->request->conv.request);
1784 }
1785 }
1786 /* }}} */
1787
1788 /* {{{ proto string HttpRequest::getRawResponseMessage()
1789 Get the entire HTTP response. */
1790 PHP_METHOD(HttpRequest, getRawResponseMessage)
1791 {
1792 NO_ARGS;
1793
1794 if (return_value_used) {
1795 getObject(http_request_object, obj);
1796
1797 RETURN_PHPSTR_DUP(&obj->request->conv.response);
1798 }
1799 }
1800 /* }}} */
1801
1802 /* {{{ proto HttpMessage HttpRequest::getHistory()
1803 Get all sent requests and received responses as an HttpMessage object. */
1804 PHP_METHOD(HttpRequest, getHistory)
1805 {
1806 NO_ARGS;
1807
1808 if (return_value_used) {
1809 zval *hist;
1810
1811 SET_EH_THROW_HTTP();
1812 hist = zend_read_property(THIS_CE, getThis(), ZEND_STRS("history")-1, 0 TSRMLS_CC);
1813 if (Z_TYPE_P(hist) == IS_OBJECT) {
1814 RETVAL_OBJECT(hist, 1);
1815 } else {
1816 http_error(HE_WARNING, HTTP_E_RUNTIME, "The history is empty");
1817 }
1818 SET_EH_NORMAL();
1819 }
1820 }
1821 /* }}} */
1822
1823 /* {{{ proto void HttpRequest::clearHistory()
1824 Clear the history. */
1825 PHP_METHOD(HttpRequest, clearHistory)
1826 {
1827 NO_ARGS {
1828 zval *hist;
1829
1830 MAKE_STD_ZVAL(hist);
1831 ZVAL_NULL(hist);
1832 zend_update_property(THIS_CE, getThis(), ZEND_STRS("history")-1, hist TSRMLS_CC);
1833 zval_ptr_dtor(&hist);
1834 }
1835 }
1836 /* }}} */
1837
1838 /* {{{ proto HttpMessage HttpRequest::send()
1839 Send the HTTP request. */
1840 PHP_METHOD(HttpRequest, send)
1841 {
1842 getObject(http_request_object, obj);
1843
1844 NO_ARGS;
1845
1846 SET_EH_THROW_HTTP();
1847
1848 RETVAL_FALSE;
1849
1850 if (obj->pool) {
1851 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
1852 } else if (SUCCESS == http_request_object_requesthandler(obj, getThis())) {
1853 http_request_exec(obj->request);
1854 if (SUCCESS == http_request_object_responsehandler(obj, getThis())) {
1855 RETVAL_OBJECT(zend_read_property(THIS_CE, getThis(), ZEND_STRS("responseMessage")-1, 0 TSRMLS_CC), 1);
1856 }
1857 }
1858
1859 SET_EH_NORMAL();
1860 }
1861 /* }}} */
1862
1863 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_CURL */
1864
1865 /*
1866 * Local variables:
1867 * tab-width: 4
1868 * c-basic-offset: 4
1869 * End:
1870 * vim600: noet sw=4 ts=4 fdm=marker
1871 * vim<600: noet sw=4 ts=4
1872 */
1873