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