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