- new signature for the negotiator, which doesn't mix return value types, but
[m6w6/ext-http] / http_functions.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "zend_operators.h"
24
25 #include "SAPI.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "ext/standard/php_string.h"
29 #if defined(HAVE_PHP_SESSION) && !defined(COMPILE_DL_SESSION)
30 # include "ext/session/php_session.h"
31 #endif
32
33 #include "php_http.h"
34 #include "php_http_std_defs.h"
35 #include "php_http_api.h"
36 #include "php_http_request_api.h"
37 #include "php_http_cache_api.h"
38 #include "php_http_request_method_api.h"
39 #include "php_http_request_api.h"
40 #include "php_http_date_api.h"
41 #include "php_http_headers_api.h"
42 #include "php_http_message_api.h"
43 #include "php_http_send_api.h"
44 #include "php_http_url_api.h"
45
46 #include "phpstr/phpstr.h"
47
48 ZEND_EXTERN_MODULE_GLOBALS(http)
49
50 /* {{{ proto string http_date([int timestamp])
51 *
52 * Compose a valid HTTP date regarding RFC 822/1123
53 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
54 *
55 * Takes an optional unix timestamp as parameter.
56 *
57 * Returns the HTTP date as string.
58 */
59 PHP_FUNCTION(http_date)
60 {
61 long t = -1;
62
63 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
64 RETURN_FALSE;
65 }
66
67 if (t == -1) {
68 t = (long) time(NULL);
69 }
70
71 RETURN_STRING(http_date(t), 0);
72 }
73 /* }}} */
74
75 /* {{{ proto string http_build_uri(string url[, string proto[, string host[, int port]]])
76 *
77 * Build a complete URI according to the supplied parameters.
78 *
79 * If the url is already abolute but a different proto was supplied,
80 * only the proto part of the URI will be updated. If url has no
81 * path specified, the path of the current REQUEST_URI will be taken.
82 * The host will be taken either from the Host HTTP header of the client
83 * the SERVER_NAME or just localhost if prior are not available.
84 * If a port is pecified in either the url or as sperate parameter,
85 * it will be added if it differs from te default port for HTTP(S).
86 *
87 * Returns the absolute URI as string.
88 *
89 * Examples:
90 * <code>
91 * <?php
92 * $uri = http_build_uri("page.php", "https", NULL, 488);
93 * ?>
94 * </code>
95 */
96 PHP_FUNCTION(http_build_uri)
97 {
98 char *url = NULL, *proto = NULL, *host = NULL;
99 int url_len = 0, proto_len = 0, host_len = 0;
100 long port = 0;
101
102 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssl", &url, &url_len, &proto, &proto_len, &host, &host_len, &port) != SUCCESS) {
103 RETURN_FALSE;
104 }
105
106 RETURN_STRING(http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port), 0);
107 }
108 /* }}} */
109
110 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
111 { \
112 HashTable *result; \
113 if (result = http_negotiate_ ##type(supported)) { \
114 char *key; \
115 uint key_len; \
116 ulong idx; \
117 \
118 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
119 RETVAL_STRINGL(key, key_len-1, 0); \
120 } else { \
121 RETVAL_NULL(); \
122 } \
123 \
124 if (rs_array) { \
125 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
126 } \
127 \
128 zend_hash_destroy(result); \
129 FREE_HASHTABLE(result); \
130 \
131 } else { \
132 zval **value; \
133 \
134 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
135 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void **) &value)) { \
136 RETVAL_ZVAL(*value, 1, 0); \
137 } else { \
138 RETVAL_NULL(); \
139 } \
140 \
141 if (rs_array) { \
142 zval **value; \
143 \
144 FOREACH_VAL(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 bool http_send_status(int status)
256 *
257 * Send HTTP status code.
258 *
259 * Expects an HTTP status code as parameter.
260 *
261 * Returns TRUE on success or FALSE on failure.
262 */
263 PHP_FUNCTION(http_send_status)
264 {
265 int status = 0;
266
267 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
268 RETURN_FALSE;
269 }
270 if (status < 100 || status > 510) {
271 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Invalid HTTP status code (100-510): %d", status);
272 RETURN_FALSE;
273 }
274
275 RETURN_SUCCESS(http_send_status(status));
276 }
277 /* }}} */
278
279 /* {{{ proto bool http_send_last_modified([int timestamp])
280 *
281 * Send a "Last-Modified" header with a valid HTTP date.
282 *
283 * Accepts a unix timestamp, converts it to a valid HTTP date and
284 * sends it as "Last-Modified" HTTP header. If timestamp is
285 * omitted, the current time will be sent.
286 *
287 * Returns TRUE on success or FALSE on failure.
288 */
289 PHP_FUNCTION(http_send_last_modified)
290 {
291 long t = -1;
292
293 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
294 RETURN_FALSE;
295 }
296
297 if (t == -1) {
298 t = (long) time(NULL);
299 }
300
301 RETURN_SUCCESS(http_send_last_modified(t));
302 }
303 /* }}} */
304
305 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
306 *
307 * Send the Content-Type of the sent entity. This is particularly important
308 * if you use the http_send() API.
309 *
310 * Accepts an optional string parameter containing the desired content type
311 * (primary/secondary).
312 *
313 * Returns TRUE on success or FALSE on failure.
314 */
315 PHP_FUNCTION(http_send_content_type)
316 {
317 char *ct = "application/x-octetstream";
318 int ct_len = lenof("application/x-octetstream");
319
320 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
321 RETURN_FALSE;
322 }
323
324 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
325 }
326 /* }}} */
327
328 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
329 *
330 * Send the Content-Disposition. The Content-Disposition header is very useful
331 * if the data actually sent came from a file or something similar, that should
332 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
333 *
334 * Expects a string parameter specifying the file name the "Save as..." dialogue
335 * should display. Optionally accepts a bool parameter, which, if set to true
336 * and the user agent knows how to handle the content type, will probably not
337 * cause the popup window to be shown.
338 *
339 * Returns TRUE on success or FALSE on failure.
340 */
341 PHP_FUNCTION(http_send_content_disposition)
342 {
343 char *filename;
344 int f_len;
345 zend_bool send_inline = 0;
346
347 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
348 RETURN_FALSE;
349 }
350 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
351 }
352 /* }}} */
353
354 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
355 *
356 * Matches the given unix timestamp against the clients "If-Modified-Since"
357 * resp. "If-Unmodified-Since" HTTP headers.
358 *
359 * Accepts a unix timestamp which should be matched. Optionally accepts an
360 * additional bool parameter, which if set to true will check the header
361 * usually used to validate HTTP ranges. If timestamp is omitted, the
362 * current time will be used.
363 *
364 * Returns TRUE if timestamp represents an earlier date than the header,
365 * else FALSE.
366 */
367 PHP_FUNCTION(http_match_modified)
368 {
369 long t = -1;
370 zend_bool for_range = 0;
371
372 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
373 RETURN_FALSE;
374 }
375
376 // current time if not supplied (senseless though)
377 if (t == -1) {
378 t = (long) time(NULL);
379 }
380
381 if (for_range) {
382 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t));
383 }
384 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t));
385 }
386 /* }}} */
387
388 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
389 *
390 * Matches the given ETag against the clients "If-Match" resp.
391 * "If-None-Match" HTTP headers.
392 *
393 * Expects a string parameter containing the ETag to compare. Optionally
394 * accepts a bool parameter, which, if set to true, will check the header
395 * usually used to validate HTTP ranges.
396 *
397 * Retuns TRUE if ETag matches or the header contained the asterisk ("*"),
398 * else FALSE.
399 */
400 PHP_FUNCTION(http_match_etag)
401 {
402 int etag_len;
403 char *etag;
404 zend_bool for_range = 0;
405
406 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
407 RETURN_FALSE;
408 }
409
410 if (for_range) {
411 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag));
412 }
413 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag));
414 }
415 /* }}} */
416
417 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
418 *
419 * Attempts to cache the sent entity by its last modification date.
420 *
421 * Accepts a unix timestamp as parameter which is handled as follows:
422 *
423 * If timestamp_or_expires is greater than 0, it is handled as timestamp
424 * and will be sent as date of last modification. If it is 0 or omitted,
425 * the current time will be sent as Last-Modified date. If it's negative,
426 * it is handled as expiration time in seconds, which means that if the
427 * requested last modification date is not between the calculated timespan,
428 * the Last-Modified header is updated and the actual body will be sent.
429 *
430 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
431 *
432 * A log entry will be written to the cache log if the INI entry
433 * http.cache_log is set and the cache attempt was successful.
434 */
435 PHP_FUNCTION(http_cache_last_modified)
436 {
437 long last_modified = 0, send_modified = 0, t;
438 zval *zlm;
439
440 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
441 RETURN_FALSE;
442 }
443
444 t = (long) time(NULL);
445
446 /* 0 or omitted */
447 if (!last_modified) {
448 /* does the client have? (att: caching "forever") */
449 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
450 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
451 /* send current time */
452 } else {
453 send_modified = t;
454 }
455 /* negative value is supposed to be expiration time */
456 } else if (last_modified < 0) {
457 last_modified += t;
458 send_modified = t;
459 /* send supplied time explicitly */
460 } else {
461 send_modified = last_modified;
462 }
463
464 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
465 }
466 /* }}} */
467
468 /* {{{ proto bool http_cache_etag([string etag])
469 *
470 * Attempts to cache the sent entity by its ETag, either supplied or generated
471 * by the hash algorythm specified by the INI setting "http.etag_mode".
472 *
473 * If the clients "If-None-Match" header matches the supplied/calculated
474 * ETag, the body is considered cached on the clients side and
475 * a "304 Not Modified" status code is issued.
476 *
477 * Returns FALSE on failure, or *exits* with "304 Not Modified" if the entity is cached.
478 *
479 * A log entry is written to the cache log if the INI entry
480 * "http.cache_log" is set and the cache attempt was successful.
481 */
482 PHP_FUNCTION(http_cache_etag)
483 {
484 char *etag = NULL;
485 int etag_len = 0;
486
487 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
488 RETURN_FALSE;
489 }
490
491 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
492 }
493 /* }}} */
494
495 /* {{{ proto string ob_etaghandler(string data, int mode)
496 *
497 * For use with ob_start(). Output buffer handler generating an ETag with
498 * the hash algorythm specified with the INI setting "http.etag_mode".
499 */
500 PHP_FUNCTION(ob_etaghandler)
501 {
502 char *data;
503 int data_len;
504 long mode;
505
506 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
507 RETURN_FALSE;
508 }
509
510 Z_TYPE_P(return_value) = IS_STRING;
511 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
512 }
513 /* }}} */
514
515 /* {{{ proto void http_throttle(double sec[, int bytes = 2097152])
516 *
517 * Sets the throttle delay and send buffer size for use with http_send() API.
518 * Provides a basic throttling mechanism, which will yield the current process
519 * resp. thread until the entity has been completely sent, though.
520 *
521 * Note: This doesn't really work with the FastCGI SAPI.
522 *
523 * Expects a double parameter specifying the seconds too sleep() after
524 * each chunk sent. Additionally accepts an optional int parameter
525 * representing the chunk size in bytes.
526 *
527 * Example:
528 * <pre>
529 * <?php
530 * // ~ 20 kbyte/s
531 * # http_throttle(1, 20000);
532 * # http_throttle(0.5, 10000);
533 * # http_throttle(0.1, 2000);
534 * http_send_file('document.pdf');
535 * ?>
536 * </pre>
537 */
538 PHP_FUNCTION(http_throttle)
539 {
540 long chunk_size = HTTP_SENDBUF_SIZE;
541 double interval;
542
543 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|l", &interval, &chunk_size)) {
544 return;
545 }
546
547 HTTP_G(send).throttle_delay = interval;
548 HTTP_G(send).buffer_size = chunk_size;
549 }
550 /* }}} */
551
552 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
553 *
554 * Redirect to the given url.
555 *
556 * The supplied url will be expanded with http_build_uri(), the params array will
557 * be treated with http_build_query() and the session identification will be appended
558 * if session is true.
559 *
560 * The HTTP response code will be set according to status.
561 * You can use one of the following constants for convenience:
562 * - HTTP_REDIRECT 302 Found
563 * - HTTP_REDIRECT_PERM 301 Moved Permanently
564 * - HTTP_REDIRECT_POST 303 See Other
565 * - HTTP_REDIRECT_TEMP 307 Temporary Redirect
566 *
567 * Please see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
568 * for which redirect response code to use in which situation.
569 *
570 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
571 * if the client doesn't redirect immediatly, and the request method was
572 * another one than HEAD.
573 *
574 * Returns FALSE on failure, or *exits* on success.
575 *
576 * A log entry will be written to the redirect log, if the INI entry
577 * "http.redirect_log" is set and the redirect attempt was successful.
578 */
579 PHP_FUNCTION(http_redirect)
580 {
581 int url_len;
582 size_t query_len = 0;
583 zend_bool session = 0, free_params = 0;
584 zval *params = NULL;
585 long status = 302;
586 char *query = NULL, *url = NULL, *URI, *LOC, *RED = NULL;
587
588 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bl", &url, &url_len, &params, &session, &status) != SUCCESS) {
589 RETURN_FALSE;
590 }
591
592 /* append session info */
593 if (session) {
594 if (!params) {
595 free_params = 1;
596 MAKE_STD_ZVAL(params);
597 array_init(params);
598 }
599 #ifdef HAVE_PHP_SESSION
600 # ifdef COMPILE_DL_SESSION
601 if (SUCCESS == zend_get_module_started("session")) {
602 zval nm_retval, id_retval, func;
603
604 INIT_PZVAL(&func);
605 INIT_PZVAL(&nm_retval);
606 INIT_PZVAL(&id_retval);
607 ZVAL_NULL(&nm_retval);
608 ZVAL_NULL(&id_retval);
609
610 ZVAL_STRINGL(&func, "session_id", lenof("session_id"), 0);
611 call_user_function(EG(function_table), NULL, &func, &id_retval, 0, NULL TSRMLS_CC);
612 ZVAL_STRINGL(&func, "session_name", lenof("session_name"), 0);
613 call_user_function(EG(function_table), NULL, &func, &nm_retval, 0, NULL TSRMLS_CC);
614
615 if ( Z_TYPE(nm_retval) == IS_STRING && Z_STRLEN(nm_retval) &&
616 Z_TYPE(id_retval) == IS_STRING && Z_STRLEN(id_retval)) {
617 if (add_assoc_stringl_ex(params, Z_STRVAL(nm_retval), Z_STRLEN(nm_retval)+1,
618 Z_STRVAL(id_retval), Z_STRLEN(id_retval), 0) != SUCCESS) {
619 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
620 }
621 }
622 }
623 # else
624 if (PS(session_status) == php_session_active) {
625 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
626 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
627 }
628 }
629 # endif
630 #endif
631 }
632
633 /* treat params array with http_build_query() */
634 if (params) {
635 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
636 if (free_params) {
637 zval_dtor(params);
638 FREE_ZVAL(params);
639 }
640 if (query) {
641 efree(query);
642 }
643 RETURN_FALSE;
644 }
645 }
646
647 URI = http_absolute_uri(url);
648
649 if (query_len) {
650 spprintf(&LOC, 0, "Location: %s?%s", URI, query);
651 if (SG(request_info).request_method && strcmp(SG(request_info).request_method, "HEAD")) {
652 spprintf(&RED, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
653 }
654 } else {
655 spprintf(&LOC, 0, "Location: %s", URI);
656 if (SG(request_info).request_method && strcmp(SG(request_info).request_method, "HEAD")) {
657 spprintf(&RED, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
658 }
659 }
660
661 efree(URI);
662 if (query) {
663 efree(query);
664 }
665 if (free_params) {
666 zval_dtor(params);
667 FREE_ZVAL(params);
668 }
669
670 RETURN_SUCCESS(http_exit_ex(status, LOC, RED, 1));
671 }
672 /* }}} */
673
674 /* {{{ proto bool http_send_data(string data)
675 *
676 * Sends raw data with support for (multiple) range requests.
677 *
678 * Retursn TRUE on success, or FALSE on failure.
679 */
680 PHP_FUNCTION(http_send_data)
681 {
682 zval *zdata;
683
684 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
685 RETURN_FALSE;
686 }
687
688 convert_to_string_ex(&zdata);
689 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
690 }
691 /* }}} */
692
693 /* {{{ proto bool http_send_file(string file)
694 *
695 * Sends a file with support for (multiple) range requests.
696 *
697 * Expects a string parameter referencing the file to send.
698 *
699 * Returns TRUE on success, or FALSE on failure.
700 */
701 PHP_FUNCTION(http_send_file)
702 {
703 char *file;
704 int flen = 0;
705
706 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
707 RETURN_FALSE;
708 }
709 if (!flen) {
710 RETURN_FALSE;
711 }
712
713 RETURN_SUCCESS(http_send_file(file));
714 }
715 /* }}} */
716
717 /* {{{ proto bool http_send_stream(resource stream)
718 *
719 * Sends an already opened stream with support for (multiple) range requests.
720 *
721 * Expects a resource parameter referncing the stream to read from.
722 *
723 * Returns TRUE on success, or FALSE on failure.
724 */
725 PHP_FUNCTION(http_send_stream)
726 {
727 zval *zstream;
728 php_stream *file;
729
730 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
731 RETURN_FALSE;
732 }
733
734 php_stream_from_zval(file, &zstream);
735 RETURN_SUCCESS(http_send_stream(file));
736 }
737 /* }}} */
738
739 /* {{{ proto string http_chunked_decode(string encoded)
740 *
741 * Decodes a string that was HTTP-chunked encoded.
742 *
743 * Expects a chunked encoded string as parameter.
744 *
745 * Returns the decoded string on success or FALSE on failure.
746 */
747 PHP_FUNCTION(http_chunked_decode)
748 {
749 char *encoded = NULL, *decoded = NULL;
750 size_t decoded_len = 0;
751 int encoded_len = 0;
752
753 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
754 RETURN_FALSE;
755 }
756
757 if (NULL != http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
758 RETURN_STRINGL(decoded, (int) decoded_len, 0);
759 } else {
760 RETURN_FALSE;
761 }
762 }
763 /* }}} */
764
765 /* {{{ proto object http_parse_message(string message)
766 *
767 * Parses (a) http_message(s) into a simple recursive object structure.
768 *
769 * Expects a string parameter containing a single HTTP message or
770 * several consecutive HTTP messages.
771 *
772 * Returns an hierachical object structure of the parsed messages.
773 *
774 * Example:
775 * <pre>
776 * <?php
777 * print_r(http_parse_message(http_get(URL, array('redirect' => 3)));
778 *
779 * stdClass object
780 * (
781 * [type] => 2
782 * [httpVersion] => 1.1
783 * [responseCode] => 200
784 * [headers] => Array
785 * (
786 * [Content-Length] => 3
787 * [Server] => Apache
788 * )
789 * [body] => Hi!
790 * [parentMessage] => stdClass object
791 * (
792 * [type] => 2
793 * [httpVersion] => 1.1
794 * [responseCode] => 302
795 * [headers] => Array
796 * (
797 * [Content-Length] => 0
798 * [Location] => ...
799 * )
800 * [body] =>
801 * [parentMessage] => ...
802 * )
803 * )
804 * ?>
805 * </pre>
806 */
807 PHP_FUNCTION(http_parse_message)
808 {
809 char *message;
810 int message_len;
811 http_message *msg = NULL;
812
813 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &message, &message_len)) {
814 RETURN_NULL();
815 }
816
817 if (msg = http_message_parse(message, message_len)) {
818 object_init(return_value);
819 http_message_tostruct_recursive(msg, return_value);
820 http_message_free(&msg);
821 } else {
822 RETURN_NULL();
823 }
824 }
825 /* }}} */
826
827 /* {{{ proto array http_parse_headers(string header)
828 *
829 * Parses HTTP headers into an associative array.
830 *
831 * Expects a string parameter containing HTTP headers.
832 *
833 * Returns an array on success, or FALSE on failure.
834 *
835 * Example:
836 * <pre>
837 * <?php
838 * $headers = "content-type: text/html; charset=UTF-8\r\n".
839 * "Server: Funky/1.0\r\n".
840 * "Set-Cookie: foo=bar\r\n".
841 * "Set-Cookie: baz=quux\r\n".
842 * "Folded: works\r\n\ttoo\r\n";
843 * print_r(http_parse_headers($headers));
844 *
845 * Array
846 * (
847 * [Content-Type] => text/html; chatset=UTF-8
848 * [Server] => Funky/1.0
849 * [Set-Cookie] => Array
850 * (
851 * [0] => foo=bar
852 * [1] => baz=quux
853 * )
854 * [Folded] => works
855 * too
856 * ?>
857 * </pre>
858 */
859 PHP_FUNCTION(http_parse_headers)
860 {
861 char *header;
862 int header_len;
863
864 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
865 RETURN_FALSE;
866 }
867
868 array_init(return_value);
869 if (SUCCESS != http_parse_headers(header, return_value)) {
870 zval_dtor(return_value);
871 RETURN_FALSE;
872 }
873 }
874 /* }}}*/
875
876 /* {{{ proto array http_get_request_headers(void)
877 *
878 * Get a list of incoming HTTP headers.
879 *
880 * Returns an associative array of incoming request headers.
881 */
882 PHP_FUNCTION(http_get_request_headers)
883 {
884 NO_ARGS;
885
886 array_init(return_value);
887 http_get_request_headers(return_value);
888 }
889 /* }}} */
890
891 /* {{{ proto string http_get_request_body(void)
892 *
893 * Get the raw request body (e.g. POST or PUT data).
894 *
895 * Returns NULL when using the CLI SAPI.
896 */
897 PHP_FUNCTION(http_get_request_body)
898 {
899 char *body;
900 size_t length;
901
902 NO_ARGS;
903
904 if (SUCCESS == http_get_request_body(&body, &length)) {
905 RETURN_STRINGL(body, (int) length, 0);
906 } else {
907 RETURN_NULL();
908 }
909 }
910 /* }}} */
911
912 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
913 *
914 * Match an incoming HTTP header.
915 *
916 * Expects two string parameters representing the header name (case-insensitive)
917 * and the header value that should be compared. The case sensitivity of the
918 * header value depends on the additional optional bool parameter accepted.
919 *
920 * Returns TRUE if header value matches, else FALSE.
921 */
922 PHP_FUNCTION(http_match_request_header)
923 {
924 char *header, *value;
925 int header_len, value_len;
926 zend_bool match_case = 0;
927
928 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
929 RETURN_FALSE;
930 }
931
932 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
933 }
934 /* }}} */
935
936 /* {{{ HAVE_CURL */
937 #ifdef HTTP_HAVE_CURL
938
939 /* {{{ proto string http_get(string url[, array options[, array &info]])
940 *
941 * Performs an HTTP GET request on the supplied url.
942 *
943 * The second parameter, if set, is expected to be an associative
944 * array where the following keys will be recognized:
945 * <pre>
946 * - redirect: int, whether and how many redirects to follow
947 * - unrestrictedauth: bool, whether to continue sending credentials on
948 * redirects to a different host
949 * - proxyhost: string, proxy host in "host[:port]" format
950 * - proxyport: int, use another proxy port as specified in proxyhost
951 * - proxyauth: string, proxy credentials in "user:pass" format
952 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
953 * - httpauth: string, http credentials in "user:pass" format
954 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
955 * - compress: bool, whether to allow gzip/deflate content encoding
956 * (defaults to true)
957 * - port: int, use another port as specified in the url
958 * - referer: string, the referer to sends
959 * - useragent: string, the user agent to send
960 * (defaults to PECL::HTTP/version (PHP/version)))
961 * - headers: array, list of custom headers as associative array
962 * like array("header" => "value")
963 * - cookies: array, list of cookies as associative array
964 * like array("cookie" => "value")
965 * - cookiestore: string, path to a file where cookies are/will be stored
966 * - resume: int, byte offset to start the download from;
967 * if the server supports ranges
968 * - maxfilesize: int, maximum file size that should be downloaded;
969 * has no effect, if the size of the requested entity is not known
970 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
971 * - timeout: int, seconds the request may take
972 * - connecttimeout: int, seconds the connect may take
973 * - onprogress: mixed, progress callback
974 * </pre>
975 *
976 * The optional third parameter will be filled with some additional information
977 * in form af an associative array, if supplied, like the following example:
978 * <pre>
979 * <?php
980 * array (
981 * 'effective_url' => 'http://localhost',
982 * 'response_code' => 403,
983 * 'total_time' => 0.017,
984 * 'namelookup_time' => 0.013,
985 * 'connect_time' => 0.014,
986 * 'pretransfer_time' => 0.014,
987 * 'size_upload' => 0,
988 * 'size_download' => 202,
989 * 'speed_download' => 11882,
990 * 'speed_upload' => 0,
991 * 'header_size' => 145,
992 * 'request_size' => 62,
993 * 'ssl_verifyresult' => 0,
994 * 'filetime' => -1,
995 * 'content_length_download' => 202,
996 * 'content_length_upload' => 0,
997 * 'starttransfer_time' => 0.017,
998 * 'content_type' => 'text/html; charset=iso-8859-1',
999 * 'redirect_time' => 0,
1000 * 'redirect_count' => 0,
1001 * 'http_connectcode' => 0,
1002 * 'httpauth_avail' => 0,
1003 * 'proxyauth_avail' => 0,
1004 * )
1005 * ?>
1006 * </pre>
1007 *
1008 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1009 */
1010 PHP_FUNCTION(http_get)
1011 {
1012 zval *options = NULL, *info = NULL;
1013 char *URL;
1014 int URL_len;
1015 phpstr response;
1016
1017 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1018 RETURN_FALSE;
1019 }
1020
1021 if (info) {
1022 zval_dtor(info);
1023 array_init(info);
1024 }
1025
1026 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1027 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1028 RETURN_PHPSTR_VAL(&response);
1029 } else {
1030 RETURN_FALSE;
1031 }
1032 }
1033 /* }}} */
1034
1035 /* {{{ proto string http_head(string url[, array options[, array &info]])
1036 *
1037 * Performs an HTTP HEAD request on the supplied url.
1038 *
1039 * See http_get() for a full list of available parameters and options.
1040 *
1041 * Returns the HTTP response as string on success, or FALSE on failure.
1042 */
1043 PHP_FUNCTION(http_head)
1044 {
1045 zval *options = NULL, *info = NULL;
1046 char *URL;
1047 int URL_len;
1048 phpstr response;
1049
1050 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1051 RETURN_FALSE;
1052 }
1053
1054 if (info) {
1055 zval_dtor(info);
1056 array_init(info);
1057 }
1058
1059 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1060 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1061 RETURN_PHPSTR_VAL(&response);
1062 } else {
1063 RETURN_FALSE;
1064 }
1065 }
1066 /* }}} */
1067
1068 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
1069 *
1070 * Performs an HTTP POST requeston the supplied url.
1071 *
1072 * Expects a string as second parameter containing the pre-encoded post data.
1073 * See http_get() for a full list of available parameters and options.
1074 *
1075 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1076 */
1077 PHP_FUNCTION(http_post_data)
1078 {
1079 zval *options = NULL, *info = NULL;
1080 char *URL, *postdata;
1081 int postdata_len, URL_len;
1082 phpstr response;
1083 http_request_body body;
1084
1085 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1086 RETURN_FALSE;
1087 }
1088
1089 if (info) {
1090 zval_dtor(info);
1091 array_init(info);
1092 }
1093
1094 body.type = HTTP_REQUEST_BODY_CSTRING;
1095 body.data = postdata;
1096 body.size = postdata_len;
1097
1098 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1099 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1100 RETVAL_PHPSTR_VAL(&response);
1101 } else {
1102 RETVAL_FALSE;
1103 }
1104 }
1105 /* }}} */
1106
1107 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
1108 *
1109 * Performs an HTTP POST request on the supplied url.
1110 *
1111 * Expecrs an associative array as second parameter, which will be
1112 * www-form-urlencoded. See http_get() for a full list of available options.
1113 *
1114 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1115 */
1116 PHP_FUNCTION(http_post_fields)
1117 {
1118 zval *options = NULL, *info = NULL, *fields, *files = NULL;
1119 char *URL;
1120 int URL_len;
1121 phpstr response;
1122 http_request_body body;
1123
1124 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
1125 RETURN_FALSE;
1126 }
1127
1128 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
1129 RETURN_FALSE;
1130 }
1131
1132 if (info) {
1133 zval_dtor(info);
1134 array_init(info);
1135 }
1136
1137 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1138 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1139 RETVAL_PHPSTR_VAL(&response);
1140 } else {
1141 RETVAL_FALSE;
1142 }
1143 http_request_body_dtor(&body);
1144 }
1145 /* }}} */
1146
1147 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1148 *
1149 * Performs an HTTP PUT request on the supplied url.
1150 *
1151 * Expects the second parameter to be a string referncing the file to upload.
1152 * See http_get() for a full list of available options.
1153 *
1154 * Returns the HTTP response(s) as string on success, or FALSE on failure.
1155 */
1156 PHP_FUNCTION(http_put_file)
1157 {
1158 char *URL, *file;
1159 int URL_len, f_len;
1160 zval *options = NULL, *info = NULL;
1161 phpstr response;
1162 php_stream *stream;
1163 php_stream_statbuf ssb;
1164 http_request_body body;
1165
1166 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1167 RETURN_FALSE;
1168 }
1169
1170 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1171 RETURN_FALSE;
1172 }
1173 if (php_stream_stat(stream, &ssb)) {
1174 php_stream_close(stream);
1175 RETURN_FALSE;
1176 }
1177
1178 if (info) {
1179 zval_dtor(info);
1180 array_init(info);
1181 }
1182
1183 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1184 body.data = stream;
1185 body.size = ssb.sb.st_size;
1186
1187 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1188 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1189 RETVAL_PHPSTR_VAL(&response);
1190 } else {
1191 RETVAL_FALSE;
1192 }
1193 http_request_body_dtor(&body);
1194 }
1195 /* }}} */
1196
1197 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1198 *
1199 * Performs an HTTP PUT request on the supplied url.
1200 *
1201 * Expects the second parameter to be a resource referencing an already
1202 * opened stream, from which the data to upload should be read.
1203 * See http_get() for a full list of available options.
1204 *
1205 * Returns the HTTP response(s) as string on success. or FALSE on failure.
1206 */
1207 PHP_FUNCTION(http_put_stream)
1208 {
1209 zval *resource, *options = NULL, *info = NULL;
1210 char *URL;
1211 int URL_len;
1212 phpstr response;
1213 php_stream *stream;
1214 php_stream_statbuf ssb;
1215 http_request_body body;
1216
1217 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1218 RETURN_FALSE;
1219 }
1220
1221 php_stream_from_zval(stream, &resource);
1222 if (php_stream_stat(stream, &ssb)) {
1223 RETURN_FALSE;
1224 }
1225
1226 if (info) {
1227 zval_dtor(info);
1228 array_init(info);
1229 }
1230
1231 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
1232 body.data = stream;
1233 body.size = ssb.sb.st_size;
1234
1235 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
1236 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
1237 RETURN_PHPSTR_VAL(&response);
1238 } else {
1239 RETURN_NULL();
1240 }
1241 }
1242 /* }}} */
1243 #endif /* HTTP_HAVE_CURL */
1244 /* }}} HAVE_CURL */
1245
1246 /* {{{ proto int http_request_method_register(string method)
1247 *
1248 * Register a custom request method.
1249 *
1250 * Expects a string parameter containing the request method name to register.
1251 *
1252 * Returns the ID of the request method on success, or FALSE on failure.
1253 */
1254 PHP_FUNCTION(http_request_method_register)
1255 {
1256 char *method;
1257 int *method_len;
1258 unsigned long existing;
1259
1260 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1261 RETURN_FALSE;
1262 }
1263 if (existing = http_request_method_exists(1, 0, method)) {
1264 RETURN_LONG((long) existing);
1265 }
1266
1267 RETVAL_LONG((long) http_request_method_register(method));
1268 }
1269 /* }}} */
1270
1271 /* {{{ proto bool http_request_method_unregister(mixed method)
1272 *
1273 * Unregister a previously registered custom request method.
1274 *
1275 * Expects either the request method name or ID.
1276 *
1277 * Returns TRUE on success, or FALSE on failure.
1278 */
1279 PHP_FUNCTION(http_request_method_unregister)
1280 {
1281 zval *method;
1282
1283 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1284 RETURN_FALSE;
1285 }
1286
1287 switch (Z_TYPE_P(method))
1288 {
1289 case IS_OBJECT:
1290 convert_to_string(method);
1291 case IS_STRING:
1292 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1293 convert_to_long(method);
1294 } else {
1295 unsigned long mn;
1296 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1297 RETURN_FALSE;
1298 }
1299 zval_dtor(method);
1300 ZVAL_LONG(method, (long)mn);
1301 }
1302 case IS_LONG:
1303 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1304 default:
1305 RETURN_FALSE;
1306 }
1307 }
1308 /* }}} */
1309
1310 /* {{{ proto long http_request_method_exists(mixed method)
1311 *
1312 * Check if a request method is registered (or available by default).
1313 *
1314 * Expects either the request method name or ID as parameter.
1315 *
1316 * Returns TRUE if the request method is known, else FALSE.
1317 */
1318 PHP_FUNCTION(http_request_method_exists)
1319 {
1320 IF_RETVAL_USED {
1321 zval *method;
1322
1323 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1324 RETURN_FALSE;
1325 }
1326
1327 switch (Z_TYPE_P(method))
1328 {
1329 case IS_OBJECT:
1330 convert_to_string(method);
1331 case IS_STRING:
1332 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1333 convert_to_long(method);
1334 } else {
1335 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1336 }
1337 case IS_LONG:
1338 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1339 default:
1340 RETURN_FALSE;
1341 }
1342 }
1343 }
1344 /* }}} */
1345
1346 /* {{{ proto string http_request_method_name(int method)
1347 *
1348 * Get the literal string representation of a standard or registered request method.
1349 *
1350 * Expects the request method ID as parameter.
1351 *
1352 * Returns the request method name as string on success, or FALSE on failure.
1353 */
1354 PHP_FUNCTION(http_request_method_name)
1355 {
1356 IF_RETVAL_USED {
1357 long method;
1358
1359 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1360 RETURN_FALSE;
1361 }
1362
1363 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method)), 0);
1364 }
1365 }
1366 /* }}} */
1367
1368 /* {{{ Sara Golemons http_build_query() */
1369 #ifndef ZEND_ENGINE_2
1370
1371 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1372 Generates a form-encoded query string from an associative array or object. */
1373 PHP_FUNCTION(http_build_query)
1374 {
1375 zval *formdata;
1376 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1377 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1378 phpstr *formstr;
1379
1380 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1381 RETURN_FALSE;
1382 }
1383
1384 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
1385 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1386 RETURN_FALSE;
1387 }
1388
1389 if (!arg_sep_len) {
1390 arg_sep = HTTP_URL_ARGSEP;
1391 }
1392
1393 formstr = phpstr_new();
1394 if (SUCCESS != http_urlencode_hash_implementation_ex(HASH_OF(formdata), formstr, arg_sep, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL))) {
1395 phpstr_free(&formstr);
1396 RETURN_FALSE;
1397 }
1398
1399 if (!formstr->used) {
1400 phpstr_free(&formstr);
1401 RETURN_NULL();
1402 }
1403
1404 RETURN_PHPSTR_PTR(formstr);
1405 }
1406 /* }}} */
1407 #endif /* !ZEND_ENGINE_2 */
1408 /* }}} */
1409
1410 PHP_FUNCTION(http_test)
1411 {
1412 }
1413
1414 /*
1415 * Local variables:
1416 * tab-width: 4
1417 * c-basic-offset: 4
1418 * End:
1419 * vim600: noet sw=4 ts=4 fdm=marker
1420 * vim<600: noet sw=4 ts=4
1421 */
1422