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