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