2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
21 #include "ext/standard/php_string.h"
22 #include "zend_operators.h"
24 #ifdef HAVE_PHP_SESSION
25 # include "ext/session/php_session.h"
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"
40 /* {{{ proto string http_date([int timestamp])
42 * Compose a valid HTTP date regarding RFC 1123
43 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
45 * Accepts an optional unix timestamp as parameter.
47 * Returns the HTTP date as string.
49 PHP_FUNCTION(http_date
)
53 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
58 t
= (long) HTTP_GET_REQUEST_TIME();
61 RETURN_STRING(http_date(t
), 0);
65 /* {{{ proto string http_build_url(mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]])
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.
75 * The parts of the second URL will be merged into the first according to the flags argument.
76 * The following flags are recognized:
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 naivly; no replacements are done
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
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",
97 * "host" => "ftp.example.com",
98 * "path" => "files/current/",
101 * HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
105 * Returns the new URL as string on success or FALSE on failure.
107 PHP_FUNCTION(http_build_url
)
109 char *url_str
= NULL
;
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
;
115 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/|z/lz", &z_old_url
, &z_new_url
, &flags
, &z_composed_url
) != SUCCESS
) {
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
));
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
));
131 if (Z_TYPE_P(z_old_url
) == IS_ARRAY
|| Z_TYPE_P(z_old_url
) == IS_OBJECT
) {
132 old_url
= array2url(HASH_OF(z_old_url
));
134 convert_to_string(z_old_url
);
135 if (!(old_url
= php_url_parse_ex(Z_STRVAL_P(z_old_url
), Z_STRLEN_P(z_old_url
)))) {
137 php_url_free(new_url
);
139 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url
));
144 if (z_composed_url
) {
145 http_build_url(flags
, old_url
, new_url
, &composed_url
, &url_str
, &url_len
);
147 zval_dtor(z_composed_url
);
148 array_init(z_composed_url
);
149 if (composed_url
->scheme
) {
150 add_assoc_string(z_composed_url
, "scheme", composed_url
->scheme
, 1);
152 if (composed_url
->user
) {
153 add_assoc_string(z_composed_url
, "user", composed_url
->user
, 1);
155 if (composed_url
->pass
) {
156 add_assoc_string(z_composed_url
, "pass", composed_url
->pass
, 1);
158 if (composed_url
->host
) {
159 add_assoc_string(z_composed_url
, "host", composed_url
->host
, 1);
161 if (composed_url
->port
) {
162 add_assoc_long(z_composed_url
, "port", composed_url
->port
);
164 if (composed_url
->path
) {
165 add_assoc_string(z_composed_url
, "path", composed_url
->path
, 1);
167 if (composed_url
->query
) {
168 add_assoc_string(z_composed_url
, "query", composed_url
->query
, 1);
170 if (composed_url
->fragment
) {
171 add_assoc_string(z_composed_url
, "fragment", composed_url
->fragment
, 1);
173 php_url_free(composed_url
);
175 http_build_url(flags
, old_url
, new_url
, NULL
, &url_str
, &url_len
);
179 php_url_free(new_url
);
181 php_url_free(old_url
);
183 RETURN_STRINGL(url_str
, url_len
, 0);
187 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
189 * Opponent to parse_str().
191 * Expects an array as first argument which represents the parts of the query string to build.
192 * Accepts a string as optional second parameter containing a top-level prefix to use.
193 * The optional third parameter should specify an argument separator to use (by default the
194 * INI setting arg_separator.output will be used, or "&" if neither is set).
196 * Returns the built query as string on success or FALSE on failure.
198 PHP_FUNCTION(http_build_str
)
201 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
202 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
205 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
210 arg_sep
= HTTP_URL_ARGSEP
;
211 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
214 phpstr_init(&formstr
);
215 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), &formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
220 phpstr_dtor(&formstr
);
224 RETURN_PHPSTR_VAL(&formstr
);
228 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
231 if ((result = http_negotiate_ ##type(supported))) { \
236 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
237 RETVAL_STRINGL(key, key_len-1, 0); \
243 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
246 zend_hash_destroy(result); \
247 FREE_HASHTABLE(result); \
252 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
253 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
254 RETVAL_ZVAL(*value, 1, 0); \
263 FOREACH_VAL(pos, supported, value) { \
264 convert_to_string_ex(value); \
265 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
272 /* {{{ proto string http_negotiate_language(array supported[, array &result])
274 * This function negotiates the clients preferred language based on its
275 * Accept-Language HTTP header. The qualifier is recognized and languages
276 * without qualifier are rated highest. The qualifier will be decreased by
277 * 10% for partial matches (i.e. matching primary language).
279 * Expects an array as parameter containing the supported languages as values.
280 * If the optional second parameter is supplied, it will be filled with an
281 * array containing the negotiation results.
283 * Returns the negotiated language or the default language (i.e. first array entry)
299 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
305 PHP_FUNCTION(http_negotiate_language
)
307 zval
*supported
, *rs_array
= NULL
;
309 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
315 array_init(rs_array
);
318 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
322 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
324 * This function negotiates the clients preferred charset based on its
325 * Accept-Charset HTTP header. The qualifier is recognized and charsets
326 * without qualifier are rated highest.
328 * Expects an array as parameter containing the supported charsets as values.
329 * If the optional second parameter is supplied, it will be filled with an
330 * array containing the negotiation results.
332 * Returns the negotiated charset or the default charset (i.e. first array entry)
339 * 'iso-8859-1', // default
345 * $pref = http_negotiate_charset($charsets, $result);
347 * if (strcmp($pref, 'iso-8859-1')) {
348 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
349 * iconv_set_encoding('output_encoding', $pref);
350 * ob_start('ob_iconv_handler');
357 PHP_FUNCTION(http_negotiate_charset
)
359 zval
*supported
, *rs_array
= NULL
;
361 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
367 array_init(rs_array
);
370 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
374 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
376 * This function negotiates the clients preferred content type based on its
377 * Accept HTTP header. The qualifier is recognized and content types
378 * without qualifier are rated highest.
380 * Expects an array as parameter containing the supported content types as values.
381 * If the optional second parameter is supplied, it will be filled with an
382 * array containing the negotiation results.
384 * Returns the negotiated content type or the default content type
385 * (i.e. first array entry) if none match.
390 * $ctypes = array('application/xhtml+xml', 'text/html');
391 * http_send_content_type(http_negotiate_content_type($ctypes));
395 PHP_FUNCTION(http_negotiate_content_type
)
397 zval
*supported
, *rs_array
= NULL
;
399 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
405 array_init(rs_array
);
408 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
412 /* {{{ proto bool http_send_status(int status)
414 * Send HTTP status code.
416 * Expects an HTTP status code as parameter.
418 * Returns TRUE on success or FALSE on failure.
420 PHP_FUNCTION(http_send_status
)
424 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
427 if (status
< 100 || status
> 510) {
428 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
432 RETURN_SUCCESS(http_send_status(status
));
436 /* {{{ proto bool http_send_last_modified([int timestamp])
438 * Send a "Last-Modified" header with a valid HTTP date.
440 * Accepts a unix timestamp, converts it to a valid HTTP date and
441 * sends it as "Last-Modified" HTTP header. If timestamp is
442 * omitted, the current time will be sent.
444 * Returns TRUE on success or FALSE on failure.
446 PHP_FUNCTION(http_send_last_modified
)
450 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
455 t
= (long) HTTP_GET_REQUEST_TIME();
458 RETURN_SUCCESS(http_send_last_modified(t
));
462 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
464 * Send the Content-Type of the sent entity. This is particularly important
465 * if you use the http_send() API.
467 * Accepts an optional string parameter containing the desired content type
468 * (primary/secondary).
470 * Returns TRUE on success or FALSE on failure.
472 PHP_FUNCTION(http_send_content_type
)
474 char *ct
= "application/x-octetstream";
475 int ct_len
= lenof("application/x-octetstream");
477 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
481 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
485 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
487 * Send the Content-Disposition. The Content-Disposition header is very useful
488 * if the data actually sent came from a file or something similar, that should
489 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
491 * Expects a string parameter specifying the file name the "Save as..." dialog
492 * should display. Optionally accepts a bool parameter, which, if set to true
493 * and the user agent knows how to handle the content type, will probably not
494 * cause the popup window to be shown.
496 * Returns TRUE on success or FALSE on failure.
498 PHP_FUNCTION(http_send_content_disposition
)
502 zend_bool send_inline
= 0;
504 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
507 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
511 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
513 * Matches the given unix timestamp against the clients "If-Modified-Since"
514 * resp. "If-Unmodified-Since" HTTP headers.
516 * Accepts a unix timestamp which should be matched. Optionally accepts an
517 * additional bool parameter, which if set to true will check the header
518 * usually used to validate HTTP ranges. If timestamp is omitted, the
519 * current time will be used.
521 * Returns TRUE if timestamp represents an earlier date than the header,
524 PHP_FUNCTION(http_match_modified
)
527 zend_bool for_range
= 0;
529 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
533 // current time if not supplied (senseless though)
535 t
= (long) HTTP_GET_REQUEST_TIME();
539 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
541 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
545 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
547 * Matches the given ETag against the clients "If-Match" resp.
548 * "If-None-Match" HTTP headers.
550 * Expects a string parameter containing the ETag to compare. Optionally
551 * accepts a bool parameter, which, if set to true, will check the header
552 * usually used to validate HTTP ranges.
554 * Returns TRUE if ETag matches or the header contained the asterisk ("*"),
557 PHP_FUNCTION(http_match_etag
)
561 zend_bool for_range
= 0;
563 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
568 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
570 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
574 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
576 * Attempts to cache the sent entity by its last modification date.
578 * Accepts a unix timestamp as parameter which is handled as follows:
580 * If timestamp_or_expires is greater than 0, it is handled as timestamp
581 * and will be sent as date of last modification. If it is 0 or omitted,
582 * the current time will be sent as Last-Modified date. If it's negative,
583 * it is handled as expiration time in seconds, which means that if the
584 * requested last modification date is not between the calculated timespan,
585 * the Last-Modified header is updated and the actual body will be sent.
587 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
589 * A log entry will be written to the cache log if the INI entry
590 * http.cache_log is set and the cache attempt was successful.
592 PHP_FUNCTION(http_cache_last_modified
)
594 long last_modified
= 0, send_modified
= 0, t
;
597 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
601 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
603 t
= (long) HTTP_GET_REQUEST_TIME();
606 if (!last_modified
) {
607 /* does the client have? (att: caching "forever") */
608 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
609 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
610 /* send current time */
614 /* negative value is supposed to be expiration time */
615 } else if (last_modified
< 0) {
618 /* send supplied time explicitly */
620 send_modified
= last_modified
;
623 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
627 /* {{{ proto bool http_cache_etag([string etag])
629 * Attempts to cache the sent entity by its ETag, either supplied or generated
630 * by the hash algorithm specified by the INI setting "http.etag_mode".
632 * If the clients "If-None-Match" header matches the supplied/calculated
633 * ETag, the body is considered cached on the clients side and
634 * a "304 Not Modified" status code is issued.
636 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
638 * A log entry is written to the cache log if the INI entry
639 * "http.cache_log" is set and the cache attempt was successful.
641 PHP_FUNCTION(http_cache_etag
)
646 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
650 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
652 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
656 /* {{{ proto string ob_etaghandler(string data, int mode)
658 * For use with ob_start(). Output buffer handler generating an ETag with
659 * the hash algorithm specified with the INI setting "http.etag_mode".
661 PHP_FUNCTION(ob_etaghandler
)
667 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
671 Z_TYPE_P(return_value
) = IS_STRING
;
672 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
676 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
678 * Sets the throttle delay and send buffer size for use with http_send() API.
679 * Provides a basic throttling mechanism, which will yield the current process
680 * resp. thread until the entity has been completely sent, though.
682 * Expects a double parameter specifying the seconds too sleep() after
683 * each chunk sent. Additionally accepts an optional int parameter
684 * representing the chunk size in bytes.
690 * # http_throttle(1, 20000);
691 * # http_throttle(0.5, 10000);
692 * # http_throttle(0.1, 2000);
693 * http_send_file('document.pdf');
697 PHP_FUNCTION(http_throttle
)
699 long chunk_size
= HTTP_SENDBUF_SIZE
;
702 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
706 HTTP_G
->send
.throttle_delay
= interval
;
707 HTTP_G
->send
.buffer_size
= chunk_size
;
711 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
713 * Redirect to the given url.
715 * The supplied url will be expanded with http_build_url(), the params array will
716 * be treated with http_build_query() and the session identification will be appended
717 * if session is true.
719 * The HTTP response code will be set according to status.
720 * You can use one of the following constants for convenience:
721 * - HTTP_REDIRECT 302 Found for GET/HEAD, else 303 See Other
722 * - HTTP_REDIRECT_PERM 301 Moved Permanently
723 * - HTTP_REDIRECT_FOUND 302 Found
724 * - HTTP_REDIRECT_POST 303 See Other
725 * - HTTP_REDIRECT_PROXY 305 Use Proxy
726 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
728 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
729 * for which redirect response code to use in which situation.
731 * To be RFC compliant, "Redirecting to <a>URL</a>." will be displayed,
732 * if the client doesn't redirect immediately, and the request method was
733 * another one than HEAD.
735 * Returns FALSE on failure, or *exits* on success.
737 * A log entry will be written to the redirect log, if the INI entry
738 * "http.redirect_log" is set and the redirect attempt was successful.
740 PHP_FUNCTION(http_redirect
)
743 size_t query_len
= 0;
744 zend_bool session
= 0, free_params
= 0;
746 long status
= HTTP_REDIRECT
;
747 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
749 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
753 #ifdef HAVE_PHP_SESSION
754 /* append session info */
758 MAKE_STD_ZVAL(params
);
761 if (PS(session_status
) == php_session_active
) {
762 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
763 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
769 /* treat params array with http_build_query() */
771 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
783 URI
= http_absolute_url(url
);
786 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
788 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
791 spprintf(&LOC
, 0, "Location: %s", URI
);
793 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
809 RETVAL_SUCCESS(http_send_status_header(status
, LOC
));
814 case HTTP_REDIRECT_PERM
:
815 case HTTP_REDIRECT_FOUND
:
816 case HTTP_REDIRECT_POST
:
817 case HTTP_REDIRECT_PROXY
:
818 case HTTP_REDIRECT_TEMP
:
823 http_error_ex(HE_NOTICE
, HTTP_E_RUNTIME
, "Unsupported redirection status code: %ld", status
);
825 if ( SG(request_info
).request_method
&&
826 strcasecmp(SG(request_info
).request_method
, "HEAD") &&
827 strcasecmp(SG(request_info
).request_method
, "GET")) {
828 status
= HTTP_REDIRECT_POST
;
830 status
= HTTP_REDIRECT_FOUND
;
835 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
839 /* {{{ proto bool http_send_data(string data)
841 * Sends raw data with support for (multiple) range requests.
843 * Returns TRUE on success, or FALSE on failure.
845 PHP_FUNCTION(http_send_data
)
849 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
853 convert_to_string_ex(&zdata
);
854 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
858 /* {{{ proto bool http_send_file(string file)
860 * Sends a file with support for (multiple) range requests.
862 * Expects a string parameter referencing the file to send.
864 * Returns TRUE on success, or FALSE on failure.
866 PHP_FUNCTION(http_send_file
)
871 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
878 RETURN_SUCCESS(http_send_file(file
));
882 /* {{{ proto bool http_send_stream(resource stream)
884 * Sends an already opened stream with support for (multiple) range requests.
886 * Expects a resource parameter referencing the stream to read from.
888 * Returns TRUE on success, or FALSE on failure.
890 PHP_FUNCTION(http_send_stream
)
895 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
899 php_stream_from_zval(file
, &zstream
);
900 RETURN_SUCCESS(http_send_stream(file
));
904 /* {{{ proto string http_chunked_decode(string encoded)
906 * Decodes a string that was HTTP-chunked encoded.
908 * Expects a chunked encoded string as parameter.
910 * Returns the decoded string on success or FALSE on failure.
912 PHP_FUNCTION(http_chunked_decode
)
914 char *encoded
= NULL
, *decoded
= NULL
;
915 size_t decoded_len
= 0;
918 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
922 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
923 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
930 /* {{{ proto object http_parse_message(string message)
932 * Parses (a) http_message(s) into a simple recursive object structure.
934 * Expects a string parameter containing a single HTTP message or
935 * several consecutive HTTP messages.
937 * Returns an hierarchical object structure of the parsed messages.
942 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
947 * [httpVersion] => 1.1
948 * [responseCode] => 200
951 * [Content-Length] => 3
955 * [parentMessage] => stdClass object
958 * [httpVersion] => 1.1
959 * [responseCode] => 302
962 * [Content-Length] => 0
966 * [parentMessage] => ...
972 PHP_FUNCTION(http_parse_message
)
976 http_message
*msg
= NULL
;
978 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
982 if ((msg
= http_message_parse(message
, message_len
))) {
983 object_init(return_value
);
984 http_message_tostruct_recursive(msg
, return_value
);
985 http_message_free(&msg
);
992 /* {{{ proto array http_parse_headers(string header)
994 * Parses HTTP headers into an associative array.
996 * Expects a string parameter containing HTTP headers.
998 * Returns an array on success, or FALSE on failure.
1003 * $headers = "content-type: text/html; charset=UTF-8\r\n".
1004 * "Server: Funky/1.0\r\n".
1005 * "Set-Cookie: foo=bar\r\n".
1006 * "Set-Cookie: baz=quux\r\n".
1007 * "Folded: works\r\n\ttoo\r\n";
1008 * print_r(http_parse_headers($headers));
1012 * [Content-Type] => text/html; chatset=UTF-8
1013 * [Server] => Funky/1.0
1014 * [Set-Cookie] => Array
1025 PHP_FUNCTION(http_parse_headers
)
1030 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
1034 array_init(return_value
);
1035 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
1036 zval_dtor(return_value
);
1042 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
1044 * Parses HTTP cookies like sent in a response into a struct.
1046 * Expects a string as parameter containing the value of a Set-Cookie response header.
1048 * Returns an stdClass olike shown in the example on success or FALSE on failure.
1053 * print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
1057 * [cookies] => Array
1071 * [domain] => example.com
1076 PHP_FUNCTION(http_parse_cookie
)
1078 char *cookie
, **allowed_extras
= NULL
;
1079 int i
= 0, cookie_len
;
1081 zval
*allowed_extras_array
= NULL
, **entry
= NULL
;
1083 http_cookie_list list
;
1085 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|la", &cookie
, &cookie_len
, &flags
, &allowed_extras_array
)) {
1089 if (allowed_extras_array
) {
1090 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1091 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1092 ZVAL_ADDREF(*entry
);
1093 convert_to_string_ex(entry
);
1094 allowed_extras
[i
] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1095 zval_ptr_dtor(entry
);
1099 if (http_parse_cookie_ex(&list
, cookie
, flags
, allowed_extras
)) {
1100 object_init(return_value
);
1101 http_cookie_list_tostruct(&list
, return_value
);
1102 http_cookie_list_dtor(&list
);
1107 if (allowed_extras
) {
1108 for (i
= 0; allowed_extras
[i
]; ++i
) {
1109 efree(allowed_extras
[i
]);
1111 efree(allowed_extras
);
1115 /* {{{ proto array http_get_request_headers(void)
1117 * Get a list of incoming HTTP headers.
1119 * Returns an associative array of incoming request headers.
1121 PHP_FUNCTION(http_get_request_headers
)
1125 array_init(return_value
);
1126 http_get_request_headers(return_value
);
1130 /* {{{ proto string http_get_request_body(void)
1132 * Get the raw request body (e.g. POST or PUT data).
1134 * This function can not be used after http_get_request_body_stream()
1135 * if the request method was another than POST.
1137 * Returns the raw request body as string on success or NULL on failure.
1139 PHP_FUNCTION(http_get_request_body
)
1146 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
1147 RETURN_STRINGL(body
, (int) length
, 0);
1154 /* {{{ proto resource http_get_request_body_stream(void)
1156 * Create a stream to read the raw request body (e.g. POST or PUT data).
1158 * This function can only be used once if the request method was another than POST.
1160 * Returns the raw request body as stream on success or NULL on failure.
1162 PHP_FUNCTION(http_get_request_body_stream
)
1168 if ((s
= http_get_request_body_stream())) {
1169 php_stream_to_zval(s
, return_value
);
1171 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to create request body stream");
1177 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
1179 * Match an incoming HTTP header.
1181 * Expects two string parameters representing the header name (case-insensitive)
1182 * and the header value that should be compared. The case sensitivity of the
1183 * header value depends on the additional optional bool parameter accepted.
1185 * Returns TRUE if header value matches, else FALSE.
1187 PHP_FUNCTION(http_match_request_header
)
1189 char *header
, *value
;
1190 int header_len
, value_len
;
1191 zend_bool match_case
= 0;
1193 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
1197 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
1202 #ifdef HTTP_HAVE_CURL
1204 #define RETVAL_RESPONSE_OR_BODY(request) \
1208 /* check if only the body should be returned */ \
1209 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void **) &bodyonly)) && zval_is_true(*bodyonly)) { \
1210 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1213 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1214 http_message_free(&msg); \
1217 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1221 /* {{{ proto string http_get(string url[, array options[, array &info]])
1223 * Performs an HTTP GET request on the supplied url.
1225 * The second parameter, if set, is expected to be an associative
1226 * array where the following keys will be recognized:
1228 * - redirect: int, whether and how many redirects to follow
1229 * - unrestrictedauth: bool, whether to continue sending credentials on
1230 * redirects to a different host
1231 * - proxyhost: string, proxy host in "host[:port]" format
1232 * - proxyport: int, use another proxy port as specified in proxyhost
1233 * - proxyauth: string, proxy credentials in "user:pass" format
1234 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1235 * - httpauth: string, http credentials in "user:pass" format
1236 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1237 * - compress: bool, whether to allow gzip/deflate content encoding
1238 * - port: int, use another port as specified in the url
1239 * - referer: string, the referer to send
1240 * - useragent: string, the user agent to send
1241 * (defaults to PECL::HTTP/version (PHP/version)))
1242 * - headers: array, list of custom headers as associative array
1243 * like array("header" => "value")
1244 * - cookies: array, list of cookies as associative array
1245 * like array("cookie" => "value")
1246 * - encodecookies: bool, whether to urlencode the cookies (default: true)
1247 * - resetcookies: bool, wheter to reset the cookies
1248 * - cookiestore: string, path to a file where cookies are/will be stored
1249 * - cookiesession: bool, accept (true) or reset (false) sessioncookies
1250 * - resume: int, byte offset to start the download from;
1251 * if the server supports ranges
1252 * - maxfilesize: int, maximum file size that should be downloaded;
1253 * has no effect, if the size of the requested entity is not known
1254 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1255 * - etag: string, quoted etag for If-(None-)Match header
1256 * - timeout: int, seconds the request may take
1257 * - connecttimeout: int, seconds the connect may take
1258 * - onprogress: mixed, progress callback
1259 * - interface: string, outgoing network interface (ifname, ip or hostname)
1260 * - portrange: array, 2 integers specifying outgoing portrange to try
1261 * - ssl: array, with the following options:
1262 * cert: string, path to certificate
1263 * certtype: string, type of certificate
1264 * certpasswd: string, password for certificate
1265 * key: string, path to key
1266 * keytype: string, type of key
1267 * keypasswd: string, pasword for key
1268 * engine: string, ssl engine to use
1269 * version: int, ssl version to use
1270 * verifypeer: bool, whether to verify the peer
1271 * verifyhost: bool whether to verify the host
1272 * cipher_list: string, list of allowed ciphers
1275 * random_file: string
1279 * The optional third parameter will be filled with some additional information
1280 * in form of an associative array, if supplied, like the following example:
1284 * 'effective_url' => 'http://www.example.com/',
1285 * 'response_code' => 302,
1286 * 'connect_code' => 0,
1288 * 'total_time' => 0.212348,
1289 * 'namelookup_time' => 0.038296,
1290 * 'connect_time' => 0.104144,
1291 * 'pretransfer_time' => 0.104307,
1292 * 'starttransfer_time' => 0.212077,
1293 * 'redirect_time' => 0,
1294 * 'redirect_count' => 0,
1295 * 'size_upload' => 0,
1296 * 'size_download' => 218,
1297 * 'speed_download' => 1026,
1298 * 'speed_upload' => 0,
1299 * 'header_size' => 307,
1300 * 'request_size' => 103,
1301 * 'ssl_verifyresult' => 0,
1314 * 'content_length_download' => 218,
1315 * 'content_length_upload' => 0,
1316 * 'content_type' => 'text/html',
1317 * 'httpauth_avail' => 0,
1318 * 'proxyauth_avail' => 0,
1319 * 'num_connects' => 1,
1326 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1328 PHP_FUNCTION(http_get
)
1330 zval
*options
= NULL
, *info
= NULL
;
1333 http_request request
;
1335 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1346 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
1347 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1348 http_request_exec(&request
);
1350 http_request_info(&request
, Z_ARRVAL_P(info
));
1352 RETVAL_RESPONSE_OR_BODY(request
);
1354 http_request_dtor(&request
);
1358 /* {{{ proto string http_head(string url[, array options[, array &info]])
1360 * Performs an HTTP HEAD request on the supplied url.
1362 * See http_get() for a full list of available parameters and options.
1364 * Returns the HTTP response as string on success, or FALSE on failure.
1366 PHP_FUNCTION(http_head
)
1368 zval
*options
= NULL
, *info
= NULL
;
1371 http_request request
;
1373 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1384 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
1385 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1386 http_request_exec(&request
);
1388 http_request_info(&request
, Z_ARRVAL_P(info
));
1390 RETVAL_RESPONSE_OR_BODY(request
);
1392 http_request_dtor(&request
);
1396 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1398 * Performs an HTTP POST request on the supplied url.
1400 * Expects a string as second parameter containing the pre-encoded post data.
1401 * See http_get() for a full list of available parameters and options.
1403 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1405 PHP_FUNCTION(http_post_data
)
1407 zval
*options
= NULL
, *info
= NULL
;
1408 char *URL
, *postdata
;
1409 int postdata_len
, URL_len
;
1410 http_request_body body
;
1411 http_request request
;
1413 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1424 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1425 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
1426 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1427 http_request_exec(&request
);
1429 http_request_info(&request
, Z_ARRVAL_P(info
));
1431 RETVAL_RESPONSE_OR_BODY(request
);
1433 http_request_dtor(&request
);
1437 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1439 * Performs an HTTP POST request on the supplied url.
1441 * Expects an associative array as second parameter, which will be
1442 * www-form-urlencoded. See http_get() for a full list of available options.
1444 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1446 PHP_FUNCTION(http_post_fields
)
1448 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
1451 http_request_body body
;
1452 http_request request
;
1454 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
1458 if (!http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
1469 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1470 request
.body
= &body
;
1471 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1472 http_request_exec(&request
);
1474 http_request_info(&request
, Z_ARRVAL_P(info
));
1476 RETVAL_RESPONSE_OR_BODY(request
);
1478 http_request_dtor(&request
);
1482 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1484 * Performs an HTTP PUT request on the supplied url.
1486 * Expects the second parameter to be a string referencing the file to upload.
1487 * See http_get() for a full list of available options.
1489 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1491 PHP_FUNCTION(http_put_file
)
1495 zval
*options
= NULL
, *info
= NULL
;
1497 php_stream_statbuf ssb
;
1498 http_request_body body
;
1499 http_request request
;
1501 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1505 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, HTTP_DEFAULT_STREAM_CONTEXT
))) {
1508 if (php_stream_stat(stream
, &ssb
)) {
1509 php_stream_close(stream
);
1520 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1522 body
.size
= ssb
.sb
.st_size
;
1524 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1525 request
.body
= &body
;
1526 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1527 http_request_exec(&request
);
1529 http_request_info(&request
, Z_ARRVAL_P(info
));
1531 RETVAL_RESPONSE_OR_BODY(request
);
1533 http_request_body_dtor(&body
);
1534 request
.body
= NULL
;
1535 http_request_dtor(&request
);
1539 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1541 * Performs an HTTP PUT request on the supplied url.
1543 * Expects the second parameter to be a resource referencing an already
1544 * opened stream, from which the data to upload should be read.
1545 * See http_get() for a full list of available options.
1547 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1549 PHP_FUNCTION(http_put_stream
)
1551 zval
*resource
, *options
= NULL
, *info
= NULL
;
1555 php_stream_statbuf ssb
;
1556 http_request_body body
;
1557 http_request request
;
1559 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1563 php_stream_from_zval(stream
, &resource
);
1564 if (php_stream_stat(stream
, &ssb
)) {
1575 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1577 body
.size
= ssb
.sb
.st_size
;
1579 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1580 request
.body
= &body
;
1581 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1582 http_request_exec(&request
);
1584 http_request_info(&request
, Z_ARRVAL_P(info
));
1586 RETVAL_RESPONSE_OR_BODY(request
);
1588 request
.body
= NULL
;
1589 http_request_dtor(&request
);
1592 #endif /* HTTP_HAVE_CURL */
1595 /* {{{ proto int http_request_method_register(string method)
1597 * Register a custom request method.
1599 * Expects a string parameter containing the request method name to register.
1601 * Returns the ID of the request method on success, or FALSE on failure.
1603 PHP_FUNCTION(http_request_method_register
)
1609 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1612 if ((existing
= http_request_method_exists(1, 0, method
))) {
1613 RETURN_LONG((long) existing
);
1616 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1620 /* {{{ proto bool http_request_method_unregister(mixed method)
1622 * Unregister a previously registered custom request method.
1624 * Expects either the request method name or ID.
1626 * Returns TRUE on success, or FALSE on failure.
1628 PHP_FUNCTION(http_request_method_unregister
)
1632 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1636 switch (Z_TYPE_P(method
))
1639 convert_to_string(method
);
1641 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1642 convert_to_long(method
);
1645 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1649 ZVAL_LONG(method
, (long)mn
);
1652 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1659 /* {{{ proto int http_request_method_exists(mixed method)
1661 * Check if a request method is registered (or available by default).
1663 * Expects either the request method name or ID as parameter.
1665 * Returns TRUE if the request method is known, else FALSE.
1667 PHP_FUNCTION(http_request_method_exists
)
1672 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1676 switch (Z_TYPE_P(method
))
1679 convert_to_string(method
);
1681 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1682 convert_to_long(method
);
1684 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1687 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method
), NULL
));
1695 /* {{{ proto string http_request_method_name(int method)
1697 * Get the literal string representation of a standard or registered request method.
1699 * Expects the request method ID as parameter.
1701 * Returns the request method name as string on success, or FALSE on failure.
1703 PHP_FUNCTION(http_request_method_name
)
1708 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1712 RETURN_STRING(estrdup(http_request_method_name((int) method
)), 0);
1718 #ifdef HTTP_HAVE_ZLIB
1720 /* {{{ proto string http_deflate(string data[, int flags = 0])
1722 * Compress data with gzip, zlib AKA deflate or raw deflate encoding.
1724 * Expects the first parameter to be a string containing the data that should
1727 * Returns the encoded string on success, or NULL on failure.
1729 PHP_FUNCTION(http_deflate
)
1737 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &data
, &data_len
, &flags
)) {
1741 if (SUCCESS
== http_encoding_deflate(flags
, data
, data_len
, &encoded
, &encoded_len
)) {
1742 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1748 /* {{{ proto string http_inflate(string data)
1750 * Decompress data compressed with either gzip, deflate AKA zlib or raw
1753 * Expects a string as parameter containing the compressed data.
1755 * Returns the decoded string on success, or NULL on failure.
1757 PHP_FUNCTION(http_inflate
)
1764 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1768 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1769 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1775 /* {{{ proto string ob_deflatehandler(string data, int mode)
1777 * For use with ob_start(). The deflate output buffer handler can only be used once.
1778 * It conflicts with ob_gzhandler and zlib.output_compression as well and should
1779 * not be used after ext/mbstrings mb_output_handler and ext/sessions URL-Rewriter (AKA
1780 * session.use_trans_sid).
1782 PHP_FUNCTION(ob_deflatehandler
)
1788 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1792 http_ob_deflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1793 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1797 /* {{{ proto string ob_inflatehandler(string data, int mode)
1799 * For use with ob_start(). Same restrictions as with ob_deflatehandler apply.
1801 PHP_FUNCTION(ob_inflatehandler
)
1807 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1811 http_ob_inflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1812 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1816 #endif /* HTTP_HAVE_ZLIB */
1819 /* {{{ proto int http_support([int feature = 0])
1821 * Check for feature that require external libraries.
1823 * Accepts an optional in parameter specifying which feature to probe for.
1824 * If the parameter is 0 or omitted, the return value contains a bitmask of
1825 * all supported features that depend on external libraries.
1827 * Available features to probe for are:
1829 * <li> HTTP_SUPPORT: always set
1830 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1831 * and HTTP requests can be issued
1832 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1833 * and SSL requests can be issued
1834 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1835 * and compressed HTTP responses can be decoded
1836 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1837 * and the HttpResponse::guessContentType() method is usable
1840 * Returns int, whether requested feature is supported, or a bitmask with
1841 * all supported features.
1843 PHP_FUNCTION(http_support
)
1849 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
1850 RETVAL_LONG(http_support(feature
));
1855 PHP_FUNCTION(http_test
)
1864 * vim600: noet sw=4 ts=4 fdm=marker
1865 * vim<600: noet sw=4 ts=4