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 +----------------------------------------------------------------------+
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
28 #include "ext/standard/info.h"
29 #include "ext/session/php_session.h"
30 #include "ext/standard/php_string.h"
31 #include "ext/standard/php_smart_str.h"
36 #include "php_http_api.h"
39 # include "ext/standard/php_http.h"
45 # include <winsock2.h>
46 # include <sys/types.h>
49 # include <curl/curl.h>
52 # ifdef ZEND_BEGIN_ARG_INFO
53 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3
, 0)
59 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4
, 0)
66 static unsigned char http_request_info_ref_3
[] = {3, BYREF_NONE
, BYREF_NONE
, BYREF_FORCE
};
67 static unsigned char http_request_info_ref_4
[] = {4, BYREF_NONE
, BYREF_NONE
, BYREF_NONE
, BYREF_FORCE
};
71 #endif /* HTTP_HAVE_CURL */
73 ZEND_DECLARE_MODULE_GLOBALS(http
)
75 #ifdef COMPILE_DL_HTTP
80 /* {{{ http_functions[] */
81 function_entry http_functions
[] = {
82 PHP_FE(http_date
, NULL
)
83 PHP_FE(http_absolute_uri
, NULL
)
84 PHP_FE(http_negotiate_language
, NULL
)
85 PHP_FE(http_negotiate_charset
, NULL
)
86 PHP_FE(http_redirect
, NULL
)
87 PHP_FE(http_send_status
, NULL
)
88 PHP_FE(http_send_last_modified
, NULL
)
89 PHP_FE(http_match_modified
, NULL
)
90 PHP_FE(http_match_etag
, NULL
)
91 PHP_FE(http_cache_last_modified
, NULL
)
92 PHP_FE(http_cache_etag
, NULL
)
93 PHP_FE(http_content_type
, NULL
)
94 PHP_FE(http_content_disposition
, NULL
)
95 PHP_FE(http_send_data
, NULL
)
96 PHP_FE(http_send_file
, NULL
)
97 PHP_FE(http_send_stream
, NULL
)
98 PHP_FE(http_chunked_decode
, NULL
)
99 PHP_FE(http_split_response
, NULL
)
100 PHP_FE(http_parse_headers
, NULL
)
101 PHP_FE(http_get_request_headers
, NULL
)
102 #ifdef HTTP_HAVE_CURL
103 PHP_FE(http_get
, http_request_info_ref_3
)
104 PHP_FE(http_head
, http_request_info_ref_3
)
105 PHP_FE(http_post_data
, http_request_info_ref_4
)
106 PHP_FE(http_post_array
, http_request_info_ref_4
)
108 PHP_FE(http_auth_basic
, NULL
)
109 PHP_FE(http_auth_basic_cb
, NULL
)
110 #ifndef ZEND_ENGINE_2
111 PHP_FE(http_build_query
, NULL
)
113 PHP_FE(ob_httpetaghandler
, NULL
)
118 /* {{{ http_module_entry */
119 zend_module_entry http_module_entry
= {
120 #if ZEND_MODULE_API_NO >= 20010901
121 STANDARD_MODULE_HEADER
,
130 #if ZEND_MODULE_API_NO >= 20010901
131 PHP_EXT_HTTP_VERSION
,
133 STANDARD_MODULE_PROPERTIES
137 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
138 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
140 /* {{{ proto string http_date([int timestamp])
142 * This function returns a valid HTTP date regarding RFC 822/1123
143 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
146 PHP_FUNCTION(http_date
)
150 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
155 t
= (long) time(NULL
);
158 RETURN_STRING(http_date(t
), 0);
162 /* {{{ proto string http_absolute_uri(string url[, string proto])
164 * This function returns an absolute URI constructed from url.
165 * If the url is already abolute but a different proto was supplied,
166 * only the proto part of the URI will be updated. If url has no
167 * path specified, the path of the current REQUEST_URI will be taken.
168 * The host will be taken either from the Host HTTP header of the client
169 * the SERVER_NAME or just localhost if prior are not available.
173 * url = "page.php" => http://www.example.com/current/path/page.php
174 * url = "/page.php" => http://www.example.com/page.php
175 * url = "/page.php", proto = "https" => https://www.example.com/page.php
179 PHP_FUNCTION(http_absolute_uri
)
181 char *url
= NULL
, *proto
= NULL
;
182 int url_len
= 0, proto_len
= 0;
184 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|s", &url
, &url_len
, &proto
, &proto_len
) != SUCCESS
) {
188 RETURN_STRING(http_absolute_uri(url
, proto
), 0);
192 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
194 * This function negotiates the clients preferred language based on its
195 * Accept-Language HTTP header. It returns the negotiated language or
196 * the default language if none match.
198 * The qualifier is recognized and languages without qualifier are rated highest.
200 * The supported parameter is expected to be an array having
201 * the supported languages as array values.
215 * include './langs/'. http_negotiate_language($langs) .'.php';
220 PHP_FUNCTION(http_negotiate_language
)
226 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
234 RETURN_STRING(http_negotiate_language(supported
, def
), 0);
238 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
240 * This function negotiates the clients preferred charset based on its
241 * Accept-Charset HTTP header. It returns the negotiated charset or
242 * the default charset if none match.
244 * The qualifier is recognized and charset without qualifier are rated highest.
246 * The supported parameter is expected to be an array having
247 * the supported charsets as array values.
253 * 'iso-8859-1', // default
258 * $pref = http_negotiate_charset($charsets);
259 * if (!strcmp($pref, 'iso-8859-1')) {
260 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
261 * iconv_set_encoding('output_encoding', $pref);
262 * ob_start('ob_iconv_handler');
267 PHP_FUNCTION(http_negotiate_charset
)
273 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a|s", &supported
, &def
, &def_len
) != SUCCESS
) {
281 RETURN_STRING(http_negotiate_charset(supported
, def
), 0);
285 /* {{{ proto bool http_send_status(int status)
287 * Send HTTP status code.
290 PHP_FUNCTION(http_send_status
)
294 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &status
) != SUCCESS
) {
297 if (status
< 100 || status
> 510) {
298 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Invalid HTTP status code (100-510): %d", status
);
302 RETURN_SUCCESS(http_send_status(status
));
306 /* {{{ proto bool http_send_last_modified([int timestamp])
308 * This converts the given timestamp to a valid HTTP date and
309 * sends it as "Last-Modified" HTTP header. If timestamp is
310 * omitted, current time is sent.
313 PHP_FUNCTION(http_send_last_modified
)
317 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
322 t
= (long) time(NULL
);
325 RETURN_SUCCESS(http_send_last_modified(t
));
329 /* {{{ proto bool http_match_modified([int timestamp])
331 * Matches the given timestamp against the clients "If-Modified-Since" resp.
332 * "If-Unmodified-Since" HTTP headers.
335 PHP_FUNCTION(http_match_modified
)
339 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &t
) != SUCCESS
) {
343 // current time if not supplied (senseless though)
345 t
= (long) time(NULL
);
348 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t
) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t
));
352 /* {{{ proto bool http_match_etag(string etag)
354 * This matches the given ETag against the clients
355 * "If-Match" resp. "If-None-Match" HTTP headers.
358 PHP_FUNCTION(http_match_etag
)
363 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &etag
, &etag_len
) != SUCCESS
) {
367 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag
) || http_etag_match("HTTP_IF_MATCH", etag
));
371 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
373 * If timestamp_or_exires is greater than 0, it is handled as timestamp
374 * and will be sent as date of last modification. If it is 0 or omitted,
375 * the current time will be sent as Last-Modified date. If it's negative,
376 * it is handled as expiration time in seconds, which means that if the
377 * requested last modification date is not between the calculated timespan,
378 * the Last-Modified header is updated and the actual body will be sent.
381 PHP_FUNCTION(http_cache_last_modified
)
383 long last_modified
= 0, send_modified
= 0, t
;
386 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &last_modified
) != SUCCESS
) {
390 t
= (long) time(NULL
);
393 if (!last_modified
) {
394 /* does the client have? (att: caching "forever") */
395 if (zlm
= http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
396 last_modified
= send_modified
= http_parse_date(Z_STRVAL_P(zlm
));
397 /* send current time */
401 /* negative value is supposed to be expiration time */
402 } else if (last_modified
< 0) {
405 /* send supplied time explicitly */
407 send_modified
= last_modified
;
410 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
412 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
413 if (SUCCESS
== http_send_status(304)) {
416 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
420 RETURN_SUCCESS(http_send_last_modified(send_modified
));
424 /* {{{ proto bool http_cache_etag([string etag])
426 * This function attempts to cache the HTTP body based on an ETag,
427 * either supplied or generated through calculation of the MD5
428 * checksum of the output (uses output buffering).
430 * If clients "If-None-Match" header matches the supplied/calculated
431 * ETag, the body is considered cached on the clients side and
432 * a "304 Not Modified" status code is issued.
435 PHP_FUNCTION(http_cache_etag
)
440 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &etag
, &etag_len
) != SUCCESS
) {
444 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
447 http_send_etag(etag
, etag_len
);
448 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
449 if (SUCCESS
== http_send_status(304)) {
452 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
458 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
459 if (!HTTP_G(etag_started
)) {
460 RETURN_BOOL(HTTP_G(etag_started
) = (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)));
466 /* {{{ proto string ob_httpetaghandler(string data, int mode)
468 * For use with ob_start().
469 * Note that this has to be started as first output buffer.
470 * WARNING: Don't use with http_send_*().
472 PHP_FUNCTION(ob_httpetaghandler
)
478 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sl", &data
, &data_len
, &mode
)) {
482 if (mode
& PHP_OUTPUT_HANDLER_START
) {
483 if (HTTP_G(etag_started
)) {
484 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "ob_httpetaghandler can only be used once");
485 RETURN_STRINGL(data
, data_len
, 1);
487 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
488 HTTP_G(etag_started
) = 1;
491 if (OG(ob_nesting_level
) > 1) {
492 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "ob_httpetaghandler must be started prior to other output buffers");
493 RETURN_STRINGL(data
, data_len
, 1);
496 Z_TYPE_P(return_value
) = IS_STRING
;
497 http_ob_etaghandler(data
, data_len
, &Z_STRVAL_P(return_value
), &Z_STRLEN_P(return_value
), mode
);
501 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
503 * Redirect to a given url.
504 * The supplied url will be expanded with http_absolute_uri(), the params array will
505 * be treated with http_build_query() and the session identification will be appended
506 * if session is true.
508 * Depending on permanent the redirection will be issued with a permanent
509 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
512 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
513 * if the client doesn't redirect immediatly.
515 PHP_FUNCTION(http_redirect
)
518 zend_bool session
= 0, permanent
= 0;
520 smart_str qstr
= {0};
521 char *url
, *URI
, LOC
[HTTP_URI_MAXLEN
+ 9], RED
[HTTP_URI_MAXLEN
* 2 + 34];
523 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|sa!/bb", &url
, &url_len
, ¶ms
, &session
, &permanent
) != SUCCESS
) {
527 /* append session info */
528 if (session
&& (PS(session_status
) == php_session_active
)) {
530 MAKE_STD_ZVAL(params
);
533 if (add_assoc_string(params
, PS(session_name
), PS(id
), 1) != SUCCESS
) {
534 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not append session information");
538 /* treat params array with http_build_query() */
540 if (php_url_encode_hash_ex(Z_ARRVAL_P(params
), &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
544 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode query parameters");
550 URI
= http_absolute_uri(url
, NULL
);
552 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s?%s", URI
, qstr
.c
);
553 sprintf(RED
, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI
, qstr
.c
, URI
, qstr
.c
);
556 snprintf(LOC
, HTTP_URI_MAXLEN
+ strlen("Location: "), "Location: %s", URI
);
557 sprintf(RED
, "Redirecting to <a href=\"%s\">%s</a>.\n", URI
, URI
);
561 if ((SUCCESS
== http_send_header(LOC
)) && (SUCCESS
== http_send_status((permanent
? 301 : 302)))) {
562 php_body_write(RED
, strlen(RED
) TSRMLS_CC
);
569 /* {{{ proto bool http_send_data(string data)
571 * Sends raw data with support for (multiple) range requests.
574 PHP_FUNCTION(http_send_data
)
578 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zdata
) != SUCCESS
) {
582 convert_to_string_ex(&zdata
);
583 http_send_header("Accept-Ranges: bytes");
584 RETURN_SUCCESS(http_send_data(zdata
));
588 /* {{{ proto bool http_send_file(string file)
590 * Sends a file with support for (multiple) range requests.
593 PHP_FUNCTION(http_send_file
)
597 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zfile
) != SUCCESS
) {
601 convert_to_string_ex(&zfile
);
602 http_send_header("Accept-Ranges: bytes");
603 RETURN_SUCCESS(http_send_file(zfile
));
607 /* {{{ proto bool http_send_stream(resource stream)
609 * Sends an already opened stream with support for (multiple) range requests.
612 PHP_FUNCTION(http_send_stream
)
617 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "r", &zstream
) != SUCCESS
) {
621 php_stream_from_zval(file
, &zstream
);
622 http_send_header("Accept-Ranges: bytes");
623 RETURN_SUCCESS(http_send_stream(file
));
627 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
629 * Sets the content type.
632 PHP_FUNCTION(http_content_type
)
634 char *ct
, *content_type
;
637 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &ct
, &ct_len
) != SUCCESS
) {
642 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
645 /* remember for multiple ranges */
647 efree(HTTP_G(ctype
));
649 HTTP_G(ctype
) = estrndup(ct
, ct_len
);
651 content_type
= (char *) emalloc(strlen("Content-Type: ") + ct_len
+ 1);
652 sprintf(content_type
, "Content-Type: %s", ct
);
654 RETVAL_BOOL(SUCCESS
== http_send_header(content_type
));
659 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
661 * Set the Content Disposition. The Content-Disposition header is very useful
662 * if the data actually sent came from a file or something similar, that should
663 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
666 PHP_FUNCTION(http_content_disposition
)
668 char *filename
, *header
;
670 zend_bool send_inline
= 0;
672 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|b", &filename
, &f_len
, &send_inline
) != SUCCESS
) {
677 header
= (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len
+ 1);
678 sprintf(header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
680 header
= (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len
+ 1);
681 sprintf(header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
684 RETVAL_BOOL(SUCCESS
== http_send_header(header
));
689 /* {{{ proto string http_chunked_decode(string encoded)
691 * This function decodes a string that was HTTP-chunked encoded.
692 * Returns false on failure.
694 PHP_FUNCTION(http_chunked_decode
)
696 char *encoded
= NULL
, *decoded
= NULL
;
697 int encoded_len
= 0, decoded_len
= 0;
699 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &encoded
, &encoded_len
) != SUCCESS
) {
703 if (SUCCESS
== http_chunked_decode(encoded
, encoded_len
, &decoded
, &decoded_len
)) {
704 RETURN_STRINGL(decoded
, decoded_len
, 0);
711 /* {{{ proto array http_split_response(string http_response)
713 * This function splits an HTTP response into an array with headers and the
714 * content body. The returned array may look simliar to the following example:
720 * 'Status' => '200 Ok',
721 * 'Content-Type' => 'text/plain',
722 * 'Content-Language' => 'en-US'
724 * 1 => "Hello World!"
729 PHP_FUNCTION(http_split_response
)
731 zval
*zresponse
, *zbody
, *zheaders
;
733 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zresponse
) != SUCCESS
) {
737 convert_to_string_ex(&zresponse
);
739 MAKE_STD_ZVAL(zbody
);
740 MAKE_STD_ZVAL(zheaders
);
741 array_init(zheaders
);
743 if (SUCCESS
!= http_split_response(zresponse
, zheaders
, zbody
)) {
744 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP response");
748 array_init(return_value
);
749 add_index_zval(return_value
, 0, zheaders
);
750 add_index_zval(return_value
, 1, zbody
);
754 /* {{{ proto array http_parse_headers(string header)
757 PHP_FUNCTION(http_parse_headers
)
762 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &header
, &header_len
)) {
766 array_init(return_value
);
768 if (rnrn
= strstr(header
, HTTP_CRLF HTTP_CRLF
)) {
769 header_len
= rnrn
- header
+ 2;
771 if (SUCCESS
!= http_parse_headers(header
, header_len
, return_value
)) {
772 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse HTTP header");
773 zval_dtor(return_value
);
779 /* {{{ proto array http_get_request_headers(void)
782 PHP_FUNCTION(http_get_request_headers
)
784 if (ZEND_NUM_ARGS()) {
788 array_init(return_value
);
789 http_get_request_headers(return_value
);
794 #ifdef HTTP_HAVE_CURL
796 /* {{{ proto string http_get(string url[, array options[, array &info]])
798 * Performs an HTTP GET request on the supplied url.
800 * The second parameter is expected to be an associative
801 * array where the following keys will be recognized:
803 * - redirect: int, whether and how many redirects to follow
804 * - unrestrictedauth: bool, whether to continue sending credentials on
805 * redirects to a different host
806 * - proxyhost: string, proxy host in "host[:port]" format
807 * - proxyport: int, use another proxy port as specified in proxyhost
808 * - proxyauth: string, proxy credentials in "user:pass" format
809 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
810 * - httpauth: string, http credentials in "user:pass" format
811 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
812 * - compress: bool, whether to allow gzip/deflate content encoding
814 * - port: int, use another port as specified in the url
815 * - referer: string, the referer to sends
816 * - useragent: string, the user agent to send
817 * (defaults to PECL::HTTP/version (PHP/version)))
818 * - headers: array, list of custom headers as associative array
819 * like array("header" => "value")
820 * - cookies: array, list of cookies as associative array
821 * like array("cookie" => "value")
822 * - cookiestore: string, path to a file where cookies are/will be stored
825 * The optional third parameter will be filled with some additional information
826 * in form af an associative array, if supplied, like the following example:
830 * 'effective_url' => 'http://localhost',
831 * 'response_code' => 403,
832 * 'total_time' => 0.017,
833 * 'namelookup_time' => 0.013,
834 * 'connect_time' => 0.014,
835 * 'pretransfer_time' => 0.014,
836 * 'size_upload' => 0,
837 * 'size_download' => 202,
838 * 'speed_download' => 11882,
839 * 'speed_upload' => 0,
840 * 'header_size' => 145,
841 * 'request_size' => 62,
842 * 'ssl_verifyresult' => 0,
844 * 'content_length_download' => 202,
845 * 'content_length_upload' => 0,
846 * 'starttransfer_time' => 0.017,
847 * 'content_type' => 'text/html; charset=iso-8859-1',
848 * 'redirect_time' => 0,
849 * 'redirect_count' => 0,
851 * 'http_connectcode' => 0,
852 * 'httpauth_avail' => 0,
853 * 'proxyauth_avail' => 0,
858 PHP_FUNCTION(http_get
)
860 char *URL
, *data
= NULL
;
863 zval
*options
= NULL
, *info
= NULL
;
865 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
874 if (SUCCESS
== http_get(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
875 RETURN_STRINGL(data
, data_len
, 0);
882 /* {{{ proto string http_head(string url[, array options[, array &info]])
884 * Performs an HTTP HEAD request on the suppied url.
885 * Returns the HTTP response as string.
886 * See http_get() for a full list of available options.
888 PHP_FUNCTION(http_head
)
890 char *URL
, *data
= NULL
;
893 zval
*options
= NULL
, *info
= NULL
;
895 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|a/!z", &URL
, &URL_len
, &options
, &info
) != SUCCESS
) {
904 if (SUCCESS
== http_head(URL
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
905 RETURN_STRINGL(data
, data_len
, 0);
912 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
914 * Performs an HTTP POST request, posting data.
915 * Returns the HTTP response as string.
916 * See http_get() for a full list of available options.
918 PHP_FUNCTION(http_post_data
)
920 char *URL
, *postdata
, *data
= NULL
;
922 int postdata_len
, URL_len
;
923 zval
*options
= NULL
, *info
= NULL
;
925 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|a/!z", &URL
, &URL_len
, &postdata
, &postdata_len
, &options
, &info
) != SUCCESS
) {
934 if (SUCCESS
== http_post_data(URL
, postdata
, (size_t) postdata_len
, HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
935 RETURN_STRINGL(data
, data_len
, 0);
942 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
944 * Performs an HTTP POST request, posting www-form-urlencoded array data.
945 * Returns the HTTP response as string.
946 * See http_get() for a full list of available options.
948 PHP_FUNCTION(http_post_array
)
950 char *URL
, *data
= NULL
;
953 zval
*options
= NULL
, *info
= NULL
, *postdata
;
955 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sa|a/!z", &URL
, &URL_len
, &postdata
, &options
, &info
) != SUCCESS
) {
964 if (SUCCESS
== http_post_array(URL
, Z_ARRVAL_P(postdata
), HASH_ORNULL(options
), HASH_ORNULL(info
), &data
, &data_len
)) {
965 RETURN_STRINGL(data
, data_len
, 0);
976 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
981 * if (!http_auth_basic('mike', 's3c|r3t')) {
982 * die('<h1>Authorization failed!</h1>');
987 PHP_FUNCTION(http_auth_basic
)
989 char *realm
= NULL
, *user
, *pass
, *suser
, *spass
;
990 int r_len
, u_len
, p_len
;
992 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss|s", &user
, &u_len
, &pass
, &p_len
, &realm
, &r_len
) != SUCCESS
) {
997 realm
= "Restricted";
1000 if (SUCCESS
!= http_auth_credentials(&suser
, &spass
)) {
1001 http_auth_header("Basic", realm
);
1005 if (strcasecmp(suser
, user
)) {
1006 http_auth_header("Basic", realm
);
1010 if (strcmp(spass
, pass
)) {
1011 http_auth_header("Basic", realm
);
1019 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1024 * function auth_cb($user, $pass)
1027 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1028 * if (strlen($realpass = $db->getOne($query)) {
1029 * return $pass === $realpass;
1034 * if (!http_auth_basic_cb('auth_cb')) {
1035 * die('<h1>Authorization failed</h1>');
1040 PHP_FUNCTION(http_auth_basic_cb
)
1043 char *realm
= NULL
, *user
, *pass
;
1046 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z|s", &cb
, &realm
, &r_len
) != SUCCESS
) {
1051 realm
= "Restricted";
1054 if (SUCCESS
!= http_auth_credentials(&user
, &pass
)) {
1055 http_auth_header("Basic", realm
);
1059 zval
*zparams
[2] = {NULL
, NULL
}, retval
;
1062 MAKE_STD_ZVAL(zparams
[0]);
1063 MAKE_STD_ZVAL(zparams
[1]);
1064 ZVAL_STRING(zparams
[0], user
, 0);
1065 ZVAL_STRING(zparams
[1], pass
, 0);
1067 if (SUCCESS
== call_user_function(EG(function_table
), NULL
, cb
,
1068 &retval
, 2, zparams TSRMLS_CC
)) {
1069 result
= Z_LVAL(retval
);
1078 http_auth_header("Basic", realm
);
1081 RETURN_BOOL(result
);
1087 /* {{{ php_http_init_globals(zend_http_globals *) */
1088 static void php_http_init_globals(zend_http_globals
*http_globals
)
1090 http_globals
->etag_started
= 0;
1091 http_globals
->ctype
= NULL
;
1092 http_globals
->etag
= NULL
;
1093 http_globals
->lmod
= 0;
1094 #ifdef HTTP_HAVE_CURL
1095 http_globals
->curlbuf
.body
.data
= NULL
;
1096 http_globals
->curlbuf
.body
.used
= 0;
1097 http_globals
->curlbuf
.body
.free
= 0;
1098 http_globals
->curlbuf
.hdrs
.data
= NULL
;
1099 http_globals
->curlbuf
.hdrs
.used
= 0;
1100 http_globals
->curlbuf
.hdrs
.free
= 0;
1102 http_globals
->allowed_methods
= NULL
;
1106 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
1107 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
1108 static inline void _http_check_allowed_methods(char *methods
, int length TSRMLS_DC
)
1110 if (length
&& SG(request_info
).request_method
&& (!strstr(methods
, SG(request_info
).request_method
))) {
1111 char *allow_header
= emalloc(length
+ sizeof("Allow: "));
1112 sprintf(allow_header
, "Allow: %s", methods
);
1113 http_send_header(allow_header
);
1114 efree(allow_header
);
1115 http_send_status(405);
1122 PHP_INI_MH(update_allowed_methods
)
1124 http_check_allowed_methods(new_value
, new_value_length
);
1125 return OnUpdateString(entry
, new_value
, new_value_length
, mh_arg1
, mh_arg2
, mh_arg3
, stage TSRMLS_CC
);
1129 STD_PHP_INI_ENTRY("http.allowed_methods", "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT", PHP_INI_ALL
, update_allowed_methods
, allowed_methods
, zend_http_globals
, http_globals
)
1133 /* {{{ PHP_MINIT_FUNCTION */
1134 PHP_MINIT_FUNCTION(http
)
1136 ZEND_INIT_MODULE_GLOBALS(http
, php_http_init_globals
, NULL
);
1137 REGISTER_INI_ENTRIES();
1138 #ifdef HTTP_HAVE_CURL
1139 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
| CONST_PERSISTENT
);
1140 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
| CONST_PERSISTENT
);
1141 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
| CONST_PERSISTENT
);
1147 /* {{{ PHP_MSHUTDOWN_FUNCTION */
1148 PHP_MSHUTDOWN_FUNCTION(http
)
1150 UNREGISTER_INI_ENTRIES();
1155 /* {{{ PHP_RINIT_FUNCTION */
1156 PHP_RINIT_FUNCTION(http
)
1158 char *allowed_methods
= INI_STR("http.allowed_methods");
1159 http_check_allowed_methods(allowed_methods
, strlen(allowed_methods
));
1164 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1165 PHP_RSHUTDOWN_FUNCTION(http
)
1167 HTTP_G(etag_started
) = 0;
1171 efree(HTTP_G(etag
));
1172 HTTP_G(etag
) = NULL
;
1175 if (HTTP_G(ctype
)) {
1176 efree(HTTP_G(ctype
));
1177 HTTP_G(ctype
) = NULL
;
1179 #ifdef HTTP_HAVE_CURL
1180 if (HTTP_G(curlbuf
).body
.data
) {
1181 efree(HTTP_G(curlbuf
).body
.data
);
1182 HTTP_G(curlbuf
).body
.data
= NULL
;
1184 if (HTTP_G(curlbuf
).hdrs
.data
) {
1185 efree(HTTP_G(curlbuf
).hdrs
.data
);
1186 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
1193 /* {{{ PHP_MINFO_FUNCTION */
1194 PHP_MINFO_FUNCTION(http
)
1196 php_info_print_table_start();
1197 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1198 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION
);
1199 php_info_print_table_row(2, "cURL convenience functions:",
1200 #ifdef HTTP_HAVE_CURL
1206 php_info_print_table_end();
1208 DISPLAY_INI_ENTRIES();
1217 * vim600: noet sw=4 ts=4 fdm=marker
1218 * vim<600: noet sw=4 ts=4