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