- simplify
[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
22 #include "php.h"
23 #include "ext/standard/info.h"
24 #include "ext/session/php_session.h"
25 #include "ext/standard/php_string.h"
26
27 #include "SAPI.h"
28
29 #include "phpstr/phpstr.h"
30
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_auth_api.h"
35 #include "php_http_curl_api.h"
36 #include "php_http_cache_api.h"
37 #include "php_http_curl_api.h"
38 #include "php_http_date_api.h"
39 #include "php_http_headers_api.h"
40 #include "php_http_message_api.h"
41 #include "php_http_send_api.h"
42 #include "php_http_url_api.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 php_error_docref(NULL TSRMLS_CC, E_WARNING, "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", sizeof("application/x-octetstream") - 1));
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_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
299 }
300 RETURN_BOOL(http_modified_match("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_etag_match("HTTP_IF_MATCH", etag));
322 }
323 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag));
324 }
325 /* }}} */
326
327 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
328 *
329 * If timestamp_or_exires 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, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
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, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
391 }
392 /* }}} */
393
394 /* {{{ proto string ob_httpetaghandler(string data, int mode)
395 *
396 * For use with ob_start().
397 * Note that this has to be started as first output buffer.
398 * WARNING: Don't use with http_send_*().
399 */
400 PHP_FUNCTION(ob_httpetaghandler)
401 {
402 char *data;
403 int data_len;
404 long mode;
405
406 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
407 RETURN_FALSE;
408 }
409
410 if (mode & PHP_OUTPUT_HANDLER_START) {
411 if (HTTP_G(etag_started)) {
412 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler can only be used once");
413 RETURN_STRINGL(data, data_len, 1);
414 }
415 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL);
416 HTTP_G(etag_started) = 1;
417 }
418
419 if (OG(ob_nesting_level) > 1) {
420 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler must be started prior to other output buffers");
421 RETURN_STRINGL(data, data_len, 1);
422 }
423
424 Z_TYPE_P(return_value) = IS_STRING;
425 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
426 }
427 /* }}} */
428
429 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
430 *
431 * Redirect to a given url.
432 * The supplied url will be expanded with http_absolute_uri(), the params array will
433 * be treated with http_build_query() and the session identification will be appended
434 * if session is true.
435 *
436 * Depending on permanent the redirection will be issued with a permanent
437 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
438 * status code.
439 *
440 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
441 * if the client doesn't redirect immediatly.
442 */
443 PHP_FUNCTION(http_redirect)
444 {
445 int url_len;
446 size_t query_len = 0;
447 zend_bool session = 0, permanent = 0;
448 zval *params = NULL;
449 char *query, *url, *URI,
450 LOC[HTTP_URI_MAXLEN + sizeof("Location: ")],
451 RED[HTTP_URI_MAXLEN * 2 + sizeof("Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n")];
452
453 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
454 RETURN_FALSE;
455 }
456
457 /* append session info */
458 if (session && (PS(session_status) == php_session_active)) {
459 if (!params) {
460 MAKE_STD_ZVAL(params);
461 array_init(params);
462 }
463 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
464 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
465 }
466 }
467
468 /* treat params array with http_build_query() */
469 if (params) {
470 if (SUCCESS != http_urlencode_hash_ex(Z_ARRVAL_P(params), 0, NULL, 0, &query, &query_len)) {
471 RETURN_FALSE;
472 }
473 }
474
475 URI = http_absolute_uri(url);
476
477 if (query_len) {
478 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s?%s", URI, query);
479 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
480 efree(query);
481 } else {
482 snprintf(LOC, HTTP_URI_MAXLEN + sizeof("Location: "), "Location: %s", URI);
483 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
484 }
485 efree(URI);
486
487 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
488 php_body_write(RED, strlen(RED) TSRMLS_CC);
489 RETURN_TRUE;
490 }
491 RETURN_FALSE;
492 }
493 /* }}} */
494
495 /* {{{ proto bool http_send_data(string data)
496 *
497 * Sends raw data with support for (multiple) range requests.
498 *
499 */
500 PHP_FUNCTION(http_send_data)
501 {
502 zval *zdata;
503
504 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
505 RETURN_FALSE;
506 }
507
508 convert_to_string_ex(&zdata);
509 http_send_header("Accept-Ranges: bytes");
510 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
511 }
512 /* }}} */
513
514 /* {{{ proto bool http_send_file(string file)
515 *
516 * Sends a file with support for (multiple) range requests.
517 *
518 */
519 PHP_FUNCTION(http_send_file)
520 {
521 char *file;
522 int flen = 0;
523
524 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
525 RETURN_FALSE;
526 }
527 if (!flen) {
528 RETURN_FALSE;
529 }
530
531 http_send_header("Accept-Ranges: bytes");
532 RETURN_SUCCESS(http_send_file(file));
533 }
534 /* }}} */
535
536 /* {{{ proto bool http_send_stream(resource stream)
537 *
538 * Sends an already opened stream with support for (multiple) range requests.
539 *
540 */
541 PHP_FUNCTION(http_send_stream)
542 {
543 zval *zstream;
544 php_stream *file;
545
546 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
547 RETURN_FALSE;
548 }
549
550 php_stream_from_zval(file, &zstream);
551 http_send_header("Accept-Ranges: bytes");
552 RETURN_SUCCESS(http_send_stream(file));
553 }
554 /* }}} */
555
556 /* {{{ proto string http_chunked_decode(string encoded)
557 *
558 * This function decodes a string that was HTTP-chunked encoded.
559 * Returns false on failure.
560 */
561 PHP_FUNCTION(http_chunked_decode)
562 {
563 char *encoded = NULL, *decoded = NULL;
564 int encoded_len = 0, decoded_len = 0;
565
566 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
567 RETURN_FALSE;
568 }
569
570 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
571 RETURN_STRINGL(decoded, decoded_len, 0);
572 } else {
573 RETURN_FALSE;
574 }
575 }
576 /* }}} */
577
578 /* {{{ proto array http_split_response(string http_response)
579 *
580 * This function splits an HTTP response into an array with headers and the
581 * content body. The returned array may look simliar to the following example:
582 *
583 * <pre>
584 * <?php
585 * array(
586 * 0 => array(
587 * 'Status' => '200 Ok',
588 * 'Content-Type' => 'text/plain',
589
590 * 'Content-Language' => 'en-US'
591 * ),
592 * 1 => "Hello World!"
593 * );
594 * ?>
595 * </pre>
596 */
597 PHP_FUNCTION(http_split_response)
598 {
599 zval *zresponse, *zbody, *zheaders;
600
601 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
602 RETURN_FALSE;
603 }
604
605 convert_to_string(zresponse);
606
607 MAKE_STD_ZVAL(zbody);
608 MAKE_STD_ZVAL(zheaders);
609 array_init(zheaders);
610
611 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
612 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
613 RETURN_FALSE;
614 }
615
616 array_init(return_value);
617 add_index_zval(return_value, 0, zheaders);
618 add_index_zval(return_value, 1, zbody);
619 }
620 /* }}} */
621
622 /* {{{ proto array http_parse_headers(string header)
623 *
624 */
625 PHP_FUNCTION(http_parse_headers)
626 {
627 char *header, *rnrn;
628 int header_len;
629
630 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
631 RETURN_FALSE;
632 }
633
634 array_init(return_value);
635
636 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
637 header_len = rnrn - header + 2;
638 }
639 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
640 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP headers");
641 zval_dtor(return_value);
642 RETURN_FALSE;
643 }
644 }
645 /* }}}*/
646
647 /* {{{ proto array http_get_request_headers(void)
648 *
649 */
650 PHP_FUNCTION(http_get_request_headers)
651 {
652 NO_ARGS;
653
654 array_init(return_value);
655 http_get_request_headers(return_value);
656 }
657 /* }}} */
658
659 /* {{{ HAVE_CURL */
660 #ifdef HTTP_HAVE_CURL
661
662 /* {{{ proto string http_get(string url[, array options[, array &info]])
663 *
664 * Performs an HTTP GET request on the supplied url.
665 *
666 * The second parameter is expected to be an associative
667 * array where the following keys will be recognized:
668 * <pre>
669 * - redirect: int, whether and how many redirects to follow
670 * - unrestrictedauth: bool, whether to continue sending credentials on
671 * redirects to a different host
672 * - proxyhost: string, proxy host in "host[:port]" format
673 * - proxyport: int, use another proxy port as specified in proxyhost
674 * - proxyauth: string, proxy credentials in "user:pass" format
675 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
676 * - httpauth: string, http credentials in "user:pass" format
677 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
678 * - compress: bool, whether to allow gzip/deflate content encoding
679 * (defaults to true)
680 * - port: int, use another port as specified in the url
681 * - referer: string, the referer to sends
682 * - useragent: string, the user agent to send
683 * (defaults to PECL::HTTP/version (PHP/version)))
684 * - headers: array, list of custom headers as associative array
685 * like array("header" => "value")
686 * - cookies: array, list of cookies as associative array
687 * like array("cookie" => "value")
688 * - cookiestore: string, path to a file where cookies are/will be stored
689 * - resume: int, byte offset to start the download from;
690 * if the server supports ranges
691 * - maxfilesize: int, maximum file size that should be downloaded;
692 * has no effect, if the size of the requested entity is not known
693 * - lastmodified: int, timestamp for If-(Un)Modified-Since header
694 * - timeout: int, seconds the request may take
695 * - connecttimeout: int, seconds the connect may take
696 * </pre>
697 *
698 * The optional third parameter will be filled with some additional information
699 * in form af an associative array, if supplied, like the following example:
700 * <pre>
701 * <?php
702 * array (
703 * 'effective_url' => 'http://localhost',
704 * 'response_code' => 403,
705 * 'total_time' => 0.017,
706 * 'namelookup_time' => 0.013,
707 * 'connect_time' => 0.014,
708 * 'pretransfer_time' => 0.014,
709 * 'size_upload' => 0,
710 * 'size_download' => 202,
711 * 'speed_download' => 11882,
712 * 'speed_upload' => 0,
713 * 'header_size' => 145,
714 * 'request_size' => 62,
715 * 'ssl_verifyresult' => 0,
716 * 'filetime' => -1,
717 * 'content_length_download' => 202,
718 * 'content_length_upload' => 0,
719 * 'starttransfer_time' => 0.017,
720 * 'content_type' => 'text/html; charset=iso-8859-1',
721 * 'redirect_time' => 0,
722 * 'redirect_count' => 0,
723 * 'private' => '',
724 * 'http_connectcode' => 0,
725 * 'httpauth_avail' => 0,
726 * 'proxyauth_avail' => 0,
727 * )
728 * ?>
729 * </pre>
730 */
731 PHP_FUNCTION(http_get)
732 {
733 char *URL, *data = NULL;
734 size_t data_len = 0;
735 int URL_len;
736 zval *options = NULL, *info = NULL;
737
738 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
739 RETURN_FALSE;
740 }
741
742 if (info) {
743 zval_dtor(info);
744 array_init(info);
745 }
746
747 if (SUCCESS == http_get(URL, Z_ARRVAL_P(options), Z_ARRVAL_P(info), &data, &data_len)) {
748 RETURN_STRINGL(data, data_len, 0);
749 } else {
750 RETURN_FALSE;
751 }
752 }
753 /* }}} */
754
755 /* {{{ proto string http_head(string url[, array options[, array &info]])
756 *
757 * Performs an HTTP HEAD request on the suppied url.
758 * Returns the HTTP response as string.
759 * See http_get() for a full list of available options.
760 */
761 PHP_FUNCTION(http_head)
762 {
763 char *URL, *data = NULL;
764 size_t data_len = 0;
765 int URL_len;
766 zval *options = NULL, *info = NULL;
767
768 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
769 RETURN_FALSE;
770 }
771
772 if (info) {
773 zval_dtor(info);
774 array_init(info);
775 }
776
777 if (SUCCESS == http_head(URL, Z_ARRVAL_P(options), Z_ARRVAL_P(info), &data, &data_len)) {
778 RETURN_STRINGL(data, data_len, 0);
779 } else {
780 RETURN_FALSE;
781 }
782 }
783 /* }}} */
784
785 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
786 *
787 * Performs an HTTP POST request, posting data.
788 * Returns the HTTP response as string.
789 * See http_get() for a full list of available options.
790 */
791 PHP_FUNCTION(http_post_data)
792 {
793 char *URL, *postdata, *data = NULL;
794 size_t data_len = 0;
795 int postdata_len, URL_len;
796 zval *options = NULL, *info = NULL;
797
798 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
799 RETURN_FALSE;
800 }
801
802 if (info) {
803 zval_dtor(info);
804 array_init(info);
805 }
806
807 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, Z_ARRVAL_P(options), Z_ARRVAL_P(info), &data, &data_len)) {
808 RETURN_STRINGL(data, data_len, 0);
809 } else {
810 RETURN_FALSE;
811 }
812 }
813 /* }}} */
814
815 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
816 *
817 * Performs an HTTP POST request, posting www-form-urlencoded array data.
818 * Returns the HTTP response as string.
819 * See http_get() for a full list of available options.
820 */
821 PHP_FUNCTION(http_post_array)
822 {
823 char *URL, *data = NULL;
824 size_t data_len = 0;
825 int URL_len;
826 zval *options = NULL, *info = NULL, *postdata;
827
828 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
829 RETURN_FALSE;
830 }
831
832 if (info) {
833 zval_dtor(info);
834 array_init(info);
835 }
836
837 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), Z_ARRVAL_P(options), Z_ARRVAL_P(info), &data, &data_len)) {
838 RETURN_STRINGL(data, data_len, 0);
839 } else {
840 RETURN_FALSE;
841 }
842 }
843 /* }}} */
844
845 #endif
846 /* }}} HAVE_CURL */
847
848
849 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
850 *
851 * Example:
852 * <pre>
853 * <?php
854 * if (!http_auth_basic('mike', 's3c|r3t')) {
855 * die('<h1>Authorization failed!</h1>');
856 * }
857 * ?>
858 * </pre>
859 */
860 PHP_FUNCTION(http_auth_basic)
861 {
862 char *realm = NULL, *user, *pass, *suser, *spass;
863 int r_len, u_len, p_len;
864
865 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
866 RETURN_FALSE;
867 }
868
869 if (!realm) {
870 realm = "Restricted";
871 }
872
873 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
874 http_auth_header("Basic", realm);
875 RETURN_FALSE;
876 }
877
878 if (strcasecmp(suser, user)) {
879 http_auth_header("Basic", realm);
880 RETURN_FALSE;
881 }
882
883 if (strcmp(spass, pass)) {
884 http_auth_header("Basic", realm);
885 RETURN_FALSE;
886 }
887
888 RETURN_TRUE;
889 }
890 /* }}} */
891
892 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
893 *
894 * Example:
895 * <pre>
896 * <?php
897 * function auth_cb($user, $pass)
898 * {
899 * global $db;
900 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
901 * if (strlen($realpass = $db->getOne($query)) {
902 * return $pass === $realpass;
903 * }
904 * return false;
905 * }
906 * if (!http_auth_basic_cb('auth_cb')) {
907 * die('<h1>Authorization failed</h1>');
908 * }
909 * ?>
910 * </pre>
911 */
912 PHP_FUNCTION(http_auth_basic_cb)
913 {
914 zval *cb;
915 char *realm = NULL, *user, *pass;
916 int r_len;
917
918 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
919 RETURN_FALSE;
920 }
921
922 if (!realm) {
923 realm = "Restricted";
924 }
925
926 if (SUCCESS != http_auth_credentials(&user, &pass)) {
927 http_auth_header("Basic", realm);
928 RETURN_FALSE;
929 }
930 {
931 zval *zparams[2] = {NULL, NULL}, retval;
932 int result = 0;
933
934 MAKE_STD_ZVAL(zparams[0]);
935 MAKE_STD_ZVAL(zparams[1]);
936 ZVAL_STRING(zparams[0], user, 0);
937 ZVAL_STRING(zparams[1], pass, 0);
938
939 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
940 &retval, 2, zparams TSRMLS_CC)) {
941 result = Z_LVAL(retval);
942 }
943
944 efree(user);
945 efree(pass);
946 efree(zparams[0]);
947 efree(zparams[1]);
948
949 if (!result) {
950 http_auth_header("Basic", realm);
951 }
952
953 RETURN_BOOL(result);
954 }
955 }
956 /* }}}*/
957
958 /* {{{ Sara Golemons http_build_query() */
959 #ifndef ZEND_ENGINE_2
960
961 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
962 Generates a form-encoded query string from an associative array or object. */
963 PHP_FUNCTION(http_build_query)
964 {
965 zval *formdata;
966 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
967 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
968 phpstr *formstr;
969
970 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
971 RETURN_FALSE;
972 }
973
974 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
975 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given.");
976 RETURN_FALSE;
977 }
978
979 if (!arg_sep_len) {
980 arg_sep = HTTP_URL_ARGSEP_DEFAULT;
981 }
982
983 formstr = phpstr_new();
984 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) TSRMLS_CC)) {
985 phpstr_free(formstr);
986 RETURN_FALSE;
987 }
988
989 if (!formstr->used) {
990 phpstr_free(formstr);
991 RETURN_NULL();
992 }
993
994 RETURN_PHPSTR_PTR(formstr);
995 }
996 /* }}} */
997 #endif /* !ZEND_ENGINE_2 */
998 /* }}} */
999
1000 PHP_FUNCTION(http_test)
1001 {
1002 #define HTTP_MESSAGE_STR \
1003 "GET / HTTP/1.1\r\n" \
1004 "Content-Type: foo/bar\r\n" \
1005 "Robots: Noindex,Nofollow\r\n" \
1006 "\r\n" \
1007 "Body Data!\n"
1008 #define HTTP_MESSAGE_LEN lenof(HTTP_MESSAGE_STR)
1009 http_message *msg = http_message_parse(HTTP_MESSAGE_STR, HTTP_MESSAGE_LEN);
1010 char *str;
1011 size_t len;
1012
1013 http_message_tostring(msg, &str, &len);
1014
1015 RETVAL_STRINGL(str, len, 0);
1016 http_message_free(msg);
1017 }
1018
1019 /*
1020 * Local variables:
1021 * tab-width: 4
1022 * c-basic-offset: 4
1023 * End:
1024 * vim600: noet sw=4 ts=4 fdm=marker
1025 * vim<600: noet sw=4 ts=4
1026 */
1027