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 +----------------------------------------------------------------------+
24 #include "ext/standard/info.h"
25 #include "ext/session/php_session.h"
26 #include "ext/standard/php_string.h"
30 #include "phpstr/phpstr.h"
33 #include "php_http_std_defs.h"
34 #include "php_http_api.h"
35 #include "php_http_auth_api.h"
36 #include "php_http_request_api.h"
37 #include "php_http_cache_api.h"
38 #include "php_http_request_api.h"
39 #include "php_http_date_api.h"
40 #include "php_http_headers_api.h"
41 #include "php_http_message_api.h"
42 #include "php_http_send_api.h"
43 #include "php_http_url_api.h"
45 ZEND_EXTERN_MODULE_GLOBALS(http
)
47 /* {{{ proto string http_date([int timestamp])
49 * This function returns a valid HTTP date regarding RFC 822/1123
50 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
53 PHP_FUNCTION(http_date
)
57 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
62 t
= (long) time(NULL
);
65 RETURN_STRING(http_date(t
), 0);
69 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
71 * This function returns an absolute URI constructed from url.
72 * If the url is already abolute but a different proto was supplied,
73 * only the proto part of the URI will be updated. If url has no
74 * path specified, the path of the current REQUEST_URI will be taken.
75 * The host will be taken either from the Host HTTP header of the client
76 * the SERVER_NAME or just localhost if prior are not available.
80 * url = "page.php" => http://www.example.com/current/path/page.php
81 * url = "/page.php" => http://www.example.com/page.php
82 * url = "/page.php", proto = "https" => https://www.example.com/page.php
86 PHP_FUNCTION(http_absolute_uri
)
88 char *url
= NULL
, *proto
= NULL
, *host
= NULL
;
89 int url_len
= 0, proto_len
= 0, host_len
= 0;
92 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
96 RETURN_STRING(http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
), 0);
100 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
102 * This function negotiates the clients preferred language based on its
103 * Accept-Language HTTP header. It returns the negotiated language or
104 * the default language if none match.
106 * The qualifier is recognized and languages without qualifier are rated highest.
108 * The supported parameter is expected to be an array having
109 * the supported languages as array values.
123 * include './langs/'. http_negotiate_language($langs) .'.php';
128 PHP_FUNCTION(http_negotiate_language
)
134 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
142 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
146 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
148 * This function negotiates the clients preferred charset based on its
149 * Accept-Charset HTTP header. It returns the negotiated charset or
150 * the default charset if none match.
152 * The qualifier is recognized and charset without qualifier are rated highest.
154 * The supported parameter is expected to be an array having
155 * the supported charsets as array values.
161 * 'iso-8859-1', // default
166 * $pref = http_negotiate_charset($charsets);
167 * if (!strcmp($pref, 'iso-8859-1')) {
168 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
169 * iconv_set_encoding('output_encoding', $pref);
170 * ob_start('ob_iconv_handler');
175 PHP_FUNCTION(http_negotiate_charset
)
181 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
189 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
193 /* {{{ proto bool http_send_status(int status)
195 * Send HTTP status code.
198 PHP_FUNCTION(http_send_status
)
202 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
205 if (status
< 100 || status
> 510) {
206 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
210 RETURN_SUCCESS(http_send_status(status
));
214 /* {{{ proto bool http_send_last_modified([int timestamp])
216 * This converts the given timestamp to a valid HTTP date and
217 * sends it as "Last-Modified" HTTP header. If timestamp is
218 * omitted, current time is sent.
221 PHP_FUNCTION(http_send_last_modified
)
225 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
230 t
= (long) time(NULL
);
233 RETURN_SUCCESS(http_send_last_modified(t
));
237 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
239 * Sets the content type.
242 PHP_FUNCTION(http_send_content_type
)
247 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
252 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", lenof("application/x-octetstream")));
254 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
258 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
260 * Set the Content Disposition. The Content-Disposition header is very useful
261 * if the data actually sent came from a file or something similar, that should
262 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
265 PHP_FUNCTION(http_send_content_disposition
)
269 zend_bool send_inline
= 0;
271 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
274 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
278 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
280 * Matches the given timestamp against the clients "If-Modified-Since" resp.
281 * "If-Unmodified-Since" HTTP headers.
284 PHP_FUNCTION(http_match_modified
)
287 zend_bool for_range
= 0;
289 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
293 // current time if not supplied (senseless though)
295 t
= (long) time(NULL
);
299 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
301 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
305 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
307 * This matches the given ETag against the clients
308 * "If-Match" resp. "If-None-Match" HTTP headers.
311 PHP_FUNCTION(http_match_etag
)
315 zend_bool for_range
= 0;
317 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
322 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
324 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
328 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
330 * If timestamp_or_expires is greater than 0, it is handled as timestamp
331 * and will be sent as date of last modification. If it is 0 or omitted,
332 * the current time will be sent as Last-Modified date. If it's negative,
333 * it is handled as expiration time in seconds, which means that if the
334 * requested last modification date is not between the calculated timespan,
335 * the Last-Modified header is updated and the actual body will be sent.
338 PHP_FUNCTION(http_cache_last_modified
)
340 long last_modified
= 0, send_modified
= 0, t
;
343 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
347 t
= (long) time(NULL
);
350 if (!last_modified
) {
351 /* does the client have? (att: caching "forever") */
352 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
353 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
354 /* send current time */
358 /* negative value is supposed to be expiration time */
359 } else if (last_modified
< 0) {
362 /* send supplied time explicitly */
364 send_modified
= last_modified
;
367 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
371 /* {{{ proto bool http_cache_etag([string etag])
373 * This function attempts to cache the HTTP body based on an ETag,
374 * either supplied or generated through calculation of the MD5
375 * checksum of the output (uses output buffering).
377 * If clients "If-None-Match" header matches the supplied/calculated
378 * ETag, the body is considered cached on the clients side and
379 * a "304 Not Modified" status code is issued.
382 PHP_FUNCTION(http_cache_etag
)
387 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
391 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
395 /* {{{ proto string ob_etaghandler(string data, int mode)
397 * For use with ob_start().
399 PHP_FUNCTION(ob_etaghandler
)
405 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
409 Z_TYPE_P(return_value
) = IS_STRING
;
410 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), &Z_STRLEN_P(return_value
), mode
);
414 /* {{{ proto void http_throttle(double sec[, long bytes = 2097152])
416 * Use with http_send() API.
422 * # http_throttle(1, 20000);
423 * # http_throttle(0.5, 10000);
424 * # http_throttle(0.1, 2000);
425 * http_send_file('document.pdf');
429 PHP_FUNCTION(http_throttle
)
434 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "dl", &interval
, &chunk_size
)) {
438 HTTP_G(send
).throttle_delay
= interval
;
439 HTTP_G(send
).buffer_size
= chunk_size
;
443 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
445 * Redirect to a given url.
446 * The supplied url will be expanded with http_absolute_uri(), the params array will
447 * be treated with http_build_query() and the session identification will be appended
448 * if session is true.
450 * Depending on permanent the redirection will be issued with a permanent
451 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
454 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
455 * if the client doesn't redirect immediatly.
457 PHP_FUNCTION(http_redirect
)
460 size_t query_len
= 0;
461 zend_bool session
= 0, permanent
= 0;
463 char *query
, *url
, *URI
,
464 LOC
[HTTP_URI_MAXLEN
+ sizeof("Location: ")],
465 RED
[HTTP_URI_MAXLEN
* 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
467 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
471 /* append session info */
472 if (session
&& (PS(session_status
) == php_session_active
)) {
474 MAKE_STD_ZVAL(params
);
477 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
478 http_error(E_WARNING
, HTTP_E_ENCODE
, "Could not append session information");
482 /* treat params array with http_build_query() */
484 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
489 URI
= http_absolute_uri(url
);
492 snprintf(LOC
, HTTP_URI_MAXLEN
+ sizeof("Location: "), "Location: %s?%s", URI
, query
);
493 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
496 snprintf(LOC
, HTTP_URI_MAXLEN
+ sizeof("Location: "), "Location: %s", URI
);
497 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
501 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
502 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
509 /* {{{ proto bool http_send_data(string data)
511 * Sends raw data with support for (multiple) range requests.
514 PHP_FUNCTION(http_send_data
)
518 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
522 convert_to_string_ex(&zdata
);
523 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
527 /* {{{ proto bool http_send_file(string file)
529 * Sends a file with support for (multiple) range requests.
532 PHP_FUNCTION(http_send_file
)
537 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
544 RETURN_SUCCESS(http_send_file(file
));
548 /* {{{ proto bool http_send_stream(resource stream)
550 * Sends an already opened stream with support for (multiple) range requests.
553 PHP_FUNCTION(http_send_stream
)
558 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
562 php_stream_from_zval(file
, &zstream
);
563 RETURN_SUCCESS(http_send_stream(file
));
567 /* {{{ proto string http_chunked_decode(string encoded)
569 * This function decodes a string that was HTTP-chunked encoded.
570 * Returns false on failure.
572 PHP_FUNCTION(http_chunked_decode
)
574 char *encoded
= NULL
, *decoded
= NULL
;
575 int encoded_len
= 0, decoded_len
= 0;
577 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
581 if (NULL
!= http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
582 RETURN_STRINGL(decoded
, decoded_len
, 0);
589 /* {{{ proto array http_split_response(string http_response)
591 * This function splits an HTTP response into an array with headers and the
592 * content body. The returned array may look simliar to the following example:
598 * 'Response Status' => '200 Ok',
599 * 'Content-Type' => 'text/plain',
600 * 'Content-Language' => 'en-US'
602 * 1 => "Hello World!"
607 PHP_FUNCTION(http_split_response
)
609 char *response
, *body
;
614 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &response
, &response_len
) != SUCCESS
) {
618 MAKE_STD_ZVAL(zheaders
);
619 array_init(zheaders
);
621 if (SUCCESS
!= http_split_response(response
, response_len
, Z_ARRVAL_P(zheaders
), &body
, &body_len
)) {
622 http_error(E_WARNING
, HTTP_E_PARSE
, "Could not parse HTTP response");
626 array_init(return_value
);
627 add_index_zval(return_value
, 0, zheaders
);
628 add_index_stringl(return_value
, 1, body
, body_len
, 0);
632 /* {{{ proto array http_parse_headers(string header)
635 PHP_FUNCTION(http_parse_headers
)
640 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
644 array_init(return_value
);
645 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
646 http_error(E_WARNING
, HTTP_E_PARSE
, "Could not parse HTTP headers");
647 zval_dtor(return_value
);
653 /* {{{ proto array http_get_request_headers(void)
655 * Get a list of incoming HTTP headers.
657 PHP_FUNCTION(http_get_request_headers
)
661 array_init(return_value
);
662 http_get_request_headers(return_value
);
666 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
668 * Match an incoming HTTP header.
670 PHP_FUNCTION(http_match_request_header
)
672 char *header
, *value
;
673 int header_len
, value_len
;
674 zend_bool match_case
= 0, result
= 0;
676 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
680 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
685 #ifdef HTTP_HAVE_CURL
687 /* {{{ proto string http_get(string url[, array options[, array &info]])
689 * Performs an HTTP GET request on the supplied url.
691 * The second parameter is expected to be an associative
692 * array where the following keys will be recognized:
694 * - redirect: int, whether and how many redirects to follow
695 * - unrestrictedauth: bool, whether to continue sending credentials on
696 * redirects to a different host
697 * - proxyhost: string, proxy host in "host[:port]" format
698 * - proxyport: int, use another proxy port as specified in proxyhost
699 * - proxyauth: string, proxy credentials in "user:pass" format
700 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
701 * - httpauth: string, http credentials in "user:pass" format
702 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
703 * - compress: bool, whether to allow gzip/deflate content encoding
705 * - port: int, use another port as specified in the url
706 * - referer: string, the referer to sends
707 * - useragent: string, the user agent to send
708 * (defaults to PECL::HTTP/version (PHP/version)))
709 * - headers: array, list of custom headers as associative array
710 * like array("header" => "value")
711 * - cookies: array, list of cookies as associative array
712 * like array("cookie" => "value")
713 * - cookiestore: string, path to a file where cookies are/will be stored
714 * - resume: int, byte offset to start the download from;
715 * if the server supports ranges
716 * - maxfilesize: int, maximum file size that should be downloaded;
717 * has no effect, if the size of the requested entity is not known
718 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
719 * - timeout: int, seconds the request may take
720 * - connecttimeout: int, seconds the connect may take
721 * - onprogress: mixed, progress callback
722 * - ondebug: mixed, debug callback
725 * The optional third parameter will be filled with some additional information
726 * in form af an associative array, if supplied, like the following example:
730 * 'effective_url' => 'http://localhost',
731 * 'response_code' => 403,
732 * 'total_time' => 0.017,
733 * 'namelookup_time' => 0.013,
734 * 'connect_time' => 0.014,
735 * 'pretransfer_time' => 0.014,
736 * 'size_upload' => 0,
737 * 'size_download' => 202,
738 * 'speed_download' => 11882,
739 * 'speed_upload' => 0,
740 * 'header_size' => 145,
741 * 'request_size' => 62,
742 * 'ssl_verifyresult' => 0,
744 * 'content_length_download' => 202,
745 * 'content_length_upload' => 0,
746 * 'starttransfer_time' => 0.017,
747 * 'content_type' => 'text/html; charset=iso-8859-1',
748 * 'redirect_time' => 0,
749 * 'redirect_count' => 0,
751 * 'http_connectcode' => 0,
752 * 'httpauth_avail' => 0,
753 * 'proxyauth_avail' => 0,
758 PHP_FUNCTION(http_get
)
760 zval
*options
= NULL
, *info
= NULL
;
765 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
774 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
775 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
776 RETURN_PHPSTR_VAL(response
);
783 /* {{{ proto string http_head(string url[, array options[, array &info]])
785 * Performs an HTTP HEAD request on the suppied url.
786 * Returns the HTTP response as string.
787 * See http_get() for a full list of available options.
789 PHP_FUNCTION(http_head
)
791 zval
*options
= NULL
, *info
= NULL
;
796 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
805 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
806 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
807 RETURN_PHPSTR_VAL(response
);
814 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
816 * Performs an HTTP POST request, posting data.
817 * Returns the HTTP response as string.
818 * See http_get() for a full list of available options.
820 PHP_FUNCTION(http_post_data
)
822 zval
*options
= NULL
, *info
= NULL
;
823 char *URL
, *postdata
;
824 int postdata_len
, URL_len
;
826 http_request_body body
;
828 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
837 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
838 body
.data
= postdata
;
839 body
.size
= postdata_len
;
841 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
842 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
843 RETVAL_PHPSTR_VAL(response
);
850 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
852 * Performs an HTTP POST request, posting www-form-urlencoded array data.
853 * Returns the HTTP response as string.
854 * See http_get() for a full list of available options.
856 PHP_FUNCTION(http_post_fields
)
858 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
862 http_request_body body
;
864 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
868 if (SUCCESS
!= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
877 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
878 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
879 RETVAL_PHPSTR_VAL(response
);
883 http_request_body_dtor(&body
);
887 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
889 * Performs an HTTP PUT request, uploading file.
890 * Returns the HTTP response as string.
891 * See http_get() for a full list of available options.
893 PHP_FUNCTION(http_put_file
)
897 zval
*options
= NULL
, *info
= NULL
;
900 php_stream_statbuf ssb
;
901 http_request_body body
;
903 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
907 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
910 if (php_stream_stat(stream
, &ssb
)) {
911 php_stream_close(stream
);
920 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
922 body
.size
= ssb
.sb
.st_size
;
924 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
925 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
926 RETVAL_PHPSTR_VAL(response
);
930 http_request_body_dtor(&body
);
934 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
936 * Performs an HTTP PUT request, uploading stream.
937 * Returns the HTTP response as string.
938 * See http_get() for a full list of available options.
940 PHP_FUNCTION(http_put_stream
)
942 zval
*resource
, *options
= NULL
, *info
= NULL
;
947 php_stream_statbuf ssb
;
948 http_request_body body
;
950 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
954 php_stream_from_zval(stream
, &resource
);
955 if (php_stream_stat(stream
, &ssb
)) {
964 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
966 body
.size
= ssb
.sb
.st_size
;
968 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
969 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
970 RETURN_PHPSTR_VAL(response
);
977 /* {{{ proto long http_request_method_register(string method)
979 * Register a custom request method.
981 PHP_FUNCTION(http_request_method_register
)
985 unsigned long existing
;
987 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
990 if (existing
= http_request_method_exists(1, 0, method
)) {
991 RETURN_LONG((long) existing
);
994 RETVAL_LONG((long) http_request_method_register(method
));
998 /* {{{ proto bool http_request_method_unregister(mixed method)
1000 * Unregister a previously registered custom request method.
1002 PHP_FUNCTION(http_request_method_unregister
)
1006 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1010 switch (Z_TYPE_P(method
))
1013 convert_to_string(method
);
1015 #include "zend_operators.h"
1016 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1017 convert_to_long(method
);
1020 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1024 ZVAL_LONG(method
, (long)mn
);
1027 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1034 /* {{{ proto long http_request_method_exists(mixed method)
1036 * Check if a request method is registered (or available by default).
1038 PHP_FUNCTION(http_request_method_exists
)
1043 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1047 switch (Z_TYPE_P(method
))
1050 convert_to_string(method
);
1052 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1053 convert_to_long(method
);
1055 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1058 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1066 /* {{{ proto string http_request_method_name(long method)
1068 * Get the literal string representation of a standard or registered request method.
1070 PHP_FUNCTION(http_request_method_name
)
1075 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1079 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method
)), 0);
1087 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1092 * if (!http_auth_basic('mike', 's3c|r3t')) {
1093 * die('<h1>Authorization failed!</h1>');
1098 PHP_FUNCTION(http_auth_basic
)
1100 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
1101 int r_len
, u_len
, p_len
;
1103 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
1108 realm
= "Restricted";
1111 if (SUCCESS
!= http_auth_basic_credentials(&suser
, &spass
)) {
1112 http_auth_basic_header(realm
);
1116 if (strcasecmp(suser
, user
)) {
1117 http_auth_basic_header(realm
);
1121 if (strcmp(spass
, pass
)) {
1122 http_auth_basic_header(realm
);
1130 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1135 * function auth_cb($user, $pass)
1138 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1139 * if (strlen($realpass = $db->getOne($query)) {
1140 * return $pass === $realpass;
1144 * if (!http_auth_basic_cb('auth_cb')) {
1145 * die('<h1>Authorization failed</h1>');
1150 PHP_FUNCTION(http_auth_basic_cb
)
1153 char *realm
= NULL
, *user
, *pass
;
1156 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
1161 realm
= "Restricted";
1164 if (SUCCESS
!= http_auth_basic_credentials(&user
, &pass
)) {
1165 http_auth_basic_header(realm
);
1169 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
1172 MAKE_STD_ZVAL(zparams
[0]);
1173 MAKE_STD_ZVAL(zparams
[1]);
1174 ZVAL_STRING(zparams
[0], user
, 0);
1175 ZVAL_STRING(zparams
[1], pass
, 0);
1177 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
1178 &retval
, 2, zparams TSRMLS_CC
)) {
1179 result
= Z_LVAL(retval
);
1188 http_auth_basic_header(realm
);
1191 RETURN_BOOL(result
);
1196 /* {{{ Sara Golemons http_build_query() */
1197 #ifndef ZEND_ENGINE_2
1199 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1200 Generates a form-encoded query string from an associative array or object. */
1201 PHP_FUNCTION(http_build_query
)
1204 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1205 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1208 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1212 if (Z_TYPE_P(formdata
) != IS_ARRAY
&& Z_TYPE_P(formdata
) != IS_OBJECT
) {
1213 http_error(E_WARNING
, HTTP_E_PARAM
, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1218 arg_sep
= HTTP_URL_ARGSEP
;
1221 formstr
= phpstr_new();
1222 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
))) {
1223 phpstr_free(formstr
);
1227 if (!formstr
->used
) {
1228 phpstr_free(formstr
);
1232 RETURN_PHPSTR_PTR(formstr
);
1235 #endif /* !ZEND_ENGINE_2 */
1238 PHP_FUNCTION(http_test
)
1248 * vim600: noet sw=4 ts=4 fdm=marker
1249 * vim<600: noet sw=4 ts=4