- fix leak with 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 phpstr_dtor(&response); \
989 return; \
990 } \
991 } else { \
992 RETURN_PHPSTR_VAL(&response); \
993 } \
994 }
995
996 /* {{{ proto string http_get(string url[, array options[, array &info]])
997 *
998 * Performs an HTTP GET request on the supplied url.
999 *
1000 * The second parameter, if set, is expected to be an associative
1001 * array where the following keys will be recognized:
1002 * <pre>
1003 * - redirect: int, whether and how many redirects to follow
1004 * - unrestrictedauth: bool, whether to continue sending credentials on
1005 * redirects to a different host
1006 * - proxyhost: string, proxy host in "host[:port]" format
1007 * - proxyport: int, use another proxy port as specified in proxyhost
1008 * - proxyauth: string, proxy credentials in "user:pass" format
1009 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1010 * - httpauth: string, http credentials in "user:pass" format
1011 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1012 * - compress: bool, whether to allow gzip/deflate content encoding
1013 * (defaults to true)
1014 * - port: int, use another port as specified in the url
1015 * - referer: string, the referer to send
1016 * - useragent: string, the user agent to send
1017 * (defaults to PECL::HTTP/version (PHP/version)))
1018 * - headers: array, list of custom headers as associative array
1019 * like array("header" => "value")
1020 * - cookies: array, list of cookies as associative array
1021 * like array("cookie" => "value")
1022 * - cookiestore: string, path to a file where cookies are/will be stored
1023 * - resume: int, byte offset to start the download from;
1024 * if the server supports ranges
1025 * - maxfilesize: int, maximum file size that should be downloaded;
1026 * has no effect, if the size of the requested entity is not known
1027 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
1028 * - timeout: int, seconds the request may take
1029 * - connecttimeout: int, seconds the connect may take
1030 * - onprogress: mixed, progress callback
1031 * </pre>
1032 *
1033 * The optional third parameter will be filled with some additional information
1034 * in form af an associative array, if supplied, like the following example:
1035 * <pre>
1036 * <?php
1037 * array (
1038 * 'effective_url' => 'http://localhost',
1039 * 'response_code' => 403,
1040 * 'total_time' => 0.017,
1041 * 'namelookup_time' => 0.013,
1042 * 'connect_time' => 0.014,
1043 * 'pretransfer_time' => 0.014,
1044 * 'size_upload' => 0,
1045 * 'size_download' => 202,
1046 * 'speed_download' => 11882,
1047 * 'speed_upload' => 0,
1048 * 'header_size' => 145,
1049 * 'request_size' => 62,
1050 * 'ssl_verifyresult' => 0,
1051 * 'filetime' => -1,
1052 * 'content_length_download' => 202,
1053 * 'content_length_upload' => 0,
1054 * 'starttransfer_time' => 0.017,
1055 * 'content_type' => 'text/html; charset=iso-8859-1',
1056 * 'redirect_time' => 0,
1057 * 'redirect_count' => 0,
1058 * 'http_connectcode' => 0,
1059 * 'httpauth_avail' => 0,
1060 * 'proxyauth_avail' => 0,
1061 * )
1062 * ?>
1063 * </pre>
1064 *
1065 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1066 */
1067 PHP_FUNCTION(http_get)
1068 {
1069 zval *options = NULL, *info = NULL;
1070 char *URL;
1071 int URL_len;
1072 phpstr response;
1073
1074 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1075 RETURN_FALSE;
1076 }
1077
1078 if (info) {
1079 zval_dtor(info);
1080 array_init(info);
1081 }
1082
1083 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1084 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1085 RETURN_RESPONSE_OR_BODY(response);
1086 }
1087 phpstr_dtor(&response);
1088 RETURN_FALSE;
1089 }
1090 /* }}} */
1091
1092 /* {{{ proto string http_head(string url[, array options[, array &info]])
1093 *
1094 * Performs an HTTP HEAD request on the supplied url.
1095 *
1096 * See http_get() for a full list of available parameters and options.
1097 *
1098 * Returns the HTTP response as string on success, or FALSE on failure.
1099 */
1100 PHP_FUNCTION(http_head)
1101 {
1102 zval *options = NULL, *info = NULL;
1103 char *URL;
1104 int URL_len;
1105 phpstr response;
1106
1107 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1108 RETURN_FALSE;
1109 }
1110
1111 if (info) {
1112 zval_dtor(info);
1113 array_init(info);
1114 }
1115
1116 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1117 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1118 RETURN_RESPONSE_OR_BODY(response);
1119 }
1120 phpstr_dtor(&response);
1121 RETURN_FALSE;
1122 }
1123 /* }}} */
1124
1125 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1126 *
1127 * Performs an HTTP POST requeston the supplied url.
1128 *
1129 * Expects a string as second parameter containing the pre-encoded post data.
1130 * See http_get() for a full list of available parameters and options.
1131 *
1132 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1133 */
1134 PHP_FUNCTION(http_post_data)
1135 {
1136 zval *options = NULL, *info = NULL;
1137 char *URL, *postdata;
1138 int postdata_len, URL_len;
1139 phpstr response;
1140 http_request_body body;
1141
1142 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1143 RETURN_FALSE;
1144 }
1145
1146 if (info) {
1147 zval_dtor(info);
1148 array_init(info);
1149 }
1150
1151 body.type = HTTP_REQUEST_BODY_CSTRING;
1152 body.data = postdata;
1153 body.size = postdata_len;
1154
1155 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1156 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1157 RETURN_RESPONSE_OR_BODY(response);
1158 }
1159 phpstr_dtor(&response);
1160 RETVAL_FALSE;
1161 }
1162 /* }}} */
1163
1164 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1165 *
1166 * Performs an HTTP POST request on the supplied url.
1167 *
1168 * Expecrs an associative array as second parameter, which will be
1169 * www-form-urlencoded. See http_get() for a full list of available options.
1170 *
1171 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1172 */
1173 PHP_FUNCTION(http_post_fields)
1174 {
1175 zval *options = NULL, *info = NULL, *fields, *files = NULL;
1176 char *URL;
1177 int URL_len;
1178 phpstr response;
1179 http_request_body body;
1180
1181 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
1182 RETURN_FALSE;
1183 }
1184
1185 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
1186 RETURN_FALSE;
1187 }
1188
1189 if (info) {
1190 zval_dtor(info);
1191 array_init(info);
1192 }
1193
1194 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1195 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1196 http_request_body_dtor(&body);
1197 RETURN_RESPONSE_OR_BODY(response);
1198 }
1199 http_request_body_dtor(&body);
1200 phpstr_dtor(&response);
1201 RETURN_FALSE;
1202 }
1203 /* }}} */
1204
1205 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1206 *
1207 * Performs an HTTP PUT request on the supplied url.
1208 *
1209 * Expects the second parameter to be a string referncing the file to upload.
1210 * See http_get() for a full list of available options.
1211 *
1212 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1213 */
1214 PHP_FUNCTION(http_put_file)
1215 {
1216 char *URL, *file;
1217 int URL_len, f_len;
1218 zval *options = NULL, *info = NULL;
1219 phpstr response;
1220 php_stream *stream;
1221 php_stream_statbuf ssb;
1222 http_request_body body;
1223
1224 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1225 RETURN_FALSE;
1226 }
1227
1228 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1229 RETURN_FALSE;
1230 }
1231 if (php_stream_stat(stream, &ssb)) {
1232 php_stream_close(stream);
1233 RETURN_FALSE;
1234 }
1235
1236 if (info) {
1237 zval_dtor(info);
1238 array_init(info);
1239 }
1240
1241 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1242 body.data = stream;
1243 body.size = ssb.sb.st_size;
1244
1245 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1246 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1247 http_request_body_dtor(&body);
1248 RETURN_RESPONSE_OR_BODY(response);
1249 }
1250 http_request_body_dtor(&body);
1251 phpstr_dtor(&response);
1252 RETURN_FALSE;
1253 }
1254 /* }}} */
1255
1256 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1257 *
1258 * Performs an HTTP PUT request on the supplied url.
1259 *
1260 * Expects the second parameter to be a resource referencing an already
1261 * opened stream, from which the data to upload should be read.
1262 * See http_get() for a full list of available options.
1263 *
1264 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1265 */
1266 PHP_FUNCTION(http_put_stream)
1267 {
1268 zval *resource, *options = NULL, *info = NULL;
1269 char *URL;
1270 int URL_len;
1271 phpstr response;
1272 php_stream *stream;
1273 php_stream_statbuf ssb;
1274 http_request_body body;
1275
1276 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1277 RETURN_FALSE;
1278 }
1279
1280 php_stream_from_zval(stream, &resource);
1281 if (php_stream_stat(stream, &ssb)) {
1282 RETURN_FALSE;
1283 }
1284
1285 if (info) {
1286 zval_dtor(info);
1287 array_init(info);
1288 }
1289
1290 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1291 body.data = stream;
1292 body.size = ssb.sb.st_size;
1293
1294 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1295 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1296 RETURN_RESPONSE_OR_BODY(response);
1297 }
1298 phpstr_dtor(&response);
1299 RETURN_FALSE;
1300 }
1301 /* }}} */
1302 #endif /* HTTP_HAVE_CURL */
1303 /* }}} HAVE_CURL */
1304
1305 /* {{{ proto int http_request_method_register(string method)
1306 *
1307 * Register a custom request method.
1308 *
1309 * Expects a string parameter containing the request method name to register.
1310 *
1311 * Returns the ID of the request method on success, or FALSE on failure.
1312 */
1313 PHP_FUNCTION(http_request_method_register)
1314 {
1315 char *method;
1316 int method_len;
1317 ulong existing;
1318
1319 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1320 RETURN_FALSE;
1321 }
1322 if ((existing = http_request_method_exists(1, 0, method))) {
1323 RETURN_LONG((long) existing);
1324 }
1325
1326 RETVAL_LONG((long) http_request_method_register(method, method_len));
1327 }
1328 /* }}} */
1329
1330 /* {{{ proto bool http_request_method_unregister(mixed method)
1331 *
1332 * Unregister a previously registered custom request method.
1333 *
1334 * Expects either the request method name or ID.
1335 *
1336 * Returns TRUE on success, or FALSE on failure.
1337 */
1338 PHP_FUNCTION(http_request_method_unregister)
1339 {
1340 zval *method;
1341
1342 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1343 RETURN_FALSE;
1344 }
1345
1346 switch (Z_TYPE_P(method))
1347 {
1348 case IS_OBJECT:
1349 convert_to_string(method);
1350 case IS_STRING:
1351 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1352 convert_to_long(method);
1353 } else {
1354 ulong mn;
1355 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1356 RETURN_FALSE;
1357 }
1358 zval_dtor(method);
1359 ZVAL_LONG(method, (long)mn);
1360 }
1361 case IS_LONG:
1362 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1363 default:
1364 RETURN_FALSE;
1365 }
1366 }
1367 /* }}} */
1368
1369 /* {{{ proto int http_request_method_exists(mixed method)
1370 *
1371 * Check if a request method is registered (or available by default).
1372 *
1373 * Expects either the request method name or ID as parameter.
1374 *
1375 * Returns TRUE if the request method is known, else FALSE.
1376 */
1377 PHP_FUNCTION(http_request_method_exists)
1378 {
1379 IF_RETVAL_USED {
1380 zval *method;
1381
1382 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1383 RETURN_FALSE;
1384 }
1385
1386 switch (Z_TYPE_P(method))
1387 {
1388 case IS_OBJECT:
1389 convert_to_string(method);
1390 case IS_STRING:
1391 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1392 convert_to_long(method);
1393 } else {
1394 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1395 }
1396 case IS_LONG:
1397 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1398 default:
1399 RETURN_FALSE;
1400 }
1401 }
1402 }
1403 /* }}} */
1404
1405 /* {{{ proto string http_request_method_name(int method)
1406 *
1407 * Get the literal string representation of a standard or registered request method.
1408 *
1409 * Expects the request method ID as parameter.
1410 *
1411 * Returns the request method name as string on success, or FALSE on failure.
1412 */
1413 PHP_FUNCTION(http_request_method_name)
1414 {
1415 IF_RETVAL_USED {
1416 long method;
1417
1418 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1419 RETURN_FALSE;
1420 }
1421
1422 RETURN_STRING(estrdup(http_request_method_name((ulong) method)), 0);
1423 }
1424 }
1425 /* }}} */
1426
1427 /* {{{ Sara Golemons http_build_query() */
1428 #ifndef ZEND_ENGINE_2
1429
1430 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1431 Generates a form-encoded query string from an associative array or object. */
1432 PHP_FUNCTION(http_build_query)
1433 {
1434 zval *formdata;
1435 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1436 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1437 phpstr *formstr;
1438
1439 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1440 RETURN_FALSE;
1441 }
1442
1443 if (!arg_sep_len) {
1444 arg_sep = HTTP_URL_ARGSEP;
1445 arg_sep_len = lenof(HTTP_URL_ARGSEP);
1446 }
1447
1448 formstr = phpstr_new();
1449 if (SUCCESS != http_urlencode_hash_recursive(HASH_OF(formdata), formstr, arg_sep, arg_sep_len, prefix, prefix_len)) {
1450 phpstr_free(&formstr);
1451 RETURN_FALSE;
1452 }
1453
1454 if (!formstr->used) {
1455 phpstr_free(&formstr);
1456 RETURN_NULL();
1457 }
1458
1459 RETURN_PHPSTR_PTR(formstr);
1460 }
1461 /* }}} */
1462 #endif /* !ZEND_ENGINE_2 */
1463 /* }}} */
1464
1465 /* {{{ */
1466 #ifdef HTTP_HAVE_ZLIB
1467
1468 /* {{{ proto string http_gzencode(string data[, int level = -1])
1469 *
1470 * Compress data with the HTTP compatible GZIP encoding.
1471 *
1472 * Expects the first parameter to be a string which contains the data that
1473 * should be encoded. Additionally accepts an optional in paramter specifying
1474 * the compression level, where -1 is default, 0 is no compression and 9 is
1475 * best compression ratio.
1476 *
1477 * Returns the encoded string on success, or NULL on failure.
1478 */
1479 PHP_FUNCTION(http_gzencode)
1480 {
1481 char *data;
1482 int data_len;
1483 long level = -1;
1484
1485 RETVAL_NULL();
1486
1487 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level)) {
1488 HTTP_CHECK_GZIP_LEVEL(level, return);
1489 {
1490 char *encoded;
1491 size_t encoded_len;
1492
1493 if (SUCCESS == http_encoding_gzencode(level, data, data_len, &encoded, &encoded_len)) {
1494 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1495 }
1496 }
1497 }
1498 }
1499 /* }}} */
1500
1501 /* {{{ proto string http_gzdecode(string data)
1502 *
1503 * Uncompress data compressed with the HTTP compatible GZIP encoding.
1504 *
1505 * Expects a string as parameter containing the compressed data.
1506 *
1507 * Returns the decoded string on success, or NULL on failure.
1508 */
1509 PHP_FUNCTION(http_gzdecode)
1510 {
1511 char *data;
1512 int data_len;
1513
1514 RETVAL_NULL();
1515
1516 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1517 char *decoded;
1518 size_t decoded_len;
1519
1520 if (SUCCESS == http_encoding_gzdecode(data, data_len, &decoded, &decoded_len)) {
1521 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1522 }
1523 }
1524 }
1525 /* }}} */
1526
1527 /* {{{ proto string http_deflate(string data[, int level = -1])
1528 *
1529 * Compress data with the HTTP compatible DEFLATE encoding.
1530 *
1531 * Expects the first parameter to be a string containing the data that should
1532 * be encoded. Additionally accepts an optional int parameter specifying the
1533 * compression level, where -1 is default, 0 is no compression and 9 is best
1534 * compression ratio.
1535 *
1536 * Returns the encoded string on success, or NULL on failure.
1537 */
1538 PHP_FUNCTION(http_deflate)
1539 {
1540 char *data;
1541 int data_len;
1542 long level = -1;
1543
1544 RETVAL_NULL();
1545
1546 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level)) {
1547 HTTP_CHECK_GZIP_LEVEL(level, return);
1548 {
1549 char *encoded;
1550 size_t encoded_len;
1551
1552 if (SUCCESS == http_encoding_deflate(level, data, data_len, &encoded, &encoded_len)) {
1553 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1554 }
1555 }
1556 }
1557 }
1558 /* }}} */
1559
1560 /* {{{ proto string http_inflate(string data)
1561 *
1562 * Uncompress data compressed with the HTTP compatible DEFLATE encoding.
1563 *
1564 * Expects a string as parameter containing the compressed data.
1565 *
1566 * Returns the decoded string on success, or NULL on failure.
1567 */
1568 PHP_FUNCTION(http_inflate)
1569 {
1570 char *data;
1571 int data_len;
1572
1573 RETVAL_NULL();
1574
1575 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1576 char *decoded;
1577 size_t decoded_len;
1578
1579 if (SUCCESS == http_encoding_inflate(data, data_len, &decoded, &decoded_len)) {
1580 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1581 }
1582 }
1583 }
1584 /* }}} */
1585
1586 /* {{{ proto string http_compress(string data[, int level = -1])
1587 *
1588 * Compress data with the HTTP compatible COMPRESS encoding.
1589 *
1590 * Expects the first parameter to be a string containing the data which should
1591 * be encoded. Additionally accepts an optional int parameter specifying the
1592 * compression level, where -1 is default, 0 is no compression and 9 is best
1593 * compression ratio.
1594 *
1595 * Returns the encoded string on success, or NULL on failure.
1596 */
1597 PHP_FUNCTION(http_compress)
1598 {
1599 char *data;
1600 int data_len;
1601 long level = -1;
1602
1603 RETVAL_NULL();
1604
1605 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &level)) {
1606 HTTP_CHECK_GZIP_LEVEL(level, return);
1607 {
1608 char *encoded;
1609 size_t encoded_len;
1610
1611 if (SUCCESS == http_encoding_compress(level, data, data_len, &encoded, &encoded_len)) {
1612 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1613 }
1614 }
1615 }
1616 }
1617 /* }}} */
1618
1619 /* {{{ proto string http_uncompress(string data)
1620 *
1621 * Uncompress data compressed with the HTTP compatible COMPRESS encoding.
1622 *
1623 * Expects a string as parameter containing the compressed data.
1624 *
1625 * Returns the decoded string on success, or NULL on failure.
1626 */
1627 PHP_FUNCTION(http_uncompress)
1628 {
1629 char *data;
1630 int data_len;
1631
1632 RETVAL_NULL();
1633
1634 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1635 char *decoded;
1636 size_t decoded_len;
1637
1638 if (SUCCESS == http_encoding_uncompress(data, data_len, &decoded, &decoded_len)) {
1639 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1640 }
1641 }
1642 }
1643 /* }}} */
1644 #endif /* HTTP_HAVE_ZLIB */
1645 /* }}} */
1646
1647 /* {{{ proto int http_support([int feature = 0])
1648 *
1649 * Check for feature that require external libraries.
1650 *
1651 * Accpepts an optional in parameter specifying which feature to probe for.
1652 * If the parameter is 0 or omitted, the return value contains a bitmask of
1653 * all supported features that depend on external libraries.
1654 *
1655 * Available features to probe for are:
1656 * <ul>
1657 * <li> HTTP_SUPPORT: always set
1658 * <li> HTTP_SUPPORT_REQUESTS: whether ext/http was linked against libcurl,
1659 * and HTTP requests can be issued
1660 * <li> HTTP_SUPPORT_SSLREQUESTS: whether libcurl was linked against openssl,
1661 * and SSL requests can be issued
1662 * <li> HTTP_SUPPORT_ENCODINGS: whether ext/http was linked against zlib,
1663 * and compressed HTTP responses can be decoded
1664 * <li> HTTP_SUPPORT_MAGICMIME: whether ext/http was linked against libmagic,
1665 * and the HttpResponse::guessContentType() method is usable
1666 * </ul>
1667 *
1668 * Returns int, whether requested feature is supported, or a bitmask with
1669 * all supported features.
1670 */
1671 PHP_FUNCTION(http_support)
1672 {
1673 long feature = 0;
1674
1675 RETVAL_LONG(0L);
1676
1677 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &feature)) {
1678 RETVAL_LONG(http_support(feature));
1679 }
1680 }
1681 /* }}} */
1682
1683 PHP_FUNCTION(http_test)
1684 {
1685 }
1686
1687 /*
1688 * Local variables:
1689 * tab-width: 4
1690 * c-basic-offset: 4
1691 * End:
1692 * vim600: noet sw=4 ts=4 fdm=marker
1693 * vim<600: noet sw=4 ts=4
1694 */
1695