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
));
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
));
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
)))) {
138 php_url_free(new_url
);
140 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url
));
146 if (z_composed_url
) {
147 http_build_url(flags
, old_url
, new_url
, &composed_url
, &url_str
, &url_len
);
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);
154 if (composed_url
->user
) {
155 add_assoc_string(z_composed_url
, "user", composed_url
->user
, 1);
157 if (composed_url
->pass
) {
158 add_assoc_string(z_composed_url
, "pass", composed_url
->pass
, 1);
160 if (composed_url
->host
) {
161 add_assoc_string(z_composed_url
, "host", composed_url
->host
, 1);
163 if (composed_url
->port
) {
164 add_assoc_long(z_composed_url
, "port", composed_url
->port
);
166 if (composed_url
->path
) {
167 add_assoc_string(z_composed_url
, "path", composed_url
->path
, 1);
169 if (composed_url
->query
) {
170 add_assoc_string(z_composed_url
, "query", composed_url
->query
, 1);
172 if (composed_url
->fragment
) {
173 add_assoc_string(z_composed_url
, "fragment", composed_url
->fragment
, 1);
175 php_url_free(composed_url
);
177 http_build_url(flags
, old_url
, new_url
, NULL
, &url_str
, &url_len
);
181 php_url_free(new_url
);
183 php_url_free(old_url
);
185 RETURN_STRINGL(url_str
, url_len
, 0);
189 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
191 * Opponent to parse_str().
193 * Expects an array as first argument which represents the parts of the query string to build.
194 * Accepts a string as optional second parameter containing a top-level prefix to use.
195 * The optional third parameter should specify an argument separator to use (by default the
196 * INI setting arg_separator.output will be used, or "&" if neither is set).
198 * Returns the built query as string on success or FALSE on failure.
200 PHP_FUNCTION(http_build_str
)
203 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
204 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
207 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
212 arg_sep
= HTTP_URL_ARGSEP
;
213 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
216 phpstr_init(&formstr
);
217 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), &formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
222 phpstr_dtor(&formstr
);
226 RETURN_PHPSTR_VAL(&formstr
);
230 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
233 if ((result = http_negotiate_ ##type(supported))) { \
238 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
239 RETVAL_STRINGL(key, key_len-1, 0); \
245 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
248 zend_hash_destroy(result); \
249 FREE_HASHTABLE(result); \
254 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
255 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
256 RETVAL_ZVAL(*value, 1, 0); \
265 FOREACH_VAL(pos, supported, value) { \
266 convert_to_string_ex(value); \
267 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
274 /* {{{ proto string http_negotiate_language(array supported[, array &result])
276 * This function negotiates the clients preferred language based on its
277 * Accept-Language HTTP header. The qualifier is recognized and languages
278 * without qualifier are rated highest. The qualifier will be decreased by
279 * 10% for partial matches (i.e. matching primary language).
281 * Expects an array as parameter containing the supported languages as values.
282 * If the optional second parameter is supplied, it will be filled with an
283 * array containing the negotiation results.
285 * Returns the negotiated language or the default language (i.e. first array entry)
301 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
307 PHP_FUNCTION(http_negotiate_language
)
309 zval
*supported
, *rs_array
= NULL
;
311 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
317 array_init(rs_array
);
320 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
324 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
326 * This function negotiates the clients preferred charset based on its
327 * Accept-Charset HTTP header. The qualifier is recognized and charsets
328 * without qualifier are rated highest.
330 * Expects an array as parameter containing the supported charsets as values.
331 * If the optional second parameter is supplied, it will be filled with an
332 * array containing the negotiation results.
334 * Returns the negotiated charset or the default charset (i.e. first array entry)
341 * 'iso-8859-1', // default
347 * $pref = http_negotiate_charset($charsets, $result);
349 * if (strcmp($pref, 'iso-8859-1')) {
350 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
351 * iconv_set_encoding('output_encoding', $pref);
352 * ob_start('ob_iconv_handler');
359 PHP_FUNCTION(http_negotiate_charset
)
361 zval
*supported
, *rs_array
= NULL
;
363 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
369 array_init(rs_array
);
372 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
376 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
378 * This function negotiates the clients preferred content type based on its
379 * Accept HTTP header. The qualifier is recognized and content types
380 * without qualifier are rated highest.
382 * Expects an array as parameter containing the supported content types as values.
383 * If the optional second parameter is supplied, it will be filled with an
384 * array containing the negotiation results.
386 * Returns the negotiated content type or the default content type
387 * (i.e. first array entry) if none match.
392 * $ctypes = array('application/xhtml+xml', 'text/html');
393 * http_send_content_type(http_negotiate_content_type($ctypes));
397 PHP_FUNCTION(http_negotiate_content_type
)
399 zval
*supported
, *rs_array
= NULL
;
401 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
407 array_init(rs_array
);
410 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
414 /* {{{ proto bool http_send_status(int status)
416 * Send HTTP status code.
418 * Expects an HTTP status code as parameter.
420 * Returns TRUE on success or FALSE on failure.
422 PHP_FUNCTION(http_send_status
)
426 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
429 if (status
< 100 || status
> 510) {
430 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
434 RETURN_SUCCESS(http_send_status(status
));
438 /* {{{ proto bool http_send_last_modified([int timestamp])
440 * Send a "Last-Modified" header with a valid HTTP date.
442 * Accepts a unix timestamp, converts it to a valid HTTP date and
443 * sends it as "Last-Modified" HTTP header. If timestamp is
444 * omitted, the current time will be sent.
446 * Returns TRUE on success or FALSE on failure.
448 PHP_FUNCTION(http_send_last_modified
)
452 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
457 t
= (long) HTTP_GET_REQUEST_TIME();
460 RETURN_SUCCESS(http_send_last_modified(t
));
464 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
466 * Send the Content-Type of the sent entity. This is particularly important
467 * if you use the http_send() API.
469 * Accepts an optional string parameter containing the desired content type
470 * (primary/secondary).
472 * Returns TRUE on success or FALSE on failure.
474 PHP_FUNCTION(http_send_content_type
)
476 char *ct
= "application/x-octetstream";
477 int ct_len
= lenof("application/x-octetstream");
479 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
483 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
487 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
489 * Send the Content-Disposition. The Content-Disposition header is very useful
490 * if the data actually sent came from a file or something similar, that should
491 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
493 * Expects a string parameter specifying the file name the "Save as..." dialog
494 * should display. Optionally accepts a bool parameter, which, if set to true
495 * and the user agent knows how to handle the content type, will probably not
496 * cause the popup window to be shown.
498 * Returns TRUE on success or FALSE on failure.
500 PHP_FUNCTION(http_send_content_disposition
)
504 zend_bool send_inline
= 0;
506 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
509 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
513 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
515 * Matches the given unix timestamp against the clients "If-Modified-Since"
516 * resp. "If-Unmodified-Since" HTTP headers.
518 * Accepts a unix timestamp which should be matched. Optionally accepts an
519 * additional bool parameter, which if set to true will check the header
520 * usually used to validate HTTP ranges. If timestamp is omitted, the
521 * current time will be used.
523 * Returns TRUE if timestamp represents an earlier date than the header,
526 PHP_FUNCTION(http_match_modified
)
529 zend_bool for_range
= 0;
531 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
535 // current time if not supplied (senseless though)
537 t
= (long) HTTP_GET_REQUEST_TIME();
541 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
543 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
547 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
549 * Matches the given ETag against the clients "If-Match" resp.
550 * "If-None-Match" HTTP headers.
552 * Expects a string parameter containing the ETag to compare. Optionally
553 * accepts a bool parameter, which, if set to true, will check the header
554 * usually used to validate HTTP ranges.
556 * Returns TRUE if ETag matches or the header contained the asterisk ("*"),
559 PHP_FUNCTION(http_match_etag
)
563 zend_bool for_range
= 0;
565 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
570 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
572 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
576 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
578 * Attempts to cache the sent entity by its last modification date.
580 * Accepts a unix timestamp as parameter which is handled as follows:
582 * If timestamp_or_expires is greater than 0, it is handled as timestamp
583 * and will be sent as date of last modification. If it is 0 or omitted,
584 * the current time will be sent as Last-Modified date. If it's negative,
585 * it is handled as expiration time in seconds, which means that if the
586 * requested last modification date is not between the calculated timespan,
587 * the Last-Modified header is updated and the actual body will be sent.
589 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
591 * A log entry will be written to the cache log if the INI entry
592 * http.cache_log is set and the cache attempt was successful.
594 PHP_FUNCTION(http_cache_last_modified
)
596 long last_modified
= 0, send_modified
= 0, t
;
599 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
603 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
605 t
= (long) HTTP_GET_REQUEST_TIME();
608 if (!last_modified
) {
609 /* does the client have? (att: caching "forever") */
610 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
611 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
612 /* send current time */
616 /* negative value is supposed to be expiration time */
617 } else if (last_modified
< 0) {
620 /* send supplied time explicitly */
622 send_modified
= last_modified
;
625 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
629 /* {{{ proto bool http_cache_etag([string etag])
631 * Attempts to cache the sent entity by its ETag, either supplied or generated
632 * by the hash algorithm specified by the INI setting "http.etag_mode".
634 * If the clients "If-None-Match" header matches the supplied/calculated
635 * ETag, the body is considered cached on the clients side and
636 * a "304 Not Modified" status code is issued.
638 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
640 * A log entry is written to the cache log if the INI entry
641 * "http.cache_log" is set and the cache attempt was successful.
643 PHP_FUNCTION(http_cache_etag
)
648 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
652 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
654 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
658 /* {{{ proto string ob_etaghandler(string data, int mode)
660 * For use with ob_start(). Output buffer handler generating an ETag with
661 * the hash algorithm specified with the INI setting "http.etag_mode".
663 PHP_FUNCTION(ob_etaghandler
)
669 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
673 Z_TYPE_P(return_value
) = IS_STRING
;
674 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
678 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
680 * Sets the throttle delay and send buffer size for use with http_send() API.
681 * Provides a basic throttling mechanism, which will yield the current process
682 * resp. thread until the entity has been completely sent, though.
684 * Expects a double parameter specifying the seconds too sleep() after
685 * each chunk sent. Additionally accepts an optional int parameter
686 * representing the chunk size in bytes.
692 * # http_throttle(1, 20000);
693 * # http_throttle(0.5, 10000);
694 * # http_throttle(0.1, 2000);
695 * http_send_file('document.pdf');
699 PHP_FUNCTION(http_throttle
)
701 long chunk_size
= HTTP_SENDBUF_SIZE
;
704 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
708 HTTP_G
->send
.throttle_delay
= interval
;
709 HTTP_G
->send
.buffer_size
= chunk_size
;
713 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
715 * Redirect to the given url.
717 * The supplied url will be expanded with http_build_url(), the params array will
718 * be treated with http_build_query() and the session identification will be appended
719 * if session is true.
721 * The HTTP response code will be set according to status.
722 * You can use one of the following constants for convenience:
723 * - HTTP_REDIRECT 302 Found for GET/HEAD, else 303 See Other
724 * - HTTP_REDIRECT_PERM 301 Moved Permanently
725 * - HTTP_REDIRECT_FOUND 302 Found
726 * - HTTP_REDIRECT_POST 303 See Other
727 * - HTTP_REDIRECT_PROXY 305 Use Proxy
728 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
730 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
731 * for which redirect response code to use in which situation.
733 * To be RFC compliant, "Redirecting to <a>URL</a>." will be displayed,
734 * if the client doesn't redirect immediately, and the request method was
735 * another one than HEAD.
737 * Returns FALSE on failure, or *exits* on success.
739 * A log entry will be written to the redirect log, if the INI entry
740 * "http.redirect_log" is set and the redirect attempt was successful.
742 PHP_FUNCTION(http_redirect
)
745 size_t query_len
= 0;
746 zend_bool session
= 0, free_params
= 0;
748 long status
= HTTP_REDIRECT
;
749 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
751 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
755 #ifdef HAVE_PHP_SESSION
756 /* append session info */
760 MAKE_STD_ZVAL(params
);
763 if (PS(session_status
) == php_session_active
) {
764 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
765 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
771 /* treat params array with http_build_query() */
773 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
785 URI
= http_absolute_url(url
);
788 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
790 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
793 spprintf(&LOC
, 0, "Location: %s", URI
);
795 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
811 RETVAL_SUCCESS(http_send_status_header(status
, LOC
));
816 case HTTP_REDIRECT_PERM
:
817 case HTTP_REDIRECT_FOUND
:
818 case HTTP_REDIRECT_POST
:
819 case HTTP_REDIRECT_PROXY
:
820 case HTTP_REDIRECT_TEMP
:
825 http_error_ex(HE_NOTICE
, HTTP_E_RUNTIME
, "Unsupported redirection status code: %ld", status
);
827 if ( SG(request_info
).request_method
&&
828 strcasecmp(SG(request_info
).request_method
, "HEAD") &&
829 strcasecmp(SG(request_info
).request_method
, "GET")) {
830 status
= HTTP_REDIRECT_POST
;
832 status
= HTTP_REDIRECT_FOUND
;
837 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
841 /* {{{ proto bool http_send_data(string data)
843 * Sends raw data with support for (multiple) range requests.
845 * Returns TRUE on success, or FALSE on failure.
847 PHP_FUNCTION(http_send_data
)
851 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
855 convert_to_string_ex(&zdata
);
856 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
860 /* {{{ proto bool http_send_file(string file)
862 * Sends a file with support for (multiple) range requests.
864 * Expects a string parameter referencing the file to send.
866 * Returns TRUE on success, or FALSE on failure.
868 PHP_FUNCTION(http_send_file
)
873 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
880 RETURN_SUCCESS(http_send_file(file
));
884 /* {{{ proto bool http_send_stream(resource stream)
886 * Sends an already opened stream with support for (multiple) range requests.
888 * Expects a resource parameter referencing the stream to read from.
890 * Returns TRUE on success, or FALSE on failure.
892 PHP_FUNCTION(http_send_stream
)
897 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
901 php_stream_from_zval(file
, &zstream
);
902 RETURN_SUCCESS(http_send_stream(file
));
906 /* {{{ proto string http_chunked_decode(string encoded)
908 * Decodes a string that was HTTP-chunked encoded.
910 * Expects a chunked encoded string as parameter.
912 * Returns the decoded string on success or FALSE on failure.
914 PHP_FUNCTION(http_chunked_decode
)
916 char *encoded
= NULL
, *decoded
= NULL
;
917 size_t decoded_len
= 0;
920 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
924 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
925 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
932 /* {{{ proto object http_parse_message(string message)
934 * Parses (a) http_message(s) into a simple recursive object structure.
936 * Expects a string parameter containing a single HTTP message or
937 * several consecutive HTTP messages.
939 * Returns an hierarchical object structure of the parsed messages.
944 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
949 * [httpVersion] => 1.1
950 * [responseCode] => 200
953 * [Content-Length] => 3
957 * [parentMessage] => stdClass object
960 * [httpVersion] => 1.1
961 * [responseCode] => 302
964 * [Content-Length] => 0
968 * [parentMessage] => ...
974 PHP_FUNCTION(http_parse_message
)
978 http_message
*msg
= NULL
;
980 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
984 if ((msg
= http_message_parse(message
, message_len
))) {
985 object_init(return_value
);
986 http_message_tostruct_recursive(msg
, return_value
);
987 http_message_free(&msg
);
994 /* {{{ proto array http_parse_headers(string header)
996 * Parses HTTP headers into an associative array.
998 * Expects a string parameter containing HTTP headers.
1000 * Returns an array on success, or FALSE on failure.
1005 * $headers = "content-type: text/html; charset=UTF-8\r\n".
1006 * "Server: Funky/1.0\r\n".
1007 * "Set-Cookie: foo=bar\r\n".
1008 * "Set-Cookie: baz=quux\r\n".
1009 * "Folded: works\r\n\ttoo\r\n";
1010 * print_r(http_parse_headers($headers));
1014 * [Content-Type] => text/html; chatset=UTF-8
1015 * [Server] => Funky/1.0
1016 * [Set-Cookie] => Array
1027 PHP_FUNCTION(http_parse_headers
)
1032 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
1036 array_init(return_value
);
1037 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
1038 zval_dtor(return_value
);
1039 http_error(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Failed to parse headers");
1045 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
1047 * Parses HTTP cookies like sent in a response into a struct.
1049 * Expects a string as parameter containing the value of a Set-Cookie response header.
1051 * Returns an stdClass olike shown in the example on success or FALSE on failure.
1056 * print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
1060 * [cookies] => Array
1074 * [domain] => example.com
1079 PHP_FUNCTION(http_parse_cookie
)
1081 char *cookie
, **allowed_extras
= NULL
;
1082 int i
= 0, cookie_len
;
1084 zval
*allowed_extras_array
= NULL
, **entry
= NULL
;
1086 http_cookie_list list
;
1088 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|la", &cookie
, &cookie_len
, &flags
, &allowed_extras_array
)) {
1092 if (allowed_extras_array
) {
1093 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1094 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1095 ZVAL_ADDREF(*entry
);
1096 convert_to_string_ex(entry
);
1097 allowed_extras
[i
] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1098 zval_ptr_dtor(entry
);
1102 if (http_parse_cookie_ex(&list
, cookie
, flags
, allowed_extras
)) {
1103 object_init(return_value
);
1104 http_cookie_list_tostruct(&list
, return_value
);
1105 http_cookie_list_dtor(&list
);
1110 if (allowed_extras
) {
1111 for (i
= 0; allowed_extras
[i
]; ++i
) {
1112 efree(allowed_extras
[i
]);
1114 efree(allowed_extras
);
1118 /* {{{ proto array http_get_request_headers(void)
1120 * Get a list of incoming HTTP headers.
1122 * Returns an associative array of incoming request headers.
1124 PHP_FUNCTION(http_get_request_headers
)
1128 array_init(return_value
);
1129 http_get_request_headers(return_value
);
1133 /* {{{ proto string http_get_request_body(void)
1135 * Get the raw request body (e.g. POST or PUT data).
1137 * This function can not be used after http_get_request_body_stream()
1138 * if the request method was another than POST.
1140 * Returns the raw request body as string on success or NULL on failure.
1142 PHP_FUNCTION(http_get_request_body
)
1149 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
1150 RETURN_STRINGL(body
, (int) length
, 0);
1157 /* {{{ proto resource http_get_request_body_stream(void)
1159 * Create a stream to read the raw request body (e.g. POST or PUT data).
1161 * This function can only be used once if the request method was another than POST.
1163 * Returns the raw request body as stream on success or NULL on failure.
1165 PHP_FUNCTION(http_get_request_body_stream
)
1171 if ((s
= http_get_request_body_stream())) {
1172 php_stream_to_zval(s
, return_value
);
1174 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to create request body stream");
1180 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
1182 * Match an incoming HTTP header.
1184 * Expects two string parameters representing the header name (case-insensitive)
1185 * and the header value that should be compared. The case sensitivity of the
1186 * header value depends on the additional optional bool parameter accepted.
1188 * Returns TRUE if header value matches, else FALSE.
1190 PHP_FUNCTION(http_match_request_header
)
1192 char *header
, *value
;
1193 int header_len
, value_len
;
1194 zend_bool match_case
= 0;
1196 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
1200 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
1205 #ifdef HTTP_HAVE_CURL
1207 #define RETVAL_RESPONSE_OR_BODY(request) \
1211 /* check if only the body should be returned */ \
1212 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
1213 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1216 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1217 http_message_free(&msg); \
1220 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1224 /* {{{ proto string http_get(string url[, array options[, array &info]])
1226 * Performs an HTTP GET request on the supplied url.
1228 * The second parameter, if set, is expected to be an associative
1229 * array where the following keys will be recognized:
1231 * - redirect: int, whether and how many redirects to follow
1232 * - unrestrictedauth: bool, whether to continue sending credentials on
1233 * redirects to a different host
1234 * - proxyhost: string, proxy host in "host[:port]" format
1235 * - proxyport: int, use another proxy port as specified in proxyhost
1236 * - proxytype: int, HTTP_PROXY_HTTP, SOCKS4 or SOCKS5
1237 * - proxyauth: string, proxy credentials in "user:pass" format
1238 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1239 * - httpauth: string, http credentials in "user:pass" format
1240 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1241 * - compress: bool, whether to allow gzip/deflate content encoding
1242 * - port: int, use another port as specified in the url
1243 * - referer: string, the referer to send
1244 * - useragent: string, the user agent to send
1245 * (defaults to PECL::HTTP/version (PHP/version)))
1246 * - headers: array, list of custom headers as associative array
1247 * like array("header" => "value")
1248 * - cookies: array, list of cookies as associative array
1249 * like array("cookie" => "value")
1250 * - encodecookies: bool, whether to urlencode the cookies (default: true)
1251 * - resetcookies: bool, wheter to reset the cookies
1252 * - cookiestore: string, path to a file where cookies are/will be stored
1253 * - cookiesession: bool, accept (true) or reset (false) sessioncookies
1254 * - resume: int, byte offset to start the download from;
1255 * if the server supports ranges
1256 * - range: array, array of arrays, each containing two integers,
1257 * specifying the ranges to download if server support is
1258 * given; only recognized if the resume option is empty
1259 * - maxfilesize: int, maximum file size that should be downloaded;
1260 * has no effect, if the size of the requested entity is not known
1261 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1262 * - etag: string, quoted etag for If-(None-)Match header
1263 * - timeout: int, seconds the request may take
1264 * - connecttimeout: int, seconds the connect may take
1265 * - onprogress: mixed, progress callback
1266 * - interface: string, outgoing network interface (ifname, ip or hostname)
1267 * - portrange: array, 2 integers specifying outgoing portrange to try
1268 * - ssl: array, with the following options:
1269 * cert: string, path to certificate
1270 * certtype: string, type of certificate
1271 * certpasswd: string, password for certificate
1272 * key: string, path to key
1273 * keytype: string, type of key
1274 * keypasswd: string, pasword for key
1275 * engine: string, ssl engine to use
1276 * version: int, ssl version to use
1277 * verifypeer: bool, whether to verify the peer
1278 * verifyhost: bool whether to verify the host
1279 * cipher_list: string, list of allowed ciphers
1282 * random_file: string
1286 * The optional third parameter will be filled with some additional information
1287 * in form of an associative array, if supplied, like the following example:
1291 * 'effective_url' => 'http://www.example.com/',
1292 * 'response_code' => 302,
1293 * 'connect_code' => 0,
1295 * 'total_time' => 0.212348,
1296 * 'namelookup_time' => 0.038296,
1297 * 'connect_time' => 0.104144,
1298 * 'pretransfer_time' => 0.104307,
1299 * 'starttransfer_time' => 0.212077,
1300 * 'redirect_time' => 0,
1301 * 'redirect_count' => 0,
1302 * 'size_upload' => 0,
1303 * 'size_download' => 218,
1304 * 'speed_download' => 1026,
1305 * 'speed_upload' => 0,
1306 * 'header_size' => 307,
1307 * 'request_size' => 103,
1308 * 'ssl_verifyresult' => 0,
1321 * 'content_length_download' => 218,
1322 * 'content_length_upload' => 0,
1323 * 'content_type' => 'text/html',
1324 * 'httpauth_avail' => 0,
1325 * 'proxyauth_avail' => 0,
1326 * 'num_connects' => 1,
1333 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1335 PHP_FUNCTION(http_get
)
1337 zval
*options
= NULL
, *info
= NULL
;
1340 http_request request
;
1342 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1353 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
1354 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1355 http_request_exec(&request
);
1357 http_request_info(&request
, Z_ARRVAL_P(info
));
1359 RETVAL_RESPONSE_OR_BODY(request
);
1361 http_request_dtor(&request
);
1365 /* {{{ proto string http_head(string url[, array options[, array &info]])
1367 * Performs an HTTP HEAD request on the supplied url.
1369 * See http_get() for a full list of available parameters and options.
1371 * Returns the HTTP response as string on success, or FALSE on failure.
1373 PHP_FUNCTION(http_head
)
1375 zval
*options
= NULL
, *info
= NULL
;
1378 http_request request
;
1380 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1391 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
1392 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1393 http_request_exec(&request
);
1395 http_request_info(&request
, Z_ARRVAL_P(info
));
1397 RETVAL_RESPONSE_OR_BODY(request
);
1399 http_request_dtor(&request
);
1403 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1405 * Performs an HTTP POST request on the supplied url.
1407 * Expects a string as second parameter containing the pre-encoded post data.
1408 * See http_get() for a full list of available parameters and options.
1410 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1412 PHP_FUNCTION(http_post_data
)
1414 zval
*options
= NULL
, *info
= NULL
;
1415 char *URL
, *postdata
;
1416 int postdata_len
, URL_len
;
1417 http_request_body body
;
1418 http_request request
;
1420 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1431 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1432 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
1433 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1434 http_request_exec(&request
);
1436 http_request_info(&request
, Z_ARRVAL_P(info
));
1438 RETVAL_RESPONSE_OR_BODY(request
);
1440 http_request_dtor(&request
);
1444 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1446 * Performs an HTTP POST request on the supplied url.
1448 * Expects an associative array as second parameter, which will be
1449 * www-form-urlencoded. See http_get() for a full list of available options.
1451 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1453 PHP_FUNCTION(http_post_fields
)
1455 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
1458 http_request_body body
;
1459 http_request request
;
1461 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
1465 if (!http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
1476 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1477 request
.body
= &body
;
1478 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1479 http_request_exec(&request
);
1481 http_request_info(&request
, Z_ARRVAL_P(info
));
1483 RETVAL_RESPONSE_OR_BODY(request
);
1485 http_request_dtor(&request
);
1489 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1491 * Performs an HTTP PUT request on the supplied url.
1493 * Expects the second parameter to be a string referencing the file to upload.
1494 * See http_get() for a full list of available options.
1496 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1498 PHP_FUNCTION(http_put_file
)
1502 zval
*options
= NULL
, *info
= NULL
;
1504 php_stream_statbuf ssb
;
1505 http_request_body body
;
1506 http_request request
;
1508 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1512 if (!(stream
= php_stream_open_wrapper_ex(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
))) {
1515 if (php_stream_stat(stream
, &ssb
)) {
1516 php_stream_close(stream
);
1527 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1528 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
1529 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1530 http_request_exec(&request
);
1532 http_request_info(&request
, Z_ARRVAL_P(info
));
1534 RETVAL_RESPONSE_OR_BODY(request
);
1536 http_request_dtor(&request
);
1540 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1542 * Performs an HTTP PUT request on the supplied url.
1544 * Expects the second parameter to be a resource referencing an already
1545 * opened stream, from which the data to upload should be read.
1546 * See http_get() for a full list of available options.
1548 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1550 PHP_FUNCTION(http_put_stream
)
1552 zval
*resource
, *options
= NULL
, *info
= NULL
;
1556 php_stream_statbuf ssb
;
1557 http_request_body body
;
1558 http_request request
;
1560 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1564 php_stream_from_zval(stream
, &resource
);
1565 if (php_stream_stat(stream
, &ssb
)) {
1576 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1577 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 0);
1578 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1579 http_request_exec(&request
);
1581 http_request_info(&request
, Z_ARRVAL_P(info
));
1583 RETVAL_RESPONSE_OR_BODY(request
);
1585 http_request_dtor(&request
);
1589 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1591 * Performs an HTTP PUT request on the supplied url.
1593 * Expects the second parameter to be a string containing the data to upload.
1594 * See http_get() for a full list of available options.
1596 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1598 PHP_FUNCTION(http_put_data
)
1601 int URL_len
, data_len
;
1602 zval
*options
= NULL
, *info
= NULL
;
1603 http_request_body body
;
1604 http_request request
;
1606 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1617 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1618 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1619 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1620 http_request_exec(&request
);
1622 http_request_info(&request
, Z_ARRVAL_P(info
));
1624 RETVAL_RESPONSE_OR_BODY(request
);
1626 http_request_dtor(&request
);
1630 /* {{{ proto string http_request(int method, string url[, string body[, array options[, array &info]]])
1632 * Performs a custom HTTP request on the supplied url.
1634 * Expects the first parameter to be an integer specifying the request method to use.
1635 * Accepts an optional third string parameter containing the raw request body.
1636 * See http_get() for a full list of available options.
1638 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1640 PHP_FUNCTION(http_request
)
1643 char *URL
, *data
= NULL
;
1644 int URL_len
, data_len
= 0;
1645 zval
*options
= NULL
, *info
= NULL
;
1646 http_request_body body
;
1647 http_request request
;
1649 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ls|sa/!z", &meth
, &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1660 http_request_init_ex(&request
, NULL
, meth
, URL
);
1661 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1662 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1663 http_request_exec(&request
);
1665 http_request_info(&request
, Z_ARRVAL_P(info
));
1667 RETVAL_RESPONSE_OR_BODY(request
);
1669 http_request_dtor(&request
);
1673 static char *file_get_contents(char *file
, size_t *len TSRMLS_DC
)
1675 php_stream
*s
= NULL
;
1678 if ((s
= php_stream_open_wrapper_ex(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
))) {
1679 *len
= php_stream_copy_to_mem(s
, &buf
, (size_t) -1, 0);
1680 php_stream_close(s
);
1687 struct FormData
*next
;
1692 CURLcode
Curl_getFormData(struct FormData
**, struct curl_httppost
*post
, curl_off_t
*size
);
1694 /* {{{ proto string http_request_body_encode(array fields, array files)
1696 * Generate x-www-form-urlencoded resp. form-data encoded request body.
1698 * Returns encoded string on success, or FALSE on failure.
1700 PHP_FUNCTION(http_request_body_encode
)
1702 zval
*fields
= NULL
, *files
= NULL
;
1703 HashTable
*fields_ht
, *files_ht
;
1704 http_request_body body
;
1706 struct FormData
*data
, *ptr
;
1713 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!a!", &fields
, &files
)) {
1717 fields_ht
= (fields
&& Z_TYPE_P(fields
) == IS_ARRAY
) ? Z_ARRVAL_P(fields
) : NULL
;
1718 files_ht
= (files
&& Z_TYPE_P(files
) == IS_ARRAY
) ? Z_ARRVAL_P(files
) : NULL
;
1719 if (!http_request_body_fill(&body
, fields_ht
, files_ht
)) {
1725 case HTTP_REQUEST_BODY_CURLPOST
:
1726 if (CURLE_OK
!= (rc
= Curl_getFormData(&data
, body
.data
, &size
))) {
1727 http_error_ex(HE_WARNING
, HTTP_E_RUNTIME
, "Could not encode request body: %s", curl_easy_strerror(rc
));
1730 phpstr_init_ex(&rbuf
, (size_t) size
, PHPSTR_INIT_PREALLOC
);
1731 for (ptr
= data
; ptr
; ptr
= ptr
->next
) {
1734 if ((fdata
= file_get_contents(ptr
->line
, &fsize TSRMLS_CC
))) {
1735 phpstr_append(&rbuf
, fdata
, fsize
);
1741 phpstr_append(&rbuf
, ptr
->line
, ptr
->length
);
1744 curl_free(ptr
->line
);
1751 RETVAL_PHPSTR_VAL(&rbuf
);
1754 http_request_body_dtor(&body
);
1757 case HTTP_REQUEST_BODY_CSTRING
:
1758 RETVAL_STRINGL(body
.data
, body
.size
, 0);
1762 http_request_body_dtor(&body
);
1767 #endif /* HTTP_HAVE_CURL */
1770 /* {{{ proto int http_request_method_register(string method)
1772 * Register a custom request method.
1774 * Expects a string parameter containing the request method name to register.
1776 * Returns the ID of the request method on success, or FALSE on failure.
1778 PHP_FUNCTION(http_request_method_register
)
1784 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1787 if ((existing
= http_request_method_exists(1, 0, method
))) {
1788 RETURN_LONG((long) existing
);
1791 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1795 /* {{{ proto bool http_request_method_unregister(mixed method)
1797 * Unregister a previously registered custom request method.
1799 * Expects either the request method name or ID.
1801 * Returns TRUE on success, or FALSE on failure.
1803 PHP_FUNCTION(http_request_method_unregister
)
1807 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1811 switch (Z_TYPE_P(method
))
1814 convert_to_string(method
);
1816 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1817 convert_to_long(method
);
1820 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1824 ZVAL_LONG(method
, (long)mn
);
1827 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1834 /* {{{ proto int http_request_method_exists(mixed method)
1836 * Check if a request method is registered (or available by default).
1838 * Expects either the request method name or ID as parameter.
1840 * Returns TRUE if the request method is known, else FALSE.
1842 PHP_FUNCTION(http_request_method_exists
)
1847 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1851 switch (Z_TYPE_P(method
))
1854 convert_to_string(method
);
1856 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1857 convert_to_long(method
);
1859 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1862 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method
), NULL
));
1870 /* {{{ proto string http_request_method_name(int method)
1872 * Get the literal string representation of a standard or registered request method.
1874 * Expects the request method ID as parameter.
1876 * Returns the request method name as string on success, or FALSE on failure.
1878 PHP_FUNCTION(http_request_method_name
)
1883 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1887 RETURN_STRING(estrdup(http_request_method_name((int) method
)), 0);
1893 #ifdef HTTP_HAVE_ZLIB
1895 /* {{{ proto string http_deflate(string data[, int flags = 0])
1897 * Compress data with gzip, zlib AKA deflate or raw deflate encoding.
1899 * Expects the first parameter to be a string containing the data that should
1902 * Returns the encoded string on success, or NULL on failure.
1904 PHP_FUNCTION(http_deflate
)
1912 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &data
, &data_len
, &flags
)) {
1916 if (SUCCESS
== http_encoding_deflate(flags
, data
, data_len
, &encoded
, &encoded_len
)) {
1917 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1923 /* {{{ proto string http_inflate(string data)
1925 * Decompress data compressed with either gzip, deflate AKA zlib or raw
1928 * Expects a string as parameter containing the compressed data.
1930 * Returns the decoded string on success, or NULL on failure.
1932 PHP_FUNCTION(http_inflate
)
1939 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1943 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1944 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1950 /* {{{ proto string ob_deflatehandler(string data, int mode)
1952 * For use with ob_start(). The deflate output buffer handler can only be used once.
1953 * It conflicts with ob_gzhandler and zlib.output_compression as well and should
1954 * not be used after ext/mbstrings mb_output_handler and ext/sessions URL-Rewriter (AKA
1955 * session.use_trans_sid).
1957 PHP_FUNCTION(ob_deflatehandler
)
1963 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1967 http_ob_deflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1968 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1972 /* {{{ proto string ob_inflatehandler(string data, int mode)
1974 * For use with ob_start(). Same restrictions as with ob_deflatehandler apply.
1976 PHP_FUNCTION(ob_inflatehandler
)
1982 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1986 http_ob_inflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1987 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1991 #endif /* HTTP_HAVE_ZLIB */
1994 /* {{{ proto int http_support([int feature = 0])
1996 * Check for feature that require external libraries.
1998 * Accepts an optional in parameter specifying which feature to probe for.
1999 * If the parameter is 0 or omitted, the return value contains a bitmask of
2000 * all supported features that depend on external libraries.
2002 * Available features to probe for are:
2004 * <li> HTTP_SUPPORT: always set
2005 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
2006 * and HTTP requests can be issued
2007 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
2008 * and SSL requests can be issued
2009 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
2010 * and compressed HTTP responses can be decoded
2011 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
2012 * and the HttpResponse::guessContentType() method is usable
2015 * Returns int, whether requested feature is supported, or a bitmask with
2016 * all supported features.
2018 PHP_FUNCTION(http_support
)
2024 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
2025 RETVAL_LONG(http_support(feature
));
2030 PHP_FUNCTION(http_test
)
2039 * vim600: noet sw=4 ts=4 fdm=marker
2040 * vim<600: noet sw=4 ts=4