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