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