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