- fix ssl options
[m6w6/ext-http] / http_functions.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
18 #include "php_http.h"
19
20 #include "php_ini.h"
21 #include "ext/standard/php_string.h"
22 #include "zend_operators.h"
23
24 #ifdef HAVE_PHP_SESSION
25 # include "ext/session/php_session.h"
26 #endif
27
28 #include "php_http_api.h"
29 #include "php_http_cache_api.h"
30 #include "php_http_date_api.h"
31 #include "php_http_encoding_api.h"
32 #include "php_http_headers_api.h"
33 #include "php_http_message_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_method_api.h"
36 #include "php_http_send_api.h"
37 #include "php_http_url_api.h"
38
39 /* {{{ proto string http_date([int timestamp])
40 *
41 * Compose a valid HTTP date regarding RFC 822/1123
42 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
43 *
44 * Accepts an optional unix timestamp as parameter.
45 *
46 * Returns the HTTP date as string.
47 */
48 PHP_FUNCTION(http_date)
49 {
50 long t = -1;
51
52 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
53 RETURN_FALSE;
54 }
55
56 if (t == -1) {
57 t = (long) HTTP_GET_REQUEST_TIME();
58 }
59
60 RETURN_STRING(http_date(t), 0);
61 }
62 /* }}} */
63
64 /* {{{ proto string http_build_url(mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]])
65 *
66 * Returns the new URL as string on success or FALSE on failure.
67 */
68 PHP_FUNCTION(http_build_url)
69 {
70 char *url_str = NULL;
71 size_t url_len = 0;
72 long flags = HTTP_URL_REPLACE;
73 zval *z_old_url = NULL, *z_new_url = NULL, *z_composed_url = NULL;
74 php_url *old_url = NULL, *new_url = NULL, *composed_url = NULL;
75
76 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|z/lz", &z_old_url, &z_new_url, &flags, &z_composed_url) != SUCCESS) {
77 RETURN_FALSE;
78 }
79
80 if (z_new_url) {
81 if (Z_TYPE_P(z_new_url) == IS_ARRAY || Z_TYPE_P(z_new_url) == IS_OBJECT) {
82 new_url = array2url(HASH_OF(z_new_url));
83 } else {
84 convert_to_string(z_new_url);
85 if (!(new_url = php_url_parse_ex(Z_STRVAL_P(z_new_url), Z_STRLEN_P(z_new_url)))) {
86 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url));
87 RETURN_FALSE;
88 }
89 }
90 }
91
92 if (Z_TYPE_P(z_old_url) == IS_ARRAY || Z_TYPE_P(z_old_url) == IS_OBJECT) {
93 old_url = array2url(HASH_OF(z_old_url));
94 } else {
95 convert_to_string(z_old_url);
96 if (!(old_url = php_url_parse_ex(Z_STRVAL_P(z_old_url), Z_STRLEN_P(z_old_url)))) {
97 if (new_url) {
98 php_url_free(new_url);
99 }
100 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url));
101 RETURN_FALSE;
102 }
103 }
104
105 if (z_composed_url) {
106 http_build_url(flags, old_url, new_url, &composed_url, &url_str, &url_len);
107
108 zval_dtor(z_composed_url);
109 array_init(z_composed_url);
110 if (composed_url->scheme) {
111 add_assoc_string(z_composed_url, "scheme", composed_url->scheme, 1);
112 }
113 if (composed_url->user) {
114 add_assoc_string(z_composed_url, "user", composed_url->user, 1);
115 }
116 if (composed_url->pass) {
117 add_assoc_string(z_composed_url, "pass", composed_url->pass, 1);
118 }
119 if (composed_url->host) {
120 add_assoc_string(z_composed_url, "host", composed_url->host, 1);
121 }
122 if (composed_url->port) {
123 add_assoc_long(z_composed_url, "port", composed_url->port);
124 }
125 if (composed_url->path) {
126 add_assoc_string(z_composed_url, "path", composed_url->path, 1);
127 }
128 if (composed_url->query) {
129 add_assoc_string(z_composed_url, "query", composed_url->query, 1);
130 }
131 if (composed_url->fragment) {
132 add_assoc_string(z_composed_url, "fragment", composed_url->fragment, 1);
133 }
134 php_url_free(composed_url);
135 } else {
136 http_build_url(flags, old_url, new_url, NULL, &url_str, &url_len);
137 }
138
139 if (new_url) {
140 php_url_free(new_url);
141 }
142 php_url_free(old_url);
143
144 RETURN_STRINGL(url_str, url_len, 0);
145 }
146 /* }}} */
147
148 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
149 *
150 * Opponent to parse_str().
151 */
152 PHP_FUNCTION(http_build_str)
153 {
154 zval *formdata;
155 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
156 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
157 phpstr formstr;
158
159 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
160 RETURN_FALSE;
161 }
162
163 if (!arg_sep_len) {
164 arg_sep = HTTP_URL_ARGSEP;
165 arg_sep_len = lenof(HTTP_URL_ARGSEP);
166 }
167
168 phpstr_init(&formstr);
169 if (SUCCESS != http_urlencode_hash_recursive(HASH_OF(formdata), &formstr, arg_sep, arg_sep_len, prefix, prefix_len)) {
170 RETURN_FALSE;
171 }
172
173 if (!formstr.used) {
174 phpstr_dtor(&formstr);
175 RETURN_NULL();
176 }
177
178 RETURN_PHPSTR_VAL(&formstr);
179 }
180 /* }}} */
181
182 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
183 { \
184 HashTable *result; \
185 if ((result = http_negotiate_ ##type(supported))) { \
186 char *key; \
187 uint key_len; \
188 ulong idx; \
189 \
190 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
191 RETVAL_STRINGL(key, key_len-1, 0); \
192 } else { \
193 RETVAL_NULL(); \
194 } \
195 \
196 if (rs_array) { \
197 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
198 } \
199 \
200 zend_hash_destroy(result); \
201 FREE_HASHTABLE(result); \
202 \
203 } else { \
204 zval **value; \
205 \
206 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
207 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
208 RETVAL_ZVAL(*value, 1, 0); \
209 } else { \
210 RETVAL_NULL(); \
211 } \
212 \
213 if (rs_array) { \
214 HashPosition pos; \
215 zval **value; \
216 \
217 FOREACH_VAL(pos, supported, value) { \
218 convert_to_string_ex(value); \
219 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
220 } \
221 } \
222 } \
223 }
224
225
226 /* {{{ proto string http_negotiate_language(array supported[, array &result])
227 *
228 * This function negotiates the clients preferred language based on its
229 * Accept-Language HTTP header. The qualifier is recognized and languages
230 * without qualifier are rated highest. The qualifier will be decreased by
231 * 10% for partial matches (i.e. matching primary language).
232 *
233 * Expects an array as parameter cotaining the supported languages as values.
234 * If the optional second parameter is supplied, it will be filled with an
235 * array containing the negotiation results.
236 *
237 * Returns the negotiated language or the default language (i.e. first array entry)
238 * if none match.
239 *
240 * Example:
241 * <pre>
242 * <?php
243 * $langs = array(
244 * 'en-US',// default
245 * 'fr',
246 * 'fr-FR',
247 * 'de',
248 * 'de-DE',
249 * 'de-AT',
250 * 'de-CH',
251 * );
252 *
253 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
254 *
255 * print_r($result);
256 * ?>
257 * </pre>
258 */
259 PHP_FUNCTION(http_negotiate_language)
260 {
261 zval *supported, *rs_array = NULL;
262
263 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array) != SUCCESS) {
264 RETURN_FALSE;
265 }
266
267 if (rs_array) {
268 zval_dtor(rs_array);
269 array_init(rs_array);
270 }
271
272 HTTP_DO_NEGOTIATE(language, supported, rs_array);
273 }
274 /* }}} */
275
276 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
277 *
278 * This function negotiates the clients preferred charset based on its
279 * Accept-Charset HTTP header. The qualifier is recognized and charsets
280 * without qualifier are rated highest.
281 *
282 * Expects an array as parameter cotaining the supported charsets as values.
283 * If the optional second parameter is supplied, it will be filled with an
284 * array containing the negotiation results.
285 *
286 * Returns the negotiated charset or the default charset (i.e. first array entry)
287 * if none match.
288 *
289 * Example:
290 * <pre>
291 * <?php
292 * $charsets = array(
293 * 'iso-8859-1', // default
294 * 'iso-8859-2',
295 * 'iso-8859-15',
296 * 'utf-8'
297 * );
298 *
299 * $pref = http_negotiate_charset($charsets, $result);
300 *
301 * if (strcmp($pref, 'iso-8859-1')) {
302 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
303 * iconv_set_encoding('output_encoding', $pref);
304 * ob_start('ob_iconv_handler');
305 * }
306 *
307 * print_r($result);
308 * ?>
309 * </pre>
310 */
311 PHP_FUNCTION(http_negotiate_charset)
312 {
313 zval *supported, *rs_array = NULL;
314
315 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array) != SUCCESS) {
316 RETURN_FALSE;
317 }
318
319 if (rs_array) {
320 zval_dtor(rs_array);
321 array_init(rs_array);
322 }
323
324 HTTP_DO_NEGOTIATE(charset, supported, rs_array);
325 }
326 /* }}} */
327
328 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
329 *
330 * This function negotiates the clients preferred content type based on its
331 * Accept HTTP header. The qualifier is recognized and content types
332 * without qualifier are rated highest.
333 *
334 * Expects an array as parameter cotaining the supported content types as values.
335 * If the optional second parameter is supplied, it will be filled with an
336 * array containing the negotiation results.
337 *
338 * Returns the negotiated content type or the default content type
339 * (i.e. first array entry) if none match.
340 *
341 * Example:
342 * <pre>
343 * <?php
344 * $ctypes = array('application/xhtml+xml', 'text/html');
345 * http_send_content_type(http_negotiate_content_type($ctypes));
346 * ?>
347 * </pre>
348 */
349 PHP_FUNCTION(http_negotiate_content_type)
350 {
351 zval *supported, *rs_array = NULL;
352
353 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array)) {
354 RETURN_FALSE;
355 }
356
357 if (rs_array) {
358 zval_dtor(rs_array);
359 array_init(rs_array);
360 }
361
362 HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
363 }
364 /* }}} */
365
366 /* {{{ proto bool http_send_status(int status)
367 *
368 * Send HTTP status code.
369 *
370 * Expects an HTTP status code as parameter.
371 *
372 * Returns TRUE on success or FALSE on failure.
373 */
374 PHP_FUNCTION(http_send_status)
375 {
376 int status = 0;
377
378 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
379 RETURN_FALSE;
380 }
381 if (status < 100 || status > 510) {
382 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Invalid HTTP status code (100-510): %d", status);
383 RETURN_FALSE;
384 }
385
386 RETURN_SUCCESS(http_send_status(status));
387 }
388 /* }}} */
389
390 /* {{{ proto bool http_send_last_modified([int timestamp])
391 *
392 * Send a "Last-Modified" header with a valid HTTP date.
393 *
394 * Accepts a unix timestamp, converts it to a valid HTTP date and
395 * sends it as "Last-Modified" HTTP header. If timestamp is
396 * omitted, the current time will be sent.
397 *
398 * Returns TRUE on success or FALSE on failure.
399 */
400 PHP_FUNCTION(http_send_last_modified)
401 {
402 long t = -1;
403
404 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
405 RETURN_FALSE;
406 }
407
408 if (t == -1) {
409 t = (long) HTTP_GET_REQUEST_TIME();
410 }
411
412 RETURN_SUCCESS(http_send_last_modified(t));
413 }
414 /* }}} */
415
416 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
417 *
418 * Send the Content-Type of the sent entity. This is particularly important
419 * if you use the http_send() API.
420 *
421 * Accepts an optional string parameter containing the desired content type
422 * (primary/secondary).
423 *
424 * Returns TRUE on success or FALSE on failure.
425 */
426 PHP_FUNCTION(http_send_content_type)
427 {
428 char *ct = "application/x-octetstream";
429 int ct_len = lenof("application/x-octetstream");
430
431 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
432 RETURN_FALSE;
433 }
434
435 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
436 }
437 /* }}} */
438
439 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
440 *
441 * Send the Content-Disposition. The Content-Disposition header is very useful
442 * if the data actually sent came from a file or something similar, that should
443 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
444 *
445 * Expects a string parameter specifying the file name the "Save as..." dialogue
446 * should display. Optionally accepts a bool parameter, which, if set to true
447 * and the user agent knows how to handle the content type, will probably not
448 * cause the popup window to be shown.
449 *
450 * Returns TRUE on success or FALSE on failure.
451 */
452 PHP_FUNCTION(http_send_content_disposition)
453 {
454 char *filename;
455 int f_len;
456 zend_bool send_inline = 0;
457
458 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
459 RETURN_FALSE;
460 }
461 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
462 }
463 /* }}} */
464
465 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
466 *
467 * Matches the given unix timestamp against the clients "If-Modified-Since"
468 * resp. "If-Unmodified-Since" HTTP headers.
469 *
470 * Accepts a unix timestamp which should be matched. Optionally accepts an
471 * additional bool parameter, which if set to true will check the header
472 * usually used to validate HTTP ranges. If timestamp is omitted, the
473 * current time will be used.
474 *
475 * Returns TRUE if timestamp represents an earlier date than the header,
476 * else FALSE.
477 */
478 PHP_FUNCTION(http_match_modified)
479 {
480 long t = -1;
481 zend_bool for_range = 0;
482
483 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
484 RETURN_FALSE;
485 }
486
487 // current time if not supplied (senseless though)
488 if (t == -1) {
489 t = (long) HTTP_GET_REQUEST_TIME();
490 }
491
492 if (for_range) {
493 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t));
494 }
495 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t));
496 }
497 /* }}} */
498
499 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
500 *
501 * Matches the given ETag against the clients "If-Match" resp.
502 * "If-None-Match" HTTP headers.
503 *
504 * Expects a string parameter containing the ETag to compare. Optionally
505 * accepts a bool parameter, which, if set to true, will check the header
506 * usually used to validate HTTP ranges.
507 *
508 * Retuns TRUE if ETag matches or the header contained the asterisk ("*"),
509 * else FALSE.
510 */
511 PHP_FUNCTION(http_match_etag)
512 {
513 int etag_len;
514 char *etag;
515 zend_bool for_range = 0;
516
517 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
518 RETURN_FALSE;
519 }
520
521 if (for_range) {
522 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag));
523 }
524 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag));
525 }
526 /* }}} */
527
528 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
529 *
530 * Attempts to cache the sent entity by its last modification date.
531 *
532 * Accepts a unix timestamp as parameter which is handled as follows:
533 *
534 * If timestamp_or_expires is greater than 0, it is handled as timestamp
535 * and will be sent as date of last modification. If it is 0 or omitted,
536 * the current time will be sent as Last-Modified date. If it's negative,
537 * it is handled as expiration time in seconds, which means that if the
538 * requested last modification date is not between the calculated timespan,
539 * the Last-Modified header is updated and the actual body will be sent.
540 *
541 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
542 *
543 * A log entry will be written to the cache log if the INI entry
544 * http.cache_log is set and the cache attempt was successful.
545 */
546 PHP_FUNCTION(http_cache_last_modified)
547 {
548 long last_modified = 0, send_modified = 0, t;
549 zval *zlm;
550
551 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
552 RETURN_FALSE;
553 }
554
555 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
556
557 t = (long) HTTP_GET_REQUEST_TIME();
558
559 /* 0 or omitted */
560 if (!last_modified) {
561 /* does the client have? (att: caching "forever") */
562 if ((zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
563 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
564 /* send current time */
565 } else {
566 send_modified = t;
567 }
568 /* negative value is supposed to be expiration time */
569 } else if (last_modified < 0) {
570 last_modified += t;
571 send_modified = t;
572 /* send supplied time explicitly */
573 } else {
574 send_modified = last_modified;
575 }
576
577 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
578 }
579 /* }}} */
580
581 /* {{{ proto bool http_cache_etag([string etag])
582 *
583 * Attempts to cache the sent entity by its ETag, either supplied or generated
584 * by the hash algorythm specified by the INI setting "http.etag_mode".
585 *
586 * If the clients "If-None-Match" header matches the supplied/calculated
587 * ETag, the body is considered cached on the clients side and
588 * a "304 Not Modified" status code is issued.
589 *
590 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
591 *
592 * A log entry is written to the cache log if the INI entry
593 * "http.cache_log" is set and the cache attempt was successful.
594 */
595 PHP_FUNCTION(http_cache_etag)
596 {
597 char *etag = NULL;
598 int etag_len = 0;
599
600 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
601 RETURN_FALSE;
602 }
603
604 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
605
606 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
607 }
608 /* }}} */
609
610 /* {{{ proto string ob_etaghandler(string data, int mode)
611 *
612 * For use with ob_start(). Output buffer handler generating an ETag with
613 * the hash algorythm specified with the INI setting "http.etag_mode".
614 */
615 PHP_FUNCTION(ob_etaghandler)
616 {
617 char *data;
618 int data_len;
619 long mode;
620
621 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
622 RETURN_FALSE;
623 }
624
625 Z_TYPE_P(return_value) = IS_STRING;
626 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
627 }
628 /* }}} */
629
630 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
631 *
632 * Sets the throttle delay and send buffer size for use with http_send() API.
633 * Provides a basic throttling mechanism, which will yield the current process
634 * resp. thread until the entity has been completely sent, though.
635 *
636 * Expects a double parameter specifying the seconds too sleep() after
637 * each chunk sent. Additionally accepts an optional int parameter
638 * representing the chunk size in bytes.
639 *
640 * Example:
641 * <pre>
642 * <?php
643 * // ~ 20 kbyte/s
644 * # http_throttle(1, 20000);
645 * # http_throttle(0.5, 10000);
646 * # http_throttle(0.1, 2000);
647 * http_send_file('document.pdf');
648 * ?>
649 * </pre>
650 */
651 PHP_FUNCTION(http_throttle)
652 {
653 long chunk_size = HTTP_SENDBUF_SIZE;
654 double interval;
655
656 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|l", &interval, &chunk_size)) {
657 return;
658 }
659
660 HTTP_G(send).throttle_delay = interval;
661 HTTP_G(send).buffer_size = chunk_size;
662 }
663 /* }}} */
664
665 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
666 *
667 * Redirect to the given url.
668 *
669 * The supplied url will be expanded with http_build_url(), the params array will
670 * be treated with http_build_query() and the session identification will be appended
671 * if session is true.
672 *
673 * The HTTP response code will be set according to status.
674 * You can use one of the following constants for convenience:
675 * - HTTP_REDIRECT 302 Found for GET/HEAD, else 303 See Other
676 * - HTTP_REDIRECT_PERM 301 Moved Permanently
677 * - HTTP_REDIRECT_FOUND 302 Found
678 * - HTTP_REDIRECT_POST 303 See Other
679 * - HTTP_REDIRECT_PROXY 305 Use Proxy
680 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
681 *
682 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
683 * for which redirect response code to use in which situation.
684 *
685 * To be RFC compliant, "Redirecting to <a>URL</a>." will be displayed,
686 * if the client doesn't redirect immediatly, and the request method was
687 * another one than HEAD.
688 *
689 * Returns FALSE on failure, or *exits* on success.
690 *
691 * A log entry will be written to the redirect log, if the INI entry
692 * "http.redirect_log" is set and the redirect attempt was successful.
693 */
694 PHP_FUNCTION(http_redirect)
695 {
696 int url_len;
697 size_t query_len = 0;
698 zend_bool session = 0, free_params = 0;
699 zval *params = NULL;
700 long status = HTTP_REDIRECT;
701 char *query = NULL, *url = NULL, *URI, *LOC, *RED = NULL;
702
703 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bl", &url, &url_len, &params, &session, &status) != SUCCESS) {
704 RETURN_FALSE;
705 }
706
707 #ifdef HAVE_PHP_SESSION
708 /* append session info */
709 if (session) {
710 if (!params) {
711 free_params = 1;
712 MAKE_STD_ZVAL(params);
713 array_init(params);
714 }
715 if (PS(session_status) == php_session_active) {
716 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
717 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
718 }
719 }
720 }
721 #endif
722
723 /* treat params array with http_build_query() */
724 if (params) {
725 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
726 if (free_params) {
727 zval_dtor(params);
728 FREE_ZVAL(params);
729 }
730 if (query) {
731 efree(query);
732 }
733 RETURN_FALSE;
734 }
735 }
736
737 URI = http_absolute_url(url);
738
739 if (query_len) {
740 spprintf(&LOC, 0, "Location: %s?%s", URI, query);
741 if (status != 300) {
742 spprintf(&RED, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
743 }
744 } else {
745 spprintf(&LOC, 0, "Location: %s", URI);
746 if (status != 300) {
747 spprintf(&RED, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
748 }
749 }
750
751 efree(URI);
752 if (query) {
753 efree(query);
754 }
755 if (free_params) {
756 zval_dtor(params);
757 FREE_ZVAL(params);
758 }
759
760 switch (status)
761 {
762 case 300:
763 RETVAL_SUCCESS(http_send_status_header(status, LOC));
764 efree(LOC);
765 return;
766 break;
767
768 case HTTP_REDIRECT_PERM:
769 case HTTP_REDIRECT_FOUND:
770 case HTTP_REDIRECT_POST:
771 case HTTP_REDIRECT_PROXY:
772 case HTTP_REDIRECT_TEMP:
773 break;
774
775 case 306:
776 default:
777 http_error_ex(HE_NOTICE, HTTP_E_RUNTIME, "Unsupported redirection status code: %ld", status);
778 case HTTP_REDIRECT:
779 if ( SG(request_info).request_method &&
780 strcasecmp(SG(request_info).request_method, "HEAD") &&
781 strcasecmp(SG(request_info).request_method, "GET")) {
782 status = HTTP_REDIRECT_POST;
783 } else {
784 status = HTTP_REDIRECT_FOUND;
785 }
786 break;
787 }
788
789 RETURN_SUCCESS(http_exit_ex(status, LOC, RED, 1));
790 }
791 /* }}} */
792
793 /* {{{ proto bool http_send_data(string data)
794 *
795 * Sends raw data with support for (multiple) range requests.
796 *
797 * Retursn TRUE on success, or FALSE on failure.
798 */
799 PHP_FUNCTION(http_send_data)
800 {
801 zval *zdata;
802
803 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
804 RETURN_FALSE;
805 }
806
807 convert_to_string_ex(&zdata);
808 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
809 }
810 /* }}} */
811
812 /* {{{ proto bool http_send_file(string file)
813 *
814 * Sends a file with support for (multiple) range requests.
815 *
816 * Expects a string parameter referencing the file to send.
817 *
818 * Returns TRUE on success, or FALSE on failure.
819 */
820 PHP_FUNCTION(http_send_file)
821 {
822 char *file;
823 int flen = 0;
824
825 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
826 RETURN_FALSE;
827 }
828 if (!flen) {
829 RETURN_FALSE;
830 }
831
832 RETURN_SUCCESS(http_send_file(file));
833 }
834 /* }}} */
835
836 /* {{{ proto bool http_send_stream(resource stream)
837 *
838 * Sends an already opened stream with support for (multiple) range requests.
839 *
840 * Expects a resource parameter referncing the stream to read from.
841 *
842 * Returns TRUE on success, or FALSE on failure.
843 */
844 PHP_FUNCTION(http_send_stream)
845 {
846 zval *zstream;
847 php_stream *file;
848
849 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
850 RETURN_FALSE;
851 }
852
853 php_stream_from_zval(file, &zstream);
854 RETURN_SUCCESS(http_send_stream(file));
855 }
856 /* }}} */
857
858 /* {{{ proto string http_chunked_decode(string encoded)
859 *
860 * Decodes a string that was HTTP-chunked encoded.
861 *
862 * Expects a chunked encoded string as parameter.
863 *
864 * Returns the decoded string on success or FALSE on failure.
865 */
866 PHP_FUNCTION(http_chunked_decode)
867 {
868 char *encoded = NULL, *decoded = NULL;
869 size_t decoded_len = 0;
870 int encoded_len = 0;
871
872 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
873 RETURN_FALSE;
874 }
875
876 if (NULL != http_encoding_dechunk(encoded, encoded_len, &decoded, &decoded_len)) {
877 RETURN_STRINGL(decoded, (int) decoded_len, 0);
878 } else {
879 RETURN_FALSE;
880 }
881 }
882 /* }}} */
883
884 /* {{{ proto object http_parse_message(string message)
885 *
886 * Parses (a) http_message(s) into a simple recursive object structure.
887 *
888 * Expects a string parameter containing a single HTTP message or
889 * several consecutive HTTP messages.
890 *
891 * Returns an hierachical object structure of the parsed messages.
892 *
893 * Example:
894 * <pre>
895 * <?php
896 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
897 *
898 * stdClass object
899 * (
900 * [type] => 2
901 * [httpVersion] => 1.1
902 * [responseCode] => 200
903 * [headers] => Array
904 * (
905 * [Content-Length] => 3
906 * [Server] => Apache
907 * )
908 * [body] => Hi!
909 * [parentMessage] => stdClass object
910 * (
911 * [type] => 2
912 * [httpVersion] => 1.1
913 * [responseCode] => 302
914 * [headers] => Array
915 * (
916 * [Content-Length] => 0
917 * [Location] => ...
918 * )
919 * [body] =>
920 * [parentMessage] => ...
921 * )
922 * )
923 * ?>
924 * </pre>
925 */
926 PHP_FUNCTION(http_parse_message)
927 {
928 char *message;
929 int message_len;
930 http_message *msg = NULL;
931
932 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &message, &message_len)) {
933 RETURN_NULL();
934 }
935
936 if ((msg = http_message_parse(message, message_len))) {
937 object_init(return_value);
938 http_message_tostruct_recursive(msg, return_value);
939 http_message_free(&msg);
940 } else {
941 RETURN_NULL();
942 }
943 }
944 /* }}} */
945
946 /* {{{ proto array http_parse_headers(string header)
947 *
948 * Parses HTTP headers into an associative array.
949 *
950 * Expects a string parameter containing HTTP headers.
951 *
952 * Returns an array on success, or FALSE on failure.
953 *
954 * Example:
955 * <pre>
956 * <?php
957 * $headers = "content-type: text/html; charset=UTF-8\r\n".
958 * "Server: Funky/1.0\r\n".
959 * "Set-Cookie: foo=bar\r\n".
960 * "Set-Cookie: baz=quux\r\n".
961 * "Folded: works\r\n\ttoo\r\n";
962 * print_r(http_parse_headers($headers));
963 *
964 * Array
965 * (
966 * [Content-Type] => text/html; chatset=UTF-8
967 * [Server] => Funky/1.0
968 * [Set-Cookie] => Array
969 * (
970 * [0] => foo=bar
971 * [1] => baz=quux
972 * )
973 * [Folded] => works
974 * too
975 * )
976 * ?>
977 * </pre>
978 */
979 PHP_FUNCTION(http_parse_headers)
980 {
981 char *header;
982 int header_len;
983
984 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
985 RETURN_FALSE;
986 }
987
988 array_init(return_value);
989 if (SUCCESS != http_parse_headers(header, return_value)) {
990 zval_dtor(return_value);
991 RETURN_FALSE;
992 }
993 }
994 /* }}}*/
995
996 /* {{{ proto object http_parse_cookie(string cookie)
997 *
998 * Parses HTTP cookies like sent in a response into a struct.
999 *
1000 * Expects a string as parameter containing the value of a Set-Cookie response header.
1001 *
1002 * Returns an stdClass object with the cookie params as properties on success or FALSE on failure.
1003 *
1004 * Example:
1005 * <pre>
1006 * <?php
1007 * print_r(http_parse_cookie("foo=bar; path=/"));
1008 *
1009 * stdClass Object
1010 * (
1011 * [name] => foo
1012 * [value] => bar
1013 * [path] => /
1014 * )
1015 * ?>
1016 * </pre>
1017 */
1018 PHP_FUNCTION(http_parse_cookie)
1019 {
1020 char *cookie;
1021 int cookie_len;
1022
1023 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cookie, &cookie_len)) {
1024 RETURN_FALSE;
1025 }
1026
1027 object_init(return_value);
1028 if (SUCCESS != http_parse_cookie(cookie, HASH_OF(return_value))) {
1029 zval_dtor(return_value);
1030 RETURN_FALSE;
1031 }
1032 }
1033
1034 /* {{{ proto array http_get_request_headers(void)
1035 *
1036 * Get a list of incoming HTTP headers.
1037 *
1038 * Returns an associative array of incoming request headers.
1039 */
1040 PHP_FUNCTION(http_get_request_headers)
1041 {
1042 NO_ARGS;
1043
1044 array_init(return_value);
1045 http_get_request_headers(return_value);
1046 }
1047 /* }}} */
1048
1049 /* {{{ proto string http_get_request_body(void)
1050 *
1051 * Get the raw request body (e.g. POST or PUT data).
1052 *
1053 * This function can not be used after http_get_request_body_stream()
1054 * if the request method was another than POST.
1055 *
1056 * Returns the raw request body as string on success or NULL on failure.
1057 */
1058 PHP_FUNCTION(http_get_request_body)
1059 {
1060 char *body;
1061 size_t length;
1062
1063 NO_ARGS;
1064
1065 if (SUCCESS == http_get_request_body(&body, &length)) {
1066 RETURN_STRINGL(body, (int) length, 0);
1067 } else {
1068 RETURN_NULL();
1069 }
1070 }
1071 /* }}} */
1072
1073 /* {{{ proto resource http_get_request_body_stream(void)
1074 *
1075 * Create a stream to read the raw request body (e.g. POST or PUT data).
1076 *
1077 * This function can only be used once if the request method was another than POST.
1078 *
1079 * Returns the raw request body as stream on success or NULL on failure.
1080 */
1081 PHP_FUNCTION(http_get_request_body_stream)
1082 {
1083 php_stream *s;
1084
1085 NO_ARGS;
1086
1087 if ((s = http_get_request_body_stream())) {
1088 php_stream_to_zval(s, return_value);
1089 } else {
1090 http_error(HE_WARNING, HTTP_E_RUNTIME, "Failed to create request body stream");
1091 RETURN_NULL();
1092 }
1093 }
1094 /* }}} */
1095
1096 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
1097 *
1098 * Match an incoming HTTP header.
1099 *
1100 * Expects two string parameters representing the header name (case-insensitive)
1101 * and the header value that should be compared. The case sensitivity of the
1102 * header value depends on the additional optional bool parameter accepted.
1103 *
1104 * Returns TRUE if header value matches, else FALSE.
1105 */
1106 PHP_FUNCTION(http_match_request_header)
1107 {
1108 char *header, *value;
1109 int header_len, value_len;
1110 zend_bool match_case = 0;
1111
1112 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
1113 RETURN_FALSE;
1114 }
1115
1116 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
1117 }
1118 /* }}} */
1119
1120 /* {{{ HAVE_CURL */
1121 #ifdef HTTP_HAVE_CURL
1122
1123 #define RETVAL_RESPONSE_OR_BODY(request) \
1124 { \
1125 zval **bodyonly; \
1126 \
1127 /* check if only the body should be returned */ \
1128 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void **) &bodyonly)) && zval_is_true(*bodyonly)) { \
1129 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1130 \
1131 if (msg) { \
1132 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1133 http_message_free(&msg); \
1134 } \
1135 } else { \
1136 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1137 } \
1138 }
1139
1140 /* {{{ proto string http_get(string url[, array options[, array &info]])
1141 *
1142 * Performs an HTTP GET request on the supplied url.
1143 *
1144 * The second parameter, if set, is expected to be an associative
1145 * array where the following keys will be recognized:
1146 * <pre>
1147 * - redirect: int, whether and how many redirects to follow
1148 * - unrestrictedauth: bool, whether to continue sending credentials on
1149 * redirects to a different host
1150 * - proxyhost: string, proxy host in "host[:port]" format
1151 * - proxyport: int, use another proxy port as specified in proxyhost
1152 * - proxyauth: string, proxy credentials in "user:pass" format
1153 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1154 * - httpauth: string, http credentials in "user:pass" format
1155 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1156 * - compress: bool, whether to allow gzip/deflate content encoding
1157 * - port: int, use another port as specified in the url
1158 * - referer: string, the referer to send
1159 * - useragent: string, the user agent to send
1160 * (defaults to PECL::HTTP/version (PHP/version)))
1161 * - headers: array, list of custom headers as associative array
1162 * like array("header" => "value")
1163 * - cookies: array, list of cookies as associative array
1164 * like array("cookie" => "value")
1165 * - cookiestore: string, path to a file where cookies are/will be stored
1166 * - resume: int, byte offset to start the download from;
1167 * if the server supports ranges
1168 * - maxfilesize: int, maximum file size that should be downloaded;
1169 * has no effect, if the size of the requested entity is not known
1170 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1171 * - etag: string, quoted etag for If-(None-)Match header
1172 * - timeout: int, seconds the request may take
1173 * - connecttimeout: int, seconds the connect may take
1174 * - onprogress: mixed, progress callback
1175 * - ssl: array, with the following options:
1176 * cert: string, path to certificate
1177 * certtype: string, type of certificate
1178 * certpasswd: string, password for certificate
1179 * key: string, path to key
1180 * keytype: string, type of key
1181 * keypasswd: string, pasword for key
1182 * engine: string, ssl engine to use
1183 * version: int, ssl version to use
1184 * verifypeer: bool, whether to verify the peer
1185 * verifyhost: bool whether to verify the host
1186 * cipher_list: string, list of allowed ciphers
1187 * cainfo: string
1188 * capath: string
1189 * random_file: string
1190 * egdsocket: string
1191 * </pre>
1192 *
1193 * The optional third parameter will be filled with some additional information
1194 * in form af an associative array, if supplied, like the following example:
1195 * <pre>
1196 * <?php
1197 * array (
1198 * 'effective_url' => 'http://localhost',
1199 * 'response_code' => 403,
1200 * 'total_time' => 0.017,
1201 * 'namelookup_time' => 0.013,
1202 * 'connect_time' => 0.014,
1203 * 'pretransfer_time' => 0.014,
1204 * 'size_upload' => 0,
1205 * 'size_download' => 202,
1206 * 'speed_download' => 11882,
1207 * 'speed_upload' => 0,
1208 * 'header_size' => 145,
1209 * 'request_size' => 62,
1210 * 'ssl_verifyresult' => 0,
1211 * 'filetime' => -1,
1212 * 'content_length_download' => 202,
1213 * 'content_length_upload' => 0,
1214 * 'starttransfer_time' => 0.017,
1215 * 'content_type' => 'text/html; charset=iso-8859-1',
1216 * 'redirect_time' => 0,
1217 * 'redirect_count' => 0,
1218 * 'http_connectcode' => 0,
1219 * 'httpauth_avail' => 0,
1220 * 'proxyauth_avail' => 0,
1221 * )
1222 * ?>
1223 * </pre>
1224 *
1225 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1226 */
1227 PHP_FUNCTION(http_get)
1228 {
1229 zval *options = NULL, *info = NULL;
1230 char *URL;
1231 int URL_len;
1232 http_request request;
1233
1234 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1235 RETURN_FALSE;
1236 }
1237
1238 if (info) {
1239 zval_dtor(info);
1240 array_init(info);
1241 }
1242
1243 RETVAL_FALSE;
1244
1245 http_request_init_ex(&request, NULL, HTTP_GET, URL);
1246 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1247 http_request_exec(&request);
1248 if (info) {
1249 http_request_info(&request, Z_ARRVAL_P(info));
1250 }
1251 RETVAL_RESPONSE_OR_BODY(request);
1252 }
1253 http_request_dtor(&request);
1254 }
1255 /* }}} */
1256
1257 /* {{{ proto string http_head(string url[, array options[, array &info]])
1258 *
1259 * Performs an HTTP HEAD request on the supplied url.
1260 *
1261 * See http_get() for a full list of available parameters and options.
1262 *
1263 * Returns the HTTP response as string on success, or FALSE on failure.
1264 */
1265 PHP_FUNCTION(http_head)
1266 {
1267 zval *options = NULL, *info = NULL;
1268 char *URL;
1269 int URL_len;
1270 http_request request;
1271
1272 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1273 RETURN_FALSE;
1274 }
1275
1276 if (info) {
1277 zval_dtor(info);
1278 array_init(info);
1279 }
1280
1281 RETVAL_FALSE;
1282
1283 http_request_init_ex(&request, NULL, HTTP_HEAD, URL);
1284 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1285 http_request_exec(&request);
1286 if (info) {
1287 http_request_info(&request, Z_ARRVAL_P(info));
1288 }
1289 RETVAL_RESPONSE_OR_BODY(request);
1290 }
1291 http_request_dtor(&request);
1292 }
1293 /* }}} */
1294
1295 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1296 *
1297 * Performs an HTTP POST requeston the supplied url.
1298 *
1299 * Expects a string as second parameter containing the pre-encoded post data.
1300 * See http_get() for a full list of available parameters and options.
1301 *
1302 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1303 */
1304 PHP_FUNCTION(http_post_data)
1305 {
1306 zval *options = NULL, *info = NULL;
1307 char *URL, *postdata;
1308 int postdata_len, URL_len;
1309 http_request_body body;
1310 http_request request;
1311
1312 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1313 RETURN_FALSE;
1314 }
1315
1316 if (info) {
1317 zval_dtor(info);
1318 array_init(info);
1319 }
1320
1321 RETVAL_FALSE;
1322
1323 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1324 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, postdata, postdata_len, 0);
1325 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1326 http_request_exec(&request);
1327 if (info) {
1328 http_request_info(&request, Z_ARRVAL_P(info));
1329 }
1330 RETVAL_RESPONSE_OR_BODY(request);
1331 }
1332 http_request_dtor(&request);
1333 }
1334 /* }}} */
1335
1336 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1337 *
1338 * Performs an HTTP POST request on the supplied url.
1339 *
1340 * Expecrs an associative array as second parameter, which will be
1341 * www-form-urlencoded. See http_get() for a full list of available options.
1342 *
1343 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1344 */
1345 PHP_FUNCTION(http_post_fields)
1346 {
1347 zval *options = NULL, *info = NULL, *fields, *files = NULL;
1348 char *URL;
1349 int URL_len;
1350 http_request_body body;
1351 http_request request;
1352
1353 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
1354 RETURN_FALSE;
1355 }
1356
1357 if (!http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
1358 RETURN_FALSE;
1359 }
1360
1361 if (info) {
1362 zval_dtor(info);
1363 array_init(info);
1364 }
1365
1366 RETVAL_FALSE;
1367
1368 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1369 request.body = &body;
1370 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1371 http_request_exec(&request);
1372 if (info) {
1373 http_request_info(&request, Z_ARRVAL_P(info));
1374 }
1375 RETVAL_RESPONSE_OR_BODY(request);
1376 }
1377 http_request_dtor(&request);
1378 }
1379 /* }}} */
1380
1381 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1382 *
1383 * Performs an HTTP PUT request on the supplied url.
1384 *
1385 * Expects the second parameter to be a string referncing the file to upload.
1386 * See http_get() for a full list of available options.
1387 *
1388 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1389 */
1390 PHP_FUNCTION(http_put_file)
1391 {
1392 char *URL, *file;
1393 int URL_len, f_len;
1394 zval *options = NULL, *info = NULL;
1395 php_stream *stream;
1396 php_stream_statbuf ssb;
1397 http_request_body body;
1398 http_request request;
1399
1400 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1401 RETURN_FALSE;
1402 }
1403
1404 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1405 RETURN_FALSE;
1406 }
1407 if (php_stream_stat(stream, &ssb)) {
1408 php_stream_close(stream);
1409 RETURN_FALSE;
1410 }
1411
1412 if (info) {
1413 zval_dtor(info);
1414 array_init(info);
1415 }
1416
1417 RETVAL_FALSE;
1418
1419 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1420 body.data = stream;
1421 body.size = ssb.sb.st_size;
1422
1423 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1424 request.body = &body;
1425 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1426 http_request_exec(&request);
1427 if (info) {
1428 http_request_info(&request, Z_ARRVAL_P(info));
1429 }
1430 RETVAL_RESPONSE_OR_BODY(request);
1431 }
1432 http_request_body_dtor(&body);
1433 request.body = NULL;
1434 http_request_dtor(&request);
1435 }
1436 /* }}} */
1437
1438 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1439 *
1440 * Performs an HTTP PUT request on the supplied url.
1441 *
1442 * Expects the second parameter to be a resource referencing an already
1443 * opened stream, from which the data to upload should be read.
1444 * See http_get() for a full list of available options.
1445 *
1446 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1447 */
1448 PHP_FUNCTION(http_put_stream)
1449 {
1450 zval *resource, *options = NULL, *info = NULL;
1451 char *URL;
1452 int URL_len;
1453 php_stream *stream;
1454 php_stream_statbuf ssb;
1455 http_request_body body;
1456 http_request request;
1457
1458 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1459 RETURN_FALSE;
1460 }
1461
1462 php_stream_from_zval(stream, &resource);
1463 if (php_stream_stat(stream, &ssb)) {
1464 RETURN_FALSE;
1465 }
1466
1467 if (info) {
1468 zval_dtor(info);
1469 array_init(info);
1470 }
1471
1472 RETVAL_FALSE;
1473
1474 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1475 body.data = stream;
1476 body.size = ssb.sb.st_size;
1477
1478 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1479 request.body = &body;
1480 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1481 http_request_exec(&request);
1482 if (info) {
1483 http_request_info(&request, Z_ARRVAL_P(info));
1484 }
1485 RETVAL_RESPONSE_OR_BODY(request);
1486 }
1487 request.body = NULL;
1488 http_request_dtor(&request);
1489 }
1490 /* }}} */
1491 #endif /* HTTP_HAVE_CURL */
1492 /* }}} HAVE_CURL */
1493
1494 /* {{{ proto int http_request_method_register(string method)
1495 *
1496 * Register a custom request method.
1497 *
1498 * Expects a string parameter containing the request method name to register.
1499 *
1500 * Returns the ID of the request method on success, or FALSE on failure.
1501 */
1502 PHP_FUNCTION(http_request_method_register)
1503 {
1504 char *method;
1505 int method_len;
1506 ulong existing;
1507
1508 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1509 RETURN_FALSE;
1510 }
1511 if ((existing = http_request_method_exists(1, 0, method))) {
1512 RETURN_LONG((long) existing);
1513 }
1514
1515 RETVAL_LONG((long) http_request_method_register(method, method_len));
1516 }
1517 /* }}} */
1518
1519 /* {{{ proto bool http_request_method_unregister(mixed method)
1520 *
1521 * Unregister a previously registered custom request method.
1522 *
1523 * Expects either the request method name or ID.
1524 *
1525 * Returns TRUE on success, or FALSE on failure.
1526 */
1527 PHP_FUNCTION(http_request_method_unregister)
1528 {
1529 zval *method;
1530
1531 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1532 RETURN_FALSE;
1533 }
1534
1535 switch (Z_TYPE_P(method))
1536 {
1537 case IS_OBJECT:
1538 convert_to_string(method);
1539 case IS_STRING:
1540 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1541 convert_to_long(method);
1542 } else {
1543 int mn;
1544 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1545 RETURN_FALSE;
1546 }
1547 zval_dtor(method);
1548 ZVAL_LONG(method, (long)mn);
1549 }
1550 case IS_LONG:
1551 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1552 default:
1553 RETURN_FALSE;
1554 }
1555 }
1556 /* }}} */
1557
1558 /* {{{ proto int http_request_method_exists(mixed method)
1559 *
1560 * Check if a request method is registered (or available by default).
1561 *
1562 * Expects either the request method name or ID as parameter.
1563 *
1564 * Returns TRUE if the request method is known, else FALSE.
1565 */
1566 PHP_FUNCTION(http_request_method_exists)
1567 {
1568 IF_RETVAL_USED {
1569 zval *method;
1570
1571 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1572 RETURN_FALSE;
1573 }
1574
1575 switch (Z_TYPE_P(method))
1576 {
1577 case IS_OBJECT:
1578 convert_to_string(method);
1579 case IS_STRING:
1580 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1581 convert_to_long(method);
1582 } else {
1583 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1584 }
1585 case IS_LONG:
1586 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method), NULL));
1587 default:
1588 RETURN_FALSE;
1589 }
1590 }
1591 }
1592 /* }}} */
1593
1594 /* {{{ proto string http_request_method_name(int method)
1595 *
1596 * Get the literal string representation of a standard or registered request method.
1597 *
1598 * Expects the request method ID as parameter.
1599 *
1600 * Returns the request method name as string on success, or FALSE on failure.
1601 */
1602 PHP_FUNCTION(http_request_method_name)
1603 {
1604 IF_RETVAL_USED {
1605 long method;
1606
1607 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1608 RETURN_FALSE;
1609 }
1610
1611 RETURN_STRING(estrdup(http_request_method_name((int) method)), 0);
1612 }
1613 }
1614 /* }}} */
1615
1616 /* {{{ */
1617 #ifdef HTTP_HAVE_ZLIB
1618
1619 /* {{{ proto string http_deflate(string data[, int flags = 0])
1620 *
1621 * Compress data with gzip, zlib AKA deflate or raw deflate encoding.
1622 *
1623 * Expects the first parameter to be a string containing the data that should
1624 * be encoded.
1625 *
1626 * Returns the encoded string on success, or NULL on failure.
1627 */
1628 PHP_FUNCTION(http_deflate)
1629 {
1630 char *data;
1631 int data_len;
1632 long flags = 0;
1633
1634 RETVAL_NULL();
1635
1636 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &flags)) {
1637 char *encoded;
1638 size_t encoded_len;
1639
1640 if (SUCCESS == http_encoding_deflate(flags, data, data_len, &encoded, &encoded_len)) {
1641 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1642 }
1643 }
1644 }
1645 /* }}} */
1646
1647 /* {{{ proto string http_inflate(string data)
1648 *
1649 * Uncompress data compressed with either gzip, deflate AKA zlib or raw
1650 * deflate encoding.
1651 *
1652 * Expects a string as parameter containing the compressed data.
1653 *
1654 * Returns the decoded string on success, or NULL on failure.
1655 */
1656 PHP_FUNCTION(http_inflate)
1657 {
1658 char *data;
1659 int data_len;
1660
1661 RETVAL_NULL();
1662
1663 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1664 char *decoded;
1665 size_t decoded_len;
1666
1667 if (SUCCESS == http_encoding_inflate(data, data_len, &decoded, &decoded_len)) {
1668 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1669 }
1670 }
1671 }
1672 /* }}} */
1673
1674 /* {{{ proto string ob_deflatehandler(string data, int mode)
1675 *
1676 * For use with ob_start(). The deflate output buffer handler can only be used once.
1677 * It conflicts with ob_gzhanlder and zlib.output_compression as well and should
1678 * not be used after ext/mbstrings mb_output_handler and ext/sessions URL-Rewriter (AKA
1679 * session.use_trans_sid).
1680 */
1681 PHP_FUNCTION(ob_deflatehandler)
1682 {
1683 char *data;
1684 int data_len;
1685 long mode;
1686
1687 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1688 RETURN_FALSE;
1689 }
1690
1691 http_ob_deflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1692 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1693 }
1694 /* }}} */
1695
1696 /* {{{ proto string ob_inflatehandler(string data, int mode)
1697 *
1698 * For use with ob_start(). Same restrictions as with ob_deflatehandler apply.
1699 */
1700 PHP_FUNCTION(ob_inflatehandler)
1701 {
1702 char *data;
1703 int data_len;
1704 long mode;
1705
1706 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1707 RETURN_FALSE;
1708 }
1709
1710 http_ob_inflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1711 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1712 }
1713 /* }}} */
1714
1715 #endif /* HTTP_HAVE_ZLIB */
1716 /* }}} */
1717
1718 /* {{{ proto int http_support([int feature = 0])
1719 *
1720 * Check for feature that require external libraries.
1721 *
1722 * Accpepts an optional in parameter specifying which feature to probe for.
1723 * If the parameter is 0 or omitted, the return value contains a bitmask of
1724 * all supported features that depend on external libraries.
1725 *
1726 * Available features to probe for are:
1727 * <ul>
1728 * <li> HTTP_SUPPORT: always set
1729 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1730 * and HTTP requests can be issued
1731 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1732 * and SSL requests can be issued
1733 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1734 * and compressed HTTP responses can be decoded
1735 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1736 * and the HttpResponse::guessContentType() method is usable
1737 * </ul>
1738 *
1739 * Returns int, whether requested feature is supported, or a bitmask with
1740 * all supported features.
1741 */
1742 PHP_FUNCTION(http_support)
1743 {
1744 long feature = 0;
1745
1746 RETVAL_LONG(0L);
1747
1748 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &feature)) {
1749 RETVAL_LONG(http_support(feature));
1750 }
1751 }
1752 /* }}} */
1753
1754 PHP_FUNCTION(http_test)
1755 {
1756 }
1757
1758 /*
1759 * Local variables:
1760 * tab-width: 4
1761 * c-basic-offset: 4
1762 * End:
1763 * vim600: noet sw=4 ts=4 fdm=marker
1764 * vim<600: noet sw=4 ts=4
1765 */
1766