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