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