- ditch HttpMessage::__construct() and ::setRaw
[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
22 #include "php.h"
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_api.h"
26 #include "php_http_cache_api.h"
27 #include "php_http_curl_api.h"
28 #include "php_http_date_api.h"
29 #include "php_http_headers_api.h"
30 #include "php_http_message_api.h"
31 #include "php_http_send_api.h"
32 #include "php_http_url_api.h"
33
34 #include "php_http_message_object.h"
35 #include "php_http_response_object.h"
36 #include "php_http_request_object.h"
37 #include "php_http_exception_object.h"
38
39 #ifdef ZEND_ENGINE_2
40
41 /* {{{ HttpResponse */
42
43 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
44 *
45 * Instantiates a new HttpResponse object, which can be used to send
46 * any data/resource/file to an HTTP client with caching and multiple
47 * ranges/resuming support.
48 *
49 * NOTE: GZIPping is not implemented yet.
50 */
51 PHP_METHOD(HttpResponse, __construct)
52 {
53 zend_bool do_cache = 0, do_gzip = 0;
54 getObject(http_response_object, obj);
55
56 SET_EH_THROW_HTTP();
57 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
58 UPD_PROP(obj, long, cache, do_cache);
59 UPD_PROP(obj, long, gzip, do_gzip);
60 }
61 SET_EH_NORMAL();
62 }
63 /* }}} */
64
65 /* {{{ proto bool HttpResponse::setCache(bool cache)
66 *
67 * Whether it sould be attempted to cache the entitity.
68 * This will result in necessary caching headers and checks of clients
69 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
70 * matches a "304 Not Modified" status code will be issued.
71 *
72 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
73 * to something more appropriate than "no-cache"!
74 */
75 PHP_METHOD(HttpResponse, setCache)
76 {
77 zend_bool do_cache = 0;
78 getObject(http_response_object, obj);
79
80 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
81 RETURN_FALSE;
82 }
83
84 UPD_PROP(obj, long, cache, do_cache);
85 RETURN_TRUE;
86 }
87 /* }}} */
88
89 /* {{{ proto bool HttpResponse::getCache()
90 *
91 * Get current caching setting.
92 */
93 PHP_METHOD(HttpResponse, getCache)
94 {
95 zval *do_cache = NULL;
96 getObject(http_response_object, obj);
97
98 NO_ARGS;
99
100 do_cache = GET_PROP(obj, cache);
101 RETURN_BOOL(Z_LVAL_P(do_cache));
102 }
103 /* }}}*/
104
105 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
106 *
107 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
108 */
109 PHP_METHOD(HttpResponse, setGzip)
110 {
111 zend_bool do_gzip = 0;
112 getObject(http_response_object, obj);
113
114 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
115 RETURN_FALSE;
116 }
117
118 UPD_PROP(obj, long, gzip, do_gzip);
119 RETURN_TRUE;
120 }
121 /* }}} */
122
123 /* {{{ proto bool HttpResponse::getGzip()
124 *
125 * Get current gzipping setting.
126 */
127 PHP_METHOD(HttpResponse, getGzip)
128 {
129 zval *do_gzip = NULL;
130 getObject(http_response_object, obj);
131
132 NO_ARGS;
133
134 do_gzip = GET_PROP(obj, gzip);
135 RETURN_BOOL(Z_LVAL_P(do_gzip));
136 }
137 /* }}} */
138
139 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
140 *
141 * Set a custom cache-control header, usually being "private" or "public"; if
142 * $raw is set to true the header will be sent as-is.
143 */
144 PHP_METHOD(HttpResponse, setCacheControl)
145 {
146 char *ccontrol;
147 int cc_len;
148 zend_bool raw = 0;
149 getObject(http_response_object, obj);
150
151 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
152 RETURN_FALSE;
153 }
154
155 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
156 http_error_ex(E_WARNING, HTTP_E_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
157 RETURN_FALSE;
158 }
159
160 UPD_PROP(obj, long, raw_cache_header, raw);
161 UPD_PROP(obj, string, cacheControl, ccontrol);
162 RETURN_TRUE;
163 }
164 /* }}} */
165
166 /* {{{ proto string HttpResponse::getCacheControl()
167 *
168 * Get current Cache-Control header setting.
169 */
170 PHP_METHOD(HttpResponse, getCacheControl)
171 {
172 zval *ccontrol;
173 getObject(http_response_object, obj);
174
175 NO_ARGS;
176
177 ccontrol = GET_PROP(obj, cacheControl);
178 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
179 }
180 /* }}} */
181
182 /* {{{ proto bool HttpResponse::setContentType(string content_type)
183 *
184 * Set the content-type of the sent entity.
185 */
186 PHP_METHOD(HttpResponse, setContentType)
187 {
188 char *ctype;
189 int ctype_len;
190 getObject(http_response_object, obj);
191
192 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
193 RETURN_FALSE;
194 }
195
196 if (!strchr(ctype, '/')) {
197 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
198 RETURN_FALSE;
199 }
200
201 UPD_PROP(obj, string, contentType, ctype);
202
203 RETURN_TRUE;
204 }
205 /* }}} */
206
207 /* {{{ proto string HttpResponse::getContentType()
208 *
209 * Get current Content-Type header setting.
210 */
211 PHP_METHOD(HttpResponse, getContentType)
212 {
213 zval *ctype;
214 getObject(http_response_object, obj);
215
216 NO_ARGS;
217
218 ctype = GET_PROP(obj, contentType);
219 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
220 }
221 /* }}} */
222
223 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
224 *
225 * Set the Content-Disposition of the sent entity. This setting aims to suggest
226 * the receiveing user agent how to handle the sent entity; usually the client
227 * will show the user a "Save As..." popup.
228 */
229 PHP_METHOD(HttpResponse, setContentDisposition)
230 {
231 char *file;
232 int file_len;
233 zend_bool is_inline = 0;
234 getObject(http_response_object, obj);
235
236 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
237 RETURN_FALSE;
238 }
239
240 UPD_PROP(obj, string, dispoFile, file);
241 UPD_PROP(obj, long, dispoInline, is_inline);
242 RETURN_TRUE;
243 }
244 /* }}} */
245
246 /* {{{ proto array HttpResponse::getContentDisposition()
247 *
248 * Get current Content-Disposition setting.
249 * Will return an associative array like:
250 * <pre>
251 * array(
252 * 'filename' => 'foo.bar',
253 * 'inline' => false
254 * )
255 * </pre>
256 */
257 PHP_METHOD(HttpResponse, getContentDisposition)
258 {
259 zval *file;
260 zval *is_inline;
261 getObject(http_response_object, obj);
262
263 if (ZEND_NUM_ARGS()) {
264 WRONG_PARAM_COUNT;
265 }
266
267 file = GET_PROP(obj, dispoFile);
268 is_inline = GET_PROP(obj, dispoInline);
269
270 array_init(return_value);
271 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
272 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
273 }
274 /* }}} */
275
276 /* {{{ proto bool HttpResponse::setETag(string etag)
277 *
278 * Set a custom ETag. Use this only if you know what you're doing.
279 */
280 PHP_METHOD(HttpResponse, setETag)
281 {
282 char *etag;
283 int etag_len;
284 getObject(http_response_object, obj);
285
286 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
287 RETURN_FALSE;
288 }
289
290 UPD_PROP(obj, string, eTag, etag);
291 RETURN_TRUE;
292 }
293 /* }}} */
294
295 /* {{{ proto string HttpResponse::getETag()
296 *
297 * Get the previously set custom ETag.
298 */
299 PHP_METHOD(HttpResponse, getETag)
300 {
301 zval *etag;
302 getObject(http_response_object, obj);
303
304 NO_ARGS;
305
306 etag = GET_PROP(obj, eTag);
307 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
308 }
309 /* }}} */
310
311 /* {{{ proto bool HttpResponse::setData(string data)
312 *
313 * Set the data to be sent.
314 */
315 PHP_METHOD(HttpResponse, setData)
316 {
317 zval *the_data;
318 getObject(http_response_object, obj);
319
320 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
321 RETURN_FALSE;
322 }
323
324 convert_to_string_ex(&the_data);
325 SET_PROP(obj, data, the_data);
326 UPD_PROP(obj, long, lastModified, http_last_modified(the_data, SEND_DATA));
327 UPD_PROP(obj, long, send_mode, SEND_DATA);
328 RETURN_TRUE;
329 }
330 /* }}} */
331
332 /* {{{ proto string HttpResponse::getData()
333 *
334 * Get the previously set data to be sent.
335 */
336 PHP_METHOD(HttpResponse, getData)
337 {
338 zval *the_data;
339 getObject(http_response_object, obj);
340
341 NO_ARGS;
342
343 the_data = GET_PROP(obj, data);
344 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
345 }
346 /* }}} */
347
348 /* {{{ proto bool HttpResponse::setStream(resource stream)
349 *
350 * Set the resource to be sent.
351 */
352 PHP_METHOD(HttpResponse, setStream)
353 {
354 zval *the_stream;
355 php_stream *the_real_stream;
356 getObject(http_response_object, obj);
357
358 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
359 RETURN_FALSE;
360 }
361
362 php_stream_from_zval(the_real_stream, &the_stream);
363
364 SET_PROP(obj, stream, the_stream);
365 UPD_PROP(obj, long, lastModified, http_last_modified(the_real_stream, SEND_RSRC));
366 UPD_PROP(obj, long, send_mode, SEND_RSRC);
367 RETURN_TRUE;
368 }
369 /* }}} */
370
371 /* {{{ proto resource HttpResponse::getStream()
372 *
373 * Get the previously set resource to be sent.
374 */
375 PHP_METHOD(HttpResponse, getStream)
376 {
377 zval *the_stream;
378 getObject(http_response_object, obj);
379
380 NO_ARGS;
381
382 the_stream = GET_PROP(obj, stream);
383 RETURN_RESOURCE(Z_LVAL_P(the_stream));
384 }
385 /* }}} */
386
387 /* {{{ proto bool HttpResponse::setFile(string file)
388 *
389 * Set the file to be sent.
390 */
391 PHP_METHOD(HttpResponse, setFile)
392 {
393 zval *the_file;
394 getObject(http_response_object, obj);
395
396 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
397 RETURN_FALSE;
398 }
399
400 convert_to_string_ex(&the_file);
401
402 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
403 UPD_PROP(obj, long, lastModified, http_last_modified(the_file, -1));
404 UPD_PROP(obj, long, send_mode, -1);
405 RETURN_TRUE;
406 }
407 /* }}} */
408
409 /* {{{ proto string HttpResponse::getFile()
410 *
411 * Get the previously set file to be sent.
412 */
413 PHP_METHOD(HttpResponse, getFile)
414 {
415 zval *the_file;
416 getObject(http_response_object, obj);
417
418 NO_ARGS;
419
420 the_file = GET_PROP(obj, file);
421 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
422 }
423 /* }}} */
424
425 /* {{{ proto bool HttpResponse::send()
426 *
427 * Finally send the entity.
428 *
429 * Example:
430 * <pre>
431 * <?php
432 * $r = new HttpResponse(true);
433 * $r->setFile('../hidden/contract.pdf');
434 * $r->setContentType('application/pdf');
435 * $r->send();
436 * ?>
437 * </pre>
438 *
439 */
440 PHP_METHOD(HttpResponse, send)
441 {
442 zval *do_cache, *do_gzip;
443 getObject(http_response_object, obj);
444
445 NO_ARGS;
446
447 do_cache = GET_PROP(obj, cache);
448 do_gzip = GET_PROP(obj, gzip);
449
450 /* gzip */
451 if (Z_LVAL_P(do_gzip)) {
452 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC);
453 }
454
455 /* caching */
456 if (Z_LVAL_P(do_cache)) {
457 zval *cctrl, *etag, *lmod, *ccraw;
458
459 etag = GET_PROP(obj, eTag);
460 lmod = GET_PROP(obj, lastModified);
461 cctrl = GET_PROP(obj, cacheControl);
462 ccraw = GET_PROP(obj, raw_cache_header);
463
464 if (Z_LVAL_P(ccraw)) {
465 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
466 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
467 } else {
468 char cc_header[42] = {0};
469 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
470 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
471 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
472 }
473 }
474
475 /* content type */
476 {
477 zval *ctype = GET_PROP(obj, contentType);
478 if (Z_STRLEN_P(ctype)) {
479 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
480 } else {
481 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
482 }
483 }
484
485 /* content disposition */
486 {
487 zval *dispo_file = GET_PROP(obj, dispoFile);
488 if (Z_STRLEN_P(dispo_file)) {
489 zval *dispo_inline = GET_PROP(obj, dispoInline);
490 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), (zend_bool) Z_LVAL_P(dispo_inline));
491 }
492 }
493
494 /* send */
495 {
496 zval *send_mode = GET_PROP(obj, send_mode);
497 switch (Z_LVAL_P(send_mode))
498 {
499 case SEND_DATA:
500 {
501 zval *zdata = GET_PROP(obj, data);
502 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
503 }
504
505 case SEND_RSRC:
506 {
507 php_stream *the_real_stream;
508 zval *the_stream = GET_PROP(obj, stream);
509 php_stream_from_zval(the_real_stream, &the_stream);
510 RETURN_SUCCESS(http_send_stream(the_real_stream));
511 }
512
513 default:
514 {
515 zval *zfile = GET_PROP(obj, file);
516 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile)));
517 }
518 }
519 }
520 }
521 /* }}} */
522 /* }}} */
523
524 /* {{{ HttpMessage */
525
526 /* {{{ proto static HttpMessage HttpMessage::fromString(string raw_message)
527 *
528 * Create an HttpMessage object from a string.
529 */
530 PHP_METHOD(HttpMessage, fromString)
531 {
532 char *string = NULL;
533 int length = 0;
534 http_message *msg = NULL;
535 http_message_object obj;
536
537 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &length)) {
538 RETURN_NULL();
539 }
540
541 if (!(msg = http_message_parse(string, length))) {
542 RETURN_NULL();
543 }
544
545 Z_TYPE_P(return_value) = IS_OBJECT;
546 return_value->value.obj = http_message_object_from_msg(msg);
547 }
548 /* }}} */
549
550 /* {{{ proto string HttpMessage::getBody()
551 *
552 * Get the body of the parsed Message.
553 */
554 PHP_METHOD(HttpMessage, getBody)
555 {
556 zval *body;
557 getObject(http_message_object, obj);
558
559 NO_ARGS;
560
561 body = GET_PROP(obj, body);
562 RETURN_STRINGL(Z_STRVAL_P(body), Z_STRLEN_P(body), 1);
563 }
564 /* }}} */
565
566 /* {{{ proto array HttpMessage::getHeaders()
567 *
568 * Get Message Headers.
569 */
570 PHP_METHOD(HttpMessage, getHeaders)
571 {
572 zval *headers;
573 getObject(http_message_object, obj);
574
575 NO_ARGS;
576
577 headers = GET_PROP(obj, headers);
578 array_init(return_value);
579 array_copy(headers, return_value);
580 }
581 /* }}} */
582
583 /* {{{ proto void HttpMessage::setHeaders(array headers)
584 *
585 * Sets new headers.
586 */
587 PHP_METHOD(HttpMessage, setHeaders)
588 {
589 zval *headers;
590 getObject(http_message_object, obj);
591
592 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &headers)) {
593 return;
594 }
595
596 SET_PROP(obj, headers, headers);
597 }
598 /* }}} */
599
600 /* {{{ proto void HttpMessage::addHeaders(array headers[, bool append = false])
601 *
602 * Add headers. If append is true, headers with the same name will be separated, else overwritten.
603 */
604 PHP_METHOD(HttpMessage, addHeaders)
605 {
606 zval *old_headers, *new_headers;
607 zend_bool append = 0;
608 getObject(http_message_object, obj);
609
610 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
611 return;
612 }
613
614 old_headers = GET_PROP(obj, headers);
615 if (append) {
616 array_append(new_headers, old_headers);
617 } else {
618 array_merge(new_headers, old_headers);
619 }
620 SET_PROP(obj, headers, old_headers);
621 }
622 /* }}} */
623
624 /* {{{ proto long HttpMessage::getType()
625 *
626 * Get Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
627 */
628 PHP_METHOD(HttpMessage, getType)
629 {
630 zval *type;
631 getObject(http_message_object, obj);
632
633 NO_ARGS;
634
635 type = GET_PROP(obj, type);
636 RETURN_LONG(Z_LVAL_P(type));
637 }
638 /* }}} */
639
640 /* {{{ proto void HttpMessage::setType(long type)
641 *
642 * Set Message Type. (HTTP_MSG_NONE|HTTP_MSG_REQUEST|HTTP_MSG_RESPONSE)
643 */
644 PHP_METHOD(HttpMessage, setType)
645 {
646 long type;
647 getObject(http_message_object, obj);
648
649 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
650 return;
651 }
652 UPD_PROP(obj, long, type, type);
653 }
654 /* }}} */
655
656 /* {{{ proto long HttpMessage::getResponseCode()
657 *
658 * Get the Response Code of the Message.
659 */
660 PHP_METHOD(HttpMessage, getResponseCode)
661 {
662 zval *status;
663 getObject(http_message_object, obj);
664
665 NO_ARGS;
666
667 if (obj->message->type != HTTP_MSG_RESPONSE) {
668 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
669 RETURN_NULL();
670 }
671
672 status = GET_PROP(obj, responseCode);
673 RETURN_LONG(Z_LVAL_P(status));
674 }
675 /* }}} */
676
677 /* {{{ proto bool HttpMessage::setResponseCode(long code)
678 *
679 * Set the response code of an HTTP Response Message.
680 * Returns false if the Message is not of type HTTP_MSG_RESPONSE,
681 * or if the response code is out of range (100-510).
682 */
683 PHP_METHOD(HttpMessage, setResponseCode)
684 {
685 long code;
686 getObject(http_message_object, obj);
687
688 if (obj->message->type != HTTP_MSG_RESPONSE) {
689 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_RESPONSE");
690 RETURN_FALSE;
691 }
692
693 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
694 RETURN_FALSE;
695 }
696 if (code < 100 || code > 510) {
697 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid response code (100-510): %ld", code);
698 RETURN_FALSE;
699 }
700
701 UPD_PROP(obj, long, responseCode, code);
702 RETURN_TRUE;
703 }
704 /* }}} */
705
706 /* {{{ proto string HttpMessage::getRequestMethod()
707 *
708 * Get the Request Method of the Message.
709 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
710 */
711 PHP_METHOD(HttpMessage, getRequestMethod)
712 {
713 zval *method;
714 getObject(http_message_object, obj);
715
716 NO_ARGS;
717
718 if (obj->message->type != HTTP_MSG_REQUEST) {
719 http_error(E_NOTICE, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
720 RETURN_NULL();
721 }
722
723 method = GET_PROP(obj, requestMethod);
724 RETURN_STRINGL(Z_STRVAL_P(method), Z_STRLEN_P(method), 1);
725 }
726 /* }}} */
727
728 /* {{{ proto bool HttpMessage::setRequestMethod(string method)
729 *
730 * Set the Request Method of the HTTP Message.
731 * Returns false if the Message is not of type HTTP_MSG_REQUEST.
732 */
733 PHP_METHOD(HttpMessage, setRequestMethod)
734 {
735 char *method;
736 int method_len;
737 getObject(http_message_object, obj);
738
739 if (obj->message->type != HTTP_MSG_REQUEST) {
740 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
741 RETURN_FALSE;
742 }
743
744 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
745 RETURN_FALSE;
746 }
747 if (method_len < 1) {
748 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestMethod to an empty string");
749 RETURN_FALSE;
750 }
751
752 UPD_PROP(obj, string, requestMethod, method);
753 RETURN_TRUE;
754 }
755 /* }}} */
756
757 /* {{{ proto string HttpMessage::getRequestUri()
758 *
759 * Get the Request URI of the Message.
760 */
761 PHP_METHOD(HttpMessage, getRequestUri)
762 {
763 zval *uri;
764 getObject(http_message_object, obj);
765
766 NO_ARGS;
767
768 if (obj->message->type != HTTP_MSG_REQUEST) {
769 RETURN_NULL();
770 }
771
772 uri = GET_PROP(obj, requestUri);
773 RETURN_STRINGL(Z_STRVAL_P(uri), Z_STRLEN_P(uri), 1);
774 }
775 /* }}} */
776
777 /* {{{ proto bool HttpMessage::setRequestUri(string URI)
778 *
779 * Set the Request URI of the HTTP Message.
780 * Returns false if the Message is not of type HTTP_MSG_REQUEST,
781 * or if paramtere URI was empty.
782 */
783 PHP_METHOD(HttpMessage, setRequestUri)
784 {
785 char *URI;
786 int URIlen;
787 getObject(http_message_object, obj);
788
789 if (obj->message->type != HTTP_MSG_REQUEST) {
790 http_error(E_WARNING, HTTP_E_MSG, "HttpMessage is not of type HTTP_MSG_REQUEST");
791 RETURN_FALSE;
792 }
793 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
794 RETURN_FALSE;
795 }
796 if (URIlen < 1) {
797 http_error(E_WARNING, HTTP_E_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
798 RETURN_FALSE;
799 }
800
801 UPD_PROP(obj, string, requestUri, URI);
802 RETURN_TRUE;
803 }
804 /* }}} */
805
806 /* {{{ proto string HttpMessage::getHttpVersion()
807 *
808 * Get the HTTP Protocol Version of the Message.
809 */
810 PHP_METHOD(HttpMessage, getHttpVersion)
811 {
812 zval *version;
813 char ver[4] = {0};
814 getObject(http_message_object, obj);
815
816 NO_ARGS;
817
818 version = GET_PROP(obj, httpVersion);
819
820 if (Z_TYPE_P(version) == IS_NULL) {
821 RETURN_NULL();
822 }
823
824 sprintf(ver, "1.1f", Z_DVAL_P(version));
825 RETURN_STRINGL(ver, 3, 1);
826 }
827 /* }}} */
828
829 /* {{{ proto bool HttpMessage::setHttpVersion(string version)
830 *
831 * Set the HTTP Protocol version of the Message.
832 * Returns false if version is invalid (1.0 and 1.1).
833 */
834 PHP_METHOD(HttpMessage, setHttpVersion)
835 {
836 char v[4];
837 zval *zv, *version;
838 getObject(http_message_object, obj);
839
840 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &zv)) {
841 return;
842 }
843
844 convert_to_double_ex(&zv);
845 sprintf(v, "%1.1f", Z_DVAL_P(zv));
846 if (strcmp(v, "1.0") && strcmp(v, "1.1")) {
847 http_error_ex(E_WARNING, HTTP_E_PARAM, "Invalid HTTP version (1.0 or 1.1): %s", v);
848 RETURN_FALSE;
849 }
850
851 SET_PROP(obj, httpVersion, zv);
852 }
853 /* }}} */
854
855 /* {{{ proto string HttpMessage::toString()
856 *
857 * Get the string representation of the Message.
858 */
859 PHP_METHOD(HttpMessage, toString)
860 {
861 char *string;
862 size_t length;
863 getObject(http_message_object, obj);
864
865 NO_ARGS;
866
867 http_message_tostring(obj->message, &string, &length);
868 RETURN_STRINGL(string, length, 0);
869 }
870 /* }}} */
871
872 /* }}} */
873
874 #ifdef HTTP_HAVE_CURL
875 /* {{{ HttpRequest */
876
877 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
878 *
879 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
880 * and POST (including posting files) HTTP requests.
881 */
882 PHP_METHOD(HttpRequest, __construct)
883 {
884 char *URL = NULL;
885 int URL_len;
886 long meth = -1;
887 getObject(http_request_object, obj);
888
889 SET_EH_THROW_HTTP();
890 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
891 INIT_PARR(obj, options);
892 INIT_PARR(obj, responseInfo);
893 INIT_PARR(obj, responseData);
894 INIT_PARR(obj, postData);
895 INIT_PARR(obj, postFiles);
896
897 if (URL) {
898 UPD_PROP(obj, string, url, URL);
899 }
900 if (meth > -1) {
901 UPD_PROP(obj, long, method, meth);
902 }
903 }
904 SET_EH_NORMAL();
905 }
906 /* }}} */
907
908 /* {{{ proto void HttpRequest::__destruct()
909 *
910 * Destroys the HttpRequest object.
911 */
912 PHP_METHOD(HttpRequest, __destruct)
913 {
914 getObject(http_request_object, obj);
915
916 NO_ARGS;
917
918 FREE_PARR(obj, options);
919 FREE_PARR(obj, responseInfo);
920 FREE_PARR(obj, responseData);
921 FREE_PARR(obj, postData);
922 FREE_PARR(obj, postFiles);
923 }
924 /* }}} */
925
926 /* {{{ proto bool HttpRequest::setOptions(array options)
927 *
928 * Set the request options to use. See http_get() for a full list of available options.
929 */
930 PHP_METHOD(HttpRequest, setOptions)
931 {
932 char *key = NULL;
933 long idx = 0;
934 zval *opts, *old_opts, **opt;
935 getObject(http_request_object, obj);
936
937 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
938 RETURN_FALSE;
939 }
940
941 old_opts = GET_PROP(obj, options);
942
943 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
944 FOREACH_KEYVAL(opts, key, idx, opt) {
945 if (key) {
946 if (!strcmp(key, "headers")) {
947 zval **headers;
948 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
949 array_merge(*opt, *headers);
950 continue;
951 }
952 } else if (!strcmp(key, "cookies")) {
953 zval **cookies;
954 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
955 array_merge(*opt, *cookies);
956 continue;
957 }
958 }
959 zval_add_ref(opt);
960 add_assoc_zval(old_opts, key, *opt);
961
962 /* reset */
963 key = NULL;
964 }
965 }
966
967 RETURN_TRUE;
968 }
969 /* }}} */
970
971 /* {{{ proto array HttpRequest::getOptions()
972 *
973 * Get current set options.
974 */
975 PHP_METHOD(HttpRequest, getOptions)
976 {
977 zval *opts;
978 getObject(http_request_object, obj);
979
980 NO_ARGS;
981
982 opts = GET_PROP(obj, options);
983 array_init(return_value);
984 array_copy(opts, return_value);
985 }
986 /* }}} */
987
988 /* {{{ proto void HttpRequest::unsetOptions()
989 *
990 * Unset all options/headers/cookies.
991 */
992 PHP_METHOD(HttpRequest, unsetOptions)
993 {
994 getObject(http_request_object, obj);
995
996 NO_ARGS;
997
998 FREE_PARR(obj, options);
999 INIT_PARR(obj, options);
1000 }
1001 /* }}} */
1002
1003 /* {{{ proto bool HttpRequest::setSslOptions(array options)
1004 *
1005 * Set additional SSL options.
1006 */
1007 PHP_METHOD(HttpRequest, setSslOptions)
1008 {
1009 zval *opts, *old_opts, **ssl_options;
1010 getObject(http_request_object, obj);
1011
1012 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
1013 RETURN_FALSE;
1014 }
1015
1016 old_opts = GET_PROP(obj, options);
1017
1018 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1019 array_merge(opts, *ssl_options);
1020 } else {
1021 zval_add_ref(&opts);
1022 add_assoc_zval(old_opts, "ssl", opts);
1023 }
1024
1025 RETURN_TRUE;
1026 }
1027 /* }}} */
1028
1029 /* {{{ proto array HttpRequest::getSslOtpions()
1030 *
1031 * Get previously set SSL options.
1032 */
1033 PHP_METHOD(HttpRequest, getSslOptions)
1034 {
1035 zval *opts, **ssl_options;
1036 getObject(http_request_object, obj);
1037
1038 NO_ARGS;
1039
1040 opts = GET_PROP(obj, options);
1041
1042 array_init(return_value);
1043
1044 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"), (void **) &ssl_options)) {
1045 array_copy(*ssl_options, return_value);
1046 }
1047 }
1048 /* }}} */
1049
1050 /* {{{ proto void HttpRequest::unsetSslOptions()
1051 *
1052 * Unset previously set SSL options.
1053 */
1054 PHP_METHOD(HttpRequest, unsetSslOptions)
1055 {
1056 zval *opts;
1057 getObject(http_request_object, obj);
1058
1059 NO_ARGS;
1060
1061 opts = GET_PROP(obj, options);
1062 zend_hash_del(Z_ARRVAL_P(opts), "ssl", sizeof("ssl"));
1063 }
1064 /* }}} */
1065
1066 /* {{{ proto bool HttpRequest::addHeaders(array headers)
1067 *
1068 * Add request header name/value pairs.
1069 */
1070 PHP_METHOD(HttpRequest, addHeaders)
1071 {
1072 zval *opts, **headers, *new_headers;
1073 getObject(http_request_object, obj);
1074
1075 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
1076 RETURN_FALSE;
1077 }
1078
1079 opts = GET_PROP(obj, options);
1080
1081 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1082 array_merge(new_headers, *headers);
1083 } else {
1084 zval_add_ref(&new_headers);
1085 add_assoc_zval(opts, "headers", new_headers);
1086 }
1087
1088 RETURN_TRUE;
1089 }
1090 /* }}} */
1091
1092 /* {{{ proto array HttpRequest::getHeaders()
1093 *
1094 * Get previously set request headers.
1095 */
1096 PHP_METHOD(HttpRequest, getHeaders)
1097 {
1098 zval *opts, **headers;
1099 getObject(http_request_object, obj);
1100
1101 NO_ARGS;
1102
1103 opts = GET_PROP(obj, options);
1104
1105 array_init(return_value);
1106
1107 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
1108 array_copy(*headers, return_value);
1109 }
1110 }
1111 /* }}} */
1112
1113 /* {{{ proto void HttpRequest::unsetHeaders()
1114 *
1115 * Unset previously set request headers.
1116 */
1117 PHP_METHOD(HttpRequest, unsetHeaders)
1118 {
1119 zval *opts;
1120 getObject(http_request_object, obj);
1121
1122 NO_ARGS;
1123
1124 opts = GET_PROP(obj, options);
1125 zend_hash_del(Z_ARRVAL_P(opts), "headers", sizeof("headers"));
1126 }
1127 /* }}} */
1128
1129 /* {{{ proto bool HttpRequest::addCookies(array cookies)
1130 *
1131 * Add cookies.
1132 */
1133 PHP_METHOD(HttpRequest, addCookies)
1134 {
1135 zval *opts, **cookies, *new_cookies;
1136 getObject(http_request_object, obj);
1137
1138 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
1139 RETURN_FALSE;
1140 }
1141
1142 opts = GET_PROP(obj, options);
1143
1144 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1145 array_merge(new_cookies, *cookies);
1146 } else {
1147 zval_add_ref(&new_cookies);
1148 add_assoc_zval(opts, "cookies", new_cookies);
1149 }
1150
1151 RETURN_TRUE;
1152 }
1153 /* }}} */
1154
1155 /* {{{ proto array HttpRequest::getCookies()
1156 *
1157 * Get previously set cookies.
1158 */
1159 PHP_METHOD(HttpRequest, getCookies)
1160 {
1161 zval *opts, **cookies;
1162 getObject(http_request_object, obj);
1163
1164 NO_ARGS;
1165
1166 opts = GET_PROP(obj, options);
1167
1168 array_init(return_value);
1169
1170 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
1171 array_copy(*cookies, return_value);
1172 }
1173 }
1174 /* }}} */
1175
1176 /* {{{ proto void HttpRequest::unsetCookies()
1177 *
1178 */
1179 PHP_METHOD(HttpRequest, unsetCookies)
1180 {
1181 zval *opts;
1182 getObject(http_request_object, obj);
1183
1184 NO_ARGS;
1185
1186 opts = GET_PROP(obj, options);
1187 zend_hash_del(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"));
1188 }
1189 /* }}} */
1190
1191 /* {{{ proto bool HttpRequest::setURL(string url)
1192 *
1193 * Set the request URL.
1194 */
1195 PHP_METHOD(HttpRequest, setURL)
1196 {
1197 char *URL = NULL;
1198 int URL_len;
1199 getObject(http_request_object, obj);
1200
1201 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
1202 RETURN_FALSE;
1203 }
1204
1205 UPD_PROP(obj, string, url, URL);
1206 RETURN_TRUE;
1207 }
1208 /* }}} */
1209
1210 /* {{{ proto string HttpRequest::getUrl()
1211 *
1212 * Get the previously set request URL.
1213 */
1214 PHP_METHOD(HttpRequest, getURL)
1215 {
1216 zval *URL;
1217 getObject(http_request_object, obj);
1218
1219 NO_ARGS;
1220
1221 URL = GET_PROP(obj, url);
1222 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
1223 }
1224 /* }}} */
1225
1226 /* {{{ proto bool HttpRequest::setMethod(long request_method)
1227 *
1228 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
1229 * <tt>HTTP_POST</tt> constants.
1230 */
1231 PHP_METHOD(HttpRequest, setMethod)
1232 {
1233 long meth;
1234 getObject(http_request_object, obj);
1235
1236 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
1237 RETURN_FALSE;
1238 }
1239
1240 UPD_PROP(obj, long, method, meth);
1241 RETURN_TRUE;
1242 }
1243 /* }}} */
1244
1245 /* {{{ proto long HttpRequest::getMethod()
1246 *
1247 * Get the previously set request method.
1248 */
1249 PHP_METHOD(HttpRequest, getMethod)
1250 {
1251 zval *meth;
1252 getObject(http_request_object, obj);
1253
1254 NO_ARGS;
1255
1256 meth = GET_PROP(obj, method);
1257 RETURN_LONG(Z_LVAL_P(meth));
1258 }
1259 /* }}} */
1260
1261 /* {{{ proto bool HttpRequest::setContentType(string content_type)
1262 *
1263 * Set the content type the post request should have.
1264 * Use this only if you know what you're doing.
1265 */
1266 PHP_METHOD(HttpRequest, setContentType)
1267 {
1268 char *ctype;
1269 int ct_len;
1270 getObject(http_request_object, obj);
1271
1272 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
1273 RETURN_FALSE;
1274 }
1275
1276 if (!strchr(ctype, '/')) {
1277 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
1278 RETURN_FALSE;
1279 }
1280
1281 UPD_PROP(obj, string, contentType, ctype);
1282 RETURN_TRUE;
1283 }
1284 /* }}} */
1285
1286 /* {{{ proto string HttpRequest::getContentType()
1287 *
1288 * Get the previously content type.
1289 */
1290 PHP_METHOD(HttpRequest, getContentType)
1291 {
1292 zval *ctype;
1293 getObject(http_request_object, obj);
1294
1295 NO_ARGS;
1296
1297 ctype = GET_PROP(obj, contentType);
1298 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
1299 }
1300 /* }}} */
1301
1302 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
1303 *
1304 * Set the URL query parameters to use.
1305 * Overwrites previously set query parameters.
1306 * Affects any request types.
1307 */
1308 PHP_METHOD(HttpRequest, setQueryData)
1309 {
1310 zval *qdata;
1311 char *query_data = NULL;
1312 getObject(http_request_object, obj);
1313
1314 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
1315 RETURN_FALSE;
1316 }
1317
1318 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
1319 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
1320 RETURN_FALSE;
1321 }
1322 UPD_PROP(obj, string, queryData, query_data);
1323 efree(query_data);
1324 RETURN_TRUE;
1325 }
1326
1327 convert_to_string(qdata);
1328 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
1329 RETURN_TRUE;
1330 }
1331 /* }}} */
1332
1333 /* {{{ proto string HttpRequest::getQueryData()
1334 *
1335 * Get the current query data in form of an urlencoded query string.
1336 */
1337 PHP_METHOD(HttpRequest, getQueryData)
1338 {
1339 zval *qdata;
1340 getObject(http_request_object, obj);
1341
1342 NO_ARGS;
1343
1344 qdata = GET_PROP(obj, queryData);
1345 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
1346 }
1347 /* }}} */
1348
1349 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
1350 *
1351 * Add parameters to the query parameter list.
1352 * Affects any request type.
1353 */
1354 PHP_METHOD(HttpRequest, addQueryData)
1355 {
1356 zval *qdata, *old_qdata;
1357 char *query_data = NULL;
1358 getObject(http_request_object, obj);
1359
1360 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1361 RETURN_FALSE;
1362 }
1363
1364 old_qdata = GET_PROP(obj, queryData);
1365
1366 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
1367 RETURN_FALSE;
1368 }
1369
1370 UPD_PROP(obj, string, queryData, query_data);
1371 efree(query_data);
1372
1373 RETURN_TRUE;
1374 }
1375 /* }}} */
1376
1377 /* {{{ proto void HttpRequest::unsetQueryData()
1378 *
1379 * Clean the query parameters.
1380 * Affects any request type.
1381 */
1382 PHP_METHOD(HttpRequest, unsetQueryData)
1383 {
1384 getObject(http_request_object, obj);
1385
1386 NO_ARGS;
1387
1388 UPD_PROP(obj, string, queryData, "");
1389 }
1390 /* }}} */
1391
1392 /* {{{ proto bool HttpRequest::addPostData(array post_data)
1393 *
1394 * Adds POST data entries.
1395 * Affects only POST requests.
1396 */
1397 PHP_METHOD(HttpRequest, addPostData)
1398 {
1399 zval *post, *post_data;
1400 getObject(http_request_object, obj);
1401
1402 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1403 RETURN_FALSE;
1404 }
1405
1406 post = GET_PROP(obj, postData);
1407 array_merge(post_data, post);
1408
1409 RETURN_TRUE;
1410 }
1411 /* }}} */
1412
1413 /* {{{ proto bool HttpRequest::setPostData(array post_data)
1414 *
1415 * Set the POST data entries.
1416 * Overwrites previously set POST data.
1417 * Affects only POST requests.
1418 */
1419 PHP_METHOD(HttpRequest, setPostData)
1420 {
1421 zval *post, *post_data;
1422 getObject(http_request_object, obj);
1423
1424 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
1425 RETURN_FALSE;
1426 }
1427
1428 post = GET_PROP(obj, postData);
1429 zend_hash_clean(Z_ARRVAL_P(post));
1430 array_copy(post_data, post);
1431
1432 RETURN_TRUE;
1433 }
1434 /* }}}*/
1435
1436 /* {{{ proto array HttpRequest::getPostData()
1437 *
1438 * Get previously set POST data.
1439 */
1440 PHP_METHOD(HttpRequest, getPostData)
1441 {
1442 zval *post_data;
1443 getObject(http_request_object, obj);
1444
1445 NO_ARGS;
1446
1447 post_data = GET_PROP(obj, postData);
1448 array_init(return_value);
1449 array_copy(post_data, return_value);
1450 }
1451 /* }}} */
1452
1453 /* {{{ proto void HttpRequest::unsetPostData()
1454 *
1455 * Clean POST data entires.
1456 * Affects only POST requests.
1457 */
1458 PHP_METHOD(HttpRequest, unsetPostData)
1459 {
1460 zval *post_data;
1461 getObject(http_request_object, obj);
1462
1463 NO_ARGS;
1464
1465 post_data = GET_PROP(obj, postData);
1466 zend_hash_clean(Z_ARRVAL_P(post_data));
1467 }
1468 /* }}} */
1469
1470 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
1471 *
1472 * Add a file to the POST request.
1473 * Affects only POST requests.
1474 */
1475 PHP_METHOD(HttpRequest, addPostFile)
1476 {
1477 zval *files, *entry;
1478 char *name, *file, *type = NULL;
1479 int name_len, file_len, type_len = 0;
1480 getObject(http_request_object, obj);
1481
1482 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
1483 RETURN_FALSE;
1484 }
1485
1486 if (type_len) {
1487 if (!strchr(type, '/')) {
1488 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
1489 RETURN_FALSE;
1490 }
1491 } else {
1492 type = "application/x-octetstream";
1493 type_len = sizeof("application/x-octetstream") - 1;
1494 }
1495
1496 MAKE_STD_ZVAL(entry);
1497 array_init(entry);
1498
1499 add_assoc_stringl(entry, "name", name, name_len, 1);
1500 add_assoc_stringl(entry, "type", type, type_len, 1);
1501 add_assoc_stringl(entry, "file", file, file_len, 1);
1502
1503 files = GET_PROP(obj, postFiles);
1504 add_next_index_zval(files, entry);
1505
1506 RETURN_TRUE;
1507 }
1508 /* }}} */
1509
1510 /* {{{ proto array HttpRequest::getPostFiles()
1511 *
1512 * Get all previously added POST files.
1513 */
1514 PHP_METHOD(HttpRequest, getPostFiles)
1515 {
1516 zval *files;
1517 getObject(http_request_object, obj);
1518
1519 NO_ARGS;
1520
1521 files = GET_PROP(obj, postFiles);
1522
1523 array_init(return_value);
1524 array_copy(files, return_value);
1525 }
1526 /* }}} */
1527
1528 /* {{{ proto void HttpRequest::unsetPostFiles()
1529 *
1530 * Unset the POST files list.
1531 * Affects only POST requests.
1532 */
1533 PHP_METHOD(HttpRequest, unsetPostFiles)
1534 {
1535 zval *files;
1536 getObject(http_request_object, obj);
1537
1538 NO_ARGS;
1539
1540 files = GET_PROP(obj, postFiles);
1541 zend_hash_clean(Z_ARRVAL_P(files));
1542 }
1543 /* }}} */
1544
1545 /* {{{ proto array HttpRequest::getResponseData()
1546 *
1547 * Get all response data after the request has been sent.
1548 */
1549 PHP_METHOD(HttpRequest, getResponseData)
1550 {
1551 zval *data;
1552 getObject(http_request_object, obj);
1553
1554 NO_ARGS;
1555
1556 data = GET_PROP(obj, responseData);
1557 array_init(return_value);
1558 array_copy(data, return_value);
1559 }
1560 /* }}} */
1561
1562 /* {{{ proto mixed HttpRequest::getResponseHeader([string name])
1563 *
1564 * Get response header(s) after the request has been sent.
1565 */
1566 PHP_METHOD(HttpRequest, getResponseHeader)
1567 {
1568 zval *data, **headers, **header;
1569 char *header_name = NULL;
1570 int header_len = 0;
1571 getObject(http_response_object, obj);
1572
1573 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1574 RETURN_FALSE;
1575 }
1576
1577 data = GET_PROP(obj, responseData);
1578 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1579 RETURN_FALSE;
1580 }
1581
1582 if (!header_len || !header_name) {
1583 array_init(return_value);
1584 array_copy(*headers, return_value);
1585 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1586 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1587 } else {
1588 RETURN_FALSE;
1589 }
1590 }
1591 /* }}} */
1592
1593 /* {{{ proto array HttpRequest::getResponseCookie([string name])
1594 *
1595 * Get response cookie(s) after the request has been sent.
1596 */
1597 PHP_METHOD(HttpRequest, getResponseCookie)
1598 {
1599 zval *data, **headers;
1600 char *cookie_name = NULL;
1601 int cookie_len = 0;
1602 getObject(http_request_object, obj);
1603
1604 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &cookie_name, &cookie_len)) {
1605 RETURN_FALSE;
1606 }
1607
1608 array_init(return_value);
1609
1610 data = GET_PROP(obj, responseData);
1611 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1612 ulong idx = 0;
1613 char *key = NULL;
1614 zval **header = NULL;
1615
1616 FOREACH_HASH_KEYVAL(Z_ARRVAL_PP(headers), key, idx, header) {
1617 if (key && !strcasecmp(key, "Set-Cookie")) {
1618 /* several cookies? */
1619 if (Z_TYPE_PP(header) == IS_ARRAY) {
1620 zval **cookie;
1621
1622 FOREACH_HASH_VAL(Z_ARRVAL_PP(header), cookie) {
1623 zval *cookie_hash;
1624 MAKE_STD_ZVAL(cookie_hash);
1625 array_init(cookie_hash);
1626
1627 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(cookie), Z_ARRVAL_P(cookie_hash))) {
1628 if (!cookie_len) {
1629 add_next_index_zval(return_value, cookie_hash);
1630 } else {
1631 zval **name;
1632
1633 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1634 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1635 add_next_index_zval(return_value, cookie_hash);
1636 return; /* <<< FOUND >>> */
1637 } else {
1638 zval_dtor(cookie_hash);
1639 efree(cookie_hash);
1640 }
1641 }
1642 } else {
1643 zval_dtor(cookie_hash);
1644 efree(cookie_hash);
1645 }
1646 }
1647 } else {
1648 zval *cookie_hash;
1649 MAKE_STD_ZVAL(cookie_hash);
1650 array_init(cookie_hash);
1651
1652 if (SUCCESS == http_parse_cookie(Z_STRVAL_PP(header), Z_ARRVAL_P(cookie_hash))) {
1653 if (!cookie_len) {
1654 add_next_index_zval(return_value, cookie_hash);
1655 } else {
1656 zval **name;
1657
1658 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(cookie_hash), "name", sizeof("name"), (void **) &name)) &&
1659 (!strcmp(Z_STRVAL_PP(name), cookie_name))) {
1660 add_next_index_zval(return_value, cookie_hash);
1661 } else {
1662 zval_dtor(cookie_hash);
1663 efree(cookie_hash);
1664 }
1665 }
1666 } else {
1667 zval_dtor(cookie_hash);
1668 efree(cookie_hash);
1669 }
1670 }
1671 break;
1672 }
1673 /* reset key */
1674 key = NULL;
1675 }
1676 }
1677 }
1678 /* }}} */
1679
1680 /* {{{ proto string HttpRequest::getResponseBody()
1681 *
1682 * Get the response body after the request has been sent.
1683 */
1684 PHP_METHOD(HttpRequest, getResponseBody)
1685 {
1686 zval *data, **body;
1687 getObject(http_request_object, obj);
1688
1689 NO_ARGS;
1690
1691 data = GET_PROP(obj, responseData);
1692 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1693 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1694 } else {
1695 RETURN_FALSE;
1696 }
1697 }
1698 /* }}} */
1699
1700 /* {{{ proto int HttpRequest::getResponseCode()
1701 *
1702 * Get the response code after the request has been sent.
1703 */
1704 PHP_METHOD(HttpRequest, getResponseCode)
1705 {
1706 zval *code;
1707 getObject(http_request_object, obj);
1708
1709 NO_ARGS;
1710
1711 code = GET_PROP(obj, responseCode);
1712 RETURN_LONG(Z_LVAL_P(code));
1713 }
1714 /* }}} */
1715
1716 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1717 *
1718 * Get response info after the request has been sent.
1719 * See http_get() for a full list of returned info.
1720 */
1721 PHP_METHOD(HttpRequest, getResponseInfo)
1722 {
1723 zval *info, **infop;
1724 char *info_name = NULL;
1725 int info_len = 0;
1726 getObject(http_request_object, obj);
1727
1728 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1729 RETURN_FALSE;
1730 }
1731
1732 info = GET_PROP(obj, responseInfo);
1733
1734 if (info_len && info_name) {
1735 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1736 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1737 } else {
1738 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Could not find response info named %s", info_name);
1739 RETURN_FALSE;
1740 }
1741 } else {
1742 array_init(return_value);
1743 array_copy(info, return_value);
1744 }
1745 }
1746 /* }}}*/
1747
1748 /* {{{ proto HttpMessage HttpRequest::getResponseMessage()
1749 *
1750 * Get the full response as HttpMessage object.
1751 */
1752 PHP_METHOD(HttpRequest, getResponseMessage)
1753 {
1754 zval *message;
1755 getObject(http_request_object, obj);
1756
1757 NO_ARGS;
1758
1759 message = GET_PROP(obj, responseMessage);
1760 Z_TYPE_P(return_value) = IS_OBJECT;
1761 return_value->is_ref = 1;
1762 return_value->value.obj = message->value.obj;
1763 zval_add_ref(&return_value);
1764 }
1765
1766 /* {{{ proto bool HttpRequest::send()
1767 *
1768 * Send the HTTP request.
1769 *
1770 * GET example:
1771 * <pre>
1772 * <?php
1773 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1774 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1775 * $r->addQueryData(array('category' => 3));
1776 * try {
1777 * $r->send();
1778 * if ($r->getResponseCode() == 200) {
1779 * file_put_contents('local.rss', $r->getResponseBody());
1780 * }
1781 * } catch (HttpException $ex) {
1782 * echo $ex;
1783 * }
1784 * ?>
1785 * </pre>
1786 *
1787 * POST example:
1788 * <pre>
1789 * <?php
1790 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1791 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1792 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1793 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1794 * if ($r->send()) {
1795 * echo $r->getResponseBody();
1796 * }
1797 * ?>
1798 * </pre>
1799 */
1800 PHP_METHOD(HttpRequest, send)
1801 {
1802 STATUS status = FAILURE;
1803 zval *meth, *URL, *qdata, *opts, *info, *resp;
1804 char *request_uri;
1805 getObject(http_request_object, obj);
1806
1807 NO_ARGS;
1808
1809 SET_EH_THROW_HTTP();
1810
1811 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1812 http_error(E_WARNING, HTTP_E_CURL, "Could not initilaize curl");
1813 RETURN_FALSE;
1814 }
1815
1816 meth = GET_PROP(obj, method);
1817 URL = GET_PROP(obj, url);
1818 qdata = GET_PROP(obj, queryData);
1819 opts = GET_PROP(obj, options);
1820 info = GET_PROP(obj, responseInfo);
1821 resp = GET_PROP(obj, responseData);
1822
1823 // HTTP_URI_MAXLEN+1 long char *
1824 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1825
1826 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1827 if (!strchr(request_uri, '?')) {
1828 strcat(request_uri, "?");
1829 } else {
1830 strcat(request_uri, "&");
1831 }
1832 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1833 }
1834
1835 switch (Z_LVAL_P(meth))
1836 {
1837 case HTTP_GET:
1838 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1839 break;
1840
1841 case HTTP_HEAD:
1842 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1843 break;
1844
1845 case HTTP_POST:
1846 {
1847 zval *post_files, *post_data;
1848
1849 post_files = GET_PROP(obj, postFiles);
1850 post_data = GET_PROP(obj, postData);
1851
1852 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
1853
1854 /* urlencoded post */
1855 status = http_post_array_ex(obj->ch, request_uri, Z_ARRVAL_P(post_data), Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1856
1857 } else {
1858
1859 /*
1860 * multipart post
1861 */
1862 char *key = NULL;
1863 long idx;
1864 zval **data;
1865 struct curl_httppost *http_post_data[2] = {NULL, NULL};
1866
1867 /* normal data */
1868 FOREACH_KEYVAL(post_data, key, idx, data) {
1869 if (key) {
1870 convert_to_string_ex(data);
1871 curl_formadd(&http_post_data[0], &http_post_data[1],
1872 CURLFORM_COPYNAME, key,
1873 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
1874 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
1875 CURLFORM_END
1876 );
1877
1878 /* reset */
1879 key = NULL;
1880 }
1881 }
1882
1883 /* file data */
1884 FOREACH_VAL(post_files, data) {
1885 zval **file, **type, **name;
1886
1887 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
1888 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
1889 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
1890
1891 curl_formadd(&http_post_data[0], &http_post_data[1],
1892 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
1893 CURLFORM_FILE, Z_STRVAL_PP(file),
1894 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
1895 CURLFORM_END
1896 );
1897 }
1898 }
1899
1900 status = http_post_curldata_ex(obj->ch, request_uri, http_post_data[0], Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &obj->response);
1901 curl_formfree(http_post_data[0]);
1902 }
1903 }
1904 break;
1905
1906 default:
1907 break;
1908 }
1909
1910 efree(request_uri);
1911
1912 /* final data handling */
1913 if (status == SUCCESS) {
1914 http_message *msg;
1915
1916 if (msg = http_message_parse(PHPSTR_VAL(&obj->response), PHPSTR_LEN(&obj->response))) {
1917 zval *headers, *message;
1918 char *body;
1919 size_t body_len;
1920
1921 UPD_PROP(obj, long, responseCode, msg->info.response.code);
1922
1923 MAKE_STD_ZVAL(headers)
1924 array_init(headers);
1925
1926 zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1927 phpstr_data(PHPSTR(msg), &body, &body_len);
1928
1929 add_assoc_zval(resp, "headers", headers);
1930 add_assoc_stringl(resp, "body", body, body_len, 0);
1931
1932 message = GET_PROP(obj, responseMessage);
1933 zval_dtor(message);
1934 Z_TYPE_P(message) = IS_OBJECT;
1935 message->value.obj = http_message_object_from_msg(msg);
1936 SET_PROP(obj, responseMessage, message);
1937 } else {
1938 status = FAILURE;
1939 }
1940 }
1941
1942 SET_EH_NORMAL();
1943 RETURN_SUCCESS(status);
1944 }
1945 /* }}} */
1946 /* }}} */
1947 #endif /* HTTP_HAVE_CURL */
1948
1949 #endif /* ZEND_ENGINE_2 */
1950
1951 /*
1952 * Local variables:
1953 * tab-width: 4
1954 * c-basic-offset: 4
1955 * End:
1956 * vim600: noet sw=4 ts=4 fdm=marker
1957 * vim<600: noet sw=4 ts=4
1958 */
1959