* that should do the trick
[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_api.h"
25 #include "php_http_curl_api.h"
26 #include "php_http_std_defs.h"
27
28 #ifdef ZEND_ENGINE_2
29
30 /* {{{ HttpResponse */
31
32 /* {{{ proto void HttpResponse::__construct(bool cache, bool gzip)
33 *
34 * Instantiates a new HttpResponse object, which can be used to send
35 * any data/resource/file to an HTTP client with caching and multiple
36 * ranges/resuming support.
37 *
38 * NOTE: GZIPping is not implemented yet.
39 */
40 PHP_METHOD(HttpResponse, __construct)
41 {
42 zend_bool do_cache = 0, do_gzip = 0;
43 getObject(http_response_object, obj);
44
45 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
46 // throw exception
47 return;
48 }
49
50 UPD_PROP(obj, long, cache, do_cache);
51 UPD_PROP(obj, long, gzip, do_gzip);
52 }
53 /* }}} */
54
55 /* {{{ proto bool HttpResponse::setCache(bool cache)
56 *
57 * Whether it sould be attempted to cache the entitity.
58 * This will result in necessary caching headers and checks of clients
59 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
60 * matches a "304 Not Modified" status code will be issued.
61 *
62 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
63 * to something more appropriate than "no-cache"!
64 */
65 PHP_METHOD(HttpResponse, setCache)
66 {
67 zend_bool do_cache = 0;
68 getObject(http_response_object, obj);
69
70 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
71 RETURN_FALSE;
72 }
73
74 UPD_PROP(obj, long, cache, do_cache);
75 RETURN_TRUE;
76 }
77 /* }}} */
78
79 /* {{{ proto bool HttpResponse::getCache()
80 *
81 * Get current caching setting.
82 */
83 PHP_METHOD(HttpResponse, getCache)
84 {
85 zval *do_cache = NULL;
86 getObject(http_response_object, obj);
87
88 NO_ARGS;
89
90 do_cache = GET_PROP(obj, cache);
91 RETURN_BOOL(Z_LVAL_P(do_cache));
92 }
93 /* }}}*/
94
95 /* {{{ proto bool HttpResponse::setGzip(bool gzip)
96 *
97 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
98 */
99 PHP_METHOD(HttpResponse, setGzip)
100 {
101 zend_bool do_gzip = 0;
102 getObject(http_response_object, obj);
103
104 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
105 RETURN_FALSE;
106 }
107
108 UPD_PROP(obj, long, gzip, do_gzip);
109 RETURN_TRUE;
110 }
111 /* }}} */
112
113 /* {{{ proto bool HttpResponse::getGzip()
114 *
115 * Get current gzipping setting.
116 */
117 PHP_METHOD(HttpResponse, getGzip)
118 {
119 zval *do_gzip = NULL;
120 getObject(http_response_object, obj);
121
122 NO_ARGS;
123
124 do_gzip = GET_PROP(obj, gzip);
125 RETURN_BOOL(Z_LVAL_P(do_gzip));
126 }
127 /* }}} */
128
129 /* {{{ proto bool HttpResponse::setCacheControl(string control[, bool raw = false])
130 *
131 * Set a custom cache-control header, usually being "private" or "public"; if
132 * $raw is set to true the header will be sent as-is.
133 */
134 PHP_METHOD(HttpResponse, setCacheControl)
135 {
136 char *ccontrol;
137 int cc_len;
138 zend_bool raw = 0;
139 getObject(http_response_object, obj);
140
141 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
142 RETURN_FALSE;
143 }
144
145 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
146 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
147 RETURN_FALSE;
148 }
149
150 UPD_PROP(obj, long, raw_cache_header, raw);
151 UPD_PROP(obj, string, cacheControl, ccontrol);
152 RETURN_TRUE;
153 }
154 /* }}} */
155
156 /* {{{ proto string HttpResponse::getCacheControl()
157 *
158 * Get current Cache-Control header setting.
159 */
160 PHP_METHOD(HttpResponse, getCacheControl)
161 {
162 zval *ccontrol;
163 getObject(http_response_object, obj);
164
165 NO_ARGS;
166
167 ccontrol = GET_PROP(obj, cacheControl);
168 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
169 }
170 /* }}} */
171
172 /* {{{ proto bool HttpResponse::setContentType(string content_type)
173 *
174 * Set the content-type of the sent entity.
175 */
176 PHP_METHOD(HttpResponse, setContentType)
177 {
178 char *ctype;
179 int ctype_len;
180 getObject(http_response_object, obj);
181
182 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
183 RETURN_FALSE;
184 }
185
186 if (!strchr(ctype, '/')) {
187 php_error_docref(NULL TSRMLS_CC, E_WARNING,
188 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
189 RETURN_FALSE;
190 }
191
192 UPD_PROP(obj, string, contentType, ctype);
193
194 RETURN_TRUE;
195 }
196 /* }}} */
197
198 /* {{{ proto string HttpResponse::getContentType()
199 *
200 * Get current Content-Type header setting.
201 */
202 PHP_METHOD(HttpResponse, getContentType)
203 {
204 zval *ctype;
205 getObject(http_response_object, obj);
206
207 NO_ARGS;
208
209 ctype = GET_PROP(obj, contentType);
210 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
211 }
212 /* }}} */
213
214 /* {{{ proto bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
215 *
216 * Set the Content-Disposition of the sent entity. This setting aims to suggest
217 * the receiveing user agent how to handle the sent entity; usually the client
218 * will show the user a "Save As..." popup.
219 */
220 PHP_METHOD(HttpResponse, setContentDisposition)
221 {
222 char *file;
223 int file_len;
224 zend_bool is_inline = 0;
225 getObject(http_response_object, obj);
226
227 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
228 RETURN_FALSE;
229 }
230
231 UPD_PROP(obj, string, dispoFile, file);
232 UPD_PROP(obj, long, dispoInline, is_inline);
233 RETURN_TRUE;
234 }
235 /* }}} */
236
237 /* {{{ proto array HttpResponse::getContentDisposition()
238 *
239 * Get current Content-Disposition setting.
240 * Will return an associative array like:
241 * <pre>
242 * array(
243 * 'filename' => 'foo.bar',
244 * 'inline' => false
245 * )
246 * </pre>
247 */
248 PHP_METHOD(HttpResponse, getContentDisposition)
249 {
250 zval *file;
251 zval *is_inline;
252 getObject(http_response_object, obj);
253
254 if (ZEND_NUM_ARGS()) {
255 WRONG_PARAM_COUNT;
256 }
257
258 file = GET_PROP(obj, dispoFile);
259 is_inline = GET_PROP(obj, dispoInline);
260
261 array_init(return_value);
262 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
263 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
264 }
265 /* }}} */
266
267 /* {{{ proto bool HttpResponse::setETag(string etag)
268 *
269 * Set a custom ETag. Use this only if you know what you're doing.
270 */
271 PHP_METHOD(HttpResponse, setETag)
272 {
273 char *etag;
274 int etag_len;
275 getObject(http_response_object, obj);
276
277 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
278 RETURN_FALSE;
279 }
280
281 UPD_PROP(obj, string, eTag, etag);
282 RETURN_TRUE;
283 }
284 /* }}} */
285
286 /* {{{ proto string HttpResponse::getETag()
287 *
288 * Get the previously set custom ETag.
289 */
290 PHP_METHOD(HttpResponse, getETag)
291 {
292 zval *etag;
293 getObject(http_response_object, obj);
294
295 NO_ARGS;
296
297 etag = GET_PROP(obj, eTag);
298 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
299 }
300 /* }}} */
301
302 /* {{{ proto bool HttpResponse::setData(string data)
303 *
304 * Set the data to be sent.
305 */
306 PHP_METHOD(HttpResponse, setData)
307 {
308 zval *the_data;
309 getObject(http_response_object, obj);
310
311 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
312 RETURN_FALSE;
313 }
314
315 convert_to_string_ex(&the_data);
316 SET_PROP(obj, data, the_data);
317 UPD_PROP(obj, long, lastModified, http_lmod(the_data, SEND_DATA));
318 UPD_PROP(obj, long, send_mode, SEND_DATA);
319 RETURN_TRUE;
320 }
321 /* }}} */
322
323 /* {{{ proto string HttpResponse::getData()
324 *
325 * Get the previously set data to be sent.
326 */
327 PHP_METHOD(HttpResponse, getData)
328 {
329 zval *the_data;
330 getObject(http_response_object, obj);
331
332 NO_ARGS;
333
334 the_data = GET_PROP(obj, data);
335 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
336 }
337 /* }}} */
338
339 /* {{{ proto bool HttpResponse::setStream(resource stream)
340 *
341 * Set the resource to be sent.
342 */
343 PHP_METHOD(HttpResponse, setStream)
344 {
345 zval *the_stream;
346 php_stream *the_real_stream;
347 getObject(http_response_object, obj);
348
349 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
350 RETURN_FALSE;
351 }
352
353 php_stream_from_zval(the_real_stream, &the_stream);
354
355 SET_PROP(obj, stream, the_stream);
356 UPD_PROP(obj, long, lastModified, http_lmod(the_real_stream, SEND_RSRC));
357 UPD_PROP(obj, long, send_mode, SEND_RSRC);
358 RETURN_TRUE;
359 }
360 /* }}} */
361
362 /* {{{ proto resource HttpResponse::getStream()
363 *
364 * Get the previously set resource to be sent.
365 */
366 PHP_METHOD(HttpResponse, getStream)
367 {
368 zval *the_stream;
369 getObject(http_response_object, obj);
370
371 NO_ARGS;
372
373 the_stream = GET_PROP(obj, stream);
374 RETURN_RESOURCE(Z_LVAL_P(the_stream));
375 }
376 /* }}} */
377
378 /* {{{ proto bool HttpResponse::setFile(string file)
379 *
380 * Set the file to be sent.
381 */
382 PHP_METHOD(HttpResponse, setFile)
383 {
384 zval *the_file;
385 getObject(http_response_object, obj);
386
387 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
388 RETURN_FALSE;
389 }
390
391 convert_to_string_ex(&the_file);
392
393 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
394 UPD_PROP(obj, long, lastModified, http_lmod(the_file, -1));
395 UPD_PROP(obj, long, send_mode, -1);
396 RETURN_TRUE;
397 }
398 /* }}} */
399
400 /* {{{ proto string HttpResponse::getFile()
401 *
402 * Get the previously set file to be sent.
403 */
404 PHP_METHOD(HttpResponse, getFile)
405 {
406 zval *the_file;
407 getObject(http_response_object, obj);
408
409 NO_ARGS;
410
411 the_file = GET_PROP(obj, file);
412 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
413 }
414 /* }}} */
415
416 /* {{{ proto bool HttpResponse::send()
417 *
418 * Finally send the entity.
419 *
420 * Example:
421 * <pre>
422 * <?php
423 * $r = new HttpResponse(true);
424 * $r->setFile('../hidden/contract.pdf');
425 * $r->setContentType('application/pdf');
426 * $r->send();
427 * ?>
428 * </pre>
429 *
430 */
431 PHP_METHOD(HttpResponse, send)
432 {
433 zval *do_cache, *do_gzip;
434 getObject(http_response_object, obj);
435
436 NO_ARGS;
437
438 do_cache = GET_PROP(obj, cache);
439 do_gzip = GET_PROP(obj, gzip);
440
441 /* caching */
442 if (Z_LVAL_P(do_cache)) {
443 zval *cctrl, *etag, *lmod, *ccraw;
444
445 etag = GET_PROP(obj, eTag);
446 lmod = GET_PROP(obj, lastModified);
447 cctrl = GET_PROP(obj, cacheControl);
448 ccraw = GET_PROP(obj, raw_cache_header);
449
450 if (Z_LVAL_P(ccraw)) {
451 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
452 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));
453 } else {
454 char cc_header[42] = {0};
455 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
456 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
457 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
458 }
459 }
460
461 /* gzip */
462 if (Z_LVAL_P(do_gzip)) {
463 /* ... */
464 }
465
466 /* content type */
467 {
468 zval *ctype = GET_PROP(obj, contentType);
469 if (Z_STRLEN_P(ctype)) {
470 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
471 } else {
472 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
473 }
474 }
475
476 /* content disposition */
477 {
478 zval *dispo_file = GET_PROP(obj, dispoFile);
479 if (Z_STRLEN_P(dispo_file)) {
480 zval *dispo_inline = GET_PROP(obj, dispoInline);
481 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), (zend_bool) Z_LVAL_P(dispo_inline));
482 }
483 }
484
485 /* send */
486 {
487 zval *send_mode = GET_PROP(obj, send_mode);
488 switch (Z_LVAL_P(send_mode))
489 {
490 case SEND_DATA:
491 {
492 zval *zdata = GET_PROP(obj, data);
493 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
494 }
495
496 case SEND_RSRC:
497 {
498 php_stream *the_real_stream;
499 zval *the_stream = GET_PROP(obj, stream);
500 php_stream_from_zval(the_real_stream, &the_stream);
501 RETURN_SUCCESS(http_send_stream(the_real_stream));
502 }
503
504 default:
505 {
506 zval *zfile = GET_PROP(obj, file);
507 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(zfile)));
508 }
509 }
510 }
511 }
512 /* }}} */
513 /* }}} */
514
515 #ifdef HTTP_HAVE_CURL
516 /* {{{ HttpRequest */
517
518 /* {{{ proto void HttpRequest::__construct([string url[, long request_method = HTTP_GET]])
519 *
520 * Instantiate a new HttpRequest object which can be used to issue HEAD, GET
521 * and POST (including posting files) HTTP requests.
522 */
523 PHP_METHOD(HttpRequest, __construct)
524 {
525 char *URL = NULL;
526 int URL_len;
527 long meth = -1;
528 getObject(http_request_object, obj);
529
530 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
531 return;
532 }
533
534 INIT_PARR(obj, options);
535 INIT_PARR(obj, responseInfo);
536 INIT_PARR(obj, responseData);
537 INIT_PARR(obj, postData);
538 INIT_PARR(obj, postFiles);
539
540 if (URL) {
541 UPD_PROP(obj, string, url, URL);
542 }
543 if (meth > -1) {
544 UPD_PROP(obj, long, method, meth);
545 }
546 }
547 /* }}} */
548
549 /* {{{ proto void HttpRequest::__destruct()
550 *
551 * Destroys the HttpRequest object.
552 */
553 PHP_METHOD(HttpRequest, __destruct)
554 {
555 getObject(http_request_object, obj);
556
557 NO_ARGS;
558
559 FREE_PARR(obj, options);
560 FREE_PARR(obj, responseInfo);
561 FREE_PARR(obj, responseData);
562 FREE_PARR(obj, postData);
563 FREE_PARR(obj, postFiles);
564 }
565 /* }}} */
566
567 /* {{{ proto bool HttpRequest::setOptions(array options)
568 *
569 * Set the request options to use. See http_get() for a full list of available options.
570 */
571 PHP_METHOD(HttpRequest, setOptions)
572 {
573 char *key = NULL;
574 long idx = 0;
575 zval *opts, *old_opts, **opt;
576 getObject(http_request_object, obj);
577
578 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &opts)) {
579 RETURN_FALSE;
580 }
581
582 old_opts = GET_PROP(obj, options);
583
584 /* headers and cookies need extra attention -- thus cannot use array_merge() directly */
585 FOREACH_KEYVAL(opts, key, idx, opt) {
586 if (key) {
587 if (!strcmp(key, "headers")) {
588 zval **headers;
589 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "headers", sizeof("headers"), (void **) &headers)) {
590 array_merge(*opt, *headers);
591 continue;
592 }
593 } else if (!strcmp(key, "cookies")) {
594 zval **cookies;
595 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(old_opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
596 array_merge(*opt, *cookies);
597 continue;
598 }
599 }
600 zval_add_ref(opt);
601 add_assoc_zval(old_opts, key, *opt);
602
603 /* reset */
604 key = NULL;
605 }
606 }
607
608 RETURN_TRUE;
609 }
610 /* }}} */
611
612 /* {{{ proto array HttpRequest::getOptions()
613 *
614 * Get current set options.
615 */
616 PHP_METHOD(HttpRequest, getOptions)
617 {
618 zval *opts;
619 getObject(http_request_object, obj);
620
621 NO_ARGS;
622
623 opts = GET_PROP(obj, options);
624 array_init(return_value);
625 array_copy(opts, return_value);
626 }
627 /* }}} */
628
629 /* {{{ proto void HttpRequest::unsetOptions()
630 *
631 * Unset all options/headers/cookies.
632 */
633 PHP_METHOD(HttpRequest, unsetOptions)
634 {
635 getObject(http_request_object, obj);
636
637 NO_ARGS;
638
639 FREE_PARR(obj, options);
640 INIT_PARR(obj, options);
641 }
642 /* }}} */
643
644 /* {{{ proto bool HttpRequest::addHeader(array header)
645 *
646 * Add (a) request header name/value pair(s).
647 */
648 PHP_METHOD(HttpRequest, addHeader)
649 {
650 zval *opts, **headers, *new_headers;
651 getObject(http_request_object, obj);
652
653 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_headers)) {
654 RETURN_FALSE;
655 }
656
657 opts = GET_PROP(obj, options);
658
659 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) {
660 array_merge(new_headers, *headers);
661 } else {
662 zval_add_ref(&new_headers);
663 add_assoc_zval(opts, "headers", new_headers);
664 }
665
666 RETURN_TRUE;
667 }
668 /* }}} */
669
670 /* {{{ proto bool HttpRequest::addCookie(array cookie)
671 *
672 * Add (a) cookie(s).
673 */
674 PHP_METHOD(HttpRequest, addCookie)
675 {
676 zval *opts, **cookies, *new_cookies;
677 getObject(http_request_object, obj);
678
679 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/", &new_cookies)) {
680 RETURN_FALSE;
681 }
682
683 opts = GET_PROP(obj, options);
684
685 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "cookies", sizeof("cookies"), (void **) &cookies)) {
686 array_merge(new_cookies, *cookies);
687 } else {
688 zval_add_ref(&new_cookies);
689 add_assoc_zval(opts, "cookies", new_cookies);
690 }
691
692 RETURN_TRUE;
693 }
694 /* }}} */
695
696 /* {{{ proto bool HttpRequest::setURL(string url)
697 *
698 * Set the request URL.
699 */
700 PHP_METHOD(HttpRequest, setURL)
701 {
702 char *URL = NULL;
703 int URL_len;
704 getObject(http_request_object, obj);
705
706 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
707 RETURN_FALSE;
708 }
709
710 UPD_PROP(obj, string, url, URL);
711 RETURN_TRUE;
712 }
713 /* }}} */
714
715 /* {{{ proto string HttpRequest::getUrl()
716 *
717 * Get the previously set request URL.
718 */
719 PHP_METHOD(HttpRequest, getURL)
720 {
721 zval *URL;
722 getObject(http_request_object, obj);
723
724 NO_ARGS;
725
726 URL = GET_PROP(obj, url);
727 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
728 }
729 /* }}} */
730
731 /* {{{ proto bool HttpRequest::setMethod(long request_method)
732 *
733 * Set the request methods; one of the <tt>HTTP_HEAD</tt>, <tt>HTTP_GET</tt> or
734 * <tt>HTTP_POST</tt> constants.
735 */
736 PHP_METHOD(HttpRequest, setMethod)
737 {
738 long meth;
739 getObject(http_request_object, obj);
740
741 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
742 RETURN_FALSE;
743 }
744
745 UPD_PROP(obj, long, method, meth);
746 RETURN_TRUE;
747 }
748 /* }}} */
749
750 /* {{{ proto long HttpRequest::getMethod()
751 *
752 * Get the previously set request method.
753 */
754 PHP_METHOD(HttpRequest, getMethod)
755 {
756 zval *meth;
757 getObject(http_request_object, obj);
758
759 NO_ARGS;
760
761 meth = GET_PROP(obj, method);
762 RETURN_LONG(Z_LVAL_P(meth));
763 }
764 /* }}} */
765
766 /* {{{ proto bool HttpRequest::setContentType(string content_type)
767 *
768 * Set the content type the post request should have.
769 * Use this only if you know what you're doing.
770 */
771 PHP_METHOD(HttpRequest, setContentType)
772 {
773 char *ctype;
774 int ct_len;
775 getObject(http_request_object, obj);
776
777 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
778 RETURN_FALSE;
779 }
780
781 if (!strchr(ctype, '/')) {
782 php_error_docref(NULL TSRMLS_CC, E_WARNING,
783 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
784 ctype);
785 RETURN_FALSE;
786 }
787
788 UPD_PROP(obj, string, contentType, ctype);
789 RETURN_TRUE;
790 }
791 /* }}} */
792
793 /* {{{ proto string HttpRequest::getContentType()
794 *
795 * Get the previously content type.
796 */
797 PHP_METHOD(HttpRequest, getContentType)
798 {
799 zval *ctype;
800 getObject(http_request_object, obj);
801
802 NO_ARGS;
803
804 ctype = GET_PROP(obj, contentType);
805 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
806 }
807 /* }}} */
808
809 /* {{{ proto bool HttpRequest::setQueryData(mixed query_data)
810 *
811 * Set the URL query parameters to use.
812 * Overwrites previously set query parameters.
813 * Affects any request types.
814 */
815 PHP_METHOD(HttpRequest, setQueryData)
816 {
817 zval *qdata;
818 char *query_data = NULL;
819 getObject(http_request_object, obj);
820
821 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
822 RETURN_FALSE;
823 }
824
825 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
826 if (SUCCESS != http_urlencode_hash(HASH_OF(qdata), &query_data)) {
827 RETURN_FALSE;
828 }
829 UPD_PROP(obj, string, queryData, query_data);
830 efree(query_data);
831 RETURN_TRUE;
832 }
833
834 convert_to_string(qdata);
835 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
836 RETURN_TRUE;
837 }
838 /* }}} */
839
840 /* {{{ proto string HttpRequest::getQueryData()
841 *
842 * Get the current query data in form of an urlencoded query string.
843 */
844 PHP_METHOD(HttpRequest, getQueryData)
845 {
846 zval *qdata;
847 getObject(http_request_object, obj);
848
849 NO_ARGS;
850
851 qdata = GET_PROP(obj, queryData);
852 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
853 }
854 /* }}} */
855
856 /* {{{ proto bool HttpRequest::addQueryData(array query_params)
857 *
858 * Add parameters to the query parameter list.
859 * Affects any request type.
860 */
861 PHP_METHOD(HttpRequest, addQueryData)
862 {
863 zval *qdata, *old_qdata;
864 char *query_data = NULL;
865 getObject(http_request_object, obj);
866
867 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
868 RETURN_FALSE;
869 }
870
871 old_qdata = GET_PROP(obj, queryData);
872
873 if (SUCCESS != http_urlencode_hash_ex(HASH_OF(qdata), 1, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata), &query_data, NULL)) {
874 RETURN_FALSE;
875 }
876
877 UPD_PROP(obj, string, queryData, query_data);
878 efree(query_data);
879
880 RETURN_TRUE;
881 }
882 /* }}} */
883
884 /* {{{ proto void HttpRequest::unsetQueryData()
885 *
886 * Clean the query parameters.
887 * Affects any request type.
888 */
889 PHP_METHOD(HttpRequest, unsetQueryData)
890 {
891 getObject(http_request_object, obj);
892
893 NO_ARGS;
894
895 UPD_PROP(obj, string, queryData, "");
896 }
897 /* }}} */
898
899 /* {{{ proto bool HttpRequest::addPostData(array post_data)
900 *
901 * Adds POST data entries.
902 * Affects only POST requests.
903 */
904 PHP_METHOD(HttpRequest, addPostData)
905 {
906 zval *post, *post_data;
907 getObject(http_request_object, obj);
908
909 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
910 RETURN_FALSE;
911 }
912
913 post = GET_PROP(obj, postData);
914 array_merge(post_data, post);
915
916 RETURN_TRUE;
917 }
918 /* }}} */
919
920 /* {{{ proto bool HttpRequest::setPostData(array post_data)
921 *
922 * Set the POST data entries.
923 * Overwrites previously set POST data.
924 * Affects only POST requests.
925 */
926 PHP_METHOD(HttpRequest, setPostData)
927 {
928 zval *post, *post_data;
929 getObject(http_request_object, obj);
930
931 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &post_data)) {
932 RETURN_FALSE;
933 }
934
935 post = GET_PROP(obj, postData);
936 zend_hash_clean(Z_ARRVAL_P(post));
937 array_copy(post_data, post);
938
939 RETURN_TRUE;
940 }
941 /* }}}*/
942
943 /* {{{ proto array HttpRequest::getPostData()
944 *
945 * Get previously set POST data.
946 */
947 PHP_METHOD(HttpRequest, getPostData)
948 {
949 zval *post_data;
950 getObject(http_request_object, obj);
951
952 NO_ARGS;
953
954 post_data = GET_PROP(obj, postData);
955 array_init(return_value);
956 array_copy(post_data, return_value);
957 }
958 /* }}} */
959
960 /* {{{ proto void HttpRequest::unsetPostData()
961 *
962 * Clean POST data entires.
963 * Affects only POST requests.
964 */
965 PHP_METHOD(HttpRequest, unsetPostData)
966 {
967 zval *post_data;
968 getObject(http_request_object, obj);
969
970 NO_ARGS;
971
972 post_data = GET_PROP(obj, postData);
973 zend_hash_clean(Z_ARRVAL_P(post_data));
974 }
975 /* }}} */
976
977 /* {{{ proto bool HttpRequest::addPostFile(string name, string file[, string content_type = "application/x-octetstream"])
978 *
979 * Add a file to the POST request.
980 * Affects only POST requests.
981 */
982 PHP_METHOD(HttpRequest, addPostFile)
983 {
984 zval *files, *entry;
985 char *name, *file, *type = NULL;
986 int name_len, file_len, type_len = 0;
987 getObject(http_request_object, obj);
988
989 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &name, &name_len, &file, &file_len, &type, &type_len)) {
990 RETURN_FALSE;
991 }
992
993 if (type_len) {
994 if (!strchr(type, '/')) {
995 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
996 RETURN_FALSE;
997 }
998 } else {
999 type = "application/x-octetstream";
1000 type_len = sizeof("application/x-octetstream") - 1;
1001 }
1002
1003 MAKE_STD_ZVAL(entry);
1004 array_init(entry);
1005
1006 add_assoc_stringl(entry, "name", name, name_len, 1);
1007 add_assoc_stringl(entry, "type", type, type_len, 1);
1008 add_assoc_stringl(entry, "file", file, file_len, 1);
1009
1010 files = GET_PROP(obj, postFiles);
1011 add_next_index_zval(files, entry);
1012
1013 RETURN_TRUE;
1014 }
1015 /* }}} */
1016
1017 /* {{{ proto array HttpRequest::getPostFiles()
1018 *
1019 * Get all previously added POST files.
1020 */
1021 PHP_METHOD(HttpRequest, getPostFiles)
1022 {
1023 zval *files;
1024 getObject(http_request_object, obj);
1025
1026 NO_ARGS;
1027
1028 files = GET_PROP(obj, postFiles);
1029
1030 array_init(return_value);
1031 array_copy(files, return_value);
1032 }
1033 /* }}} */
1034
1035 /* {{{ proto void HttpRequest::unsetPostFiles()
1036 *
1037 * Unset the POST files list.
1038 * Affects only POST requests.
1039 */
1040 PHP_METHOD(HttpRequest, unsetPostFiles)
1041 {
1042 zval *files;
1043 getObject(http_request_object, obj);
1044
1045 NO_ARGS;
1046
1047 files = GET_PROP(obj, postFiles);
1048 zend_hash_clean(Z_ARRVAL_P(files));
1049 }
1050 /* }}} */
1051
1052 /* {{{ proto array HttpRequest::getResponseData()
1053 *
1054 * Get all response data after the request has been sent.
1055 */
1056 PHP_METHOD(HttpRequest, getResponseData)
1057 {
1058 zval *data;
1059 getObject(http_request_object, obj);
1060
1061 NO_ARGS;
1062
1063 data = GET_PROP(obj, responseData);
1064 array_init(return_value);
1065 array_copy(data, return_value);
1066 }
1067 /* }}} */
1068
1069 /* {{{ proto string HttpRequest::getResponseHeader([string name])
1070 *
1071 * Get response header(s) after the request has been sent.
1072 */
1073 PHP_METHOD(HttpRequest, getResponseHeader)
1074 {
1075 zval *data, **headers, **header;
1076 char *header_name = NULL;
1077 int header_len = 0;
1078 getObject(http_response_object, obj);
1079
1080 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
1081 RETURN_FALSE;
1082 }
1083
1084 data = GET_PROP(obj, responseData);
1085 if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1086 RETURN_FALSE;
1087 }
1088
1089 if (!header_len || !header_name) {
1090 array_init(return_value);
1091 array_copy(*headers, return_value);
1092 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
1093 RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1094 } else {
1095 RETURN_FALSE;
1096 }
1097 }
1098
1099 /* {{{ proto string HttpRequest::getResponseBody()
1100 *
1101 * Get the response body after the request has been sent.
1102 */
1103 PHP_METHOD(HttpRequest, getResponseBody)
1104 {
1105 zval *data, **body;
1106 getObject(http_request_object, obj);
1107
1108 NO_ARGS;
1109
1110 data = GET_PROP(obj, responseData);
1111 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1112 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1113 } else {
1114 RETURN_FALSE;
1115 }
1116 }
1117 /* }}} */
1118
1119 /* {{{ proto int HttpRequest::getResponseCode()
1120 *
1121 * Get the response code after the request has been sent.
1122 */
1123 PHP_METHOD(HttpRequest, getResponseCode)
1124 {
1125 zval **code, **hdrs, *data;
1126 getObject(http_request_object, obj);
1127
1128 NO_ARGS;
1129
1130 data = GET_PROP(obj, responseData);
1131 if ( (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &hdrs)) &&
1132 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Status", sizeof("Status"), (void **) &code))) {
1133 RETVAL_STRINGL(Z_STRVAL_PP(code), Z_STRLEN_PP(code), 1);
1134 convert_to_long(return_value);
1135 } else {
1136 RETURN_FALSE;
1137 }
1138 }
1139 /* }}} */
1140
1141 /* {{{ proto array HttpRequest::getResponseInfo([string name])
1142 *
1143 * Get response info after the request has been sent.
1144 * See http_get() for a full list of returned info.
1145 */
1146 PHP_METHOD(HttpRequest, getResponseInfo)
1147 {
1148 zval *info, **infop;
1149 char *info_name = NULL;
1150 int info_len = 0;
1151 getObject(http_request_object, obj);
1152
1153 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
1154 RETURN_FALSE;
1155 }
1156
1157 info = GET_PROP(obj, responseInfo);
1158
1159 if (info_len && info_name) {
1160 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
1161 RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
1162 } else {
1163 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not find response info named %s", info_name);
1164 RETURN_FALSE;
1165 }
1166 } else {
1167 array_init(return_value);
1168 array_copy(info, return_value);
1169 }
1170 }
1171 /* }}}*/
1172
1173 /* {{{ proto bool HttpRequest::send()
1174 *
1175 * Send the HTTP request.
1176 *
1177 * GET example:
1178 * <pre>
1179 * <?php
1180 * $r = new HttpRequest('http://example.com/feed.rss', HTTP_GET);
1181 * $r->setOptions(array('lastmodified' => filemtime('local.rss')));
1182 * $r->addQueryData(array('category' => 3));
1183 * if ($r->send() && $r->getResponseCode() == 200) {
1184 * file_put_contents('local.rss', $r->getResponseBody());
1185 * }
1186 * ?>
1187 * </pre>
1188 *
1189 * POST example:
1190 * <pre>
1191 * <?php
1192 * $r = new HttpRequest('http://example.com/form.php', HTTP_POST);
1193 * $r->setOptions(array('cookies' => array('lang' => 'de')));
1194 * $r->addPostData(array('user' => 'mike', 'pass' => 's3c|r3t'));
1195 * $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
1196 * if ($r->send()) {
1197 * echo $r->getResponseBody();
1198 * }
1199 * ?>
1200 * </pre>
1201 */
1202 PHP_METHOD(HttpRequest, send)
1203 {
1204 STATUS status = FAILURE;
1205 zval *meth, *URL, *qdata, *opts, *info, *resp;
1206 char *response_data, *request_uri;
1207 size_t response_len;
1208 getObject(http_request_object, obj);
1209
1210 NO_ARGS;
1211
1212 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1213 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL");
1214 RETURN_FALSE;
1215 }
1216
1217 meth = GET_PROP(obj, method);
1218 URL = GET_PROP(obj, url);
1219 qdata = GET_PROP(obj, queryData);
1220 opts = GET_PROP(obj, options);
1221 info = GET_PROP(obj, responseInfo);
1222 resp = GET_PROP(obj, responseData);
1223
1224 // HTTP_URI_MAXLEN+1 long char *
1225 request_uri = http_absolute_uri_ex(Z_STRVAL_P(URL), Z_STRLEN_P(URL), NULL, 0, NULL, 0, 0);
1226
1227 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1228 if (!strchr(request_uri, '?')) {
1229 strcat(request_uri, "?");
1230 } else {
1231 strcat(request_uri, "&");
1232 }
1233 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1234 }
1235
1236 switch (Z_LVAL_P(meth))
1237 {
1238 case HTTP_GET:
1239 status = http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1240 break;
1241
1242 case HTTP_HEAD:
1243 status = http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1244 break;
1245
1246 case HTTP_POST:
1247 {
1248 zval *post_files, *post_data;
1249
1250 post_files = GET_PROP(obj, postFiles);
1251 post_data = GET_PROP(obj, postData);
1252
1253 if (!zend_hash_num_elements(Z_ARRVAL_P(post_files))) {
1254
1255 /* urlencoded post */
1256 status = http_post_array_ex(obj->ch, request_uri, Z_ARRVAL_P(post_data), Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1257
1258 } else {
1259
1260 /*
1261 * multipart post
1262 */
1263 char *key = NULL;
1264 long idx;
1265 zval **data;
1266 struct curl_httppost *http_post_data[2] = {NULL, NULL};
1267
1268 /* normal data */
1269 FOREACH_KEYVAL(post_data, key, idx, data) {
1270 if (key) {
1271 convert_to_string_ex(data);
1272 curl_formadd(&http_post_data[0], &http_post_data[1],
1273 CURLFORM_COPYNAME, key,
1274 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
1275 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
1276 CURLFORM_END
1277 );
1278
1279 /* reset */
1280 key = NULL;
1281 }
1282 }
1283
1284 /* file data */
1285 FOREACH_VAL(post_files, data) {
1286 zval **file, **type, **name;
1287
1288 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
1289 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
1290 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
1291
1292 curl_formadd(&http_post_data[0], &http_post_data[1],
1293 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
1294 CURLFORM_FILE, Z_STRVAL_PP(file),
1295 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
1296 CURLFORM_END
1297 );
1298 }
1299 }
1300
1301 status = http_post_curldata_ex(obj->ch, request_uri, http_post_data[0], Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len);
1302 curl_formfree(http_post_data[0]);
1303 }
1304 }
1305 break;
1306
1307 default:
1308 break;
1309 }
1310
1311 efree(request_uri);
1312
1313 /* final data handling */
1314 if (status != SUCCESS) {
1315 RETURN_FALSE;
1316 } else {
1317 char *body = NULL;
1318 size_t body_len = 0;
1319 zval *zheaders;
1320
1321 MAKE_STD_ZVAL(zheaders)
1322 array_init(zheaders);
1323
1324 if (SUCCESS != http_split_response_ex(response_data, response_len, Z_ARRVAL_P(zheaders), &body, &body_len)) {
1325 zval_dtor(zheaders);
1326 efree(zheaders),
1327 efree(response_data);
1328 RETURN_FALSE;
1329 }
1330
1331 add_assoc_zval(resp, "headers", zheaders);
1332 add_assoc_stringl(resp, "body", body, body_len, 0);
1333
1334 efree(response_data);
1335
1336 RETURN_TRUE;
1337 }
1338 /* */
1339 }
1340 /* }}} */
1341 /* }}} */
1342 #endif /* HTTP_HAVE_CURL */
1343
1344 #endif /* ZEND_ENGINE_2 */
1345
1346 /*
1347 * Local variables:
1348 * tab-width: 4
1349 * c-basic-offset: 4
1350 * End:
1351 * vim600: noet sw=4 ts=4 fdm=marker
1352 * vim<600: noet sw=4 ts=4
1353 */
1354