2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
25 #include "ext/standard/info.h"
26 #include "ext/session/php_session.h"
27 #include "ext/standard/php_string.h"
30 #include "php_http_std_defs.h"
31 #include "php_http_api.h"
32 #include "php_http_auth_api.h"
33 #include "php_http_request_api.h"
34 #include "php_http_cache_api.h"
35 #include "php_http_request_api.h"
36 #include "php_http_date_api.h"
37 #include "php_http_headers_api.h"
38 #include "php_http_message_api.h"
39 #include "php_http_send_api.h"
40 #include "php_http_url_api.h"
42 #include "phpstr/phpstr.h"
44 ZEND_EXTERN_MODULE_GLOBALS(http
)
46 /* {{{ proto string http_date([int timestamp])
48 * This function returns a valid HTTP date regarding RFC 822/1123
49 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
52 PHP_FUNCTION(http_date
)
56 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
61 t
= (long) time(NULL
);
64 RETURN_STRING(http_date(t
), 0);
68 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
70 * This function returns an absolute URI constructed from url.
71 * If the url is already abolute but a different proto was supplied,
72 * only the proto part of the URI will be updated. If url has no
73 * path specified, the path of the current REQUEST_URI will be taken.
74 * The host will be taken either from the Host HTTP header of the client
75 * the SERVER_NAME or just localhost if prior are not available.
79 * url = "page.php" => http://www.example.com/current/path/page.php
80 * url = "/page.php" => http://www.example.com/page.php
81 * url = "/page.php", proto = "https" => https://www.example.com/page.php
85 PHP_FUNCTION(http_absolute_uri
)
87 char *url
= NULL
, *proto
= NULL
, *host
= NULL
;
88 int url_len
= 0, proto_len
= 0, host_len
= 0;
91 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
95 RETURN_STRING(http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
), 0);
99 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
101 * This function negotiates the clients preferred language based on its
102 * Accept-Language HTTP header. It returns the negotiated language or
103 * the default language if none match.
105 * The qualifier is recognized and languages without qualifier are rated highest.
107 * The supported parameter is expected to be an array having
108 * the supported languages as array values.
122 * include './langs/'. http_negotiate_language($langs) .'.php';
127 PHP_FUNCTION(http_negotiate_language
)
133 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
141 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
145 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
147 * This function negotiates the clients preferred charset based on its
148 * Accept-Charset HTTP header. It returns the negotiated charset or
149 * the default charset if none match.
151 * The qualifier is recognized and charset without qualifier are rated highest.
153 * The supported parameter is expected to be an array having
154 * the supported charsets as array values.
160 * 'iso-8859-1', // default
165 * $pref = http_negotiate_charset($charsets);
166 * if (!strcmp($pref, 'iso-8859-1')) {
167 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
168 * iconv_set_encoding('output_encoding', $pref);
169 * ob_start('ob_iconv_handler');
174 PHP_FUNCTION(http_negotiate_charset
)
180 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
188 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
192 /* {{{ proto bool http_send_status(int status)
194 * Send HTTP status code.
197 PHP_FUNCTION(http_send_status
)
201 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
204 if (status
< 100 || status
> 510) {
205 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
209 RETURN_SUCCESS(http_send_status(status
));
213 /* {{{ proto bool http_send_last_modified([int timestamp])
215 * This converts the given timestamp to a valid HTTP date and
216 * sends it as "Last-Modified" HTTP header. If timestamp is
217 * omitted, current time is sent.
220 PHP_FUNCTION(http_send_last_modified
)
224 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
229 t
= (long) time(NULL
);
232 RETURN_SUCCESS(http_send_last_modified(t
));
236 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
238 * Sets the content type.
241 PHP_FUNCTION(http_send_content_type
)
246 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
251 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", lenof("application/x-octetstream")));
253 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
257 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
259 * Set the Content Disposition. The Content-Disposition header is very useful
260 * if the data actually sent came from a file or something similar, that should
261 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
264 PHP_FUNCTION(http_send_content_disposition
)
268 zend_bool send_inline
= 0;
270 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
273 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
277 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
279 * Matches the given timestamp against the clients "If-Modified-Since" resp.
280 * "If-Unmodified-Since" HTTP headers.
283 PHP_FUNCTION(http_match_modified
)
286 zend_bool for_range
= 0;
288 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
292 // current time if not supplied (senseless though)
294 t
= (long) time(NULL
);
298 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
300 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
304 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
306 * This matches the given ETag against the clients
307 * "If-Match" resp. "If-None-Match" HTTP headers.
310 PHP_FUNCTION(http_match_etag
)
314 zend_bool for_range
= 0;
316 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
321 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
323 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
327 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
329 * If timestamp_or_expires is greater than 0, it is handled as timestamp
330 * and will be sent as date of last modification. If it is 0 or omitted,
331 * the current time will be sent as Last-Modified date. If it's negative,
332 * it is handled as expiration time in seconds, which means that if the
333 * requested last modification date is not between the calculated timespan,
334 * the Last-Modified header is updated and the actual body will be sent.
337 PHP_FUNCTION(http_cache_last_modified
)
339 long last_modified
= 0, send_modified
= 0, t
;
342 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
346 t
= (long) time(NULL
);
349 if (!last_modified
) {
350 /* does the client have? (att: caching "forever") */
351 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
352 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
353 /* send current time */
357 /* negative value is supposed to be expiration time */
358 } else if (last_modified
< 0) {
361 /* send supplied time explicitly */
363 send_modified
= last_modified
;
366 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
370 /* {{{ proto bool http_cache_etag([string etag])
372 * This function attempts to cache the HTTP body based on an ETag,
373 * either supplied or generated through calculation of the MD5
374 * checksum of the output (uses output buffering).
376 * If clients "If-None-Match" header matches the supplied/calculated
377 * ETag, the body is considered cached on the clients side and
378 * a "304 Not Modified" status code is issued.
381 PHP_FUNCTION(http_cache_etag
)
386 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
390 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
394 /* {{{ proto string ob_etaghandler(string data, int mode)
396 * For use with ob_start().
398 PHP_FUNCTION(ob_etaghandler
)
404 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
408 Z_TYPE_P(return_value
) = IS_STRING
;
409 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), &Z_STRLEN_P(return_value
), mode
);
413 /* {{{ proto void http_throttle(double sec[, long bytes = 2097152])
415 * Use with http_send() API.
421 * # http_throttle(1, 20000);
422 * # http_throttle(0.5, 10000);
423 * # http_throttle(0.1, 2000);
424 * http_send_file('document.pdf');
428 PHP_FUNCTION(http_throttle
)
433 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "dl", &interval
, &chunk_size
)) {
437 HTTP_G(send
).throttle_delay
= interval
;
438 HTTP_G(send
).buffer_size
= chunk_size
;
442 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
444 * Redirect to a given url.
445 * The supplied url will be expanded with http_absolute_uri(), the params array will
446 * be treated with http_build_query() and the session identification will be appended
447 * if session is true.
449 * Depending on permanent the redirection will be issued with a permanent
450 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
453 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
454 * if the client doesn't redirect immediatly.
456 PHP_FUNCTION(http_redirect
)
459 size_t query_len
= 0;
460 zend_bool session
= 0, permanent
= 0;
462 char *query
= NULL
, *url
= NULL
, *URI
,
463 LOC
[HTTP_URI_MAXLEN
+ sizeof("Location: ")],
464 RED
[HTTP_URI_MAXLEN
* 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
466 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
470 /* append session info */
471 if (session
&& (PS(session_status
) == php_session_active
)) {
473 MAKE_STD_ZVAL(params
);
476 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
477 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not append session information");
481 /* treat params array with http_build_query() */
483 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
488 URI
= http_absolute_uri(url
);
491 snprintf(LOC
, HTTP_URI_MAXLEN
+ sizeof("Location: "), "Location: %s?%s", URI
, query
);
492 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
495 snprintf(LOC
, HTTP_URI_MAXLEN
+ sizeof("Location: "), "Location: %s", URI
);
496 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
500 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
501 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
508 /* {{{ proto bool http_send_data(string data)
510 * Sends raw data with support for (multiple) range requests.
513 PHP_FUNCTION(http_send_data
)
517 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
521 convert_to_string_ex(&zdata
);
522 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
526 /* {{{ proto bool http_send_file(string file)
528 * Sends a file with support for (multiple) range requests.
531 PHP_FUNCTION(http_send_file
)
536 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
543 RETURN_SUCCESS(http_send_file(file
));
547 /* {{{ proto bool http_send_stream(resource stream)
549 * Sends an already opened stream with support for (multiple) range requests.
552 PHP_FUNCTION(http_send_stream
)
557 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
561 php_stream_from_zval(file
, &zstream
);
562 RETURN_SUCCESS(http_send_stream(file
));
566 /* {{{ proto string http_chunked_decode(string encoded)
568 * This function decodes a string that was HTTP-chunked encoded.
569 * Returns false on failure.
571 PHP_FUNCTION(http_chunked_decode
)
573 char *encoded
= NULL
, *decoded
= NULL
;
574 int encoded_len
= 0, decoded_len
= 0;
576 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
580 if (NULL
!= http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
581 RETURN_STRINGL(decoded
, decoded_len
, 0);
588 /* {{{ proto array http_split_response(string http_response)
590 * This function splits an HTTP response into an array with headers and the
591 * content body. The returned array may look simliar to the following example:
597 * 'Response Status' => '200 Ok',
598 * 'Content-Type' => 'text/plain',
599 * 'Content-Language' => 'en-US'
601 * 1 => "Hello World!"
606 PHP_FUNCTION(http_split_response
)
608 char *response
, *body
;
613 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &response
, &response_len
) != SUCCESS
) {
617 MAKE_STD_ZVAL(zheaders
);
618 array_init(zheaders
);
620 if (SUCCESS
!= http_split_response(response
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
621 http_error(E_WARNING
, HTTP_E_PARSE
, "Could not parse HTTP response");
625 array_init(return_value
);
626 add_index_zval(return_value
, 0, zheaders
);
627 add_index_stringl(return_value
, 1, body
, body_len
, 0);
631 /* {{{ proto array http_parse_headers(string header)
634 PHP_FUNCTION(http_parse_headers
)
639 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
643 array_init(return_value
);
644 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
645 http_error(E_WARNING
, HTTP_E_PARSE
, "Could not parse HTTP headers");
646 zval_dtor(return_value
);
652 /* {{{ proto array http_get_request_headers(void)
654 * Get a list of incoming HTTP headers.
656 PHP_FUNCTION(http_get_request_headers
)
660 array_init(return_value
);
661 http_get_request_headers(return_value
);
665 /* {{{ proto string http_get_request_body(void)
667 * Get the raw request body (e.g. POST or PUT data).
669 PHP_FUNCTION(http_get_request_body
)
676 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
677 RETURN_STRINGL(body
, (int) length
, 0);
684 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
686 * Match an incoming HTTP header.
688 PHP_FUNCTION(http_match_request_header
)
690 char *header
, *value
;
691 int header_len
, value_len
;
692 zend_bool match_case
= 0;
694 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
698 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
703 #ifdef HTTP_HAVE_CURL
705 /* {{{ proto string http_get(string url[, array options[, array &info]])
707 * Performs an HTTP GET request on the supplied url.
709 * The second parameter is expected to be an associative
710 * array where the following keys will be recognized:
712 * - redirect: int, whether and how many redirects to follow
713 * - unrestrictedauth: bool, whether to continue sending credentials on
714 * redirects to a different host
715 * - proxyhost: string, proxy host in "host[:port]" format
716 * - proxyport: int, use another proxy port as specified in proxyhost
717 * - proxyauth: string, proxy credentials in "user:pass" format
718 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
719 * - httpauth: string, http credentials in "user:pass" format
720 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
721 * - compress: bool, whether to allow gzip/deflate content encoding
723 * - port: int, use another port as specified in the url
724 * - referer: string, the referer to sends
725 * - useragent: string, the user agent to send
726 * (defaults to PECL::HTTP/version (PHP/version)))
727 * - headers: array, list of custom headers as associative array
728 * like array("header" => "value")
729 * - cookies: array, list of cookies as associative array
730 * like array("cookie" => "value")
731 * - cookiestore: string, path to a file where cookies are/will be stored
732 * - resume: int, byte offset to start the download from;
733 * if the server supports ranges
734 * - maxfilesize: int, maximum file size that should be downloaded;
735 * has no effect, if the size of the requested entity is not known
736 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
737 * - timeout: int, seconds the request may take
738 * - connecttimeout: int, seconds the connect may take
739 * - onprogress: mixed, progress callback
742 * The optional third parameter will be filled with some additional information
743 * in form af an associative array, if supplied, like the following example:
747 * 'effective_url' => 'http://localhost',
748 * 'response_code' => 403,
749 * 'total_time' => 0.017,
750 * 'namelookup_time' => 0.013,
751 * 'connect_time' => 0.014,
752 * 'pretransfer_time' => 0.014,
753 * 'size_upload' => 0,
754 * 'size_download' => 202,
755 * 'speed_download' => 11882,
756 * 'speed_upload' => 0,
757 * 'header_size' => 145,
758 * 'request_size' => 62,
759 * 'ssl_verifyresult' => 0,
761 * 'content_length_download' => 202,
762 * 'content_length_upload' => 0,
763 * 'starttransfer_time' => 0.017,
764 * 'content_type' => 'text/html; charset=iso-8859-1',
765 * 'redirect_time' => 0,
766 * 'redirect_count' => 0,
768 * 'http_connectcode' => 0,
769 * 'httpauth_avail' => 0,
770 * 'proxyauth_avail' => 0,
775 PHP_FUNCTION(http_get
)
777 zval
*options
= NULL
, *info
= NULL
;
782 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
791 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
792 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
793 RETURN_PHPSTR_VAL(response
);
800 /* {{{ proto string http_head(string url[, array options[, array &info]])
802 * Performs an HTTP HEAD request on the suppied url.
803 * Returns the HTTP response as string.
804 * See http_get() for a full list of available options.
806 PHP_FUNCTION(http_head
)
808 zval
*options
= NULL
, *info
= NULL
;
813 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
822 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
823 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
824 RETURN_PHPSTR_VAL(response
);
831 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
833 * Performs an HTTP POST request, posting data.
834 * Returns the HTTP response as string.
835 * See http_get() for a full list of available options.
837 PHP_FUNCTION(http_post_data
)
839 zval
*options
= NULL
, *info
= NULL
;
840 char *URL
, *postdata
;
841 int postdata_len
, URL_len
;
843 http_request_body body
;
845 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
854 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
855 body
.data
= postdata
;
856 body
.size
= postdata_len
;
858 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
859 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
860 RETVAL_PHPSTR_VAL(response
);
867 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
869 * Performs an HTTP POST request, posting www-form-urlencoded array data.
870 * Returns the HTTP response as string.
871 * See http_get() for a full list of available options.
873 PHP_FUNCTION(http_post_fields
)
875 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
879 http_request_body body
;
881 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
885 if (SUCCESS
!= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
894 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
895 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
896 RETVAL_PHPSTR_VAL(response
);
900 http_request_body_dtor(&body
);
904 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
906 * Performs an HTTP PUT request, uploading file.
907 * Returns the HTTP response as string.
908 * See http_get() for a full list of available options.
910 PHP_FUNCTION(http_put_file
)
914 zval
*options
= NULL
, *info
= NULL
;
917 php_stream_statbuf ssb
;
918 http_request_body body
;
920 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
924 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
927 if (php_stream_stat(stream
, &ssb
)) {
928 php_stream_close(stream
);
937 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
939 body
.size
= ssb
.sb
.st_size
;
941 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
942 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
943 RETVAL_PHPSTR_VAL(response
);
947 http_request_body_dtor(&body
);
951 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
953 * Performs an HTTP PUT request, uploading stream.
954 * Returns the HTTP response as string.
955 * See http_get() for a full list of available options.
957 PHP_FUNCTION(http_put_stream
)
959 zval
*resource
, *options
= NULL
, *info
= NULL
;
964 php_stream_statbuf ssb
;
965 http_request_body body
;
967 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
971 php_stream_from_zval(stream
, &resource
);
972 if (php_stream_stat(stream
, &ssb
)) {
981 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
983 body
.size
= ssb
.sb
.st_size
;
985 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
986 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
987 RETURN_PHPSTR_VAL(response
);
994 /* {{{ proto long http_request_method_register(string method)
996 * Register a custom request method.
998 PHP_FUNCTION(http_request_method_register
)
1002 unsigned long existing
;
1004 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1007 if (existing
= http_request_method_exists(1, 0, method
)) {
1008 RETURN_LONG((long) existing
);
1011 RETVAL_LONG((long) http_request_method_register(method
));
1015 /* {{{ proto bool http_request_method_unregister(mixed method)
1017 * Unregister a previously registered custom request method.
1019 PHP_FUNCTION(http_request_method_unregister
)
1023 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1027 switch (Z_TYPE_P(method
))
1030 convert_to_string(method
);
1032 #include "zend_operators.h"
1033 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1034 convert_to_long(method
);
1037 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1041 ZVAL_LONG(method
, (long)mn
);
1044 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1051 /* {{{ proto long http_request_method_exists(mixed method)
1053 * Check if a request method is registered (or available by default).
1055 PHP_FUNCTION(http_request_method_exists
)
1060 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1064 switch (Z_TYPE_P(method
))
1067 convert_to_string(method
);
1069 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1070 convert_to_long(method
);
1072 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1075 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1083 /* {{{ proto string http_request_method_name(long method)
1085 * Get the literal string representation of a standard or registered request method.
1087 PHP_FUNCTION(http_request_method_name
)
1092 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1096 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method
)), 0);
1104 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1109 * if (!http_auth_basic('mike', 's3c|r3t')) {
1110 * die('<h1>Authorization failed!</h1>');
1115 PHP_FUNCTION(http_auth_basic
)
1117 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
1118 int r_len
, u_len
, p_len
;
1120 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
1125 realm
= "Restricted";
1128 if (SUCCESS
!= http_auth_basic_credentials(&suser
, &spass
)) {
1129 http_auth_basic_header(realm
);
1133 if (strcasecmp(suser
, user
)) {
1134 http_auth_basic_header(realm
);
1138 if (strcmp(spass
, pass
)) {
1139 http_auth_basic_header(realm
);
1147 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1152 * function auth_cb($user, $pass)
1155 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1156 * if (strlen($realpass = $db->getOne($query)) {
1157 * return $pass === $realpass;
1161 * if (!http_auth_basic_cb('auth_cb')) {
1162 * die('<h1>Authorization failed</h1>');
1167 PHP_FUNCTION(http_auth_basic_cb
)
1170 char *realm
= NULL
, *user
, *pass
;
1173 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
1178 realm
= "Restricted";
1181 if (SUCCESS
!= http_auth_basic_credentials(&user
, &pass
)) {
1182 http_auth_basic_header(realm
);
1186 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
1189 MAKE_STD_ZVAL(zparams
[0]);
1190 MAKE_STD_ZVAL(zparams
[1]);
1191 ZVAL_STRING(zparams
[0], user
, 0);
1192 ZVAL_STRING(zparams
[1], pass
, 0);
1194 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
1195 &retval
, 2, zparams TSRMLS_CC
)) {
1196 result
= Z_LVAL(retval
);
1205 http_auth_basic_header(realm
);
1208 RETURN_BOOL(result
);
1213 /* {{{ Sara Golemons http_build_query() */
1214 #ifndef ZEND_ENGINE_2
1216 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1217 Generates a form-encoded query string from an associative array or object. */
1218 PHP_FUNCTION(http_build_query
)
1221 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1222 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1225 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1229 if (Z_TYPE_P(formdata
) != IS_ARRAY
&& Z_TYPE_P(formdata
) != IS_OBJECT
) {
1230 http_error(E_WARNING
, HTTP_E_PARAM
, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1235 arg_sep
= HTTP_URL_ARGSEP
;
1238 formstr
= phpstr_new();
1239 if (SUCCESS
!= http_urlencode_hash_implementation_ex(HASH_OF(formdata
), formstr
, arg_sep
, prefix
, prefix_len
, NULL
, 0, NULL
, 0, (Z_TYPE_P(formdata
) == IS_OBJECT
? formdata
: NULL
))) {
1240 phpstr_free(formstr
);
1244 if (!formstr
->used
) {
1245 phpstr_free(formstr
);
1249 RETURN_PHPSTR_PTR(formstr
);
1252 #endif /* !ZEND_ENGINE_2 */
1255 PHP_FUNCTION(http_test
)
1265 * vim600: noet sw=4 ts=4 fdm=marker
1266 * vim<600: noet sw=4 ts=4