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