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