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