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