- ditch http_split_response()
[m6w6/ext-http] / http_functions.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 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "SAPI.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "ext/standard/php_string.h"
27 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
28 # include "ext/session/php_session.h"
29 #endif
30
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_auth_api.h"
35 #include "php_http_request_api.h"
36 #include "php_http_cache_api.h"
37 #include "php_http_request_api.h"
38 #include "php_http_date_api.h"
39 #include "php_http_headers_api.h"
40 #include "php_http_message_api.h"
41 #include "php_http_send_api.h"
42 #include "php_http_url_api.h"
43
44 #include "phpstr/phpstr.h"
45
46 ZEND_EXTERN_MODULE_GLOBALS(http)
47
48 /* {{{ proto string http_date([int timestamp])
49 *
50 * This function returns a valid HTTP date regarding RFC 822/1123
51 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
52 *
53 */
54 PHP_FUNCTION(http_date)
55 {
56 long t = -1;
57
58 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
59 RETURN_FALSE;
60 }
61
62 if (t == -1) {
63 t = (long) time(NULL);
64 }
65
66 RETURN_STRING(http_date(t), 0);
67 }
68 /* }}} */
69
70 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
71 *
72 * This function returns an absolute URI constructed from url.
73 * If the url is already abolute but a different proto was supplied,
74 * only the proto part of the URI will be updated. If url has no
75 * path specified, the path of the current REQUEST_URI will be taken.
76 * The host will be taken either from the Host HTTP header of the client
77 * the SERVER_NAME or just localhost if prior are not available.
78 *
79 * Some examples:
80 * <pre>
81 * url = "page.php" => http://www.example.com/current/path/page.php
82 * url = "/page.php" => http://www.example.com/page.php
83 * url = "/page.php", proto = "https" => https://www.example.com/page.php
84 * </pre>
85 *
86 */
87 PHP_FUNCTION(http_absolute_uri)
88 {
89 char *url = NULL, *proto = NULL, *host = NULL;
90 int url_len = 0, proto_len = 0, host_len = 0;
91 long port = 0;
92
93 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssl", &url, &url_len, &proto, &proto_len, &host, &host_len, &port) != SUCCESS) {
94 RETURN_FALSE;
95 }
96
97 RETURN_STRING(http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port), 0);
98 }
99 /* }}} */
100
101 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
102 *
103 * This function negotiates the clients preferred language based on its
104 * Accept-Language HTTP header. It returns the negotiated language or
105 * the default language if none match.
106 *
107 * The qualifier is recognized and languages without qualifier are rated highest.
108 *
109 * The supported parameter is expected to be an array having
110 * the supported languages as array values.
111 *
112 * Example:
113 * <pre>
114 * <?php
115 * $langs = array(
116 * 'en-US',// default
117 * 'fr',
118 * 'fr-FR',
119 * 'de',
120 * 'de-DE',
121 * 'de-AT',
122 * 'de-CH',
123 * );
124 * include './langs/'. http_negotiate_language($langs) .'.php';
125 * ?>
126 * </pre>
127 *
128 */
129 PHP_FUNCTION(http_negotiate_language)
130 {
131 zval *supported;
132 char *def = NULL;
133 int def_len = 0;
134
135 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
136 RETURN_FALSE;
137 }
138
139 if (!def) {
140 def = "en-US";
141 }
142
143 RETURN_STRING(http_negotiate_language(supported, def), 0);
144 }
145 /* }}} */
146
147 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
148 *
149 * This function negotiates the clients preferred charset based on its
150 * Accept-Charset HTTP header. It returns the negotiated charset or
151 * the default charset if none match.
152 *
153 * The qualifier is recognized and charset without qualifier are rated highest.
154 *
155 * The supported parameter is expected to be an array having
156 * the supported charsets as array values.
157 *
158 * Example:
159 * <pre>
160 * <?php
161 * $charsets = array(
162 * 'iso-8859-1', // default
163 * 'iso-8859-2',
164 * 'iso-8859-15',
165 * 'utf-8'
166 * );
167 * $pref = http_negotiate_charset($charsets);
168 * if (!strcmp($pref, 'iso-8859-1')) {
169 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
170 * iconv_set_encoding('output_encoding', $pref);
171 * ob_start('ob_iconv_handler');
172 * }
173 * ?>
174 * </pre>
175 */
176 PHP_FUNCTION(http_negotiate_charset)
177 {
178 zval *supported;
179 char *def = NULL;
180 int def_len = 0;
181
182 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
183 RETURN_FALSE;
184 }
185
186 if (!def) {
187 def = "iso-8859-1";
188 }
189
190 RETURN_STRING(http_negotiate_charset(supported, def), 0);
191 }
192 /* }}} */
193
194 /* {{{ proto bool http_send_status(int status)
195 *
196 * Send HTTP status code.
197 *
198 */
199 PHP_FUNCTION(http_send_status)
200 {
201 int status = 0;
202
203 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
204 RETURN_FALSE;
205 }
206 if (status < 100 || status > 510) {
207 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Invalid HTTP status code (100-510): %d", status);
208 RETURN_FALSE;
209 }
210
211 RETURN_SUCCESS(http_send_status(status));
212 }
213 /* }}} */
214
215 /* {{{ proto bool http_send_last_modified([int timestamp])
216 *
217 * This converts the given timestamp to a valid HTTP date and
218 * sends it as "Last-Modified" HTTP header. If timestamp is
219 * omitted, current time is sent.
220 *
221 */
222 PHP_FUNCTION(http_send_last_modified)
223 {
224 long t = -1;
225
226 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
227 RETURN_FALSE;
228 }
229
230 if (t == -1) {
231 t = (long) time(NULL);
232 }
233
234 RETURN_SUCCESS(http_send_last_modified(t));
235 }
236 /* }}} */
237
238 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
239 *
240 * Sets the content type.
241 *
242 */
243 PHP_FUNCTION(http_send_content_type)
244 {
245 char *ct;
246 int ct_len = 0;
247
248 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
249 RETURN_FALSE;
250 }
251
252 if (!ct_len) {
253 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", lenof("application/x-octetstream")));
254 }
255 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
256 }
257 /* }}} */
258
259 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
260 *
261 * Set the Content Disposition. The Content-Disposition header is very useful
262 * if the data actually sent came from a file or something similar, that should
263 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
264 *
265 */
266 PHP_FUNCTION(http_send_content_disposition)
267 {
268 char *filename;
269 int f_len;
270 zend_bool send_inline = 0;
271
272 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
273 RETURN_FALSE;
274 }
275 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
276 }
277 /* }}} */
278
279 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
280 *
281 * Matches the given timestamp against the clients "If-Modified-Since" resp.
282 * "If-Unmodified-Since" HTTP headers.
283 *
284 */
285 PHP_FUNCTION(http_match_modified)
286 {
287 long t = -1;
288 zend_bool for_range = 0;
289
290 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
291 RETURN_FALSE;
292 }
293
294 // current time if not supplied (senseless though)
295 if (t == -1) {
296 t = (long) time(NULL);
297 }
298
299 if (for_range) {
300 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t));
301 }
302 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t));
303 }
304 /* }}} */
305
306 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
307 *
308 * This matches the given ETag against the clients
309 * "If-Match" resp. "If-None-Match" HTTP headers.
310 *
311 */
312 PHP_FUNCTION(http_match_etag)
313 {
314 int etag_len;
315 char *etag;
316 zend_bool for_range = 0;
317
318 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
319 RETURN_FALSE;
320 }
321
322 if (for_range) {
323 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag));
324 }
325 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag));
326 }
327 /* }}} */
328
329 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
330 *
331 * If timestamp_or_expires is greater than 0, it is handled as timestamp
332 * and will be sent as date of last modification. If it is 0 or omitted,
333 * the current time will be sent as Last-Modified date. If it's negative,
334 * it is handled as expiration time in seconds, which means that if the
335 * requested last modification date is not between the calculated timespan,
336 * the Last-Modified header is updated and the actual body will be sent.
337 *
338 */
339 PHP_FUNCTION(http_cache_last_modified)
340 {
341 long last_modified = 0, send_modified = 0, t;
342 zval *zlm;
343
344 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
345 RETURN_FALSE;
346 }
347
348 t = (long) time(NULL);
349
350 /* 0 or omitted */
351 if (!last_modified) {
352 /* does the client have? (att: caching "forever") */
353 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
354 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
355 /* send current time */
356 } else {
357 send_modified = t;
358 }
359 /* negative value is supposed to be expiration time */
360 } else if (last_modified < 0) {
361 last_modified += t;
362 send_modified = t;
363 /* send supplied time explicitly */
364 } else {
365 send_modified = last_modified;
366 }
367
368 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
369 }
370 /* }}} */
371
372 /* {{{ proto bool http_cache_etag([string etag])
373 *
374 * This function attempts to cache the HTTP body based on an ETag,
375 * either supplied or generated through calculation of the MD5
376 * checksum of the output (uses output buffering).
377 *
378 * If clients "If-None-Match" header matches the supplied/calculated
379 * ETag, the body is considered cached on the clients side and
380 * a "304 Not Modified" status code is issued.
381 *
382 */
383 PHP_FUNCTION(http_cache_etag)
384 {
385 char *etag = NULL;
386 int etag_len = 0;
387
388 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
389 RETURN_FALSE;
390 }
391
392 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
393 }
394 /* }}} */
395
396 /* {{{ proto string ob_etaghandler(string data, int mode)
397 *
398 * For use with ob_start().
399 */
400 PHP_FUNCTION(ob_etaghandler)
401 {
402 char *data;
403 int data_len;
404 long mode;
405
406 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
407 RETURN_FALSE;
408 }
409
410 Z_TYPE_P(return_value) = IS_STRING;
411 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
412 }
413 /* }}} */
414
415 /* {{{ proto void http_throttle(double sec[, long bytes = 2097152])
416 *
417 * Use with http_send() API.
418 *
419 * Example:
420 * <pre>
421 * <?php
422 * // ~ 20 kbyte/s
423 * # http_throttle(1, 20000);
424 * # http_throttle(0.5, 10000);
425 * # http_throttle(0.1, 2000);
426 * http_send_file('document.pdf');
427 * ?>
428 * </pre>
429 */
430 PHP_FUNCTION(http_throttle)
431 {
432 long chunk_size;
433 double interval;
434
435 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dl", &interval, &chunk_size)) {
436 return;
437 }
438
439 HTTP_G(send).throttle_delay = interval;
440 HTTP_G(send).buffer_size = chunk_size;
441 }
442 /* }}} */
443
444 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
445 *
446 * Redirect to a given url.
447 * The supplied url will be expanded with http_absolute_uri(), the params array will
448 * be treated with http_build_query() and the session identification will be appended
449 * if session is true.
450 *
451 * Depending on permanent the redirection will be issued with a permanent
452 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
453 * status code.
454 *
455 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
456 * if the client doesn't redirect immediatly.
457 */
458 PHP_FUNCTION(http_redirect)
459 {
460 int url_len;
461 size_t query_len = 0;
462 zend_bool session = 0, permanent = 0, free_params = 0;
463 zval *params = NULL;
464 char *query = NULL, *url = NULL, *URI, *LOC, *RED = NULL;
465
466 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
467 RETURN_FALSE;
468 }
469
470 /* append session info */
471 if (session) {
472 if (!params) {
473 free_params = 1;
474 MAKE_STD_ZVAL(params);
475 array_init(params);
476 }
477 #ifdef HAVE_PHP_SESSION
478 # ifdef COMPILE_DL_SESSION
479 if (SUCCESS == zend_get_module_started("session")) {
480 zval nm_retval, id_retval, func;
481
482 INIT_PZVAL(&func);
483 INIT_PZVAL(&nm_retval);
484 INIT_PZVAL(&id_retval);
485 ZVAL_NULL(&nm_retval);
486 ZVAL_NULL(&id_retval);
487
488 ZVAL_STRINGL(&func, "session_id", lenof("session_id"), 0);
489 call_user_function(EG(function_table), NULL, &func, &id_retval, 0, NULL TSRMLS_CC);
490 ZVAL_STRINGL(&func, "session_name", lenof("session_name"), 0);
491 call_user_function(EG(function_table), NULL, &func, &nm_retval, 0, NULL TSRMLS_CC);
492
493 if ( Z_TYPE(nm_retval) == IS_STRING && Z_STRLEN(nm_retval) &&
494 Z_TYPE(id_retval) == IS_STRING && Z_STRLEN(id_retval)) {
495 if (add_assoc_stringl_ex(params, Z_STRVAL(nm_retval), Z_STRLEN(nm_retval)+1,
496 Z_STRVAL(id_retval), Z_STRLEN(id_retval), 0) != SUCCESS) {
497 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
498 }
499 }
500 }
501 # else
502 if (PS(session_status) == php_session_active) {
503 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
504 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
505 }
506 }
507 # endif
508 #endif
509 }
510
511 /* treat params array with http_build_query() */
512 if (params) {
513 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
514 if (free_params) {
515 zval_dtor(params);
516 FREE_ZVAL(params);
517 }
518 if (query) {
519 efree(query);
520 }
521 RETURN_FALSE;
522 }
523 }
524
525 URI = http_absolute_uri(url);
526
527 if (query_len) {
528 spprintf(&LOC, 0, "Location: %s?%s", URI, query);
529 if (SG(request_info).request_method && strcmp(SG(request_info).request_method, "HEAD")) {
530 spprintf(&RED, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
531 }
532 } else {
533 spprintf(&LOC, 0, "Location: %s", URI);
534 if (SG(request_info).request_method && strcmp(SG(request_info).request_method, "HEAD")) {
535 spprintf(&RED, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
536 }
537 }
538
539 efree(URI);
540 if (query) {
541 efree(query);
542 }
543 if (free_params) {
544 zval_dtor(params);
545 FREE_ZVAL(params);
546 }
547
548 RETURN_SUCCESS(http_exit_ex(permanent ? 301 : 302, LOC, RED, 1));
549 }
550 /* }}} */
551
552 /* {{{ proto bool http_send_data(string data)
553 *
554 * Sends raw data with support for (multiple) range requests.
555 *
556 */
557 PHP_FUNCTION(http_send_data)
558 {
559 zval *zdata;
560
561 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
562 RETURN_FALSE;
563 }
564
565 convert_to_string_ex(&zdata);
566 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
567 }
568 /* }}} */
569
570 /* {{{ proto bool http_send_file(string file)
571 *
572 * Sends a file with support for (multiple) range requests.
573 *
574 */
575 PHP_FUNCTION(http_send_file)
576 {
577 char *file;
578 int flen = 0;
579
580 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
581 RETURN_FALSE;
582 }
583 if (!flen) {
584 RETURN_FALSE;
585 }
586
587 RETURN_SUCCESS(http_send_file(file));
588 }
589 /* }}} */
590
591 /* {{{ proto bool http_send_stream(resource stream)
592 *
593 * Sends an already opened stream with support for (multiple) range requests.
594 *
595 */
596 PHP_FUNCTION(http_send_stream)
597 {
598 zval *zstream;
599 php_stream *file;
600
601 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
602 RETURN_FALSE;
603 }
604
605 php_stream_from_zval(file, &zstream);
606 RETURN_SUCCESS(http_send_stream(file));
607 }
608 /* }}} */
609
610 /* {{{ proto string http_chunked_decode(string encoded)
611 *
612 * This function decodes a string that was HTTP-chunked encoded.
613 * Returns false on failure.
614 */
615 PHP_FUNCTION(http_chunked_decode)
616 {
617 char *encoded = NULL, *decoded = NULL;
618 int encoded_len = 0, decoded_len = 0;
619
620 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
621 RETURN_FALSE;
622 }
623
624 if (NULL != http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
625 RETURN_STRINGL(decoded, decoded_len, 0);
626 } else {
627 RETURN_FALSE;
628 }
629 }
630 /* }}} */
631
632 /* {{{ proto object http_parse_message(string message)
633 *
634 * Parses (a) http_message(s) into a simple recursive object structure:
635 *
636 * <pre>
637 * <?php
638 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
639 *
640 * stdClass object
641 * (
642 * [type] => 2
643 * [httpVersion] => 1.1
644 * [responseCode] => 200
645 * [headers] => Array
646 * (
647 * [Content-Length] => 3
648 * [Server] => Apache
649 * )
650 * [body] => Hi!
651 * [parentMessage] => stdClass object
652 * (
653 * [type] => 2
654 * [httpVersion] => 1.1
655 * [responseCode] => 302
656 * [headers] => Array
657 * (
658 * [Content-Length] => 0
659 * [Location] => ...
660 * )
661 * [body] =>
662 * [parentMessage] => ...
663 * )
664 * )
665 * ?>
666 * </pre>
667 */
668 PHP_FUNCTION(http_parse_message)
669 {
670 char *message;
671 int message_len;
672 http_message *msg = NULL;
673
674 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &message, &message_len)) {
675 RETURN_NULL();
676 }
677
678
679 if (msg = http_message_parse(message, message_len)) {
680 object_init(return_value);
681 http_message_tostruct_recursive(msg, return_value);
682 } else {
683 RETURN_NULL();
684 }
685 }
686 /* }}} */
687
688 /* {{{ proto array http_parse_headers(string header)
689 *
690 */
691 PHP_FUNCTION(http_parse_headers)
692 {
693 char *header;
694 int header_len;
695
696 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
697 RETURN_FALSE;
698 }
699
700 array_init(return_value);
701 if (SUCCESS != http_parse_headers(header, return_value)) {
702 zval_dtor(return_value);
703 RETURN_FALSE;
704 }
705 }
706 /* }}}*/
707
708 /* {{{ proto array http_get_request_headers(void)
709 *
710 * Get a list of incoming HTTP headers.
711 */
712 PHP_FUNCTION(http_get_request_headers)
713 {
714 NO_ARGS;
715
716 array_init(return_value);
717 http_get_request_headers(return_value);
718 }
719 /* }}} */
720
721 /* {{{ proto string http_get_request_body(void)
722 *
723 * Get the raw request body (e.g. POST or PUT data).
724 */
725 PHP_FUNCTION(http_get_request_body)
726 {
727 char *body;
728 size_t length;
729
730 NO_ARGS;
731
732 if (SUCCESS == http_get_request_body(&body, &length)) {
733 RETURN_STRINGL(body, (int) length, 0);
734 } else {
735 RETURN_NULL();
736 }
737 }
738 /* }}} */
739
740 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
741 *
742 * Match an incoming HTTP header.
743 */
744 PHP_FUNCTION(http_match_request_header)
745 {
746 char *header, *value;
747 int header_len, value_len;
748 zend_bool match_case = 0;
749
750 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
751 RETURN_FALSE;
752 }
753
754 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
755 }
756 /* }}} */
757
758 /* {{{ HAVE_CURL */
759 #ifdef HTTP_HAVE_CURL
760
761 /* {{{ proto string http_get(string url[, array options[, array &info]])
762 *
763 * Performs an HTTP GET request on the supplied url.
764 *
765 * The second parameter is expected to be an associative
766 * array where the following keys will be recognized:
767 * <pre>
768 * - redirect: int, whether and how many redirects to follow
769 * - unrestrictedauth: bool, whether to continue sending credentials on
770 * redirects to a different host
771 * - proxyhost: string, proxy host in "host[:port]" format
772 * - proxyport: int, use another proxy port as specified in proxyhost
773 * - proxyauth: string, proxy credentials in "user:pass" format
774 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
775 * - httpauth: string, http credentials in "user:pass" format
776 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
777 * - compress: bool, whether to allow gzip/deflate content encoding
778 * (defaults to true)
779 * - port: int, use another port as specified in the url
780 * - referer: string, the referer to sends
781 * - useragent: string, the user agent to send
782 * (defaults to PECL::HTTP/version (PHP/version)))
783 * - headers: array, list of custom headers as associative array
784 * like array("header" => "value")
785 * - cookies: array, list of cookies as associative array
786 * like array("cookie" => "value")
787 * - cookiestore: string, path to a file where cookies are/will be stored
788 * - resume: int, byte offset to start the download from;
789 * if the server supports ranges
790 * - maxfilesize: int, maximum file size that should be downloaded;
791 * has no effect, if the size of the requested entity is not known
792 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
793 * - timeout: int, seconds the request may take
794 * - connecttimeout: int, seconds the connect may take
795 * - onprogress: mixed, progress callback
796 * </pre>
797 *
798 * The optional third parameter will be filled with some additional information
799 * in form af an associative array, if supplied, like the following example:
800 * <pre>
801 * <?php
802 * array (
803 * 'effective_url' => 'http://localhost',
804 * 'response_code' => 403,
805 * 'total_time' => 0.017,
806 * 'namelookup_time' => 0.013,
807 * 'connect_time' => 0.014,
808 * 'pretransfer_time' => 0.014,
809 * 'size_upload' => 0,
810 * 'size_download' => 202,
811 * 'speed_download' => 11882,
812 * 'speed_upload' => 0,
813 * 'header_size' => 145,
814 * 'request_size' => 62,
815 * 'ssl_verifyresult' => 0,
816 * 'filetime' => -1,
817 * 'content_length_download' => 202,
818 * 'content_length_upload' => 0,
819 * 'starttransfer_time' => 0.017,
820 * 'content_type' => 'text/html; charset=iso-8859-1',
821 * 'redirect_time' => 0,
822 * 'redirect_count' => 0,
823 * 'http_connectcode' => 0,
824 * 'httpauth_avail' => 0,
825 * 'proxyauth_avail' => 0,
826 * )
827 * ?>
828 * </pre>
829 */
830 PHP_FUNCTION(http_get)
831 {
832 zval *options = NULL, *info = NULL;
833 char *URL;
834 int URL_len;
835 phpstr response;
836
837 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
838 RETURN_FALSE;
839 }
840
841 if (info) {
842 zval_dtor(info);
843 array_init(info);
844 }
845
846 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
847 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
848 RETURN_PHPSTR_VAL(&response);
849 } else {
850 RETURN_FALSE;
851 }
852 }
853 /* }}} */
854
855 /* {{{ proto string http_head(string url[, array options[, array &info]])
856 *
857 * Performs an HTTP HEAD request on the suppied url.
858 * Returns the HTTP response as string.
859 * See http_get() for a full list of available options.
860 */
861 PHP_FUNCTION(http_head)
862 {
863 zval *options = NULL, *info = NULL;
864 char *URL;
865 int URL_len;
866 phpstr response;
867
868 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
869 RETURN_FALSE;
870 }
871
872 if (info) {
873 zval_dtor(info);
874 array_init(info);
875 }
876
877 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
878 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
879 RETURN_PHPSTR_VAL(&response);
880 } else {
881 RETURN_FALSE;
882 }
883 }
884 /* }}} */
885
886 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
887 *
888 * Performs an HTTP POST request, posting data.
889 * Returns the HTTP response as string.
890 * See http_get() for a full list of available options.
891 */
892 PHP_FUNCTION(http_post_data)
893 {
894 zval *options = NULL, *info = NULL;
895 char *URL, *postdata;
896 int postdata_len, URL_len;
897 phpstr response;
898 http_request_body body;
899
900 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
901 RETURN_FALSE;
902 }
903
904 if (info) {
905 zval_dtor(info);
906 array_init(info);
907 }
908
909 body.type = HTTP_REQUEST_BODY_CSTRING;
910 body.data = postdata;
911 body.size = postdata_len;
912
913 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
914 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
915 RETVAL_PHPSTR_VAL(&response);
916 } else {
917 RETVAL_FALSE;
918 }
919 }
920 /* }}} */
921
922 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
923 *
924 * Performs an HTTP POST request, posting www-form-urlencoded array data.
925 * Returns the HTTP response as string.
926 * See http_get() for a full list of available options.
927 */
928 PHP_FUNCTION(http_post_fields)
929 {
930 zval *options = NULL, *info = NULL, *fields, *files = NULL;
931 char *URL;
932 int URL_len;
933 phpstr response;
934 http_request_body body;
935
936 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
937 RETURN_FALSE;
938 }
939
940 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
941 RETURN_FALSE;
942 }
943
944 if (info) {
945 zval_dtor(info);
946 array_init(info);
947 }
948
949 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
950 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
951 RETVAL_PHPSTR_VAL(&response);
952 } else {
953 RETVAL_FALSE;
954 }
955 http_request_body_dtor(&body);
956 }
957 /* }}} */
958
959 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
960 *
961 * Performs an HTTP PUT request, uploading file.
962 * Returns the HTTP response as string.
963 * See http_get() for a full list of available options.
964 */
965 PHP_FUNCTION(http_put_file)
966 {
967 char *URL, *file;
968 int URL_len, f_len;
969 zval *options = NULL, *info = NULL;
970 phpstr response;
971 php_stream *stream;
972 php_stream_statbuf ssb;
973 http_request_body body;
974
975 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
976 RETURN_FALSE;
977 }
978
979 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
980 RETURN_FALSE;
981 }
982 if (php_stream_stat(stream, &ssb)) {
983 php_stream_close(stream);
984 RETURN_FALSE;
985 }
986
987 if (info) {
988 zval_dtor(info);
989 array_init(info);
990 }
991
992 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
993 body.data = stream;
994 body.size = ssb.sb.st_size;
995
996 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
997 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
998 RETVAL_PHPSTR_VAL(&response);
999 } else {
1000 RETVAL_FALSE;
1001 }
1002 http_request_body_dtor(&body);
1003 }
1004 /* }}} */
1005
1006 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1007 *
1008 * Performs an HTTP PUT request, uploading stream.
1009 * Returns the HTTP response as string.
1010 * See http_get() for a full list of available options.
1011 */
1012 PHP_FUNCTION(http_put_stream)
1013 {
1014 zval *resource, *options = NULL, *info = NULL;
1015 char *URL;
1016 int URL_len;
1017 phpstr response;
1018 php_stream *stream;
1019 php_stream_statbuf ssb;
1020 http_request_body body;
1021
1022 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1023 RETURN_FALSE;
1024 }
1025
1026 php_stream_from_zval(stream, &resource);
1027 if (php_stream_stat(stream, &ssb)) {
1028 RETURN_FALSE;
1029 }
1030
1031 if (info) {
1032 zval_dtor(info);
1033 array_init(info);
1034 }
1035
1036 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1037 body.data = stream;
1038 body.size = ssb.sb.st_size;
1039
1040 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1041 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1042 RETURN_PHPSTR_VAL(&response);
1043 } else {
1044 RETURN_NULL();
1045 }
1046 }
1047 /* }}} */
1048
1049 /* {{{ proto long http_request_method_register(string method)
1050 *
1051 * Register a custom request method.
1052 */
1053 PHP_FUNCTION(http_request_method_register)
1054 {
1055 char *method;
1056 int *method_len;
1057 unsigned long existing;
1058
1059 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1060 RETURN_FALSE;
1061 }
1062 if (existing = http_request_method_exists(1, 0, method)) {
1063 RETURN_LONG((long) existing);
1064 }
1065
1066 RETVAL_LONG((long) http_request_method_register(method));
1067 }
1068 /* }}} */
1069
1070 /* {{{ proto bool http_request_method_unregister(mixed method)
1071 *
1072 * Unregister a previously registered custom request method.
1073 */
1074 PHP_FUNCTION(http_request_method_unregister)
1075 {
1076 zval *method;
1077
1078 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1079 RETURN_FALSE;
1080 }
1081
1082 switch (Z_TYPE_P(method))
1083 {
1084 case IS_OBJECT:
1085 convert_to_string(method);
1086 case IS_STRING:
1087 #include "zend_operators.h"
1088 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1089 convert_to_long(method);
1090 } else {
1091 unsigned long mn;
1092 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1093 RETURN_FALSE;
1094 }
1095 zval_dtor(method);
1096 ZVAL_LONG(method, (long)mn);
1097 }
1098 case IS_LONG:
1099 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1100 default:
1101 RETURN_FALSE;
1102 }
1103 }
1104 /* }}} */
1105
1106 /* {{{ proto long http_request_method_exists(mixed method)
1107 *
1108 * Check if a request method is registered (or available by default).
1109 */
1110 PHP_FUNCTION(http_request_method_exists)
1111 {
1112 IF_RETVAL_USED {
1113 zval *method;
1114
1115 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1116 RETURN_FALSE;
1117 }
1118
1119 switch (Z_TYPE_P(method))
1120 {
1121 case IS_OBJECT:
1122 convert_to_string(method);
1123 case IS_STRING:
1124 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1125 convert_to_long(method);
1126 } else {
1127 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1128 }
1129 case IS_LONG:
1130 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1131 default:
1132 RETURN_FALSE;
1133 }
1134 }
1135 }
1136 /* }}} */
1137
1138 /* {{{ proto string http_request_method_name(long method)
1139 *
1140 * Get the literal string representation of a standard or registered request method.
1141 */
1142 PHP_FUNCTION(http_request_method_name)
1143 {
1144 IF_RETVAL_USED {
1145 long method;
1146
1147 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1148 RETURN_FALSE;
1149 }
1150
1151 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method)), 0);
1152 }
1153 }
1154 /* }}} */
1155 #endif
1156 /* }}} HAVE_CURL */
1157
1158
1159 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1160 *
1161 * Example:
1162 * <pre>
1163 * <?php
1164 * if (!http_auth_basic('mike', 's3c|r3t')) {
1165 * die('<h1>Authorization failed!</h1>');
1166 * }
1167 * ?>
1168 * </pre>
1169 */
1170 PHP_FUNCTION(http_auth_basic)
1171 {
1172 char *realm = NULL, *user, *pass, *suser, *spass;
1173 int r_len, u_len, p_len;
1174
1175 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
1176 RETURN_FALSE;
1177 }
1178
1179 if (!realm) {
1180 realm = "Restricted";
1181 }
1182
1183 if (SUCCESS != http_auth_basic_credentials(&suser, &spass)) {
1184 http_auth_basic_header(realm);
1185 RETURN_FALSE;
1186 }
1187
1188 if (strcasecmp(suser, user)) {
1189 http_auth_basic_header(realm);
1190 RETURN_FALSE;
1191 }
1192
1193 if (strcmp(spass, pass)) {
1194 http_auth_basic_header(realm);
1195 RETURN_FALSE;
1196 }
1197
1198 RETURN_TRUE;
1199 }
1200 /* }}} */
1201
1202 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1203 *
1204 * Example:
1205 * <pre>
1206 * <?php
1207 * function auth_cb($user, $pass)
1208 * {
1209 * global $db;
1210 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1211 * if (strlen($realpass = $db->getOne($query)) {
1212 * return $pass === $realpass;
1213 * }
1214 * return false;
1215 * }
1216 * if (!http_auth_basic_cb('auth_cb')) {
1217 * die('<h1>Authorization failed</h1>');
1218 * }
1219 * ?>
1220 * </pre>
1221 */
1222 PHP_FUNCTION(http_auth_basic_cb)
1223 {
1224 zval *cb;
1225 char *realm = NULL, *user, *pass;
1226 int r_len;
1227
1228 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1229 RETURN_FALSE;
1230 }
1231
1232 if (!realm) {
1233 realm = "Restricted";
1234 }
1235
1236 if (SUCCESS != http_auth_basic_credentials(&user, &pass)) {
1237 http_auth_basic_header(realm);
1238 RETURN_FALSE;
1239 }
1240 {
1241 zval *zparams[2] = {NULL, NULL}, retval;
1242 int result = 0;
1243
1244 MAKE_STD_ZVAL(zparams[0]);
1245 MAKE_STD_ZVAL(zparams[1]);
1246 ZVAL_STRING(zparams[0], user, 0);
1247 ZVAL_STRING(zparams[1], pass, 0);
1248
1249 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1250 &retval, 2, zparams TSRMLS_CC)) {
1251 result = Z_LVAL(retval);
1252 }
1253
1254 efree(user);
1255 efree(pass);
1256 efree(zparams[0]);
1257 efree(zparams[1]);
1258
1259 if (!result) {
1260 http_auth_basic_header(realm);
1261 }
1262
1263 RETURN_BOOL(result);
1264 }
1265 }
1266 /* }}}*/
1267
1268 /* {{{ Sara Golemons http_build_query() */
1269 #ifndef ZEND_ENGINE_2
1270
1271 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1272 Generates a form-encoded query string from an associative array or object. */
1273 PHP_FUNCTION(http_build_query)
1274 {
1275 zval *formdata;
1276 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1277 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1278 phpstr *formstr;
1279
1280 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1281 RETURN_FALSE;
1282 }
1283
1284 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
1285 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1286 RETURN_FALSE;
1287 }
1288
1289 if (!arg_sep_len) {
1290 arg_sep = HTTP_URL_ARGSEP;
1291 }
1292
1293 formstr = phpstr_new();
1294 if (SUCCESS != http_urlencode_hash_implementation_ex(HASH_OF(formdata), formstr, arg_sep, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL))) {
1295 phpstr_free(&formstr);
1296 RETURN_FALSE;
1297 }
1298
1299 if (!formstr->used) {
1300 phpstr_free(&formstr);
1301 RETURN_NULL();
1302 }
1303
1304 RETURN_PHPSTR_PTR(formstr);
1305 }
1306 /* }}} */
1307 #endif /* !ZEND_ENGINE_2 */
1308 /* }}} */
1309
1310 PHP_FUNCTION(http_test)
1311 {
1312 ulong idx;
1313 char *key;
1314 zval **data;
1315 FOREACH_HASH_KEYVAL(&EG(symbol_table), key, idx, data) {
1316 convert_to_string_ex(data);
1317 fprintf(stderr, "\t %s => %s\n", key, Z_STRVAL_PP(data));
1318 }
1319 }
1320
1321 /*
1322 * Local variables:
1323 * tab-width: 4
1324 * c-basic-offset: 4
1325 * End:
1326 * vim600: noet sw=4 ts=4 fdm=marker
1327 * vim<600: noet sw=4 ts=4
1328 */
1329