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