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