- release
[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 phpstr_dtor(&response);
1032 RETURN_FALSE;
1033 }
1034 }
1035 /* }}} */
1036
1037 /* {{{ proto string http_head(string url[, array options[, array &info]])
1038 *
1039 * Performs an HTTP HEAD request on the supplied url.
1040 *
1041 * See http_get() for a full list of available parameters and options.
1042 *
1043 * Returns the HTTP response as string on success, or FALSE on failure.
1044 */
1045 PHP_FUNCTION(http_head)
1046 {
1047 zval *options = NULL, *info = NULL;
1048 char *URL;
1049 int URL_len;
1050 phpstr response;
1051
1052 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1053 RETURN_FALSE;
1054 }
1055
1056 if (info) {
1057 zval_dtor(info);
1058 array_init(info);
1059 }
1060
1061 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1062 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1063 RETURN_PHPSTR_VAL(&response);
1064 } else {
1065 phpstr_dtor(&response);
1066 RETURN_FALSE;
1067 }
1068 }
1069 /* }}} */
1070
1071 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1072 *
1073 * Performs an HTTP POST requeston the supplied url.
1074 *
1075 * Expects a string as second parameter containing the pre-encoded post data.
1076 * See http_get() for a full list of available parameters and options.
1077 *
1078 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1079 */
1080 PHP_FUNCTION(http_post_data)
1081 {
1082 zval *options = NULL, *info = NULL;
1083 char *URL, *postdata;
1084 int postdata_len, URL_len;
1085 phpstr response;
1086 http_request_body body;
1087
1088 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1089 RETURN_FALSE;
1090 }
1091
1092 if (info) {
1093 zval_dtor(info);
1094 array_init(info);
1095 }
1096
1097 body.type = HTTP_REQUEST_BODY_CSTRING;
1098 body.data = postdata;
1099 body.size = postdata_len;
1100
1101 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1102 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1103 RETVAL_PHPSTR_VAL(&response);
1104 } else {
1105 phpstr_dtor(&response);
1106 RETVAL_FALSE;
1107 }
1108 }
1109 /* }}} */
1110
1111 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1112 *
1113 * Performs an HTTP POST request on the supplied url.
1114 *
1115 * Expecrs an associative array as second parameter, which will be
1116 * www-form-urlencoded. See http_get() for a full list of available options.
1117 *
1118 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1119 */
1120 PHP_FUNCTION(http_post_fields)
1121 {
1122 zval *options = NULL, *info = NULL, *fields, *files = NULL;
1123 char *URL;
1124 int URL_len;
1125 phpstr response;
1126 http_request_body body;
1127
1128 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
1129 RETURN_FALSE;
1130 }
1131
1132 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
1133 RETURN_FALSE;
1134 }
1135
1136 if (info) {
1137 zval_dtor(info);
1138 array_init(info);
1139 }
1140
1141 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1142 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1143 RETVAL_PHPSTR_VAL(&response);
1144 } else {
1145 phpstr_dtor(&response);
1146 RETVAL_FALSE;
1147 }
1148 http_request_body_dtor(&body);
1149 }
1150 /* }}} */
1151
1152 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1153 *
1154 * Performs an HTTP PUT request on the supplied url.
1155 *
1156 * Expects the second parameter to be a string referncing the file to upload.
1157 * See http_get() for a full list of available options.
1158 *
1159 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1160 */
1161 PHP_FUNCTION(http_put_file)
1162 {
1163 char *URL, *file;
1164 int URL_len, f_len;
1165 zval *options = NULL, *info = NULL;
1166 phpstr response;
1167 php_stream *stream;
1168 php_stream_statbuf ssb;
1169 http_request_body body;
1170
1171 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1172 RETURN_FALSE;
1173 }
1174
1175 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1176 RETURN_FALSE;
1177 }
1178 if (php_stream_stat(stream, &ssb)) {
1179 php_stream_close(stream);
1180 RETURN_FALSE;
1181 }
1182
1183 if (info) {
1184 zval_dtor(info);
1185 array_init(info);
1186 }
1187
1188 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1189 body.data = stream;
1190 body.size = ssb.sb.st_size;
1191
1192 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1193 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1194 RETVAL_PHPSTR_VAL(&response);
1195 } else {
1196 phpstr_dtor(&response);
1197 RETVAL_FALSE;
1198 }
1199 http_request_body_dtor(&body);
1200 }
1201 /* }}} */
1202
1203 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1204 *
1205 * Performs an HTTP PUT request on the supplied url.
1206 *
1207 * Expects the second parameter to be a resource referencing an already
1208 * opened stream, from which the data to upload should be read.
1209 * See http_get() for a full list of available options.
1210 *
1211 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1212 */
1213 PHP_FUNCTION(http_put_stream)
1214 {
1215 zval *resource, *options = NULL, *info = NULL;
1216 char *URL;
1217 int URL_len;
1218 phpstr response;
1219 php_stream *stream;
1220 php_stream_statbuf ssb;
1221 http_request_body body;
1222
1223 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1224 RETURN_FALSE;
1225 }
1226
1227 php_stream_from_zval(stream, &resource);
1228 if (php_stream_stat(stream, &ssb)) {
1229 RETURN_FALSE;
1230 }
1231
1232 if (info) {
1233 zval_dtor(info);
1234 array_init(info);
1235 }
1236
1237 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1238 body.data = stream;
1239 body.size = ssb.sb.st_size;
1240
1241 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1242 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1243 RETURN_PHPSTR_VAL(&response);
1244 } else {
1245 phpstr_dtor(&response);
1246 RETURN_NULL();
1247 }
1248 }
1249 /* }}} */
1250 #endif /* HTTP_HAVE_CURL */
1251 /* }}} HAVE_CURL */
1252
1253 /* {{{ proto int http_request_method_register(string method)
1254 *
1255 * Register a custom request method.
1256 *
1257 * Expects a string parameter containing the request method name to register.
1258 *
1259 * Returns the ID of the request method on success, or FALSE on failure.
1260 */
1261 PHP_FUNCTION(http_request_method_register)
1262 {
1263 char *method;
1264 int method_len;
1265 unsigned long existing;
1266
1267 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1268 RETURN_FALSE;
1269 }
1270 if (existing = http_request_method_exists(1, 0, method)) {
1271 RETURN_LONG((long) existing);
1272 }
1273
1274 RETVAL_LONG((long) http_request_method_register(method, method_len));
1275 }
1276 /* }}} */
1277
1278 /* {{{ proto bool http_request_method_unregister(mixed method)
1279 *
1280 * Unregister a previously registered custom request method.
1281 *
1282 * Expects either the request method name or ID.
1283 *
1284 * Returns TRUE on success, or FALSE on failure.
1285 */
1286 PHP_FUNCTION(http_request_method_unregister)
1287 {
1288 zval *method;
1289
1290 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1291 RETURN_FALSE;
1292 }
1293
1294 switch (Z_TYPE_P(method))
1295 {
1296 case IS_OBJECT:
1297 convert_to_string(method);
1298 case IS_STRING:
1299 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1300 convert_to_long(method);
1301 } else {
1302 unsigned long mn;
1303 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1304 RETURN_FALSE;
1305 }
1306 zval_dtor(method);
1307 ZVAL_LONG(method, (long)mn);
1308 }
1309 case IS_LONG:
1310 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1311 default:
1312 RETURN_FALSE;
1313 }
1314 }
1315 /* }}} */
1316
1317 /* {{{ proto int http_request_method_exists(mixed method)
1318 *
1319 * Check if a request method is registered (or available by default).
1320 *
1321 * Expects either the request method name or ID as parameter.
1322 *
1323 * Returns TRUE if the request method is known, else FALSE.
1324 */
1325 PHP_FUNCTION(http_request_method_exists)
1326 {
1327 IF_RETVAL_USED {
1328 zval *method;
1329
1330 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1331 RETURN_FALSE;
1332 }
1333
1334 switch (Z_TYPE_P(method))
1335 {
1336 case IS_OBJECT:
1337 convert_to_string(method);
1338 case IS_STRING:
1339 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1340 convert_to_long(method);
1341 } else {
1342 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1343 }
1344 case IS_LONG:
1345 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1346 default:
1347 RETURN_FALSE;
1348 }
1349 }
1350 }
1351 /* }}} */
1352
1353 /* {{{ proto string http_request_method_name(int method)
1354 *
1355 * Get the literal string representation of a standard or registered request method.
1356 *
1357 * Expects the request method ID as parameter.
1358 *
1359 * Returns the request method name as string on success, or FALSE on failure.
1360 */
1361 PHP_FUNCTION(http_request_method_name)
1362 {
1363 IF_RETVAL_USED {
1364 long method;
1365
1366 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1367 RETURN_FALSE;
1368 }
1369
1370 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method)), 0);
1371 }
1372 }
1373 /* }}} */
1374
1375 /* {{{ Sara Golemons http_build_query() */
1376 #ifndef ZEND_ENGINE_2
1377
1378 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1379 Generates a form-encoded query string from an associative array or object. */
1380 PHP_FUNCTION(http_build_query)
1381 {
1382 zval *formdata;
1383 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1384 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1385 phpstr *formstr;
1386
1387 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1388 RETURN_FALSE;
1389 }
1390
1391 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
1392 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1393 RETURN_FALSE;
1394 }
1395
1396 if (!arg_sep_len) {
1397 arg_sep = HTTP_URL_ARGSEP;
1398 }
1399
1400 formstr = phpstr_new();
1401 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))) {
1402 phpstr_free(&formstr);
1403 RETURN_FALSE;
1404 }
1405
1406 if (!formstr->used) {
1407 phpstr_free(&formstr);
1408 RETURN_NULL();
1409 }
1410
1411 RETURN_PHPSTR_PTR(formstr);
1412 }
1413 /* }}} */
1414 #endif /* !ZEND_ENGINE_2 */
1415 /* }}} */
1416
1417 /* {{{ */
1418 #ifdef HTTP_HAVE_ZLIB
1419
1420 /* {{{ proto string http_gzencode(string data[, int level = -1])
1421 *
1422 * Compress data with the HTTP compatible GZIP encoding.
1423 *
1424 * Expects the first parameter to be a string which contains the data that
1425 * should be encoded. Additionally accepts an optional in paramter specifying
1426 * the compression level, where -1 is default, 0 is no compression and 9 is
1427 * best compression ratio.
1428 *
1429 * Returns the encoded string on success, or NULL on failure.
1430 */
1431 PHP_FUNCTION(http_gzencode)
1432 {
1433 char *data;
1434 int data_len;
1435 long level = -1;
1436
1437 RETVAL_NULL();
1438
1439 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level)) {
1440 HTTP_CHECK_GZIP_LEVEL(level, return);
1441 {
1442 char *encoded;
1443 size_t encoded_len;
1444
1445 if (SUCCESS == http_encoding_gzencode(level, data, data_len, &encoded, &encoded_len)) {
1446 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1447 }
1448 }
1449 }
1450 }
1451 /* }}} */
1452
1453 /* {{{ proto string http_gzdecode(string data)
1454 *
1455 * Uncompress data compressed with the HTTP compatible GZIP encoding.
1456 *
1457 * Expects a string as parameter containing the compressed data.
1458 *
1459 * Returns the decoded string on success, or NULL on failure.
1460 */
1461 PHP_FUNCTION(http_gzdecode)
1462 {
1463 char *data;
1464 int data_len;
1465
1466 RETVAL_NULL();
1467
1468 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1469 char *decoded;
1470 size_t decoded_len;
1471
1472 if (SUCCESS == http_encoding_gzdecode(data, data_len, &decoded, &decoded_len)) {
1473 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1474 }
1475 }
1476 }
1477 /* }}} */
1478
1479 /* {{{ proto string http_deflate(string data[, int level = -1])
1480 *
1481 * Compress data with the HTTP compatible DEFLATE encoding.
1482 *
1483 * Expects the first parameter to be a string containing the data that should
1484 * be encoded. Additionally accepts an optional int parameter specifying the
1485 * compression level, where -1 is default, 0 is no compression and 9 is best
1486 * compression ratio.
1487 *
1488 * Returns the encoded string on success, or NULL on failure.
1489 */
1490 PHP_FUNCTION(http_deflate)
1491 {
1492 char *data;
1493 int data_len;
1494 long level = -1;
1495
1496 RETVAL_NULL();
1497
1498 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level)) {
1499 HTTP_CHECK_GZIP_LEVEL(level, return);
1500 {
1501 char *encoded;
1502 size_t encoded_len;
1503
1504 if (SUCCESS == http_encoding_deflate(level, data, data_len, &encoded, &encoded_len)) {
1505 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1506 }
1507 }
1508 }
1509 }
1510 /* }}} */
1511
1512 /* {{{ proto string http_inflate(string data)
1513 *
1514 * Uncompress data compressed with the HTTP compatible DEFLATE encoding.
1515 *
1516 * Expects a string as parameter containing the compressed data.
1517 *
1518 * Returns the decoded string on success, or NULL on failure.
1519 */
1520 PHP_FUNCTION(http_inflate)
1521 {
1522 char *data;
1523 int data_len;
1524
1525 RETVAL_NULL();
1526
1527 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1528 char *decoded;
1529 size_t decoded_len;
1530
1531 if (SUCCESS == http_encoding_inflate(data, data_len, &decoded, &decoded_len)) {
1532 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1533 }
1534 }
1535 }
1536 /* }}} */
1537
1538 /* {{{ proto string http_compress(string data[, int level = -1])
1539 *
1540 * Compress data with the HTTP compatible COMPRESS encoding.
1541 *
1542 * Expects the first parameter to be a string containing the data which should
1543 * be encoded. Additionally accepts an optional int parameter specifying the
1544 * compression level, where -1 is default, 0 is no compression and 9 is best
1545 * compression ratio.
1546 *
1547 * Returns the encoded string on success, or NULL on failure.
1548 */
1549 PHP_FUNCTION(http_compress)
1550 {
1551 char *data;
1552 int data_len;
1553 long level = -1;
1554
1555 RETVAL_NULL();
1556
1557 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level)) {
1558 HTTP_CHECK_GZIP_LEVEL(level, return);
1559 {
1560 char *encoded;
1561 size_t encoded_len;
1562
1563 if (SUCCESS == http_encoding_compress(level, data, data_len, &encoded, &encoded_len)) {
1564 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1565 }
1566 }
1567 }
1568 }
1569 /* }}} */
1570
1571 /* {{{ proto string http_uncompress(string data)
1572 *
1573 * Uncompress data compressed with the HTTP compatible COMPRESS encoding.
1574 *
1575 * Expects a string as parameter containing the compressed data.
1576 *
1577 * Returns the decoded string on success, or NULL on failure.
1578 */
1579 PHP_FUNCTION(http_uncompress)
1580 {
1581 char *data;
1582 int data_len;
1583
1584 RETVAL_NULL();
1585
1586 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1587 char *decoded;
1588 size_t decoded_len;
1589
1590 if (SUCCESS == http_encoding_uncompress(data, data_len, &decoded, &decoded_len)) {
1591 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1592 }
1593 }
1594 }
1595 /* }}} */
1596 #endif /* HTTP_HAVE_ZLIB */
1597 /* }}} */
1598
1599 /* {{{ proto int http_support([int feature = 0])
1600 *
1601 * Check for feature that require external libraries.
1602 *
1603 * Accpepts an optional in parameter specifying which feature to probe for.
1604 * If the parameter is 0 or omitted, the return value contains a bitmask of
1605 * all supported features that depend on external libraries.
1606 *
1607 * Available features to probe for are:
1608 * <ul>
1609 * <li> HTTP_SUPPORT: always set
1610 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1611 * and HTTP requests can be issued
1612 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1613 * and SSL requests can be issued
1614 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1615 * and compressed HTTP responses can be decoded
1616 * <li> HTTP_SUPPORT_MHASHETAGS: whether ext/http was linked against libmhash,
1617 * and ETags can be generated with the available mhash algorithms
1618 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1619 * and the HttpResponse::guessContentType() method is usable
1620 * </ul>
1621 *
1622 * Returns int, whether requested feature is supported, or a bitmask with
1623 * all supported features.
1624 */
1625 PHP_FUNCTION(http_support)
1626 {
1627 long feature = 0;
1628
1629 RETVAL_LONG(0L);
1630
1631 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &feature)) {
1632 RETVAL_LONG(http_support(feature));
1633 }
1634 }
1635 /* }}} */
1636
1637 PHP_FUNCTION(http_test)
1638 {
1639 }
1640
1641 /*
1642 * Local variables:
1643 * tab-width: 4
1644 * c-basic-offset: 4
1645 * End:
1646 * vim600: noet sw=4 ts=4 fdm=marker
1647 * vim<600: noet sw=4 ts=4
1648 */
1649