2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
18 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
27 #include "ext/standard/info.h"
28 #include "ext/session/php_session.h"
29 #include "ext/standard/php_string.h"
30 #include "ext/standard/php_smart_str.h"
35 #include "php_http_api.h"
38 # include "ext/standard/php_http.h"
44 # include <winsock2.h>
45 # include <sys/types.h>
48 # include <curl/curl.h>
51 # ifdef ZEND_BEGIN_ARG_INFO
52 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3
, 0)
58 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4
, 0)
65 static unsigned char http_request_info_ref_3
[] = {3, BYREF_NONE
, BYREF_NONE
, BYREF_FORCE
};
66 static unsigned char http_request_info_ref_4
[] = {4, BYREF_NONE
, BYREF_NONE
, BYREF_NONE
, BYREF_FORCE
};
70 #endif /* HTTP_HAVE_CURL */
72 ZEND_DECLARE_MODULE_GLOBALS(http
)
74 #ifdef COMPILE_DL_HTTP
79 /* {{{ http_functions[] */
80 function_entry http_functions
[] = {
81 PHP_FE(http_date
, NULL
)
82 PHP_FE(http_absolute_uri
, NULL
)
83 PHP_FE(http_negotiate_language
, NULL
)
84 PHP_FE(http_negotiate_charset
, NULL
)
85 PHP_FE(http_redirect
, NULL
)
86 PHP_FE(http_send_status
, NULL
)
87 PHP_FE(http_send_last_modified
, NULL
)
88 PHP_FE(http_match_modified
, NULL
)
89 PHP_FE(http_match_etag
, NULL
)
90 PHP_FE(http_cache_last_modified
, NULL
)
91 PHP_FE(http_cache_etag
, NULL
)
92 PHP_FE(http_content_type
, NULL
)
93 PHP_FE(http_content_disposition
, NULL
)
94 PHP_FE(http_send_data
, NULL
)
95 PHP_FE(http_send_file
, NULL
)
96 PHP_FE(http_send_stream
, NULL
)
97 PHP_FE(http_chunked_decode
, NULL
)
98 PHP_FE(http_split_response
, NULL
)
99 PHP_FE(http_parse_headers
, NULL
)
100 PHP_FE(http_get_request_headers
, NULL
)
101 #ifdef HTTP_HAVE_CURL
102 PHP_FE(http_get
, http_request_info_ref_3
)
103 PHP_FE(http_head
, http_request_info_ref_3
)
104 PHP_FE(http_post_data
, http_request_info_ref_4
)
105 PHP_FE(http_post_array
, http_request_info_ref_4
)
107 PHP_FE(http_auth_basic
, NULL
)
108 PHP_FE(http_auth_basic_cb
, NULL
)
109 #ifndef ZEND_ENGINE_2
110 PHP_FE(http_build_query
, NULL
)
116 /* {{{ http_module_entry */
117 zend_module_entry http_module_entry
= {
118 #if ZEND_MODULE_API_NO >= 20010901
119 STANDARD_MODULE_HEADER
,
128 #if ZEND_MODULE_API_NO >= 20010901
129 PHP_EXT_HTTP_VERSION
,
131 STANDARD_MODULE_PROPERTIES
135 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
136 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
138 /* {{{ proto string http_date([int timestamp])
140 * This function returns a valid HTTP date regarding RFC 822/1123
141 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
144 PHP_FUNCTION(http_date
)
148 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
153 t
= (long) time(NULL
);
156 RETURN_STRING(http_date(t
), 0);
160 /* {{{ proto string http_absolute_uri(string url[, string proto])
162 * This function returns an absolute URI constructed from url.
163 * If the url is already abolute but a different proto was supplied,
164 * only the proto part of the URI will be updated. If url has no
165 * path specified, the path of the current REQUEST_URI will be taken.
166 * The host will be taken either from the Host HTTP header of the client
167 * the SERVER_NAME or just localhost if prior are not available.
171 * url = "page.php" => http://www.example.com/current/path/page.php
172 * url = "/page.php" => http://www.example.com/page.php
173 * url = "/page.php", proto = "https" => https://www.example.com/page.php
177 PHP_FUNCTION(http_absolute_uri
)
179 char *url
= NULL
, *proto
= NULL
;
180 int url_len
= 0, proto_len
= 0;
182 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
186 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
190 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
192 * This function negotiates the clients preferred language based on its
193 * Accept-Language HTTP header. It returns the negotiated language or
194 * the default language if none match.
196 * The qualifier is recognized and languages without qualifier are rated highest.
198 * The supported parameter is expected to be an array having
199 * the supported languages as array values.
213 * include './langs/'. http_negotiate_language($langs) .'.php';
218 PHP_FUNCTION(http_negotiate_language
)
224 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
232 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
236 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
238 * This function negotiates the clients preferred charset based on its
239 * Accept-Charset HTTP header. It returns the negotiated charset or
240 * the default charset if none match.
242 * The qualifier is recognized and charset without qualifier are rated highest.
244 * The supported parameter is expected to be an array having
245 * the supported charsets as array values.
251 * 'iso-8859-1', // default
256 * $pref = http_negotiate_charset($charsets);
257 * if (!strcmp($pref, 'iso-8859-1')) {
258 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
259 * iconv_set_encoding('output_encoding', $pref);
260 * ob_start('ob_iconv_handler');
265 PHP_FUNCTION(http_negotiate_charset
)
271 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
279 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
283 /* {{{ proto bool http_send_status(int status)
285 * Send HTTP status code.
288 PHP_FUNCTION(http_send_status
)
292 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
295 if (status
< 100 || status
> 510) {
296 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
300 RETURN_SUCCESS(http_send_status(status
));
304 /* {{{ proto bool http_send_last_modified([int timestamp])
306 * This converts the given timestamp to a valid HTTP date and
307 * sends it as "Last-Modified" HTTP header. If timestamp is
308 * omitted, current time is sent.
311 PHP_FUNCTION(http_send_last_modified
)
315 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
320 t
= (long) time(NULL
);
323 RETURN_SUCCESS(http_send_last_modified(t
));
327 /* {{{ proto bool http_match_modified([int timestamp])
329 * Matches the given timestamp against the clients "If-Modified-Since" resp.
330 * "If-Unmodified-Since" HTTP headers.
333 PHP_FUNCTION(http_match_modified
)
337 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
341 // current time if not supplied (senseless though)
343 t
= (long) time(NULL
);
346 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
350 /* {{{ proto bool http_match_etag(string etag)
352 * This matches the given ETag against the clients
353 * "If-Match" resp. "If-None-Match" HTTP headers.
356 PHP_FUNCTION(http_match_etag
)
361 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
365 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
369 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
371 * If timestamp_or_exires is greater than 0, it is handled as timestamp
372 * and will be sent as date of last modification. If it is 0 or omitted,
373 * the current time will be sent as Last-Modified date. If it's negative,
374 * it is handled as expiration time in seconds, which means that if the
375 * requested last modification date is not between the calculated timespan,
376 * the Last-Modified header is updated and the actual body will be sent.
379 PHP_FUNCTION(http_cache_last_modified
)
381 long last_modified
= 0, send_modified
= 0, t
;
384 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
388 t
= (long) time(NULL
);
391 if (!last_modified
) {
392 /* does the client have? (att: caching "forever") */
393 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
394 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
395 /* send current time */
399 /* negative value is supposed to be expiration time */
400 } else if (last_modified
< 0) {
403 /* send supplied time explicitly */
405 send_modified
= last_modified
;
408 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
410 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
411 if (SUCCESS
== http_send_status(304)) {
414 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
418 RETURN_SUCCESS(http_send_last_modified(send_modified
));
422 /* {{{ proto bool http_cache_etag([string etag])
424 * This function attempts to cache the HTTP body based on an ETag,
425 * either supplied or generated through calculation of the MD5
426 * checksum of the output (uses output buffering).
428 * If clients "If-None-Match" header matches the supplied/calculated
429 * ETag, the body is considered cached on the clients side and
430 * a "304 Not Modified" status code is issued.
433 PHP_FUNCTION(http_cache_etag
)
438 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
442 php_end_ob_buffers(0 TSRMLS_CC
);
443 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
445 /* if no etag is given and we didn't already
446 * start ob_etaghandler -- start it
448 if (!HTTP_G(etag_started
) && !etag_len
) {
449 php_ob_set_internal_handler(_http_ob_etaghandler
, (uint
) 4096, "etag output handler", 0 TSRMLS_CC
);
450 HTTP_G(etag_started
) = 1;
451 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint
) 4096, 0 TSRMLS_CC
));
454 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
455 if (SUCCESS
== http_send_status(304)) {
458 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
463 RETURN_SUCCESS(http_send_etag(etag
, etag_len
));
467 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
469 * Redirect to a given url.
470 * The supplied url will be expanded with http_absolute_uri(), the params array will
471 * be treated with http_build_query() and the session identification will be appended
472 * if session is true.
474 * Depending on permanent the redirection will be issued with a permanent
475 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
478 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
479 * if the client doesn't redirect immediatly.
481 PHP_FUNCTION(http_redirect
)
484 zend_bool session
= 0, permanent
= 0;
486 smart_str qstr
= {0};
487 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9], RED
[HTTP_URI_MAXLEN
* 2 + 34];
489 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
493 /* append session info */
494 if (session
&& (PS(session_status
) == php_session_active
)) {
496 MAKE_STD_ZVAL(params
);
499 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
500 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
504 /* treat params array with http_build_query() */
506 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
510 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
516 URI
= http_absolute_uri(url
, NULL
);
518 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
519 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
522 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
523 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
527 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
528 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
535 /* {{{ proto bool http_send_data(string data)
537 * Sends raw data with support for (multiple) range requests.
540 PHP_FUNCTION(http_send_data
)
544 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
548 convert_to_string_ex(&zdata
);
549 http_send_header("Accept-Ranges: bytes");
550 RETURN_SUCCESS(http_send_data(zdata
));
554 /* {{{ proto bool http_send_file(string file)
556 * Sends a file with support for (multiple) range requests.
559 PHP_FUNCTION(http_send_file
)
563 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
567 convert_to_string_ex(&zfile
);
568 http_send_header("Accept-Ranges: bytes");
569 RETURN_SUCCESS(http_send_file(zfile
));
573 /* {{{ proto bool http_send_stream(resource stream)
575 * Sends an already opened stream with support for (multiple) range requests.
578 PHP_FUNCTION(http_send_stream
)
583 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
587 php_stream_from_zval(file
, &zstream
);
588 http_send_header("Accept-Ranges: bytes");
589 RETURN_SUCCESS(http_send_stream(file
));
593 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
595 * Sets the content type.
598 PHP_FUNCTION(http_content_type
)
600 char *ct
, *content_type
;
603 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
608 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
611 /* remember for multiple ranges */
613 efree(HTTP_G(ctype
));
615 HTTP_G(ctype
) = estrndup(ct
, ct_len
);
617 content_type
= (char *) emalloc(strlen("Content-Type: ") + ct_len
+ 1);
618 sprintf(content_type
, "Content-Type: %s", ct
);
620 RETVAL_BOOL(SUCCESS
== http_send_header(content_type
));
625 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
627 * Set the Content Disposition. The Content-Disposition header is very useful
628 * if the data actually sent came from a file or something similar, that should
629 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
632 PHP_FUNCTION(http_content_disposition
)
634 char *filename
, *header
;
636 zend_bool send_inline
= 0;
638 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
643 header
= (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len
+ 1);
644 sprintf(header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
646 header
= (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len
+ 1);
647 sprintf(header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
650 RETVAL_BOOL(SUCCESS
== http_send_header(header
));
655 /* {{{ proto string http_chunked_decode(string encoded)
657 * This function decodes a string that was HTTP-chunked encoded.
658 * Returns false on failure.
660 PHP_FUNCTION(http_chunked_decode
)
662 char *encoded
= NULL
, *decoded
= NULL
;
663 int encoded_len
= 0, decoded_len
= 0;
665 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
669 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
670 RETURN_STRINGL(decoded
, decoded_len
, 0);
677 /* {{{ proto array http_split_response(string http_response)
679 * This function splits an HTTP response into an array with headers and the
680 * content body. The returned array may look simliar to the following example:
686 * 'Status' => '200 Ok',
687 * 'Content-Type' => 'text/plain',
688 * 'Content-Language' => 'en-US'
690 * 1 => "Hello World!"
695 PHP_FUNCTION(http_split_response
)
697 zval
*zresponse
, *zbody
, *zheaders
;
699 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
703 convert_to_string_ex(&zresponse
);
705 MAKE_STD_ZVAL(zbody
);
706 MAKE_STD_ZVAL(zheaders
);
707 array_init(zheaders
);
709 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
710 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP response");
714 array_init(return_value
);
715 add_index_zval(return_value
, 0, zheaders
);
716 add_index_zval(return_value
, 1, zbody
);
720 /* {{{ proto array http_parse_headers(string header)
723 PHP_FUNCTION(http_parse_headers
)
728 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
732 array_init(return_value
);
734 if (rnrn
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
735 header_len
= rnrn
- header
+ 2;
737 if (SUCCESS
!= http_parse_headers(header
, header_len
, return_value
)) {
738 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP header");
739 zval_dtor(return_value
);
745 /* {{{ proto array http_get_request_headers(void)
748 PHP_FUNCTION(http_get_request_headers
)
750 if (ZEND_NUM_ARGS()) {
754 array_init(return_value
);
755 http_get_request_headers(return_value
);
760 #ifdef HTTP_HAVE_CURL
762 /* {{{ proto string http_get(string url[, array options[, array &info]])
764 * Performs an HTTP GET request on the supplied url.
766 * The second parameter is expected to be an associative
767 * array where the following keys will be recognized:
769 * - redirect: int, whether and how many redirects to follow
770 * - unrestrictedauth: bool, whether to continue sending credentials on
771 * redirects to a different host
772 * - proxyhost: string, proxy host in "host[:port]" format
773 * - proxyport: int, use another proxy port as specified in proxyhost
774 * - proxyauth: string, proxy credentials in "user:pass" format
775 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
776 * - httpauth: string, http credentials in "user:pass" format
777 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
778 * - compress: bool, whether to allow gzip/deflate content encoding
780 * - port: int, use another port as specified in the url
781 * - referer: string, the referer to sends
782 * - useragent: string, the user agent to send
783 * (defaults to PECL::HTTP/version (PHP/version)))
784 * - headers: array, list of custom headers as associative array
785 * like array("header" => "value")
786 * - cookies: array, list of cookies as associative array
787 * like array("cookie" => "value")
788 * - cookiestore: string, path to a file where cookies are/will be stored
791 * The optional third parameter will be filled with some additional information
792 * in form af an associative array, if supplied, like the following example:
796 * 'effective_url' => 'http://localhost',
797 * 'response_code' => 403,
798 * 'total_time' => 0.017,
799 * 'namelookup_time' => 0.013,
800 * 'connect_time' => 0.014,
801 * 'pretransfer_time' => 0.014,
802 * 'size_upload' => 0,
803 * 'size_download' => 202,
804 * 'speed_download' => 11882,
805 * 'speed_upload' => 0,
806 * 'header_size' => 145,
807 * 'request_size' => 62,
808 * 'ssl_verifyresult' => 0,
810 * 'content_length_download' => 202,
811 * 'content_length_upload' => 0,
812 * 'starttransfer_time' => 0.017,
813 * 'content_type' => 'text/html; charset=iso-8859-1',
814 * 'redirect_time' => 0,
815 * 'redirect_count' => 0,
817 * 'http_connectcode' => 0,
818 * 'httpauth_avail' => 0,
819 * 'proxyauth_avail' => 0,
824 PHP_FUNCTION(http_get
)
826 char *URL
, *data
= NULL
;
829 zval
*options
= NULL
, *info
= NULL
;
831 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
840 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
841 RETURN_STRINGL(data
, data_len
, 0);
848 /* {{{ proto string http_head(string url[, array options[, array &info]])
850 * Performs an HTTP HEAD request on the suppied url.
851 * Returns the HTTP response as string.
852 * See http_get() for a full list of available options.
854 PHP_FUNCTION(http_head
)
856 char *URL
, *data
= NULL
;
859 zval
*options
= NULL
, *info
= NULL
;
861 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
870 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
871 RETURN_STRINGL(data
, data_len
, 0);
878 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
880 * Performs an HTTP POST request, posting data.
881 * Returns the HTTP response as string.
882 * See http_get() for a full list of available options.
884 PHP_FUNCTION(http_post_data
)
886 char *URL
, *postdata
, *data
= NULL
;
888 int postdata_len
, URL_len
;
889 zval
*options
= NULL
, *info
= NULL
;
891 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
900 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
901 RETURN_STRINGL(data
, data_len
, 0);
908 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
910 * Performs an HTTP POST request, posting www-form-urlencoded array data.
911 * Returns the HTTP response as string.
912 * See http_get() for a full list of available options.
914 PHP_FUNCTION(http_post_array
)
916 char *URL
, *data
= NULL
;
919 zval
*options
= NULL
, *info
= NULL
, *postdata
;
921 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
930 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
931 RETURN_STRINGL(data
, data_len
, 0);
942 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
947 * if (!http_auth_basic('mike', 's3c|r3t')) {
948 * die('<h1>Authorization failed!</h1>');
953 PHP_FUNCTION(http_auth_basic
)
955 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
956 int r_len
, u_len
, p_len
;
958 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
963 realm
= "Restricted";
966 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
967 http_auth_header("Basic", realm
);
971 if (strcasecmp(suser
, user
)) {
972 http_auth_header("Basic", realm
);
976 if (strcmp(spass
, pass
)) {
977 http_auth_header("Basic", realm
);
985 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
990 * function auth_cb($user, $pass)
993 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
994 * if (strlen($realpass = $db->getOne($query)) {
995 * return $pass === $realpass;
1000 * if (!http_auth_basic_cb('auth_cb')) {
1001 * die('<h1>Authorization failed</h1>');
1006 PHP_FUNCTION(http_auth_basic_cb
)
1009 char *realm
= NULL
, *user
, *pass
;
1012 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
1017 realm
= "Restricted";
1020 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
1021 http_auth_header("Basic", realm
);
1025 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
1028 MAKE_STD_ZVAL(zparams
[0]);
1029 MAKE_STD_ZVAL(zparams
[1]);
1030 ZVAL_STRING(zparams
[0], user
, 0);
1031 ZVAL_STRING(zparams
[1], pass
, 0);
1033 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
1034 &retval
, 2, zparams TSRMLS_CC
)) {
1035 result
= Z_LVAL(retval
);
1044 http_auth_header("Basic", realm
);
1047 RETURN_BOOL(result
);
1053 /* {{{ php_http_init_globals(zend_http_globals *) */
1054 static void php_http_init_globals(zend_http_globals
*http_globals
)
1056 http_globals
->etag_started
= 0;
1057 http_globals
->ctype
= NULL
;
1058 http_globals
->etag
= NULL
;
1059 http_globals
->lmod
= 0;
1060 #ifdef HTTP_HAVE_CURL
1061 http_globals
->curlbuf
.body
.data
= NULL
;
1062 http_globals
->curlbuf
.body
.used
= 0;
1063 http_globals
->curlbuf
.body
.free
= 0;
1064 http_globals
->curlbuf
.hdrs
.data
= NULL
;
1065 http_globals
->curlbuf
.hdrs
.used
= 0;
1066 http_globals
->curlbuf
.hdrs
.free
= 0;
1068 http_globals
->allowed_methods
= NULL
;
1073 PHP_INI_MH(update_allowed_methods
)
1075 if (SG(request_info
).request_method
&& new_value_length
&& (!strstr(new_value
, SG(request_info
).request_method
))) {
1076 char *allow_header
= emalloc(new_value_length
+ sizeof("Allow: "));
1077 sprintf(allow_header
, "Allow: %s", new_value
);
1078 http_send_header(allow_header
);
1079 efree(allow_header
);
1080 http_send_status(405);
1083 return OnUpdateString(entry
, new_value
, new_value_length
, mh_arg1
, mh_arg2
, mh_arg3
, stage TSRMLS_CC
);
1087 STD_PHP_INI_ENTRY("http.allowed_methods", "HEAD,GET,POST", PHP_INI_ALL
, update_allowed_methods
, allowed_methods
, zend_http_globals
, http_globals
)
1091 /* {{{ PHP_MINIT_FUNCTION */
1092 PHP_MINIT_FUNCTION(http
)
1094 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
1095 REGISTER_INI_ENTRIES();
1096 #ifdef HTTP_HAVE_CURL
1097 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
1098 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
1099 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
1105 /* {{{ PHP_MSHUTDOWN_FUNCTION */
1106 PHP_MSHUTDOWN_FUNCTION(http
)
1108 UNREGISTER_INI_ENTRIES();
1113 /* {{{ PHP_RINIT_FUNCTION */
1114 PHP_RINIT_FUNCTION(http
)
1116 char *allowed_methods
= INI_STR("http.allowed_methods");
1118 if (SG(request_info
).request_method
&& (am_len
= strlen(allowed_methods
)) && (!strstr(allowed_methods
, SG(request_info
).request_method
))) {
1119 char *allow_header
= emalloc(am_len
+ sizeof("Allow: "));
1120 sprintf(allow_header
, "Allow: %s", allowed_methods
);
1121 http_send_header(allow_header
);
1122 efree(allow_header
);
1123 http_send_status(405);
1129 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1130 PHP_RSHUTDOWN_FUNCTION(http
)
1132 if (HTTP_G(ctype
)) {
1133 efree(HTTP_G(ctype
));
1134 HTTP_G(ctype
) = NULL
;
1137 efree(HTTP_G(etag
));
1138 HTTP_G(etag
) = NULL
;
1140 #ifdef HTTP_HAVE_CURL
1141 if (HTTP_G(curlbuf
).body
.data
) {
1142 efree(HTTP_G(curlbuf
).body
.data
);
1143 HTTP_G(curlbuf
).body
.data
= NULL
;
1145 if (HTTP_G(curlbuf
).hdrs
.data
) {
1146 efree(HTTP_G(curlbuf
).hdrs
.data
);
1147 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
1154 /* {{{ PHP_MINFO_FUNCTION */
1155 PHP_MINFO_FUNCTION(http
)
1157 php_info_print_table_start();
1158 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1159 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
1160 php_info_print_table_row(2, "cURL convenience functions:",
1161 #ifdef HTTP_HAVE_CURL
1167 php_info_print_table_end();
1169 DISPLAY_INI_ENTRIES();
1178 * vim600: noet sw=4 ts=4 fdm=marker
1179 * vim<600: noet sw=4 ts=4