- reflection awareness of HttpRequest
[m6w6/ext-http] / http_methods.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #ifdef ZEND_ENGINE_2
24
25 #include "php_streams.h"
26
27 #include "php_http.h"
28 #include "php_http_std_defs.h"
29 #include "php_http_api.h"
30 #include "php_http_cache_api.h"
31 #include "php_http_request_api.h"
32 #include "php_http_request_pool_api.h"
33 #include "php_http_date_api.h"
34 #include "php_http_headers_api.h"
35 #include "php_http_message_api.h"
36 #include "php_http_send_api.h"
37 #include "php_http_url_api.h"
38
39 #include "php_http_message_object.h"
40 #include "php_http_response_object.h"
41 #include "php_http_request_object.h"
42 #include "php_http_requestpool_object.h"
43 #include "php_http_exception_object.h"
44
45 #include "missing.h"
46
47 ZEND_EXTERN_MODULE_GLOBALS(http);
48
49 /* {{{ HttpResponse */
50
51 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
52 *
53 * Instantiates a new HttpResponse object, which can be used to send
54 * any data/resource/file to an HTTP client with caching and multiple
55 * ranges/resuming support.
56 */
57 PHP_METHOD(HttpResponse, __construct)
58 {
59 zend_bool do_cache = 0, do_gzip = 0;
60 getObject(http_response_object, obj);
61
62 SET_EH_THROW_HTTP();
63 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
64 UPD_PROP(obj, long, cache, do_cache);
65 UPD_PROP(obj, long, gzip, do_gzip);
66 }
67 SET_EH_NORMAL();
68 }
69 /* }}} */
70
71 /* {{{ proto void HttpResponse::__destruct()
72 *
73 * -
74 */
75 PHP_METHOD(HttpResponse, __destruct)
76 {
77 getObject(http_response_object, obj);
78 zval *catch_ob = GET_PROP(obj, catch_ob), *sent = GET_PROP(obj, sent);
79 fprintf(stderr, "DTOR!\n"); fflush(stderr);
80 if (!Z_LVAL_P(sent) && Z_LVAL_P(catch_ob)) {
81 zval ob_data;
82
83 /* fetch catched output buffer */
84 if (SUCCESS == php_ob_get_buffer(&ob_data TSRMLS_CC)) {
85 zval *lmod = GET_PROP(obj, lastModified);
86
87 SET_PROP(obj, data, &ob_data);
88 UPD_PROP(obj, long, send_mode, SEND_DATA);
89
90 if (!Z_LVAL_P(lmod)) {
91 UPD_PROP(obj, long, lastModified, http_last_modified(&ob_data, SEND_DATA));
92 }
93 zval_dtor(&ob_data);
94
95 http_response_object_sendhandler(getThis(), obj, 1, return_value);
96 RETVAL_NULL();
97 }
98 }
99 }
100 /* }}} */
101
102 /* {{{ proto bool HttpResponse::setCache(bool cache)
103 *
104 * Whether it sould be attempted to cache the entitity.
105 * This will result in necessary caching headers and checks of clients
106 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
107 * matches a "304 Not Modified" status code will be issued.
108 *
109 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
110 * to something more appropriate than "no-cache"!
111 */
112 PHP_METHOD(HttpResponse, setCache)
113 {
114 zend_bool do_cache = 0;
115 getObject(http_response_object, obj);
116
117 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
118 RETURN_FALSE;
119 }
120
121 UPD_PROP(obj, long, cache, do_cache);
122 RETURN_TRUE;
123 }
124 /* }}} */
125
126 /* {{{ proto bool HttpResponse::getCache()
127 *
128 * Get current caching setting.
129 */
130 PHP_METHOD(HttpResponse, getCache)
131 {
132 NO_ARGS;
133
134 IF_RETVAL_USED {
135 zval *do_cache = NULL;
136 getObject(http_response_object, obj);
137
138 do_cache = GET_PROP(obj, cache);
139 RETURN_BOOL(Z_LVAL_P(do_cache));
140 }
141 }
142 /* }}}*/
143
144 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
145 *
146 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
147 */
148 PHP_METHOD(HttpResponse, setGzip)
149 {
150 zend_bool do_gzip = 0;
151 getObject(http_response_object, obj);
152
153 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
154 RETURN_FALSE;
155 }
156
157 UPD_PROP(obj, long, gzip, do_gzip);
158 RETURN_TRUE;
159 }
160 /* }}} */
161
162 /* {{{ proto bool HttpResponse::getGzip()
163 *
164 * Get current gzipping setting.
165 */
166 PHP_METHOD(HttpResponse, getGzip)
167 {
168 NO_ARGS;
169
170 IF_RETVAL_USED {
171 zval *do_gzip;
172 getObject(http_response_object, obj);
173
174 do_gzip = GET_PROP(obj, gzip);
175 RETURN_BOOL(Z_LVAL_P(do_gzip));
176 }
177 }
178 /* }}} */
179
180 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
181 *
182 * Set a custom cache-control header, usually being "private" or "public"; if
183 * $raw is set to true the header will be sent as-is.
184 */
185 PHP_METHOD(HttpResponse, setCacheControl)
186 {
187 char *ccontrol;
188 int cc_len;
189 zend_bool raw = 0;
190 getObject(http_response_object, obj);
191
192 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
193 RETURN_FALSE;
194 }
195
196 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
197 http_error_ex(E_WARNING, HTTP_E_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
198 RETURN_FALSE;
199 }
200
201 UPD_PROP(obj, long, raw_cache_header, raw);
202 UPD_PROP(obj, string, cacheControl, ccontrol);
203 RETURN_TRUE;
204 }
205 /* }}} */
206
207 /* {{{ proto string HttpResponse::getCacheControl()
208 *
209 * Get current Cache-Control header setting.
210 */
211 PHP_METHOD(HttpResponse, getCacheControl)
212 {
213 NO_ARGS;
214
215 IF_RETVAL_USED {
216 zval *ccontrol;
217 getObject(http_response_object, obj);
218
219 ccontrol = GET_PROP(obj, cacheControl);
220 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
221 }
222 }
223 /* }}} */
224
225 /* {{{ proto bool HttpResponse::setContentType(string content_type)
226 *
227 * Set the content-type of the sent entity.
228 */
229 PHP_METHOD(HttpResponse, setContentType)
230 {
231 char *ctype;
232 int ctype_len;
233 getObject(http_response_object, obj);
234
235 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
236 RETURN_FALSE;
237 }
238
239 if (!strchr(ctype, '/')) {
240 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
241 RETURN_FALSE;
242 }
243
244 UPD_PROP(obj, string, contentType, ctype);
245
246 RETURN_TRUE;
247 }
248 /* }}} */
249
250 /* {{{ proto string HttpResponse::getContentType()
251 *
252 * Get current Content-Type header setting.
253 */
254 PHP_METHOD(HttpResponse, getContentType)
255 {
256 NO_ARGS;
257
258 IF_RETVAL_USED {
259 zval *ctype;
260 getObject(http_response_object, obj);
261
262 ctype = GET_PROP(obj, contentType);
263 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
264 }
265 }
266 /* }}} */
267
268 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
269 *
270 * Set the Content-Disposition of the sent entity. This setting aims to suggest
271 * the receiveing user agent how to handle the sent entity; usually the client
272 * will show the user a "Save As..." popup.
273 */
274 PHP_METHOD(HttpResponse, setContentDisposition)
275 {
276 char *file;
277 int file_len;
278 zend_bool is_inline = 0;
279 getObject(http_response_object, obj);
280
281 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
282 RETURN_FALSE;
283 }
284
285 UPD_PROP(obj, string, dispoFile, file);
286 UPD_PROP(obj, long, dispoInline, is_inline);
287 RETURN_TRUE;
288 }
289 /* }}} */
290
291 /* {{{ proto array HttpResponse::getContentDisposition()
292 *
293 * Get current Content-Disposition setting.
294 * Will return an associative array like:
295 * <pre>
296 * array(
297 * 'filename' => 'foo.bar',
298 * 'inline' => false
299 * )
300 * </pre>
301 */
302 PHP_METHOD(HttpResponse, getContentDisposition)
303 {
304 NO_ARGS;
305
306 IF_RETVAL_USED {
307 zval *file, *is_inline;
308 getObject(http_response_object, obj);
309
310 file = GET_PROP(obj, dispoFile);
311 is_inline = GET_PROP(obj, dispoInline);
312
313 array_init(return_value);
314 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
315 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
316 }
317 }
318 /* }}} */
319
320 /* {{{ proto bool HttpResponse::setETag(string etag)
321 *
322 * Set a custom ETag. Use this only if you know what you're doing.
323 */
324 PHP_METHOD(HttpResponse, setETag)
325 {
326 char *etag;
327 int etag_len;
328 getObject(http_response_object, obj);
329
330 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
331 RETURN_FALSE;
332 }
333
334 UPD_PROP(obj, string, eTag, etag);
335 RETURN_TRUE;
336 }
337 /* }}} */
338
339 /* {{{ proto string HttpResponse::getETag()
340 *
341 * Get the previously set custom ETag.
342 */
343 PHP_METHOD(HttpResponse, getETag)
344 {
345 NO_ARGS;
346
347 IF_RETVAL_USED {
348 zval *etag;
349 getObject(http_response_object, obj);
350
351 etag = GET_PROP(obj, eTag);
352 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
353 }
354 }
355 /* }}} */
356
357 /* {{{ proto void HttpResponse::setThrottleDelay(double seconds)
358 *
359 */
360 PHP_METHOD(HttpResponse, setThrottleDelay)
361 {
362 double seconds;
363
364 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &seconds)) {
365 getObject(http_response_object, obj);
366
367 UPD_PROP(obj, double, throttleDelay, seconds);
368 }
369 }
370 /* }}} */
371
372 /* {{{ proto double HttpResponse::getThrottleDelay()
373 *
374 */
375 PHP_METHOD(HttpResponse, getThrottleDelay)
376 {
377 NO_ARGS;
378
379 IF_RETVAL_USED {
380 zval *seconds;
381 getObject(http_response_object, obj);
382
383 seconds = GET_PROP(obj, throttleDelay);
384 RETURN_DOUBLE(Z_DVAL_P(seconds));
385 }
386 }
387 /* }}} */
388
389 /* {{{ proto void HttpResponse::setSendBuffersize(long bytes)
390 *
391 */
392 PHP_METHOD(HttpResponse, setSendBuffersize)
393 {
394 long bytes;
395
396 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bytes)) {
397 getObject(http_response_object, obj);
398
399 UPD_PROP(obj, long, sendBuffersize, bytes);
400 }
401 }
402 /* }}} */
403
404 /* {{{ proto long HttpResponse::getSendBuffersize()
405 *
406 */
407 PHP_METHOD(HttpResponse, getSendBuffersize)
408 {
409 NO_ARGS;
410
411 IF_RETVAL_USED {
412 zval *bytes;
413 getObject(http_response_object, obj);
414
415 bytes = GET_PROP(obj, sendBuffersize);
416 RETURN_LONG(Z_LVAL_P(bytes));
417 }
418 }
419 /* }}} */
420
421 /* {{{ proto bool HttpResponse::setData(string data)
422 *
423 * Set the data to be sent.
424 */
425 PHP_METHOD(HttpResponse, setData)
426 {
427 zval *the_data;
428 getObject(http_response_object, obj);
429
430 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &the_data)) {
431 RETURN_FALSE;
432 }
433
434 convert_to_string_ex(&the_data);
435 SET_PROP(obj, data, the_data);
436 UPD_PROP(obj, long, lastModified, http_last_modified(the_data, SEND_DATA));
437 UPD_PROP(obj, long, send_mode, SEND_DATA);
438 RETURN_TRUE;
439 }
440 /* }}} */
441
442 /* {{{ proto string HttpResponse::getData()
443 *
444 * Get the previously set data to be sent.
445 */
446 PHP_METHOD(HttpResponse, getData)
447 {
448 NO_ARGS;
449
450 IF_RETVAL_USED {
451 zval *the_data;
452 getObject(http_response_object, obj);
453
454 the_data = GET_PROP(obj, data);
455 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
456 }
457 }
458 /* }}} */
459
460 /* {{{ proto bool HttpResponse::setStream(resource stream)
461 *
462 * Set the resource to be sent.
463 */
464 PHP_METHOD(HttpResponse, setStream)
465 {
466 zval *the_stream;
467 php_stream *the_real_stream;
468 getObject(http_response_object, obj);
469
470 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
471 RETURN_FALSE;
472 }
473
474 php_stream_from_zval(the_real_stream, &the_stream);
475
476 SET_PROP(obj, stream, the_stream);
477 UPD_PROP(obj, long, lastModified, http_last_modified(the_real_stream, SEND_RSRC));
478 UPD_PROP(obj, long, send_mode, SEND_RSRC);
479 RETURN_TRUE;
480 }
481 /* }}} */
482
483 /* {{{ proto resource HttpResponse::getStream()
484 *
485 * Get the previously set resource to be sent.
486 */
487 PHP_METHOD(HttpResponse, getStream)
488 {
489 NO_ARGS;
490
491 IF_RETVAL_USED {
492 zval *the_stream;
493 getObject(http_response_object, obj);
494
495 the_stream = GET_PROP(obj, stream);
496 RETURN_RESOURCE(Z_LVAL_P(the_stream));
497 }
498 }
499 /* }}} */
500
501 /* {{{ proto bool HttpResponse::setFile(string file)
502 *
503 * Set the file to be sent.
504 */
505 PHP_METHOD(HttpResponse, setFile)
506 {
507 zval *the_file;
508 getObject(http_response_object, obj);
509
510 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
511 RETURN_FALSE;
512 }
513
514 convert_to_string_ex(&the_file);
515
516 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
517 UPD_PROP(obj, long, lastModified, http_last_modified(the_file, -1));
518 UPD_PROP(obj, long, send_mode, -1);
519 RETURN_TRUE;
520 }
521 /* }}} */
522
523 /* {{{ proto string HttpResponse::getFile()
524 *
525 * Get the previously set file to be sent.
526 */
527 PHP_METHOD(HttpResponse, getFile)
528 {
529 NO_ARGS;
530
531 IF_RETVAL_USED {
532 zval *the_file;
533 getObject(http_response_object, obj);
534
535 the_file = GET_PROP(obj, file);
536 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
537 }
538 }
539 /* }}} */
540
541 /* {{{ proto bool HttpResponse::send([bool clean_ob = true])
542 *
543 * Finally send the entity.
544 *
545 * Example:
546 * <pre>
547 * <?php
548 * $r = new HttpResponse(true);
549 * $r->setFile('../hidden/contract.pdf');
550 * $r->setContentType('application/pdf');
551 * $r->send();
552 * ?>
553 * </pre>
554 *
555 */
556 PHP_METHOD(HttpResponse, send)
557 {
558 zend_bool clean_ob = 1;
559
560 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
561 RETURN_FALSE;
562 }
563
564 http_response_object_sendhandler(getThis(), NULL, clean_ob, return_value);
565 }
566 /* }}} */
567
568 /* {{{ proto bool HttpResponse::catchOutput([bool clean_ob = false])
569 *
570 * Use this method instead of HttpResponse::set*() and HttpResponse::send()
571 * to let HttpResponse catch and handle the scripts output (i.e. echo/print).
572 *
573 * Example:
574 * <pre>
575 * <?php
576 * $r = new HttpResponse(true, true);
577 * $r->setContentType('text/html; charset=utf-8');
578 * $r->catchOutput(true);
579 * // script follows
580 * ?>
581 * </pre>
582 */
583 PHP_METHOD(HttpResponse, catchOutput)
584 {
585 zend_bool clean_ob = 0;
586 getObject(http_response_object, obj);
587
588 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
589 RETURN_FALSE;
590 }
591
592 UPD_PROP(obj, long, catch_ob, 1);
593
594 if (clean_ob) {
595 /* interrupt on-the-fly etag generation */
596 HTTP_G(etag).started = 0;
597 /* discard previous output buffers */
598 php_end_ob_buffers(0 TSRMLS_CC);
599 }
600
601 if (SUCCESS != php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC)) {
602 RETURN_FALSE;
603 }
604
605 RETURN_TRUE;
606 }
607 /* }}} */
608
609 /* {{{ HttpMessage */
610
611 /* {{{ proto void HttpMessage::__construct([string message])
612 *
613 * Instantiate a new HttpMessage object.
614 */
615 PHP_METHOD(HttpMessage, __construct)
616 {
617 char *message = NULL;
618 int length = 0;
619 getObject(http_message_object, obj);
620
621 SET_EH_THROW_HTTP();
622 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
623 if (obj->message = http_message_parse(message, length)) {
624 if (obj->message->parent) {
625 obj->parent = http_message_object_from_msg(obj->message->parent);
626 }
627 }
628 } else if (!obj->message) {
629 obj->message = http_message_new();
630 }
631 SET_EH_NORMAL();
632 }
633 /* }}} */
634
635 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
636 *
637 * Create an HttpMessage object from a string.
638 */
639 PHP_METHOD(HttpMessage, fromString)
640 {
641 char *string = NULL;
642 int length = 0;
643 http_message *msg = NULL;
644
645 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
646 RETURN_NULL();
647 }
648
649 if (!(msg = http_message_parse(string, length))) {
650 RETURN_NULL();
651 }
652
653 Z_TYPE_P(return_value) = IS_OBJECT;
654 return_value->value.obj = http_message_object_from_msg(msg);
655 }
656 /* }}} */
657
658 /* {{{ proto string HttpMessage::getBody()
659 *
660 * Get the body of the parsed Message.
661 */
662 PHP_METHOD(HttpMessage, getBody)
663 {
664 NO_ARGS;
665
666 IF_RETVAL_USED {
667 getObject(http_message_object, obj);
668 RETURN_PHPSTR(&obj->message->body, PHPSTR_FREE_NOT, 1);
669 }
670 }
671 /* }}} */
672
673 /* {{{ proto array HttpMessage::getHeaders()
674 *
675 * Get Message Headers.
676 */
677 PHP_METHOD(HttpMessage, getHeaders)
678 {
679 NO_ARGS;
680
681 IF_RETVAL_USED {
682 zval headers;
683 getObject(http_message_object, obj);
684
685 Z_ARRVAL(headers) = &obj->message->hdrs;
686 array_init(return_value);
687 array_copy(&headers, return_value);
688 }
689 }
690 /* }}} */
691
692 /* {{{ proto void HttpMessage::setHeaders(array headers)
693 *
694 * Sets new headers.
695 */
696 PHP_METHOD(HttpMessage, setHeaders)
697 {
698 zval *new_headers, old_headers;
699 getObject(http_message_object, obj);
700
701 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
702 return;
703 }
704
705 zend_hash_clean(&obj->message->hdrs);
706 Z_ARRVAL(old_headers) = &obj->message->hdrs;
707 array_copy(new_headers, &old_headers);
708 }
709 /* }}} */
710
711 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
712 *
713 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
714 */
715 PHP_METHOD(HttpMessage, addHeaders)
716 {
717 zval old_headers, *new_headers;
718 zend_bool append = 0;
719 getObject(http_message_object, obj);
720
721 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
722 return;
723 }
724
725 Z_ARRVAL(old_headers) = &obj->message->hdrs;
726 if (append) {
727 array_append(new_headers, &old_headers);
728 } else {
729 array_merge(new_headers, &old_headers);
730 }
731 }
732 /* }}} */
733
734 /* {{{ proto long HttpMessage::getType()
735 *
736 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
737 */
738 PHP_METHOD(HttpMessage, getType)
739 {
740 NO_ARGS;
741
742 IF_RETVAL_USED {
743 getObject(http_message_object, obj);
744 RETURN_LONG(obj->message->type);
745 }
746 }
747 /* }}} */
748
749 /* {{{ proto void HttpMessage::setType(long type)
750 *
751 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
752 */
753 PHP_METHOD(HttpMessage, setType)
754 {
755 long type;
756 getObject(http_message_object, obj);
757
758 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
759 return;
760 }
761 http_message_set_type(obj->message, type);
762 }
763 /* }}} */
764
765 /* {{{ proto long HttpMessage::getResponseCode()
766 *
767 * Get the Response Code of the Message.
768 */
769 PHP_METHOD(HttpMessage, getResponseCode)
770 {
771 NO_ARGS;
772
773 IF_RETVAL_USED {
774 getObject(http_message_object, obj);
775
776 if (!HTTP_MSG_TYPE(RESPONSE, obj->message)) {
777 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
778 RETURN_NULL();
779 }
780
781 RETURN_LONG(obj->message->info.response.code);
782 }
783 }
784 /* }}} */
785
786 /* {{{ proto bool HttpMessage::setResponseCode(long code)
787 *
788 * Set the response code of an HTTP Response Message.
789 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
790 * or if the response code is out of range (100-510).
791 */
792 PHP_METHOD(HttpMessage, setResponseCode)
793 {
794 long code;
795 getObject(http_message_object, obj);
796
797 if (!HTTP_MSG_TYPE(RESPONSE, obj->message)) {
798 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
799 RETURN_FALSE;
800 }
801
802 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
803 RETURN_FALSE;
804 }
805 if (code < 100 || code > 510) {
806 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid response code (100-510): %ld", code);
807 RETURN_FALSE;
808 }
809
810 obj->message->info.response.code = code;
811 RETURN_TRUE;
812 }
813 /* }}} */
814
815 /* {{{ proto string HttpMessage::getRequestMethod()
816 *
817 * Get the Request Method of the Message.
818 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
819 */
820 PHP_METHOD(HttpMessage, getRequestMethod)
821 {
822 NO_ARGS;
823
824 IF_RETVAL_USED {
825 getObject(http_message_object, obj);
826
827 if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
828 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
829 RETURN_NULL();
830 }
831
832 RETURN_STRING(obj->message->info.request.method, 1);
833 }
834 }
835 /* }}} */
836
837 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
838 *
839 * Set the Request Method of the HTTP Message.
840 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
841 */
842 PHP_METHOD(HttpMessage, setRequestMethod)
843 {
844 char *method;
845 int method_len;
846 getObject(http_message_object, obj);
847
848 if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
849 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
850 RETURN_FALSE;
851 }
852
853 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
854 RETURN_FALSE;
855 }
856 if (method_len < 1) {
857 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
858 RETURN_FALSE;
859 }
860 if (SUCCESS != http_check_method(method)) {
861 http_error_ex(E_WARNING, HTTP_E_PARAM, "Unkown request method: %s", method);
862 RETURN_FALSE;
863 }
864
865 STR_SET(obj->message->info.request.method, estrndup(method, method_len));
866 RETURN_TRUE;
867 }
868 /* }}} */
869
870 /* {{{ proto string HttpMessage::getRequestUri()
871 *
872 * Get the Request URI of the Message.
873 */
874 PHP_METHOD(HttpMessage, getRequestUri)
875 {
876 NO_ARGS;
877
878 IF_RETVAL_USED {
879 getObject(http_message_object, obj);
880
881 if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
882 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
883 RETURN_NULL();
884 }
885
886 RETURN_STRING(obj->message->info.request.URI, 1);
887 }
888 }
889 /* }}} */
890
891 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
892 *
893 * Set the Request URI of the HTTP Message.
894 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
895 * or if paramtere URI was empty.
896 */
897 PHP_METHOD(HttpMessage, setRequestUri)
898 {
899 char *URI;
900 int URIlen;
901 getObject(http_message_object, obj);
902
903 if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
904 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
905 RETURN_FALSE;
906 }
907 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
908 RETURN_FALSE;
909 }
910 if (URIlen < 1) {
911 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
912 RETURN_FALSE;
913 }
914
915 STR_SET(obj->message->info.request.URI, estrndup(URI, URIlen));
916 RETURN_TRUE;
917 }
918 /* }}} */
919
920 /* {{{ proto string HttpMessage::getHttpVersion()
921 *
922 * Get the HTTP Protocol Version of the Message.
923 */
924 PHP_METHOD(HttpMessage, getHttpVersion)
925 {
926 NO_ARGS;
927
928 IF_RETVAL_USED {
929 char ver[4] = {0};
930 float version;
931 getObject(http_message_object, obj);
932
933 switch (obj->message->type)
934 {
935 case HTTP_MSG_RESPONSE:
936 version = obj->message->info.response.http_version;
937 break;
938
939 case HTTP_MSG_REQUEST:
940 version = obj->message->info.request.http_version;
941 break;
942
943 case HTTP_MSG_NONE:
944 default:
945 RETURN_NULL();
946 }
947 sprintf(ver, "%1.1f", version);
948 RETURN_STRINGL(ver, 3, 1);
949 }
950 }
951 /* }}} */
952
953 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
954 *
955 * Set the HTTP Protocol version of the Message.
956 * Returns false if version is invalid (1.0 and 1.1).
957 */
958 PHP_METHOD(HttpMessage, setHttpVersion)
959 {
960 char v[4];
961 zval *zv;
962 getObject(http_message_object, obj);
963
964 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
965 return;
966 }
967
968 if (HTTP_MSG_TYPE(NONE, obj->message)) {
969 http_error(E_WARNING, HTTP_E_MSG, "Message is neither of type HTTP_MSG_RESPONSE nor HTTP_MSG_REQUEST");
970 RETURN_FALSE;
971 }
972
973 convert_to_double(zv);
974 sprintf(v, "%1.1f", Z_DVAL_P(zv));
975 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
976 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid HTTP protocol version (1.0 or 1.1): %s", v);
977 RETURN_FALSE;
978 }
979
980 if (HTTP_MSG_TYPE(RESPONSE, obj->message)) {
981 obj->message->info.response.http_version = (float) Z_DVAL_P(zv);
982 } else {
983 obj->message->info.request.http_version = (float) Z_DVAL_P(zv);
984 }
985 RETURN_TRUE;
986 }
987 /* }}} */
988
989 /* {{{ proto HttpMessage HttpMessage::getParentMessage()
990 *
991 * Get parent Message.
992 */
993 PHP_METHOD(HttpMessage, getParentMessage)
994 {
995 NO_ARGS;
996
997 IF_RETVAL_USED {
998 getObject(http_message_object, obj);
999
1000 if (obj->message->parent) {
1001 RETVAL_OBJVAL(obj->parent);
1002 } else {
1003 RETVAL_NULL();
1004 }
1005 }
1006 }
1007 /* }}} */
1008
1009 /* {{{ proto bool HttpMessage::send()
1010 *
1011 * Send the Message according to its type as Response or Request.
1012 */
1013 PHP_METHOD(HttpMessage, send)
1014 {
1015 getObject(http_message_object, obj);
1016
1017 NO_ARGS;
1018
1019 RETURN_SUCCESS(http_message_send(obj->message));
1020 }
1021 /* }}} */
1022
1023 /* {{{ proto string HttpMessage::toString([bool include_parent = true])
1024 *
1025 * Get the string representation of the Message.
1026 */
1027 PHP_METHOD(HttpMessage, toString)
1028 {
1029 IF_RETVAL_USED {
1030 char *string;
1031 size_t length;
1032 zend_bool include_parent = 1;
1033 getObject(http_message_object, obj);
1034
1035 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
1036 RETURN_FALSE;
1037 }
1038
1039 if (include_parent) {
1040 http_message_serialize(obj->message, &string, &length);
1041 } else {
1042 http_message_tostring(obj->message, &string, &length);
1043 }
1044 RETURN_STRINGL(string, length, 0);
1045 }
1046 }
1047 /* }}} */
1048
1049 /* }}} */
1050
1051 #ifdef HTTP_HAVE_CURL
1052 /* {{{ HttpRequest */
1053
1054 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
1055 *
1056 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
1057 * and POST (including posting files) HTTP requests.
1058 */
1059 PHP_METHOD(HttpRequest, __construct)
1060 {
1061 char *URL = NULL;
1062 int URL_len;
1063 long meth = -1;
1064 getObject(http_request_object, obj);
1065
1066 SET_EH_THROW_HTTP();
1067 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
1068 INIT_PARR(obj, options);
1069 INIT_PARR(obj, responseInfo);
1070 INIT_PARR(obj, responseData);
1071 INIT_PARR(obj, postFields);
1072 INIT_PARR(obj, postFiles);
1073
1074 if (URL) {
1075 UPD_PROP(obj, string, url, URL);
1076 }
1077 if (meth > -1) {
1078 UPD_PROP(obj, long, method, meth);
1079 }
1080 }
1081 SET_EH_NORMAL();
1082 }
1083 /* }}} */
1084
1085 /* {{{ proto void HttpRequest::__destruct()
1086 *
1087 * Destroys the HttpRequest object.
1088 */
1089 PHP_METHOD(HttpRequest, __destruct)
1090 {
1091 getObject(http_request_object, obj);
1092
1093 NO_ARGS;
1094
1095 FREE_PARR(obj, options);
1096 FREE_PARR(obj, responseInfo);
1097 FREE_PARR(obj, responseData);
1098 FREE_PARR(obj, postFields);
1099 FREE_PARR(obj, postFiles);
1100 }
1101 /* }}} */
1102
1103 /* {{{ proto bool HttpRequest::setOptions(array options)
1104 *
1105 * Set the request options to use. See http_get() for a full list of available options.
1106 */
1107 PHP_METHOD(HttpRequest, setOptions)
1108 {
1109 char *key = NULL;
1110 ulong idx = 0;
1111 zval *opts, *old_opts, **opt;
1112 getObject(http_request_object, obj);
1113
1114 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1115 RETURN_FALSE;
1116 }
1117
1118 old_opts = GET_PROP(obj, options);
1119
1120 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
1121 FOREACH_KEYVAL(opts, key, idx, opt) {
1122 if (key) {
1123 if (!strcmp(key, "headers")) {
1124 zval **headers;
1125 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
1126 array_merge(*opt, *headers);
1127 continue;
1128 }
1129 } else if (!strcmp(key, "cookies")) {
1130 zval **cookies;
1131 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1132 array_merge(*opt, *cookies);
1133 continue;
1134 }
1135 }
1136 zval_add_ref(opt);
1137 add_assoc_zval(old_opts, key, *opt);
1138
1139 /* reset */
1140 key = NULL;
1141 }
1142 }
1143
1144 RETURN_TRUE;
1145 }
1146 /* }}} */
1147
1148 /* {{{ proto array HttpRequest::getOptions()
1149 *
1150 * Get current set options.
1151 */
1152 PHP_METHOD(HttpRequest, getOptions)
1153 {
1154 NO_ARGS;
1155
1156 IF_RETVAL_USED {
1157 zval *opts;
1158 getObject(http_request_object, obj);
1159
1160 opts = GET_PROP(obj, options);
1161 array_init(return_value);
1162 array_copy(opts, return_value);
1163 }
1164 }
1165 /* }}} */
1166
1167 /* {{{ proto void HttpRequest::unsetOptions()
1168 *
1169 * Unset all options/headers/cookies.
1170 */
1171 PHP_METHOD(HttpRequest, unsetOptions)
1172 {
1173 getObject(http_request_object, obj);
1174
1175 NO_ARGS;
1176
1177 FREE_PARR(obj, options);
1178 INIT_PARR(obj, options);
1179 }
1180 /* }}} */
1181
1182 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1183 *
1184 * Set additional SSL options.
1185 */
1186 PHP_METHOD(HttpRequest, setSslOptions)
1187 {
1188 zval *opts, *old_opts, **ssl_options;
1189 getObject(http_request_object, obj);
1190
1191 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1192 RETURN_FALSE;
1193 }
1194
1195 old_opts = GET_PROP(obj, options);
1196
1197 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1198 array_merge(opts, *ssl_options);
1199 } else {
1200 zval_add_ref(&opts);
1201 add_assoc_zval(old_opts, "ssl", opts);
1202 }
1203
1204 RETURN_TRUE;
1205 }
1206 /* }}} */
1207
1208 /* {{{ proto array HttpRequest::getSslOtpions()
1209 *
1210 * Get previously set SSL options.
1211 */
1212 PHP_METHOD(HttpRequest, getSslOptions)
1213 {
1214 NO_ARGS;
1215
1216 IF_RETVAL_USED {
1217 zval *opts, **ssl_options;
1218 getObject(http_request_object, obj);
1219
1220 opts = GET_PROP(obj, options);
1221
1222 array_init(return_value);
1223
1224 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1225 array_copy(*ssl_options, return_value);
1226 }
1227 }
1228 }
1229 /* }}} */
1230
1231 /* {{{ proto void HttpRequest::unsetSslOptions()
1232 *
1233 * Unset previously set SSL options.
1234 */
1235 PHP_METHOD(HttpRequest, unsetSslOptions)
1236 {
1237 zval *opts;
1238 getObject(http_request_object, obj);
1239
1240 NO_ARGS;
1241
1242 opts = GET_PROP(obj, options);
1243 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
1244 }
1245 /* }}} */
1246
1247 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1248 *
1249 * Add request header name/value pairs.
1250 */
1251 PHP_METHOD(HttpRequest, addHeaders)
1252 {
1253 zval *opts, **headers, *new_headers;
1254 getObject(http_request_object, obj);
1255
1256 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
1257 RETURN_FALSE;
1258 }
1259
1260 opts = GET_PROP(obj, options);
1261
1262 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1263 array_merge(new_headers, *headers);
1264 } else {
1265 zval_add_ref(&new_headers);
1266 add_assoc_zval(opts, "headers", new_headers);
1267 }
1268
1269 RETURN_TRUE;
1270 }
1271 /* }}} */
1272
1273 /* {{{ proto array HttpRequest::getHeaders()
1274 *
1275 * Get previously set request headers.
1276 */
1277 PHP_METHOD(HttpRequest, getHeaders)
1278 {
1279 NO_ARGS;
1280
1281 IF_RETVAL_USED {
1282 zval *opts, **headers;
1283 getObject(http_request_object, obj);
1284
1285 opts = GET_PROP(obj, options);
1286
1287 array_init(return_value);
1288
1289 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1290 array_copy(*headers, return_value);
1291 }
1292 }
1293 }
1294 /* }}} */
1295
1296 /* {{{ proto void HttpRequest::unsetHeaders()
1297 *
1298 * Unset previously set request headers.
1299 */
1300 PHP_METHOD(HttpRequest, unsetHeaders)
1301 {
1302 zval *opts;
1303 getObject(http_request_object, obj);
1304
1305 NO_ARGS;
1306
1307 opts = GET_PROP(obj, options);
1308 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
1309 }
1310 /* }}} */
1311
1312 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1313 *
1314 * Add cookies.
1315 */
1316 PHP_METHOD(HttpRequest, addCookies)
1317 {
1318 zval *opts, **cookies, *new_cookies;
1319 getObject(http_request_object, obj);
1320
1321 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
1322 RETURN_FALSE;
1323 }
1324
1325 opts = GET_PROP(obj, options);
1326
1327 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1328 array_merge(new_cookies, *cookies);
1329 } else {
1330 zval_add_ref(&new_cookies);
1331 add_assoc_zval(opts, "cookies", new_cookies);
1332 }
1333
1334 RETURN_TRUE;
1335 }
1336 /* }}} */
1337
1338 /* {{{ proto array HttpRequest::getCookies()
1339 *
1340 * Get previously set cookies.
1341 */
1342 PHP_METHOD(HttpRequest, getCookies)
1343 {
1344 NO_ARGS;
1345
1346 IF_RETVAL_USED {
1347 zval *opts, **cookies;
1348 getObject(http_request_object, obj);
1349
1350 opts = GET_PROP(obj, options);
1351
1352 array_init(return_value);
1353
1354 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1355 array_copy(*cookies, return_value);
1356 }
1357 }
1358 }
1359 /* }}} */
1360
1361 /* {{{ proto void HttpRequest::unsetCookies()
1362 *
1363 */
1364 PHP_METHOD(HttpRequest, unsetCookies)
1365 {
1366 zval *opts;
1367 getObject(http_request_object, obj);
1368
1369 NO_ARGS;
1370
1371 opts = GET_PROP(obj, options);
1372 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
1373 }
1374 /* }}} */
1375
1376 /* {{{ proto bool HttpRequest::setUrl(string url)
1377 *
1378 * Set the request URL.
1379 */
1380 PHP_METHOD(HttpRequest, setUrl)
1381 {
1382 char *URL = NULL;
1383 int URL_len;
1384 getObject(http_request_object, obj);
1385
1386 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1387 RETURN_FALSE;
1388 }
1389
1390 UPD_PROP(obj, string, url, URL);
1391 RETURN_TRUE;
1392 }
1393 /* }}} */
1394
1395 /* {{{ proto string HttpRequest::getUrl()
1396 *
1397 * Get the previously set request URL.
1398 */
1399 PHP_METHOD(HttpRequest, getUrl)
1400 {
1401 NO_ARGS;
1402
1403 IF_RETVAL_USED {
1404 zval *URL;
1405 getObject(http_request_object, obj);
1406
1407 URL = GET_PROP(obj, url);
1408 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
1409 }
1410 }
1411 /* }}} */
1412
1413 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1414 *
1415 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1416 * <tt>HTTP_POST</tt> constants.
1417 */
1418 PHP_METHOD(HttpRequest, setMethod)
1419 {
1420 long meth;
1421 getObject(http_request_object, obj);
1422
1423 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1424 RETURN_FALSE;
1425 }
1426
1427 UPD_PROP(obj, long, method, meth);
1428 RETURN_TRUE;
1429 }
1430 /* }}} */
1431
1432 /* {{{ proto long HttpRequest::getMethod()
1433 *
1434 * Get the previously set request method.
1435 */
1436 PHP_METHOD(HttpRequest, getMethod)
1437 {
1438 NO_ARGS;
1439
1440 IF_RETVAL_USED {
1441 zval *meth;
1442 getObject(http_request_object, obj);
1443
1444 meth = GET_PROP(obj, method);
1445 RETURN_LONG(Z_LVAL_P(meth));
1446 }
1447 }
1448 /* }}} */
1449
1450 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1451 *
1452 * Set the content type the post request should have.
1453 * Use this only if you know what you're doing.
1454 */
1455 PHP_METHOD(HttpRequest, setContentType)
1456 {
1457 char *ctype;
1458 int ct_len;
1459 getObject(http_request_object, obj);
1460
1461 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1462 RETURN_FALSE;
1463 }
1464
1465 if (!strchr(ctype, '/')) {
1466 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
1467 RETURN_FALSE;
1468 }
1469
1470 UPD_PROP(obj, string, contentType, ctype);
1471 RETURN_TRUE;
1472 }
1473 /* }}} */
1474
1475 /* {{{ proto string HttpRequest::getContentType()
1476 *
1477 * Get the previously content type.
1478 */
1479 PHP_METHOD(HttpRequest, getContentType)
1480 {
1481 NO_ARGS;
1482
1483 IF_RETVAL_USED {
1484 zval *ctype;
1485 getObject(http_request_object, obj);
1486
1487 ctype = GET_PROP(obj, contentType);
1488 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1489 }
1490 }
1491 /* }}} */
1492
1493 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1494 *
1495 * Set the URL query parameters to use.
1496 * Overwrites previously set query parameters.
1497 * Affects any request types.
1498 */
1499 PHP_METHOD(HttpRequest, setQueryData)
1500 {
1501 zval *qdata;
1502 char *query_data = NULL;
1503 getObject(http_request_object, obj);
1504
1505 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1506 RETURN_FALSE;
1507 }
1508
1509 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1510 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1511 RETURN_FALSE;
1512 }
1513 UPD_PROP(obj, string, queryData, query_data);
1514 efree(query_data);
1515 RETURN_TRUE;
1516 }
1517
1518 convert_to_string(qdata);
1519 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1520 RETURN_TRUE;
1521 }
1522 /* }}} */
1523
1524 /* {{{ proto string HttpRequest::getQueryData()
1525 *
1526 * Get the current query data in form of an urlencoded query string.
1527 */
1528 PHP_METHOD(HttpRequest, getQueryData)
1529 {
1530 NO_ARGS;
1531
1532 IF_RETVAL_USED {
1533 zval *qdata;
1534 getObject(http_request_object, obj);
1535
1536 qdata = GET_PROP(obj, queryData);
1537 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1538 }
1539 }
1540 /* }}} */
1541
1542 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1543 *
1544 * Add parameters to the query parameter list.
1545 * Affects any request type.
1546 */
1547 PHP_METHOD(HttpRequest, addQueryData)
1548 {
1549 zval *qdata, *old_qdata;
1550 char *query_data = NULL;
1551 getObject(http_request_object, obj);
1552
1553 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1554 RETURN_FALSE;
1555 }
1556
1557 old_qdata = GET_PROP(obj, queryData);
1558
1559 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1560 RETURN_FALSE;
1561 }
1562
1563 UPD_PROP(obj, string, queryData, query_data);
1564 efree(query_data);
1565
1566 RETURN_TRUE;
1567 }
1568 /* }}} */
1569
1570 /* {{{ proto void HttpRequest::unsetQueryData()
1571 *
1572 * Clean the query parameters.
1573 * Affects any request type.
1574 */
1575 PHP_METHOD(HttpRequest, unsetQueryData)
1576 {
1577 getObject(http_request_object, obj);
1578
1579 NO_ARGS;
1580
1581 UPD_PROP(obj, string, queryData, "");
1582 }
1583 /* }}} */
1584
1585 /* {{{ proto bool HttpRequest::addPostFields(array post_data)
1586 *
1587 * Adds POST data entries.
1588 * Affects only POST requests.
1589 */
1590 PHP_METHOD(HttpRequest, addPostFields)
1591 {
1592 zval *post, *post_data;
1593 getObject(http_request_object, obj);
1594
1595 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1596 RETURN_FALSE;
1597 }
1598
1599 post = GET_PROP(obj, postFields);
1600 array_merge(post_data, post);
1601
1602 RETURN_TRUE;
1603 }
1604 /* }}} */
1605
1606 /* {{{ proto bool HttpRequest::setPostFields(array post_data)
1607 *
1608 * Set the POST data entries.
1609 * Overwrites previously set POST data.
1610 * Affects only POST requests.
1611 */
1612 PHP_METHOD(HttpRequest, setPostFields)
1613 {
1614 zval *post, *post_data;
1615 getObject(http_request_object, obj);
1616
1617 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1618 RETURN_FALSE;
1619 }
1620
1621 post = GET_PROP(obj, postFields);
1622 zend_hash_clean(Z_ARRVAL_P(post));
1623 array_copy(post_data, post);
1624
1625 RETURN_TRUE;
1626 }
1627 /* }}}*/
1628
1629 /* {{{ proto array HttpRequest::getPostFields()
1630 *
1631 * Get previously set POST data.
1632 */
1633 PHP_METHOD(HttpRequest, getPostFields)
1634 {
1635 NO_ARGS;
1636
1637 IF_RETVAL_USED {
1638 zval *post_data;
1639 getObject(http_request_object, obj);
1640
1641 post_data = GET_PROP(obj, postFields);
1642 array_init(return_value);
1643 array_copy(post_data, return_value);
1644 }
1645 }
1646 /* }}} */
1647
1648 /* {{{ proto void HttpRequest::unsetPostFields()
1649 *
1650 * Clean POST data entires.
1651 * Affects only POST requests.
1652 */
1653 PHP_METHOD(HttpRequest, unsetPostFields)
1654 {
1655 zval *post_data;
1656 getObject(http_request_object, obj);
1657
1658 NO_ARGS;
1659
1660 post_data = GET_PROP(obj, postFields);
1661 zend_hash_clean(Z_ARRVAL_P(post_data));
1662 }
1663 /* }}} */
1664
1665 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1666 *
1667 * Add a file to the POST request.
1668 * Affects only POST requests.
1669 */
1670 PHP_METHOD(HttpRequest, addPostFile)
1671 {
1672 zval *files, *entry;
1673 char *name, *file, *type = NULL;
1674 int name_len, file_len, type_len = 0;
1675 getObject(http_request_object, obj);
1676
1677 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1678 RETURN_FALSE;
1679 }
1680
1681 if (type_len) {
1682 if (!strchr(type, '/')) {
1683 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1684 RETURN_FALSE;
1685 }
1686 } else {
1687 type = "application/x-octetstream";
1688 type_len = sizeof("application/x-octetstream") - 1;
1689 }
1690
1691 MAKE_STD_ZVAL(entry);
1692 array_init(entry);
1693
1694 add_assoc_stringl(entry, "name", name, name_len, 1);
1695 add_assoc_stringl(entry, "type", type, type_len, 1);
1696 add_assoc_stringl(entry, "file", file, file_len, 1);
1697
1698 files = GET_PROP(obj, postFiles);
1699 add_next_index_zval(files, entry);
1700
1701 RETURN_TRUE;
1702 }
1703 /* }}} */
1704
1705 /* {{{ proto bool HttpRequest::setPostFiles(array post_files)
1706 *
1707 * Set files to post.
1708 * Overwrites previously set post files.
1709 * Affects only POST requests.
1710 */
1711 PHP_METHOD(HttpRequest, setPostFiles)
1712 {
1713 zval *files, *pFiles;
1714 getObject(http_request_object, obj);
1715
1716 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &files)) {
1717 RETURN_FALSE;
1718 }
1719
1720 pFiles = GET_PROP(obj, postFiles);
1721 zend_hash_clean(Z_ARRVAL_P(pFiles));
1722 array_copy(files, pFiles);
1723
1724 RETURN_TRUE;
1725 }
1726 /* }}} */
1727
1728 /* {{{ proto array HttpRequest::getPostFiles()
1729 *
1730 * Get all previously added POST files.
1731 */
1732 PHP_METHOD(HttpRequest, getPostFiles)
1733 {
1734 NO_ARGS;
1735
1736 IF_RETVAL_USED {
1737 zval *files;
1738 getObject(http_request_object, obj);
1739
1740 files = GET_PROP(obj, postFiles);
1741
1742 array_init(return_value);
1743 array_copy(files, return_value);
1744 }
1745 }
1746 /* }}} */
1747
1748 /* {{{ proto void HttpRequest::unsetPostFiles()
1749 *
1750 * Unset the POST files list.
1751 * Affects only POST requests.
1752 */
1753 PHP_METHOD(HttpRequest, unsetPostFiles)
1754 {
1755 zval *files;
1756 getObject(http_request_object, obj);
1757
1758 NO_ARGS;
1759
1760 files = GET_PROP(obj, postFiles);
1761 zend_hash_clean(Z_ARRVAL_P(files));
1762 }
1763 /* }}} */
1764
1765 /* {{{ proto bool HttpRequest::SetPutFile(string file)
1766 *
1767 * Set file to put.
1768 * Affects only PUT requests.
1769 */
1770 PHP_METHOD(HttpRequest, setPutFile)
1771 {
1772 char *file;
1773 int file_len;
1774 getObject(http_request_object, obj);
1775
1776 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &file_len)) {
1777 RETURN_FALSE;
1778 }
1779
1780 UPD_PROP(obj, string, putFile, file);
1781 RETURN_TRUE;
1782 }
1783 /* }}} */
1784
1785 /* {{{ proto string HttpRequest::getPutFile()
1786 *
1787 * Get previously set put file.
1788 */
1789 PHP_METHOD(HttpRequest, getPutFile)
1790 {
1791 NO_ARGS;
1792
1793 IF_RETVAL_USED {
1794 zval *putfile;
1795 getObject(http_request_object, obj);
1796
1797 putfile = GET_PROP(obj, putFile);
1798 RETVAL_STRINGL(Z_STRVAL_P(putfile), Z_STRLEN_P(putfile), 1);
1799 }
1800 }
1801 /* }}} */
1802
1803 /* {{{ proto void HttpRequest::unsetPutFile()
1804 *
1805 * Unset file to put.
1806 * Affects only PUT requests.
1807 */
1808 PHP_METHOD(HttpRequest, unsetPutFile)
1809 {
1810 getObject(http_request_object, obj);
1811
1812 NO_ARGS;
1813
1814 UPD_PROP(obj, string, putFile, "");
1815 }
1816 /* }}} */
1817
1818 /* {{{ proto array HttpRequest::getResponseData()
1819 *
1820 * Get all response data after the request has been sent.
1821 */
1822 PHP_METHOD(HttpRequest, getResponseData)
1823 {
1824 NO_ARGS;
1825
1826 IF_RETVAL_USED {
1827 zval *data;
1828 getObject(http_request_object, obj);
1829
1830 data = GET_PROP(obj, responseData);
1831 array_init(return_value);
1832 array_copy(data, return_value);
1833 }
1834 }
1835 /* }}} */
1836
1837 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1838 *
1839 * Get response header(s) after the request has been sent.
1840 */
1841 PHP_METHOD(HttpRequest, getResponseHeader)
1842 {
1843 IF_RETVAL_USED {
1844 zval *data, **headers, **header;
1845 char *header_name = NULL;
1846 int header_len = 0;
1847 getObject(http_response_object, obj);
1848
1849 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1850 RETURN_FALSE;
1851 }
1852
1853 data = GET_PROP(obj, responseData);
1854 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1855 RETURN_FALSE;
1856 }
1857
1858 if (!header_len || !header_name) {
1859 array_init(return_value);
1860 array_copy(*headers, return_value);
1861 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1862 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1863 } else {
1864 RETURN_FALSE;
1865 }
1866 }
1867 }
1868 /* }}} */
1869
1870 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1871 *
1872 * Get response cookie(s) after the request has been sent.
1873 */
1874 PHP_METHOD(HttpRequest, getResponseCookie)
1875 {
1876 IF_RETVAL_USED {
1877 zval *data, **headers;
1878 char *cookie_name = NULL;
1879 int cookie_len = 0;
1880 getObject(http_request_object, obj);
1881
1882 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1883 RETURN_FALSE;
1884 }
1885
1886 array_init(return_value);
1887
1888 data = GET_PROP(obj, responseData);
1889 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1890 ulong idx = 0;
1891 char *key = NULL;
1892 zval **header = NULL;
1893
1894 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1895 if (key && !strcasecmp(key, "Set-Cookie")) {
1896 /* several cookies? */
1897 if (Z_TYPE_PP(header) == IS_ARRAY) {
1898 zval **cookie;
1899
1900 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1901 zval *cookie_hash;
1902 MAKE_STD_ZVAL(cookie_hash);
1903 array_init(cookie_hash);
1904
1905 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1906 if (!cookie_len) {
1907 add_next_index_zval(return_value, cookie_hash);
1908 } else {
1909 zval **name;
1910
1911 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1912 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1913 add_next_index_zval(return_value, cookie_hash);
1914 return; /* <<< FOUND >>> */
1915 } else {
1916 zval_dtor(cookie_hash);
1917 efree(cookie_hash);
1918 }
1919 }
1920 } else {
1921 zval_dtor(cookie_hash);
1922 efree(cookie_hash);
1923 }
1924 }
1925 } else {
1926 zval *cookie_hash;
1927 MAKE_STD_ZVAL(cookie_hash);
1928 array_init(cookie_hash);
1929
1930 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1931 if (!cookie_len) {
1932 add_next_index_zval(return_value, cookie_hash);
1933 } else {
1934 zval **name;
1935
1936 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1937 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1938 add_next_index_zval(return_value, cookie_hash);
1939 } else {
1940 zval_dtor(cookie_hash);
1941 efree(cookie_hash);
1942 }
1943 }
1944 } else {
1945 zval_dtor(cookie_hash);
1946 efree(cookie_hash);
1947 }
1948 }
1949 break;
1950 }
1951 /* reset key */
1952 key = NULL;
1953 }
1954 }
1955 }
1956 }
1957 /* }}} */
1958
1959 /* {{{ proto string HttpRequest::getResponseBody()
1960 *
1961 * Get the response body after the request has been sent.
1962 */
1963 PHP_METHOD(HttpRequest, getResponseBody)
1964 {
1965 NO_ARGS;
1966
1967 IF_RETVAL_USED {
1968 zval *data, **body;
1969 getObject(http_request_object, obj);
1970
1971 data = GET_PROP(obj, responseData);
1972 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1973 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1974 } else {
1975 RETURN_FALSE;
1976 }
1977 }
1978 }
1979 /* }}} */
1980
1981 /* {{{ proto int HttpRequest::getResponseCode()
1982 *
1983 * Get the response code after the request has been sent.
1984 */
1985 PHP_METHOD(HttpRequest, getResponseCode)
1986 {
1987 NO_ARGS;
1988
1989 IF_RETVAL_USED {
1990 zval *code;
1991 getObject(http_request_object, obj);
1992
1993 code = GET_PROP(obj, responseCode);
1994 RETURN_LONG(Z_LVAL_P(code));
1995 }
1996 }
1997 /* }}} */
1998
1999 /* {{{ proto array HttpRequest::getResponseInfo([string name])
2000 *
2001 * Get response info after the request has been sent.
2002 * See http_get() for a full list of returned info.
2003 */
2004 PHP_METHOD(HttpRequest, getResponseInfo)
2005 {
2006 IF_RETVAL_USED {
2007 zval *info, **infop;
2008 char *info_name = NULL;
2009 int info_len = 0;
2010 getObject(http_request_object, obj);
2011
2012 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
2013 RETURN_FALSE;
2014 }
2015
2016 info = GET_PROP(obj, responseInfo);
2017
2018 if (info_len && info_name) {
2019 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
2020 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
2021 } else {
2022 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
2023 RETURN_FALSE;
2024 }
2025 } else {
2026 array_init(return_value);
2027 array_copy(info, return_value);
2028 }
2029 }
2030 }
2031 /* }}}*/
2032
2033 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
2034 *
2035 * Get the full response as HttpMessage object.
2036 */
2037 PHP_METHOD(HttpRequest, getResponseMessage)
2038 {
2039 NO_ARGS;
2040
2041 IF_RETVAL_USED {
2042 zval *message;
2043 getObject(http_request_object, obj);
2044
2045 message = GET_PROP(obj, responseMessage);
2046 if (Z_TYPE_P(message) == IS_OBJECT) {
2047 RETVAL_OBJECT(message);
2048 } else {
2049 RETURN_NULL();
2050 }
2051 }
2052 }
2053 /* }}} */
2054
2055 /* {{{ proto bool HttpRequest::send()
2056 *
2057 * Send the HTTP request.
2058 *
2059 * GET example:
2060 * <pre>
2061 * <?php
2062 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
2063 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
2064 * $r->addQueryData(array('category' => 3));
2065 * try {
2066 * $r->send();
2067 * if ($r->getResponseCode() == 200) {
2068 * file_put_contents('local.rss', $r->getResponseBody());
2069 * }
2070 * } catch (HttpException $ex) {
2071 * echo $ex;
2072 * }
2073 * ?>
2074 * </pre>
2075 *
2076 * POST example:
2077 * <pre>
2078 * <?php
2079 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
2080 * $r->setOptions(array('cookies' => array('lang' => 'de')));
2081 * $r->addpostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
2082 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
2083 * if ($r->send()) {
2084 * echo $r->getResponseBody();
2085 * }
2086 * ?>
2087 * </pre>
2088 */
2089 PHP_METHOD(HttpRequest, send)
2090 {
2091 STATUS status = FAILURE;
2092 http_request_body body = {0};
2093 getObject(http_request_object, obj);
2094
2095 NO_ARGS;
2096
2097 SET_EH_THROW_HTTP();
2098
2099 if (obj->pool) {
2100 http_error(E_WARNING, HTTP_E_CURL, "Cannot perform HttpRequest::send() while attached to an HttpRequestPool");
2101 SET_EH_NORMAL();
2102 RETURN_FALSE;
2103 }
2104
2105 if (SUCCESS == (status = http_request_object_requesthandler(obj, getThis(), &body))) {
2106 status = http_request_exec(obj->ch, NULL);
2107 }
2108 http_request_body_dtor(&body);
2109
2110 /* final data handling */
2111 if (SUCCESS == status) {
2112 status = http_request_object_responsehandler(obj, getThis());
2113 }
2114
2115 SET_EH_NORMAL();
2116 RETURN_SUCCESS(status);
2117 }
2118 /* }}} */
2119
2120 /* {{{ HttpRequestPool */
2121
2122 /* {{{ proto void HttpRequestPool::__construct([HttpRequest request[, ...]])
2123 *
2124 * Instantiate a new HttpRequestPool object. An HttpRequestPool is
2125 * able to send several HttpRequests in parallel.
2126 *
2127 * Example:
2128 * <pre>
2129 * <?php
2130 * $urls = array('www.php.net', 'pecl.php.net', 'pear.php.net')
2131 * $pool = new HttpRequestPool;
2132 * foreach ($urls as $url) {
2133 * $req[$url] = new HttpRequest("http://$url", HTTP_HEAD);
2134 * $pool->attach($req[$url]);
2135 * }
2136 * $pool->send();
2137 * foreach ($urls as $url) {
2138 * printf("%s (%s) is %s\n",
2139 * $url, $req[$url]->getResponseInfo('effective_url'),
2140 * $r->getResponseCode() == 200 ? 'alive' : 'not alive'
2141 * );
2142 * }
2143 * ?>
2144 * </pre>
2145 */
2146 PHP_METHOD(HttpRequestPool, __construct)
2147 {
2148 int argc = ZEND_NUM_ARGS();
2149 zval ***argv = safe_emalloc(argc, sizeof(zval *), 0);
2150 getObject(http_requestpool_object, obj);
2151
2152 if (SUCCESS == zend_get_parameters_array_ex(argc, argv)) {
2153 int i;
2154
2155 for (i = 0; i < argc; ++i) {
2156 if (Z_TYPE_PP(argv[i]) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(argv[i]), http_request_object_ce TSRMLS_CC)) {
2157 http_request_pool_attach(&obj->pool, *(argv[i]));
2158 }
2159 }
2160 }
2161 efree(argv);
2162 }
2163 /* }}} */
2164
2165 /* {{{ proto void HttpRequestPool::__destruct()
2166 *
2167 * Clean up HttpRequestPool object.
2168 */
2169 PHP_METHOD(HttpRequestPool, __destruct)
2170 {
2171 getObject(http_requestpool_object, obj);
2172
2173 NO_ARGS;
2174
2175 http_request_pool_detach_all(&obj->pool);
2176 }
2177 /* }}} */
2178
2179 /* {{{ proto void HttpRequestPool::reset()
2180 *
2181 * Detach all attached HttpRequest objects.
2182 */
2183 PHP_METHOD(HttpRequestPool, reset)
2184 {
2185 getObject(http_requestpool_object, obj);
2186
2187 NO_ARGS;
2188
2189 http_request_pool_detach_all(&obj->pool);
2190 }
2191
2192 /* {{{ proto bool HttpRequestPool::attach(HttpRequest request)
2193 *
2194 * Attach an HttpRequest object to this HttpRequestPool.
2195 * NOTE: set all options prior attaching!
2196 */
2197 PHP_METHOD(HttpRequestPool, attach)
2198 {
2199 zval *request;
2200 STATUS status = FAILURE;
2201 getObject(http_requestpool_object, obj);
2202
2203 SET_EH_THROW_HTTP();
2204 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
2205 status = http_request_pool_attach(&obj->pool, request);
2206 }
2207 SET_EH_NORMAL();
2208 RETURN_SUCCESS(status);
2209 }
2210 /* }}} */
2211
2212 /* {{{ proto bool HttpRequestPool::detach(HttpRequest request)
2213 *
2214 * Detach an HttpRequest object from this HttpRequestPool.
2215 */
2216 PHP_METHOD(HttpRequestPool, detach)
2217 {
2218 zval *request;
2219 STATUS status = FAILURE;
2220 getObject(http_requestpool_object, obj);
2221
2222 SET_EH_THROW_HTTP();
2223 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, http_request_object_ce)) {
2224 status = http_request_pool_detach(&obj->pool, request);
2225 }
2226 SET_EH_NORMAL();
2227 RETURN_SUCCESS(status);
2228 }
2229 /* }}} */
2230
2231 /* {{{ proto bool HttpRequestPool::send()
2232 *
2233 * Send all attached HttpRequest objects in parallel.
2234 */
2235 PHP_METHOD(HttpRequestPool, send)
2236 {
2237 STATUS status;
2238 getObject(http_requestpool_object, obj);
2239
2240 NO_ARGS;
2241
2242 SET_EH_THROW_HTTP();
2243 status = http_request_pool_send(&obj->pool);
2244 SET_EH_NORMAL();
2245
2246 RETURN_SUCCESS(status);
2247 }
2248 /* }}} */
2249
2250 /* {{{ proto protected bool HttpRequestPool::socketSend()
2251 *
2252 * Usage:
2253 * <pre>
2254 * <?php
2255 * while ($pool->socketSend()) {
2256 * do_something_else();
2257 * if (!$pool->socketSelect()) {
2258 * die('Socket error');
2259 * }
2260 * }
2261 * $pool->socketRead();
2262 * ?>
2263 * </pre>
2264 */
2265 PHP_METHOD(HttpRequestPool, socketSend)
2266 {
2267 getObject(http_requestpool_object, obj);
2268
2269 NO_ARGS;
2270
2271 RETURN_BOOL(0 < http_request_pool_perform(&obj->pool));
2272 }
2273 /* }}} */
2274
2275 /* {{{ proto protected bool HttpRequestPool::socketSelect()
2276 *
2277 * See HttpRequestPool::socketSend().
2278 */
2279 PHP_METHOD(HttpRequestPool, socketSelect)
2280 {
2281 getObject(http_requestpool_object, obj);
2282
2283 NO_ARGS;
2284
2285 RETURN_SUCCESS(http_request_pool_select(&obj->pool));
2286 }
2287 /* }}} */
2288
2289 /* {{{ proto protected void HttpRequestPool::socketRead()
2290 *
2291 * See HttpRequestPool::socketSend().
2292 */
2293 PHP_METHOD(HttpRequestPool, socketRead)
2294 {
2295 getObject(http_requestpool_object, obj);
2296
2297 NO_ARGS;
2298
2299 zend_llist_apply(&obj->pool.handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
2300 }
2301 /* }}} */
2302
2303 /* }}} */
2304
2305 /* }}} */
2306 #endif /* HTTP_HAVE_CURL */
2307
2308 #endif /* ZEND_ENGINE_2 */
2309
2310 /*
2311 * Local variables:
2312 * tab-width: 4
2313 * c-basic-offset: 4
2314 * End:
2315 * vim600: noet sw=4 ts=4 fdm=marker
2316 * vim<600: noet sw=4 ts=4
2317 */
2318