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
20 #define HTTP_WANT_ZLIB
25 #include "ext/standard/info.h"
26 #include "ext/standard/php_string.h"
27 #include "zend_operators.h"
29 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
30 # include "ext/session/php_session.h"
33 #include "php_http_api.h"
34 #include "php_http_cache_api.h"
35 #include "php_http_date_api.h"
36 #include "php_http_encoding_api.h"
37 #include "php_http_headers_api.h"
38 #include "php_http_message_api.h"
39 #include "php_http_request_api.h"
40 #include "php_http_request_method_api.h"
41 #include "php_http_send_api.h"
42 #include "php_http_url_api.h"
44 ZEND_EXTERN_MODULE_GLOBALS(http
)
46 /* {{{ proto string http_date([int timestamp])
48 * Compose a valid HTTP date regarding RFC 822/1123
49 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
51 * Takes an optional unix timestamp as parameter.
53 * Returns the HTTP date as string.
55 PHP_FUNCTION(http_date
)
59 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
64 t
= (long) time(NULL
);
67 RETURN_STRING(http_date(t
), 0);
71 /* {{{ proto string http_build_uri(string url[, string proto[, string host[, int port]]])
73 * Build a complete URI according to the supplied parameters.
75 * If the url is already abolute but a different proto was supplied,
76 * only the proto part of the URI will be updated. If url has no
77 * path specified, the path of the current REQUEST_URI will be taken.
78 * The host will be taken either from the Host HTTP header of the client
79 * the SERVER_NAME or just localhost if prior are not available.
80 * If a port is pecified in either the url or as sperate parameter,
81 * it will be added if it differs from te default port for HTTP(S).
83 * Returns the absolute URI as string on success or false on failure.
88 * $uri = http_build_uri("page.php", "https", NULL, 488);
92 PHP_FUNCTION(http_build_uri
)
94 char *url
= NULL
, *proto
= NULL
, *host
= NULL
, *built
= NULL
;
95 int url_len
= 0, proto_len
= 0, host_len
= 0;
98 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ssl", &url
, &url_len
, &proto
, &proto_len
, &host
, &host_len
, &port
) != SUCCESS
) {
102 if ((built
= http_absolute_uri_ex(url
, url_len
, proto
, proto_len
, host
, host_len
, port
))) {
103 RETURN_STRING(built
, 0);
109 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
112 if ((result = http_negotiate_ ##type(supported))) { \
117 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
118 RETVAL_STRINGL(key, key_len-1, 0); \
124 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
127 zend_hash_destroy(result); \
128 FREE_HASHTABLE(result); \
133 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
134 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
135 RETVAL_ZVAL(*value, 1, 0); \
144 FOREACH_VAL(pos, 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 string http_negotiate_ctype(array supported[, array &result])
257 * This function negotiates the clients preferred content type based on its
258 * Accept HTTP header. The qualifier is recognized and content types
259 * without qualifier are rated highest.
261 * Expects an array as parameter cotaining the supported content types as values.
262 * If the optional second parameter is supplied, it will be filled with an
263 * array containing the negotiation results.
265 * Returns the negotiated content type or the default content type
266 * (i.e. first array entry) if none match.
271 * $ctypes = array('application/xhtml+xml', 'text/html');
272 * http_send_content_type(http_negotiate_content_type($ctypes));
276 PHP_FUNCTION(http_negotiate_content_type
)
278 zval
*supported
, *rs_array
= NULL
;
280 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
286 array_init(rs_array
);
289 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
293 /* {{{ proto bool http_send_status(int status)
295 * Send HTTP status code.
297 * Expects an HTTP status code as parameter.
299 * Returns TRUE on success or FALSE on failure.
301 PHP_FUNCTION(http_send_status
)
305 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
308 if (status
< 100 || status
> 510) {
309 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
313 RETURN_SUCCESS(http_send_status(status
));
317 /* {{{ proto bool http_send_last_modified([int timestamp])
319 * Send a "Last-Modified" header with a valid HTTP date.
321 * Accepts a unix timestamp, converts it to a valid HTTP date and
322 * sends it as "Last-Modified" HTTP header. If timestamp is
323 * omitted, the current time will be sent.
325 * Returns TRUE on success or FALSE on failure.
327 PHP_FUNCTION(http_send_last_modified
)
331 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
336 t
= (long) time(NULL
);
339 RETURN_SUCCESS(http_send_last_modified(t
));
343 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
345 * Send the Content-Type of the sent entity. This is particularly important
346 * if you use the http_send() API.
348 * Accepts an optional string parameter containing the desired content type
349 * (primary/secondary).
351 * Returns TRUE on success or FALSE on failure.
353 PHP_FUNCTION(http_send_content_type
)
355 char *ct
= "application/x-octetstream";
356 int ct_len
= lenof("application/x-octetstream");
358 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
362 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
366 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
368 * Send the Content-Disposition. The Content-Disposition header is very useful
369 * if the data actually sent came from a file or something similar, that should
370 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
372 * Expects a string parameter specifying the file name the "Save as..." dialogue
373 * should display. Optionally accepts a bool parameter, which, if set to true
374 * and the user agent knows how to handle the content type, will probably not
375 * cause the popup window to be shown.
377 * Returns TRUE on success or FALSE on failure.
379 PHP_FUNCTION(http_send_content_disposition
)
383 zend_bool send_inline
= 0;
385 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
388 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
392 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
394 * Matches the given unix timestamp against the clients "If-Modified-Since"
395 * resp. "If-Unmodified-Since" HTTP headers.
397 * Accepts a unix timestamp which should be matched. Optionally accepts an
398 * additional bool parameter, which if set to true will check the header
399 * usually used to validate HTTP ranges. If timestamp is omitted, the
400 * current time will be used.
402 * Returns TRUE if timestamp represents an earlier date than the header,
405 PHP_FUNCTION(http_match_modified
)
408 zend_bool for_range
= 0;
410 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
414 // current time if not supplied (senseless though)
416 t
= (long) time(NULL
);
420 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
422 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
426 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
428 * Matches the given ETag against the clients "If-Match" resp.
429 * "If-None-Match" HTTP headers.
431 * Expects a string parameter containing the ETag to compare. Optionally
432 * accepts a bool parameter, which, if set to true, will check the header
433 * usually used to validate HTTP ranges.
435 * Retuns TRUE if ETag matches or the header contained the asterisk ("*"),
438 PHP_FUNCTION(http_match_etag
)
442 zend_bool for_range
= 0;
444 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
449 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
451 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
455 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
457 * Attempts to cache the sent entity by its last modification date.
459 * Accepts a unix timestamp as parameter which is handled as follows:
461 * If timestamp_or_expires is greater than 0, it is handled as timestamp
462 * and will be sent as date of last modification. If it is 0 or omitted,
463 * the current time will be sent as Last-Modified date. If it's negative,
464 * it is handled as expiration time in seconds, which means that if the
465 * requested last modification date is not between the calculated timespan,
466 * the Last-Modified header is updated and the actual body will be sent.
468 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
470 * A log entry will be written to the cache log if the INI entry
471 * http.cache_log is set and the cache attempt was successful.
473 PHP_FUNCTION(http_cache_last_modified
)
475 long last_modified
= 0, send_modified
= 0, t
;
478 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
482 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
484 t
= (long) time(NULL
);
487 if (!last_modified
) {
488 /* does the client have? (att: caching "forever") */
489 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
490 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
491 /* send current time */
495 /* negative value is supposed to be expiration time */
496 } else if (last_modified
< 0) {
499 /* send supplied time explicitly */
501 send_modified
= last_modified
;
504 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
508 /* {{{ proto bool http_cache_etag([string etag])
510 * Attempts to cache the sent entity by its ETag, either supplied or generated
511 * by the hash algorythm specified by the INI setting "http.etag_mode".
513 * If the clients "If-None-Match" header matches the supplied/calculated
514 * ETag, the body is considered cached on the clients side and
515 * a "304 Not Modified" status code is issued.
517 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
519 * A log entry is written to the cache log if the INI entry
520 * "http.cache_log" is set and the cache attempt was successful.
522 PHP_FUNCTION(http_cache_etag
)
527 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
531 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
533 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
537 /* {{{ proto string ob_etaghandler(string data, int mode)
539 * For use with ob_start(). Output buffer handler generating an ETag with
540 * the hash algorythm specified with the INI setting "http.etag_mode".
542 PHP_FUNCTION(ob_etaghandler
)
548 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
552 Z_TYPE_P(return_value
) = IS_STRING
;
553 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
557 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
559 * Sets the throttle delay and send buffer size for use with http_send() API.
560 * Provides a basic throttling mechanism, which will yield the current process
561 * resp. thread until the entity has been completely sent, though.
563 * Note: This doesn't really work with the FastCGI SAPI.
565 * Expects a double parameter specifying the seconds too sleep() after
566 * each chunk sent. Additionally accepts an optional int parameter
567 * representing the chunk size in bytes.
573 * # http_throttle(1, 20000);
574 * # http_throttle(0.5, 10000);
575 * # http_throttle(0.1, 2000);
576 * http_send_file('document.pdf');
580 PHP_FUNCTION(http_throttle
)
582 long chunk_size
= HTTP_SENDBUF_SIZE
;
585 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
589 HTTP_G(send
).throttle_delay
= interval
;
590 HTTP_G(send
).buffer_size
= chunk_size
;
594 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
596 * Redirect to the given url.
598 * The supplied url will be expanded with http_build_uri(), the params array will
599 * be treated with http_build_query() and the session identification will be appended
600 * if session is true.
602 * The HTTP response code will be set according to status.
603 * You can use one of the following constants for convenience:
604 * - HTTP_REDIRECT 302 Found
605 * - HTTP_REDIRECT_PERM 301 Moved Permanently
606 * - HTTP_REDIRECT_POST 303 See Other
607 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
609 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
610 * for which redirect response code to use in which situation.
612 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
613 * if the client doesn't redirect immediatly, and the request method was
614 * another one than HEAD.
616 * Returns FALSE on failure, or *exits* on success.
618 * A log entry will be written to the redirect log, if the INI entry
619 * "http.redirect_log" is set and the redirect attempt was successful.
621 PHP_FUNCTION(http_redirect
)
624 size_t query_len
= 0;
625 zend_bool session
= 0, free_params
= 0;
628 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
630 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
634 /* append session info */
638 MAKE_STD_ZVAL(params
);
641 #ifdef HAVE_PHP_SESSION
642 # ifdef COMPILE_DL_SESSION
643 if (SUCCESS
== zend_get_module_started("session")) {
644 zval nm_retval
, id_retval
, func
;
647 INIT_PZVAL(&nm_retval
);
648 INIT_PZVAL(&id_retval
);
649 ZVAL_NULL(&nm_retval
);
650 ZVAL_NULL(&id_retval
);
652 ZVAL_STRINGL(&func
, "session_id", lenof("session_id"), 0);
653 call_user_function(EG(function_table
), NULL
, &func
, &id_retval
, 0, NULL TSRMLS_CC
);
654 ZVAL_STRINGL(&func
, "session_name", lenof("session_name"), 0);
655 call_user_function(EG(function_table
), NULL
, &func
, &nm_retval
, 0, NULL TSRMLS_CC
);
657 if ( Z_TYPE(nm_retval
) == IS_STRING
&& Z_STRLEN(nm_retval
) &&
658 Z_TYPE(id_retval
) == IS_STRING
&& Z_STRLEN(id_retval
)) {
659 if (add_assoc_stringl_ex(params
, Z_STRVAL(nm_retval
), Z_STRLEN(nm_retval
)+1,
660 Z_STRVAL(id_retval
), Z_STRLEN(id_retval
), 0) != SUCCESS
) {
661 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
666 if (PS(session_status
) == php_session_active
) {
667 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
668 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
675 /* treat params array with http_build_query() */
677 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
689 URI
= http_absolute_uri(url
);
692 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
693 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
694 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
697 spprintf(&LOC
, 0, "Location: %s", URI
);
698 if (SG(request_info
).request_method
&& strcmp(SG(request_info
).request_method
, "HEAD")) {
699 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
712 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
716 /* {{{ proto bool http_send_data(string data)
718 * Sends raw data with support for (multiple) range requests.
720 * Retursn TRUE on success, or FALSE on failure.
722 PHP_FUNCTION(http_send_data
)
726 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
730 convert_to_string_ex(&zdata
);
731 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata
), Z_STRLEN_P(zdata
)));
735 /* {{{ proto bool http_send_file(string file)
737 * Sends a file with support for (multiple) range requests.
739 * Expects a string parameter referencing the file to send.
741 * Returns TRUE on success, or FALSE on failure.
743 PHP_FUNCTION(http_send_file
)
748 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
755 RETURN_SUCCESS(http_send_file(file
));
759 /* {{{ proto bool http_send_stream(resource stream)
761 * Sends an already opened stream with support for (multiple) range requests.
763 * Expects a resource parameter referncing the stream to read from.
765 * Returns TRUE on success, or FALSE on failure.
767 PHP_FUNCTION(http_send_stream
)
772 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
776 php_stream_from_zval(file
, &zstream
);
777 RETURN_SUCCESS(http_send_stream(file
));
781 /* {{{ proto string http_chunked_decode(string encoded)
783 * Decodes a string that was HTTP-chunked encoded.
785 * Expects a chunked encoded string as parameter.
787 * Returns the decoded string on success or FALSE on failure.
789 PHP_FUNCTION(http_chunked_decode
)
791 char *encoded
= NULL
, *decoded
= NULL
;
792 size_t decoded_len
= 0;
795 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
799 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
800 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
807 /* {{{ proto object http_parse_message(string message)
809 * Parses (a) http_message(s) into a simple recursive object structure.
811 * Expects a string parameter containing a single HTTP message or
812 * several consecutive HTTP messages.
814 * Returns an hierachical object structure of the parsed messages.
819 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
824 * [httpVersion] => 1.1
825 * [responseCode] => 200
828 * [Content-Length] => 3
832 * [parentMessage] => stdClass object
835 * [httpVersion] => 1.1
836 * [responseCode] => 302
839 * [Content-Length] => 0
843 * [parentMessage] => ...
849 PHP_FUNCTION(http_parse_message
)
853 http_message
*msg
= NULL
;
855 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
859 if ((msg
= http_message_parse(message
, message_len
))) {
860 object_init(return_value
);
861 http_message_tostruct_recursive(msg
, return_value
);
862 http_message_free(&msg
);
869 /* {{{ proto array http_parse_headers(string header)
871 * Parses HTTP headers into an associative array.
873 * Expects a string parameter containing HTTP headers.
875 * Returns an array on success, or FALSE on failure.
880 * $headers = "content-type: text/html; charset=UTF-8\r\n".
881 * "Server: Funky/1.0\r\n".
882 * "Set-Cookie: foo=bar\r\n".
883 * "Set-Cookie: baz=quux\r\n".
884 * "Folded: works\r\n\ttoo\r\n";
885 * print_r(http_parse_headers($headers));
889 * [Content-Type] => text/html; chatset=UTF-8
890 * [Server] => Funky/1.0
891 * [Set-Cookie] => Array
902 PHP_FUNCTION(http_parse_headers
)
907 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
911 array_init(return_value
);
912 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
913 zval_dtor(return_value
);
919 /* {{{ proto object http_parse_cookie(string cookie)
921 * Parses HTTP cookies like sent in a response into a struct.
923 * Expects a string as parameter containing the value of a Set-Cookie response header.
925 * Returns an stdClass object with the cookie params as properties on success or FALSE on failure.
930 * print_r(http_parse_cookie("foo=bar; path=/"));
941 PHP_FUNCTION(http_parse_cookie
)
946 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &cookie
, &cookie_len
)) {
950 object_init(return_value
);
951 if (SUCCESS
!= http_parse_cookie(cookie
, HASH_OF(return_value
))) {
952 zval_dtor(return_value
);
957 /* {{{ proto array http_get_request_headers(void)
959 * Get a list of incoming HTTP headers.
961 * Returns an associative array of incoming request headers.
963 PHP_FUNCTION(http_get_request_headers
)
967 array_init(return_value
);
968 http_get_request_headers(return_value
);
972 /* {{{ proto string http_get_request_body(void)
974 * Get the raw request body (e.g. POST or PUT data).
976 * Returns NULL when using the CLI SAPI.
978 PHP_FUNCTION(http_get_request_body
)
985 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
986 RETURN_STRINGL(body
, (int) length
, 0);
993 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
995 * Match an incoming HTTP header.
997 * Expects two string parameters representing the header name (case-insensitive)
998 * and the header value that should be compared. The case sensitivity of the
999 * header value depends on the additional optional bool parameter accepted.
1001 * Returns TRUE if header value matches, else FALSE.
1003 PHP_FUNCTION(http_match_request_header
)
1005 char *header
, *value
;
1006 int header_len
, value_len
;
1007 zend_bool match_case
= 0;
1009 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
1013 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
1018 #ifdef HTTP_HAVE_CURL
1020 #define RETVAL_RESPONSE_OR_BODY(request) \
1024 /* check if only the body should be returned */ \
1025 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void **) &bodyonly)) && zval_is_true(*bodyonly)) { \
1026 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1029 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1030 http_message_free(&msg); \
1033 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1037 /* {{{ proto string http_get(string url[, array options[, array &info]])
1039 * Performs an HTTP GET request on the supplied url.
1041 * The second parameter, if set, is expected to be an associative
1042 * array where the following keys will be recognized:
1044 * - redirect: int, whether and how many redirects to follow
1045 * - unrestrictedauth: bool, whether to continue sending credentials on
1046 * redirects to a different host
1047 * - proxyhost: string, proxy host in "host[:port]" format
1048 * - proxyport: int, use another proxy port as specified in proxyhost
1049 * - proxyauth: string, proxy credentials in "user:pass" format
1050 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1051 * - httpauth: string, http credentials in "user:pass" format
1052 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1053 * - compress: bool, whether to allow gzip/deflate content encoding
1054 * (defaults to true)
1055 * - port: int, use another port as specified in the url
1056 * - referer: string, the referer to send
1057 * - useragent: string, the user agent to send
1058 * (defaults to PECL::HTTP/version (PHP/version)))
1059 * - headers: array, list of custom headers as associative array
1060 * like array("header" => "value")
1061 * - cookies: array, list of cookies as associative array
1062 * like array("cookie" => "value")
1063 * - cookiestore: string, path to a file where cookies are/will be stored
1064 * - resume: int, byte offset to start the download from;
1065 * if the server supports ranges
1066 * - maxfilesize: int, maximum file size that should be downloaded;
1067 * has no effect, if the size of the requested entity is not known
1068 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1069 * - timeout: int, seconds the request may take
1070 * - connecttimeout: int, seconds the connect may take
1071 * - onprogress: mixed, progress callback
1074 * The optional third parameter will be filled with some additional information
1075 * in form af an associative array, if supplied, like the following example:
1079 * 'effective_url' => 'http://localhost',
1080 * 'response_code' => 403,
1081 * 'total_time' => 0.017,
1082 * 'namelookup_time' => 0.013,
1083 * 'connect_time' => 0.014,
1084 * 'pretransfer_time' => 0.014,
1085 * 'size_upload' => 0,
1086 * 'size_download' => 202,
1087 * 'speed_download' => 11882,
1088 * 'speed_upload' => 0,
1089 * 'header_size' => 145,
1090 * 'request_size' => 62,
1091 * 'ssl_verifyresult' => 0,
1093 * 'content_length_download' => 202,
1094 * 'content_length_upload' => 0,
1095 * 'starttransfer_time' => 0.017,
1096 * 'content_type' => 'text/html; charset=iso-8859-1',
1097 * 'redirect_time' => 0,
1098 * 'redirect_count' => 0,
1099 * 'http_connectcode' => 0,
1100 * 'httpauth_avail' => 0,
1101 * 'proxyauth_avail' => 0,
1106 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1108 PHP_FUNCTION(http_get
)
1110 zval
*options
= NULL
, *info
= NULL
;
1113 http_request request
;
1115 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1126 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
1127 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1128 http_request_exec(&request
);
1130 http_request_info(&request
, Z_ARRVAL_P(info
));
1132 RETVAL_RESPONSE_OR_BODY(request
);
1134 http_request_dtor(&request
);
1138 /* {{{ proto string http_head(string url[, array options[, array &info]])
1140 * Performs an HTTP HEAD request on the supplied url.
1142 * See http_get() for a full list of available parameters and options.
1144 * Returns the HTTP response as string on success, or FALSE on failure.
1146 PHP_FUNCTION(http_head
)
1148 zval
*options
= NULL
, *info
= NULL
;
1151 http_request request
;
1153 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
1164 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
1165 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1166 http_request_exec(&request
);
1168 http_request_info(&request
, Z_ARRVAL_P(info
));
1170 RETVAL_RESPONSE_OR_BODY(request
);
1172 http_request_dtor(&request
);
1176 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1178 * Performs an HTTP POST requeston the supplied url.
1180 * Expects a string as second parameter containing the pre-encoded post data.
1181 * See http_get() for a full list of available parameters and options.
1183 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1185 PHP_FUNCTION(http_post_data
)
1187 zval
*options
= NULL
, *info
= NULL
;
1188 char *URL
, *postdata
;
1189 int postdata_len
, URL_len
;
1190 http_request_body body
;
1191 http_request request
;
1193 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
1204 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1205 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
1206 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1207 http_request_exec(&request
);
1209 http_request_info(&request
, Z_ARRVAL_P(info
));
1211 RETVAL_RESPONSE_OR_BODY(request
);
1213 http_request_dtor(&request
);
1217 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1219 * Performs an HTTP POST request on the supplied url.
1221 * Expecrs an associative array as second parameter, which will be
1222 * www-form-urlencoded. See http_get() for a full list of available options.
1224 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1226 PHP_FUNCTION(http_post_fields
)
1228 zval
*options
= NULL
, *info
= NULL
, *fields
, *files
= NULL
;
1231 http_request_body body
;
1232 http_request request
;
1234 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|aa/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
1238 if (!http_request_body_fill(&body
, Z_ARRVAL_P(fields
), files
? Z_ARRVAL_P(files
) : NULL
)) {
1249 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1250 request
.body
= &body
;
1251 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1252 http_request_exec(&request
);
1254 http_request_info(&request
, Z_ARRVAL_P(info
));
1256 RETVAL_RESPONSE_OR_BODY(request
);
1258 http_request_dtor(&request
);
1262 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1264 * Performs an HTTP PUT request on the supplied url.
1266 * Expects the second parameter to be a string referncing the file to upload.
1267 * See http_get() for a full list of available options.
1269 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1271 PHP_FUNCTION(http_put_file
)
1275 zval
*options
= NULL
, *info
= NULL
;
1277 php_stream_statbuf ssb
;
1278 http_request_body body
;
1279 http_request request
;
1281 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1285 if (!(stream
= php_stream_open_wrapper(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1288 if (php_stream_stat(stream
, &ssb
)) {
1289 php_stream_close(stream
);
1300 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1302 body
.size
= ssb
.sb
.st_size
;
1304 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1305 request
.body
= &body
;
1306 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1307 http_request_exec(&request
);
1309 http_request_info(&request
, Z_ARRVAL_P(info
));
1311 RETVAL_RESPONSE_OR_BODY(request
);
1313 http_request_body_dtor(&body
);
1314 request
.body
= NULL
;
1315 http_request_dtor(&request
);
1319 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1321 * Performs an HTTP PUT request on the supplied url.
1323 * Expects the second parameter to be a resource referencing an already
1324 * opened stream, from which the data to upload should be read.
1325 * See http_get() for a full list of available options.
1327 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1329 PHP_FUNCTION(http_put_stream
)
1331 zval
*resource
, *options
= NULL
, *info
= NULL
;
1335 php_stream_statbuf ssb
;
1336 http_request_body body
;
1337 http_request request
;
1339 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1343 php_stream_from_zval(stream
, &resource
);
1344 if (php_stream_stat(stream
, &ssb
)) {
1355 body
.type
= HTTP_REQUEST_BODY_UPLOADFILE
;
1357 body
.size
= ssb
.sb
.st_size
;
1359 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
1360 request
.body
= &body
;
1361 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1362 http_request_exec(&request
);
1364 http_request_info(&request
, Z_ARRVAL_P(info
));
1366 RETVAL_RESPONSE_OR_BODY(request
);
1368 request
.body
= NULL
;
1369 http_request_dtor(&request
);
1372 #endif /* HTTP_HAVE_CURL */
1375 /* {{{ proto int http_request_method_register(string method)
1377 * Register a custom request method.
1379 * Expects a string parameter containing the request method name to register.
1381 * Returns the ID of the request method on success, or FALSE on failure.
1383 PHP_FUNCTION(http_request_method_register
)
1389 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1392 if ((existing
= http_request_method_exists(1, 0, method
))) {
1393 RETURN_LONG((long) existing
);
1396 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1400 /* {{{ proto bool http_request_method_unregister(mixed method)
1402 * Unregister a previously registered custom request method.
1404 * Expects either the request method name or ID.
1406 * Returns TRUE on success, or FALSE on failure.
1408 PHP_FUNCTION(http_request_method_unregister
)
1412 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1416 switch (Z_TYPE_P(method
))
1419 convert_to_string(method
);
1421 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1422 convert_to_long(method
);
1425 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1429 ZVAL_LONG(method
, (long)mn
);
1432 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1439 /* {{{ proto int http_request_method_exists(mixed method)
1441 * Check if a request method is registered (or available by default).
1443 * Expects either the request method name or ID as parameter.
1445 * Returns TRUE if the request method is known, else FALSE.
1447 PHP_FUNCTION(http_request_method_exists
)
1452 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1456 switch (Z_TYPE_P(method
))
1459 convert_to_string(method
);
1461 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1462 convert_to_long(method
);
1464 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1467 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method
), NULL
));
1475 /* {{{ proto string http_request_method_name(int method)
1477 * Get the literal string representation of a standard or registered request method.
1479 * Expects the request method ID as parameter.
1481 * Returns the request method name as string on success, or FALSE on failure.
1483 PHP_FUNCTION(http_request_method_name
)
1488 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1492 RETURN_STRING(estrdup(http_request_method_name((ulong
) method
)), 0);
1497 /* {{{ Sara Golemons http_build_query() */
1498 #ifndef ZEND_ENGINE_2
1500 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1501 Generates a form-encoded query string from an associative array or object. */
1502 PHP_FUNCTION(http_build_query
)
1505 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
1506 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
1509 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
1514 arg_sep
= HTTP_URL_ARGSEP
;
1515 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
1518 formstr
= phpstr_new();
1519 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
1520 phpstr_free(&formstr
);
1524 if (!formstr
->used
) {
1525 phpstr_free(&formstr
);
1529 RETURN_PHPSTR_PTR(formstr
);
1532 #endif /* !ZEND_ENGINE_2 */
1536 #ifdef HTTP_HAVE_ZLIB
1538 /* {{{ proto string http_gzencode(string data[, int level = -1[, int mtime = 0]])
1540 * Compress data with the HTTP compatible GZIP encoding.
1542 * Expects the first parameter to be a string which contains the data that
1543 * should be encoded. Additionally accepts an optional in paramter specifying
1544 * the compression level, where -1 is default, 0 is no compression and 9 is
1545 * best compression ratio.
1547 * Returns the encoded string on success, or NULL on failure.
1549 PHP_FUNCTION(http_gzencode
)
1553 long level
= -1, mtime
= 0;
1557 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|ll", &data
, &data_len
, &level
, &mtime
)) {
1558 HTTP_CHECK_GZIP_LEVEL(level
, return);
1563 if (SUCCESS
== http_encoding_gzencode(level
, mtime
, data
, data_len
, &encoded
, &encoded_len
)) {
1564 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1571 /* {{{ proto string http_gzdecode(string data)
1573 * Uncompress data compressed with the HTTP compatible GZIP encoding.
1575 * Expects a string as parameter containing the compressed data.
1577 * Returns the decoded string on success, or NULL on failure.
1579 PHP_FUNCTION(http_gzdecode
)
1586 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1590 if (SUCCESS
== http_encoding_gzdecode(data
, data_len
, &decoded
, &decoded_len
)) {
1591 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1597 /* {{{ proto string http_deflate(string data[, int level = -1[, bool zlib_header = false]])
1599 * Compress data with the HTTP compatible DEFLATE encoding.
1601 * Expects the first parameter to be a string containing the data that should
1602 * be encoded. Additionally accepts an optional int parameter specifying the
1603 * compression level, where -1 is default, 0 is no compression and 9 is best
1604 * compression ratio.
1606 * Returns the encoded string on success, or NULL on failure.
1608 PHP_FUNCTION(http_deflate
)
1617 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|lb", &data
, &data_len
, &level
, &zhdr
)) {
1618 HTTP_CHECK_GZIP_LEVEL(level
, return);
1623 if (SUCCESS
== http_encoding_deflate(level
, zhdr
, data
, data_len
, &encoded
, &encoded_len
)) {
1624 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1631 /* {{{ proto string http_inflate(string data)
1633 * Uncompress data compressed with the HTTP compatible DEFLATE encoding.
1635 * Expects a string as parameter containing the compressed data.
1637 * Returns the decoded string on success, or NULL on failure.
1639 PHP_FUNCTION(http_inflate
)
1646 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1650 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1651 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1657 #endif /* HTTP_HAVE_ZLIB */
1660 /* {{{ proto int http_support([int feature = 0])
1662 * Check for feature that require external libraries.
1664 * Accpepts an optional in parameter specifying which feature to probe for.
1665 * If the parameter is 0 or omitted, the return value contains a bitmask of
1666 * all supported features that depend on external libraries.
1668 * Available features to probe for are:
1670 * <li> HTTP_SUPPORT: always set
1671 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1672 * and HTTP requests can be issued
1673 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1674 * and SSL requests can be issued
1675 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1676 * and compressed HTTP responses can be decoded
1677 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1678 * and the HttpResponse::guessContentType() method is usable
1681 * Returns int, whether requested feature is supported, or a bitmask with
1682 * all supported features.
1684 PHP_FUNCTION(http_support
)
1690 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
1691 RETVAL_LONG(http_support(feature
));
1696 PHP_FUNCTION(http_test
)
1705 * vim600: noet sw=4 ts=4 fdm=marker
1706 * vim<600: noet sw=4 ts=4