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