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 +----------------------------------------------------------------------+
23 #include "zend_operators.h"
27 #include "ext/standard/info.h"
28 #include "ext/standard/php_string.h"
29 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
30 # include "ext/session/php_session.h"
34 #include "php_http_std_defs.h"
35 #include "php_http_api.h"
36 #include "php_http_request_api.h"
37 #include "php_http_cache_api.h"
38 #include "php_http_request_method_api.h"
39 #include "php_http_request_api.h"
40 #include "php_http_date_api.h"
41 #include "php_http_headers_api.h"
42 #include "php_http_message_api.h"
43 #include "php_http_send_api.h"
44 #include "php_http_url_api.h"
46 #include "phpstr/phpstr.h"
48 ZEND_EXTERN_MODULE_GLOBALS(http
)
50 /* {{{ proto string http_date([int timestamp])
52 * Compose a valid HTTP date regarding RFC 822/1123
53 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
55 * Takes an optional unix timestamp as parameter.
57 * Returns the HTTP date as string.
59 PHP_FUNCTION(http_date
)
63 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
68 t
= (long) time(NULL
);
71 RETURN_STRING(http_date(t
), 0);
75 /* {{{ proto string http_build_uri(string url[, string proto[, string host[, int port]]])
77 * Build a complete URI according to the supplied parameters.
79 * If the url is already abolute but a different proto was supplied,
80 * only the proto part of the URI will be updated. If url has no
81 * path specified, the path of the current REQUEST_URI will be taken.
82 * The host will be taken either from the Host HTTP header of the client
83 * the SERVER_NAME or just localhost if prior are not available.
84 * If a port is pecified in either the url or as sperate parameter,
85 * it will be added if it differs from te default port for HTTP(S).
87 * Returns the absolute URI as string.
92 * $uri = http_build_uri("page.php", "https", NULL, 488);
96 PHP_FUNCTION(http_build_uri
)
98 char *url
= NULL
, *proto
= NULL
, *host
= NULL
;
99 int url_len
= 0, proto_len
= 0, host_len
= 0;
102 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
106 RETURN_STRING(http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
), 0);
110 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
113 if (result = http_negotiate_ ##type(supported)) { \
118 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
119 RETVAL_STRINGL(key, key_len-1, 0); \
125 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
128 zend_hash_destroy(result); \
129 FREE_HASHTABLE(result); \
134 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
135 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
136 RETVAL_ZVAL(*value, 1, 0); \
144 FOREACH_VAL(supported, value) { \
145 convert_to_string_ex(value); \
146 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
153 /* {{{ proto string http_negotiate_language(array supported[, array &result])
155 * This function negotiates the clients preferred language based on its
156 * Accept-Language HTTP header. The qualifier is recognized and languages
157 * without qualifier are rated highest. The qualifier will be decreased by
158 * 10% for partial matches (i.e. matching primary language).
160 * Expects an array as parameter cotaining the supported languages as values.
161 * If the optional second parameter is supplied, it will be filled with an
162 * array containing the negotiation results.
164 * Returns the negotiated language or the default language (i.e. first array entry)
180 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
186 PHP_FUNCTION(http_negotiate_language
)
188 zval
*supported
, *rs_array
= NULL
;
190 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
196 array_init(rs_array
);
199 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
203 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
205 * This function negotiates the clients preferred charset based on its
206 * Accept-Charset HTTP header. The qualifier is recognized and charsets
207 * without qualifier are rated highest.
209 * Expects an array as parameter cotaining the supported charsets as values.
210 * If the optional second parameter is supplied, it will be filled with an
211 * array containing the negotiation results.
213 * Returns the negotiated charset or the default charset (i.e. first array entry)
220 * 'iso-8859-1', // default
226 * $pref = http_negotiate_charset($charsets, $result);
228 * if (strcmp($pref, 'iso-8859-1')) {
229 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
230 * iconv_set_encoding('output_encoding', $pref);
231 * ob_start('ob_iconv_handler');
238 PHP_FUNCTION(http_negotiate_charset
)
240 zval
*supported
, *rs_array
= NULL
;
242 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
248 array_init(rs_array
);
251 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
255 /* {{{ proto bool http_send_status(int status)
257 * Send HTTP status code.
259 * Expects an HTTP status code as parameter.
261 * Returns TRUE on success or FALSE on failure.
263 PHP_FUNCTION(http_send_status
)
267 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
270 if (status
< 100 || status
> 510) {
271 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
275 RETURN_SUCCESS(http_send_status(status
));
279 /* {{{ proto bool http_send_last_modified([int timestamp])
281 * Send a "Last-Modified" header with a valid HTTP date.
283 * Accepts a unix timestamp, converts it to a valid HTTP date and
284 * sends it as "Last-Modified" HTTP header. If timestamp is
285 * omitted, the current time will be sent.
287 * Returns TRUE on success or FALSE on failure.
289 PHP_FUNCTION(http_send_last_modified
)
293 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
298 t
= (long) time(NULL
);
301 RETURN_SUCCESS(http_send_last_modified(t
));
305 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
307 * Send the Content-Type of the sent entity. This is particularly important
308 * if you use the http_send() API.
310 * Accepts an optional string parameter containing the desired content type
311 * (primary/secondary).
313 * Returns TRUE on success or FALSE on failure.
315 PHP_FUNCTION(http_send_content_type
)
317 char *ct
= "application/x-octetstream";
318 int ct_len
= lenof("application/x-octetstream");
320 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
324 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
328 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
330 * Send the Content-Disposition. The Content-Disposition header is very useful
331 * if the data actually sent came from a file or something similar, that should
332 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
334 * Expects a string parameter specifying the file name the "Save as..." dialogue
335 * should display. Optionally accepts a bool parameter, which, if set to true
336 * and the user agent knows how to handle the content type, will probably not
337 * cause the popup window to be shown.
339 * Returns TRUE on success or FALSE on failure.
341 PHP_FUNCTION(http_send_content_disposition
)
345 zend_bool send_inline
= 0;
347 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
350 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
354 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
356 * Matches the given unix timestamp against the clients "If-Modified-Since"
357 * resp. "If-Unmodified-Since" HTTP headers.
359 * Accepts a unix timestamp which should be matched. Optionally accepts an
360 * additional bool parameter, which if set to true will check the header
361 * usually used to validate HTTP ranges. If timestamp is omitted, the
362 * current time will be used.
364 * Returns TRUE if timestamp represents an earlier date than the header,
367 PHP_FUNCTION(http_match_modified
)
370 zend_bool for_range
= 0;
372 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
376 // current time if not supplied (senseless though)
378 t
= (long) time(NULL
);
382 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
384 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
388 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
390 * Matches the given ETag against the clients "If-Match" resp.
391 * "If-None-Match" HTTP headers.
393 * Expects a string parameter containing the ETag to compare. Optionally
394 * accepts a bool parameter, which, if set to true, will check the header
395 * usually used to validate HTTP ranges.
397 * Retuns TRUE if ETag matches or the header contained the asterisk ("*"),
400 PHP_FUNCTION(http_match_etag
)
404 zend_bool for_range
= 0;
406 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
411 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
413 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
417 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
419 * Attempts to cache the sent entity by its last modification date.
421 * Accepts a unix timestamp as parameter which is handled as follows:
423 * If timestamp_or_expires is greater than 0, it is handled as timestamp
424 * and will be sent as date of last modification. If it is 0 or omitted,
425 * the current time will be sent as Last-Modified date. If it's negative,
426 * it is handled as expiration time in seconds, which means that if the
427 * requested last modification date is not between the calculated timespan,
428 * the Last-Modified header is updated and the actual body will be sent.
430 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
432 * A log entry will be written to the cache log if the INI entry
433 * http.cache_log is set and the cache attempt was successful.
435 PHP_FUNCTION(http_cache_last_modified
)
437 long last_modified
= 0, send_modified
= 0, t
;
440 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
444 t
= (long) time(NULL
);
447 if (!last_modified
) {
448 /* does the client have? (att: caching "forever") */
449 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
450 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
451 /* send current time */
455 /* negative value is supposed to be expiration time */
456 } else if (last_modified
< 0) {
459 /* send supplied time explicitly */
461 send_modified
= last_modified
;
464 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
468 /* {{{ proto bool http_cache_etag([string etag])
470 * Attempts to cache the sent entity by its ETag, either supplied or generated
471 * by the hash algorythm specified by the INI setting "http.etag_mode".
473 * If the clients "If-None-Match" header matches the supplied/calculated
474 * ETag, the body is considered cached on the clients side and
475 * a "304 Not Modified" status code is issued.
477 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
479 * A log entry is written to the cache log if the INI entry
480 * "http.cache_log" is set and the cache attempt was successful.
482 PHP_FUNCTION(http_cache_etag
)
487 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
491 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
495 /* {{{ proto string ob_etaghandler(string data, int mode)
497 * For use with ob_start(). Output buffer handler generating an ETag with
498 * the hash algorythm specified with the INI setting "http.etag_mode".
500 PHP_FUNCTION(ob_etaghandler
)
506 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
510 Z_TYPE_P(return_value
) = IS_STRING
;
511 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
515 /* {{{ proto void http_throttle(double sec[, int bytes = 2097152])
517 * Sets the throttle delay and send buffer size for use with http_send() API.
518 * Provides a basic throttling mechanism, which will yield the current process
519 * resp. thread until the entity has been completely sent, though.
521 * Note: This doesn't really work with the FastCGI SAPI.
523 * Expects a double parameter specifying the seconds too sleep() after
524 * each chunk sent. Additionally accepts an optional int parameter
525 * representing the chunk size in bytes.
531 * # http_throttle(1, 20000);
532 * # http_throttle(0.5, 10000);
533 * # http_throttle(0.1, 2000);
534 * http_send_file('document.pdf');
538 PHP_FUNCTION(http_throttle
)
540 long chunk_size
= HTTP_SENDBUF_SIZE
;
543 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
547 HTTP_G(send
).throttle_delay
= interval
;
548 HTTP_G(send
).buffer_size
= chunk_size
;
552 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
554 * Redirect to the given url.
556 * The supplied url will be expanded with http_build_uri(), the params array will
557 * be treated with http_build_query() and the session identification will be appended
558 * if session is true.
560 * The HTTP response code will be set according to status.
561 * You can use one of the following constants for convenience:
562 * - HTTP_REDIRECT 302 Found
563 * - HTTP_REDIRECT_PERM 301 Moved Permanently
564 * - HTTP_REDIRECT_POST 303 See Other
565 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
567 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
568 * for which redirect response code to use in which situation.
570 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
571 * if the client doesn't redirect immediatly, and the request method was
572 * another one than HEAD.
574 * Returns FALSE on failure, or *exits* on success.
576 * A log entry will be written to the redirect log, if the INI entry
577 * "http.redirect_log" is set and the redirect attempt was successful.
579 PHP_FUNCTION(http_redirect
)
582 size_t query_len
= 0;
583 zend_bool session
= 0, free_params
= 0;
586 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
588 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
592 /* append session info */
596 MAKE_STD_ZVAL(params
);
599 #ifdef HAVE_PHP_SESSION
600 # ifdef COMPILE_DL_SESSION
601 if (SUCCESS
== zend_get_module_started("session")) {
602 zval nm_retval
, id_retval
, func
;
605 INIT_PZVAL(&nm_retval
);
606 INIT_PZVAL(&id_retval
);
607 ZVAL_NULL(&nm_retval
);
608 ZVAL_NULL(&id_retval
);
610 ZVAL_STRINGL(&func
, "session_id", lenof("session_id"), 0);
611 call_user_function(EG(function_table
), NULL
, &func
, &id_retval
, 0, NULL TSRMLS_CC
);
612 ZVAL_STRINGL(&func
, "session_name", lenof("session_name"), 0);
613 call_user_function(EG(function_table
), NULL
, &func
, &nm_retval
, 0, NULL TSRMLS_CC
);
615 if ( Z_TYPE(nm_retval
) == IS_STRING
&& Z_STRLEN(nm_retval
) &&
616 Z_TYPE(id_retval
) == IS_STRING
&& Z_STRLEN(id_retval
)) {
617 if (add_assoc_stringl_ex(params
, Z_STRVAL(nm_retval
), Z_STRLEN(nm_retval
)+1,
618 Z_STRVAL(id_retval
), Z_STRLEN(id_retval
), 0) != SUCCESS
) {
619 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
624 if (PS(session_status
) == php_session_active
) {
625 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
626 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
633 /* treat params array with http_build_query() */
635 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
647 URI
= http_absolute_uri(url
);
650 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
651 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
652 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
655 spprintf(&LOC
, 0, "Location: %s", URI
);
656 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
657 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
670 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
674 /* {{{ proto bool http_send_data(string data)
676 * Sends raw data with support for (multiple) range requests.
678 * Retursn TRUE on success, or FALSE on failure.
680 PHP_FUNCTION(http_send_data
)
684 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
688 convert_to_string_ex(&zdata
);
689 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
693 /* {{{ proto bool http_send_file(string file)
695 * Sends a file with support for (multiple) range requests.
697 * Expects a string parameter referencing the file to send.
699 * Returns TRUE on success, or FALSE on failure.
701 PHP_FUNCTION(http_send_file
)
706 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
713 RETURN_SUCCESS(http_send_file(file
));
717 /* {{{ proto bool http_send_stream(resource stream)
719 * Sends an already opened stream with support for (multiple) range requests.
721 * Expects a resource parameter referncing the stream to read from.
723 * Returns TRUE on success, or FALSE on failure.
725 PHP_FUNCTION(http_send_stream
)
730 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
734 php_stream_from_zval(file
, &zstream
);
735 RETURN_SUCCESS(http_send_stream(file
));
739 /* {{{ proto string http_chunked_decode(string encoded)
741 * Decodes a string that was HTTP-chunked encoded.
743 * Expects a chunked encoded string as parameter.
745 * Returns the decoded string on success or FALSE on failure.
747 PHP_FUNCTION(http_chunked_decode
)
749 char *encoded
= NULL
, *decoded
= NULL
;
750 size_t decoded_len
= 0;
753 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
757 if (NULL
!= http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
758 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
765 /* {{{ proto object http_parse_message(string message)
767 * Parses (a) http_message(s) into a simple recursive object structure.
769 * Expects a string parameter containing a single HTTP message or
770 * several consecutive HTTP messages.
772 * Returns an hierachical object structure of the parsed messages.
777 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
782 * [httpVersion] => 1.1
783 * [responseCode] => 200
786 * [Content-Length] => 3
790 * [parentMessage] => stdClass object
793 * [httpVersion] => 1.1
794 * [responseCode] => 302
797 * [Content-Length] => 0
801 * [parentMessage] => ...
807 PHP_FUNCTION(http_parse_message
)
811 http_message
*msg
= NULL
;
813 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
817 if (msg
= http_message_parse(message
, message_len
)) {
818 object_init(return_value
);
819 http_message_tostruct_recursive(msg
, return_value
);
820 http_message_free(&msg
);
827 /* {{{ proto array http_parse_headers(string header)
829 * Parses HTTP headers into an associative array.
831 * Expects a string parameter containing HTTP headers.
833 * Returns an array on success, or FALSE on failure.
838 * $headers = "content-type: text/html; charset=UTF-8\r\n".
839 * "Server: Funky/1.0\r\n".
840 * "Set-Cookie: foo=bar\r\n".
841 * "Set-Cookie: baz=quux\r\n".
842 * "Folded: works\r\n\ttoo\r\n";
843 * print_r(http_parse_headers($headers));
847 * [Content-Type] => text/html; chatset=UTF-8
848 * [Server] => Funky/1.0
849 * [Set-Cookie] => Array
859 PHP_FUNCTION(http_parse_headers
)
864 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
868 array_init(return_value
);
869 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
870 zval_dtor(return_value
);
876 /* {{{ proto array http_get_request_headers(void)
878 * Get a list of incoming HTTP headers.
880 * Returns an associative array of incoming request headers.
882 PHP_FUNCTION(http_get_request_headers
)
886 array_init(return_value
);
887 http_get_request_headers(return_value
);
891 /* {{{ proto string http_get_request_body(void)
893 * Get the raw request body (e.g. POST or PUT data).
895 * Returns NULL when using the CLI SAPI.
897 PHP_FUNCTION(http_get_request_body
)
904 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
905 RETURN_STRINGL(body
, (int) length
, 0);
912 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
914 * Match an incoming HTTP header.
916 * Expects two string parameters representing the header name (case-insensitive)
917 * and the header value that should be compared. The case sensitivity of the
918 * header value depends on the additional optional bool parameter accepted.
920 * Returns TRUE if header value matches, else FALSE.
922 PHP_FUNCTION(http_match_request_header
)
924 char *header
, *value
;
925 int header_len
, value_len
;
926 zend_bool match_case
= 0;
928 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
932 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
937 #ifdef HTTP_HAVE_CURL
939 /* {{{ proto string http_get(string url[, array options[, array &info]])
941 * Performs an HTTP GET request on the supplied url.
943 * The second parameter, if set, is expected to be an associative
944 * array where the following keys will be recognized:
946 * - redirect: int, whether and how many redirects to follow
947 * - unrestrictedauth: bool, whether to continue sending credentials on
948 * redirects to a different host
949 * - proxyhost: string, proxy host in "host[:port]" format
950 * - proxyport: int, use another proxy port as specified in proxyhost
951 * - proxyauth: string, proxy credentials in "user:pass" format
952 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
953 * - httpauth: string, http credentials in "user:pass" format
954 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
955 * - compress: bool, whether to allow gzip/deflate content encoding
957 * - port: int, use another port as specified in the url
958 * - referer: string, the referer to sends
959 * - useragent: string, the user agent to send
960 * (defaults to PECL::HTTP/version (PHP/version)))
961 * - headers: array, list of custom headers as associative array
962 * like array("header" => "value")
963 * - cookies: array, list of cookies as associative array
964 * like array("cookie" => "value")
965 * - cookiestore: string, path to a file where cookies are/will be stored
966 * - resume: int, byte offset to start the download from;
967 * if the server supports ranges
968 * - maxfilesize: int, maximum file size that should be downloaded;
969 * has no effect, if the size of the requested entity is not known
970 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
971 * - timeout: int, seconds the request may take
972 * - connecttimeout: int, seconds the connect may take
973 * - onprogress: mixed, progress callback
976 * The optional third parameter will be filled with some additional information
977 * in form af an associative array, if supplied, like the following example:
981 * 'effective_url' => 'http://localhost',
982 * 'response_code' => 403,
983 * 'total_time' => 0.017,
984 * 'namelookup_time' => 0.013,
985 * 'connect_time' => 0.014,
986 * 'pretransfer_time' => 0.014,
987 * 'size_upload' => 0,
988 * 'size_download' => 202,
989 * 'speed_download' => 11882,
990 * 'speed_upload' => 0,
991 * 'header_size' => 145,
992 * 'request_size' => 62,
993 * 'ssl_verifyresult' => 0,
995 * 'content_length_download' => 202,
996 * 'content_length_upload' => 0,
997 * 'starttransfer_time' => 0.017,
998 * 'content_type' => 'text/html; charset=iso-8859-1',
999 * 'redirect_time' => 0,
1000 * 'redirect_count' => 0,
1001 * 'http_connectcode' => 0,
1002 * 'httpauth_avail' => 0,
1003 * 'proxyauth_avail' => 0,
1008 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1010 PHP_FUNCTION(http_get
)
1012 zval
*options
= NULL
, *info
= NULL
;
1017 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1026 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1027 if (SUCCESS
== http_get(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1028 RETURN_PHPSTR_VAL(&response
);
1035 /* {{{ proto string http_head(string url[, array options[, array &info]])
1037 * Performs an HTTP HEAD request on the supplied url.
1039 * See http_get() for a full list of available parameters and options.
1041 * Returns the HTTP response as string on success, or FALSE on failure.
1043 PHP_FUNCTION(http_head
)
1045 zval
*options
= NULL
, *info
= NULL
;
1050 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1059 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1060 if (SUCCESS
== http_head(URL
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1061 RETURN_PHPSTR_VAL(&response
);
1068 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1070 * Performs an HTTP POST requeston the supplied url.
1072 * Expects a string as second parameter containing the pre-encoded post data.
1073 * See http_get() for a full list of available parameters and options.
1075 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1077 PHP_FUNCTION(http_post_data
)
1079 zval
*options
= NULL
, *info
= NULL
;
1080 char *URL
, *postdata
;
1081 int postdata_len
, URL_len
;
1083 http_request_body body
;
1085 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1094 body
.type
= HTTP_REQUEST_BODY_CSTRING
;
1095 body
.data
= postdata
;
1096 body
.size
= postdata_len
;
1098 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1099 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1100 RETVAL_PHPSTR_VAL(&response
);
1107 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1109 * Performs an HTTP POST request on the supplied url.
1111 * Expecrs an associative array as second parameter, which will be
1112 * www-form-urlencoded. See http_get() for a full list of available options.
1114 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1116 PHP_FUNCTION(http_post_fields
)
1118 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
1122 http_request_body body
;
1124 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
1128 if (SUCCESS
!= http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
1137 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1138 if (SUCCESS
== http_post(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1139 RETVAL_PHPSTR_VAL(&response
);
1143 http_request_body_dtor(&body
);
1147 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1149 * Performs an HTTP PUT request on the supplied url.
1151 * Expects the second parameter to be a string referncing the file to upload.
1152 * See http_get() for a full list of available options.
1154 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1156 PHP_FUNCTION(http_put_file
)
1160 zval
*options
= NULL
, *info
= NULL
;
1163 php_stream_statbuf ssb
;
1164 http_request_body body
;
1166 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1170 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1173 if (php_stream_stat(stream
, &ssb
)) {
1174 php_stream_close(stream
);
1183 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1185 body
.size
= ssb
.sb
.st_size
;
1187 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1188 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1189 RETVAL_PHPSTR_VAL(&response
);
1193 http_request_body_dtor(&body
);
1197 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1199 * Performs an HTTP PUT request on the supplied url.
1201 * Expects the second parameter to be a resource referencing an already
1202 * opened stream, from which the data to upload should be read.
1203 * See http_get() for a full list of available options.
1205 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1207 PHP_FUNCTION(http_put_stream
)
1209 zval
*resource
, *options
= NULL
, *info
= NULL
;
1214 php_stream_statbuf ssb
;
1215 http_request_body body
;
1217 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1221 php_stream_from_zval(stream
, &resource
);
1222 if (php_stream_stat(stream
, &ssb
)) {
1231 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1233 body
.size
= ssb
.sb
.st_size
;
1235 phpstr_init_ex(&response
, HTTP_CURLBUF_SIZE
, 0);
1236 if (SUCCESS
== http_put(URL
, &body
, options
? Z_ARRVAL_P(options
) : NULL
, info
? Z_ARRVAL_P(info
) : NULL
, &response
)) {
1237 RETURN_PHPSTR_VAL(&response
);
1243 #endif /* HTTP_HAVE_CURL */
1246 /* {{{ proto int http_request_method_register(string method)
1248 * Register a custom request method.
1250 * Expects a string parameter containing the request method name to register.
1252 * Returns the ID of the request method on success, or FALSE on failure.
1254 PHP_FUNCTION(http_request_method_register
)
1258 unsigned long existing
;
1260 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1263 if (existing
= http_request_method_exists(1, 0, method
)) {
1264 RETURN_LONG((long) existing
);
1267 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1271 /* {{{ proto bool http_request_method_unregister(mixed method)
1273 * Unregister a previously registered custom request method.
1275 * Expects either the request method name or ID.
1277 * Returns TRUE on success, or FALSE on failure.
1279 PHP_FUNCTION(http_request_method_unregister
)
1283 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1287 switch (Z_TYPE_P(method
))
1290 convert_to_string(method
);
1292 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1293 convert_to_long(method
);
1296 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1300 ZVAL_LONG(method
, (long)mn
);
1303 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1310 /* {{{ proto int http_request_method_exists(mixed method)
1312 * Check if a request method is registered (or available by default).
1314 * Expects either the request method name or ID as parameter.
1316 * Returns TRUE if the request method is known, else FALSE.
1318 PHP_FUNCTION(http_request_method_exists
)
1323 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1327 switch (Z_TYPE_P(method
))
1330 convert_to_string(method
);
1332 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1333 convert_to_long(method
);
1335 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1338 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1346 /* {{{ proto string http_request_method_name(int method)
1348 * Get the literal string representation of a standard or registered request method.
1350 * Expects the request method ID as parameter.
1352 * Returns the request method name as string on success, or FALSE on failure.
1354 PHP_FUNCTION(http_request_method_name
)
1359 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1363 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method
)), 0);
1368 /* {{{ Sara Golemons http_build_query() */
1369 #ifndef ZEND_ENGINE_2
1371 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1372 Generates a form-encoded query string from an associative array or object. */
1373 PHP_FUNCTION(http_build_query
)
1376 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1377 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1380 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1384 if (Z_TYPE_P(formdata
) != IS_ARRAY
&& Z_TYPE_P(formdata
) != IS_OBJECT
) {
1385 http_error(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1390 arg_sep
= HTTP_URL_ARGSEP
;
1393 formstr
= phpstr_new();
1394 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
))) {
1395 phpstr_free(&formstr
);
1399 if (!formstr
->used
) {
1400 phpstr_free(&formstr
);
1404 RETURN_PHPSTR_PTR(formstr
);
1407 #endif /* !ZEND_ENGINE_2 */
1410 PHP_FUNCTION(http_test
)
1419 * vim600: noet sw=4 ts=4 fdm=marker
1420 * vim<600: noet sw=4 ts=4