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