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