56ae91d1024f3a3c3aa5292df29393aa85b32bd3
[m6w6/ext-http] / http.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #if defined(HAVE_CURL) && HAVE_CURL
25 # ifdef PHP_WIN32
26 # include <winsock2.h>
27 # include <sys/types.h>
28 # endif
29 #include <curl/curl.h>
30 #endif
31
32 #include "php.h"
33 #include "snprintf.h"
34 #include "ext/standard/info.h"
35 #include "ext/session/php_session.h"
36 #include "ext/standard/php_string.h"
37 #include "ext/standard/php_smart_str.h"
38
39 #include "php_http.h"
40 #include "php_http_api.h"
41
42 ZEND_DECLARE_MODULE_GLOBALS(http)
43
44 #ifdef COMPILE_DL_HTTP
45 ZEND_GET_MODULE(http)
46 #endif
47
48 /* {{{ http_functions[] */
49 function_entry http_functions[] = {
50 PHP_FE(http_date, NULL)
51 PHP_FE(http_absolute_uri, NULL)
52 PHP_FE(http_negotiate_language, NULL)
53 PHP_FE(http_negotiate_charset, NULL)
54 PHP_FE(http_redirect, NULL)
55 PHP_FE(http_send_status, NULL)
56 PHP_FE(http_send_last_modified, NULL)
57 PHP_FE(http_match_modified, NULL)
58 PHP_FE(http_match_etag, NULL)
59 PHP_FE(http_cache_last_modified, NULL)
60 PHP_FE(http_cache_etag, NULL)
61 PHP_FE(http_content_type, NULL)
62 PHP_FE(http_content_disposition, NULL)
63 PHP_FE(http_send_data, NULL)
64 PHP_FE(http_send_file, NULL)
65 PHP_FE(http_send_stream, NULL)
66 PHP_FE(http_chunked_decode, NULL)
67 PHP_FE(http_split_response, NULL)
68 #if defined(HAVE_CURL) && HAVE_CURL
69 PHP_FE(http_get, NULL)
70 PHP_FE(http_head, NULL)
71 PHP_FE(http_post_data, NULL)
72 PHP_FE(http_post_array, NULL)
73 #endif
74 PHP_FE(http_auth_basic, NULL)
75 PHP_FE(http_auth_basic_cb, NULL)
76 {NULL, NULL, NULL}
77 };
78 /* }}} */
79
80 /* {{{ http_module_entry */
81 zend_module_entry http_module_entry = {
82 #if ZEND_MODULE_API_NO >= 20010901
83 STANDARD_MODULE_HEADER,
84 #endif
85 "http",
86 http_functions,
87 PHP_MINIT(http),
88 NULL,
89 NULL,
90 PHP_RSHUTDOWN(http),
91 PHP_MINFO(http),
92 #if ZEND_MODULE_API_NO >= 20010901
93 PHP_EXT_HTTP_VERSION,
94 #endif
95 STANDARD_MODULE_PROPERTIES
96 };
97 /* }}} */
98
99 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
100 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
101
102 /* {{{ proto string http_date([int timestamp])
103 *
104 * This function returns a valid HTTP date regarding RFC 822/1123
105 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
106 *
107 */
108 PHP_FUNCTION(http_date)
109 {
110 long t = -1;
111
112 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
113 RETURN_FALSE;
114 }
115
116 if (t == -1) {
117 t = (long) time(NULL);
118 }
119
120 RETURN_STRING(http_date(t), 0);
121 }
122 /* }}} */
123
124 /* {{{ proto string http_absolute_uri(string url[, string proto])
125 *
126 * This function returns an absolute URI constructed from url.
127 * If the url is already abolute but a different proto was supplied,
128 * only the proto part of the URI will be updated. If url has no
129 * path specified, the path of the current REQUEST_URI will be taken.
130 * The host will be taken either from the Host HTTP header of the client
131 * the SERVER_NAME or just localhost if prior are not available.
132 *
133 * Some examples:
134 * <pre>
135 * url = "page.php" => http://www.example.com/current/path/page.php
136 * url = "/page.php" => http://www.example.com/page.php
137 * url = "/page.php", proto = "https" => https://www.example.com/page.php
138 * </pre>
139 *
140 */
141 PHP_FUNCTION(http_absolute_uri)
142 {
143 char *url = NULL, *proto = NULL;
144 int url_len = 0, proto_len = 0;
145
146 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
147 RETURN_FALSE;
148 }
149
150 RETURN_STRING(http_absolute_uri(url, proto), 0);
151 }
152 /* }}} */
153
154 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
155 *
156 * This function negotiates the clients preferred language based on its
157 * Accept-Language HTTP header. It returns the negotiated language or
158 * the default language if none match.
159 *
160 * The qualifier is recognized and languages without qualifier are rated highest.
161 *
162 * The supported parameter is expected to be an array having
163 * the supported languages as array values.
164 *
165 * Example:
166 * <pre>
167 * <?php
168 * $langs = array(
169 * 'en-US',// default
170 * 'fr',
171 * 'fr-FR',
172 * 'de',
173 * 'de-DE',
174 * 'de-AT',
175 * 'de-CH',
176 * );
177 * include './langs/'. http_negotiate_language($langs) .'.php';
178 * ?>
179 * </pre>
180 *
181 */
182 PHP_FUNCTION(http_negotiate_language)
183 {
184 zval *supported;
185 char *def = NULL;
186 int def_len = 0;
187
188 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
189 RETURN_FALSE;
190 }
191
192 if (!def) {
193 def = "en-US";
194 }
195
196 RETURN_STRING(http_negotiate_language(supported, def), 0);
197 }
198 /* }}} */
199
200 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
201 *
202 * This function negotiates the clients preferred charset based on its
203 * Accept-Charset HTTP header. It returns the negotiated charset or
204 * the default charset if none match.
205 *
206 * The qualifier is recognized and charset without qualifier are rated highest.
207 *
208 * The supported parameter is expected to be an array having
209 * the supported charsets as array values.
210 *
211 * Example:
212 * <pre>
213 * <?php
214 * $charsets = array(
215 * 'iso-8859-1', // default
216 * 'iso-8859-2',
217 * 'iso-8859-15',
218 * 'utf-8'
219 * );
220 * $pref = http_negotiate_charset($charsets);
221 * if (!strcmp($pref, 'iso-8859-1')) {
222 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
223 * iconv_set_encoding('output_encoding', $pref);
224 * ob_start('ob_iconv_handler');
225 * }
226 * ?>
227 * </pre>
228 */
229 PHP_FUNCTION(http_negotiate_charset)
230 {
231 zval *supported;
232 char *def = NULL;
233 int def_len = 0;
234
235 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
236 RETURN_FALSE;
237 }
238
239 if (!def) {
240 def = "iso-8859-1";
241 }
242
243 RETURN_STRING(http_negotiate_charset(supported, def), 0);
244 }
245 /* }}} */
246
247 /* {{{ proto bool http_send_status(int status)
248 *
249 * Send HTTP status code.
250 *
251 */
252 PHP_FUNCTION(http_send_status)
253 {
254 int status = 0;
255
256 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
257 RETURN_FALSE;
258 }
259 if (status < 100 || status > 510) {
260 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
261 RETURN_FALSE;
262 }
263
264 RETURN_SUCCESS(http_send_status(status));
265 }
266 /* }}} */
267
268 /* {{{ proto bool http_send_last_modified([int timestamp])
269 *
270 * This converts the given timestamp to a valid HTTP date and
271 * sends it as "Last-Modified" HTTP header. If timestamp is
272 * omitted, current time is sent.
273 *
274 */
275 PHP_FUNCTION(http_send_last_modified)
276 {
277 long t = -1;
278
279 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
280 RETURN_FALSE;
281 }
282
283 if (t == -1) {
284 t = (long) time(NULL);
285 }
286
287 RETURN_SUCCESS(http_send_last_modified(t));
288 }
289 /* }}} */
290
291 /* {{{ proto bool http_match_modified([int timestamp])
292 *
293 * Matches the given timestamp against the clients "If-Modified-Since" resp.
294 * "If-Unmodified-Since" HTTP headers.
295 *
296 */
297 PHP_FUNCTION(http_match_modified)
298 {
299 long t = -1;
300
301 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
302 RETURN_FALSE;
303 }
304
305 // current time if not supplied (senseless though)
306 if (t == -1) {
307 t = (long) time(NULL);
308 }
309
310 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
311 }
312 /* }}} */
313
314 /* {{{ proto bool http_match_etag(string etag)
315 *
316 * This matches the given ETag against the clients
317 * "If-Match" resp. "If-None-Match" HTTP headers.
318 *
319 */
320 PHP_FUNCTION(http_match_etag)
321 {
322 int etag_len;
323 char *etag;
324
325 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
326 RETURN_FALSE;
327 }
328
329 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
330 }
331 /* }}} */
332
333 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
334 *
335 * If timestamp_or_exires is greater than 0, it is handled as timestamp
336 * and will be sent as date of last modification. If it is 0 or omitted,
337 * the current time will be sent as Last-Modified date. If it's negative,
338 * it is handled as expiration time in seconds, which means that if the
339 * requested last modification date is not between the calculated timespan,
340 * the Last-Modified header is updated and the actual body will be sent.
341 *
342 */
343 PHP_FUNCTION(http_cache_last_modified)
344 {
345 long last_modified = 0, send_modified = 0, t;
346 zval *zlm;
347
348 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
349 RETURN_FALSE;
350 }
351
352 t = (long) time(NULL);
353
354 /* 0 or omitted */
355 if (!last_modified) {
356 /* does the client have? (att: caching "forever") */
357 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
358 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
359 /* send current time */
360 } else {
361 send_modified = t;
362 }
363 /* negative value is supposed to be expiration time */
364 } else if (last_modified < 0) {
365 last_modified += t;
366 send_modified = t;
367 /* send supplied time explicitly */
368 } else {
369 send_modified = last_modified;
370 }
371
372 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
373
374 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
375 if (SUCCESS == http_send_status(304)) {
376 zend_bailout();
377 } else {
378 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
379 RETURN_FALSE;
380 }
381 }
382 RETURN_SUCCESS(http_send_last_modified(send_modified));
383 }
384 /* }}} */
385
386 /* {{{ proto bool http_cache_etag([string etag])
387 *
388 * This function attempts to cache the HTTP body based on an ETag,
389 * either supplied or generated through calculation of the MD5
390 * checksum of the output (uses output buffering).
391 *
392 * If clients "If-None-Match" header matches the supplied/calculated
393 * ETag, the body is considered cached on the clients side and
394 * a "304 Not Modified" status code is issued.
395 *
396 */
397 PHP_FUNCTION(http_cache_etag)
398 {
399 char *etag;
400 int etag_len = 0;
401
402 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
403 RETURN_FALSE;
404 }
405
406 php_end_ob_buffers(0 TSRMLS_CC);
407 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
408
409 /* if no etag is given and we didn't already
410 * start ob_etaghandler -- start it
411 */
412 if (!HTTP_G(etag_started) && !etag_len) {
413 php_ob_set_internal_handler(_http_ob_etaghandler, (uint) 4096, "etag output handler", 0 TSRMLS_CC);
414 HTTP_G(etag_started) = 1;
415 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint) 4096, 0 TSRMLS_CC));
416 }
417
418 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
419 if (SUCCESS == http_send_status(304)) {
420 zend_bailout();
421 } else {
422 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
423 RETURN_FALSE;
424 }
425 }
426
427 RETURN_SUCCESS(http_send_etag(etag, etag_len));
428 }
429 /* }}} */
430
431 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
432 *
433 * Redirect to a given url.
434 * The supplied url will be expanded with http_absolute_uri(), the params array will
435 * be treated with http_build_query() and the session identification will be appended
436 * if session is true.
437 *
438 * Depending on permanent the redirection will be issued with a permanent
439 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
440 * status code.
441 *
442 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
443 * if the client doesn't redirect immediatly.
444 */
445 PHP_FUNCTION(http_redirect)
446 {
447 int url_len;
448 zend_bool session = 0, permanent = 0;
449 zval *params = NULL;
450 smart_str qstr = {0};
451 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
452
453 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
454 RETURN_FALSE;
455 }
456
457 /* append session info */
458 if (session && (PS(session_status) == php_session_active)) {
459 if (!params) {
460 MAKE_STD_ZVAL(params);
461 array_init(params);
462 }
463 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
464 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
465 }
466 }
467
468 /* treat params array with http_build_query() */
469 if (params) {
470 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
471 if (qstr.c) {
472 efree(qstr.c);
473 }
474 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
475 RETURN_FALSE;
476 }
477 smart_str_0(&qstr);
478 }
479
480 URI = http_absolute_uri(url, NULL);
481 if (qstr.c) {
482 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c);
483 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, qstr.c, URI, qstr.c);
484 efree(qstr.c);
485 } else {
486 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
487 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
488 }
489 efree(URI);
490
491 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
492 php_body_write(RED, strlen(RED) TSRMLS_CC);
493 RETURN_TRUE;
494 }
495 RETURN_FALSE;
496 }
497 /* }}} */
498
499 /* {{{ proto bool http_send_data(string data)
500 *
501 * Sends raw data with support for (multiple) range requests.
502 *
503 */
504 PHP_FUNCTION(http_send_data)
505 {
506 zval *zdata;
507
508 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
509 RETURN_FALSE;
510 }
511
512 convert_to_string_ex(&zdata);
513 http_send_header("Accept-Ranges: bytes");
514 RETURN_SUCCESS(http_send_data(zdata));
515 }
516 /* }}} */
517
518 /* {{{ proto bool http_send_file(string file)
519 *
520 * Sends a file with support for (multiple) range requests.
521 *
522 */
523 PHP_FUNCTION(http_send_file)
524 {
525 zval *zfile;
526
527 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
528 RETURN_FALSE;
529 }
530
531 convert_to_string_ex(&zfile);
532 http_send_header("Accept-Ranges: bytes");
533 RETURN_SUCCESS(http_send_file(zfile));
534 }
535 /* }}} */
536
537 /* {{{ proto bool http_send_stream(resource stream)
538 *
539 * Sends an already opened stream with support for (multiple) range requests.
540 *
541 */
542 PHP_FUNCTION(http_send_stream)
543 {
544 zval *zstream;
545 php_stream *file;
546
547 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
548 RETURN_FALSE;
549 }
550
551 php_stream_from_zval(file, &zstream);
552 http_send_header("Accept-Ranges: bytes");
553 RETURN_SUCCESS(http_send_stream(file));
554 }
555 /* }}} */
556
557 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
558 *
559 * Sets the content type.
560 *
561 */
562 PHP_FUNCTION(http_content_type)
563 {
564 char *ct, *content_type;
565 int ct_len = 0;
566
567 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
568 RETURN_FALSE;
569 }
570
571 if (!ct_len) {
572 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
573 }
574
575 /* remember for multiple ranges */
576 if (HTTP_G(ctype)) {
577 efree(HTTP_G(ctype));
578 }
579 HTTP_G(ctype) = estrndup(ct, ct_len);
580
581 content_type = (char *) emalloc(strlen("Content-Type: ") + ct_len + 1);
582 sprintf(content_type, "Content-Type: %s", ct);
583
584 RETVAL_BOOL(SUCCESS == http_send_header(content_type));
585 efree(content_type);
586 }
587 /* }}} */
588
589 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
590 *
591 * Set the Content Disposition. The Content-Disposition header is very useful
592 * if the data actually sent came from a file or something similar, that should
593 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
594 *
595 */
596 PHP_FUNCTION(http_content_disposition)
597 {
598 char *filename, *header;
599 int f_len;
600 zend_bool send_inline = 0;
601
602 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
603 RETURN_FALSE;
604 }
605
606 if (send_inline) {
607 header = (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len + 1);
608 sprintf(header, "Content-Disposition: inline; filename=\"%s\"", filename);
609 } else {
610 header = (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len + 1);
611 sprintf(header, "Content-Disposition: attachment; filename=\"%s\"", filename);
612 }
613
614 RETVAL_BOOL(SUCCESS == http_send_header(header));
615 efree(header);
616 }
617 /* }}} */
618
619 /* {{{ proto string http_chunked_decode(string encoded)
620 *
621 * This function decodes a string that was HTTP-chunked encoded.
622 * Returns false on failure.
623 */
624 PHP_FUNCTION(http_chunked_decode)
625 {
626 char *encoded = NULL, *decoded = NULL;
627 int encoded_len = 0, decoded_len = 0;
628
629 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
630 RETURN_FALSE;
631 }
632
633 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
634 RETURN_STRINGL(decoded, decoded_len, 0);
635 } else {
636 RETURN_FALSE;
637 }
638 }
639 /* }}} */
640
641 /* {{{ proto array http_split_response(string http_response)
642 *
643 * This function splits an HTTP response into an array with headers and the
644 * content body. The returned array may look simliar to the following example:
645 *
646 * <pre>
647 * array(
648 * 0 => array(
649 * 'Status' => '200 Ok',
650 * 'Content-Type' => 'text/plain',
651 * 'Content-Language' => 'en-US'
652 * ),
653 * 1 => "Hello World!"
654 * );
655 * </pre>
656 */
657 PHP_FUNCTION(http_split_response)
658 {
659 zval *zresponse, *zbody, *zheaders;
660
661 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
662 RETURN_FALSE;
663 }
664
665 convert_to_string_ex(&zresponse);
666
667 MAKE_STD_ZVAL(zbody);
668 MAKE_STD_ZVAL(zheaders);
669 array_init(zheaders);
670
671 http_split_response(zresponse, zheaders, zbody);
672
673 array_init(return_value);
674 add_index_zval(return_value, 0, zheaders);
675 add_index_zval(return_value, 1, zbody);
676 }
677 /* }}} */
678
679 /* {{{ HAVE_CURL */
680 #if defined(HAVE_CURL) && HAVE_CURL
681
682 /* {{{ proto string http_get(string url[, array options[, array &info]])
683 *
684 * Performs an HTTP GET request on the supplied url.
685 *
686 * The second parameter is expected to be an associative
687 * array where the following keys will be recognized:
688 * <pre>
689 * - redirect: int, whether and how many redirects to follow
690 * - unrestrictedauth: bool, whether to continue sending credentials on
691 * redirects to a different host
692 * - proxyhost: string, proxy host in "host[:port]" format
693 * - proxyport: int, use another proxy port as specified in proxyhost
694 * - proxyauth: string, proxy credentials in "user:pass" format
695 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
696 * - httpauth: string, http credentials in "user:pass" format
697 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
698 * - compress: bool, whether to allow gzip/deflate content encoding
699 * (defaults to true)
700 * - port: int, use another port as specified in the url
701 * - referer: string, the referer to sends
702 * - useragent: string, the user agent to send
703 * (defaults to PECL::HTTP/version (PHP/version)))
704 * - headers: array, list of custom headers as associative array
705 * like array("header" => "value")
706 * - cookies: array, list of cookies as associative array
707 * like array("cookie" => "value")
708 * - cookiestore: string, path to a file where cookies are/will be stored
709 * </pre>
710 *
711 * The optional third parameter will be filled with some additional information
712 * in form af an associative array, if supplied (don't forget to initialize it
713 * with NULL or array()).
714 */
715 PHP_FUNCTION(http_get)
716 {
717 char *URL, *data = NULL;
718 size_t data_len = 0;
719 int URL_len;
720 zval *options = NULL, *info = NULL;
721
722 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
723 RETURN_FALSE;
724 }
725
726 if (info) {
727 zval_dtor(info);
728 array_init(info);
729 }
730
731 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
732 RETURN_STRINGL(data, data_len, 0);
733 } else {
734 RETURN_FALSE;
735 }
736 }
737 /* }}} */
738
739 /* {{{ proto string http_head(string url[, array options[, array &info]])
740 *
741 * Performs an HTTP HEAD request on the suppied url.
742 * Returns the HTTP response as string.
743 * See http_get() for a full list of available options.
744 */
745 PHP_FUNCTION(http_head)
746 {
747 char *URL, *data = NULL;
748 size_t data_len = 0;
749 int URL_len;
750 zval *options = NULL, *info = NULL;
751
752 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
753 RETURN_FALSE;
754 }
755
756 if (info) {
757 zval_dtor(info);
758 array_init(info);
759 }
760
761 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
762 RETURN_STRINGL(data, data_len, 0);
763 } else {
764 RETURN_FALSE;
765 }
766 }
767 /* }}} */
768
769 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
770 *
771 * Performs an HTTP POST request, posting data.
772 * Returns the HTTP response as string.
773 * See http_get() for a full list of available options.
774 */
775 PHP_FUNCTION(http_post_data)
776 {
777 char *URL, *postdata, *data = NULL;
778 size_t data_len = 0;
779 int postdata_len, URL_len;
780 zval *options = NULL, *info = NULL;
781
782 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
783 RETURN_FALSE;
784 }
785
786 if (info) {
787 zval_dtor(info);
788 array_init(info);
789 }
790
791 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
792 RETURN_STRINGL(data, data_len, 0);
793 } else {
794 RETURN_FALSE;
795 }
796 }
797 /* }}} */
798
799 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
800 *
801 * Performs an HTTP POST request, posting www-form-urlencoded array data.
802 * Returns the HTTP response as string.
803 * See http_get() for a full list of available options.
804 */
805 PHP_FUNCTION(http_post_array)
806 {
807 char *URL, *data = NULL;
808 size_t data_len = 0;
809 int URL_len;
810 zval *options = NULL, *info = NULL, *postdata;
811
812 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
813 RETURN_FALSE;
814 }
815
816 if (info) {
817 zval_dtor(info);
818 array_init(info);
819 }
820
821 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
822 RETURN_STRINGL(data, data_len, 0);
823 } else {
824 RETURN_FALSE;
825 }
826 }
827 /* }}} */
828
829 #endif
830 /* }}} HAVE_CURL */
831
832
833 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
834 *
835 * Example:
836 * <pre>
837 * <?php
838 * if (!http_auth_basic('mike', 's3c|r3t')) {
839 * die('<h1>Authorization failed!</h1>');
840 * }
841 * ?>
842 * </pre>
843 */
844 PHP_FUNCTION(http_auth_basic)
845 {
846 char *realm = NULL, *user, *pass, *suser, *spass;
847 int r_len, u_len, p_len;
848
849 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
850 RETURN_FALSE;
851 }
852
853 if (!realm) {
854 realm = "Restricted";
855 }
856
857 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
858 http_auth_header("Basic", realm);
859 RETURN_FALSE;
860 }
861
862 if (strcasecmp(suser, user)) {
863 http_auth_header("Basic", realm);
864 RETURN_FALSE;
865 }
866
867 if (strcmp(spass, pass)) {
868 http_auth_header("Basic", realm);
869 RETURN_FALSE;
870 }
871
872 RETURN_TRUE;
873 }
874 /* }}} */
875
876 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
877 *
878 * Example:
879 * <pre>
880 * <?php
881 * function auth_cb($user, $pass)
882 * {
883 * global $db;
884 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
885 * if (strlen($realpass = $db->getOne($query)) {
886 * return $pass === $realpass;
887 * }
888 * return false;
889 * }
890 *
891 * if (!http_auth_basic_cb('auth_cb')) {
892 * die('<h1>Authorization failed</h1>');
893 * }
894 * ?>
895 * </pre>
896 */
897 PHP_FUNCTION(http_auth_basic_cb)
898 {
899 zval *cb;
900 char *realm = NULL, *user, *pass;
901 int r_len;
902
903 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
904 RETURN_FALSE;
905 }
906
907 if (!realm) {
908 realm = "Restricted";
909 }
910
911 if (SUCCESS != http_auth_credentials(&user, &pass)) {
912 http_auth_header("Basic", realm);
913 RETURN_FALSE;
914 }
915 {
916 zval *zparams[2] = {NULL, NULL}, retval;
917 int result = 0;
918
919 MAKE_STD_ZVAL(zparams[0]);
920 MAKE_STD_ZVAL(zparams[1]);
921 ZVAL_STRING(zparams[0], user, 0);
922 ZVAL_STRING(zparams[1], pass, 0);
923
924 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
925 &retval, 2, zparams TSRMLS_CC)) {
926 result = Z_LVAL(retval);
927 }
928
929 efree(user);
930 efree(pass);
931 efree(zparams[0]);
932 efree(zparams[1]);
933
934 if (!result) {
935 http_auth_header("Basic", realm);
936 }
937
938 RETURN_BOOL(result);
939 }
940 }
941 /* }}}*/
942
943
944 /* {{{ php_http_init_globals(zend_http_globals *) */
945 static void php_http_init_globals(zend_http_globals *http_globals)
946 {
947 http_globals->etag_started = 0;
948 http_globals->ctype = NULL;
949 http_globals->etag = NULL;
950 http_globals->lmod = 0;
951 #if defined(HAVE_CURL) && HAVE_CURL
952 http_globals->curlbuf.body.data = NULL;
953 http_globals->curlbuf.body.used = 0;
954 http_globals->curlbuf.body.free = 0;
955 http_globals->curlbuf.hdrs.data = NULL;
956 http_globals->curlbuf.hdrs.used = 0;
957 http_globals->curlbuf.hdrs.free = 0;
958 #endif
959 }
960 /* }}} */
961
962 /* {{{ PHP_MINIT_FUNCTION */
963 PHP_MINIT_FUNCTION(http)
964 {
965 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
966 #if defined(HAVE_CURL) && HAVE_CURL
967 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC, CONST_CS | CONST_PERSISTENT);
968 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS | CONST_PERSISTENT);
969 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM, CONST_CS | CONST_PERSISTENT);
970 #endif
971 return SUCCESS;
972 }
973 /* }}} */
974
975 /* {{{ PHP_RSHUTDOWN_FUNCTION */
976 PHP_RSHUTDOWN_FUNCTION(http)
977 {
978 if (HTTP_G(ctype)) {
979 efree(HTTP_G(ctype));
980 }
981 if (HTTP_G(etag)) {
982 efree(HTTP_G(etag));
983 }
984 #if defined(HAVE_CURL) && HAVE_CURL
985 if (HTTP_G(curlbuf).body.data) {
986 efree(HTTP_G(curlbuf).body.data);
987 }
988 if (HTTP_G(curlbuf).hdrs.data) {
989 efree(HTTP_G(curlbuf).hdrs.data);
990 }
991 #endif
992 return SUCCESS;
993 }
994 /* }}} */
995
996 /* {{{ PHP_MINFO_FUNCTION */
997 PHP_MINFO_FUNCTION(http)
998 {
999 php_info_print_table_start();
1000 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1001 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION);
1002 php_info_print_table_row(2, "cURL convenience functions:",
1003 #if defined(HAVE_CURL) && HAVE_CURL
1004 "enabled"
1005 #else
1006 "disabled"
1007 #endif
1008 );
1009 php_info_print_table_end();
1010 }
1011 /* }}} */
1012
1013 /*
1014 * Local variables:
1015 * tab-width: 4
1016 * c-basic-offset: 4
1017 * End:
1018 * vim600: noet sw=4 ts=4 fdm=marker
1019 * vim<600: noet sw=4 ts=4
1020 */
1021