- fix PHP4 build
[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_string(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
501 if (SG(request_info).request_method && strcmp(SG(request_info).request_method, "HEAD")) {
502 PHPWRITE(RED, strlen(RED));
503 }
504 RETURN_TRUE;
505 }
506 RETURN_FALSE;
507 }
508 /* }}} */
509
510 /* {{{ proto bool http_send_data(string data)
511 *
512 * Sends raw data with support for (multiple) range requests.
513 *
514 */
515 PHP_FUNCTION(http_send_data)
516 {
517 zval *zdata;
518
519 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
520 RETURN_FALSE;
521 }
522
523 convert_to_string_ex(&zdata);
524 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
525 }
526 /* }}} */
527
528 /* {{{ proto bool http_send_file(string file)
529 *
530 * Sends a file with support for (multiple) range requests.
531 *
532 */
533 PHP_FUNCTION(http_send_file)
534 {
535 char *file;
536 int flen = 0;
537
538 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
539 RETURN_FALSE;
540 }
541 if (!flen) {
542 RETURN_FALSE;
543 }
544
545 RETURN_SUCCESS(http_send_file(file));
546 }
547 /* }}} */
548
549 /* {{{ proto bool http_send_stream(resource stream)
550 *
551 * Sends an already opened stream with support for (multiple) range requests.
552 *
553 */
554 PHP_FUNCTION(http_send_stream)
555 {
556 zval *zstream;
557 php_stream *file;
558
559 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
560 RETURN_FALSE;
561 }
562
563 php_stream_from_zval(file, &zstream);
564 RETURN_SUCCESS(http_send_stream(file));
565 }
566 /* }}} */
567
568 /* {{{ proto string http_chunked_decode(string encoded)
569 *
570 * This function decodes a string that was HTTP-chunked encoded.
571 * Returns false on failure.
572 */
573 PHP_FUNCTION(http_chunked_decode)
574 {
575 char *encoded = NULL, *decoded = NULL;
576 int encoded_len = 0, decoded_len = 0;
577
578 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
579 RETURN_FALSE;
580 }
581
582 if (NULL != http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
583 RETURN_STRINGL(decoded, decoded_len, 0);
584 } else {
585 RETURN_FALSE;
586 }
587 }
588 /* }}} */
589
590 /* {{{ proto array http_split_response(string http_response)
591 *
592 * This function splits an HTTP response into an array with headers and the
593 * content body. The returned array may look simliar to the following example:
594 *
595 * <pre>
596 * <?php
597 * array(
598 * 0 => array(
599 * 'Response Status' => '200 Ok',
600 * 'Content-Type' => 'text/plain',
601 * 'Content-Language' => 'en-US'
602 * ),
603 * 1 => "Hello World!"
604 * );
605 * ?>
606 * </pre>
607 */
608 PHP_FUNCTION(http_split_response)
609 {
610 char *response, *body;
611 int response_len;
612 size_t body_len;
613 zval *zheaders;
614
615 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &response, &response_len) != SUCCESS) {
616 RETURN_FALSE;
617 }
618
619 MAKE_STD_ZVAL(zheaders);
620 array_init(zheaders);
621
622 if (SUCCESS != http_split_response(response, response_len, Z_ARRVAL_P(zheaders), &body, &body_len)) {
623 RETURN_FALSE;
624 }
625
626 array_init(return_value);
627 add_index_zval(return_value, 0, zheaders);
628 add_index_stringl(return_value, 1, body, body_len, 0);
629 }
630 /* }}} */
631
632 /* {{{ proto array http_parse_headers(string header)
633 *
634 */
635 PHP_FUNCTION(http_parse_headers)
636 {
637 char *header;
638 int header_len;
639
640 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
641 RETURN_FALSE;
642 }
643
644 array_init(return_value);
645 if (SUCCESS != http_parse_headers(header, return_value)) {
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_STRINGL(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;
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 * </pre>
741 *
742 * The optional third parameter will be filled with some additional information
743 * in form af an associative array, if supplied, like the following example:
744 * <pre>
745 * <?php
746 * array (
747 * 'effective_url' => 'http://localhost',
748 * 'response_code' => 403,
749 * 'total_time' => 0.017,
750 * 'namelookup_time' => 0.013,
751 * 'connect_time' => 0.014,
752 * 'pretransfer_time' => 0.014,
753 * 'size_upload' => 0,
754 * 'size_download' => 202,
755 * 'speed_download' => 11882,
756 * 'speed_upload' => 0,
757 * 'header_size' => 145,
758 * 'request_size' => 62,
759 * 'ssl_verifyresult' => 0,
760 * 'filetime' => -1,
761 * 'content_length_download' => 202,
762 * 'content_length_upload' => 0,
763 * 'starttransfer_time' => 0.017,
764 * 'content_type' => 'text/html; charset=iso-8859-1',
765 * 'redirect_time' => 0,
766 * 'redirect_count' => 0,
767 * 'private' => '',
768 * 'http_connectcode' => 0,
769 * 'httpauth_avail' => 0,
770 * 'proxyauth_avail' => 0,
771 * )
772 * ?>
773 * </pre>
774 */
775 PHP_FUNCTION(http_get)
776 {
777 zval *options = NULL, *info = NULL;
778 char *URL;
779 int URL_len;
780 phpstr response;
781
782 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
783 RETURN_FALSE;
784 }
785
786 if (info) {
787 zval_dtor(info);
788 array_init(info);
789 }
790
791 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
792 if (SUCCESS == http_get(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
793 RETURN_PHPSTR_VAL(response);
794 } else {
795 RETURN_FALSE;
796 }
797 }
798 /* }}} */
799
800 /* {{{ proto string http_head(string url[, array options[, array &info]])
801 *
802 * Performs an HTTP HEAD request on the suppied url.
803 * Returns the HTTP response as string.
804 * See http_get() for a full list of available options.
805 */
806 PHP_FUNCTION(http_head)
807 {
808 zval *options = NULL, *info = NULL;
809 char *URL;
810 int URL_len;
811 phpstr response;
812
813 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
814 RETURN_FALSE;
815 }
816
817 if (info) {
818 zval_dtor(info);
819 array_init(info);
820 }
821
822 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
823 if (SUCCESS == http_head(URL, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
824 RETURN_PHPSTR_VAL(response);
825 } else {
826 RETURN_FALSE;
827 }
828 }
829 /* }}} */
830
831 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
832 *
833 * Performs an HTTP POST request, posting data.
834 * Returns the HTTP response as string.
835 * See http_get() for a full list of available options.
836 */
837 PHP_FUNCTION(http_post_data)
838 {
839 zval *options = NULL, *info = NULL;
840 char *URL, *postdata;
841 int postdata_len, URL_len;
842 phpstr response;
843 http_request_body body;
844
845 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
846 RETURN_FALSE;
847 }
848
849 if (info) {
850 zval_dtor(info);
851 array_init(info);
852 }
853
854 body.type = HTTP_REQUEST_BODY_CSTRING;
855 body.data = postdata;
856 body.size = postdata_len;
857
858 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
859 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
860 RETVAL_PHPSTR_VAL(response);
861 } else {
862 RETVAL_FALSE;
863 }
864 }
865 /* }}} */
866
867 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
868 *
869 * Performs an HTTP POST request, posting www-form-urlencoded array data.
870 * Returns the HTTP response as string.
871 * See http_get() for a full list of available options.
872 */
873 PHP_FUNCTION(http_post_fields)
874 {
875 zval *options = NULL, *info = NULL, *fields, *files = NULL;
876 char *URL;
877 int URL_len;
878 phpstr response;
879 http_request_body body;
880
881 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|aa/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
882 RETURN_FALSE;
883 }
884
885 if (SUCCESS != http_request_body_fill(&body, Z_ARRVAL_P(fields), files ? Z_ARRVAL_P(files) : NULL)) {
886 RETURN_FALSE;
887 }
888
889 if (info) {
890 zval_dtor(info);
891 array_init(info);
892 }
893
894 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
895 if (SUCCESS == http_post(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
896 RETVAL_PHPSTR_VAL(response);
897 } else {
898 RETVAL_FALSE;
899 }
900 http_request_body_dtor(&body);
901 }
902 /* }}} */
903
904 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
905 *
906 * Performs an HTTP PUT request, uploading file.
907 * Returns the HTTP response as string.
908 * See http_get() for a full list of available options.
909 */
910 PHP_FUNCTION(http_put_file)
911 {
912 char *URL, *file;
913 int URL_len, f_len;
914 zval *options = NULL, *info = NULL;
915 phpstr response;
916 php_stream *stream;
917 php_stream_statbuf ssb;
918 http_request_body body;
919
920 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
921 RETURN_FALSE;
922 }
923
924 if (!(stream = php_stream_open_wrapper(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
925 RETURN_FALSE;
926 }
927 if (php_stream_stat(stream, &ssb)) {
928 php_stream_close(stream);
929 RETURN_FALSE;
930 }
931
932 if (info) {
933 zval_dtor(info);
934 array_init(info);
935 }
936
937 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
938 body.data = stream;
939 body.size = ssb.sb.st_size;
940
941 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
942 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
943 RETVAL_PHPSTR_VAL(response);
944 } else {
945 RETVAL_FALSE;
946 }
947 http_request_body_dtor(&body);
948 }
949 /* }}} */
950
951 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
952 *
953 * Performs an HTTP PUT request, uploading stream.
954 * Returns the HTTP response as string.
955 * See http_get() for a full list of available options.
956 */
957 PHP_FUNCTION(http_put_stream)
958 {
959 zval *resource, *options = NULL, *info = NULL;
960 char *URL;
961 int URL_len;
962 phpstr response;
963 php_stream *stream;
964 php_stream_statbuf ssb;
965 http_request_body body;
966
967 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
968 RETURN_FALSE;
969 }
970
971 php_stream_from_zval(stream, &resource);
972 if (php_stream_stat(stream, &ssb)) {
973 RETURN_FALSE;
974 }
975
976 if (info) {
977 zval_dtor(info);
978 array_init(info);
979 }
980
981 body.type = HTTP_REQUEST_BODY_UPLOADFILE;
982 body.data = stream;
983 body.size = ssb.sb.st_size;
984
985 phpstr_init_ex(&response, HTTP_CURLBUF_SIZE, 0);
986 if (SUCCESS == http_put(URL, &body, options ? Z_ARRVAL_P(options) : NULL, info ? Z_ARRVAL_P(info) : NULL, &response)) {
987 RETURN_PHPSTR_VAL(response);
988 } else {
989 RETURN_NULL();
990 }
991 }
992 /* }}} */
993
994 /* {{{ proto long http_request_method_register(string method)
995 *
996 * Register a custom request method.
997 */
998 PHP_FUNCTION(http_request_method_register)
999 {
1000 char *method;
1001 int *method_len;
1002 unsigned long existing;
1003
1004 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1005 RETURN_FALSE;
1006 }
1007 if (existing = http_request_method_exists(1, 0, method)) {
1008 RETURN_LONG((long) existing);
1009 }
1010
1011 RETVAL_LONG((long) http_request_method_register(method));
1012 }
1013 /* }}} */
1014
1015 /* {{{ proto bool http_request_method_unregister(mixed method)
1016 *
1017 * Unregister a previously registered custom request method.
1018 */
1019 PHP_FUNCTION(http_request_method_unregister)
1020 {
1021 zval *method;
1022
1023 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1024 RETURN_FALSE;
1025 }
1026
1027 switch (Z_TYPE_P(method))
1028 {
1029 case IS_OBJECT:
1030 convert_to_string(method);
1031 case IS_STRING:
1032 #include "zend_operators.h"
1033 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1034 convert_to_long(method);
1035 } else {
1036 unsigned long mn;
1037 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1038 RETURN_FALSE;
1039 }
1040 zval_dtor(method);
1041 ZVAL_LONG(method, (long)mn);
1042 }
1043 case IS_LONG:
1044 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1045 default:
1046 RETURN_FALSE;
1047 }
1048 }
1049 /* }}} */
1050
1051 /* {{{ proto long http_request_method_exists(mixed method)
1052 *
1053 * Check if a request method is registered (or available by default).
1054 */
1055 PHP_FUNCTION(http_request_method_exists)
1056 {
1057 IF_RETVAL_USED {
1058 zval *method;
1059
1060 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1061 RETURN_FALSE;
1062 }
1063
1064 switch (Z_TYPE_P(method))
1065 {
1066 case IS_OBJECT:
1067 convert_to_string(method);
1068 case IS_STRING:
1069 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1070 convert_to_long(method);
1071 } else {
1072 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1073 }
1074 case IS_LONG:
1075 RETURN_LONG((long) http_request_method_exists(0, Z_LVAL_P(method), NULL));
1076 default:
1077 RETURN_FALSE;
1078 }
1079 }
1080 }
1081 /* }}} */
1082
1083 /* {{{ proto string http_request_method_name(long method)
1084 *
1085 * Get the literal string representation of a standard or registered request method.
1086 */
1087 PHP_FUNCTION(http_request_method_name)
1088 {
1089 IF_RETVAL_USED {
1090 long method;
1091
1092 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1093 RETURN_FALSE;
1094 }
1095
1096 RETURN_STRING(estrdup(http_request_method_name((unsigned long) method)), 0);
1097 }
1098 }
1099 /* }}} */
1100 #endif
1101 /* }}} HAVE_CURL */
1102
1103
1104 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1105 *
1106 * Example:
1107 * <pre>
1108 * <?php
1109 * if (!http_auth_basic('mike', 's3c|r3t')) {
1110 * die('<h1>Authorization failed!</h1>');
1111 * }
1112 * ?>
1113 * </pre>
1114 */
1115 PHP_FUNCTION(http_auth_basic)
1116 {
1117 char *realm = NULL, *user, *pass, *suser, *spass;
1118 int r_len, u_len, p_len;
1119
1120 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
1121 RETURN_FALSE;
1122 }
1123
1124 if (!realm) {
1125 realm = "Restricted";
1126 }
1127
1128 if (SUCCESS != http_auth_basic_credentials(&suser, &spass)) {
1129 http_auth_basic_header(realm);
1130 RETURN_FALSE;
1131 }
1132
1133 if (strcasecmp(suser, user)) {
1134 http_auth_basic_header(realm);
1135 RETURN_FALSE;
1136 }
1137
1138 if (strcmp(spass, pass)) {
1139 http_auth_basic_header(realm);
1140 RETURN_FALSE;
1141 }
1142
1143 RETURN_TRUE;
1144 }
1145 /* }}} */
1146
1147 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1148 *
1149 * Example:
1150 * <pre>
1151 * <?php
1152 * function auth_cb($user, $pass)
1153 * {
1154 * global $db;
1155 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1156 * if (strlen($realpass = $db->getOne($query)) {
1157 * return $pass === $realpass;
1158 * }
1159 * return false;
1160 * }
1161 * if (!http_auth_basic_cb('auth_cb')) {
1162 * die('<h1>Authorization failed</h1>');
1163 * }
1164 * ?>
1165 * </pre>
1166 */
1167 PHP_FUNCTION(http_auth_basic_cb)
1168 {
1169 zval *cb;
1170 char *realm = NULL, *user, *pass;
1171 int r_len;
1172
1173 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1174 RETURN_FALSE;
1175 }
1176
1177 if (!realm) {
1178 realm = "Restricted";
1179 }
1180
1181 if (SUCCESS != http_auth_basic_credentials(&user, &pass)) {
1182 http_auth_basic_header(realm);
1183 RETURN_FALSE;
1184 }
1185 {
1186 zval *zparams[2] = {NULL, NULL}, retval;
1187 int result = 0;
1188
1189 MAKE_STD_ZVAL(zparams[0]);
1190 MAKE_STD_ZVAL(zparams[1]);
1191 ZVAL_STRING(zparams[0], user, 0);
1192 ZVAL_STRING(zparams[1], pass, 0);
1193
1194 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1195 &retval, 2, zparams TSRMLS_CC)) {
1196 result = Z_LVAL(retval);
1197 }
1198
1199 efree(user);
1200 efree(pass);
1201 efree(zparams[0]);
1202 efree(zparams[1]);
1203
1204 if (!result) {
1205 http_auth_basic_header(realm);
1206 }
1207
1208 RETURN_BOOL(result);
1209 }
1210 }
1211 /* }}}*/
1212
1213 /* {{{ Sara Golemons http_build_query() */
1214 #ifndef ZEND_ENGINE_2
1215
1216 /* {{{ proto string http_build_query(mixed formdata [, string prefix[, string arg_separator]])
1217 Generates a form-encoded query string from an associative array or object. */
1218 PHP_FUNCTION(http_build_query)
1219 {
1220 zval *formdata;
1221 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
1222 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
1223 phpstr *formstr;
1224
1225 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
1226 RETURN_FALSE;
1227 }
1228
1229 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
1230 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Parameter 1 expected to be Array or Object. Incorrect value given.");
1231 RETURN_FALSE;
1232 }
1233
1234 if (!arg_sep_len) {
1235 arg_sep = HTTP_URL_ARGSEP;
1236 }
1237
1238 formstr = phpstr_new();
1239 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))) {
1240 phpstr_free(formstr);
1241 RETURN_FALSE;
1242 }
1243
1244 if (!formstr->used) {
1245 phpstr_free(formstr);
1246 RETURN_NULL();
1247 }
1248
1249 RETURN_PHPSTR_PTR(formstr);
1250 }
1251 /* }}} */
1252 #endif /* !ZEND_ENGINE_2 */
1253 /* }}} */
1254
1255 PHP_FUNCTION(http_test)
1256 {
1257 }
1258
1259 /*
1260 * Local variables:
1261 * tab-width: 4
1262 * c-basic-offset: 4
1263 * End:
1264 * vim600: noet sw=4 ts=4 fdm=marker
1265 * vim<600: noet sw=4 ts=4
1266 */
1267