- get rid of 1001 strict-aliasing warnings
[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 RETURN_FALSE;
1038 }
1039 }
1040 /* }}}*/
1041
1042 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
1043 *
1044 * Parses HTTP cookies like sent in a response into a struct.
1045 *
1046 * Expects a string as parameter containing the value of a Set-Cookie response header.
1047 *
1048 * Returns an stdClass olike shown in the example on success or FALSE on failure.
1049 *
1050 * Example:
1051 * <pre>
1052 * <?php
1053 * print_r(http_parse_cookie("foo=bar; bar=baz; path=/; domain=example.com; comment=; secure", 0, array("comment")));
1054 *
1055 * stdClass Object
1056 * (
1057 * [cookies] => Array
1058 * (
1059 * [foo] => bar
1060 * [bar] => baz
1061 * )
1062 *
1063 * [extras] => Array
1064 * (
1065 * [comment] =>
1066 * )
1067 *
1068 * [flags] => 16
1069 * [expires] => 0
1070 * [path] => /
1071 * [domain] => example.com
1072 * )
1073 * ?>
1074 * </pre>
1075 */
1076 PHP_FUNCTION(http_parse_cookie)
1077 {
1078 char *cookie, **allowed_extras = NULL;
1079 int i = 0, cookie_len;
1080 long flags = 0;
1081 zval *allowed_extras_array = NULL, **entry = NULL;
1082 HashPosition pos;
1083 http_cookie_list list;
1084
1085 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la", &cookie, &cookie_len, &flags, &allowed_extras_array)) {
1086 RETURN_FALSE;
1087 }
1088
1089 if (allowed_extras_array) {
1090 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
1091 FOREACH_VAL(pos, allowed_extras_array, entry) {
1092 ZVAL_ADDREF(*entry);
1093 convert_to_string_ex(entry);
1094 allowed_extras[i] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
1095 zval_ptr_dtor(entry);
1096 }
1097 }
1098
1099 if (http_parse_cookie_ex(&list, cookie, flags, allowed_extras)) {
1100 object_init(return_value);
1101 http_cookie_list_tostruct(&list, return_value);
1102 http_cookie_list_dtor(&list);
1103 } else {
1104 RETVAL_FALSE;
1105 }
1106
1107 if (allowed_extras) {
1108 for (i = 0; allowed_extras[i]; ++i) {
1109 efree(allowed_extras[i]);
1110 }
1111 efree(allowed_extras);
1112 }
1113 }
1114
1115 /* {{{ proto array http_get_request_headers(void)
1116 *
1117 * Get a list of incoming HTTP headers.
1118 *
1119 * Returns an associative array of incoming request headers.
1120 */
1121 PHP_FUNCTION(http_get_request_headers)
1122 {
1123 NO_ARGS;
1124
1125 array_init(return_value);
1126 http_get_request_headers(return_value);
1127 }
1128 /* }}} */
1129
1130 /* {{{ proto string http_get_request_body(void)
1131 *
1132 * Get the raw request body (e.g. POST or PUT data).
1133 *
1134 * This function can not be used after http_get_request_body_stream()
1135 * if the request method was another than POST.
1136 *
1137 * Returns the raw request body as string on success or NULL on failure.
1138 */
1139 PHP_FUNCTION(http_get_request_body)
1140 {
1141 char *body;
1142 size_t length;
1143
1144 NO_ARGS;
1145
1146 if (SUCCESS == http_get_request_body(&body, &length)) {
1147 RETURN_STRINGL(body, (int) length, 0);
1148 } else {
1149 RETURN_NULL();
1150 }
1151 }
1152 /* }}} */
1153
1154 /* {{{ proto resource http_get_request_body_stream(void)
1155 *
1156 * Create a stream to read the raw request body (e.g. POST or PUT data).
1157 *
1158 * This function can only be used once if the request method was another than POST.
1159 *
1160 * Returns the raw request body as stream on success or NULL on failure.
1161 */
1162 PHP_FUNCTION(http_get_request_body_stream)
1163 {
1164 php_stream *s;
1165
1166 NO_ARGS;
1167
1168 if ((s = http_get_request_body_stream())) {
1169 php_stream_to_zval(s, return_value);
1170 } else {
1171 http_error(HE_WARNING, HTTP_E_RUNTIME, "Failed to create request body stream");
1172 RETURN_NULL();
1173 }
1174 }
1175 /* }}} */
1176
1177 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
1178 *
1179 * Match an incoming HTTP header.
1180 *
1181 * Expects two string parameters representing the header name (case-insensitive)
1182 * and the header value that should be compared. The case sensitivity of the
1183 * header value depends on the additional optional bool parameter accepted.
1184 *
1185 * Returns TRUE if header value matches, else FALSE.
1186 */
1187 PHP_FUNCTION(http_match_request_header)
1188 {
1189 char *header, *value;
1190 int header_len, value_len;
1191 zend_bool match_case = 0;
1192
1193 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
1194 RETURN_FALSE;
1195 }
1196
1197 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
1198 }
1199 /* }}} */
1200
1201 /* {{{ HAVE_CURL */
1202 #ifdef HTTP_HAVE_CURL
1203
1204 #define RETVAL_RESPONSE_OR_BODY(request) \
1205 { \
1206 zval **bodyonly; \
1207 \
1208 /* check if only the body should be returned */ \
1209 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
1210 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
1211 \
1212 if (msg) { \
1213 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
1214 http_message_free(&msg); \
1215 } \
1216 } else { \
1217 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
1218 } \
1219 }
1220
1221 /* {{{ proto string http_get(string url[, array options[, array &info]])
1222 *
1223 * Performs an HTTP GET request on the supplied url.
1224 *
1225 * The second parameter, if set, is expected to be an associative
1226 * array where the following keys will be recognized:
1227 * <pre>
1228 * - redirect: int, whether and how many redirects to follow
1229 * - unrestrictedauth: bool, whether to continue sending credentials on
1230 * redirects to a different host
1231 * - proxyhost: string, proxy host in "host[:port]" format
1232 * - proxyport: int, use another proxy port as specified in proxyhost
1233 * - proxytype: int, HTTP_PROXY_HTTP, SOCKS4 or SOCKS5
1234 * - proxyauth: string, proxy credentials in "user:pass" format
1235 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1236 * - httpauth: string, http credentials in "user:pass" format
1237 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1238 * - compress: bool, whether to allow gzip/deflate content encoding
1239 * - port: int, use another port as specified in the url
1240 * - referer: string, the referer to send
1241 * - useragent: string, the user agent to send
1242 * (defaults to PECL::HTTP/version (PHP/version)))
1243 * - headers: array, list of custom headers as associative array
1244 * like array("header" => "value")
1245 * - cookies: array, list of cookies as associative array
1246 * like array("cookie" => "value")
1247 * - encodecookies: bool, whether to urlencode the cookies (default: true)
1248 * - resetcookies: bool, wheter to reset the cookies
1249 * - cookiestore: string, path to a file where cookies are/will be stored
1250 * - cookiesession: bool, accept (true) or reset (false) sessioncookies
1251 * - resume: int, byte offset to start the download from;
1252 * if the server supports ranges
1253 * - range: array, array of arrays, each containing two integers,
1254 * specifying the ranges to download if server support is
1255 * given; only recognized if the resume option is empty
1256 * - maxfilesize: int, maximum file size that should be downloaded;
1257 * has no effect, if the size of the requested entity is not known
1258 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1259 * - etag: string, quoted etag for If-(None-)Match header
1260 * - timeout: int, seconds the request may take
1261 * - connecttimeout: int, seconds the connect may take
1262 * - onprogress: mixed, progress callback
1263 * - interface: string, outgoing network interface (ifname, ip or hostname)
1264 * - portrange: array, 2 integers specifying outgoing portrange to try
1265 * - ssl: array, with the following options:
1266 * cert: string, path to certificate
1267 * certtype: string, type of certificate
1268 * certpasswd: string, password for certificate
1269 * key: string, path to key
1270 * keytype: string, type of key
1271 * keypasswd: string, pasword for key
1272 * engine: string, ssl engine to use
1273 * version: int, ssl version to use
1274 * verifypeer: bool, whether to verify the peer
1275 * verifyhost: bool whether to verify the host
1276 * cipher_list: string, list of allowed ciphers
1277 * cainfo: string
1278 * capath: string
1279 * random_file: string
1280 * egdsocket: string
1281 * </pre>
1282 *
1283 * The optional third parameter will be filled with some additional information
1284 * in form of an associative array, if supplied, like the following example:
1285 * <pre>
1286 * <?php
1287 * array (
1288 * 'effective_url' => 'http://www.example.com/',
1289 * 'response_code' => 302,
1290 * 'connect_code' => 0,
1291 * 'filetime' => -1,
1292 * 'total_time' => 0.212348,
1293 * 'namelookup_time' => 0.038296,
1294 * 'connect_time' => 0.104144,
1295 * 'pretransfer_time' => 0.104307,
1296 * 'starttransfer_time' => 0.212077,
1297 * 'redirect_time' => 0,
1298 * 'redirect_count' => 0,
1299 * 'size_upload' => 0,
1300 * 'size_download' => 218,
1301 * 'speed_download' => 1026,
1302 * 'speed_upload' => 0,
1303 * 'header_size' => 307,
1304 * 'request_size' => 103,
1305 * 'ssl_verifyresult' => 0,
1306 * 'ssl_engines' =>
1307 * array (
1308 * 0 => 'dynamic',
1309 * 1 => 'cswift',
1310 * 2 => 'chil',
1311 * 3 => 'atalla',
1312 * 4 => 'nuron',
1313 * 5 => 'ubsec',
1314 * 6 => 'aep',
1315 * 7 => 'sureware',
1316 * 8 => '4758cca',
1317 * ),
1318 * 'content_length_download' => 218,
1319 * 'content_length_upload' => 0,
1320 * 'content_type' => 'text/html',
1321 * 'httpauth_avail' => 0,
1322 * 'proxyauth_avail' => 0,
1323 * 'num_connects' => 1,
1324 * 'os_errno' => 0,
1325 * 'error' => '',
1326 * )
1327 * ?>
1328 * </pre>
1329 *
1330 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1331 */
1332 PHP_FUNCTION(http_get)
1333 {
1334 zval *options = NULL, *info = NULL;
1335 char *URL;
1336 int URL_len;
1337 http_request request;
1338
1339 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1340 RETURN_FALSE;
1341 }
1342
1343 if (info) {
1344 zval_dtor(info);
1345 array_init(info);
1346 }
1347
1348 RETVAL_FALSE;
1349
1350 http_request_init_ex(&request, NULL, HTTP_GET, URL);
1351 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1352 http_request_exec(&request);
1353 if (info) {
1354 http_request_info(&request, Z_ARRVAL_P(info));
1355 }
1356 RETVAL_RESPONSE_OR_BODY(request);
1357 }
1358 http_request_dtor(&request);
1359 }
1360 /* }}} */
1361
1362 /* {{{ proto string http_head(string url[, array options[, array &info]])
1363 *
1364 * Performs an HTTP HEAD request on the supplied url.
1365 *
1366 * See http_get() for a full list of available parameters and options.
1367 *
1368 * Returns the HTTP response as string on success, or FALSE on failure.
1369 */
1370 PHP_FUNCTION(http_head)
1371 {
1372 zval *options = NULL, *info = NULL;
1373 char *URL;
1374 int URL_len;
1375 http_request request;
1376
1377 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1378 RETURN_FALSE;
1379 }
1380
1381 if (info) {
1382 zval_dtor(info);
1383 array_init(info);
1384 }
1385
1386 RETVAL_FALSE;
1387
1388 http_request_init_ex(&request, NULL, HTTP_HEAD, URL);
1389 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1390 http_request_exec(&request);
1391 if (info) {
1392 http_request_info(&request, Z_ARRVAL_P(info));
1393 }
1394 RETVAL_RESPONSE_OR_BODY(request);
1395 }
1396 http_request_dtor(&request);
1397 }
1398 /* }}} */
1399
1400 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1401 *
1402 * Performs an HTTP POST request on the supplied url.
1403 *
1404 * Expects a string as second parameter containing the pre-encoded post data.
1405 * See http_get() for a full list of available parameters and options.
1406 *
1407 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1408 */
1409 PHP_FUNCTION(http_post_data)
1410 {
1411 zval *options = NULL, *info = NULL;
1412 char *URL, *postdata;
1413 int postdata_len, URL_len;
1414 http_request_body body;
1415 http_request request;
1416
1417 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1418 RETURN_FALSE;
1419 }
1420
1421 if (info) {
1422 zval_dtor(info);
1423 array_init(info);
1424 }
1425
1426 RETVAL_FALSE;
1427
1428 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1429 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, postdata, postdata_len, 0);
1430 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1431 http_request_exec(&request);
1432 if (info) {
1433 http_request_info(&request, Z_ARRVAL_P(info));
1434 }
1435 RETVAL_RESPONSE_OR_BODY(request);
1436 }
1437 http_request_dtor(&request);
1438 }
1439 /* }}} */
1440
1441 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1442 *
1443 * Performs an HTTP POST request on the supplied url.
1444 *
1445 * Expects an associative array as second parameter, which will be
1446 * www-form-urlencoded. See http_get() for a full list of available options.
1447 *
1448 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1449 */
1450 PHP_FUNCTION(http_post_fields)
1451 {
1452 zval *options = NULL, *info = NULL, *fields, *files = NULL;
1453 char *URL;
1454 int URL_len;
1455 http_request_body body;
1456 http_request request;
1457
1458 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
1459 RETURN_FALSE;
1460 }
1461
1462 if (!http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
1463 RETURN_FALSE;
1464 }
1465
1466 if (info) {
1467 zval_dtor(info);
1468 array_init(info);
1469 }
1470
1471 RETVAL_FALSE;
1472
1473 http_request_init_ex(&request, NULL, HTTP_POST, URL);
1474 request.body = &body;
1475 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1476 http_request_exec(&request);
1477 if (info) {
1478 http_request_info(&request, Z_ARRVAL_P(info));
1479 }
1480 RETVAL_RESPONSE_OR_BODY(request);
1481 }
1482 http_request_dtor(&request);
1483 }
1484 /* }}} */
1485
1486 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1487 *
1488 * Performs an HTTP PUT request on the supplied url.
1489 *
1490 * Expects the second parameter to be a string referencing the file to upload.
1491 * See http_get() for a full list of available options.
1492 *
1493 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1494 */
1495 PHP_FUNCTION(http_put_file)
1496 {
1497 char *URL, *file;
1498 int URL_len, f_len;
1499 zval *options = NULL, *info = NULL;
1500 php_stream *stream;
1501 php_stream_statbuf ssb;
1502 http_request_body body;
1503 http_request request;
1504
1505 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1506 RETURN_FALSE;
1507 }
1508
1509 if (!(stream = php_stream_open_wrapper_ex(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT))) {
1510 RETURN_FALSE;
1511 }
1512 if (php_stream_stat(stream, &ssb)) {
1513 php_stream_close(stream);
1514 RETURN_FALSE;
1515 }
1516
1517 if (info) {
1518 zval_dtor(info);
1519 array_init(info);
1520 }
1521
1522 RETVAL_FALSE;
1523
1524 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1525 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
1526 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1527 http_request_exec(&request);
1528 if (info) {
1529 http_request_info(&request, Z_ARRVAL_P(info));
1530 }
1531 RETVAL_RESPONSE_OR_BODY(request);
1532 }
1533 http_request_dtor(&request);
1534 }
1535 /* }}} */
1536
1537 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1538 *
1539 * Performs an HTTP PUT request on the supplied url.
1540 *
1541 * Expects the second parameter to be a resource referencing an already
1542 * opened stream, from which the data to upload should be read.
1543 * See http_get() for a full list of available options.
1544 *
1545 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1546 */
1547 PHP_FUNCTION(http_put_stream)
1548 {
1549 zval *resource, *options = NULL, *info = NULL;
1550 char *URL;
1551 int URL_len;
1552 php_stream *stream;
1553 php_stream_statbuf ssb;
1554 http_request_body body;
1555 http_request request;
1556
1557 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1558 RETURN_FALSE;
1559 }
1560
1561 php_stream_from_zval(stream, &resource);
1562 if (php_stream_stat(stream, &ssb)) {
1563 RETURN_FALSE;
1564 }
1565
1566 if (info) {
1567 zval_dtor(info);
1568 array_init(info);
1569 }
1570
1571 RETVAL_FALSE;
1572
1573 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1574 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 0);
1575 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1576 http_request_exec(&request);
1577 if (info) {
1578 http_request_info(&request, Z_ARRVAL_P(info));
1579 }
1580 RETVAL_RESPONSE_OR_BODY(request);
1581 }
1582 http_request_dtor(&request);
1583 }
1584 /* }}} */
1585
1586 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1587 *
1588 * Performs an HTTP PUT request on the supplied url.
1589 *
1590 * Expects the second parameter to be a string containing the data to upload.
1591 * See http_get() for a full list of available options.
1592 *
1593 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1594 */
1595 PHP_FUNCTION(http_put_data)
1596 {
1597 char *URL, *data;
1598 int URL_len, data_len;
1599 zval *options = NULL, *info = NULL;
1600 http_request_body body;
1601 http_request request;
1602
1603 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &data, &data_len, &options, &info)) {
1604 RETURN_FALSE;
1605 }
1606
1607 if (info) {
1608 zval_dtor(info);
1609 array_init(info);
1610 }
1611
1612 RETVAL_FALSE;
1613
1614 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1615 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, data, data_len, 0);
1616 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1617 http_request_exec(&request);
1618 if (info) {
1619 http_request_info(&request, Z_ARRVAL_P(info));
1620 }
1621 RETVAL_RESPONSE_OR_BODY(request);
1622 }
1623 http_request_dtor(&request);
1624 }
1625 /* }}} */
1626 #endif /* HTTP_HAVE_CURL */
1627 /* }}} HAVE_CURL */
1628
1629 /* {{{ proto int http_request_method_register(string method)
1630 *
1631 * Register a custom request method.
1632 *
1633 * Expects a string parameter containing the request method name to register.
1634 *
1635 * Returns the ID of the request method on success, or FALSE on failure.
1636 */
1637 PHP_FUNCTION(http_request_method_register)
1638 {
1639 char *method;
1640 int method_len;
1641 ulong existing;
1642
1643 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1644 RETURN_FALSE;
1645 }
1646 if ((existing = http_request_method_exists(1, 0, method))) {
1647 RETURN_LONG((long) existing);
1648 }
1649
1650 RETVAL_LONG((long) http_request_method_register(method, method_len));
1651 }
1652 /* }}} */
1653
1654 /* {{{ proto bool http_request_method_unregister(mixed method)
1655 *
1656 * Unregister a previously registered custom request method.
1657 *
1658 * Expects either the request method name or ID.
1659 *
1660 * Returns TRUE on success, or FALSE on failure.
1661 */
1662 PHP_FUNCTION(http_request_method_unregister)
1663 {
1664 zval *method;
1665
1666 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1667 RETURN_FALSE;
1668 }
1669
1670 switch (Z_TYPE_P(method))
1671 {
1672 case IS_OBJECT:
1673 convert_to_string(method);
1674 case IS_STRING:
1675 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1676 convert_to_long(method);
1677 } else {
1678 int mn;
1679 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1680 RETURN_FALSE;
1681 }
1682 zval_dtor(method);
1683 ZVAL_LONG(method, (long)mn);
1684 }
1685 case IS_LONG:
1686 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1687 default:
1688 RETURN_FALSE;
1689 }
1690 }
1691 /* }}} */
1692
1693 /* {{{ proto int http_request_method_exists(mixed method)
1694 *
1695 * Check if a request method is registered (or available by default).
1696 *
1697 * Expects either the request method name or ID as parameter.
1698 *
1699 * Returns TRUE if the request method is known, else FALSE.
1700 */
1701 PHP_FUNCTION(http_request_method_exists)
1702 {
1703 IF_RETVAL_USED {
1704 zval *method;
1705
1706 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1707 RETURN_FALSE;
1708 }
1709
1710 switch (Z_TYPE_P(method))
1711 {
1712 case IS_OBJECT:
1713 convert_to_string(method);
1714 case IS_STRING:
1715 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1716 convert_to_long(method);
1717 } else {
1718 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1719 }
1720 case IS_LONG:
1721 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method), NULL));
1722 default:
1723 RETURN_FALSE;
1724 }
1725 }
1726 }
1727 /* }}} */
1728
1729 /* {{{ proto string http_request_method_name(int method)
1730 *
1731 * Get the literal string representation of a standard or registered request method.
1732 *
1733 * Expects the request method ID as parameter.
1734 *
1735 * Returns the request method name as string on success, or FALSE on failure.
1736 */
1737 PHP_FUNCTION(http_request_method_name)
1738 {
1739 IF_RETVAL_USED {
1740 long method;
1741
1742 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1743 RETURN_FALSE;
1744 }
1745
1746 RETURN_STRING(estrdup(http_request_method_name((int) method)), 0);
1747 }
1748 }
1749 /* }}} */
1750
1751 /* {{{ */
1752 #ifdef HTTP_HAVE_ZLIB
1753
1754 /* {{{ proto string http_deflate(string data[, int flags = 0])
1755 *
1756 * Compress data with gzip, zlib AKA deflate or raw deflate encoding.
1757 *
1758 * Expects the first parameter to be a string containing the data that should
1759 * be encoded.
1760 *
1761 * Returns the encoded string on success, or NULL on failure.
1762 */
1763 PHP_FUNCTION(http_deflate)
1764 {
1765 char *data;
1766 int data_len;
1767 long flags = 0;
1768
1769 RETVAL_NULL();
1770
1771 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &flags)) {
1772 char *encoded;
1773 size_t encoded_len;
1774
1775 if (SUCCESS == http_encoding_deflate(flags, data, data_len, &encoded, &encoded_len)) {
1776 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1777 }
1778 }
1779 }
1780 /* }}} */
1781
1782 /* {{{ proto string http_inflate(string data)
1783 *
1784 * Decompress data compressed with either gzip, deflate AKA zlib or raw
1785 * deflate encoding.
1786 *
1787 * Expects a string as parameter containing the compressed data.
1788 *
1789 * Returns the decoded string on success, or NULL on failure.
1790 */
1791 PHP_FUNCTION(http_inflate)
1792 {
1793 char *data;
1794 int data_len;
1795
1796 RETVAL_NULL();
1797
1798 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1799 char *decoded;
1800 size_t decoded_len;
1801
1802 if (SUCCESS == http_encoding_inflate(data, data_len, &decoded, &decoded_len)) {
1803 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1804 }
1805 }
1806 }
1807 /* }}} */
1808
1809 /* {{{ proto string ob_deflatehandler(string data, int mode)
1810 *
1811 * For use with ob_start(). The deflate output buffer handler can only be used once.
1812 * It conflicts with ob_gzhandler and zlib.output_compression as well and should
1813 * not be used after ext/mbstrings mb_output_handler and ext/sessions URL-Rewriter (AKA
1814 * session.use_trans_sid).
1815 */
1816 PHP_FUNCTION(ob_deflatehandler)
1817 {
1818 char *data;
1819 int data_len;
1820 long mode;
1821
1822 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1823 RETURN_FALSE;
1824 }
1825
1826 http_ob_deflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1827 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1828 }
1829 /* }}} */
1830
1831 /* {{{ proto string ob_inflatehandler(string data, int mode)
1832 *
1833 * For use with ob_start(). Same restrictions as with ob_deflatehandler apply.
1834 */
1835 PHP_FUNCTION(ob_inflatehandler)
1836 {
1837 char *data;
1838 int data_len;
1839 long mode;
1840
1841 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1842 RETURN_FALSE;
1843 }
1844
1845 http_ob_inflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1846 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1847 }
1848 /* }}} */
1849
1850 #endif /* HTTP_HAVE_ZLIB */
1851 /* }}} */
1852
1853 /* {{{ proto int http_support([int feature = 0])
1854 *
1855 * Check for feature that require external libraries.
1856 *
1857 * Accepts an optional in parameter specifying which feature to probe for.
1858 * If the parameter is 0 or omitted, the return value contains a bitmask of
1859 * all supported features that depend on external libraries.
1860 *
1861 * Available features to probe for are:
1862 * <ul>
1863 * <li> HTTP_SUPPORT: always set
1864 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1865 * and HTTP requests can be issued
1866 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1867 * and SSL requests can be issued
1868 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1869 * and compressed HTTP responses can be decoded
1870 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1871 * and the HttpResponse::guessContentType() method is usable
1872 * </ul>
1873 *
1874 * Returns int, whether requested feature is supported, or a bitmask with
1875 * all supported features.
1876 */
1877 PHP_FUNCTION(http_support)
1878 {
1879 long feature = 0;
1880
1881 RETVAL_LONG(0L);
1882
1883 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &feature)) {
1884 RETVAL_LONG(http_support(feature));
1885 }
1886 }
1887 /* }}} */
1888
1889 PHP_FUNCTION(http_test)
1890 {
1891 }
1892
1893 /*
1894 * Local variables:
1895 * tab-width: 4
1896 * c-basic-offset: 4
1897 * End:
1898 * vim600: noet sw=4 ts=4 fdm=marker
1899 * vim<600: noet sw=4 ts=4
1900 */
1901