- no run-tests.diff any more
[m6w6/ext-http] / http_functions.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "SAPI.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "ext/session/php_session.h"
27 #include "ext/standard/php_string.h"
28
29 #include "php_http.h"
30 #include "php_http_std_defs.h"
31 #include "php_http_api.h"
32 #include "php_http_auth_api.h"
33 #include "php_http_request_api.h"
34 #include "php_http_cache_api.h"
35 #include "php_http_request_api.h"
36 #include "php_http_date_api.h"
37 #include "php_http_headers_api.h"
38 #include "php_http_message_api.h"
39 #include "php_http_send_api.h"
40 #include "php_http_url_api.h"
41
42 #include "phpstr/phpstr.h"
43
44 ZEND_EXTERN_MODULE_GLOBALS(http)
45
46 /* {{{ proto string http_date([int timestamp])
47 *
48 * This function returns a valid HTTP date regarding RFC 822/1123
49 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
50 *
51 */
52 PHP_FUNCTION(http_date)
53 {
54 long t = -1;
55
56 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
57 RETURN_FALSE;
58 }
59
60 if (t == -1) {
61 t = (long) time(NULL);
62 }
63
64 RETURN_STRING(http_date(t), 0);
65 }
66 /* }}} */
67
68 /* {{{ proto string http_absolute_uri(string url[, string proto[, string host[, int port]]])
69 *
70 * This function returns an absolute URI constructed from url.
71 * If the url is already abolute but a different proto was supplied,
72 * only the proto part of the URI will be updated. If url has no
73 * path specified, the path of the current REQUEST_URI will be taken.
74 * The host will be taken either from the Host HTTP header of the client
75 * the SERVER_NAME or just localhost if prior are not available.
76 *
77 * Some examples:
78 * <pre>
79 * url = "page.php" => http://www.example.com/current/path/page.php
80 * url = "/page.php" => http://www.example.com/page.php
81 * url = "/page.php", proto = "https" => https://www.example.com/page.php
82 * </pre>
83 *
84 */
85 PHP_FUNCTION(http_absolute_uri)
86 {
87 char *url = NULL, *proto = NULL, *host = NULL;
88 int url_len = 0, proto_len = 0, host_len = 0;
89 long port = 0;
90
91 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ssl", &url, &url_len, &proto, &proto_len, &host, &host_len, &port) != SUCCESS) {
92 RETURN_FALSE;
93 }
94
95 RETURN_STRING(http_absolute_uri_ex(url, url_len, proto, proto_len, host, host_len, port), 0);
96 }
97 /* }}} */
98
99 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
100 *
101 * This function negotiates the clients preferred language based on its
102 * Accept-Language HTTP header. It returns the negotiated language or
103 * the default language if none match.
104 *
105 * The qualifier is recognized and languages without qualifier are rated highest.
106 *
107 * The supported parameter is expected to be an array having
108 * the supported languages as array values.
109 *
110 * Example:
111 * <pre>
112 * <?php
113 * $langs = array(
114 * 'en-US',// default
115 * 'fr',
116 * 'fr-FR',
117 * 'de',
118 * 'de-DE',
119 * 'de-AT',
120 * 'de-CH',
121 * );
122 * include './langs/'. http_negotiate_language($langs) .'.php';
123 * ?>
124 * </pre>
125 *
126 */
127 PHP_FUNCTION(http_negotiate_language)
128 {
129 zval *supported;
130 char *def = NULL;
131 int def_len = 0;
132
133 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
134 RETURN_FALSE;
135 }
136
137 if (!def) {
138 def = "en-US";
139 }
140
141 RETURN_STRING(http_negotiate_language(supported, def), 0);
142 }
143 /* }}} */
144
145 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
146 *
147 * This function negotiates the clients preferred charset based on its
148 * Accept-Charset HTTP header. It returns the negotiated charset or
149 * the default charset if none match.
150 *
151 * The qualifier is recognized and charset without qualifier are rated highest.
152 *
153 * The supported parameter is expected to be an array having
154 * the supported charsets as array values.
155 *
156 * Example:
157 * <pre>
158 * <?php
159 * $charsets = array(
160 * 'iso-8859-1', // default
161 * 'iso-8859-2',
162 * 'iso-8859-15',
163 * 'utf-8'
164 * );
165 * $pref = http_negotiate_charset($charsets);
166 * if (!strcmp($pref, 'iso-8859-1')) {
167 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
168 * iconv_set_encoding('output_encoding', $pref);
169 * ob_start('ob_iconv_handler');
170 * }
171 * ?>
172 * </pre>
173 */
174 PHP_FUNCTION(http_negotiate_charset)
175 {
176 zval *supported;
177 char *def = NULL;
178 int def_len = 0;
179
180 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
181 RETURN_FALSE;
182 }
183
184 if (!def) {
185 def = "iso-8859-1";
186 }
187
188 RETURN_STRING(http_negotiate_charset(supported, def), 0);
189 }
190 /* }}} */
191
192 /* {{{ proto bool http_send_status(int status)
193 *
194 * Send HTTP status code.
195 *
196 */
197 PHP_FUNCTION(http_send_status)
198 {
199 int status = 0;
200
201 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
202 RETURN_FALSE;
203 }
204 if (status < 100 || status > 510) {
205 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Invalid HTTP status code (100-510): %d", status);
206 RETURN_FALSE;
207 }
208
209 RETURN_SUCCESS(http_send_status(status));
210 }
211 /* }}} */
212
213 /* {{{ proto bool http_send_last_modified([int timestamp])
214 *
215 * This converts the given timestamp to a valid HTTP date and
216 * sends it as "Last-Modified" HTTP header. If timestamp is
217 * omitted, current time is sent.
218 *
219 */
220 PHP_FUNCTION(http_send_last_modified)
221 {
222 long t = -1;
223
224 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
225 RETURN_FALSE;
226 }
227
228 if (t == -1) {
229 t = (long) time(NULL);
230 }
231
232 RETURN_SUCCESS(http_send_last_modified(t));
233 }
234 /* }}} */
235
236 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
237 *
238 * Sets the content type.
239 *
240 */
241 PHP_FUNCTION(http_send_content_type)
242 {
243 char *ct;
244 int ct_len = 0;
245
246 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
247 RETURN_FALSE;
248 }
249
250 if (!ct_len) {
251 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", lenof("application/x-octetstream")));
252 }
253 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
254 }
255 /* }}} */
256
257 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
258 *
259 * Set the Content Disposition. The Content-Disposition header is very useful
260 * if the data actually sent came from a file or something similar, that should
261 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
262 *
263 */
264 PHP_FUNCTION(http_send_content_disposition)
265 {
266 char *filename;
267 int f_len;
268 zend_bool send_inline = 0;
269
270 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
271 RETURN_FALSE;
272 }
273 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
274 }
275 /* }}} */
276
277 /* {{{ proto bool http_match_modified([int timestamp[, for_range = false]])
278 *
279 * Matches the given timestamp against the clients "If-Modified-Since" resp.
280 * "If-Unmodified-Since" HTTP headers.
281 *
282 */
283 PHP_FUNCTION(http_match_modified)
284 {
285 long t = -1;
286 zend_bool for_range = 0;
287
288 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
289 RETURN_FALSE;
290 }
291
292 // current time if not supplied (senseless though)
293 if (t == -1) {
294 t = (long) time(NULL);
295 }
296
297 if (for_range) {
298 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t));
299 }
300 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t));
301 }
302 /* }}} */
303
304 /* {{{ proto bool http_match_etag(string etag[, for_range = false])
305 *
306 * This matches the given ETag against the clients
307 * "If-Match" resp. "If-None-Match" HTTP headers.
308 *
309 */
310 PHP_FUNCTION(http_match_etag)
311 {
312 int etag_len;
313 char *etag;
314 zend_bool for_range = 0;
315
316 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
317 RETURN_FALSE;
318 }
319
320 if (for_range) {
321 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag));
322 }
323 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag));
324 }
325 /* }}} */
326
327 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
328 *
329 * If timestamp_or_expires is greater than 0, it is handled as timestamp
330 * and will be sent as date of last modification. If it is 0 or omitted,
331 * the current time will be sent as Last-Modified date. If it's negative,
332 * it is handled as expiration time in seconds, which means that if the
333 * requested last modification date is not between the calculated timespan,
334 * the Last-Modified header is updated and the actual body will be sent.
335 *
336 */
337 PHP_FUNCTION(http_cache_last_modified)
338 {
339 long last_modified = 0, send_modified = 0, t;
340 zval *zlm;
341
342 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
343 RETURN_FALSE;
344 }
345
346 t = (long) time(NULL);
347
348 /* 0 or omitted */
349 if (!last_modified) {
350 /* does the client have? (att: caching "forever") */
351 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
352 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
353 /* send current time */
354 } else {
355 send_modified = t;
356 }
357 /* negative value is supposed to be expiration time */
358 } else if (last_modified < 0) {
359 last_modified += t;
360 send_modified = t;
361 /* send supplied time explicitly */
362 } else {
363 send_modified = last_modified;
364 }
365
366 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
367 }
368 /* }}} */
369
370 /* {{{ proto bool http_cache_etag([string etag])
371 *
372 * This function attempts to cache the HTTP body based on an ETag,
373 * either supplied or generated through calculation of the MD5
374 * checksum of the output (uses output buffering).
375 *
376 * If clients "If-None-Match" header matches the supplied/calculated
377 * ETag, the body is considered cached on the clients side and
378 * a "304 Not Modified" status code is issued.
379 *
380 */
381 PHP_FUNCTION(http_cache_etag)
382 {
383 char *etag = NULL;
384 int etag_len = 0;
385
386 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
387 RETURN_FALSE;
388 }
389
390 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
391 }
392 /* }}} */
393
394 /* {{{ proto string ob_etaghandler(string data, int mode)
395 *
396 * For use with ob_start().
397 */
398 PHP_FUNCTION(ob_etaghandler)
399 {
400 char *data;
401 int data_len;
402 long mode;
403
404 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
405 RETURN_FALSE;
406 }
407
408 Z_TYPE_P(return_value) = IS_STRING;
409 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
410 }
411 /* }}} */
412
413 /* {{{ proto void http_throttle(double sec[, long bytes = 2097152])
414 *
415 * Use with http_send() API.
416 *
417 * Example:
418 * <pre>
419 * <?php
420 * // ~ 20 kbyte/s
421 * # http_throttle(1, 20000);
422 * # http_throttle(0.5, 10000);
423 * # http_throttle(0.1, 2000);
424 * http_send_file('document.pdf');
425 * ?>
426 * </pre>
427 */
428 PHP_FUNCTION(http_throttle)
429 {
430 long chunk_size;
431 double interval;
432
433 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dl", &interval, &chunk_size)) {
434 return;
435 }
436
437 HTTP_G(send).throttle_delay = interval;
438 HTTP_G(send).buffer_size = chunk_size;
439 }
440 /* }}} */
441
442 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
443 *
444 * Redirect to a given url.
445 * The supplied url will be expanded with http_absolute_uri(), the params array will
446 * be treated with http_build_query() and the session identification will be appended
447 * if session is true.
448 *
449 * Depending on permanent the redirection will be issued with a permanent
450 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
451 * status code.
452 *
453 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
454 * if the client doesn't redirect immediatly.
455 */
456 PHP_FUNCTION(http_redirect)
457 {
458 int url_len;
459 size_t query_len = 0;
460 zend_bool session = 0, permanent = 0;
461 zval *params = NULL;
462 char *query = NULL, *url = NULL, *URI,
463 LOC[HTTP_URI_MAXLEN + sizeof("Location: ")],
464 RED[HTTP_URI_MAXLEN * 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
465
466 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
467 RETURN_FALSE;
468 }
469
470 /* append session info */
471 if (session && (PS(session_status) == php_session_active)) {
472 if (!params) {
473 MAKE_STD_ZVAL(params);
474 array_init(params);
475 }
476 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
477 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
478 }
479 }
480
481 /* treat params array with http_build_query() */
482 if (params) {
483 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
484 RETURN_FALSE;
485 }
486 }
487
488 URI = http_absolute_uri(url);
489
490 if (query_len) {
491 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s?%s", URI, query);
492 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
493 efree(query);
494 } else {
495 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s", URI);
496 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
497 }
498 efree(URI);
499
500 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
501 php_body_write(RED, strlen(RED) TSRMLS_CC);
502 RETURN_TRUE;
503 }
504 RETURN_FALSE;
505 }
506 /* }}} */
507
508 /* {{{ proto bool http_send_data(string data)
509 *
510 * Sends raw data with support for (multiple) range requests.
511 *
512 */
513 PHP_FUNCTION(http_send_data)
514 {
515 zval *zdata;
516
517 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
518 RETURN_FALSE;
519 }
520
521 convert_to_string_ex(&zdata);
522 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
523 }
524 /* }}} */
525
526 /* {{{ proto bool http_send_file(string file)
527 *
528 * Sends a file with support for (multiple) range requests.
529 *
530 */
531 PHP_FUNCTION(http_send_file)
532 {
533 char *file;
534 int flen = 0;
535
536 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
537 RETURN_FALSE;
538 }
539 if (!flen) {
540 RETURN_FALSE;
541 }
542
543 RETURN_SUCCESS(http_send_file(file));
544 }
545 /* }}} */
546
547 /* {{{ proto bool http_send_stream(resource stream)
548 *
549 * Sends an already opened stream with support for (multiple) range requests.
550 *
551 */
552 PHP_FUNCTION(http_send_stream)
553 {
554 zval *zstream;
555 php_stream *file;
556
557 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
558 RETURN_FALSE;
559 }
560
561 php_stream_from_zval(file, &zstream);
562 RETURN_SUCCESS(http_send_stream(file));
563 }
564 /* }}} */
565
566 /* {{{ proto string http_chunked_decode(string encoded)
567 *
568 * This function decodes a string that was HTTP-chunked encoded.
569 * Returns false on failure.
570 */
571 PHP_FUNCTION(http_chunked_decode)
572 {
573 char *encoded = NULL, *decoded = NULL;
574 int encoded_len = 0, decoded_len = 0;
575
576 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
577 RETURN_FALSE;
578 }
579
580 if (NULL != http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
581 RETURN_STRINGL(decoded, decoded_len, 0);
582 } else {
583 RETURN_FALSE;
584 }
585 }
586 /* }}} */
587
588 /* {{{ proto array http_split_response(string http_response)
589 *
590 * This function splits an HTTP response into an array with headers and the
591 * content body. The returned array may look simliar to the following example:
592 *
593 * <pre>
594 * <?php
595 * array(
596 * 0 => array(
597 * 'Response Status' => '200 Ok',
598 * 'Content-Type' => 'text/plain',
599 * 'Content-Language' => 'en-US'
600 * ),
601 * 1 => "Hello World!"
602 * );
603 * ?>
604 * </pre>
605 */
606 PHP_FUNCTION(http_split_response)
607 {
608 char *response, *body;
609 int response_len;
610 size_t body_len;
611 zval *zheaders;
612
613 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &response, &response_len) != SUCCESS) {
614 RETURN_FALSE;
615 }
616
617 MAKE_STD_ZVAL(zheaders);
618 array_init(zheaders);
619
620 if (SUCCESS != http_split_response(response, response_len, Z_ARRVAL_P(zheaders), &body, &body_len)) {
621 RETURN_FALSE;
622 }
623
624 array_init(return_value);
625 add_index_zval(return_value, 0, zheaders);
626 add_index_stringl(return_value, 1, body, body_len, 0);
627 }
628 /* }}} */
629
630 /* {{{ proto array http_parse_headers(string header)
631 *
632 */
633 PHP_FUNCTION(http_parse_headers)
634 {
635 char *header;
636 int header_len;
637
638 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
639 RETURN_FALSE;
640 }
641
642 array_init(return_value);
643 if (SUCCESS != http_parse_headers(header, return_value)) {
644 zval_dtor(return_value);
645 RETURN_FALSE;
646 }
647 }
648 /* }}}*/
649
650 /* {{{ proto array http_get_request_headers(void)
651 *
652 * Get a list of incoming HTTP headers.
653 */
654 PHP_FUNCTION(http_get_request_headers)
655 {
656 NO_ARGS;
657
658 array_init(return_value);
659 http_get_request_headers(return_value);
660 }
661 /* }}} */
662
663 /* {{{ proto string http_get_request_body(void)
664 *
665 * Get the raw request body (e.g. POST or PUT data).
666 */
667 PHP_FUNCTION(http_get_request_body)
668 {
669 char *body;
670 size_t length;
671
672 NO_ARGS;
673
674 if (SUCCESS == http_get_request_body(&body, &length)) {
675 RETURN_STRINGL(body, (int) length, 0);
676 } else {
677 RETURN_NULL();
678 }
679 }
680 /* }}} */
681
682 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
683 *
684 * Match an incoming HTTP header.
685 */
686 PHP_FUNCTION(http_match_request_header)
687 {
688 char *header, *value;
689 int header_len, value_len;
690 zend_bool match_case = 0;
691
692 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
693 RETURN_FALSE;
694 }
695
696 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
697 }
698 /* }}} */
699
700 /* {{{ HAVE_CURL */
701 #ifdef HTTP_HAVE_CURL
702
703 /* {{{ proto string http_get(string url[, array options[, array &info]])
704 *
705 * Performs an HTTP GET request on the supplied url.
706 *
707 * The second parameter is expected to be an associative
708 * array where the following keys will be recognized:
709 * <pre>
710 * - redirect: int, whether and how many redirects to follow
711 * - unrestrictedauth: bool, whether to continue sending credentials on
712 * redirects to a different host
713 * - proxyhost: string, proxy host in "host[:port]" format
714 * - proxyport: int, use another proxy port as specified in proxyhost
715 * - proxyauth: string, proxy credentials in "user:pass" format
716 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
717 * - httpauth: string, http credentials in "user:pass" format
718 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
719 * - compress: bool, whether to allow gzip/deflate content encoding
720 * (defaults to true)
721 * - port: int, use another port as specified in the url
722 * - referer: string, the referer to sends
723 * - useragent: string, the user agent to send
724 * (defaults to PECL::HTTP/version (PHP/version)))
725 * - headers: array, list of custom headers as associative array
726 * like array("header" => "value")
727 * - cookies: array, list of cookies as associative array
728 * like array("cookie" => "value")
729 * - cookiestore: string, path to a file where cookies are/will be stored
730 * - resume: int, byte offset to start the download from;
731 * if the server supports ranges
732 * - maxfilesize: int, maximum file size that should be downloaded;
733 * has no effect, if the size of the requested entity is not known
734 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
735 * - timeout: int, seconds the request may take
736 * - connecttimeout: int, seconds the connect may take
737 * - onprogress: mixed, progress callback
738 * </pre>
739 *
740 * The optional third parameter will be filled with some additional information
741 * in form af an associative array, if supplied, like the following example:
742 * <pre>
743 * <?php
744 * array (
745 * 'effective_url' => 'http://localhost',
746 * 'response_code' => 403,
747 * 'total_time' => 0.017,
748 * 'namelookup_time' => 0.013,
749 * 'connect_time' => 0.014,
750 * 'pretransfer_time' => 0.014,
751 * 'size_upload' => 0,
752 * 'size_download' => 202,
753 * 'speed_download' => 11882,
754 * 'speed_upload' => 0,
755 * 'header_size' => 145,
756 * 'request_size' => 62,
757 * 'ssl_verifyresult' => 0,
758 * 'filetime' => -1,
759 * 'content_length_download' => 202,
760 * 'content_length_upload' => 0,
761 * 'starttransfer_time' => 0.017,
762 * 'content_type' => 'text/html; charset=iso-8859-1',
763 * 'redirect_time' => 0,
764 * 'redirect_count' => 0,
765 * 'private' => '',
766 * 'http_connectcode' => 0,
767 * 'httpauth_avail' => 0,
768 * 'proxyauth_avail' => 0,
769 * )
770 * ?>
771 * </pre>
772 */
773 PHP_FUNCTION(http_get)
774 {
775 zval *options = NULL, *info = NULL;
776 char *URL;
777 int URL_len;
778 phpstr response;
779
780 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
781 RETURN_FALSE;
782 }
783
784 if (info) {
785 zval_dtor(info);
786 array_init(info);
787 }
788
789 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
790 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
791 RETURN_PHPSTR_VAL(response);
792 } else {
793 RETURN_FALSE;
794 }
795 }
796 /* }}} */
797
798 /* {{{ proto string http_head(string url[, array options[, array &info]])
799 *
800 * Performs an HTTP HEAD request on the suppied url.
801 * Returns the HTTP response as string.
802 * See http_get() for a full list of available options.
803 */
804 PHP_FUNCTION(http_head)
805 {
806 zval *options = NULL, *info = NULL;
807 char *URL;
808 int URL_len;
809 phpstr response;
810
811 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
812 RETURN_FALSE;
813 }
814
815 if (info) {
816 zval_dtor(info);
817 array_init(info);
818 }
819
820 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
821 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
822 RETURN_PHPSTR_VAL(response);
823 } else {
824 RETURN_FALSE;
825 }
826 }
827 /* }}} */
828
829 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
830 *
831 * Performs an HTTP POST request, posting data.
832 * Returns the HTTP response as string.
833 * See http_get() for a full list of available options.
834 */
835 PHP_FUNCTION(http_post_data)
836 {
837 zval *options = NULL, *info = NULL;
838 char *URL, *postdata;
839 int postdata_len, URL_len;
840 phpstr response;
841 http_request_body body;
842
843 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
844 RETURN_FALSE;
845 }
846
847 if (info) {
848 zval_dtor(info);
849 array_init(info);
850 }
851
852 body.type = HTTP_REQUEST_BODY_CSTRING;
853 body.data = postdata;
854 body.size = postdata_len;
855
856 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
857 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
858 RETVAL_PHPSTR_VAL(response);
859 } else {
860 RETVAL_FALSE;
861 }
862 }
863 /* }}} */
864
865 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
866 *
867 * Performs an HTTP POST request, posting www-form-urlencoded array data.
868 * Returns the HTTP response as string.
869 * See http_get() for a full list of available options.
870 */
871 PHP_FUNCTION(http_post_fields)
872 {
873 zval *options = NULL, *info = NULL, *fields, *files = NULL;
874 char *URL;
875 int URL_len;
876 phpstr response;
877 http_request_body body;
878
879 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
880 RETURN_FALSE;
881 }
882
883 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
884 RETURN_FALSE;
885 }
886
887 if (info) {
888 zval_dtor(info);
889 array_init(info);
890 }
891
892 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
893 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
894 RETVAL_PHPSTR_VAL(response);
895 } else {
896 RETVAL_FALSE;
897 }
898 http_request_body_dtor(&body);
899 }
900 /* }}} */
901
902 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
903 *
904 * Performs an HTTP PUT request, uploading file.
905 * Returns the HTTP response as string.
906 * See http_get() for a full list of available options.
907 */
908 PHP_FUNCTION(http_put_file)
909 {
910 char *URL, *file;
911 int URL_len, f_len;
912 zval *options = NULL, *info = NULL;
913 phpstr response;
914 php_stream *stream;
915 php_stream_statbuf ssb;
916 http_request_body body;
917
918 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
919 RETURN_FALSE;
920 }
921
922 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
923 RETURN_FALSE;
924 }
925 if (php_stream_stat(stream, &ssb)) {
926 php_stream_close(stream);
927 RETURN_FALSE;
928 }
929
930 if (info) {
931 zval_dtor(info);
932 array_init(info);
933 }
934
935 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
936 body.data = stream;
937 body.size = ssb.sb.st_size;
938
939 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
940 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
941 RETVAL_PHPSTR_VAL(response);
942 } else {
943 RETVAL_FALSE;
944 }
945 http_request_body_dtor(&body);
946 }
947 /* }}} */
948
949 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
950 *
951 * Performs an HTTP PUT request, uploading stream.
952 * Returns the HTTP response as string.
953 * See http_get() for a full list of available options.
954 */
955 PHP_FUNCTION(http_put_stream)
956 {
957 zval *resource, *options = NULL, *info = NULL;
958 char *URL;
959 int URL_len;
960 phpstr response;
961 php_stream *stream;
962 php_stream_statbuf ssb;
963 http_request_body body;
964
965 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
966 RETURN_FALSE;
967 }
968
969 php_stream_from_zval(stream, &resource);
970 if (php_stream_stat(stream, &ssb)) {
971 RETURN_FALSE;
972 }
973
974 if (info) {
975 zval_dtor(info);
976 array_init(info);
977 }
978
979 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
980 body.data = stream;
981 body.size = ssb.sb.st_size;
982
983 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
984 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
985 RETURN_PHPSTR_VAL(response);
986 } else {
987 RETURN_NULL();
988 }
989 }
990 /* }}} */
991
992 /* {{{ proto long http_request_method_register(string method)
993 *
994 * Register a custom request method.
995 */
996 PHP_FUNCTION(http_request_method_register)
997 {
998 char *method;
999 int *method_len;
1000 unsigned long existing;
1001
1002 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1003 RETURN_FALSE;
1004 }
1005 if (existing = http_request_method_exists(1, 0, method)) {
1006 RETURN_LONG((long) existing);
1007 }
1008
1009 RETVAL_LONG((long) http_request_method_register(method));
1010 }
1011 /* }}} */
1012
1013 /* {{{ proto bool http_request_method_unregister(mixed method)
1014 *
1015 * Unregister a previously registered custom request method.
1016 */
1017 PHP_FUNCTION(http_request_method_unregister)
1018 {
1019 zval *method;
1020
1021 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1022 RETURN_FALSE;
1023 }
1024
1025 switch (Z_TYPE_P(method))
1026 {
1027 case IS_OBJECT:
1028 convert_to_string(method);
1029 case IS_STRING:
1030 #include "zend_operators.h"
1031 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1032 convert_to_long(method);
1033 } else {
1034 unsigned long mn;
1035 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1036 RETURN_FALSE;
1037 }
1038 zval_dtor(method);
1039 ZVAL_LONG(method, (long)mn);
1040 }
1041 case IS_LONG:
1042 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1043 default:
1044 RETURN_FALSE;
1045 }
1046 }
1047 /* }}} */
1048
1049 /* {{{ proto long http_request_method_exists(mixed method)
1050 *
1051 * Check if a request method is registered (or available by default).
1052 */
1053 PHP_FUNCTION(http_request_method_exists)
1054 {
1055 IF_RETVAL_USED {
1056 zval *method;
1057
1058 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1059 RETURN_FALSE;
1060 }
1061
1062 switch (Z_TYPE_P(method))
1063 {
1064 case IS_OBJECT:
1065 convert_to_string(method);
1066 case IS_STRING:
1067 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1068 convert_to_long(method);
1069 } else {
1070 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1071 }
1072 case IS_LONG:
1073 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1074 default:
1075 RETURN_FALSE;
1076 }
1077 }
1078 }
1079 /* }}} */
1080
1081 /* {{{ proto string http_request_method_name(long method)
1082 *
1083 * Get the literal string representation of a standard or registered request method.
1084 */
1085 PHP_FUNCTION(http_request_method_name)
1086 {
1087 IF_RETVAL_USED {
1088 long method;
1089
1090 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1091 RETURN_FALSE;
1092 }
1093
1094 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method)), 0);
1095 }
1096 }
1097 /* }}} */
1098 #endif
1099 /* }}} HAVE_CURL */
1100
1101
1102 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1103 *
1104 * Example:
1105 * <pre>
1106 * <?php
1107 * if (!http_auth_basic('mike', 's3c|r3t')) {
1108 * die('<h1>Authorization failed!</h1>');
1109 * }
1110 * ?>
1111 * </pre>
1112 */
1113 PHP_FUNCTION(http_auth_basic)
1114 {
1115 char *realm = NULL, *user, *pass, *suser, *spass;
1116 int r_len, u_len, p_len;
1117
1118 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
1119 RETURN_FALSE;
1120 }
1121
1122 if (!realm) {
1123 realm = "Restricted";
1124 }
1125
1126 if (SUCCESS != http_auth_basic_credentials(&suser, &spass)) {
1127 http_auth_basic_header(realm);
1128 RETURN_FALSE;
1129 }
1130
1131 if (strcasecmp(suser, user)) {
1132 http_auth_basic_header(realm);
1133 RETURN_FALSE;
1134 }
1135
1136 if (strcmp(spass, pass)) {
1137 http_auth_basic_header(realm);
1138 RETURN_FALSE;
1139 }
1140
1141 RETURN_TRUE;
1142 }
1143 /* }}} */
1144
1145 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1146 *
1147 * Example:
1148 * <pre>
1149 * <?php
1150 * function auth_cb($user, $pass)
1151 * {
1152 * global $db;
1153 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1154 * if (strlen($realpass = $db->getOne($query)) {
1155 * return $pass === $realpass;
1156 * }
1157 * return false;
1158 * }
1159 * if (!http_auth_basic_cb('auth_cb')) {
1160 * die('<h1>Authorization failed</h1>');
1161 * }
1162 * ?>
1163 * </pre>
1164 */
1165 PHP_FUNCTION(http_auth_basic_cb)
1166 {
1167 zval *cb;
1168 char *realm = NULL, *user, *pass;
1169 int r_len;
1170
1171 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1172 RETURN_FALSE;
1173 }
1174
1175 if (!realm) {
1176 realm = "Restricted";
1177 }
1178
1179 if (SUCCESS != http_auth_basic_credentials(&user, &pass)) {
1180 http_auth_basic_header(realm);
1181 RETURN_FALSE;
1182 }
1183 {
1184 zval *zparams[2] = {NULL, NULL}, retval;
1185 int result = 0;
1186
1187 MAKE_STD_ZVAL(zparams[0]);
1188 MAKE_STD_ZVAL(zparams[1]);
1189 ZVAL_STRING(zparams[0], user, 0);
1190 ZVAL_STRING(zparams[1], pass, 0);
1191
1192 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1193 &retval, 2, zparams TSRMLS_CC)) {
1194 result = Z_LVAL(retval);
1195 }
1196
1197 efree(user);
1198 efree(pass);
1199 efree(zparams[0]);
1200 efree(zparams[1]);
1201
1202 if (!result) {
1203 http_auth_basic_header(realm);
1204 }
1205
1206 RETURN_BOOL(result);
1207 }
1208 }
1209 /* }}}*/
1210
1211 /* {{{ Sara Golemons http_build_query() */
1212 #ifndef ZEND_ENGINE_2
1213
1214 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1215 Generates a form-encoded query string from an associative array or object. */
1216 PHP_FUNCTION(http_build_query)
1217 {
1218 zval *formdata;
1219 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1220 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1221 phpstr *formstr;
1222
1223 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1224 RETURN_FALSE;
1225 }
1226
1227 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
1228 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1229 RETURN_FALSE;
1230 }
1231
1232 if (!arg_sep_len) {
1233 arg_sep = HTTP_URL_ARGSEP;
1234 }
1235
1236 formstr = phpstr_new();
1237 if (SUCCESS != http_urlencode_hash_implementation_ex(HASH_OF(formdata), formstr, arg_sep, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL))) {
1238 phpstr_free(formstr);
1239 RETURN_FALSE;
1240 }
1241
1242 if (!formstr->used) {
1243 phpstr_free(formstr);
1244 RETURN_NULL();
1245 }
1246
1247 RETURN_PHPSTR_PTR(formstr);
1248 }
1249 /* }}} */
1250 #endif /* !ZEND_ENGINE_2 */
1251 /* }}} */
1252
1253 PHP_FUNCTION(http_test)
1254 {
1255 RETURN_BOOL(HTTP_G(only_exceptions));
1256 }
1257
1258 /*
1259 * Local variables:
1260 * tab-width: 4
1261 * c-basic-offset: 4
1262 * End:
1263 * vim600: noet sw=4 ts=4 fdm=marker
1264 * vim<600: noet sw=4 ts=4
1265 */
1266