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 HTTP_HAVE_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
= HTTP_G
->request
.time
;
61 RETURN_STRING(http_date(t
), 0);
65 #if PHP_MAJOR_VERSION == 4 && PHP_MINOR_VERSION == 3 && PHP_RELEASE_VERSION < 10
66 # define php_url_parse_ex(u, l) php_url_parse(u)
69 /* {{{ proto string http_build_url([mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]]])
73 * Expexts (part(s) of) an URL as first parameter in form of a string or assoziative array
74 * like parse_url() returns. Accepts an optional second parameter in the same way as the
75 * first argument. Accepts an optional third integer parameter, which is a bitmask of
76 * binary or'ed HTTP_URL_* constants. The optional fourth parameter will be filled
77 * with the results as associative array like parse_url() would return.
79 * The parts of the second URL will be merged into the first according to the flags argument.
80 * The following flags are recognized:
82 * - HTTP_URL_REPLACE: (default) set parts of the second url will replace the parts in the first
83 * - HTTP_URL_JOIN_PATH: the path of the second url will be merged into the one of the first
84 * - HTTP_URL_JOIN_QUERY: the two querystrings will be merged recursively
85 * - HTTP_URL_STRIP_USER: the user part will not appear in the result
86 * - HTTP_URL_STRIP_PASS: the password part will not appear in the result
87 * - HTTP_URL_STRIP_AUTH: neither the user nor the password part will appear in the result
88 * - HTTP_URL_STRIP_PORT: no explicit port will be set in the result
89 * - HTTP_URL_STRIP_PATH: the path part will not appear in the result
90 * - HTTP_URL_STRIP_QUERY: no query string will be present in the result
91 * - HTTP_URL_STRIP_FRAGMENT: no fragment will be present in the result
97 * // ftp://ftp.example.com/pub/files/current/?a=b&a=c
98 * echo http_build_url("http://user@www.example.com/pub/index.php?a=b#files",
101 * "host" => "ftp.example.com",
102 * "path" => "files/current/",
105 * HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
109 * Returns the new URL as string on success or FALSE on failure.
111 PHP_FUNCTION(http_build_url
)
113 char *url_str
= NULL
;
115 long flags
= HTTP_URL_REPLACE
;
116 zval
*z_old_url
= NULL
, *z_new_url
= NULL
, *z_composed_url
= NULL
;
117 php_url
*old_url
= NULL
, *new_url
= NULL
, *composed_url
= NULL
;
119 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z!/z!/lz", &z_old_url
, &z_new_url
, &flags
, &z_composed_url
) != SUCCESS
) {
124 if (Z_TYPE_P(z_new_url
) == IS_ARRAY
|| Z_TYPE_P(z_new_url
) == IS_OBJECT
) {
125 new_url
= http_url_from_struct(NULL
, HASH_OF(z_new_url
));
127 convert_to_string(z_new_url
);
128 if (!(new_url
= php_url_parse_ex(Z_STRVAL_P(z_new_url
), Z_STRLEN_P(z_new_url
)))) {
129 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url
));
136 if (Z_TYPE_P(z_old_url
) == IS_ARRAY
|| Z_TYPE_P(z_old_url
) == IS_OBJECT
) {
137 old_url
= http_url_from_struct(NULL
, HASH_OF(z_old_url
));
139 convert_to_string(z_old_url
);
140 if (!(old_url
= php_url_parse_ex(Z_STRVAL_P(z_old_url
), Z_STRLEN_P(z_old_url
)))) {
142 php_url_free(new_url
);
144 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url
));
150 if (z_composed_url
) {
151 http_build_url(flags
, old_url
, new_url
, &composed_url
, &url_str
, &url_len
);
152 http_url_tostruct(composed_url
, z_composed_url
);
153 php_url_free(composed_url
);
155 http_build_url(flags
, old_url
, new_url
, NULL
, &url_str
, &url_len
);
159 php_url_free(new_url
);
162 php_url_free(old_url
);
165 RETURN_STRINGL(url_str
, url_len
, 0);
169 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
171 * Opponent to parse_str().
173 * Expects an array as first argument which represents the parts of the query string to build.
174 * Accepts a string as optional second parameter containing a top-level prefix to use.
175 * The optional third parameter should specify an argument separator to use (by default the
176 * INI setting arg_separator.output will be used, or "&" if neither is set).
178 * Returns the built query as string on success or FALSE on failure.
180 PHP_FUNCTION(http_build_str
)
183 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
184 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
187 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
192 arg_sep
= HTTP_URL_ARGSEP
;
193 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
196 phpstr_init(&formstr
);
197 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), &formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
202 phpstr_dtor(&formstr
);
206 RETURN_PHPSTR_VAL(&formstr
);
210 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
213 if ((result = http_negotiate_ ##type(supported))) { \
218 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
219 RETVAL_STRINGL(key, key_len-1, 0); \
225 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
228 zend_hash_destroy(result); \
229 FREE_HASHTABLE(result); \
234 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
235 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
236 RETVAL_ZVAL(*value, 1, 0); \
245 FOREACH_VAL(pos, supported, value) { \
246 convert_to_string_ex(value); \
247 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
254 /* {{{ proto string http_negotiate_language(array supported[, array &result])
256 * This function negotiates the clients preferred language based on its
257 * Accept-Language HTTP header. The qualifier is recognized and languages
258 * without qualifier are rated highest. The qualifier will be decreased by
259 * 10% for partial matches (i.e. matching primary language).
261 * Expects an array as parameter containing the supported languages as values.
262 * If the optional second parameter is supplied, it will be filled with an
263 * array containing the negotiation results.
265 * Returns the negotiated language or the default language (i.e. first array entry)
281 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
287 PHP_FUNCTION(http_negotiate_language
)
289 zval
*supported
, *rs_array
= NULL
;
291 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
297 array_init(rs_array
);
300 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
304 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
306 * This function negotiates the clients preferred charset based on its
307 * Accept-Charset HTTP header. The qualifier is recognized and charsets
308 * without qualifier are rated highest.
310 * Expects an array as parameter containing the supported charsets as values.
311 * If the optional second parameter is supplied, it will be filled with an
312 * array containing the negotiation results.
314 * Returns the negotiated charset or the default charset (i.e. first array entry)
321 * 'iso-8859-1', // default
327 * $pref = http_negotiate_charset($charsets, $result);
329 * if (strcmp($pref, 'iso-8859-1')) {
330 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
331 * iconv_set_encoding('output_encoding', $pref);
332 * ob_start('ob_iconv_handler');
339 PHP_FUNCTION(http_negotiate_charset
)
341 zval
*supported
, *rs_array
= NULL
;
343 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
349 array_init(rs_array
);
352 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
356 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
358 * This function negotiates the clients preferred content type based on its
359 * Accept HTTP header. The qualifier is recognized and content types
360 * without qualifier are rated highest.
362 * Expects an array as parameter containing the supported content types as values.
363 * If the optional second parameter is supplied, it will be filled with an
364 * array containing the negotiation results.
366 * Returns the negotiated content type or the default content type
367 * (i.e. first array entry) if none match.
372 * $ctypes = array('application/xhtml+xml', 'text/html');
373 * http_send_content_type(http_negotiate_content_type($ctypes));
377 PHP_FUNCTION(http_negotiate_content_type
)
379 zval
*supported
, *rs_array
= NULL
;
381 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
387 array_init(rs_array
);
390 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
394 /* {{{ proto bool http_send_status(int status)
396 * Send HTTP status code.
398 * Expects an HTTP status code as parameter.
400 * Returns TRUE on success or FALSE on failure.
402 PHP_FUNCTION(http_send_status
)
406 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
409 if (status
< 100 || status
> 510) {
410 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
414 RETURN_SUCCESS(http_send_status(status
));
418 /* {{{ proto bool http_send_last_modified([int timestamp])
420 * Send a "Last-Modified" header with a valid HTTP date.
422 * Accepts a unix timestamp, converts it to a valid HTTP date and
423 * sends it as "Last-Modified" HTTP header. If timestamp is
424 * omitted, the current time will be sent.
426 * Returns TRUE on success or FALSE on failure.
428 PHP_FUNCTION(http_send_last_modified
)
432 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
437 t
= HTTP_G
->request
.time
;
440 RETURN_SUCCESS(http_send_last_modified(t
));
444 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
446 * Send the Content-Type of the sent entity. This is particularly important
447 * if you use the http_send() API.
449 * Accepts an optional string parameter containing the desired content type
450 * (primary/secondary).
452 * Returns TRUE on success or FALSE on failure.
454 PHP_FUNCTION(http_send_content_type
)
456 char *ct
= "application/x-octetstream";
457 int ct_len
= lenof("application/x-octetstream");
459 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
463 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
467 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
469 * Send the Content-Disposition. The Content-Disposition header is very useful
470 * if the data actually sent came from a file or something similar, that should
471 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
473 * Expects a string parameter specifying the file name the "Save as..." dialog
474 * should display. Optionally accepts a bool parameter, which, if set to true
475 * and the user agent knows how to handle the content type, will probably not
476 * cause the popup window to be shown.
478 * Returns TRUE on success or FALSE on failure.
480 PHP_FUNCTION(http_send_content_disposition
)
484 zend_bool send_inline
= 0;
486 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
489 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
493 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
495 * Matches the given unix timestamp against the clients "If-Modified-Since"
496 * resp. "If-Unmodified-Since" HTTP headers.
498 * Accepts a unix timestamp which should be matched. Optionally accepts an
499 * additional bool parameter, which if set to true will check the header
500 * usually used to validate HTTP ranges. If timestamp is omitted, the
501 * current time will be used.
503 * Returns TRUE if timestamp represents an earlier date than the header,
506 PHP_FUNCTION(http_match_modified
)
509 zend_bool for_range
= 0;
511 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
515 // current time if not supplied (senseless though)
517 t
= HTTP_G
->request
.time
;
521 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
523 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
527 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
529 * Matches the given ETag against the clients "If-Match" resp.
530 * "If-None-Match" HTTP headers.
532 * Expects a string parameter containing the ETag to compare. Optionally
533 * accepts a bool parameter, which, if set to true, will check the header
534 * usually used to validate HTTP ranges.
536 * Returns TRUE if ETag matches or the header contained the asterisk ("*"),
539 PHP_FUNCTION(http_match_etag
)
543 zend_bool for_range
= 0;
545 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
550 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
552 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
556 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
558 * Attempts to cache the sent entity by its last modification date.
560 * Accepts a unix timestamp as parameter which is handled as follows:
562 * If timestamp_or_expires is greater than 0, it is handled as timestamp
563 * and will be sent as date of last modification. If it is 0 or omitted,
564 * the current time will be sent as Last-Modified date. If it's negative,
565 * it is handled as expiration time in seconds, which means that if the
566 * requested last modification date is not between the calculated timespan,
567 * the Last-Modified header is updated and the actual body will be sent.
569 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
571 * A log entry will be written to the cache log if the INI entry
572 * http.log.cache is set and the cache attempt was successful.
574 PHP_FUNCTION(http_cache_last_modified
)
576 long last_modified
= 0, send_modified
= 0, t
;
579 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
583 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
585 t
= HTTP_G
->request
.time
;
588 if (!last_modified
) {
589 /* does the client have? (att: caching "forever") */
590 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
591 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
592 /* send current time */
596 /* negative value is supposed to be expiration time */
597 } else if (last_modified
< 0) {
600 /* send supplied time explicitly */
602 send_modified
= last_modified
;
605 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
609 /* {{{ proto bool http_cache_etag([string etag])
611 * Attempts to cache the sent entity by its ETag, either supplied or generated
612 * by the hash algorithm specified by the INI setting "http.etag.mode".
614 * If the clients "If-None-Match" header matches the supplied/calculated
615 * ETag, the body is considered cached on the clients side and
616 * a "304 Not Modified" status code is issued.
618 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
620 * A log entry is written to the cache log if the INI entry
621 * "http.log.cache" is set and the cache attempt was successful.
623 PHP_FUNCTION(http_cache_etag
)
628 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
632 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
634 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
638 /* {{{ proto string ob_etaghandler(string data, int mode)
640 * For use with ob_start(). Output buffer handler generating an ETag with
641 * the hash algorithm specified with the INI setting "http.etag.mode".
643 PHP_FUNCTION(ob_etaghandler
)
649 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
653 Z_TYPE_P(return_value
) = IS_STRING
;
654 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
658 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
660 * Sets the throttle delay and send buffer size for use with http_send() API.
661 * Provides a basic throttling mechanism, which will yield the current process
662 * resp. thread until the entity has been completely sent, though.
664 * Expects a double parameter specifying the seconds too sleep() after
665 * each chunk sent. Additionally accepts an optional int parameter
666 * representing the chunk size in bytes.
672 * # http_throttle(1, 20000);
673 * # http_throttle(0.5, 10000);
674 * # http_throttle(0.1, 2000);
675 * http_send_file('document.pdf');
679 PHP_FUNCTION(http_throttle
)
681 long chunk_size
= HTTP_SENDBUF_SIZE
;
684 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
688 HTTP_G
->send
.throttle_delay
= interval
;
689 HTTP_G
->send
.buffer_size
= chunk_size
;
693 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
695 * Redirect to the given url.
697 * The supplied url will be expanded with http_build_url(), the params array will
698 * be treated with http_build_query() and the session identification will be appended
699 * if session is true.
701 * The HTTP response code will be set according to status.
702 * You can use one of the following constants for convenience:
703 * - HTTP_REDIRECT 302 Found for GET/HEAD, else 303 See Other
704 * - HTTP_REDIRECT_PERM 301 Moved Permanently
705 * - HTTP_REDIRECT_FOUND 302 Found
706 * - HTTP_REDIRECT_POST 303 See Other
707 * - HTTP_REDIRECT_PROXY 305 Use Proxy
708 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
710 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
711 * for which redirect response code to use in which situation.
713 * To be RFC compliant, "Redirecting to <a>URL</a>." will be displayed,
714 * if the client doesn't redirect immediately, and the request method was
715 * another one than HEAD.
717 * Returns FALSE on failure, or *exits* on success.
719 * A log entry will be written to the redirect log, if the INI entry
720 * "http.log.redirect" is set and the redirect attempt was successful.
722 PHP_FUNCTION(http_redirect
)
725 size_t query_len
= 0;
726 zend_bool session
= 0, free_params
= 0;
728 long status
= HTTP_REDIRECT
;
729 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
731 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
735 #ifdef HTTP_HAVE_SESSION
736 /* append session info */
740 MAKE_STD_ZVAL(params
);
743 if (PS(session_status
) == php_session_active
) {
744 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
745 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
751 /* treat params array with http_build_query() */
753 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
765 URI
= http_absolute_url(url
);
768 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
770 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
773 spprintf(&LOC
, 0, "Location: %s", URI
);
775 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
790 RETVAL_SUCCESS(http_send_status_header(status
, LOC
));
794 case HTTP_REDIRECT_PERM
:
795 case HTTP_REDIRECT_FOUND
:
796 case HTTP_REDIRECT_POST
:
797 case HTTP_REDIRECT_PROXY
:
798 case HTTP_REDIRECT_TEMP
:
803 http_error_ex(HE_NOTICE
, HTTP_E_RUNTIME
, "Unsupported redirection status code: %ld", status
);
805 if ( SG(request_info
).request_method
&&
806 strcasecmp(SG(request_info
).request_method
, "HEAD") &&
807 strcasecmp(SG(request_info
).request_method
, "GET")) {
808 status
= HTTP_REDIRECT_POST
;
810 status
= HTTP_REDIRECT_FOUND
;
815 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
819 /* {{{ proto bool http_send_data(string data)
821 * Sends raw data with support for (multiple) range requests.
823 * Returns TRUE on success, or FALSE on failure.
825 PHP_FUNCTION(http_send_data
)
829 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
833 convert_to_string_ex(&zdata
);
834 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
838 /* {{{ proto bool http_send_file(string file)
840 * Sends a file with support for (multiple) range requests.
842 * Expects a string parameter referencing the file to send.
844 * Returns TRUE on success, or FALSE on failure.
846 PHP_FUNCTION(http_send_file
)
851 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
858 RETURN_SUCCESS(http_send_file(file
));
862 /* {{{ proto bool http_send_stream(resource stream)
864 * Sends an already opened stream with support for (multiple) range requests.
866 * Expects a resource parameter referencing the stream to read from.
868 * Returns TRUE on success, or FALSE on failure.
870 PHP_FUNCTION(http_send_stream
)
875 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
879 php_stream_from_zval(file
, &zstream
);
880 RETURN_SUCCESS(http_send_stream(file
));
884 /* {{{ proto string http_chunked_decode(string encoded)
886 * Decodes a string that was HTTP-chunked encoded.
888 * Expects a chunked encoded string as parameter.
890 * Returns the decoded string on success or FALSE on failure.
892 PHP_FUNCTION(http_chunked_decode
)
894 char *encoded
= NULL
, *decoded
= NULL
;
895 size_t decoded_len
= 0;
898 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
902 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
903 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
910 /* {{{ proto object http_parse_message(string message)
912 * Parses (a) http_message(s) into a simple recursive object structure.
914 * Expects a string parameter containing a single HTTP message or
915 * several consecutive HTTP messages.
917 * Returns an hierarchical object structure of the parsed messages.
922 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
927 * [httpVersion] => 1.1
928 * [responseCode] => 200
931 * [Content-Length] => 3
935 * [parentMessage] => stdClass object
938 * [httpVersion] => 1.1
939 * [responseCode] => 302
942 * [Content-Length] => 0
946 * [parentMessage] => ...
952 PHP_FUNCTION(http_parse_message
)
956 http_message
*msg
= NULL
;
958 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
962 if ((msg
= http_message_parse(message
, message_len
))) {
963 object_init(return_value
);
964 http_message_tostruct_recursive(msg
, return_value
);
965 http_message_free(&msg
);
972 /* {{{ proto array http_parse_headers(string header)
974 * Parses HTTP headers into an associative array.
976 * Expects a string parameter containing HTTP headers.
978 * Returns an array on success, or FALSE on failure.
983 * $headers = "content-type: text/html; charset=UTF-8\r\n".
984 * "Server: Funky/1.0\r\n".
985 * "Set-Cookie: foo=bar\r\n".
986 * "Set-Cookie: baz=quux\r\n".
987 * "Folded: works\r\n\ttoo\r\n";
988 * print_r(http_parse_headers($headers));
992 * [Content-Type] => text/html; chatset=UTF-8
993 * [Server] => Funky/1.0
994 * [Set-Cookie] => Array
1005 PHP_FUNCTION(http_parse_headers
)
1010 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
1014 array_init(return_value
);
1015 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
1016 zval_dtor(return_value
);
1017 http_error(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Failed to parse headers");
1023 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
1025 * Parses HTTP cookies like sent in a response into a struct.
1027 * Expects a string as parameter containing the value of a Set-Cookie response header.
1029 * Returns an stdClass olike shown in the example on success or FALSE on failure.
1034 * print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
1038 * [cookies] => Array
1052 * [domain] => example.com
1057 PHP_FUNCTION(http_parse_cookie
)
1059 char *cookie
, **allowed_extras
= NULL
;
1060 int i
= 0, cookie_len
;
1062 zval
*allowed_extras_array
= NULL
, **entry
= NULL
;
1064 http_cookie_list list
;
1066 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|la", &cookie
, &cookie_len
, &flags
, &allowed_extras_array
)) {
1070 if (allowed_extras_array
) {
1071 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
1072 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
1073 ZVAL_ADDREF(*entry
);
1074 convert_to_string_ex(entry
);
1075 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
1076 zval_ptr_dtor(entry
);
1080 if (http_parse_cookie_ex(&list
, cookie
, flags
, allowed_extras
)) {
1081 object_init(return_value
);
1082 http_cookie_list_tostruct(&list
, return_value
);
1083 http_cookie_list_dtor(&list
);
1088 if (allowed_extras
) {
1089 for (i
= 0; allowed_extras
[i
]; ++i
) {
1090 efree(allowed_extras
[i
]);
1092 efree(allowed_extras
);
1097 /* {{{ proto string http_build_cookie(array cookie)
1099 * Build a cookie string from an array/object like returned by http_parse_cookie().
1101 PHP_FUNCTION(http_build_cookie
)
1106 http_cookie_list list
;
1108 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &strct
)) {
1112 http_cookie_list_fromstruct(&list
, strct
);
1113 http_cookie_list_tostring(&list
, &str
, &len
);
1114 http_cookie_list_dtor(&list
);
1116 RETURN_STRINGL(str
, len
, 0);
1120 /* {{{ proto object http_parse_params(string param[, int flags = HTTP_PARAMS_DEFAULT])
1122 * Parse parameter list.
1124 PHP_FUNCTION(http_parse_params
)
1129 long flags
= HTTP_PARAMS_DEFAULT
;
1131 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", ¶m
, ¶m_len
, &flags
)) {
1135 params
= ecalloc(1, sizeof(zval
));
1137 if (SUCCESS
!= http_parse_params(param
, flags
, Z_ARRVAL_P(params
))) {
1142 object_init(return_value
);
1143 add_property_zval(return_value
, "params", params
);
1147 /* {{{ proto array http_get_request_headers(void)
1149 * Get a list of incoming HTTP headers.
1151 * Returns an associative array of incoming request headers.
1153 PHP_FUNCTION(http_get_request_headers
)
1157 array_init(return_value
);
1158 http_get_request_headers(Z_ARRVAL_P(return_value
));
1162 /* {{{ proto string http_get_request_body(void)
1164 * Get the raw request body (e.g. POST or PUT data).
1166 * This function can not be used after http_get_request_body_stream()
1167 * if the request method was another than POST.
1169 * Returns the raw request body as string on success or NULL on failure.
1171 PHP_FUNCTION(http_get_request_body
)
1178 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
1179 RETURN_STRINGL(body
, (int) length
, 0);
1186 /* {{{ proto resource http_get_request_body_stream(void)
1188 * Create a stream to read the raw request body (e.g. POST or PUT data).
1190 * This function can only be used once if the request method was another than POST.
1192 * Returns the raw request body as stream on success or NULL on failure.
1194 PHP_FUNCTION(http_get_request_body_stream
)
1200 if ((s
= http_get_request_body_stream())) {
1201 php_stream_to_zval(s
, return_value
);
1203 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to create request body stream");
1209 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
1211 * Match an incoming HTTP header.
1213 * Expects two string parameters representing the header name (case-insensitive)
1214 * and the header value that should be compared. The case sensitivity of the
1215 * header value depends on the additional optional bool parameter accepted.
1217 * Returns TRUE if header value matches, else FALSE.
1219 PHP_FUNCTION(http_match_request_header
)
1221 char *header
, *value
;
1222 int header_len
, value_len
;
1223 zend_bool match_case
= 0;
1225 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
1229 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
1234 #ifdef HTTP_HAVE_CURL
1236 #define RETVAL_RESPONSE_OR_BODY(request) \
1240 /* check if only the body should be returned */ \
1241 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
1242 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1245 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1246 http_message_free(&msg); \
1249 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1253 /* {{{ proto string http_get(string url[, array options[, array &info]])
1255 * Performs an HTTP GET request on the supplied url.
1257 * The second parameter, if set, is expected to be an associative
1258 * array where the following keys will be recognized:
1260 * - redirect: int, whether and how many redirects to follow
1261 * - unrestrictedauth: bool, whether to continue sending credentials on
1262 * redirects to a different host
1263 * - proxyhost: string, proxy host in "host[:port]" format
1264 * - proxyport: int, use another proxy port as specified in proxyhost
1265 * - proxytype: int, HTTP_PROXY_HTTP, SOCKS4 or SOCKS5
1266 * - proxyauth: string, proxy credentials in "user:pass" format
1267 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1268 * - httpauth: string, http credentials in "user:pass" format
1269 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1270 * - compress: bool, whether to allow gzip/deflate content encoding
1271 * - port: int, use another port as specified in the url
1272 * - referer: string, the referer to send
1273 * - useragent: string, the user agent to send
1274 * (defaults to PECL::HTTP/version (PHP/version)))
1275 * - headers: array, list of custom headers as associative array
1276 * like array("header" => "value")
1277 * - cookies: array, list of cookies as associative array
1278 * like array("cookie" => "value")
1279 * - encodecookies: bool, whether to urlencode the cookies (default: true)
1280 * - cookiestore: string, path to a file where cookies are/will be stored
1281 * - cookiesession: bool, don't load session cookies from cookiestore if TRUE
1282 * - resume: int, byte offset to start the download from;
1283 * if the server supports ranges
1284 * - range: array, array of arrays, each containing two integers,
1285 * specifying the ranges to download if server support is
1286 * given; only recognized if the resume option is empty
1287 * - maxfilesize: int, maximum file size that should be downloaded;
1288 * has no effect, if the size of the requested entity is not known
1289 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1290 * - etag: string, quoted etag for If-(None-)Match header
1291 * - timeout: int, seconds the request may take
1292 * - connecttimeout: int, seconds the connect may take
1293 * - onprogress: mixed, progress callback
1294 * - interface: string, outgoing network interface (ifname, ip or hostname)
1295 * - portrange: array, 2 integers specifying outgoing portrange to try
1296 * - ssl: array, with the following options:
1297 * cert: string, path to certificate
1298 * certtype: string, type of certificate
1299 * certpasswd: string, password for certificate
1300 * key: string, path to key
1301 * keytype: string, type of key
1302 * keypasswd: string, pasword for key
1303 * engine: string, ssl engine to use
1304 * version: int, ssl version to use
1305 * verifypeer: bool, whether to verify the peer
1306 * verifyhost: bool whether to verify the host
1307 * cipher_list: string, list of allowed ciphers
1310 * random_file: string
1314 * The optional third parameter will be filled with some additional information
1315 * in form of an associative array, if supplied, like the following example:
1319 * 'effective_url' => 'http://www.example.com/',
1320 * 'response_code' => 302,
1321 * 'connect_code' => 0,
1323 * 'total_time' => 0.212348,
1324 * 'namelookup_time' => 0.038296,
1325 * 'connect_time' => 0.104144,
1326 * 'pretransfer_time' => 0.104307,
1327 * 'starttransfer_time' => 0.212077,
1328 * 'redirect_time' => 0,
1329 * 'redirect_count' => 0,
1330 * 'size_upload' => 0,
1331 * 'size_download' => 218,
1332 * 'speed_download' => 1026,
1333 * 'speed_upload' => 0,
1334 * 'header_size' => 307,
1335 * 'request_size' => 103,
1336 * 'ssl_verifyresult' => 0,
1349 * 'content_length_download' => 218,
1350 * 'content_length_upload' => 0,
1351 * 'content_type' => 'text/html',
1352 * 'httpauth_avail' => 0,
1353 * 'proxyauth_avail' => 0,
1354 * 'num_connects' => 1,
1361 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1363 PHP_FUNCTION(http_get
)
1365 zval
*options
= NULL
, *info
= NULL
;
1368 http_request request
;
1370 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1381 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
1382 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1383 http_request_exec(&request
);
1385 http_request_info(&request
, Z_ARRVAL_P(info
));
1387 RETVAL_RESPONSE_OR_BODY(request
);
1389 http_request_dtor(&request
);
1393 /* {{{ proto string http_head(string url[, array options[, array &info]])
1395 * Performs an HTTP HEAD request on the supplied url.
1397 * See http_get() for a full list of available parameters and options.
1399 * Returns the HTTP response as string on success, or FALSE on failure.
1401 PHP_FUNCTION(http_head
)
1403 zval
*options
= NULL
, *info
= NULL
;
1406 http_request request
;
1408 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1419 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
1420 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1421 http_request_exec(&request
);
1423 http_request_info(&request
, Z_ARRVAL_P(info
));
1425 RETVAL_RESPONSE_OR_BODY(request
);
1427 http_request_dtor(&request
);
1431 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1433 * Performs an HTTP POST request on the supplied url.
1435 * Expects a string as second parameter containing the pre-encoded post data.
1436 * See http_get() for a full list of available parameters and options.
1438 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1440 PHP_FUNCTION(http_post_data
)
1442 zval
*options
= NULL
, *info
= NULL
;
1443 char *URL
, *postdata
;
1444 int postdata_len
, URL_len
;
1445 http_request_body body
;
1446 http_request request
;
1448 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1459 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1460 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
1461 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1462 http_request_exec(&request
);
1464 http_request_info(&request
, Z_ARRVAL_P(info
));
1466 RETVAL_RESPONSE_OR_BODY(request
);
1468 http_request_dtor(&request
);
1472 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1474 * Performs an HTTP POST request on the supplied url.
1476 * Expects an associative array as second parameter, which will be
1477 * www-form-urlencoded. See http_get() for a full list of available options.
1479 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1481 PHP_FUNCTION(http_post_fields
)
1483 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
1486 http_request_body body
;
1487 http_request request
;
1489 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
1493 if (!http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
1504 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1505 request
.body
= &body
;
1506 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1507 http_request_exec(&request
);
1509 http_request_info(&request
, Z_ARRVAL_P(info
));
1511 RETVAL_RESPONSE_OR_BODY(request
);
1513 http_request_dtor(&request
);
1517 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1519 * Performs an HTTP PUT request on the supplied url.
1521 * Expects the second parameter to be a string referencing the file to upload.
1522 * See http_get() for a full list of available options.
1524 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1526 PHP_FUNCTION(http_put_file
)
1530 zval
*options
= NULL
, *info
= NULL
;
1532 php_stream_statbuf ssb
;
1533 http_request_body body
;
1534 http_request request
;
1536 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1540 if (!(stream
= php_stream_open_wrapper_ex(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
))) {
1543 if (php_stream_stat(stream
, &ssb
)) {
1544 php_stream_close(stream
);
1555 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1556 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
1557 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1558 http_request_exec(&request
);
1560 http_request_info(&request
, Z_ARRVAL_P(info
));
1562 RETVAL_RESPONSE_OR_BODY(request
);
1564 http_request_dtor(&request
);
1568 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1570 * Performs an HTTP PUT request on the supplied url.
1572 * Expects the second parameter to be a resource referencing an already
1573 * opened stream, from which the data to upload should be read.
1574 * See http_get() for a full list of available options.
1576 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1578 PHP_FUNCTION(http_put_stream
)
1580 zval
*resource
, *options
= NULL
, *info
= NULL
;
1584 php_stream_statbuf ssb
;
1585 http_request_body body
;
1586 http_request request
;
1588 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1592 php_stream_from_zval(stream
, &resource
);
1593 if (php_stream_stat(stream
, &ssb
)) {
1604 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1605 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 0);
1606 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1607 http_request_exec(&request
);
1609 http_request_info(&request
, Z_ARRVAL_P(info
));
1611 RETVAL_RESPONSE_OR_BODY(request
);
1613 http_request_dtor(&request
);
1617 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1619 * Performs an HTTP PUT request on the supplied url.
1621 * Expects the second parameter to be a string containing the data to upload.
1622 * See http_get() for a full list of available options.
1624 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1626 PHP_FUNCTION(http_put_data
)
1629 int URL_len
, data_len
;
1630 zval
*options
= NULL
, *info
= NULL
;
1631 http_request_body body
;
1632 http_request request
;
1634 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1645 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1646 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1647 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1648 http_request_exec(&request
);
1650 http_request_info(&request
, Z_ARRVAL_P(info
));
1652 RETVAL_RESPONSE_OR_BODY(request
);
1654 http_request_dtor(&request
);
1658 /* {{{ proto string http_request(int method, string url[, string body[, array options[, array &info]]])
1660 * Performs a custom HTTP request on the supplied url.
1662 * Expects the first parameter to be an integer specifying the request method to use.
1663 * Accepts an optional third string parameter containing the raw request body.
1664 * See http_get() for a full list of available options.
1666 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1668 PHP_FUNCTION(http_request
)
1671 char *URL
, *data
= NULL
;
1672 int URL_len
, data_len
= 0;
1673 zval
*options
= NULL
, *info
= NULL
;
1674 http_request_body body
;
1675 http_request request
;
1677 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ls|sa/!z", &meth
, &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1688 http_request_init_ex(&request
, NULL
, meth
, URL
);
1689 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1690 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1691 http_request_exec(&request
);
1693 http_request_info(&request
, Z_ARRVAL_P(info
));
1695 RETVAL_RESPONSE_OR_BODY(request
);
1697 http_request_dtor(&request
);
1701 /* {{{ proto string http_request_body_encode(array fields, array files)
1703 * Generate x-www-form-urlencoded resp. form-data encoded request body.
1705 * Returns encoded string on success, or FALSE on failure.
1707 PHP_FUNCTION(http_request_body_encode
)
1709 zval
*fields
= NULL
, *files
= NULL
;
1710 HashTable
*fields_ht
, *files_ht
;
1711 http_request_body body
;
1715 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!a!", &fields
, &files
)) {
1719 fields_ht
= (fields
&& Z_TYPE_P(fields
) == IS_ARRAY
) ? Z_ARRVAL_P(fields
) : NULL
;
1720 files_ht
= (files
&& Z_TYPE_P(files
) == IS_ARRAY
) ? Z_ARRVAL_P(files
) : NULL
;
1721 if (http_request_body_fill(&body
, fields_ht
, files_ht
) && (SUCCESS
== http_request_body_encode(&body
, &buf
, &len
))) {
1722 RETVAL_STRINGL(buf
, len
, 0);
1724 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not encode request body");
1727 http_request_body_dtor(&body
);
1729 #endif /* HTTP_HAVE_CURL */
1732 /* {{{ proto int http_request_method_register(string method)
1734 * Register a custom request method.
1736 * Expects a string parameter containing the request method name to register.
1738 * Returns the ID of the request method on success, or FALSE on failure.
1740 PHP_FUNCTION(http_request_method_register
)
1746 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1749 if ((existing
= http_request_method_exists(1, 0, method
))) {
1750 RETURN_LONG((long) existing
);
1753 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1757 /* {{{ proto bool http_request_method_unregister(mixed method)
1759 * Unregister a previously registered custom request method.
1761 * Expects either the request method name or ID.
1763 * Returns TRUE on success, or FALSE on failure.
1765 PHP_FUNCTION(http_request_method_unregister
)
1769 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1773 switch (Z_TYPE_P(method
)) {
1775 convert_to_string(method
);
1777 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1778 convert_to_long(method
);
1781 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1785 ZVAL_LONG(method
, (long)mn
);
1788 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1795 /* {{{ proto int http_request_method_exists(mixed method)
1797 * Check if a request method is registered (or available by default).
1799 * Expects either the request method name or ID as parameter.
1801 * Returns TRUE if the request method is known, else FALSE.
1803 PHP_FUNCTION(http_request_method_exists
)
1805 if (return_value_used
) {
1808 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1812 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
);
1819 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1822 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method
), NULL
));
1830 /* {{{ proto string http_request_method_name(int method)
1832 * Get the literal string representation of a standard or registered request method.
1834 * Expects the request method ID as parameter.
1836 * Returns the request method name as string on success, or FALSE on failure.
1838 PHP_FUNCTION(http_request_method_name
)
1840 if (return_value_used
) {
1843 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1847 RETURN_STRING(estrdup(http_request_method_name((int) method
)), 0);
1853 #ifdef HTTP_HAVE_ZLIB
1855 /* {{{ proto string http_deflate(string data[, int flags = 0])
1857 * Compress data with gzip, zlib AKA deflate or raw deflate encoding.
1859 * Expects the first parameter to be a string containing the data that should
1862 * Returns the encoded string on success, or NULL on failure.
1864 PHP_FUNCTION(http_deflate
)
1872 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &data
, &data_len
, &flags
)) {
1876 if (SUCCESS
== http_encoding_deflate(flags
, data
, data_len
, &encoded
, &encoded_len
)) {
1877 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1883 /* {{{ proto string http_inflate(string data)
1885 * Decompress data compressed with either gzip, deflate AKA zlib or raw
1888 * Expects a string as parameter containing the compressed data.
1890 * Returns the decoded string on success, or NULL on failure.
1892 PHP_FUNCTION(http_inflate
)
1899 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1903 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1904 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1910 /* {{{ proto string ob_deflatehandler(string data, int mode)
1912 * For use with ob_start(). The deflate output buffer handler can only be used once.
1913 * It conflicts with ob_gzhandler and zlib.output_compression as well and should
1914 * not be used after ext/mbstrings mb_output_handler and ext/sessions URL-Rewriter (AKA
1915 * session.use_trans_sid).
1917 PHP_FUNCTION(ob_deflatehandler
)
1923 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1927 http_ob_deflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1928 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1932 /* {{{ proto string ob_inflatehandler(string data, int mode)
1934 * For use with ob_start(). Same restrictions as with ob_deflatehandler apply.
1936 PHP_FUNCTION(ob_inflatehandler
)
1942 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1946 http_ob_inflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1947 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1951 #endif /* HTTP_HAVE_ZLIB */
1954 /* {{{ proto int http_support([int feature = 0])
1956 * Check for feature that require external libraries.
1958 * Accepts an optional in parameter specifying which feature to probe for.
1959 * If the parameter is 0 or omitted, the return value contains a bitmask of
1960 * all supported features that depend on external libraries.
1962 * Available features to probe for are:
1964 * <li> HTTP_SUPPORT: always set
1965 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1966 * and HTTP requests can be issued
1967 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1968 * and SSL requests can be issued
1969 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1970 * and compressed HTTP responses can be decoded
1971 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1972 * and the HttpResponse::guessContentType() method is usable
1975 * Returns int, whether requested feature is supported, or a bitmask with
1976 * all supported features.
1978 PHP_FUNCTION(http_support
)
1984 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
1985 RETVAL_LONG(http_support(feature
));
1990 PHP_FUNCTION(http_test
)
1999 * vim600: noet sw=4 ts=4 fdm=marker
2000 * vim<600: noet sw=4 ts=4