2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
19 #define HTTP_WANT_CURL
24 #include "ext/standard/info.h"
25 #include "ext/standard/php_string.h"
26 #include "zend_operators.h"
28 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
29 # include "ext/session/php_session.h"
32 #include "php_http_api.h"
33 #include "php_http_cache_api.h"
34 #include "php_http_date_api.h"
35 #include "php_http_encoding_api.h"
36 #include "php_http_headers_api.h"
37 #include "php_http_message_api.h"
38 #include "php_http_request_api.h"
39 #include "php_http_request_method_api.h"
40 #include "php_http_send_api.h"
41 #include "php_http_url_api.h"
43 ZEND_EXTERN_MODULE_GLOBALS(http
)
45 /* {{{ proto string http_date([int timestamp])
47 * Compose a valid HTTP date regarding RFC 822/1123
48 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
50 * Takes an optional unix timestamp as parameter.
52 * Returns the HTTP date as string.
54 PHP_FUNCTION(http_date
)
58 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
63 t
= (long) time(NULL
);
66 RETURN_STRING(http_date(t
), 0);
70 /* {{{ proto string http_build_uri(string url[, string proto[, string host[, int port]]])
72 * Build a complete URI according to the supplied parameters.
74 * If the url is already abolute but a different proto was supplied,
75 * only the proto part of the URI will be updated. If url has no
76 * path specified, the path of the current REQUEST_URI will be taken.
77 * The host will be taken either from the Host HTTP header of the client
78 * the SERVER_NAME or just localhost if prior are not available.
79 * If a port is pecified in either the url or as sperate parameter,
80 * it will be added if it differs from te default port for HTTP(S).
82 * Returns the absolute URI as string on success or false on failure.
87 * $uri = http_build_uri("page.php", "https", NULL, 488);
91 PHP_FUNCTION(http_build_uri
)
93 char *url
= NULL
, *proto
= NULL
, *host
= NULL
, *built
= NULL
;
94 int url_len
= 0, proto_len
= 0, host_len
= 0;
97 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
101 if ((built
= http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
))) {
102 RETURN_STRING(built
, 0);
108 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
111 if ((result = http_negotiate_ ##type(supported))) { \
116 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
117 RETVAL_STRINGL(key, key_len-1, 0); \
123 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
126 zend_hash_destroy(result); \
127 FREE_HASHTABLE(result); \
132 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
133 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
134 RETVAL_ZVAL(*value, 1, 0); \
143 FOREACH_VAL(pos, supported, value) { \
144 convert_to_string_ex(value); \
145 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
152 /* {{{ proto string http_negotiate_language(array supported[, array &result])
154 * This function negotiates the clients preferred language based on its
155 * Accept-Language HTTP header. The qualifier is recognized and languages
156 * without qualifier are rated highest. The qualifier will be decreased by
157 * 10% for partial matches (i.e. matching primary language).
159 * Expects an array as parameter cotaining the supported languages as values.
160 * If the optional second parameter is supplied, it will be filled with an
161 * array containing the negotiation results.
163 * Returns the negotiated language or the default language (i.e. first array entry)
179 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
185 PHP_FUNCTION(http_negotiate_language
)
187 zval
*supported
, *rs_array
= NULL
;
189 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
195 array_init(rs_array
);
198 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
202 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
204 * This function negotiates the clients preferred charset based on its
205 * Accept-Charset HTTP header. The qualifier is recognized and charsets
206 * without qualifier are rated highest.
208 * Expects an array as parameter cotaining the supported charsets as values.
209 * If the optional second parameter is supplied, it will be filled with an
210 * array containing the negotiation results.
212 * Returns the negotiated charset or the default charset (i.e. first array entry)
219 * 'iso-8859-1', // default
225 * $pref = http_negotiate_charset($charsets, $result);
227 * if (strcmp($pref, 'iso-8859-1')) {
228 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
229 * iconv_set_encoding('output_encoding', $pref);
230 * ob_start('ob_iconv_handler');
237 PHP_FUNCTION(http_negotiate_charset
)
239 zval
*supported
, *rs_array
= NULL
;
241 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
247 array_init(rs_array
);
250 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
254 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
256 * This function negotiates the clients preferred content type based on its
257 * Accept HTTP header. The qualifier is recognized and content types
258 * without qualifier are rated highest.
260 * Expects an array as parameter cotaining the supported content types as values.
261 * If the optional second parameter is supplied, it will be filled with an
262 * array containing the negotiation results.
264 * Returns the negotiated content type or the default content type
265 * (i.e. first array entry) if none match.
270 * $ctypes = array('application/xhtml+xml', 'text/html');
271 * http_send_content_type(http_negotiate_content_type($ctypes));
275 PHP_FUNCTION(http_negotiate_content_type
)
277 zval
*supported
, *rs_array
= NULL
;
279 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
285 array_init(rs_array
);
288 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
292 /* {{{ proto bool http_send_status(int status)
294 * Send HTTP status code.
296 * Expects an HTTP status code as parameter.
298 * Returns TRUE on success or FALSE on failure.
300 PHP_FUNCTION(http_send_status
)
304 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
307 if (status
< 100 || status
> 510) {
308 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
312 RETURN_SUCCESS(http_send_status(status
));
316 /* {{{ proto bool http_send_last_modified([int timestamp])
318 * Send a "Last-Modified" header with a valid HTTP date.
320 * Accepts a unix timestamp, converts it to a valid HTTP date and
321 * sends it as "Last-Modified" HTTP header. If timestamp is
322 * omitted, the current time will be sent.
324 * Returns TRUE on success or FALSE on failure.
326 PHP_FUNCTION(http_send_last_modified
)
330 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
335 t
= (long) time(NULL
);
338 RETURN_SUCCESS(http_send_last_modified(t
));
342 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
344 * Send the Content-Type of the sent entity. This is particularly important
345 * if you use the http_send() API.
347 * Accepts an optional string parameter containing the desired content type
348 * (primary/secondary).
350 * Returns TRUE on success or FALSE on failure.
352 PHP_FUNCTION(http_send_content_type
)
354 char *ct
= "application/x-octetstream";
355 int ct_len
= lenof("application/x-octetstream");
357 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
361 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
365 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
367 * Send the Content-Disposition. The Content-Disposition header is very useful
368 * if the data actually sent came from a file or something similar, that should
369 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
371 * Expects a string parameter specifying the file name the "Save as..." dialogue
372 * should display. Optionally accepts a bool parameter, which, if set to true
373 * and the user agent knows how to handle the content type, will probably not
374 * cause the popup window to be shown.
376 * Returns TRUE on success or FALSE on failure.
378 PHP_FUNCTION(http_send_content_disposition
)
382 zend_bool send_inline
= 0;
384 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
387 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
391 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
393 * Matches the given unix timestamp against the clients "If-Modified-Since"
394 * resp. "If-Unmodified-Since" HTTP headers.
396 * Accepts a unix timestamp which should be matched. Optionally accepts an
397 * additional bool parameter, which if set to true will check the header
398 * usually used to validate HTTP ranges. If timestamp is omitted, the
399 * current time will be used.
401 * Returns TRUE if timestamp represents an earlier date than the header,
404 PHP_FUNCTION(http_match_modified
)
407 zend_bool for_range
= 0;
409 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
413 // current time if not supplied (senseless though)
415 t
= (long) time(NULL
);
419 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
421 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
425 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
427 * Matches the given ETag against the clients "If-Match" resp.
428 * "If-None-Match" HTTP headers.
430 * Expects a string parameter containing the ETag to compare. Optionally
431 * accepts a bool parameter, which, if set to true, will check the header
432 * usually used to validate HTTP ranges.
434 * Retuns TRUE if ETag matches or the header contained the asterisk ("*"),
437 PHP_FUNCTION(http_match_etag
)
441 zend_bool for_range
= 0;
443 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
448 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
450 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
454 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
456 * Attempts to cache the sent entity by its last modification date.
458 * Accepts a unix timestamp as parameter which is handled as follows:
460 * If timestamp_or_expires is greater than 0, it is handled as timestamp
461 * and will be sent as date of last modification. If it is 0 or omitted,
462 * the current time will be sent as Last-Modified date. If it's negative,
463 * it is handled as expiration time in seconds, which means that if the
464 * requested last modification date is not between the calculated timespan,
465 * the Last-Modified header is updated and the actual body will be sent.
467 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
469 * A log entry will be written to the cache log if the INI entry
470 * http.cache_log is set and the cache attempt was successful.
472 PHP_FUNCTION(http_cache_last_modified
)
474 long last_modified
= 0, send_modified
= 0, t
;
477 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
481 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
483 t
= (long) time(NULL
);
486 if (!last_modified
) {
487 /* does the client have? (att: caching "forever") */
488 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
489 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
490 /* send current time */
494 /* negative value is supposed to be expiration time */
495 } else if (last_modified
< 0) {
498 /* send supplied time explicitly */
500 send_modified
= last_modified
;
503 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
507 /* {{{ proto bool http_cache_etag([string etag])
509 * Attempts to cache the sent entity by its ETag, either supplied or generated
510 * by the hash algorythm specified by the INI setting "http.etag_mode".
512 * If the clients "If-None-Match" header matches the supplied/calculated
513 * ETag, the body is considered cached on the clients side and
514 * a "304 Not Modified" status code is issued.
516 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
518 * A log entry is written to the cache log if the INI entry
519 * "http.cache_log" is set and the cache attempt was successful.
521 PHP_FUNCTION(http_cache_etag
)
526 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
530 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
532 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
536 /* {{{ proto string ob_etaghandler(string data, int mode)
538 * For use with ob_start(). Output buffer handler generating an ETag with
539 * the hash algorythm specified with the INI setting "http.etag_mode".
541 PHP_FUNCTION(ob_etaghandler
)
547 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
551 Z_TYPE_P(return_value
) = IS_STRING
;
552 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
556 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
558 * Sets the throttle delay and send buffer size for use with http_send() API.
559 * Provides a basic throttling mechanism, which will yield the current process
560 * resp. thread until the entity has been completely sent, though.
562 * Note: This doesn't really work with the FastCGI SAPI.
564 * Expects a double parameter specifying the seconds too sleep() after
565 * each chunk sent. Additionally accepts an optional int parameter
566 * representing the chunk size in bytes.
572 * # http_throttle(1, 20000);
573 * # http_throttle(0.5, 10000);
574 * # http_throttle(0.1, 2000);
575 * http_send_file('document.pdf');
579 PHP_FUNCTION(http_throttle
)
581 long chunk_size
= HTTP_SENDBUF_SIZE
;
584 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
588 HTTP_G(send
).throttle_delay
= interval
;
589 HTTP_G(send
).buffer_size
= chunk_size
;
593 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
595 * Redirect to the given url.
597 * The supplied url will be expanded with http_build_uri(), the params array will
598 * be treated with http_build_query() and the session identification will be appended
599 * if session is true.
601 * The HTTP response code will be set according to status.
602 * You can use one of the following constants for convenience:
603 * - HTTP_REDIRECT 302 Found
604 * - HTTP_REDIRECT_PERM 301 Moved Permanently
605 * - HTTP_REDIRECT_POST 303 See Other
606 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
608 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
609 * for which redirect response code to use in which situation.
611 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
612 * if the client doesn't redirect immediatly, and the request method was
613 * another one than HEAD.
615 * Returns FALSE on failure, or *exits* on success.
617 * A log entry will be written to the redirect log, if the INI entry
618 * "http.redirect_log" is set and the redirect attempt was successful.
620 PHP_FUNCTION(http_redirect
)
623 size_t query_len
= 0;
624 zend_bool session
= 0, free_params
= 0;
627 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
629 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
633 /* append session info */
637 MAKE_STD_ZVAL(params
);
640 #ifdef HAVE_PHP_SESSION
641 # ifdef COMPILE_DL_SESSION
642 if (SUCCESS
== zend_get_module_started("session")) {
643 zval nm_retval
, id_retval
, func
;
646 INIT_PZVAL(&nm_retval
);
647 INIT_PZVAL(&id_retval
);
648 ZVAL_NULL(&nm_retval
);
649 ZVAL_NULL(&id_retval
);
651 ZVAL_STRINGL(&func
, "session_id", lenof("session_id"), 0);
652 call_user_function(EG(function_table
), NULL
, &func
, &id_retval
, 0, NULL TSRMLS_CC
);
653 ZVAL_STRINGL(&func
, "session_name", lenof("session_name"), 0);
654 call_user_function(EG(function_table
), NULL
, &func
, &nm_retval
, 0, NULL TSRMLS_CC
);
656 if ( Z_TYPE(nm_retval
) == IS_STRING
&& Z_STRLEN(nm_retval
) &&
657 Z_TYPE(id_retval
) == IS_STRING
&& Z_STRLEN(id_retval
)) {
658 if (add_assoc_stringl_ex(params
, Z_STRVAL(nm_retval
), Z_STRLEN(nm_retval
)+1,
659 Z_STRVAL(id_retval
), Z_STRLEN(id_retval
), 0) != SUCCESS
) {
660 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
665 if (PS(session_status
) == php_session_active
) {
666 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
667 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
674 /* treat params array with http_build_query() */
676 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
688 URI
= http_absolute_uri(url
);
691 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
692 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
693 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
696 spprintf(&LOC
, 0, "Location: %s", URI
);
697 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
698 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
711 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
715 /* {{{ proto bool http_send_data(string data)
717 * Sends raw data with support for (multiple) range requests.
719 * Retursn TRUE on success, or FALSE on failure.
721 PHP_FUNCTION(http_send_data
)
725 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
729 convert_to_string_ex(&zdata
);
730 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
734 /* {{{ proto bool http_send_file(string file)
736 * Sends a file with support for (multiple) range requests.
738 * Expects a string parameter referencing the file to send.
740 * Returns TRUE on success, or FALSE on failure.
742 PHP_FUNCTION(http_send_file
)
747 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
754 RETURN_SUCCESS(http_send_file(file
));
758 /* {{{ proto bool http_send_stream(resource stream)
760 * Sends an already opened stream with support for (multiple) range requests.
762 * Expects a resource parameter referncing the stream to read from.
764 * Returns TRUE on success, or FALSE on failure.
766 PHP_FUNCTION(http_send_stream
)
771 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
775 php_stream_from_zval(file
, &zstream
);
776 RETURN_SUCCESS(http_send_stream(file
));
780 /* {{{ proto string http_chunked_decode(string encoded)
782 * Decodes a string that was HTTP-chunked encoded.
784 * Expects a chunked encoded string as parameter.
786 * Returns the decoded string on success or FALSE on failure.
788 PHP_FUNCTION(http_chunked_decode
)
790 char *encoded
= NULL
, *decoded
= NULL
;
791 size_t decoded_len
= 0;
794 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
798 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
799 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
806 /* {{{ proto object http_parse_message(string message)
808 * Parses (a) http_message(s) into a simple recursive object structure.
810 * Expects a string parameter containing a single HTTP message or
811 * several consecutive HTTP messages.
813 * Returns an hierachical object structure of the parsed messages.
818 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
823 * [httpVersion] => 1.1
824 * [responseCode] => 200
827 * [Content-Length] => 3
831 * [parentMessage] => stdClass object
834 * [httpVersion] => 1.1
835 * [responseCode] => 302
838 * [Content-Length] => 0
842 * [parentMessage] => ...
848 PHP_FUNCTION(http_parse_message
)
852 http_message
*msg
= NULL
;
854 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
858 if ((msg
= http_message_parse(message
, message_len
))) {
859 object_init(return_value
);
860 http_message_tostruct_recursive(msg
, return_value
);
861 http_message_free(&msg
);
868 /* {{{ proto array http_parse_headers(string header)
870 * Parses HTTP headers into an associative array.
872 * Expects a string parameter containing HTTP headers.
874 * Returns an array on success, or FALSE on failure.
879 * $headers = "content-type: text/html; charset=UTF-8\r\n".
880 * "Server: Funky/1.0\r\n".
881 * "Set-Cookie: foo=bar\r\n".
882 * "Set-Cookie: baz=quux\r\n".
883 * "Folded: works\r\n\ttoo\r\n";
884 * print_r(http_parse_headers($headers));
888 * [Content-Type] => text/html; chatset=UTF-8
889 * [Server] => Funky/1.0
890 * [Set-Cookie] => Array
901 PHP_FUNCTION(http_parse_headers
)
906 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
910 array_init(return_value
);
911 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
912 zval_dtor(return_value
);
918 /* {{{ proto object http_parse_cookie(string cookie)
920 * Parses HTTP cookies like sent in a response into a struct.
922 * Expects a string as parameter containing the value of a Set-Cookie response header.
924 * Returns an stdClass object with the cookie params as properties on success or FALSE on failure.
929 * print_r(http_parse_cookie("foo=bar; path=/"));
940 PHP_FUNCTION(http_parse_cookie
)
945 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &cookie
, &cookie_len
)) {
949 object_init(return_value
);
950 if (SUCCESS
!= http_parse_cookie(cookie
, HASH_OF(return_value
))) {
951 zval_dtor(return_value
);
956 /* {{{ proto array http_get_request_headers(void)
958 * Get a list of incoming HTTP headers.
960 * Returns an associative array of incoming request headers.
962 PHP_FUNCTION(http_get_request_headers
)
966 array_init(return_value
);
967 http_get_request_headers(return_value
);
971 /* {{{ proto string http_get_request_body(void)
973 * Get the raw request body (e.g. POST or PUT data).
975 * Returns NULL when using the CLI SAPI.
977 PHP_FUNCTION(http_get_request_body
)
984 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
985 RETURN_STRINGL(body
, (int) length
, 0);
992 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
994 * Match an incoming HTTP header.
996 * Expects two string parameters representing the header name (case-insensitive)
997 * and the header value that should be compared. The case sensitivity of the
998 * header value depends on the additional optional bool parameter accepted.
1000 * Returns TRUE if header value matches, else FALSE.
1002 PHP_FUNCTION(http_match_request_header
)
1004 char *header
, *value
;
1005 int header_len
, value_len
;
1006 zend_bool match_case
= 0;
1008 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
1012 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
1017 #ifdef HTTP_HAVE_CURL
1019 #define RETVAL_RESPONSE_OR_BODY(request) \
1023 /* check if only the body should be returned */ \
1024 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void **) &bodyonly)) && zval_is_true(*bodyonly)) { \
1025 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1028 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1029 http_message_free(&msg); \
1032 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1036 /* {{{ proto string http_get(string url[, array options[, array &info]])
1038 * Performs an HTTP GET request on the supplied url.
1040 * The second parameter, if set, is expected to be an associative
1041 * array where the following keys will be recognized:
1043 * - redirect: int, whether and how many redirects to follow
1044 * - unrestrictedauth: bool, whether to continue sending credentials on
1045 * redirects to a different host
1046 * - proxyhost: string, proxy host in "host[:port]" format
1047 * - proxyport: int, use another proxy port as specified in proxyhost
1048 * - proxyauth: string, proxy credentials in "user:pass" format
1049 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1050 * - httpauth: string, http credentials in "user:pass" format
1051 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1052 * - compress: bool, whether to allow gzip/deflate content encoding
1053 * (defaults to true)
1054 * - port: int, use another port as specified in the url
1055 * - referer: string, the referer to send
1056 * - useragent: string, the user agent to send
1057 * (defaults to PECL::HTTP/version (PHP/version)))
1058 * - headers: array, list of custom headers as associative array
1059 * like array("header" => "value")
1060 * - cookies: array, list of cookies as associative array
1061 * like array("cookie" => "value")
1062 * - cookiestore: string, path to a file where cookies are/will be stored
1063 * - resume: int, byte offset to start the download from;
1064 * if the server supports ranges
1065 * - maxfilesize: int, maximum file size that should be downloaded;
1066 * has no effect, if the size of the requested entity is not known
1067 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1068 * - timeout: int, seconds the request may take
1069 * - connecttimeout: int, seconds the connect may take
1070 * - onprogress: mixed, progress callback
1073 * The optional third parameter will be filled with some additional information
1074 * in form af an associative array, if supplied, like the following example:
1078 * 'effective_url' => 'http://localhost',
1079 * 'response_code' => 403,
1080 * 'total_time' => 0.017,
1081 * 'namelookup_time' => 0.013,
1082 * 'connect_time' => 0.014,
1083 * 'pretransfer_time' => 0.014,
1084 * 'size_upload' => 0,
1085 * 'size_download' => 202,
1086 * 'speed_download' => 11882,
1087 * 'speed_upload' => 0,
1088 * 'header_size' => 145,
1089 * 'request_size' => 62,
1090 * 'ssl_verifyresult' => 0,
1092 * 'content_length_download' => 202,
1093 * 'content_length_upload' => 0,
1094 * 'starttransfer_time' => 0.017,
1095 * 'content_type' => 'text/html; charset=iso-8859-1',
1096 * 'redirect_time' => 0,
1097 * 'redirect_count' => 0,
1098 * 'http_connectcode' => 0,
1099 * 'httpauth_avail' => 0,
1100 * 'proxyauth_avail' => 0,
1105 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1107 PHP_FUNCTION(http_get
)
1109 zval
*options
= NULL
, *info
= NULL
;
1112 http_request request
;
1114 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1125 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
1126 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1127 http_request_exec(&request
);
1129 http_request_info(&request
, Z_ARRVAL_P(info
));
1131 RETVAL_RESPONSE_OR_BODY(request
);
1133 http_request_dtor(&request
);
1137 /* {{{ proto string http_head(string url[, array options[, array &info]])
1139 * Performs an HTTP HEAD request on the supplied url.
1141 * See http_get() for a full list of available parameters and options.
1143 * Returns the HTTP response as string on success, or FALSE on failure.
1145 PHP_FUNCTION(http_head
)
1147 zval
*options
= NULL
, *info
= NULL
;
1150 http_request request
;
1152 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1163 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
1164 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1165 http_request_exec(&request
);
1167 http_request_info(&request
, Z_ARRVAL_P(info
));
1169 RETVAL_RESPONSE_OR_BODY(request
);
1171 http_request_dtor(&request
);
1175 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1177 * Performs an HTTP POST requeston the supplied url.
1179 * Expects a string as second parameter containing the pre-encoded post data.
1180 * See http_get() for a full list of available parameters and options.
1182 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1184 PHP_FUNCTION(http_post_data
)
1186 zval
*options
= NULL
, *info
= NULL
;
1187 char *URL
, *postdata
;
1188 int postdata_len
, URL_len
;
1189 http_request_body body
;
1190 http_request request
;
1192 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1203 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1204 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
1205 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1206 http_request_exec(&request
);
1208 http_request_info(&request
, Z_ARRVAL_P(info
));
1210 RETVAL_RESPONSE_OR_BODY(request
);
1212 http_request_dtor(&request
);
1216 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1218 * Performs an HTTP POST request on the supplied url.
1220 * Expecrs an associative array as second parameter, which will be
1221 * www-form-urlencoded. See http_get() for a full list of available options.
1223 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1225 PHP_FUNCTION(http_post_fields
)
1227 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
1230 http_request_body body
;
1231 http_request request
;
1233 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
1237 if (!http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
1248 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1249 request
.body
= &body
;
1250 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1251 http_request_exec(&request
);
1253 http_request_info(&request
, Z_ARRVAL_P(info
));
1255 RETVAL_RESPONSE_OR_BODY(request
);
1257 http_request_dtor(&request
);
1261 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1263 * Performs an HTTP PUT request on the supplied url.
1265 * Expects the second parameter to be a string referncing the file to upload.
1266 * See http_get() for a full list of available options.
1268 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1270 PHP_FUNCTION(http_put_file
)
1274 zval
*options
= NULL
, *info
= NULL
;
1276 php_stream_statbuf ssb
;
1277 http_request_body body
;
1278 http_request request
;
1280 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1284 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1287 if (php_stream_stat(stream
, &ssb
)) {
1288 php_stream_close(stream
);
1299 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1301 body
.size
= ssb
.sb
.st_size
;
1303 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1304 request
.body
= &body
;
1305 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1306 http_request_exec(&request
);
1308 http_request_info(&request
, Z_ARRVAL_P(info
));
1310 RETVAL_RESPONSE_OR_BODY(request
);
1312 http_request_body_dtor(&body
);
1313 request
.body
= NULL
;
1314 http_request_dtor(&request
);
1318 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1320 * Performs an HTTP PUT request on the supplied url.
1322 * Expects the second parameter to be a resource referencing an already
1323 * opened stream, from which the data to upload should be read.
1324 * See http_get() for a full list of available options.
1326 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1328 PHP_FUNCTION(http_put_stream
)
1330 zval
*resource
, *options
= NULL
, *info
= NULL
;
1334 php_stream_statbuf ssb
;
1335 http_request_body body
;
1336 http_request request
;
1338 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1342 php_stream_from_zval(stream
, &resource
);
1343 if (php_stream_stat(stream
, &ssb
)) {
1354 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1356 body
.size
= ssb
.sb
.st_size
;
1358 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1359 request
.body
= &body
;
1360 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1361 http_request_exec(&request
);
1363 http_request_info(&request
, Z_ARRVAL_P(info
));
1365 RETVAL_RESPONSE_OR_BODY(request
);
1367 request
.body
= NULL
;
1368 http_request_dtor(&request
);
1371 #endif /* HTTP_HAVE_CURL */
1374 /* {{{ proto int http_request_method_register(string method)
1376 * Register a custom request method.
1378 * Expects a string parameter containing the request method name to register.
1380 * Returns the ID of the request method on success, or FALSE on failure.
1382 PHP_FUNCTION(http_request_method_register
)
1388 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1391 if ((existing
= http_request_method_exists(1, 0, method
))) {
1392 RETURN_LONG((long) existing
);
1395 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1399 /* {{{ proto bool http_request_method_unregister(mixed method)
1401 * Unregister a previously registered custom request method.
1403 * Expects either the request method name or ID.
1405 * Returns TRUE on success, or FALSE on failure.
1407 PHP_FUNCTION(http_request_method_unregister
)
1411 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1415 switch (Z_TYPE_P(method
))
1418 convert_to_string(method
);
1420 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1421 convert_to_long(method
);
1424 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1428 ZVAL_LONG(method
, (long)mn
);
1431 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1438 /* {{{ proto int http_request_method_exists(mixed method)
1440 * Check if a request method is registered (or available by default).
1442 * Expects either the request method name or ID as parameter.
1444 * Returns TRUE if the request method is known, else FALSE.
1446 PHP_FUNCTION(http_request_method_exists
)
1451 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1455 switch (Z_TYPE_P(method
))
1458 convert_to_string(method
);
1460 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1461 convert_to_long(method
);
1463 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1466 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1474 /* {{{ proto string http_request_method_name(int method)
1476 * Get the literal string representation of a standard or registered request method.
1478 * Expects the request method ID as parameter.
1480 * Returns the request method name as string on success, or FALSE on failure.
1482 PHP_FUNCTION(http_request_method_name
)
1487 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1491 RETURN_STRING(estrdup(http_request_method_name((ulong
) method
)), 0);
1496 /* {{{ Sara Golemons http_build_query() */
1497 #ifndef ZEND_ENGINE_2
1499 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1500 Generates a form-encoded query string from an associative array or object. */
1501 PHP_FUNCTION(http_build_query
)
1504 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1505 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1508 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1513 arg_sep
= HTTP_URL_ARGSEP
;
1514 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
1517 formstr
= phpstr_new();
1518 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
1519 phpstr_free(&formstr
);
1523 if (!formstr
->used
) {
1524 phpstr_free(&formstr
);
1528 RETURN_PHPSTR_PTR(formstr
);
1531 #endif /* !ZEND_ENGINE_2 */
1535 #ifdef HTTP_HAVE_ZLIB
1537 /* {{{ proto string http_gzencode(string data[, int level = -1[, int mtime = 0]])
1539 * Compress data with the HTTP compatible GZIP encoding.
1541 * Expects the first parameter to be a string which contains the data that
1542 * should be encoded. Additionally accepts an optional in paramter specifying
1543 * the compression level, where -1 is default, 0 is no compression and 9 is
1544 * best compression ratio.
1546 * Returns the encoded string on success, or NULL on failure.
1548 PHP_FUNCTION(http_gzencode
)
1552 long level
= -1, mtime
= 0;
1556 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ll", &data
, &data_len
, &level
, &mtime
)) {
1557 HTTP_CHECK_GZIP_LEVEL(level
, return);
1562 if (SUCCESS
== http_encoding_gzencode(level
, mtime
, data
, data_len
, &encoded
, &encoded_len
)) {
1563 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1570 /* {{{ proto string http_gzdecode(string data)
1572 * Uncompress data compressed with the HTTP compatible GZIP encoding.
1574 * Expects a string as parameter containing the compressed data.
1576 * Returns the decoded string on success, or NULL on failure.
1578 PHP_FUNCTION(http_gzdecode
)
1585 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1589 if (SUCCESS
== http_encoding_gzdecode(data
, data_len
, &decoded
, &decoded_len
)) {
1590 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1596 /* {{{ proto string http_deflate(string data[, int level = -1[, bool zlib_header = false]])
1598 * Compress data with the HTTP compatible DEFLATE encoding.
1600 * Expects the first parameter to be a string containing the data that should
1601 * be encoded. Additionally accepts an optional int parameter specifying the
1602 * compression level, where -1 is default, 0 is no compression and 9 is best
1603 * compression ratio.
1605 * Returns the encoded string on success, or NULL on failure.
1607 PHP_FUNCTION(http_deflate
)
1616 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|lb", &data
, &data_len
, &level
, &zhdr
)) {
1617 HTTP_CHECK_GZIP_LEVEL(level
, return);
1622 if (SUCCESS
== http_encoding_deflate(level
, zhdr
, data
, data_len
, &encoded
, &encoded_len
)) {
1623 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1630 /* {{{ proto string http_inflate(string data)
1632 * Uncompress data compressed with the HTTP compatible DEFLATE encoding.
1634 * Expects a string as parameter containing the compressed data.
1636 * Returns the decoded string on success, or NULL on failure.
1638 PHP_FUNCTION(http_inflate
)
1645 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1649 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1650 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1656 #endif /* HTTP_HAVE_ZLIB */
1659 /* {{{ proto int http_support([int feature = 0])
1661 * Check for feature that require external libraries.
1663 * Accpepts an optional in parameter specifying which feature to probe for.
1664 * If the parameter is 0 or omitted, the return value contains a bitmask of
1665 * all supported features that depend on external libraries.
1667 * Available features to probe for are:
1669 * <li> HTTP_SUPPORT: always set
1670 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1671 * and HTTP requests can be issued
1672 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1673 * and SSL requests can be issued
1674 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1675 * and compressed HTTP responses can be decoded
1676 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1677 * and the HttpResponse::guessContentType() method is usable
1680 * Returns int, whether requested feature is supported, or a bitmask with
1681 * all supported features.
1683 PHP_FUNCTION(http_support
)
1689 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
1690 RETVAL_LONG(http_support(feature
));
1695 PHP_FUNCTION(http_test
)
1704 * vim600: noet sw=4 ts=4 fdm=marker
1705 * vim<600: noet sw=4 ts=4