7612013c070aca40fb1c63f6c5bc14b28b547ea6
[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(E_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(E_WARNING, HTTP_E_ENCODE, "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 http_error(E_WARNING, HTTP_E_PARSE, "Could not parse HTTP response");
622 RETURN_FALSE;
623 }
624
625 array_init(return_value);
626 add_index_zval(return_value, 0, zheaders);
627 add_index_stringl(return_value, 1, body, body_len, 0);
628 }
629 /* }}} */
630
631 /* {{{ proto array http_parse_headers(string header)
632 *
633 */
634 PHP_FUNCTION(http_parse_headers)
635 {
636 char *header;
637 int header_len;
638
639 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
640 RETURN_FALSE;
641 }
642
643 array_init(return_value);
644 if (SUCCESS != http_parse_headers(header, return_value)) {
645 http_error(E_WARNING, HTTP_E_PARSE, "Could not parse HTTP headers");
646 zval_dtor(return_value);
647 RETURN_FALSE;
648 }
649 }
650 /* }}}*/
651
652 /* {{{ proto array http_get_request_headers(void)
653 *
654 * Get a list of incoming HTTP headers.
655 */
656 PHP_FUNCTION(http_get_request_headers)
657 {
658 NO_ARGS;
659
660 array_init(return_value);
661 http_get_request_headers(return_value);
662 }
663 /* }}} */
664
665 /* {{{ proto string http_get_request_body(void)
666 *
667 * Get the raw request body (e.g. POST or PUT data).
668 */
669 PHP_FUNCTION(http_get_request_body)
670 {
671 char *body;
672 size_t length;
673
674 NO_ARGS;
675
676 if (SUCCESS == http_get_request_body(&body, &length)) {
677 RETURN_STRING(body, (int) length, 0);
678 } else {
679 RETURN_NULL();
680 }
681 }
682 /* }}} */
683
684 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
685 *
686 * Match an incoming HTTP header.
687 */
688 PHP_FUNCTION(http_match_request_header)
689 {
690 char *header, *value;
691 int header_len, value_len;
692 zend_bool match_case = 0, result = 0;
693
694 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
695 RETURN_FALSE;
696 }
697
698 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
699 }
700 /* }}} */
701
702 /* {{{ HAVE_CURL */
703 #ifdef HTTP_HAVE_CURL
704
705 /* {{{ proto string http_get(string url[, array options[, array &info]])
706 *
707 * Performs an HTTP GET request on the supplied url.
708 *
709 * The second parameter is expected to be an associative
710 * array where the following keys will be recognized:
711 * <pre>
712 * - redirect: int, whether and how many redirects to follow
713 * - unrestrictedauth: bool, whether to continue sending credentials on
714 * redirects to a different host
715 * - proxyhost: string, proxy host in "host[:port]" format
716 * - proxyport: int, use another proxy port as specified in proxyhost
717 * - proxyauth: string, proxy credentials in "user:pass" format
718 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
719 * - httpauth: string, http credentials in "user:pass" format
720 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
721 * - compress: bool, whether to allow gzip/deflate content encoding
722 * (defaults to true)
723 * - port: int, use another port as specified in the url
724 * - referer: string, the referer to sends
725 * - useragent: string, the user agent to send
726 * (defaults to PECL::HTTP/version (PHP/version)))
727 * - headers: array, list of custom headers as associative array
728 * like array("header" => "value")
729 * - cookies: array, list of cookies as associative array
730 * like array("cookie" => "value")
731 * - cookiestore: string, path to a file where cookies are/will be stored
732 * - resume: int, byte offset to start the download from;
733 * if the server supports ranges
734 * - maxfilesize: int, maximum file size that should be downloaded;
735 * has no effect, if the size of the requested entity is not known
736 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
737 * - timeout: int, seconds the request may take
738 * - connecttimeout: int, seconds the connect may take
739 * - onprogress: mixed, progress callback
740 * - ondebug: mixed, debug callback
741 * </pre>
742 *
743 * The optional third parameter will be filled with some additional information
744 * in form af an associative array, if supplied, like the following example:
745 * <pre>
746 * <?php
747 * array (
748 * 'effective_url' => 'http://localhost',
749 * 'response_code' => 403,
750 * 'total_time' => 0.017,
751 * 'namelookup_time' => 0.013,
752 * 'connect_time' => 0.014,
753 * 'pretransfer_time' => 0.014,
754 * 'size_upload' => 0,
755 * 'size_download' => 202,
756 * 'speed_download' => 11882,
757 * 'speed_upload' => 0,
758 * 'header_size' => 145,
759 * 'request_size' => 62,
760 * 'ssl_verifyresult' => 0,
761 * 'filetime' => -1,
762 * 'content_length_download' => 202,
763 * 'content_length_upload' => 0,
764 * 'starttransfer_time' => 0.017,
765 * 'content_type' => 'text/html; charset=iso-8859-1',
766 * 'redirect_time' => 0,
767 * 'redirect_count' => 0,
768 * 'private' => '',
769 * 'http_connectcode' => 0,
770 * 'httpauth_avail' => 0,
771 * 'proxyauth_avail' => 0,
772 * )
773 * ?>
774 * </pre>
775 */
776 PHP_FUNCTION(http_get)
777 {
778 zval *options = NULL, *info = NULL;
779 char *URL;
780 int URL_len;
781 phpstr response;
782
783 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
784 RETURN_FALSE;
785 }
786
787 if (info) {
788 zval_dtor(info);
789 array_init(info);
790 }
791
792 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
793 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
794 RETURN_PHPSTR_VAL(response);
795 } else {
796 RETURN_FALSE;
797 }
798 }
799 /* }}} */
800
801 /* {{{ proto string http_head(string url[, array options[, array &info]])
802 *
803 * Performs an HTTP HEAD request on the suppied url.
804 * Returns the HTTP response as string.
805 * See http_get() for a full list of available options.
806 */
807 PHP_FUNCTION(http_head)
808 {
809 zval *options = NULL, *info = NULL;
810 char *URL;
811 int URL_len;
812 phpstr response;
813
814 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
815 RETURN_FALSE;
816 }
817
818 if (info) {
819 zval_dtor(info);
820 array_init(info);
821 }
822
823 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
824 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
825 RETURN_PHPSTR_VAL(response);
826 } else {
827 RETURN_FALSE;
828 }
829 }
830 /* }}} */
831
832 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
833 *
834 * Performs an HTTP POST request, posting data.
835 * Returns the HTTP response as string.
836 * See http_get() for a full list of available options.
837 */
838 PHP_FUNCTION(http_post_data)
839 {
840 zval *options = NULL, *info = NULL;
841 char *URL, *postdata;
842 int postdata_len, URL_len;
843 phpstr response;
844 http_request_body body;
845
846 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
847 RETURN_FALSE;
848 }
849
850 if (info) {
851 zval_dtor(info);
852 array_init(info);
853 }
854
855 body.type = HTTP_REQUEST_BODY_CSTRING;
856 body.data = postdata;
857 body.size = postdata_len;
858
859 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
860 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
861 RETVAL_PHPSTR_VAL(response);
862 } else {
863 RETVAL_FALSE;
864 }
865 }
866 /* }}} */
867
868 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
869 *
870 * Performs an HTTP POST request, posting www-form-urlencoded array data.
871 * Returns the HTTP response as string.
872 * See http_get() for a full list of available options.
873 */
874 PHP_FUNCTION(http_post_fields)
875 {
876 zval *options = NULL, *info = NULL, *fields, *files = NULL;
877 char *URL;
878 int URL_len;
879 phpstr response;
880 http_request_body body;
881
882 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
883 RETURN_FALSE;
884 }
885
886 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
887 RETURN_FALSE;
888 }
889
890 if (info) {
891 zval_dtor(info);
892 array_init(info);
893 }
894
895 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
896 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
897 RETVAL_PHPSTR_VAL(response);
898 } else {
899 RETVAL_FALSE;
900 }
901 http_request_body_dtor(&body);
902 }
903 /* }}} */
904
905 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
906 *
907 * Performs an HTTP PUT request, uploading file.
908 * Returns the HTTP response as string.
909 * See http_get() for a full list of available options.
910 */
911 PHP_FUNCTION(http_put_file)
912 {
913 char *URL, *file;
914 int URL_len, f_len;
915 zval *options = NULL, *info = NULL;
916 phpstr response;
917 php_stream *stream;
918 php_stream_statbuf ssb;
919 http_request_body body;
920
921 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
922 RETURN_FALSE;
923 }
924
925 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
926 RETURN_FALSE;
927 }
928 if (php_stream_stat(stream, &ssb)) {
929 php_stream_close(stream);
930 RETURN_FALSE;
931 }
932
933 if (info) {
934 zval_dtor(info);
935 array_init(info);
936 }
937
938 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
939 body.data = stream;
940 body.size = ssb.sb.st_size;
941
942 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
943 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
944 RETVAL_PHPSTR_VAL(response);
945 } else {
946 RETVAL_FALSE;
947 }
948 http_request_body_dtor(&body);
949 }
950 /* }}} */
951
952 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
953 *
954 * Performs an HTTP PUT request, uploading stream.
955 * Returns the HTTP response as string.
956 * See http_get() for a full list of available options.
957 */
958 PHP_FUNCTION(http_put_stream)
959 {
960 zval *resource, *options = NULL, *info = NULL;
961 char *URL;
962 int URL_len;
963 phpstr response;
964 php_stream *stream;
965 php_stream_statbuf ssb;
966 http_request_body body;
967
968 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
969 RETURN_FALSE;
970 }
971
972 php_stream_from_zval(stream, &resource);
973 if (php_stream_stat(stream, &ssb)) {
974 RETURN_FALSE;
975 }
976
977 if (info) {
978 zval_dtor(info);
979 array_init(info);
980 }
981
982 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
983 body.data = stream;
984 body.size = ssb.sb.st_size;
985
986 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
987 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
988 RETURN_PHPSTR_VAL(response);
989 } else {
990 RETURN_NULL();
991 }
992 }
993 /* }}} */
994
995 /* {{{ proto long http_request_method_register(string method)
996 *
997 * Register a custom request method.
998 */
999 PHP_FUNCTION(http_request_method_register)
1000 {
1001 char *method;
1002 int *method_len;
1003 unsigned long existing;
1004
1005 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1006 RETURN_FALSE;
1007 }
1008 if (existing = http_request_method_exists(1, 0, method)) {
1009 RETURN_LONG((long) existing);
1010 }
1011
1012 RETVAL_LONG((long) http_request_method_register(method));
1013 }
1014 /* }}} */
1015
1016 /* {{{ proto bool http_request_method_unregister(mixed method)
1017 *
1018 * Unregister a previously registered custom request method.
1019 */
1020 PHP_FUNCTION(http_request_method_unregister)
1021 {
1022 zval *method;
1023
1024 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1025 RETURN_FALSE;
1026 }
1027
1028 switch (Z_TYPE_P(method))
1029 {
1030 case IS_OBJECT:
1031 convert_to_string(method);
1032 case IS_STRING:
1033 #include "zend_operators.h"
1034 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1035 convert_to_long(method);
1036 } else {
1037 unsigned long mn;
1038 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1039 RETURN_FALSE;
1040 }
1041 zval_dtor(method);
1042 ZVAL_LONG(method, (long)mn);
1043 }
1044 case IS_LONG:
1045 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1046 default:
1047 RETURN_FALSE;
1048 }
1049 }
1050 /* }}} */
1051
1052 /* {{{ proto long http_request_method_exists(mixed method)
1053 *
1054 * Check if a request method is registered (or available by default).
1055 */
1056 PHP_FUNCTION(http_request_method_exists)
1057 {
1058 IF_RETVAL_USED {
1059 zval *method;
1060
1061 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1062 RETURN_FALSE;
1063 }
1064
1065 switch (Z_TYPE_P(method))
1066 {
1067 case IS_OBJECT:
1068 convert_to_string(method);
1069 case IS_STRING:
1070 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1071 convert_to_long(method);
1072 } else {
1073 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1074 }
1075 case IS_LONG:
1076 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1077 default:
1078 RETURN_FALSE;
1079 }
1080 }
1081 }
1082 /* }}} */
1083
1084 /* {{{ proto string http_request_method_name(long method)
1085 *
1086 * Get the literal string representation of a standard or registered request method.
1087 */
1088 PHP_FUNCTION(http_request_method_name)
1089 {
1090 IF_RETVAL_USED {
1091 long method;
1092
1093 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1094 RETURN_FALSE;
1095 }
1096
1097 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method)), 0);
1098 }
1099 }
1100 /* }}} */
1101 #endif
1102 /* }}} HAVE_CURL */
1103
1104
1105 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1106 *
1107 * Example:
1108 * <pre>
1109 * <?php
1110 * if (!http_auth_basic('mike', 's3c|r3t')) {
1111 * die('<h1>Authorization failed!</h1>');
1112 * }
1113 * ?>
1114 * </pre>
1115 */
1116 PHP_FUNCTION(http_auth_basic)
1117 {
1118 char *realm = NULL, *user, *pass, *suser, *spass;
1119 int r_len, u_len, p_len;
1120
1121 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
1122 RETURN_FALSE;
1123 }
1124
1125 if (!realm) {
1126 realm = "Restricted";
1127 }
1128
1129 if (SUCCESS != http_auth_basic_credentials(&suser, &spass)) {
1130 http_auth_basic_header(realm);
1131 RETURN_FALSE;
1132 }
1133
1134 if (strcasecmp(suser, user)) {
1135 http_auth_basic_header(realm);
1136 RETURN_FALSE;
1137 }
1138
1139 if (strcmp(spass, pass)) {
1140 http_auth_basic_header(realm);
1141 RETURN_FALSE;
1142 }
1143
1144 RETURN_TRUE;
1145 }
1146 /* }}} */
1147
1148 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1149 *
1150 * Example:
1151 * <pre>
1152 * <?php
1153 * function auth_cb($user, $pass)
1154 * {
1155 * global $db;
1156 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1157 * if (strlen($realpass = $db->getOne($query)) {
1158 * return $pass === $realpass;
1159 * }
1160 * return false;
1161 * }
1162 * if (!http_auth_basic_cb('auth_cb')) {
1163 * die('<h1>Authorization failed</h1>');
1164 * }
1165 * ?>
1166 * </pre>
1167 */
1168 PHP_FUNCTION(http_auth_basic_cb)
1169 {
1170 zval *cb;
1171 char *realm = NULL, *user, *pass;
1172 int r_len;
1173
1174 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1175 RETURN_FALSE;
1176 }
1177
1178 if (!realm) {
1179 realm = "Restricted";
1180 }
1181
1182 if (SUCCESS != http_auth_basic_credentials(&user, &pass)) {
1183 http_auth_basic_header(realm);
1184 RETURN_FALSE;
1185 }
1186 {
1187 zval *zparams[2] = {NULL, NULL}, retval;
1188 int result = 0;
1189
1190 MAKE_STD_ZVAL(zparams[0]);
1191 MAKE_STD_ZVAL(zparams[1]);
1192 ZVAL_STRING(zparams[0], user, 0);
1193 ZVAL_STRING(zparams[1], pass, 0);
1194
1195 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1196 &retval, 2, zparams TSRMLS_CC)) {
1197 result = Z_LVAL(retval);
1198 }
1199
1200 efree(user);
1201 efree(pass);
1202 efree(zparams[0]);
1203 efree(zparams[1]);
1204
1205 if (!result) {
1206 http_auth_basic_header(realm);
1207 }
1208
1209 RETURN_BOOL(result);
1210 }
1211 }
1212 /* }}}*/
1213
1214 /* {{{ Sara Golemons http_build_query() */
1215 #ifndef ZEND_ENGINE_2
1216
1217 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1218 Generates a form-encoded query string from an associative array or object. */
1219 PHP_FUNCTION(http_build_query)
1220 {
1221 zval *formdata;
1222 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1223 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1224 phpstr *formstr;
1225
1226 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1227 RETURN_FALSE;
1228 }
1229
1230 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
1231 http_error(E_WARNING, HTTP_E_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1232 RETURN_FALSE;
1233 }
1234
1235 if (!arg_sep_len) {
1236 arg_sep = HTTP_URL_ARGSEP;
1237 }
1238
1239 formstr = phpstr_new();
1240 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))) {
1241 phpstr_free(formstr);
1242 RETURN_FALSE;
1243 }
1244
1245 if (!formstr->used) {
1246 phpstr_free(formstr);
1247 RETURN_NULL();
1248 }
1249
1250 RETURN_PHPSTR_PTR(formstr);
1251 }
1252 /* }}} */
1253 #endif /* !ZEND_ENGINE_2 */
1254 /* }}} */
1255
1256 PHP_FUNCTION(http_test)
1257 {
1258 RETURN_NULL();
1259 }
1260
1261 /*
1262 * Local variables:
1263 * tab-width: 4
1264 * c-basic-offset: 4
1265 * End:
1266 * vim600: noet sw=4 ts=4 fdm=marker
1267 * vim<600: noet sw=4 ts=4
1268 */