-/* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
- *
- * Set the Content Disposition. The Content-Disposition header is very useful
- * if the data actually sent came from a file or something similar, that should
- * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
- *
- */
-PHP_FUNCTION(http_content_disposition)
-{
- char *filename, *header;
- int f_len;
- zend_bool send_inline = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (send_inline) {
- header = (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len + 1);
- sprintf(header, "Content-Disposition: inline; filename=\"%s\"", filename);
- } else {
- header = (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len + 1);
- sprintf(header, "Content-Disposition: attachment; filename=\"%s\"", filename);
- }
-
- RETVAL_BOOL(SUCCESS == http_send_header(header));
- efree(header);
-}
-/* }}} */
-
-/* {{{ proto string http_chunked_decode(string encoded)
- *
- * This function decodes a string that was HTTP-chunked encoded.
- * Returns false on failure.
- */
-PHP_FUNCTION(http_chunked_decode)
-{
- char *encoded = NULL, *decoded = NULL;
- int encoded_len = 0, decoded_len = 0;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
- RETURN_STRINGL(decoded, decoded_len, 0);
- } else {
- RETURN_FALSE;
- }
-}
-/* }}} */
-
-/* {{{ proto array http_split_response(string http_response)
- *
- * This function splits an HTTP response into an array with headers and the
- * content body. The returned array may look simliar to the following example:
- *
- * <pre>
- * <?php
- * array(
- * 0 => array(
- * 'Status' => '200 Ok',
- * 'Content-Type' => 'text/plain',
- * 'Content-Language' => 'en-US'
- * ),
- * 1 => "Hello World!"
- * );
- * ?>
- * </pre>
- */
-PHP_FUNCTION(http_split_response)
-{
- zval *zresponse, *zbody, *zheaders;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
- RETURN_FALSE;
- }
-
- convert_to_string_ex(&zresponse);
-
- MAKE_STD_ZVAL(zbody);
- MAKE_STD_ZVAL(zheaders);
- array_init(zheaders);
-
- if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
- RETURN_FALSE;
- }
-
- array_init(return_value);
- add_index_zval(return_value, 0, zheaders);
- add_index_zval(return_value, 1, zbody);
-}
-/* }}} */
-
-/* {{{ proto array http_parse_headers(string header)
- *
- */
-PHP_FUNCTION(http_parse_headers)
-{
- char *header, *rnrn;
- int header_len;
-
- if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
- RETURN_FALSE;
- }
-
- array_init(return_value);
-
- if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
- header_len = rnrn - header + 2;
- }
- if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
- zval_dtor(return_value);
- RETURN_FALSE;
- }
-}
-/* }}}*/
-
-/* {{{ proto array http_get_request_headers(void)
- *
- */
-PHP_FUNCTION(http_get_request_headers)
-{
- if (ZEND_NUM_ARGS()) {
- WRONG_PARAM_COUNT;
- }
-
- array_init(return_value);
- http_get_request_headers(return_value);
-}
-/* }}} */
-
-/* {{{ HAVE_CURL */
-#ifdef HTTP_HAVE_CURL
-
-/* {{{ proto string http_get(string url[, array options[, array &info]])
- *
- * Performs an HTTP GET request on the supplied url.
- *
- * The second parameter is expected to be an associative
- * array where the following keys will be recognized:
- * <pre>
- * - redirect: int, whether and how many redirects to follow
- * - unrestrictedauth: bool, whether to continue sending credentials on
- * redirects to a different host
- * - proxyhost: string, proxy host in "host[:port]" format
- * - proxyport: int, use another proxy port as specified in proxyhost
- * - proxyauth: string, proxy credentials in "user:pass" format
- * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
- * - httpauth: string, http credentials in "user:pass" format
- * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
- * - compress: bool, whether to allow gzip/deflate content encoding
- * (defaults to true)
- * - port: int, use another port as specified in the url
- * - referer: string, the referer to sends
- * - useragent: string, the user agent to send
- * (defaults to PECL::HTTP/version (PHP/version)))
- * - headers: array, list of custom headers as associative array
- * like array("header" => "value")
- * - cookies: array, list of cookies as associative array
- * like array("cookie" => "value")
- * - cookiestore: string, path to a file where cookies are/will be stored
- * </pre>
- *
- * The optional third parameter will be filled with some additional information
- * in form af an associative array, if supplied, like the following example:
- * <pre>
- * <?php
- * array (
- * 'effective_url' => 'http://localhost',
- * 'response_code' => 403,
- * 'total_time' => 0.017,
- * 'namelookup_time' => 0.013,
- * 'connect_time' => 0.014,
- * 'pretransfer_time' => 0.014,
- * 'size_upload' => 0,
- * 'size_download' => 202,
- * 'speed_download' => 11882,
- * 'speed_upload' => 0,
- * 'header_size' => 145,
- * 'request_size' => 62,
- * 'ssl_verifyresult' => 0,
- * 'filetime' => -1,
- * 'content_length_download' => 202,
- * 'content_length_upload' => 0,
- * 'starttransfer_time' => 0.017,
- * 'content_type' => 'text/html; charset=iso-8859-1',
- * 'redirect_time' => 0,
- * 'redirect_count' => 0,
- * 'private' => '',
- * 'http_connectcode' => 0,
- * 'httpauth_avail' => 0,
- * 'proxyauth_avail' => 0,
- * )
- * ?>
- * </pre>
- */
-PHP_FUNCTION(http_get)
-{
- char *URL, *data = NULL;
- size_t data_len = 0;
- int URL_len;
- zval *options = NULL, *info = NULL;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (info) {
- zval_dtor(info);
- array_init(info);
- }
-
- if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
- RETURN_STRINGL(data, data_len, 0);
- } else {
- RETURN_FALSE;
- }
-}
-/* }}} */
-
-/* {{{ proto string http_head(string url[, array options[, array &info]])
- *
- * Performs an HTTP HEAD request on the suppied url.
- * Returns the HTTP response as string.
- * See http_get() for a full list of available options.
- */
-PHP_FUNCTION(http_head)
-{
- char *URL, *data = NULL;
- size_t data_len = 0;
- int URL_len;
- zval *options = NULL, *info = NULL;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (info) {
- zval_dtor(info);
- array_init(info);
- }
-
- if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
- RETURN_STRINGL(data, data_len, 0);
- } else {
- RETURN_FALSE;
- }
-}
-/* }}} */
-
-/* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
- *
- * Performs an HTTP POST request, posting data.
- * Returns the HTTP response as string.
- * See http_get() for a full list of available options.
- */
-PHP_FUNCTION(http_post_data)
-{
- char *URL, *postdata, *data = NULL;
- size_t data_len = 0;
- int postdata_len, URL_len;
- zval *options = NULL, *info = NULL;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (info) {
- zval_dtor(info);
- array_init(info);
- }
-
- if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
- RETURN_STRINGL(data, data_len, 0);
- } else {
- RETURN_FALSE;
- }
-}
-/* }}} */
-
-/* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
- *
- * Performs an HTTP POST request, posting www-form-urlencoded array data.
- * Returns the HTTP response as string.
- * See http_get() for a full list of available options.
- */
-PHP_FUNCTION(http_post_array)
-{
- char *URL, *data = NULL;
- size_t data_len = 0;
- int URL_len;
- zval *options = NULL, *info = NULL, *postdata;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (info) {
- zval_dtor(info);
- array_init(info);
- }
-
- if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
- RETURN_STRINGL(data, data_len, 0);
- } else {
- RETURN_FALSE;
- }
-}
-/* }}} */
-
-#endif
-/* }}} HAVE_CURL */
-
-
-/* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
- *
- * Example:
- * <pre>
- * <?php
- * if (!http_auth_basic('mike', 's3c|r3t')) {
- * die('<h1>Authorization failed!</h1>');
- * }
- * ?>
- * </pre>
- */
-PHP_FUNCTION(http_auth_basic)
-{
- char *realm = NULL, *user, *pass, *suser, *spass;
- int r_len, u_len, p_len;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (!realm) {
- realm = "Restricted";
- }
-
- if (SUCCESS != http_auth_credentials(&suser, &spass)) {
- http_auth_header("Basic", realm);
- RETURN_FALSE;
- }
-
- if (strcasecmp(suser, user)) {
- http_auth_header("Basic", realm);
- RETURN_FALSE;
- }
-
- if (strcmp(spass, pass)) {
- http_auth_header("Basic", realm);
- RETURN_FALSE;
- }
-
- RETURN_TRUE;
-}
-/* }}} */
-
-/* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
- *
- * Example:
- * <pre>
- * <?php
- * function auth_cb($user, $pass)
- * {
- * global $db;
- * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
- * if (strlen($realpass = $db->getOne($query)) {
- * return $pass === $realpass;
- * }
- * return false;
- * }
- *
- * if (!http_auth_basic_cb('auth_cb')) {
- * die('<h1>Authorization failed</h1>');
- * }
- * ?>
- * </pre>
- */
-PHP_FUNCTION(http_auth_basic_cb)
-{
- zval *cb;
- char *realm = NULL, *user, *pass;
- int r_len;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
- RETURN_FALSE;
- }
-
- if (!realm) {
- realm = "Restricted";
- }
-
- if (SUCCESS != http_auth_credentials(&user, &pass)) {
- http_auth_header("Basic", realm);
- RETURN_FALSE;
- }
- {
- zval *zparams[2] = {NULL, NULL}, retval;
- int result = 0;
-
- MAKE_STD_ZVAL(zparams[0]);
- MAKE_STD_ZVAL(zparams[1]);
- ZVAL_STRING(zparams[0], user, 0);
- ZVAL_STRING(zparams[1], pass, 0);
-
- if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
- &retval, 2, zparams TSRMLS_CC)) {
- result = Z_LVAL(retval);
- }
-
- efree(user);
- efree(pass);
- efree(zparams[0]);
- efree(zparams[1]);
-
- if (!result) {
- http_auth_header("Basic", realm);
- }
-
- RETURN_BOOL(result);
- }
-}
-/* }}}*/
-
-
-/* {{{ php_http_init_globals(zend_http_globals *) */
-static void php_http_init_globals(zend_http_globals *http_globals)
-{
- http_globals->etag_started = 0;
- http_globals->ctype = NULL;
- http_globals->etag = NULL;
- http_globals->lmod = 0;
-#ifdef HTTP_HAVE_CURL
- http_globals->curlbuf.body.data = NULL;
- http_globals->curlbuf.body.used = 0;
- http_globals->curlbuf.body.free = 0;
- http_globals->curlbuf.hdrs.data = NULL;
- http_globals->curlbuf.hdrs.used = 0;
- http_globals->curlbuf.hdrs.free = 0;
-#endif
- http_globals->allowed_methods = NULL;
-}
-/* }}} */
-
-/* {{{ static inline STATUS http_check_allowed_methods(char *, int) */