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