- fix compiler warning on win32
[m6w6/ext-http] / http_functions.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
18 #include "php_http.h"
19
20 #include "php_ini.h"
21 #include "ext/standard/php_string.h"
22 #include "zend_operators.h"
23
24 #ifdef HAVE_PHP_SESSION
25 # include "ext/session/php_session.h"
26 #endif
27
28 #include "php_http_api.h"
29 #include "php_http_cache_api.h"
30 #include "php_http_cookie_api.h"
31 #include "php_http_date_api.h"
32 #include "php_http_encoding_api.h"
33 #include "php_http_headers_api.h"
34 #include "php_http_message_api.h"
35 #include "php_http_request_api.h"
36 #include "php_http_request_method_api.h"
37 #include "php_http_send_api.h"
38 #include "php_http_url_api.h"
39
40 /* {{{ proto string http_date([int timestamp])
41 *
42 * Compose a valid HTTP date regarding RFC 1123
43 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
44 *
45 * Accepts an optional unix timestamp as parameter.
46 *
47 * Returns the HTTP date as string.
48 */
49 PHP_FUNCTION(http_date)
50 {
51 long t = -1;
52
53 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
54 RETURN_FALSE;
55 }
56
57 if (t == -1) {
58 t = (long) HTTP_GET_REQUEST_TIME();
59 }
60
61 RETURN_STRING(http_date(t), 0);
62 }
63 /* }}} */
64
65 /* {{{ proto string http_build_url(mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE[, array &new_url]]])
66 *
67 * Build an URL.
68 *
69 * Expexts (part(s) of) an URL as first parameter in form of a string or assoziative array
70 * like parse_url() returns. Accepts an optional second parameter in the same way as the
71 * first argument. Accepts an optional third integer parameter, which is a bitmask of
72 * binary or'ed HTTP_URL_* constants. The optional fourth parameter will be filled
73 * with the results as associative array like parse_url() would return.
74 *
75 * The parts of the second URL will be merged into the first according to the flags argument.
76 * The following flags are recognized:
77 * <pre>
78 * - HTTP_URL_REPLACE: (default) set parts of the second url will replace the parts in the first
79 * - HTTP_URL_JOIN_PATH: the path of the second url will be merged into the one of the first
80 * - HTTP_URL_JOIN_QUERY: the two querystrings will be merged naivly; no replacements are done
81 * - HTTP_URL_STRIP_USER: the user part will not appear in the result
82 * - HTTP_URL_STRIP_PASS: the password part will not appear in the result
83 * - HTTP_URL_STRIP_AUTH: neither the user nor the password part will appear in the result
84 * - HTTP_URL_STRIP_PORT: no explicit port will be set in the result
85 * - HTTP_URL_STRIP_PATH: the path part will not appear in the result
86 * - HTTP_URL_STRIP_QUERY: no query string will be present in the result
87 * - HTTP_URL_STRIP_FRAGMENT: no fragment will be present in the result
88 * </pre>
89 *
90 * Example:
91 * <pre>
92 * <?php
93 * // ftp://ftp.example.com/pub/files/current/?a=b&a=c
94 * echo http_build_url("http://user@www.example.com/pub/index.php?a=b#files",
95 * array(
96 * "scheme" => "ftp",
97 * "host" => "ftp.example.com",
98 * "path" => "files/current/",
99 * "query" => "a=c"
100 * ),
101 * HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
102 * );
103 * ?>
104 * </pre>
105 * Returns the new URL as string on success or FALSE on failure.
106 */
107 PHP_FUNCTION(http_build_url)
108 {
109 char *url_str = NULL;
110 size_t url_len = 0;
111 long flags = HTTP_URL_REPLACE;
112 zval *z_old_url = NULL, *z_new_url = NULL, *z_composed_url = NULL;
113 php_url *old_url = NULL, *new_url = NULL, *composed_url = NULL;
114
115 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|z/lz", &z_old_url, &z_new_url, &flags, &z_composed_url) != SUCCESS) {
116 RETURN_FALSE;
117 }
118
119 if (z_new_url) {
120 if (Z_TYPE_P(z_new_url) == IS_ARRAY || Z_TYPE_P(z_new_url) == IS_OBJECT) {
121 new_url = array2url(HASH_OF(z_new_url));
122 } else {
123 convert_to_string(z_new_url);
124 if (!(new_url = php_url_parse_ex(Z_STRVAL_P(z_new_url), Z_STRLEN_P(z_new_url)))) {
125 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url));
126 RETURN_FALSE;
127 }
128 }
129 }
130
131 if (Z_TYPE_P(z_old_url) == IS_ARRAY || Z_TYPE_P(z_old_url) == IS_OBJECT) {
132 old_url = array2url(HASH_OF(z_old_url));
133 } else {
134 convert_to_string(z_old_url);
135 if (!(old_url = php_url_parse_ex(Z_STRVAL_P(z_old_url), Z_STRLEN_P(z_old_url)))) {
136 if (new_url) {
137 php_url_free(new_url);
138 }
139 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url));
140 RETURN_FALSE;
141 }
142 }
143
144 if (z_composed_url) {
145 http_build_url(flags, old_url, new_url, &composed_url, &url_str, &url_len);
146
147 zval_dtor(z_composed_url);
148 array_init(z_composed_url);
149 if (composed_url->scheme) {
150 add_assoc_string(z_composed_url, "scheme", composed_url->scheme, 1);
151 }
152 if (composed_url->user) {
153 add_assoc_string(z_composed_url, "user", composed_url->user, 1);
154 }
155 if (composed_url->pass) {
156 add_assoc_string(z_composed_url, "pass", composed_url->pass, 1);
157 }
158 if (composed_url->host) {
159 add_assoc_string(z_composed_url, "host", composed_url->host, 1);
160 }
161 if (composed_url->port) {
162 add_assoc_long(z_composed_url, "port", composed_url->port);
163 }
164 if (composed_url->path) {
165 add_assoc_string(z_composed_url, "path", composed_url->path, 1);
166 }
167 if (composed_url->query) {
168 add_assoc_string(z_composed_url, "query", composed_url->query, 1);
169 }
170 if (composed_url->fragment) {
171 add_assoc_string(z_composed_url, "fragment", composed_url->fragment, 1);
172 }
173 php_url_free(composed_url);
174 } else {
175 http_build_url(flags, old_url, new_url, NULL, &url_str, &url_len);
176 }
177
178 if (new_url) {
179 php_url_free(new_url);
180 }
181 php_url_free(old_url);
182
183 RETURN_STRINGL(url_str, url_len, 0);
184 }
185 /* }}} */
186
187 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
188 *
189 * Opponent to parse_str().
190 *
191 * Expects an array as first argument which represents the parts of the query string to build.
192 * Accepts a string as optional second parameter containing a top-level prefix to use.
193 * The optional third parameter should specify an argument separator to use (by default the
194 * INI setting arg_separator.output will be used, or "&" if neither is set).
195 *
196 * Returns the built query as string on success or FALSE on failure.
197 */
198 PHP_FUNCTION(http_build_str)
199 {
200 zval *formdata;
201 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
202 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
203 phpstr formstr;
204
205 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
206 RETURN_FALSE;
207 }
208
209 if (!arg_sep_len) {
210 arg_sep = HTTP_URL_ARGSEP;
211 arg_sep_len = lenof(HTTP_URL_ARGSEP);
212 }
213
214 phpstr_init(&formstr);
215 if (SUCCESS != http_urlencode_hash_recursive(HASH_OF(formdata), &formstr, arg_sep, arg_sep_len, prefix, prefix_len)) {
216 RETURN_FALSE;
217 }
218
219 if (!formstr.used) {
220 phpstr_dtor(&formstr);
221 RETURN_NULL();
222 }
223
224 RETURN_PHPSTR_VAL(&formstr);
225 }
226 /* }}} */
227
228 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
229 { \
230 HashTable *result; \
231 if ((result = http_negotiate_ ##type(supported))) { \
232 char *key; \
233 uint key_len; \
234 ulong idx; \
235 \
236 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
237 RETVAL_STRINGL(key, key_len-1, 0); \
238 } else { \
239 RETVAL_NULL(); \
240 } \
241 \
242 if (rs_array) { \
243 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
244 } \
245 \
246 zend_hash_destroy(result); \
247 FREE_HASHTABLE(result); \
248 \
249 } else { \
250 zval **value; \
251 \
252 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
253 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
254 RETVAL_ZVAL(*value, 1, 0); \
255 } else { \
256 RETVAL_NULL(); \
257 } \
258 \
259 if (rs_array) { \
260 HashPosition pos; \
261 zval **value; \
262 \
263 FOREACH_VAL(pos, supported, value) { \
264 convert_to_string_ex(value); \
265 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
266 } \
267 } \
268 } \
269 }
270
271
272 /* {{{ proto string http_negotiate_language(array supported[, array &result])
273 *
274 * This function negotiates the clients preferred language based on its
275 * Accept-Language HTTP header. The qualifier is recognized and languages
276 * without qualifier are rated highest. The qualifier will be decreased by
277 * 10% for partial matches (i.e. matching primary language).
278 *
279 * Expects an array as parameter containing the supported languages as values.
280 * If the optional second parameter is supplied, it will be filled with an
281 * array containing the negotiation results.
282 *
283 * Returns the negotiated language or the default language (i.e. first array entry)
284 * if none match.
285 *
286 * Example:
287 * <pre>
288 * <?php
289 * $langs = array(
290 * 'en-US',// default
291 * 'fr',
292 * 'fr-FR',
293 * 'de',
294 * 'de-DE',
295 * 'de-AT',
296 * 'de-CH',
297 * );
298 *
299 * include './langs/'. http_negotiate_language($langs, $result) .'.php';
300 *
301 * print_r($result);
302 * ?>
303 * </pre>
304 */
305 PHP_FUNCTION(http_negotiate_language)
306 {
307 zval *supported, *rs_array = NULL;
308
309 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array) != SUCCESS) {
310 RETURN_FALSE;
311 }
312
313 if (rs_array) {
314 zval_dtor(rs_array);
315 array_init(rs_array);
316 }
317
318 HTTP_DO_NEGOTIATE(language, supported, rs_array);
319 }
320 /* }}} */
321
322 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
323 *
324 * This function negotiates the clients preferred charset based on its
325 * Accept-Charset HTTP header. The qualifier is recognized and charsets
326 * without qualifier are rated highest.
327 *
328 * Expects an array as parameter containing the supported charsets as values.
329 * If the optional second parameter is supplied, it will be filled with an
330 * array containing the negotiation results.
331 *
332 * Returns the negotiated charset or the default charset (i.e. first array entry)
333 * if none match.
334 *
335 * Example:
336 * <pre>
337 * <?php
338 * $charsets = array(
339 * 'iso-8859-1', // default
340 * 'iso-8859-2',
341 * 'iso-8859-15',
342 * 'utf-8'
343 * );
344 *
345 * $pref = http_negotiate_charset($charsets, $result);
346 *
347 * if (strcmp($pref, 'iso-8859-1')) {
348 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
349 * iconv_set_encoding('output_encoding', $pref);
350 * ob_start('ob_iconv_handler');
351 * }
352 *
353 * print_r($result);
354 * ?>
355 * </pre>
356 */
357 PHP_FUNCTION(http_negotiate_charset)
358 {
359 zval *supported, *rs_array = NULL;
360
361 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array) != SUCCESS) {
362 RETURN_FALSE;
363 }
364
365 if (rs_array) {
366 zval_dtor(rs_array);
367 array_init(rs_array);
368 }
369
370 HTTP_DO_NEGOTIATE(charset, supported, rs_array);
371 }
372 /* }}} */
373
374 /* {{{ proto string http_negotiate_ctype(array supported[, array &result])
375 *
376 * This function negotiates the clients preferred content type based on its
377 * Accept HTTP header. The qualifier is recognized and content types
378 * without qualifier are rated highest.
379 *
380 * Expects an array as parameter containing the supported content types as values.
381 * If the optional second parameter is supplied, it will be filled with an
382 * array containing the negotiation results.
383 *
384 * Returns the negotiated content type or the default content type
385 * (i.e. first array entry) if none match.
386 *
387 * Example:
388 * <pre>
389 * <?php
390 * $ctypes = array('application/xhtml+xml', 'text/html');
391 * http_send_content_type(http_negotiate_content_type($ctypes));
392 * ?>
393 * </pre>
394 */
395 PHP_FUNCTION(http_negotiate_content_type)
396 {
397 zval *supported, *rs_array = NULL;
398
399 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array)) {
400 RETURN_FALSE;
401 }
402
403 if (rs_array) {
404 zval_dtor(rs_array);
405 array_init(rs_array);
406 }
407
408 HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
409 }
410 /* }}} */
411
412 /* {{{ proto bool http_send_status(int status)
413 *
414 * Send HTTP status code.
415 *
416 * Expects an HTTP status code as parameter.
417 *
418 * Returns TRUE on success or FALSE on failure.
419 */
420 PHP_FUNCTION(http_send_status)
421 {
422 int status = 0;
423
424 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
425 RETURN_FALSE;
426 }
427 if (status < 100 || status > 510) {
428 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Invalid HTTP status code (100-510): %d", status);
429 RETURN_FALSE;
430 }
431
432 RETURN_SUCCESS(http_send_status(status));
433 }
434 /* }}} */
435
436 /* {{{ proto bool http_send_last_modified([int timestamp])
437 *
438 * Send a "Last-Modified" header with a valid HTTP date.
439 *
440 * Accepts a unix timestamp, converts it to a valid HTTP date and
441 * sends it as "Last-Modified" HTTP header. If timestamp is
442 * omitted, the current time will be sent.
443 *
444 * Returns TRUE on success or FALSE on failure.
445 */
446 PHP_FUNCTION(http_send_last_modified)
447 {
448 long t = -1;
449
450 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
451 RETURN_FALSE;
452 }
453
454 if (t == -1) {
455 t = (long) HTTP_GET_REQUEST_TIME();
456 }
457
458 RETURN_SUCCESS(http_send_last_modified(t));
459 }
460 /* }}} */
461
462 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
463 *
464 * Send the Content-Type of the sent entity. This is particularly important
465 * if you use the http_send() API.
466 *
467 * Accepts an optional string parameter containing the desired content type
468 * (primary/secondary).
469 *
470 * Returns TRUE on success or FALSE on failure.
471 */
472 PHP_FUNCTION(http_send_content_type)
473 {
474 char *ct = "application/x-octetstream";
475 int ct_len = lenof("application/x-octetstream");
476
477 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
478 RETURN_FALSE;
479 }
480
481 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
482 }
483 /* }}} */
484
485 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
486 *
487 * Send the Content-Disposition. The Content-Disposition header is very useful
488 * if the data actually sent came from a file or something similar, that should
489 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
490 *
491 * Expects a string parameter specifying the file name the "Save as..." dialog
492 * should display. Optionally accepts a bool parameter, which, if set to true
493 * and the user agent knows how to handle the content type, will probably not
494 * cause the popup window to be shown.
495 *
496 * Returns TRUE on success or FALSE on failure.
497 */
498 PHP_FUNCTION(http_send_content_disposition)
499 {
500 char *filename;
501 int f_len;
502 zend_bool send_inline = 0;
503
504 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
505 RETURN_FALSE;
506 }
507 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
508 }
509 /* }}} */
510
511 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
512 *
513 * Matches the given unix timestamp against the clients "If-Modified-Since"
514 * resp. "If-Unmodified-Since" HTTP headers.
515 *
516 * Accepts a unix timestamp which should be matched. Optionally accepts an
517 * additional bool parameter, which if set to true will check the header
518 * usually used to validate HTTP ranges. If timestamp is omitted, the
519 * current time will be used.
520 *
521 * Returns TRUE if timestamp represents an earlier date than the header,
522 * else FALSE.
523 */
524 PHP_FUNCTION(http_match_modified)
525 {
526 long t = -1;
527 zend_bool for_range = 0;
528
529 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
530 RETURN_FALSE;
531 }
532
533 // current time if not supplied (senseless though)
534 if (t == -1) {
535 t = (long) HTTP_GET_REQUEST_TIME();
536 }
537
538 if (for_range) {
539 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t));
540 }
541 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t));
542 }
543 /* }}} */
544
545 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
546 *
547 * Matches the given ETag against the clients "If-Match" resp.
548 * "If-None-Match" HTTP headers.
549 *
550 * Expects a string parameter containing the ETag to compare. Optionally
551 * accepts a bool parameter, which, if set to true, will check the header
552 * usually used to validate HTTP ranges.
553 *
554 * Returns TRUE if ETag matches or the header contained the asterisk ("*"),
555 * else FALSE.
556 */
557 PHP_FUNCTION(http_match_etag)
558 {
559 int etag_len;
560 char *etag;
561 zend_bool for_range = 0;
562
563 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
564 RETURN_FALSE;
565 }
566
567 if (for_range) {
568 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag));
569 }
570 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag));
571 }
572 /* }}} */
573
574 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
575 *
576 * Attempts to cache the sent entity by its last modification date.
577 *
578 * Accepts a unix timestamp as parameter which is handled as follows:
579 *
580 * If timestamp_or_expires is greater than 0, it is handled as timestamp
581 * and will be sent as date of last modification. If it is 0 or omitted,
582 * the current time will be sent as Last-Modified date. If it's negative,
583 * it is handled as expiration time in seconds, which means that if the
584 * requested last modification date is not between the calculated timespan,
585 * the Last-Modified header is updated and the actual body will be sent.
586 *
587 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
588 *
589 * A log entry will be written to the cache log if the INI entry
590 * http.cache_log is set and the cache attempt was successful.
591 */
592 PHP_FUNCTION(http_cache_last_modified)
593 {
594 long last_modified = 0, send_modified = 0, t;
595 zval *zlm;
596
597 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
598 RETURN_FALSE;
599 }
600
601 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
602
603 t = (long) HTTP_GET_REQUEST_TIME();
604
605 /* 0 or omitted */
606 if (!last_modified) {
607 /* does the client have? (att: caching "forever") */
608 if ((zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE"))) {
609 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
610 /* send current time */
611 } else {
612 send_modified = t;
613 }
614 /* negative value is supposed to be expiration time */
615 } else if (last_modified < 0) {
616 last_modified += t;
617 send_modified = t;
618 /* send supplied time explicitly */
619 } else {
620 send_modified = last_modified;
621 }
622
623 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
624 }
625 /* }}} */
626
627 /* {{{ proto bool http_cache_etag([string etag])
628 *
629 * Attempts to cache the sent entity by its ETag, either supplied or generated
630 * by the hash algorithm specified by the INI setting "http.etag_mode".
631 *
632 * If the clients "If-None-Match" header matches the supplied/calculated
633 * ETag, the body is considered cached on the clients side and
634 * a "304 Not Modified" status code is issued.
635 *
636 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
637 *
638 * A log entry is written to the cache log if the INI entry
639 * "http.cache_log" is set and the cache attempt was successful.
640 */
641 PHP_FUNCTION(http_cache_etag)
642 {
643 char *etag = NULL;
644 int etag_len = 0;
645
646 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
647 RETURN_FALSE;
648 }
649
650 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
651
652 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
653 }
654 /* }}} */
655
656 /* {{{ proto string ob_etaghandler(string data, int mode)
657 *
658 * For use with ob_start(). Output buffer handler generating an ETag with
659 * the hash algorithm specified with the INI setting "http.etag_mode".
660 */
661 PHP_FUNCTION(ob_etaghandler)
662 {
663 char *data;
664 int data_len;
665 long mode;
666
667 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
668 RETURN_FALSE;
669 }
670
671 Z_TYPE_P(return_value) = IS_STRING;
672 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
673 }
674 /* }}} */
675
676 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
677 *
678 * Sets the throttle delay and send buffer size for use with http_send() API.
679 * Provides a basic throttling mechanism, which will yield the current process
680 * resp. thread until the entity has been completely sent, though.
681 *
682 * Expects a double parameter specifying the seconds too sleep() after
683 * each chunk sent. Additionally accepts an optional int parameter
684 * representing the chunk size in bytes.
685 *
686 * Example:
687 * <pre>
688 * <?php
689 * // ~ 20 kbyte/s
690 * # http_throttle(1, 20000);
691 * # http_throttle(0.5, 10000);
692 * # http_throttle(0.1, 2000);
693 * http_send_file('document.pdf');
694 * ?>
695 * </pre>
696 */
697 PHP_FUNCTION(http_throttle)
698 {
699 long chunk_size = HTTP_SENDBUF_SIZE;
700 double interval;
701
702 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|l", &interval, &chunk_size)) {
703 return;
704 }
705
706 HTTP_G->send.throttle_delay = interval;
707 HTTP_G->send.buffer_size = chunk_size;
708 }
709 /* }}} */
710
711 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
712 *
713 * Redirect to the given url.
714 *
715 * The supplied url will be expanded with http_build_url(), the params array will
716 * be treated with http_build_query() and the session identification will be appended
717 * if session is true.
718 *
719 * The HTTP response code will be set according to status.
720 * You can use one of the following constants for convenience:
721 * - HTTP_REDIRECT 302 Found for GET/HEAD, else 303 See Other
722 * - HTTP_REDIRECT_PERM 301 Moved Permanently
723 * - HTTP_REDIRECT_FOUND 302 Found
724 * - HTTP_REDIRECT_POST 303 See Other
725 * - HTTP_REDIRECT_PROXY 305 Use Proxy
726 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
727 *
728 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
729 * for which redirect response code to use in which situation.
730 *
731 * To be RFC compliant, "Redirecting to <a>URL</a>." will be displayed,
732 * if the client doesn't redirect immediately, and the request method was
733 * another one than HEAD.
734 *
735 * Returns FALSE on failure, or *exits* on success.
736 *
737 * A log entry will be written to the redirect log, if the INI entry
738 * "http.redirect_log" is set and the redirect attempt was successful.
739 */
740 PHP_FUNCTION(http_redirect)
741 {
742 int url_len;
743 size_t query_len = 0;
744 zend_bool session = 0, free_params = 0;
745 zval *params = NULL;
746 long status = HTTP_REDIRECT;
747 char *query = NULL, *url = NULL, *URI, *LOC, *RED = NULL;
748
749 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bl", &url, &url_len, &params, &session, &status) != SUCCESS) {
750 RETURN_FALSE;
751 }
752
753 #ifdef HAVE_PHP_SESSION
754 /* append session info */
755 if (session) {
756 if (!params) {
757 free_params = 1;
758 MAKE_STD_ZVAL(params);
759 array_init(params);
760 }
761 if (PS(session_status) == php_session_active) {
762 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
763 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
764 }
765 }
766 }
767 #endif
768
769 /* treat params array with http_build_query() */
770 if (params) {
771 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
772 if (free_params) {
773 zval_dtor(params);
774 FREE_ZVAL(params);
775 }
776 if (query) {
777 efree(query);
778 }
779 RETURN_FALSE;
780 }
781 }
782
783 URI = http_absolute_url(url);
784
785 if (query_len) {
786 spprintf(&LOC, 0, "Location: %s?%s", URI, query);
787 if (status != 300) {
788 spprintf(&RED, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
789 }
790 } else {
791 spprintf(&LOC, 0, "Location: %s", URI);
792 if (status != 300) {
793 spprintf(&RED, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
794 }
795 }
796
797 efree(URI);
798 if (query) {
799 efree(query);
800 }
801 if (free_params) {
802 zval_dtor(params);
803 FREE_ZVAL(params);
804 }
805
806 switch (status)
807 {
808 case 300:
809 RETVAL_SUCCESS(http_send_status_header(status, LOC));
810 efree(LOC);
811 return;
812 break;
813
814 case HTTP_REDIRECT_PERM:
815 case HTTP_REDIRECT_FOUND:
816 case HTTP_REDIRECT_POST:
817 case HTTP_REDIRECT_PROXY:
818 case HTTP_REDIRECT_TEMP:
819 break;
820
821 case 306:
822 default:
823 http_error_ex(HE_NOTICE, HTTP_E_RUNTIME, "Unsupported redirection status code: %ld", status);
824 case HTTP_REDIRECT:
825 if ( SG(request_info).request_method &&
826 strcasecmp(SG(request_info).request_method, "HEAD") &&
827 strcasecmp(SG(request_info).request_method, "GET")) {
828 status = HTTP_REDIRECT_POST;
829 } else {
830 status = HTTP_REDIRECT_FOUND;
831 }
832 break;
833 }
834
835 RETURN_SUCCESS(http_exit_ex(status, LOC, RED, 1));
836 }
837 /* }}} */
838
839 /* {{{ proto bool http_send_data(string data)
840 *
841 * Sends raw data with support for (multiple) range requests.
842 *
843 * Returns TRUE on success, or FALSE on failure.
844 */
845 PHP_FUNCTION(http_send_data)
846 {
847 zval *zdata;
848
849 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
850 RETURN_FALSE;
851 }
852
853 convert_to_string_ex(&zdata);
854 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
855 }
856 /* }}} */
857
858 /* {{{ proto bool http_send_file(string file)
859 *
860 * Sends a file with support for (multiple) range requests.
861 *
862 * Expects a string parameter referencing the file to send.
863 *
864 * Returns TRUE on success, or FALSE on failure.
865 */
866 PHP_FUNCTION(http_send_file)
867 {
868 char *file;
869 int flen = 0;
870
871 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
872 RETURN_FALSE;
873 }
874 if (!flen) {
875 RETURN_FALSE;
876 }
877
878 RETURN_SUCCESS(http_send_file(file));
879 }
880 /* }}} */
881
882 /* {{{ proto bool http_send_stream(resource stream)
883 *
884 * Sends an already opened stream with support for (multiple) range requests.
885 *
886 * Expects a resource parameter referencing the stream to read from.
887 *
888 * Returns TRUE on success, or FALSE on failure.
889 */
890 PHP_FUNCTION(http_send_stream)
891 {
892 zval *zstream;
893 php_stream *file;
894
895 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
896 RETURN_FALSE;
897 }
898
899 php_stream_from_zval(file, &zstream);
900 RETURN_SUCCESS(http_send_stream(file));
901 }
902 /* }}} */
903
904 /* {{{ proto string http_chunked_decode(string encoded)
905 *
906 * Decodes a string that was HTTP-chunked encoded.
907 *
908 * Expects a chunked encoded string as parameter.
909 *
910 * Returns the decoded string on success or FALSE on failure.
911 */
912 PHP_FUNCTION(http_chunked_decode)
913 {
914 char *encoded = NULL, *decoded = NULL;
915 size_t decoded_len = 0;
916 int encoded_len = 0;
917
918 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
919 RETURN_FALSE;
920 }
921
922 if (NULL != http_encoding_dechunk(encoded, encoded_len, &decoded, &decoded_len)) {
923 RETURN_STRINGL(decoded, (int) decoded_len, 0);
924 } else {
925 RETURN_FALSE;
926 }
927 }
928 /* }}} */
929
930 /* {{{ proto object http_parse_message(string message)
931 *
932 * Parses (a) http_message(s) into a simple recursive object structure.
933 *
934 * Expects a string parameter containing a single HTTP message or
935 * several consecutive HTTP messages.
936 *
937 * Returns an hierarchical object structure of the parsed messages.
938 *
939 * Example:
940 * <pre>
941 * <?php
942 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
943 *
944 * stdClass object
945 * (
946 * [type] => 2
947 * [httpVersion] => 1.1
948 * [responseCode] => 200
949 * [headers] => Array
950 * (
951 * [Content-Length] => 3
952 * [Server] => Apache
953 * )
954 * [body] => Hi!
955 * [parentMessage] => stdClass object
956 * (
957 * [type] => 2
958 * [httpVersion] => 1.1
959 * [responseCode] => 302
960 * [headers] => Array
961 * (
962 * [Content-Length] => 0
963 * [Location] => ...
964 * )
965 * [body] =>
966 * [parentMessage] => ...
967 * )
968 * )
969 * ?>
970 * </pre>
971 */
972 PHP_FUNCTION(http_parse_message)
973 {
974 char *message;
975 int message_len;
976 http_message *msg = NULL;
977
978 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &message, &message_len)) {
979 RETURN_NULL();
980 }
981
982 if ((msg = http_message_parse(message, message_len))) {
983 object_init(return_value);
984 http_message_tostruct_recursive(msg, return_value);
985 http_message_free(&msg);
986 } else {
987 RETURN_NULL();
988 }
989 }
990 /* }}} */
991
992 /* {{{ proto array http_parse_headers(string header)
993 *
994 * Parses HTTP headers into an associative array.
995 *
996 * Expects a string parameter containing HTTP headers.
997 *
998 * Returns an array on success, or FALSE on failure.
999 *
1000 * Example:
1001 * <pre>
1002 * <?php
1003 * $headers = "content-type: text/html; charset=UTF-8\r\n".
1004 * "Server: Funky/1.0\r\n".
1005 * "Set-Cookie: foo=bar\r\n".
1006 * "Set-Cookie: baz=quux\r\n".
1007 * "Folded: works\r\n\ttoo\r\n";
1008 * print_r(http_parse_headers($headers));
1009 *
1010 * Array
1011 * (
1012 * [Content-Type] => text/html; chatset=UTF-8
1013 * [Server] => Funky/1.0
1014 * [Set-Cookie] => Array
1015 * (
1016 * [0] => foo=bar
1017 * [1] => baz=quux
1018 * )
1019 * [Folded] => works
1020 * too
1021 * )
1022 * ?>
1023 * </pre>
1024 */
1025 PHP_FUNCTION(http_parse_headers)
1026 {
1027 char *header;
1028 int header_len;
1029
1030 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
1031 RETURN_FALSE;
1032 }
1033
1034 array_init(return_value);
1035 if (SUCCESS != http_parse_headers(header, return_value)) {
1036 zval_dtor(return_value);
1037 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Failed to parse headers");
1038 RETURN_FALSE;
1039 }
1040 }
1041 /* }}}*/
1042
1043 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
1044 *
1045 * Parses HTTP cookies like sent in a response into a struct.
1046 *
1047 * Expects a string as parameter containing the value of a Set-Cookie response header.
1048 *
1049 * Returns an stdClass olike shown in the example on success or FALSE on failure.
1050 *
1051 * Example:
1052 * <pre>
1053 * <?php
1054 * print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
1055 *
1056 * stdClass Object
1057 * (
1058 * [cookies] => Array
1059 * (
1060 * [foo] => bar
1061 * [bar] => baz
1062 * )
1063 *
1064 * [extras] => Array
1065 * (
1066 * [comment] =>
1067 * )
1068 *
1069 * [flags] => 16
1070 * [expires] => 0
1071 * [path] => /
1072 * [domain] => example.com
1073 * )
1074 * ?>
1075 * </pre>
1076 */
1077 PHP_FUNCTION(http_parse_cookie)
1078 {
1079 char *cookie, **allowed_extras = NULL;
1080 int i = 0, cookie_len;
1081 long flags = 0;
1082 zval *allowed_extras_array = NULL, **entry = NULL;
1083 HashPosition pos;
1084 http_cookie_list list;
1085
1086 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la", &cookie, &cookie_len, &flags, &allowed_extras_array)) {
1087 RETURN_FALSE;
1088 }
1089
1090 if (allowed_extras_array) {
1091 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1092 FOREACH_VAL(pos, allowed_extras_array, entry) {
1093 ZVAL_ADDREF(*entry);
1094 convert_to_string_ex(entry);
1095 allowed_extras[i] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
1096 zval_ptr_dtor(entry);
1097 }
1098 }
1099
1100 if (http_parse_cookie_ex(&list, cookie, flags, allowed_extras)) {
1101 object_init(return_value);
1102 http_cookie_list_tostruct(&list, return_value);
1103 http_cookie_list_dtor(&list);
1104 } else {
1105 RETVAL_FALSE;
1106 }
1107
1108 if (allowed_extras) {
1109 for (i = 0; allowed_extras[i]; ++i) {
1110 efree(allowed_extras[i]);
1111 }
1112 efree(allowed_extras);
1113 }
1114 }
1115
1116 /* {{{ proto array http_get_request_headers(void)
1117 *
1118 * Get a list of incoming HTTP headers.
1119 *
1120 * Returns an associative array of incoming request headers.
1121 */
1122 PHP_FUNCTION(http_get_request_headers)
1123 {
1124 NO_ARGS;
1125
1126 array_init(return_value);
1127 http_get_request_headers(return_value);
1128 }
1129 /* }}} */
1130
1131 /* {{{ proto string http_get_request_body(void)
1132 *
1133 * Get the raw request body (e.g. POST or PUT data).
1134 *
1135 * This function can not be used after http_get_request_body_stream()
1136 * if the request method was another than POST.
1137 *
1138 * Returns the raw request body as string on success or NULL on failure.
1139 */
1140 PHP_FUNCTION(http_get_request_body)
1141 {
1142 char *body;
1143 size_t length;
1144
1145 NO_ARGS;
1146
1147 if (SUCCESS == http_get_request_body(&body, &length)) {
1148 RETURN_STRINGL(body, (int) length, 0);
1149 } else {
1150 RETURN_NULL();
1151 }
1152 }
1153 /* }}} */
1154
1155 /* {{{ proto resource http_get_request_body_stream(void)
1156 *
1157 * Create a stream to read the raw request body (e.g. POST or PUT data).
1158 *
1159 * This function can only be used once if the request method was another than POST.
1160 *
1161 * Returns the raw request body as stream on success or NULL on failure.
1162 */
1163 PHP_FUNCTION(http_get_request_body_stream)
1164 {
1165 php_stream *s;
1166
1167 NO_ARGS;
1168
1169 if ((s = http_get_request_body_stream())) {
1170 php_stream_to_zval(s, return_value);
1171 } else {
1172 http_error(HE_WARNING, HTTP_E_RUNTIME, "Failed to create request body stream");
1173 RETURN_NULL();
1174 }
1175 }
1176 /* }}} */
1177
1178 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
1179 *
1180 * Match an incoming HTTP header.
1181 *
1182 * Expects two string parameters representing the header name (case-insensitive)
1183 * and the header value that should be compared. The case sensitivity of the
1184 * header value depends on the additional optional bool parameter accepted.
1185 *
1186 * Returns TRUE if header value matches, else FALSE.
1187 */
1188 PHP_FUNCTION(http_match_request_header)
1189 {
1190 char *header, *value;
1191 int header_len, value_len;
1192 zend_bool match_case = 0;
1193
1194 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
1195 RETURN_FALSE;
1196 }
1197
1198 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
1199 }
1200 /* }}} */
1201
1202 /* {{{ HAVE_CURL */
1203 #ifdef HTTP_HAVE_CURL
1204
1205 #define RETVAL_RESPONSE_OR_BODY(request) \
1206 { \
1207 zval **bodyonly; \
1208 \
1209 /* check if only the body should be returned */ \
1210 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
1211 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1212 \
1213 if (msg) { \
1214 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1215 http_message_free(&msg); \
1216 } \
1217 } else { \
1218 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1219 } \
1220 }
1221
1222 /* {{{ proto string http_get(string url[, array options[, array &info]])
1223 *
1224 * Performs an HTTP GET request on the supplied url.
1225 *
1226 * The second parameter, if set, is expected to be an associative
1227 * array where the following keys will be recognized:
1228 * <pre>
1229 * - redirect: int, whether and how many redirects to follow
1230 * - unrestrictedauth: bool, whether to continue sending credentials on
1231 * redirects to a different host
1232 * - proxyhost: string, proxy host in "host[:port]" format
1233 * - proxyport: int, use another proxy port as specified in proxyhost
1234 * - proxytype: int, HTTP_PROXY_HTTP, SOCKS4 or SOCKS5
1235 * - proxyauth: string, proxy credentials in "user:pass" format
1236 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1237 * - httpauth: string, http credentials in "user:pass" format
1238 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1239 * - compress: bool, whether to allow gzip/deflate content encoding
1240 * - port: int, use another port as specified in the url
1241 * - referer: string, the referer to send
1242 * - useragent: string, the user agent to send
1243 * (defaults to PECL::HTTP/version (PHP/version)))
1244 * - headers: array, list of custom headers as associative array
1245 * like array("header" => "value")
1246 * - cookies: array, list of cookies as associative array
1247 * like array("cookie" => "value")
1248 * - encodecookies: bool, whether to urlencode the cookies (default: true)
1249 * - resetcookies: bool, wheter to reset the cookies
1250 * - cookiestore: string, path to a file where cookies are/will be stored
1251 * - cookiesession: bool, accept (true) or reset (false) sessioncookies
1252 * - resume: int, byte offset to start the download from;
1253 * if the server supports ranges
1254 * - range: array, array of arrays, each containing two integers,
1255 * specifying the ranges to download if server support is
1256 * given; only recognized if the resume option is empty
1257 * - maxfilesize: int, maximum file size that should be downloaded;
1258 * has no effect, if the size of the requested entity is not known
1259 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1260 * - etag: string, quoted etag for If-(None-)Match header
1261 * - timeout: int, seconds the request may take
1262 * - connecttimeout: int, seconds the connect may take
1263 * - onprogress: mixed, progress callback
1264 * - interface: string, outgoing network interface (ifname, ip or hostname)
1265 * - portrange: array, 2 integers specifying outgoing portrange to try
1266 * - ssl: array, with the following options:
1267 * cert: string, path to certificate
1268 * certtype: string, type of certificate
1269 * certpasswd: string, password for certificate
1270 * key: string, path to key
1271 * keytype: string, type of key
1272 * keypasswd: string, pasword for key
1273 * engine: string, ssl engine to use
1274 * version: int, ssl version to use
1275 * verifypeer: bool, whether to verify the peer
1276 * verifyhost: bool whether to verify the host
1277 * cipher_list: string, list of allowed ciphers
1278 * cainfo: string
1279 * capath: string
1280 * random_file: string
1281 * egdsocket: string
1282 * </pre>
1283 *
1284 * The optional third parameter will be filled with some additional information
1285 * in form of an associative array, if supplied, like the following example:
1286 * <pre>
1287 * <?php
1288 * array (
1289 * 'effective_url' => 'http://www.example.com/',
1290 * 'response_code' => 302,
1291 * 'connect_code' => 0,
1292 * 'filetime' => -1,
1293 * 'total_time' => 0.212348,
1294 * 'namelookup_time' => 0.038296,
1295 * 'connect_time' => 0.104144,
1296 * 'pretransfer_time' => 0.104307,
1297 * 'starttransfer_time' => 0.212077,
1298 * 'redirect_time' => 0,
1299 * 'redirect_count' => 0,
1300 * 'size_upload' => 0,
1301 * 'size_download' => 218,
1302 * 'speed_download' => 1026,
1303 * 'speed_upload' => 0,
1304 * 'header_size' => 307,
1305 * 'request_size' => 103,
1306 * 'ssl_verifyresult' => 0,
1307 * 'ssl_engines' =>
1308 * array (
1309 * 0 => 'dynamic',
1310 * 1 => 'cswift',
1311 * 2 => 'chil',
1312 * 3 => 'atalla',
1313 * 4 => 'nuron',
1314 * 5 => 'ubsec',
1315 * 6 => 'aep',
1316 * 7 => 'sureware',
1317 * 8 => '4758cca',
1318 * ),
1319 * 'content_length_download' => 218,
1320 * 'content_length_upload' => 0,
1321 * 'content_type' => 'text/html',
1322 * 'httpauth_avail' => 0,
1323 * 'proxyauth_avail' => 0,
1324 * 'num_connects' => 1,
1325 * 'os_errno' => 0,
1326 * 'error' => '',
1327 * )
1328 * ?>
1329 * </pre>
1330 *
1331 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1332 */
1333 PHP_FUNCTION(http_get)
1334 {
1335 zval *options = NULL, *info = NULL;
1336 char *URL;
1337 int URL_len;
1338 http_request request;
1339
1340 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1341 RETURN_FALSE;
1342 }
1343
1344 if (info) {
1345 zval_dtor(info);
1346 array_init(info);
1347 }
1348
1349 RETVAL_FALSE;
1350
1351 http_request_init_ex(&request, NULL, HTTP_GET, URL);
1352 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1353 http_request_exec(&request);
1354 if (info) {
1355 http_request_info(&request, Z_ARRVAL_P(info));
1356 }
1357 RETVAL_RESPONSE_OR_BODY(request);
1358 }
1359 http_request_dtor(&request);
1360 }
1361 /* }}} */
1362
1363 /* {{{ proto string http_head(string url[, array options[, array &info]])
1364 *
1365 * Performs an HTTP HEAD request on the supplied url.
1366 *
1367 * See http_get() for a full list of available parameters and options.
1368 *
1369 * Returns the HTTP response as string on success, or FALSE on failure.
1370 */
1371 PHP_FUNCTION(http_head)
1372 {
1373 zval *options = NULL, *info = NULL;
1374 char *URL;
1375 int URL_len;
1376 http_request request;
1377
1378 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1379 RETURN_FALSE;
1380 }
1381
1382 if (info) {
1383 zval_dtor(info);
1384 array_init(info);
1385 }
1386
1387 RETVAL_FALSE;
1388
1389 http_request_init_ex(&request, NULL, HTTP_HEAD, URL);
1390 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1391 http_request_exec(&request);
1392 if (info) {
1393 http_request_info(&request, Z_ARRVAL_P(info));
1394 }
1395 RETVAL_RESPONSE_OR_BODY(request);
1396 }
1397 http_request_dtor(&request);
1398 }
1399 /* }}} */
1400
1401 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1402 *
1403 * Performs an HTTP POST request on the supplied url.
1404 *
1405 * Expects a string as second parameter containing the pre-encoded post data.
1406 * See http_get() for a full list of available parameters and options.
1407 *
1408 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1409 */
1410 PHP_FUNCTION(http_post_data)
1411 {
1412 zval *options = NULL, *info = NULL;
1413 char *URL, *postdata;
1414 int postdata_len, URL_len;
1415 http_request_body body;
1416 http_request request;
1417
1418 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1419 RETURN_FALSE;
1420 }
1421
1422 if (info) {
1423 zval_dtor(info);
1424 array_init(info);
1425 }
1426
1427 RETVAL_FALSE;
1428
1429 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1430 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, postdata, postdata_len, 0);
1431 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1432 http_request_exec(&request);
1433 if (info) {
1434 http_request_info(&request, Z_ARRVAL_P(info));
1435 }
1436 RETVAL_RESPONSE_OR_BODY(request);
1437 }
1438 http_request_dtor(&request);
1439 }
1440 /* }}} */
1441
1442 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1443 *
1444 * Performs an HTTP POST request on the supplied url.
1445 *
1446 * Expects an associative array as second parameter, which will be
1447 * www-form-urlencoded. See http_get() for a full list of available options.
1448 *
1449 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1450 */
1451 PHP_FUNCTION(http_post_fields)
1452 {
1453 zval *options = NULL, *info = NULL, *fields, *files = NULL;
1454 char *URL;
1455 int URL_len;
1456 http_request_body body;
1457 http_request request;
1458
1459 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
1460 RETURN_FALSE;
1461 }
1462
1463 if (!http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
1464 RETURN_FALSE;
1465 }
1466
1467 if (info) {
1468 zval_dtor(info);
1469 array_init(info);
1470 }
1471
1472 RETVAL_FALSE;
1473
1474 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1475 request.body = &body;
1476 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1477 http_request_exec(&request);
1478 if (info) {
1479 http_request_info(&request, Z_ARRVAL_P(info));
1480 }
1481 RETVAL_RESPONSE_OR_BODY(request);
1482 }
1483 http_request_dtor(&request);
1484 }
1485 /* }}} */
1486
1487 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1488 *
1489 * Performs an HTTP PUT request on the supplied url.
1490 *
1491 * Expects the second parameter to be a string referencing the file to upload.
1492 * See http_get() for a full list of available options.
1493 *
1494 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1495 */
1496 PHP_FUNCTION(http_put_file)
1497 {
1498 char *URL, *file;
1499 int URL_len, f_len;
1500 zval *options = NULL, *info = NULL;
1501 php_stream *stream;
1502 php_stream_statbuf ssb;
1503 http_request_body body;
1504 http_request request;
1505
1506 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1507 RETURN_FALSE;
1508 }
1509
1510 if (!(stream = php_stream_open_wrapper_ex(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT))) {
1511 RETURN_FALSE;
1512 }
1513 if (php_stream_stat(stream, &ssb)) {
1514 php_stream_close(stream);
1515 RETURN_FALSE;
1516 }
1517
1518 if (info) {
1519 zval_dtor(info);
1520 array_init(info);
1521 }
1522
1523 RETVAL_FALSE;
1524
1525 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1526 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
1527 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1528 http_request_exec(&request);
1529 if (info) {
1530 http_request_info(&request, Z_ARRVAL_P(info));
1531 }
1532 RETVAL_RESPONSE_OR_BODY(request);
1533 }
1534 http_request_dtor(&request);
1535 }
1536 /* }}} */
1537
1538 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1539 *
1540 * Performs an HTTP PUT request on the supplied url.
1541 *
1542 * Expects the second parameter to be a resource referencing an already
1543 * opened stream, from which the data to upload should be read.
1544 * See http_get() for a full list of available options.
1545 *
1546 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1547 */
1548 PHP_FUNCTION(http_put_stream)
1549 {
1550 zval *resource, *options = NULL, *info = NULL;
1551 char *URL;
1552 int URL_len;
1553 php_stream *stream;
1554 php_stream_statbuf ssb;
1555 http_request_body body;
1556 http_request request;
1557
1558 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1559 RETURN_FALSE;
1560 }
1561
1562 php_stream_from_zval(stream, &resource);
1563 if (php_stream_stat(stream, &ssb)) {
1564 RETURN_FALSE;
1565 }
1566
1567 if (info) {
1568 zval_dtor(info);
1569 array_init(info);
1570 }
1571
1572 RETVAL_FALSE;
1573
1574 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1575 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 0);
1576 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1577 http_request_exec(&request);
1578 if (info) {
1579 http_request_info(&request, Z_ARRVAL_P(info));
1580 }
1581 RETVAL_RESPONSE_OR_BODY(request);
1582 }
1583 http_request_dtor(&request);
1584 }
1585 /* }}} */
1586
1587 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1588 *
1589 * Performs an HTTP PUT request on the supplied url.
1590 *
1591 * Expects the second parameter to be a string containing the data to upload.
1592 * See http_get() for a full list of available options.
1593 *
1594 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1595 */
1596 PHP_FUNCTION(http_put_data)
1597 {
1598 char *URL, *data;
1599 int URL_len, data_len;
1600 zval *options = NULL, *info = NULL;
1601 http_request_body body;
1602 http_request request;
1603
1604 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &data, &data_len, &options, &info)) {
1605 RETURN_FALSE;
1606 }
1607
1608 if (info) {
1609 zval_dtor(info);
1610 array_init(info);
1611 }
1612
1613 RETVAL_FALSE;
1614
1615 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1616 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, data, data_len, 0);
1617 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1618 http_request_exec(&request);
1619 if (info) {
1620 http_request_info(&request, Z_ARRVAL_P(info));
1621 }
1622 RETVAL_RESPONSE_OR_BODY(request);
1623 }
1624 http_request_dtor(&request);
1625 }
1626 /* }}} */
1627
1628 /* {{{ proto string http_request(int method, string url[, string body[, array options[, array &info]]])
1629 *
1630 * Performs a custom HTTP request on the supplied url.
1631 *
1632 * Expects the first parameter to be an integer specifying the request method to use.
1633 * Accepts an optional third string parameter containing the raw request body.
1634 * See http_get() for a full list of available options.
1635 *
1636 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1637 */
1638 PHP_FUNCTION(http_request)
1639 {
1640 long meth;
1641 char *URL, *data = NULL;
1642 int URL_len, data_len = 0;
1643 zval *options = NULL, *info = NULL;
1644 http_request_body body;
1645 http_request request;
1646
1647 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|sa/!z", &meth, &URL, &URL_len, &data, &data_len, &options, &info)) {
1648 RETURN_FALSE;
1649 }
1650
1651 if (info) {
1652 zval_dtor(info);
1653 array_init(info);
1654 }
1655
1656 RETVAL_FALSE;
1657
1658 http_request_init_ex(&request, NULL, meth, URL);
1659 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, data, data_len, 0);
1660 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1661 http_request_exec(&request);
1662 if (info) {
1663 http_request_info(&request, Z_ARRVAL_P(info));
1664 }
1665 RETVAL_RESPONSE_OR_BODY(request);
1666 }
1667 http_request_dtor(&request);
1668 }
1669 /* }}} */
1670
1671 /* {{{ proto string http_request_body_encode(array fields, array files)
1672 *
1673 * Generate x-www-form-urlencoded resp. form-data encoded request body.
1674 *
1675 * Returns encoded string on success, or FALSE on failure.
1676 */
1677 static char *file_get_contents(char *file, size_t *len TSRMLS_DC)
1678 {
1679 php_stream *s = NULL;
1680 char *buf = NULL;
1681
1682 if ((s = php_stream_open_wrapper_ex(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT))) {
1683 *len = php_stream_copy_to_mem(s, &buf, (size_t) -1, 0);
1684 php_stream_close(s);
1685 } else {
1686 *len = 0;
1687 }
1688 return buf;
1689 }
1690 struct FormData {
1691 struct FormData *next;
1692 int type;
1693 char *line;
1694 size_t length;
1695 };
1696 CURLcode Curl_getFormData(struct FormData **, struct curl_httppost *post, curl_off_t *size);
1697 PHP_FUNCTION(http_request_body_encode)
1698 {
1699 zval *fields = NULL, *files = NULL;
1700 HashTable *fields_ht, *files_ht;
1701 http_request_body body;
1702 phpstr rbuf;
1703 struct FormData *data, *ptr;
1704 curl_off_t size;
1705 char *fdata = NULL;
1706 size_t fsize = 0;
1707 CURLcode rc;
1708 int fgc_error = 0;
1709
1710 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!", &fields, &files)) {
1711 RETURN_FALSE;
1712 }
1713
1714 fields_ht = (fields && Z_TYPE_P(fields) == IS_ARRAY) ? Z_ARRVAL_P(fields) : NULL;
1715 files_ht = (files && Z_TYPE_P(files) == IS_ARRAY) ? Z_ARRVAL_P(files) : NULL;
1716 if (!http_request_body_fill(&body, fields_ht, files_ht)) {
1717 RETURN_FALSE;
1718 }
1719
1720 switch (body.type)
1721 {
1722 case HTTP_REQUEST_BODY_CURLPOST:
1723 if (CURLE_OK != (rc = Curl_getFormData(&data, body.data, &size))) {
1724 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Could not encode request body: %s", curl_easy_strerror(rc));
1725 RETVAL_FALSE;
1726 } else {
1727 phpstr_init_ex(&rbuf, (size_t) size, PHPSTR_INIT_PREALLOC);
1728 for (ptr = data; ptr; ptr = ptr->next) {
1729 if (!fgc_error) {
1730 if (ptr->type) {
1731 if ((fdata = file_get_contents(ptr->line, &fsize TSRMLS_CC))) {
1732 phpstr_append(&rbuf, fdata, fsize);
1733 efree(fdata);
1734 } else {
1735 fgc_error = 1;
1736 }
1737 } else {
1738 phpstr_append(&rbuf, ptr->line, ptr->length);
1739 }
1740 }
1741 curl_free(ptr->line);
1742 }
1743 curl_free(data);
1744 if (fgc_error) {
1745 phpstr_dtor(&rbuf);
1746 RETVAL_FALSE;
1747 } else {
1748 RETVAL_PHPSTR_VAL(&rbuf);
1749 }
1750 }
1751 http_request_body_dtor(&body);
1752 break;
1753
1754 case HTTP_REQUEST_BODY_CSTRING:
1755 RETVAL_STRINGL(body.data, body.size, 0);
1756 break;
1757
1758 default:
1759 http_request_body_dtor(&body);
1760 RETVAL_FALSE;
1761 break;
1762 }
1763 }
1764 #endif /* HTTP_HAVE_CURL */
1765 /* }}} HAVE_CURL */
1766
1767 /* {{{ proto int http_request_method_register(string method)
1768 *
1769 * Register a custom request method.
1770 *
1771 * Expects a string parameter containing the request method name to register.
1772 *
1773 * Returns the ID of the request method on success, or FALSE on failure.
1774 */
1775 PHP_FUNCTION(http_request_method_register)
1776 {
1777 char *method;
1778 int method_len;
1779 ulong existing;
1780
1781 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1782 RETURN_FALSE;
1783 }
1784 if ((existing = http_request_method_exists(1, 0, method))) {
1785 RETURN_LONG((long) existing);
1786 }
1787
1788 RETVAL_LONG((long) http_request_method_register(method, method_len));
1789 }
1790 /* }}} */
1791
1792 /* {{{ proto bool http_request_method_unregister(mixed method)
1793 *
1794 * Unregister a previously registered custom request method.
1795 *
1796 * Expects either the request method name or ID.
1797 *
1798 * Returns TRUE on success, or FALSE on failure.
1799 */
1800 PHP_FUNCTION(http_request_method_unregister)
1801 {
1802 zval *method;
1803
1804 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1805 RETURN_FALSE;
1806 }
1807
1808 switch (Z_TYPE_P(method))
1809 {
1810 case IS_OBJECT:
1811 convert_to_string(method);
1812 case IS_STRING:
1813 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1814 convert_to_long(method);
1815 } else {
1816 int mn;
1817 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1818 RETURN_FALSE;
1819 }
1820 zval_dtor(method);
1821 ZVAL_LONG(method, (long)mn);
1822 }
1823 case IS_LONG:
1824 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1825 default:
1826 RETURN_FALSE;
1827 }
1828 }
1829 /* }}} */
1830
1831 /* {{{ proto int http_request_method_exists(mixed method)
1832 *
1833 * Check if a request method is registered (or available by default).
1834 *
1835 * Expects either the request method name or ID as parameter.
1836 *
1837 * Returns TRUE if the request method is known, else FALSE.
1838 */
1839 PHP_FUNCTION(http_request_method_exists)
1840 {
1841 IF_RETVAL_USED {
1842 zval *method;
1843
1844 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1845 RETURN_FALSE;
1846 }
1847
1848 switch (Z_TYPE_P(method))
1849 {
1850 case IS_OBJECT:
1851 convert_to_string(method);
1852 case IS_STRING:
1853 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1854 convert_to_long(method);
1855 } else {
1856 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1857 }
1858 case IS_LONG:
1859 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method), NULL));
1860 default:
1861 RETURN_FALSE;
1862 }
1863 }
1864 }
1865 /* }}} */
1866
1867 /* {{{ proto string http_request_method_name(int method)
1868 *
1869 * Get the literal string representation of a standard or registered request method.
1870 *
1871 * Expects the request method ID as parameter.
1872 *
1873 * Returns the request method name as string on success, or FALSE on failure.
1874 */
1875 PHP_FUNCTION(http_request_method_name)
1876 {
1877 IF_RETVAL_USED {
1878 long method;
1879
1880 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1881 RETURN_FALSE;
1882 }
1883
1884 RETURN_STRING(estrdup(http_request_method_name((int) method)), 0);
1885 }
1886 }
1887 /* }}} */
1888
1889 /* {{{ */
1890 #ifdef HTTP_HAVE_ZLIB
1891
1892 /* {{{ proto string http_deflate(string data[, int flags = 0])
1893 *
1894 * Compress data with gzip, zlib AKA deflate or raw deflate encoding.
1895 *
1896 * Expects the first parameter to be a string containing the data that should
1897 * be encoded.
1898 *
1899 * Returns the encoded string on success, or NULL on failure.
1900 */
1901 PHP_FUNCTION(http_deflate)
1902 {
1903 char *data;
1904 int data_len;
1905 long flags = 0;
1906
1907 RETVAL_NULL();
1908
1909 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &flags)) {
1910 char *encoded;
1911 size_t encoded_len;
1912
1913 if (SUCCESS == http_encoding_deflate(flags, data, data_len, &encoded, &encoded_len)) {
1914 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1915 }
1916 }
1917 }
1918 /* }}} */
1919
1920 /* {{{ proto string http_inflate(string data)
1921 *
1922 * Decompress data compressed with either gzip, deflate AKA zlib or raw
1923 * deflate encoding.
1924 *
1925 * Expects a string as parameter containing the compressed data.
1926 *
1927 * Returns the decoded string on success, or NULL on failure.
1928 */
1929 PHP_FUNCTION(http_inflate)
1930 {
1931 char *data;
1932 int data_len;
1933
1934 RETVAL_NULL();
1935
1936 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1937 char *decoded;
1938 size_t decoded_len;
1939
1940 if (SUCCESS == http_encoding_inflate(data, data_len, &decoded, &decoded_len)) {
1941 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1942 }
1943 }
1944 }
1945 /* }}} */
1946
1947 /* {{{ proto string ob_deflatehandler(string data, int mode)
1948 *
1949 * For use with ob_start(). The deflate output buffer handler can only be used once.
1950 * It conflicts with ob_gzhandler and zlib.output_compression as well and should
1951 * not be used after ext/mbstrings mb_output_handler and ext/sessions URL-Rewriter (AKA
1952 * session.use_trans_sid).
1953 */
1954 PHP_FUNCTION(ob_deflatehandler)
1955 {
1956 char *data;
1957 int data_len;
1958 long mode;
1959
1960 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1961 RETURN_FALSE;
1962 }
1963
1964 http_ob_deflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1965 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1966 }
1967 /* }}} */
1968
1969 /* {{{ proto string ob_inflatehandler(string data, int mode)
1970 *
1971 * For use with ob_start(). Same restrictions as with ob_deflatehandler apply.
1972 */
1973 PHP_FUNCTION(ob_inflatehandler)
1974 {
1975 char *data;
1976 int data_len;
1977 long mode;
1978
1979 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1980 RETURN_FALSE;
1981 }
1982
1983 http_ob_inflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1984 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1985 }
1986 /* }}} */
1987
1988 #endif /* HTTP_HAVE_ZLIB */
1989 /* }}} */
1990
1991 /* {{{ proto int http_support([int feature = 0])
1992 *
1993 * Check for feature that require external libraries.
1994 *
1995 * Accepts an optional in parameter specifying which feature to probe for.
1996 * If the parameter is 0 or omitted, the return value contains a bitmask of
1997 * all supported features that depend on external libraries.
1998 *
1999 * Available features to probe for are:
2000 * <ul>
2001 * <li> HTTP_SUPPORT: always set
2002 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
2003 * and HTTP requests can be issued
2004 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
2005 * and SSL requests can be issued
2006 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
2007 * and compressed HTTP responses can be decoded
2008 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
2009 * and the HttpResponse::guessContentType() method is usable
2010 * </ul>
2011 *
2012 * Returns int, whether requested feature is supported, or a bitmask with
2013 * all supported features.
2014 */
2015 PHP_FUNCTION(http_support)
2016 {
2017 long feature = 0;
2018
2019 RETVAL_LONG(0L);
2020
2021 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &feature)) {
2022 RETVAL_LONG(http_support(feature));
2023 }
2024 }
2025 /* }}} */
2026
2027 PHP_FUNCTION(http_test)
2028 {
2029 }
2030
2031 /*
2032 * Local variables:
2033 * tab-width: 4
2034 * c-basic-offset: 4
2035 * End:
2036 * vim600: noet sw=4 ts=4 fdm=marker
2037 * vim<600: noet sw=4 ts=4
2038 */
2039