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