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