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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
21 #include "ext/standard/php_string.h"
22 #include "zend_operators.h"
24 #ifdef HTTP_HAVE_SESSION
25 # include "ext/session/php_session.h"
28 #include "php_http_api.h"
29 #include "php_http_cache_api.h"
30 #include "php_http_cookie_api.h"
31 #include "php_http_date_api.h"
32 #include "php_http_encoding_api.h"
33 #include "php_http_headers_api.h"
34 #include "php_http_message_api.h"
35 #include "php_http_request_api.h"
36 #include "php_http_request_method_api.h"
37 #include "php_http_persistent_handle_api.h"
38 #include "php_http_send_api.h"
39 #include "php_http_url_api.h"
41 /* {{{ proto string http_date([int timestamp])
42 Compose a valid HTTP date regarding RFC 1123 looking like: "Wed, 22 Dec 2004 11:34:47 GMT" */
43 PHP_FUNCTION(http_date
)
47 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
52 t
= HTTP_G
->request
.time
;
55 RETURN_STRING(http_date(t
), 0);
59 /* {{{ proto string http_build_url([mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE|HTTP_URL_FROM_ENV[, array &new_url]]]])
61 PHP_FUNCTION(http_build_url
)
65 long flags
= HTTP_URL_REPLACE
|HTTP_URL_FROM_ENV
;
66 zval
*z_old_url
= NULL
, *z_new_url
= NULL
, *z_composed_url
= NULL
;
67 php_url
*old_url
= NULL
, *new_url
= NULL
, *composed_url
= NULL
;
69 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z!/z!/lz", &z_old_url
, &z_new_url
, &flags
, &z_composed_url
) != SUCCESS
) {
74 if (Z_TYPE_P(z_new_url
) == IS_ARRAY
|| Z_TYPE_P(z_new_url
) == IS_OBJECT
) {
75 new_url
= http_url_from_struct(NULL
, HASH_OF(z_new_url
));
77 convert_to_string(z_new_url
);
78 if (!(new_url
= php_url_parse_ex(Z_STRVAL_P(z_new_url
), Z_STRLEN_P(z_new_url
)))) {
79 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url
));
86 if (Z_TYPE_P(z_old_url
) == IS_ARRAY
|| Z_TYPE_P(z_old_url
) == IS_OBJECT
) {
87 old_url
= http_url_from_struct(NULL
, HASH_OF(z_old_url
));
89 convert_to_string(z_old_url
);
90 if (!(old_url
= php_url_parse_ex(Z_STRVAL_P(z_old_url
), Z_STRLEN_P(z_old_url
)))) {
92 php_url_free(new_url
);
94 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url
));
100 if (z_composed_url
) {
101 http_build_url(flags
, old_url
, new_url
, &composed_url
, &url_str
, &url_len
);
102 http_url_tostruct(composed_url
, z_composed_url
);
103 php_url_free(composed_url
);
105 http_build_url(flags
, old_url
, new_url
, NULL
, &url_str
, &url_len
);
109 php_url_free(new_url
);
112 php_url_free(old_url
);
115 RETURN_STRINGL(url_str
, url_len
, 0);
119 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
120 Opponent to parse_str(). */
121 PHP_FUNCTION(http_build_str
)
124 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
125 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
128 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
133 arg_sep
= HTTP_URL_ARGSEP
;
134 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
137 phpstr_init(&formstr
);
138 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), &formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
143 phpstr_dtor(&formstr
);
147 RETURN_PHPSTR_VAL(&formstr
);
151 #define HTTP_DO_NEGOTIATE_DEFAULT(supported) \
155 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
156 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
157 RETVAL_ZVAL(*value, 1, 0); \
162 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
165 if ((result = http_negotiate_ ##type(supported))) { \
170 if (zend_hash_num_elements(result) && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
171 RETVAL_STRINGL(key, key_len-1, 0); \
173 HTTP_DO_NEGOTIATE_DEFAULT(supported); \
177 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
180 zend_hash_destroy(result); \
181 FREE_HASHTABLE(result); \
184 HTTP_DO_NEGOTIATE_DEFAULT(supported); \
190 FOREACH_VAL(pos, supported, value_ptr) { \
191 zval *value = http_zsep(IS_STRING, *value_ptr); \
192 add_assoc_double(rs_array, Z_STRVAL_P(value), 1.0); \
193 zval_ptr_dtor(&value); \
199 /* {{{ proto string http_negotiate_language(array supported[, array &result])
200 Negotiate the clients preferred language. */
201 PHP_FUNCTION(http_negotiate_language
)
203 zval
*supported
, *rs_array
= NULL
;
205 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
211 array_init(rs_array
);
214 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
218 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
219 Negotiate the clients preferred charset. */
220 PHP_FUNCTION(http_negotiate_charset
)
222 zval
*supported
, *rs_array
= NULL
;
224 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
230 array_init(rs_array
);
233 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
237 /* {{{ proto string http_negotiate_content_type(array supported[, array &result])
238 Negotiate the clients preferred content type. */
239 PHP_FUNCTION(http_negotiate_content_type
)
241 zval
*supported
, *rs_array
= NULL
;
243 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
249 array_init(rs_array
);
252 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
256 /* {{{ proto bool http_send_status(int status)
257 Send HTTP status code. */
258 PHP_FUNCTION(http_send_status
)
262 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
265 if (status
< 100 || status
> 510) {
266 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
270 RETURN_SUCCESS(http_send_status(status
));
274 /* {{{ proto bool http_send_last_modified([int timestamp])
275 Send a "Last-Modified" header with a valid HTTP date. */
276 PHP_FUNCTION(http_send_last_modified
)
280 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
285 t
= HTTP_G
->request
.time
;
288 RETURN_SUCCESS(http_send_last_modified(t
));
292 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
293 Send the Content-Type of the sent entity. This is particularly important if you use the http_send() API. */
294 PHP_FUNCTION(http_send_content_type
)
296 char *ct
= "application/x-octetstream";
297 int ct_len
= lenof("application/x-octetstream");
299 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
303 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
307 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
308 Send the Content-Disposition. */
309 PHP_FUNCTION(http_send_content_disposition
)
313 zend_bool send_inline
= 0;
315 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
318 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
322 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
323 Matches the given unix timestamp against the clients "If-Modified-Since" resp. "If-Unmodified-Since" HTTP headers. */
324 PHP_FUNCTION(http_match_modified
)
327 zend_bool for_range
= 0;
329 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
333 // current time if not supplied (senseless though)
335 t
= HTTP_G
->request
.time
;
339 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
341 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
345 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
346 Matches the given ETag against the clients "If-Match" resp. "If-None-Match" HTTP headers. */
347 PHP_FUNCTION(http_match_etag
)
351 zend_bool for_range
= 0;
353 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
358 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
360 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
364 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
365 Attempts to cache the sent entity by its last modification date. */
366 PHP_FUNCTION(http_cache_last_modified
)
368 long last_modified
= 0, send_modified
= 0, t
;
371 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
375 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
377 t
= HTTP_G
->request
.time
;
380 if (!last_modified
) {
381 /* does the client have? (att: caching "forever") */
382 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE", 1))) {
383 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
384 /* send current time */
388 /* negative value is supposed to be expiration time */
389 } else if (last_modified
< 0) {
392 /* send supplied time explicitly */
394 send_modified
= last_modified
;
397 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
401 /* {{{ proto bool http_cache_etag([string etag])
402 Attempts to cache the sent entity by its ETag, either supplied or generated by the hash algorithm specified by the INI setting "http.etag.mode". */
403 PHP_FUNCTION(http_cache_etag
)
408 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
412 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
414 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
418 /* {{{ proto string ob_etaghandler(string data, int mode)
419 For use with ob_start(). Output buffer handler generating an ETag with the hash algorithm specified with the INI setting "http.etag.mode". */
420 PHP_FUNCTION(ob_etaghandler
)
426 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
430 Z_TYPE_P(return_value
) = IS_STRING
;
431 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
435 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
436 Sets the throttle delay and send buffer size for use with http_send() API. */
437 PHP_FUNCTION(http_throttle
)
439 long chunk_size
= HTTP_SENDBUF_SIZE
;
442 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
446 HTTP_G
->send
.throttle_delay
= interval
;
447 HTTP_G
->send
.buffer_size
= chunk_size
;
451 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
452 Redirect to the given url. */
453 PHP_FUNCTION(http_redirect
)
456 size_t query_len
= 0;
457 zend_bool session
= 0, free_params
= 0;
459 long status
= HTTP_REDIRECT
;
460 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
462 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
466 #ifdef HTTP_HAVE_SESSION
467 /* append session info */
471 MAKE_STD_ZVAL(params
);
474 if (PS(session_status
) == php_session_active
) {
475 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
476 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
482 /* treat params array with http_build_query() */
484 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
496 URI
= http_absolute_url_ex(url
, HTTP_URL_FROM_ENV
);
499 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
501 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
504 spprintf(&LOC
, 0, "Location: %s", URI
);
506 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
521 RETVAL_SUCCESS(http_send_status_header(status
, LOC
));
525 case HTTP_REDIRECT_PERM
:
526 case HTTP_REDIRECT_FOUND
:
527 case HTTP_REDIRECT_POST
:
528 case HTTP_REDIRECT_PROXY
:
529 case HTTP_REDIRECT_TEMP
:
534 http_error_ex(HE_NOTICE
, HTTP_E_RUNTIME
, "Unsupported redirection status code: %ld", status
);
536 if ( SG(request_info
).request_method
&&
537 strcasecmp(SG(request_info
).request_method
, "HEAD") &&
538 strcasecmp(SG(request_info
).request_method
, "GET")) {
539 status
= HTTP_REDIRECT_POST
;
541 status
= HTTP_REDIRECT_FOUND
;
546 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
550 /* {{{ proto bool http_send_data(string data)
551 Sends raw data with support for (multiple) range requests. */
552 PHP_FUNCTION(http_send_data
)
557 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data_buf
, &data_len
) != SUCCESS
) {
561 RETURN_SUCCESS(http_send_data(data_buf
, data_len
));
565 /* {{{ proto bool http_send_file(string file)
566 Sends a file with support for (multiple) range requests. */
567 PHP_FUNCTION(http_send_file
)
572 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
579 RETURN_SUCCESS(http_send_file(file
));
583 /* {{{ proto bool http_send_stream(resource stream)
584 Sends an already opened stream with support for (multiple) range requests. */
585 PHP_FUNCTION(http_send_stream
)
590 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
594 php_stream_from_zval(file
, &zstream
);
595 RETURN_SUCCESS(http_send_stream(file
));
599 /* {{{ proto string http_chunked_decode(string encoded)
600 Decodes a string that was HTTP-chunked encoded. */
601 PHP_FUNCTION(http_chunked_decode
)
603 char *encoded
= NULL
, *decoded
= NULL
;
604 size_t decoded_len
= 0;
607 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
611 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
612 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
619 /* {{{ proto object http_parse_message(string message)
620 Parses (a) http_message(s) into a simple recursive object structure. */
621 PHP_FUNCTION(http_parse_message
)
625 http_message
*msg
= NULL
;
627 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
631 if ((msg
= http_message_parse(message
, message_len
))) {
632 object_init(return_value
);
633 http_message_tostruct_recursive(msg
, return_value
);
634 http_message_free(&msg
);
641 /* {{{ proto array http_parse_headers(string header)
642 Parses HTTP headers into an associative array. */
643 PHP_FUNCTION(http_parse_headers
)
648 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
652 array_init(return_value
);
653 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
654 zval_dtor(return_value
);
655 http_error(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Failed to parse headers");
661 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
662 Parses HTTP cookies like sent in a response into a struct. */
663 PHP_FUNCTION(http_parse_cookie
)
665 char *cookie
, **allowed_extras
= NULL
;
666 int i
= 0, cookie_len
;
668 zval
*allowed_extras_array
= NULL
, **entry
= NULL
;
670 http_cookie_list list
;
672 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|la!", &cookie
, &cookie_len
, &flags
, &allowed_extras_array
)) {
676 if (allowed_extras_array
) {
677 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
678 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
679 zval
*data
= http_zsep(IS_STRING
, *entry
);
680 allowed_extras
[i
++] = estrndup(Z_STRVAL_P(data
), Z_STRLEN_P(data
));
681 zval_ptr_dtor(&data
);
685 if (http_parse_cookie_ex(&list
, cookie
, flags
, allowed_extras
)) {
686 object_init(return_value
);
687 http_cookie_list_tostruct(&list
, return_value
);
688 http_cookie_list_dtor(&list
);
693 if (allowed_extras
) {
694 for (i
= 0; allowed_extras
[i
]; ++i
) {
695 efree(allowed_extras
[i
]);
697 efree(allowed_extras
);
702 /* {{{ proto string http_build_cookie(array cookie)
703 Build a cookie string from an array/object like returned by http_parse_cookie(). */
704 PHP_FUNCTION(http_build_cookie
)
709 http_cookie_list list
;
711 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &strct
)) {
715 http_cookie_list_fromstruct(&list
, strct
);
716 http_cookie_list_tostring(&list
, &str
, &len
);
717 http_cookie_list_dtor(&list
);
719 RETURN_STRINGL(str
, len
, 0);
723 /* {{{ proto object http_parse_params(string param[, int flags = HTTP_PARAMS_DEFAULT])
724 Parse parameter list. */
725 PHP_FUNCTION(http_parse_params
)
730 long flags
= HTTP_PARAMS_DEFAULT
;
732 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", ¶m
, ¶m_len
, &flags
)) {
736 MAKE_STD_ZVAL(params
);
738 if (SUCCESS
!= http_parse_params(param
, flags
, Z_ARRVAL_P(params
))) {
739 zval_ptr_dtor(¶ms
);
743 object_init(return_value
);
744 add_property_zval(return_value
, "params", params
);
746 zval_ptr_dtor(¶ms
);
751 /* {{{ proto array http_get_request_headers(void)
752 Get a list of incoming HTTP headers. */
753 PHP_FUNCTION(http_get_request_headers
)
757 array_init(return_value
);
758 http_get_request_headers(Z_ARRVAL_P(return_value
));
762 /* {{{ proto string http_get_request_body(void)
763 Get the raw request body (e.g. POST or PUT data). */
764 PHP_FUNCTION(http_get_request_body
)
771 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
772 RETURN_STRINGL(body
, (int) length
, 0);
779 /* {{{ proto resource http_get_request_body_stream(void)
780 Create a stream to read the raw request body (e.g. POST or PUT data). This function can only be used once if the request method was another than POST. */
781 PHP_FUNCTION(http_get_request_body_stream
)
787 if ((s
= http_get_request_body_stream())) {
788 php_stream_to_zval(s
, return_value
);
790 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to create request body stream");
796 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
797 Match an incoming HTTP header. */
798 PHP_FUNCTION(http_match_request_header
)
800 char *header
, *value
;
801 int header_len
, value_len
;
802 zend_bool match_case
= 0;
804 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
808 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
812 /* {{{ proto object http_persistent_handles_count() */
813 PHP_FUNCTION(http_persistent_handles_count
)
816 object_init(return_value
);
817 if (!http_persistent_handle_statall_ex(HASH_OF(return_value
))) {
818 zval_dtor(return_value
);
824 /* {{{ proto void http_persistent_handles_clean([string name]) */
825 PHP_FUNCTION(http_persistent_handles_clean
)
827 char *name_str
= NULL
;
830 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &name_str
, &name_len
)) {
831 http_persistent_handle_cleanup_ex(name_str
, name_len
, 1);
836 /* {{{ proto string http_persistent_handles_ident([string ident]) */
837 PHP_FUNCTION(http_persistent_handles_ident
)
839 char *ident_str
= NULL
;
842 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ident_str
, &ident_len
)) {
843 RETVAL_STRING(zend_ini_string(ZEND_STRS("http.persistent.handles.ident"), 0), 1);
844 if (ident_str
&& ident_len
) {
845 zend_alter_ini_entry(ZEND_STRS("http.persistent.handles.ident"), ident_str
, ident_len
, ZEND_INI_USER
, PHP_INI_STAGE_RUNTIME
);
852 #ifdef HTTP_HAVE_CURL
854 #define RETVAL_RESPONSE_OR_BODY(request) \
858 /* check if only the body should be returned */ \
859 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && i_zend_is_true(*bodyonly)) { \
860 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
863 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
864 http_message_free(&msg); \
867 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
871 /* {{{ proto string http_get(string url[, array options[, array &info]])
872 Performs an HTTP GET request on the supplied url. */
873 PHP_FUNCTION(http_get
)
875 zval
*options
= NULL
, *info
= NULL
;
878 http_request request
;
880 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
891 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
892 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
893 http_request_exec(&request
);
895 http_request_info(&request
, Z_ARRVAL_P(info
));
897 RETVAL_RESPONSE_OR_BODY(request
);
899 http_request_dtor(&request
);
903 /* {{{ proto string http_head(string url[, array options[, array &info]])
904 Performs an HTTP HEAD request on the supplied url. */
905 PHP_FUNCTION(http_head
)
907 zval
*options
= NULL
, *info
= NULL
;
910 http_request request
;
912 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
923 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
924 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
925 http_request_exec(&request
);
927 http_request_info(&request
, Z_ARRVAL_P(info
));
929 RETVAL_RESPONSE_OR_BODY(request
);
931 http_request_dtor(&request
);
935 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
936 Performs an HTTP POST request on the supplied url. */
937 PHP_FUNCTION(http_post_data
)
939 zval
*options
= NULL
, *info
= NULL
;
940 char *URL
, *postdata
;
941 int postdata_len
, URL_len
;
942 http_request_body body
;
943 http_request request
;
945 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
956 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
957 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
958 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
959 http_request_exec(&request
);
961 http_request_info(&request
, Z_ARRVAL_P(info
));
963 RETVAL_RESPONSE_OR_BODY(request
);
965 http_request_dtor(&request
);
969 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
970 Performs an HTTP POST request on the supplied url. */
971 PHP_FUNCTION(http_post_fields
)
973 zval
*options
= NULL
, *info
= NULL
, *fields
= NULL
, *files
= NULL
;
976 http_request_body body
;
977 http_request request
;
979 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa!|a!a/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
983 if (!http_request_body_fill(&body
, fields
? Z_ARRVAL_P(fields
) : NULL
, files
? Z_ARRVAL_P(files
) : NULL
)) {
994 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
995 request
.body
= &body
;
996 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
997 http_request_exec(&request
);
999 http_request_info(&request
, Z_ARRVAL_P(info
));
1001 RETVAL_RESPONSE_OR_BODY(request
);
1003 http_request_dtor(&request
);
1007 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1008 Performs an HTTP PUT request on the supplied url. */
1009 PHP_FUNCTION(http_put_file
)
1013 zval
*options
= NULL
, *info
= NULL
;
1015 php_stream_statbuf ssb
;
1016 http_request_body body
;
1017 http_request request
;
1019 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1023 if (!(stream
= php_stream_open_wrapper_ex(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
))) {
1026 if (php_stream_stat(stream
, &ssb
)) {
1027 php_stream_close(stream
);
1038 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1039 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
1040 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1041 http_request_exec(&request
);
1043 http_request_info(&request
, Z_ARRVAL_P(info
));
1045 RETVAL_RESPONSE_OR_BODY(request
);
1047 http_request_dtor(&request
);
1051 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1052 Performs an HTTP PUT request on the supplied url. */
1053 PHP_FUNCTION(http_put_stream
)
1055 zval
*resource
, *options
= NULL
, *info
= NULL
;
1059 php_stream_statbuf ssb
;
1060 http_request_body body
;
1061 http_request request
;
1063 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1067 php_stream_from_zval(stream
, &resource
);
1068 if (php_stream_stat(stream
, &ssb
)) {
1079 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1080 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 0);
1081 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1082 http_request_exec(&request
);
1084 http_request_info(&request
, Z_ARRVAL_P(info
));
1086 RETVAL_RESPONSE_OR_BODY(request
);
1088 http_request_dtor(&request
);
1092 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1093 Performs an HTTP PUT request on the supplied url. */
1094 PHP_FUNCTION(http_put_data
)
1097 int URL_len
, data_len
;
1098 zval
*options
= NULL
, *info
= NULL
;
1099 http_request_body body
;
1100 http_request request
;
1102 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1113 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1114 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1115 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1116 http_request_exec(&request
);
1118 http_request_info(&request
, Z_ARRVAL_P(info
));
1120 RETVAL_RESPONSE_OR_BODY(request
);
1122 http_request_dtor(&request
);
1126 /* {{{ proto string http_request(int method, string url[, string body[, array options[, array &info]]])
1127 Performs a custom HTTP request on the supplied url. */
1128 PHP_FUNCTION(http_request
)
1131 char *URL
, *data
= NULL
;
1132 int URL_len
, data_len
= 0;
1133 zval
*options
= NULL
, *info
= NULL
;
1134 http_request_body body
;
1135 http_request request
;
1137 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ls|sa/!z", &meth
, &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1148 http_request_init_ex(&request
, NULL
, meth
, URL
);
1149 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1150 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1151 http_request_exec(&request
);
1153 http_request_info(&request
, Z_ARRVAL_P(info
));
1155 RETVAL_RESPONSE_OR_BODY(request
);
1157 http_request_dtor(&request
);
1161 /* {{{ proto string http_request_body_encode(array fields, array files)
1162 Generate x-www-form-urlencoded resp. form-data encoded request body. */
1163 PHP_FUNCTION(http_request_body_encode
)
1165 zval
*fields
= NULL
, *files
= NULL
;
1166 HashTable
*fields_ht
, *files_ht
;
1167 http_request_body body
;
1171 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!a!", &fields
, &files
)) {
1175 fields_ht
= (fields
&& Z_TYPE_P(fields
) == IS_ARRAY
) ? Z_ARRVAL_P(fields
) : NULL
;
1176 files_ht
= (files
&& Z_TYPE_P(files
) == IS_ARRAY
) ? Z_ARRVAL_P(files
) : NULL
;
1177 if (http_request_body_fill(&body
, fields_ht
, files_ht
) && (SUCCESS
== http_request_body_encode(&body
, &buf
, &len
))) {
1178 RETVAL_STRINGL(buf
, len
, 0);
1180 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not encode request body");
1183 http_request_body_dtor(&body
);
1185 #endif /* HTTP_HAVE_CURL */
1188 /* {{{ proto int http_request_method_register(string method)
1189 Register a custom request method. */
1190 PHP_FUNCTION(http_request_method_register
)
1196 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1199 if ((existing
= http_request_method_exists(1, 0, method
))) {
1200 RETURN_LONG((long) existing
);
1203 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1207 /* {{{ proto bool http_request_method_unregister(mixed method)
1208 Unregister a previously registered custom request method. */
1209 PHP_FUNCTION(http_request_method_unregister
)
1213 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1217 switch (Z_TYPE_P(method
)) {
1219 convert_to_string(method
);
1221 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1222 convert_to_long(method
);
1225 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1229 ZVAL_LONG(method
, (long)mn
);
1232 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1239 /* {{{ proto int http_request_method_exists(mixed method)
1240 Check if a request method is registered (or available by default). */
1241 PHP_FUNCTION(http_request_method_exists
)
1243 if (return_value_used
) {
1246 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1250 switch (Z_TYPE_P(method
)) {
1252 convert_to_string(method
);
1254 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1255 convert_to_long(method
);
1257 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1260 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method
), NULL
));
1268 /* {{{ proto string http_request_method_name(int method)
1269 Get the literal string representation of a standard or registered request method. */
1270 PHP_FUNCTION(http_request_method_name
)
1272 if (return_value_used
) {
1275 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1279 RETURN_STRING(estrdup(http_request_method_name((int) method
)), 0);
1285 #ifdef HTTP_HAVE_ZLIB
1287 /* {{{ proto string http_deflate(string data[, int flags = 0])
1288 Compress data with gzip, zlib AKA deflate or raw deflate encoding. */
1289 PHP_FUNCTION(http_deflate
)
1297 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &data
, &data_len
, &flags
)) {
1301 if (SUCCESS
== http_encoding_deflate(flags
, data
, data_len
, &encoded
, &encoded_len
)) {
1302 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1308 /* {{{ proto string http_inflate(string data)
1309 Decompress data compressed with either gzip, deflate AKA zlib or raw deflate encoding. */
1310 PHP_FUNCTION(http_inflate
)
1317 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1321 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1322 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1328 /* {{{ proto string ob_deflatehandler(string data, int mode)
1329 For use with ob_start(). The deflate output buffer handler can only be used once. */
1330 PHP_FUNCTION(ob_deflatehandler
)
1336 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1340 http_ob_deflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1341 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1345 /* {{{ proto string ob_inflatehandler(string data, int mode)
1346 For use with ob_start(). Same restrictions as with ob_deflatehandler apply. */
1347 PHP_FUNCTION(ob_inflatehandler
)
1353 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1357 http_ob_inflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1358 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1362 #endif /* HTTP_HAVE_ZLIB */
1365 /* {{{ proto int http_support([int feature = 0])
1366 Check for feature that require external libraries. */
1367 PHP_FUNCTION(http_support
)
1373 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
1374 RETVAL_LONG(http_support(feature
));
1384 * vim600: noet sw=4 ts=4 fdm=marker
1385 * vim<600: noet sw=4 ts=4