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-2006, 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 #if PHP_MAJOR_VERSION == 4 && PHP_MINOR_VERSION == 3 && PHP_RELEASE_VERSION < 10
60 # define php_url_parse_ex(u, l) php_url_parse(u)
63 /* {{{ proto string http_build_url([mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]]])
65 PHP_FUNCTION(http_build_url
)
69 long flags
= HTTP_URL_REPLACE
;
70 zval
*z_old_url
= NULL
, *z_new_url
= NULL
, *z_composed_url
= NULL
;
71 php_url
*old_url
= NULL
, *new_url
= NULL
, *composed_url
= NULL
;
73 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z!/z!/lz", &z_old_url
, &z_new_url
, &flags
, &z_composed_url
) != SUCCESS
) {
78 if (Z_TYPE_P(z_new_url
) == IS_ARRAY
|| Z_TYPE_P(z_new_url
) == IS_OBJECT
) {
79 new_url
= http_url_from_struct(NULL
, HASH_OF(z_new_url
));
81 convert_to_string(z_new_url
);
82 if (!(new_url
= php_url_parse_ex(Z_STRVAL_P(z_new_url
), Z_STRLEN_P(z_new_url
)))) {
83 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url
));
90 if (Z_TYPE_P(z_old_url
) == IS_ARRAY
|| Z_TYPE_P(z_old_url
) == IS_OBJECT
) {
91 old_url
= http_url_from_struct(NULL
, HASH_OF(z_old_url
));
93 convert_to_string(z_old_url
);
94 if (!(old_url
= php_url_parse_ex(Z_STRVAL_P(z_old_url
), Z_STRLEN_P(z_old_url
)))) {
96 php_url_free(new_url
);
98 http_error_ex(HE_WARNING
, HTTP_E_URL
, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url
));
104 if (z_composed_url
) {
105 http_build_url(flags
, old_url
, new_url
, &composed_url
, &url_str
, &url_len
);
106 http_url_tostruct(composed_url
, z_composed_url
);
107 php_url_free(composed_url
);
109 http_build_url(flags
, old_url
, new_url
, NULL
, &url_str
, &url_len
);
113 php_url_free(new_url
);
116 php_url_free(old_url
);
119 RETURN_STRINGL(url_str
, url_len
, 0);
123 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
124 Opponent to parse_str(). */
125 PHP_FUNCTION(http_build_str
)
128 char *prefix
= NULL
, *arg_sep
= INI_STR("arg_separator.output");
129 int prefix_len
= 0, arg_sep_len
= strlen(arg_sep
);
132 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|ss", &formdata
, &prefix
, &prefix_len
, &arg_sep
, &arg_sep_len
) != SUCCESS
) {
137 arg_sep
= HTTP_URL_ARGSEP
;
138 arg_sep_len
= lenof(HTTP_URL_ARGSEP
);
141 phpstr_init(&formstr
);
142 if (SUCCESS
!= http_urlencode_hash_recursive(HASH_OF(formdata
), &formstr
, arg_sep
, arg_sep_len
, prefix
, prefix_len
)) {
147 phpstr_dtor(&formstr
);
151 RETURN_PHPSTR_VAL(&formstr
);
155 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
158 if ((result = http_negotiate_ ##type(supported))) { \
163 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
164 RETVAL_STRINGL(key, key_len-1, 0); \
170 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
173 zend_hash_destroy(result); \
174 FREE_HASHTABLE(result); \
179 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
180 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
181 RETVAL_ZVAL(*value, 1, 0); \
190 FOREACH_VAL(pos, supported, value) { \
191 convert_to_string_ex(value); \
192 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
198 /* {{{ proto string http_negotiate_language(array supported[, array &result])
199 Negotiate the clients preferred language. */
200 PHP_FUNCTION(http_negotiate_language
)
202 zval
*supported
, *rs_array
= NULL
;
204 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
210 array_init(rs_array
);
213 HTTP_DO_NEGOTIATE(language
, supported
, rs_array
);
217 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
218 Negotiate the clients preferred charset. */
219 PHP_FUNCTION(http_negotiate_charset
)
221 zval
*supported
, *rs_array
= NULL
;
223 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
) != SUCCESS
) {
229 array_init(rs_array
);
232 HTTP_DO_NEGOTIATE(charset
, supported
, rs_array
);
236 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
237 Negotiate the clients preferred content type. */
238 PHP_FUNCTION(http_negotiate_content_type
)
240 zval
*supported
, *rs_array
= NULL
;
242 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|z", &supported
, &rs_array
)) {
248 array_init(rs_array
);
251 HTTP_DO_NEGOTIATE(content_type
, supported
, rs_array
);
255 /* {{{ proto bool http_send_status(int status)
256 Send HTTP status code. */
257 PHP_FUNCTION(http_send_status
)
261 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
264 if (status
< 100 || status
> 510) {
265 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Invalid HTTP status code (100-510): %d", status
);
269 RETURN_SUCCESS(http_send_status(status
));
273 /* {{{ proto bool http_send_last_modified([int timestamp])
274 Send a "Last-Modified" header with a valid HTTP date. */
275 PHP_FUNCTION(http_send_last_modified
)
279 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
284 t
= HTTP_G
->request
.time
;
287 RETURN_SUCCESS(http_send_last_modified(t
));
291 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
292 Send the Content-Type of the sent entity. This is particularly important if you use the http_send() API. */
293 PHP_FUNCTION(http_send_content_type
)
295 char *ct
= "application/x-octetstream";
296 int ct_len
= lenof("application/x-octetstream");
298 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
302 RETURN_SUCCESS(http_send_content_type(ct
, ct_len
));
306 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
307 Send the Content-Disposition. */
308 PHP_FUNCTION(http_send_content_disposition
)
312 zend_bool send_inline
= 0;
314 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
317 RETURN_SUCCESS(http_send_content_disposition(filename
, f_len
, send_inline
));
321 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
322 Matches the given unix timestamp against the clients "If-Modified-Since" resp. "If-Unmodified-Since" HTTP headers. */
323 PHP_FUNCTION(http_match_modified
)
326 zend_bool for_range
= 0;
328 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|lb", &t
, &for_range
) != SUCCESS
) {
332 // current time if not supplied (senseless though)
334 t
= HTTP_G
->request
.time
;
338 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t
));
340 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t
));
344 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
345 Matches the given ETag against the clients "If-Match" resp. "If-None-Match" HTTP headers. */
346 PHP_FUNCTION(http_match_etag
)
350 zend_bool for_range
= 0;
352 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &etag
, &etag_len
, &for_range
) != SUCCESS
) {
357 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag
));
359 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag
));
363 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
364 Attempts to cache the sent entity by its last modification date. */
365 PHP_FUNCTION(http_cache_last_modified
)
367 long last_modified
= 0, send_modified
= 0, t
;
370 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
374 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
376 t
= HTTP_G
->request
.time
;
379 if (!last_modified
) {
380 /* does the client have? (att: caching "forever") */
381 if ((zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE", 1))) {
382 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
383 /* send current time */
387 /* negative value is supposed to be expiration time */
388 } else if (last_modified
< 0) {
391 /* send supplied time explicitly */
393 send_modified
= last_modified
;
396 RETURN_SUCCESS(http_cache_last_modified(last_modified
, send_modified
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
400 /* {{{ proto bool http_cache_etag([string etag])
401 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". */
402 PHP_FUNCTION(http_cache_etag
)
407 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
411 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE
);
413 RETURN_SUCCESS(http_cache_etag(etag
, etag_len
, HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
)));
417 /* {{{ proto string ob_etaghandler(string data, int mode)
418 For use with ob_start(). Output buffer handler generating an ETag with the hash algorithm specified with the INI setting "http.etag.mode". */
419 PHP_FUNCTION(ob_etaghandler
)
425 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
429 Z_TYPE_P(return_value
) = IS_STRING
;
430 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
434 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
435 Sets the throttle delay and send buffer size for use with http_send() API. */
436 PHP_FUNCTION(http_throttle
)
438 long chunk_size
= HTTP_SENDBUF_SIZE
;
441 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "d|l", &interval
, &chunk_size
)) {
445 HTTP_G
->send
.throttle_delay
= interval
;
446 HTTP_G
->send
.buffer_size
= chunk_size
;
450 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
451 Redirect to the given url. */
452 PHP_FUNCTION(http_redirect
)
455 size_t query_len
= 0;
456 zend_bool session
= 0, free_params
= 0;
458 long status
= HTTP_REDIRECT
;
459 char *query
= NULL
, *url
= NULL
, *URI
, *LOC
, *RED
= NULL
;
461 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bl", &url
, &url_len
, ¶ms
, &session
, &status
) != SUCCESS
) {
465 #ifdef HTTP_HAVE_SESSION
466 /* append session info */
470 MAKE_STD_ZVAL(params
);
473 if (PS(session_status
) == php_session_active
) {
474 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
475 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not append session information");
481 /* treat params array with http_build_query() */
483 if (SUCCESS
!= http_urlencode_hash_ex(Z_ARRVAL_P(params
), 0, NULL
, 0, &query
, &query_len
)) {
495 URI
= http_absolute_url(url
);
498 spprintf(&LOC
, 0, "Location: %s?%s", URI
, query
);
500 spprintf(&RED
, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, query
, URI
, query
);
503 spprintf(&LOC
, 0, "Location: %s", URI
);
505 spprintf(&RED
, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
520 RETVAL_SUCCESS(http_send_status_header(status
, LOC
));
524 case HTTP_REDIRECT_PERM
:
525 case HTTP_REDIRECT_FOUND
:
526 case HTTP_REDIRECT_POST
:
527 case HTTP_REDIRECT_PROXY
:
528 case HTTP_REDIRECT_TEMP
:
533 http_error_ex(HE_NOTICE
, HTTP_E_RUNTIME
, "Unsupported redirection status code: %ld", status
);
535 if ( SG(request_info
).request_method
&&
536 strcasecmp(SG(request_info
).request_method
, "HEAD") &&
537 strcasecmp(SG(request_info
).request_method
, "GET")) {
538 status
= HTTP_REDIRECT_POST
;
540 status
= HTTP_REDIRECT_FOUND
;
545 RETURN_SUCCESS(http_exit_ex(status
, LOC
, RED
, 1));
549 /* {{{ proto bool http_send_data(string data)
550 Sends raw data with support for (multiple) range requests. */
551 PHP_FUNCTION(http_send_data
)
556 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data_buf
, &data_len
) != SUCCESS
) {
560 RETURN_SUCCESS(http_send_data(data_buf
, data_len
));
564 /* {{{ proto bool http_send_file(string file)
565 Sends a file with support for (multiple) range requests. */
566 PHP_FUNCTION(http_send_file
)
571 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &file
, &flen
) != SUCCESS
) {
578 RETURN_SUCCESS(http_send_file(file
));
582 /* {{{ proto bool http_send_stream(resource stream)
583 Sends an already opened stream with support for (multiple) range requests. */
584 PHP_FUNCTION(http_send_stream
)
589 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
593 php_stream_from_zval(file
, &zstream
);
594 RETURN_SUCCESS(http_send_stream(file
));
598 /* {{{ proto string http_chunked_decode(string encoded)
599 Decodes a string that was HTTP-chunked encoded. */
600 PHP_FUNCTION(http_chunked_decode
)
602 char *encoded
= NULL
, *decoded
= NULL
;
603 size_t decoded_len
= 0;
606 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
610 if (NULL
!= http_encoding_dechunk(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
611 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
618 /* {{{ proto object http_parse_message(string message)
619 Parses (a) http_message(s) into a simple recursive object structure. */
620 PHP_FUNCTION(http_parse_message
)
624 http_message
*msg
= NULL
;
626 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &message
, &message_len
)) {
630 if ((msg
= http_message_parse(message
, message_len
))) {
631 object_init(return_value
);
632 http_message_tostruct_recursive(msg
, return_value
);
633 http_message_free(&msg
);
640 /* {{{ proto array http_parse_headers(string header)
641 Parses HTTP headers into an associative array. */
642 PHP_FUNCTION(http_parse_headers
)
647 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
651 array_init(return_value
);
652 if (SUCCESS
!= http_parse_headers(header
, return_value
)) {
653 zval_dtor(return_value
);
654 http_error(HE_WARNING
, HTTP_E_MALFORMED_HEADERS
, "Failed to parse headers");
660 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
661 Parses HTTP cookies like sent in a response into a struct. */
662 PHP_FUNCTION(http_parse_cookie
)
664 char *cookie
, **allowed_extras
= NULL
;
665 int i
= 0, cookie_len
;
667 zval
*allowed_extras_array
= NULL
, **entry
= NULL
;
669 http_cookie_list list
;
671 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|la!", &cookie
, &cookie_len
, &flags
, &allowed_extras_array
)) {
675 if (allowed_extras_array
) {
676 allowed_extras
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array
)) + 1, sizeof(char *));
677 FOREACH_VAL(pos
, allowed_extras_array
, entry
) {
679 convert_to_string_ex(entry
);
680 allowed_extras
[i
++] = estrndup(Z_STRVAL_PP(entry
), Z_STRLEN_PP(entry
));
681 zval_ptr_dtor(entry
);
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 params
= ecalloc(1, sizeof(zval
));
738 if (SUCCESS
!= http_parse_params(param
, flags
, Z_ARRVAL_P(params
))) {
743 object_init(return_value
);
744 add_property_zval(return_value
, "params", params
);
748 /* {{{ proto array http_get_request_headers(void)
749 Get a list of incoming HTTP headers. */
750 PHP_FUNCTION(http_get_request_headers
)
754 array_init(return_value
);
755 http_get_request_headers(Z_ARRVAL_P(return_value
));
759 /* {{{ proto string http_get_request_body(void)
760 Get the raw request body (e.g. POST or PUT data). */
761 PHP_FUNCTION(http_get_request_body
)
768 if (SUCCESS
== http_get_request_body(&body
, &length
)) {
769 RETURN_STRINGL(body
, (int) length
, 0);
776 /* {{{ proto resource http_get_request_body_stream(void)
777 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. */
778 PHP_FUNCTION(http_get_request_body_stream
)
784 if ((s
= http_get_request_body_stream())) {
785 php_stream_to_zval(s
, return_value
);
787 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to create request body stream");
793 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
794 Match an incoming HTTP header. */
795 PHP_FUNCTION(http_match_request_header
)
797 char *header
, *value
;
798 int header_len
, value_len
;
799 zend_bool match_case
= 0;
801 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|b", &header
, &header_len
, &value
, &value_len
, &match_case
)) {
805 RETURN_BOOL(http_match_request_header_ex(header
, value
, match_case
));
810 #ifdef HTTP_HAVE_CURL
811 #ifdef HTTP_HAVE_PERSISTENT_HANDLES
813 /* {{{ proto object http_persistent_handles_count() */
814 PHP_FUNCTION(http_persistent_handles_count
)
822 if ((n
= http_persistent_handle_statall(&names
, &counts
))) {
823 object_init(return_value
);
824 for (i
= 0; i
< n
; ++i
) {
825 add_property_long(return_value
, names
[i
], counts
[i
]);
834 /* {{{ proto void http_persistent_handles_clean([string name]) */
835 PHP_FUNCTION(http_persistent_handles_clean
)
837 char *name_str
= NULL
;
840 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &name_str
, &name_len
)) {
841 http_persistent_handle_cleanup_ex(name_str
, name_len
);
846 #endif /* HTTP_HAVE_PERSISTENT_HANDLES */
848 #define RETVAL_RESPONSE_OR_BODY(request) \
852 /* check if only the body should be returned */ \
853 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
854 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
857 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
858 http_message_free(&msg); \
861 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
865 /* {{{ proto string http_get(string url[, array options[, array &info]])
866 Performs an HTTP GET request on the supplied url. */
867 PHP_FUNCTION(http_get
)
869 zval
*options
= NULL
, *info
= NULL
;
872 http_request request
;
874 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
885 http_request_init_ex(&request
, NULL
, HTTP_GET
, URL
);
886 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
887 http_request_exec(&request
);
889 http_request_info(&request
, Z_ARRVAL_P(info
));
891 RETVAL_RESPONSE_OR_BODY(request
);
893 http_request_dtor(&request
);
897 /* {{{ proto string http_head(string url[, array options[, array &info]])
898 Performs an HTTP HEAD request on the supplied url. */
899 PHP_FUNCTION(http_head
)
901 zval
*options
= NULL
, *info
= NULL
;
904 http_request request
;
906 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
917 http_request_init_ex(&request
, NULL
, HTTP_HEAD
, URL
);
918 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
919 http_request_exec(&request
);
921 http_request_info(&request
, Z_ARRVAL_P(info
));
923 RETVAL_RESPONSE_OR_BODY(request
);
925 http_request_dtor(&request
);
929 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
930 Performs an HTTP POST request on the supplied url. */
931 PHP_FUNCTION(http_post_data
)
933 zval
*options
= NULL
, *info
= NULL
;
934 char *URL
, *postdata
;
935 int postdata_len
, URL_len
;
936 http_request_body body
;
937 http_request request
;
939 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
950 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
951 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, postdata
, postdata_len
, 0);
952 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
953 http_request_exec(&request
);
955 http_request_info(&request
, Z_ARRVAL_P(info
));
957 RETVAL_RESPONSE_OR_BODY(request
);
959 http_request_dtor(&request
);
963 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
964 Performs an HTTP POST request on the supplied url. */
965 PHP_FUNCTION(http_post_fields
)
967 zval
*options
= NULL
, *info
= NULL
, *fields
= NULL
, *files
= NULL
;
970 http_request_body body
;
971 http_request request
;
973 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa!|a!a/!z", &URL
, &URL_len
, &fields
, &files
, &options
, &info
) != SUCCESS
) {
977 if (!http_request_body_fill(&body
, fields
? Z_ARRVAL_P(fields
) : NULL
, files
? Z_ARRVAL_P(files
) : NULL
)) {
988 http_request_init_ex(&request
, NULL
, HTTP_POST
, URL
);
989 request
.body
= &body
;
990 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
991 http_request_exec(&request
);
993 http_request_info(&request
, Z_ARRVAL_P(info
));
995 RETVAL_RESPONSE_OR_BODY(request
);
997 http_request_dtor(&request
);
1001 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1002 Performs an HTTP PUT request on the supplied url. */
1003 PHP_FUNCTION(http_put_file
)
1007 zval
*options
= NULL
, *info
= NULL
;
1009 php_stream_statbuf ssb
;
1010 http_request_body body
;
1011 http_request request
;
1013 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &file
, &f_len
, &options
, &info
)) {
1017 if (!(stream
= php_stream_open_wrapper_ex(file
, "rb", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
, HTTP_DEFAULT_STREAM_CONTEXT
))) {
1020 if (php_stream_stat(stream
, &ssb
)) {
1021 php_stream_close(stream
);
1032 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1033 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 1);
1034 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1035 http_request_exec(&request
);
1037 http_request_info(&request
, Z_ARRVAL_P(info
));
1039 RETVAL_RESPONSE_OR_BODY(request
);
1041 http_request_dtor(&request
);
1045 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1046 Performs an HTTP PUT request on the supplied url. */
1047 PHP_FUNCTION(http_put_stream
)
1049 zval
*resource
, *options
= NULL
, *info
= NULL
;
1053 php_stream_statbuf ssb
;
1054 http_request_body body
;
1055 http_request request
;
1057 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sr|a/!z", &URL
, &URL_len
, &resource
, &options
, &info
)) {
1061 php_stream_from_zval(stream
, &resource
);
1062 if (php_stream_stat(stream
, &ssb
)) {
1073 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1074 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_UPLOADFILE
, stream
, ssb
.sb
.st_size
, 0);
1075 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1076 http_request_exec(&request
);
1078 http_request_info(&request
, Z_ARRVAL_P(info
));
1080 RETVAL_RESPONSE_OR_BODY(request
);
1082 http_request_dtor(&request
);
1086 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1087 Performs an HTTP PUT request on the supplied url. */
1088 PHP_FUNCTION(http_put_data
)
1091 int URL_len
, data_len
;
1092 zval
*options
= NULL
, *info
= NULL
;
1093 http_request_body body
;
1094 http_request request
;
1096 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1107 http_request_init_ex(&request
, NULL
, HTTP_PUT
, URL
);
1108 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1109 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1110 http_request_exec(&request
);
1112 http_request_info(&request
, Z_ARRVAL_P(info
));
1114 RETVAL_RESPONSE_OR_BODY(request
);
1116 http_request_dtor(&request
);
1120 /* {{{ proto string http_request(int method, string url[, string body[, array options[, array &info]]])
1121 Performs a custom HTTP request on the supplied url. */
1122 PHP_FUNCTION(http_request
)
1125 char *URL
, *data
= NULL
;
1126 int URL_len
, data_len
= 0;
1127 zval
*options
= NULL
, *info
= NULL
;
1128 http_request_body body
;
1129 http_request request
;
1131 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ls|sa/!z", &meth
, &URL
, &URL_len
, &data
, &data_len
, &options
, &info
)) {
1142 http_request_init_ex(&request
, NULL
, meth
, URL
);
1143 request
.body
= http_request_body_init_ex(&body
, HTTP_REQUEST_BODY_CSTRING
, data
, data_len
, 0);
1144 if (SUCCESS
== http_request_prepare(&request
, options
?Z_ARRVAL_P(options
):NULL
)) {
1145 http_request_exec(&request
);
1147 http_request_info(&request
, Z_ARRVAL_P(info
));
1149 RETVAL_RESPONSE_OR_BODY(request
);
1151 http_request_dtor(&request
);
1155 /* {{{ proto string http_request_body_encode(array fields, array files)
1156 Generate x-www-form-urlencoded resp. form-data encoded request body. */
1157 PHP_FUNCTION(http_request_body_encode
)
1159 zval
*fields
= NULL
, *files
= NULL
;
1160 HashTable
*fields_ht
, *files_ht
;
1161 http_request_body body
;
1165 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a!a!", &fields
, &files
)) {
1169 fields_ht
= (fields
&& Z_TYPE_P(fields
) == IS_ARRAY
) ? Z_ARRVAL_P(fields
) : NULL
;
1170 files_ht
= (files
&& Z_TYPE_P(files
) == IS_ARRAY
) ? Z_ARRVAL_P(files
) : NULL
;
1171 if (http_request_body_fill(&body
, fields_ht
, files_ht
) && (SUCCESS
== http_request_body_encode(&body
, &buf
, &len
))) {
1172 RETVAL_STRINGL(buf
, len
, 0);
1174 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Could not encode request body");
1177 http_request_body_dtor(&body
);
1179 #endif /* HTTP_HAVE_CURL */
1182 /* {{{ proto int http_request_method_register(string method)
1183 Register a custom request method. */
1184 PHP_FUNCTION(http_request_method_register
)
1190 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &method
, &method_len
)) {
1193 if ((existing
= http_request_method_exists(1, 0, method
))) {
1194 RETURN_LONG((long) existing
);
1197 RETVAL_LONG((long) http_request_method_register(method
, method_len
));
1201 /* {{{ proto bool http_request_method_unregister(mixed method)
1202 Unregister a previously registered custom request method. */
1203 PHP_FUNCTION(http_request_method_unregister
)
1207 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1211 switch (Z_TYPE_P(method
)) {
1213 convert_to_string(method
);
1215 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1216 convert_to_long(method
);
1219 if (!(mn
= http_request_method_exists(1, 0, Z_STRVAL_P(method
)))) {
1223 ZVAL_LONG(method
, (long)mn
);
1226 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method
)));
1233 /* {{{ proto int http_request_method_exists(mixed method)
1234 Check if a request method is registered (or available by default). */
1235 PHP_FUNCTION(http_request_method_exists
)
1237 if (return_value_used
) {
1240 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z/", &method
)) {
1244 switch (Z_TYPE_P(method
)) {
1246 convert_to_string(method
);
1248 if (is_numeric_string(Z_STRVAL_P(method
), Z_STRLEN_P(method
), NULL
, NULL
, 1)) {
1249 convert_to_long(method
);
1251 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method
)));
1254 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method
), NULL
));
1262 /* {{{ proto string http_request_method_name(int method)
1263 Get the literal string representation of a standard or registered request method. */
1264 PHP_FUNCTION(http_request_method_name
)
1266 if (return_value_used
) {
1269 if ((SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &method
)) || (method
< 0)) {
1273 RETURN_STRING(estrdup(http_request_method_name((int) method
)), 0);
1279 #ifdef HTTP_HAVE_ZLIB
1281 /* {{{ proto string http_deflate(string data[, int flags = 0])
1282 Compress data with gzip, zlib AKA deflate or raw deflate encoding. */
1283 PHP_FUNCTION(http_deflate
)
1291 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &data
, &data_len
, &flags
)) {
1295 if (SUCCESS
== http_encoding_deflate(flags
, data
, data_len
, &encoded
, &encoded_len
)) {
1296 RETURN_STRINGL(encoded
, (int) encoded_len
, 0);
1302 /* {{{ proto string http_inflate(string data)
1303 Decompress data compressed with either gzip, deflate AKA zlib or raw deflate encoding. */
1304 PHP_FUNCTION(http_inflate
)
1311 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
1315 if (SUCCESS
== http_encoding_inflate(data
, data_len
, &decoded
, &decoded_len
)) {
1316 RETURN_STRINGL(decoded
, (int) decoded_len
, 0);
1322 /* {{{ proto string ob_deflatehandler(string data, int mode)
1323 For use with ob_start(). The deflate output buffer handler can only be used once. */
1324 PHP_FUNCTION(ob_deflatehandler
)
1330 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1334 http_ob_deflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1335 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1339 /* {{{ proto string ob_inflatehandler(string data, int mode)
1340 For use with ob_start(). Same restrictions as with ob_deflatehandler apply. */
1341 PHP_FUNCTION(ob_inflatehandler
)
1347 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
1351 http_ob_inflatehandler(data
, data_len
, &Z_STRVAL_P(return_value
), (uint
*) &Z_STRLEN_P(return_value
), mode
);
1352 Z_TYPE_P(return_value
) = Z_STRVAL_P(return_value
) ? IS_STRING
: IS_NULL
;
1356 #endif /* HTTP_HAVE_ZLIB */
1359 /* {{{ proto int http_support([int feature = 0])
1360 Check for feature that require external libraries. */
1361 PHP_FUNCTION(http_support
)
1367 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &feature
)) {
1368 RETVAL_LONG(http_support(feature
));
1378 * vim600: noet sw=4 ts=4 fdm=marker
1379 * vim<600: noet sw=4 ts=4