- fix some cc warnings on x64
[m6w6/ext-http] / http_functions.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
18 #include "php_http.h"
19
20 #include "php_ini.h"
21 #include "ext/standard/php_string.h"
22 #include "zend_operators.h"
23
24 #ifdef HTTP_HAVE_SESSION
25 # include "ext/session/php_session.h"
26 #endif
27
28 #include "php_http_api.h"
29 #include "php_http_cache_api.h"
30 #include "php_http_cookie_api.h"
31 #include "php_http_date_api.h"
32 #include "php_http_encoding_api.h"
33 #include "php_http_headers_api.h"
34 #include "php_http_message_api.h"
35 #include "php_http_request_api.h"
36 #include "php_http_request_method_api.h"
37 #include "php_http_persistent_handle_api.h"
38 #include "php_http_send_api.h"
39 #include "php_http_url_api.h"
40
41 /* {{{ proto string http_date([int timestamp])
42 Compose a valid HTTP date regarding RFC 1123 looking like: "Wed, 22 Dec 2004 11:34:47 GMT" */
43 PHP_FUNCTION(http_date)
44 {
45 long t = -1;
46
47 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
48 RETURN_FALSE;
49 }
50
51 if (t == -1) {
52 t = HTTP_G->request.time;
53 }
54
55 RETURN_STRING(http_date(t), 0);
56 }
57 /* }}} */
58
59 /* {{{ proto string http_build_url([mixed url[, mixed parts[, int flags = HTTP_URL_REPLACE|HTTP_URL_FROM_ENV[, array &new_url]]]])
60 Build an URL. */
61 PHP_FUNCTION(http_build_url)
62 {
63 char *url_str = NULL;
64 size_t url_len = 0;
65 long flags = HTTP_URL_REPLACE|HTTP_URL_FROM_ENV;
66 zval *z_old_url = NULL, *z_new_url = NULL, *z_composed_url = NULL;
67 php_url *old_url = NULL, *new_url = NULL, *composed_url = NULL;
68
69 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!/z!/lz", &z_old_url, &z_new_url, &flags, &z_composed_url) != SUCCESS) {
70 RETURN_FALSE;
71 }
72
73 if (z_new_url) {
74 if (Z_TYPE_P(z_new_url) == IS_ARRAY || Z_TYPE_P(z_new_url) == IS_OBJECT) {
75 new_url = http_url_from_struct(NULL, HASH_OF(z_new_url));
76 } else {
77 convert_to_string(z_new_url);
78 if (!(new_url = php_url_parse_ex(Z_STRVAL_P(z_new_url), Z_STRLEN_P(z_new_url)))) {
79 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_new_url));
80 RETURN_FALSE;
81 }
82 }
83 }
84
85 if (z_old_url) {
86 if (Z_TYPE_P(z_old_url) == IS_ARRAY || Z_TYPE_P(z_old_url) == IS_OBJECT) {
87 old_url = http_url_from_struct(NULL, HASH_OF(z_old_url));
88 } else {
89 convert_to_string(z_old_url);
90 if (!(old_url = php_url_parse_ex(Z_STRVAL_P(z_old_url), Z_STRLEN_P(z_old_url)))) {
91 if (new_url) {
92 php_url_free(new_url);
93 }
94 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", Z_STRVAL_P(z_old_url));
95 RETURN_FALSE;
96 }
97 }
98 }
99
100 if (z_composed_url) {
101 http_build_url(flags, old_url, new_url, &composed_url, &url_str, &url_len);
102 http_url_tostruct(composed_url, z_composed_url);
103 php_url_free(composed_url);
104 } else {
105 http_build_url(flags, old_url, new_url, NULL, &url_str, &url_len);
106 }
107
108 if (new_url) {
109 php_url_free(new_url);
110 }
111 if (old_url) {
112 php_url_free(old_url);
113 }
114
115 RETURN_STRINGL(url_str, url_len, 0);
116 }
117 /* }}} */
118
119 /* {{{ proto string http_build_str(array query [, string prefix[, string arg_separator]])
120 Opponent to parse_str(). */
121 PHP_FUNCTION(http_build_str)
122 {
123 zval *formdata;
124 char *prefix = NULL, *arg_sep = INI_STR("arg_separator.output");
125 int prefix_len = 0, arg_sep_len = strlen(arg_sep);
126 phpstr formstr;
127
128 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
129 RETURN_FALSE;
130 }
131
132 if (!arg_sep_len) {
133 arg_sep = HTTP_URL_ARGSEP;
134 arg_sep_len = lenof(HTTP_URL_ARGSEP);
135 }
136
137 phpstr_init(&formstr);
138 if (SUCCESS != http_urlencode_hash_recursive(HASH_OF(formdata), &formstr, arg_sep, arg_sep_len, prefix, prefix_len)) {
139 RETURN_FALSE;
140 }
141
142 if (!formstr.used) {
143 phpstr_dtor(&formstr);
144 RETURN_NULL();
145 }
146
147 RETURN_PHPSTR_VAL(&formstr);
148 }
149 /* }}} */
150
151 #define HTTP_DO_NEGOTIATE_DEFAULT(supported) \
152 { \
153 zval **value; \
154 \
155 zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported)); \
156 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(supported), (void *) &value)) { \
157 RETVAL_ZVAL(*value, 1, 0); \
158 } else { \
159 RETVAL_NULL(); \
160 } \
161 }
162 #define HTTP_DO_NEGOTIATE(type, supported, rs_array) \
163 { \
164 HashTable *result; \
165 if ((result = http_negotiate_ ##type(supported))) { \
166 char *key; \
167 uint key_len; \
168 ulong idx; \
169 \
170 if (zend_hash_num_elements(result) && HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key, &key_len, &idx, 1, NULL)) { \
171 RETVAL_STRINGL(key, key_len-1, 0); \
172 } else { \
173 HTTP_DO_NEGOTIATE_DEFAULT(supported); \
174 } \
175 \
176 if (rs_array) { \
177 zend_hash_copy(Z_ARRVAL_P(rs_array), result, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); \
178 } \
179 \
180 zend_hash_destroy(result); \
181 FREE_HASHTABLE(result); \
182 \
183 } else { \
184 HTTP_DO_NEGOTIATE_DEFAULT(supported); \
185 \
186 if (rs_array) { \
187 HashPosition pos; \
188 zval **value; \
189 \
190 FOREACH_VAL(pos, supported, value) { \
191 convert_to_string_ex(value); \
192 add_assoc_double(rs_array, Z_STRVAL_PP(value), 1.0); \
193 } \
194 } \
195 } \
196 }
197
198 /* {{{ proto string http_negotiate_language(array supported[, array &result])
199 Negotiate the clients preferred language. */
200 PHP_FUNCTION(http_negotiate_language)
201 {
202 zval *supported, *rs_array = NULL;
203
204 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array) != SUCCESS) {
205 RETURN_FALSE;
206 }
207
208 if (rs_array) {
209 zval_dtor(rs_array);
210 array_init(rs_array);
211 }
212
213 HTTP_DO_NEGOTIATE(language, supported, rs_array);
214 }
215 /* }}} */
216
217 /* {{{ proto string http_negotiate_charset(array supported[, array &result])
218 Negotiate the clients preferred charset. */
219 PHP_FUNCTION(http_negotiate_charset)
220 {
221 zval *supported, *rs_array = NULL;
222
223 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array) != SUCCESS) {
224 RETURN_FALSE;
225 }
226
227 if (rs_array) {
228 zval_dtor(rs_array);
229 array_init(rs_array);
230 }
231
232 HTTP_DO_NEGOTIATE(charset, supported, rs_array);
233 }
234 /* }}} */
235
236 /* {{{ proto string http_negotiate_content_type(array supported[, array &result])
237 Negotiate the clients preferred content type. */
238 PHP_FUNCTION(http_negotiate_content_type)
239 {
240 zval *supported, *rs_array = NULL;
241
242 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|z", &supported, &rs_array)) {
243 RETURN_FALSE;
244 }
245
246 if (rs_array) {
247 zval_dtor(rs_array);
248 array_init(rs_array);
249 }
250
251 HTTP_DO_NEGOTIATE(content_type, supported, rs_array);
252 }
253 /* }}} */
254
255 /* {{{ proto bool http_send_status(int status)
256 Send HTTP status code. */
257 PHP_FUNCTION(http_send_status)
258 {
259 int status = 0;
260
261 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
262 RETURN_FALSE;
263 }
264 if (status < 100 || status > 510) {
265 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Invalid HTTP status code (100-510): %d", status);
266 RETURN_FALSE;
267 }
268
269 RETURN_SUCCESS(http_send_status(status));
270 }
271 /* }}} */
272
273 /* {{{ proto bool http_send_last_modified([int timestamp])
274 Send a "Last-Modified" header with a valid HTTP date. */
275 PHP_FUNCTION(http_send_last_modified)
276 {
277 long t = -1;
278
279 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
280 RETURN_FALSE;
281 }
282
283 if (t == -1) {
284 t = HTTP_G->request.time;
285 }
286
287 RETURN_SUCCESS(http_send_last_modified(t));
288 }
289 /* }}} */
290
291 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
292 Send the Content-Type of the sent entity. This is particularly important if you use the http_send() API. */
293 PHP_FUNCTION(http_send_content_type)
294 {
295 char *ct = "application/x-octetstream";
296 int ct_len = lenof("application/x-octetstream");
297
298 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
299 RETURN_FALSE;
300 }
301
302 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
303 }
304 /* }}} */
305
306 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
307 Send the Content-Disposition. */
308 PHP_FUNCTION(http_send_content_disposition)
309 {
310 char *filename;
311 int f_len;
312 zend_bool send_inline = 0;
313
314 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
315 RETURN_FALSE;
316 }
317 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
318 }
319 /* }}} */
320
321 /* {{{ proto bool http_match_modified([int timestamp[, bool for_range = false]])
322 Matches the given unix timestamp against the clients "If-Modified-Since" resp. "If-Unmodified-Since" HTTP headers. */
323 PHP_FUNCTION(http_match_modified)
324 {
325 long t = -1;
326 zend_bool for_range = 0;
327
328 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", &t, &for_range) != SUCCESS) {
329 RETURN_FALSE;
330 }
331
332 // current time if not supplied (senseless though)
333 if (t == -1) {
334 t = HTTP_G->request.time;
335 }
336
337 if (for_range) {
338 RETURN_BOOL(http_match_last_modified("HTTP_IF_UNMODIFIED_SINCE", t));
339 }
340 RETURN_BOOL(http_match_last_modified("HTTP_IF_MODIFIED_SINCE", t));
341 }
342 /* }}} */
343
344 /* {{{ proto bool http_match_etag(string etag[, bool for_range = false])
345 Matches the given ETag against the clients "If-Match" resp. "If-None-Match" HTTP headers. */
346 PHP_FUNCTION(http_match_etag)
347 {
348 int etag_len;
349 char *etag;
350 zend_bool for_range = 0;
351
352 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &etag, &etag_len, &for_range) != SUCCESS) {
353 RETURN_FALSE;
354 }
355
356 if (for_range) {
357 RETURN_BOOL(http_match_etag("HTTP_IF_MATCH", etag));
358 }
359 RETURN_BOOL(http_match_etag("HTTP_IF_NONE_MATCH", etag));
360 }
361 /* }}} */
362
363 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
364 Attempts to cache the sent entity by its last modification date. */
365 PHP_FUNCTION(http_cache_last_modified)
366 {
367 long last_modified = 0, send_modified = 0, t;
368 zval *zlm;
369
370 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
371 RETURN_FALSE;
372 }
373
374 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
375
376 t = HTTP_G->request.time;
377
378 /* 0 or omitted */
379 if (!last_modified) {
380 /* does the client have? (att: caching "forever") */
381 if ((zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE", 1))) {
382 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
383 /* send current time */
384 } else {
385 send_modified = t;
386 }
387 /* negative value is supposed to be expiration time */
388 } else if (last_modified < 0) {
389 last_modified += t;
390 send_modified = t;
391 /* send supplied time explicitly */
392 } else {
393 send_modified = last_modified;
394 }
395
396 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
397 }
398 /* }}} */
399
400 /* {{{ proto bool http_cache_etag([string etag])
401 Attempts to cache the sent entity by its ETag, either supplied or generated by the hash algorithm specified by the INI setting "http.etag.mode". */
402 PHP_FUNCTION(http_cache_etag)
403 {
404 char *etag = NULL;
405 int etag_len = 0;
406
407 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
408 RETURN_FALSE;
409 }
410
411 HTTP_CHECK_HEADERS_SENT(RETURN_FALSE);
412
413 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL)));
414 }
415 /* }}} */
416
417 /* {{{ proto string ob_etaghandler(string data, int mode)
418 For use with ob_start(). Output buffer handler generating an ETag with the hash algorithm specified with the INI setting "http.etag.mode". */
419 PHP_FUNCTION(ob_etaghandler)
420 {
421 char *data;
422 int data_len;
423 long mode;
424
425 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
426 RETURN_FALSE;
427 }
428
429 Z_TYPE_P(return_value) = IS_STRING;
430 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
431 }
432 /* }}} */
433
434 /* {{{ proto void http_throttle(double sec[, int bytes = 40960])
435 Sets the throttle delay and send buffer size for use with http_send() API. */
436 PHP_FUNCTION(http_throttle)
437 {
438 long chunk_size = HTTP_SENDBUF_SIZE;
439 double interval;
440
441 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|l", &interval, &chunk_size)) {
442 return;
443 }
444
445 HTTP_G->send.throttle_delay = interval;
446 HTTP_G->send.buffer_size = chunk_size;
447 }
448 /* }}} */
449
450 /* {{{ proto void http_redirect([string url[, array params[, bool session = false[, int status = 302]]]])
451 Redirect to the given url. */
452 PHP_FUNCTION(http_redirect)
453 {
454 int url_len = 0;
455 size_t query_len = 0;
456 zend_bool session = 0, free_params = 0;
457 zval *params = NULL;
458 long status = HTTP_REDIRECT;
459 char *query = NULL, *url = NULL, *URI, *LOC, *RED = NULL;
460
461 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bl", &url, &url_len, &params, &session, &status) != SUCCESS) {
462 RETURN_FALSE;
463 }
464
465 #ifdef HTTP_HAVE_SESSION
466 /* append session info */
467 if (session) {
468 if (!params) {
469 free_params = 1;
470 MAKE_STD_ZVAL(params);
471 array_init(params);
472 }
473 if (PS(session_status) == php_session_active) {
474 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
475 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not append session information");
476 }
477 }
478 }
479 #endif
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 if (free_params) {
485 zval_dtor(params);
486 FREE_ZVAL(params);
487 }
488 if (query) {
489 efree(query);
490 }
491 RETURN_FALSE;
492 }
493 }
494
495 URI = http_absolute_url_ex(url, HTTP_URL_FROM_ENV);
496
497 if (query_len) {
498 spprintf(&LOC, 0, "Location: %s?%s", URI, query);
499 if (status != 300) {
500 spprintf(&RED, 0, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, query, URI, query);
501 }
502 } else {
503 spprintf(&LOC, 0, "Location: %s", URI);
504 if (status != 300) {
505 spprintf(&RED, 0, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
506 }
507 }
508
509 efree(URI);
510 if (query) {
511 efree(query);
512 }
513 if (free_params) {
514 zval_dtor(params);
515 FREE_ZVAL(params);
516 }
517
518 switch (status) {
519 case 300:
520 RETVAL_SUCCESS(http_send_status_header(status, LOC));
521 efree(LOC);
522 return;
523
524 case HTTP_REDIRECT_PERM:
525 case HTTP_REDIRECT_FOUND:
526 case HTTP_REDIRECT_POST:
527 case HTTP_REDIRECT_PROXY:
528 case HTTP_REDIRECT_TEMP:
529 break;
530
531 case 306:
532 default:
533 http_error_ex(HE_NOTICE, HTTP_E_RUNTIME, "Unsupported redirection status code: %ld", status);
534 case HTTP_REDIRECT:
535 if ( SG(request_info).request_method &&
536 strcasecmp(SG(request_info).request_method, "HEAD") &&
537 strcasecmp(SG(request_info).request_method, "GET")) {
538 status = HTTP_REDIRECT_POST;
539 } else {
540 status = HTTP_REDIRECT_FOUND;
541 }
542 break;
543 }
544
545 RETURN_SUCCESS(http_exit_ex(status, LOC, RED, 1));
546 }
547 /* }}} */
548
549 /* {{{ proto bool http_send_data(string data)
550 Sends raw data with support for (multiple) range requests. */
551 PHP_FUNCTION(http_send_data)
552 {
553 int data_len;
554 char *data_buf;
555
556 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data_buf, &data_len) != SUCCESS) {
557 RETURN_FALSE;
558 }
559
560 RETURN_SUCCESS(http_send_data(data_buf, data_len));
561 }
562 /* }}} */
563
564 /* {{{ proto bool http_send_file(string file)
565 Sends a file with support for (multiple) range requests. */
566 PHP_FUNCTION(http_send_file)
567 {
568 char *file;
569 int flen = 0;
570
571 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &file, &flen) != SUCCESS) {
572 RETURN_FALSE;
573 }
574 if (!flen) {
575 RETURN_FALSE;
576 }
577
578 RETURN_SUCCESS(http_send_file(file));
579 }
580 /* }}} */
581
582 /* {{{ proto bool http_send_stream(resource stream)
583 Sends an already opened stream with support for (multiple) range requests. */
584 PHP_FUNCTION(http_send_stream)
585 {
586 zval *zstream;
587 php_stream *file;
588
589 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
590 RETURN_FALSE;
591 }
592
593 php_stream_from_zval(file, &zstream);
594 RETURN_SUCCESS(http_send_stream(file));
595 }
596 /* }}} */
597
598 /* {{{ proto string http_chunked_decode(string encoded)
599 Decodes a string that was HTTP-chunked encoded. */
600 PHP_FUNCTION(http_chunked_decode)
601 {
602 char *encoded = NULL, *decoded = NULL;
603 size_t decoded_len = 0;
604 int encoded_len = 0;
605
606 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
607 RETURN_FALSE;
608 }
609
610 if (NULL != http_encoding_dechunk(encoded, encoded_len, &decoded, &decoded_len)) {
611 RETURN_STRINGL(decoded, (int) decoded_len, 0);
612 } else {
613 RETURN_FALSE;
614 }
615 }
616 /* }}} */
617
618 /* {{{ proto object http_parse_message(string message)
619 Parses (a) http_message(s) into a simple recursive object structure. */
620 PHP_FUNCTION(http_parse_message)
621 {
622 char *message;
623 int message_len;
624 http_message *msg = NULL;
625
626 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &message, &message_len)) {
627 RETURN_NULL();
628 }
629
630 if ((msg = http_message_parse(message, message_len))) {
631 object_init(return_value);
632 http_message_tostruct_recursive(msg, return_value);
633 http_message_free(&msg);
634 } else {
635 RETURN_NULL();
636 }
637 }
638 /* }}} */
639
640 /* {{{ proto array http_parse_headers(string header)
641 Parses HTTP headers into an associative array. */
642 PHP_FUNCTION(http_parse_headers)
643 {
644 char *header;
645 int header_len;
646
647 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
648 RETURN_FALSE;
649 }
650
651 array_init(return_value);
652 if (SUCCESS != http_parse_headers(header, return_value)) {
653 zval_dtor(return_value);
654 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Failed to parse headers");
655 RETURN_FALSE;
656 }
657 }
658 /* }}}*/
659
660 /* {{{ proto object http_parse_cookie(string cookie[, int flags[, array allowed_extras]])
661 Parses HTTP cookies like sent in a response into a struct. */
662 PHP_FUNCTION(http_parse_cookie)
663 {
664 char *cookie, **allowed_extras = NULL;
665 int i = 0, cookie_len;
666 long flags = 0;
667 zval *allowed_extras_array = NULL, **entry = NULL;
668 HashPosition pos;
669 http_cookie_list list;
670
671 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la!", &cookie, &cookie_len, &flags, &allowed_extras_array)) {
672 RETURN_FALSE;
673 }
674
675 if (allowed_extras_array) {
676 allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
677 FOREACH_VAL(pos, allowed_extras_array, entry) {
678 ZVAL_ADDREF(*entry);
679 convert_to_string_ex(entry);
680 allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
681 zval_ptr_dtor(entry);
682 }
683 }
684
685 if (http_parse_cookie_ex(&list, cookie, flags, allowed_extras)) {
686 object_init(return_value);
687 http_cookie_list_tostruct(&list, return_value);
688 http_cookie_list_dtor(&list);
689 } else {
690 RETVAL_FALSE;
691 }
692
693 if (allowed_extras) {
694 for (i = 0; allowed_extras[i]; ++i) {
695 efree(allowed_extras[i]);
696 }
697 efree(allowed_extras);
698 }
699 }
700 /* }}} */
701
702 /* {{{ proto string http_build_cookie(array cookie)
703 Build a cookie string from an array/object like returned by http_parse_cookie(). */
704 PHP_FUNCTION(http_build_cookie)
705 {
706 char *str = NULL;
707 size_t len = 0;
708 zval *strct;
709 http_cookie_list list;
710
711 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &strct)) {
712 RETURN_FALSE;
713 }
714
715 http_cookie_list_fromstruct(&list, strct);
716 http_cookie_list_tostring(&list, &str, &len);
717 http_cookie_list_dtor(&list);
718
719 RETURN_STRINGL(str, len, 0);
720 }
721 /* }}} */
722
723 /* {{{ proto object http_parse_params(string param[, int flags = HTTP_PARAMS_DEFAULT])
724 Parse parameter list. */
725 PHP_FUNCTION(http_parse_params)
726 {
727 char *param;
728 int param_len;
729 zval *params;
730 long flags = HTTP_PARAMS_DEFAULT;
731
732 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &param, &param_len, &flags)) {
733 RETURN_FALSE;
734 }
735
736 params = ecalloc(1, sizeof(zval));
737 array_init(params);
738 if (SUCCESS != http_parse_params(param, flags, Z_ARRVAL_P(params))) {
739 zval_dtor(params);
740 FREE_ZVAL(params);
741 RETURN_FALSE;
742 }
743 object_init(return_value);
744 add_property_zval(return_value, "params", params);
745 }
746 /* }}} */
747
748 /* {{{ proto array http_get_request_headers(void)
749 Get a list of incoming HTTP headers. */
750 PHP_FUNCTION(http_get_request_headers)
751 {
752 NO_ARGS;
753
754 array_init(return_value);
755 http_get_request_headers(Z_ARRVAL_P(return_value));
756 }
757 /* }}} */
758
759 /* {{{ proto string http_get_request_body(void)
760 Get the raw request body (e.g. POST or PUT data). */
761 PHP_FUNCTION(http_get_request_body)
762 {
763 char *body;
764 size_t length;
765
766 NO_ARGS;
767
768 if (SUCCESS == http_get_request_body(&body, &length)) {
769 RETURN_STRINGL(body, (int) length, 0);
770 } else {
771 RETURN_NULL();
772 }
773 }
774 /* }}} */
775
776 /* {{{ proto resource http_get_request_body_stream(void)
777 Create a stream to read the raw request body (e.g. POST or PUT data). This function can only be used once if the request method was another than POST. */
778 PHP_FUNCTION(http_get_request_body_stream)
779 {
780 php_stream *s;
781
782 NO_ARGS;
783
784 if ((s = http_get_request_body_stream())) {
785 php_stream_to_zval(s, return_value);
786 } else {
787 http_error(HE_WARNING, HTTP_E_RUNTIME, "Failed to create request body stream");
788 RETURN_NULL();
789 }
790 }
791 /* }}} */
792
793 /* {{{ proto bool http_match_request_header(string header, string value[, bool match_case = false])
794 Match an incoming HTTP header. */
795 PHP_FUNCTION(http_match_request_header)
796 {
797 char *header, *value;
798 int header_len, value_len;
799 zend_bool match_case = 0;
800
801 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &header, &header_len, &value, &value_len, &match_case)) {
802 RETURN_FALSE;
803 }
804
805 RETURN_BOOL(http_match_request_header_ex(header, value, match_case));
806 }
807 /* }}} */
808
809 /* {{{ proto object http_persistent_handles_count() */
810 PHP_FUNCTION(http_persistent_handles_count)
811 {
812 NO_ARGS;
813 object_init(return_value);
814 if (!http_persistent_handle_statall_ex(HASH_OF(return_value))) {
815 zval_dtor(return_value);
816 RETURN_NULL();
817 }
818 }
819 /* }}} */
820
821 /* {{{ proto void http_persistent_handles_clean([string name]) */
822 PHP_FUNCTION(http_persistent_handles_clean)
823 {
824 char *name_str = NULL;
825 int name_len = 0;
826
827 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name_str, &name_len)) {
828 http_persistent_handle_cleanup_ex(name_str, name_len, 1);
829 }
830 }
831 /* }}} */
832
833 /* {{{ proto string http_persistent_handles_ident([string ident]) */
834 PHP_FUNCTION(http_persistent_handles_ident)
835 {
836 char *ident_str = NULL;
837 int ident_len = 0;
838
839 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ident_str, &ident_len)) {
840 RETVAL_STRING(zend_ini_string(ZEND_STRS("http.persistent.handles.ident"), 0), 1);
841 if (ident_str && ident_len) {
842 zend_alter_ini_entry(ZEND_STRS("http.persistent.handles.ident"), ident_str, ident_len, ZEND_INI_USER, PHP_INI_STAGE_RUNTIME);
843 }
844 }
845 }
846 /* }}} */
847
848 /* {{{ HAVE_CURL */
849 #ifdef HTTP_HAVE_CURL
850
851 #define RETVAL_RESPONSE_OR_BODY(request) \
852 { \
853 zval **bodyonly; \
854 \
855 /* check if only the body should be returned */ \
856 if (options && (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), "bodyonly", sizeof("bodyonly"), (void *) &bodyonly)) && zval_is_true(*bodyonly)) { \
857 http_message *msg = http_message_parse(PHPSTR_VAL(&request.conv.response), PHPSTR_LEN(&request.conv.response)); \
858 \
859 if (msg) { \
860 RETVAL_STRINGL(PHPSTR_VAL(&msg->body), PHPSTR_LEN(&msg->body), 1); \
861 http_message_free(&msg); \
862 } \
863 } else { \
864 RETVAL_STRINGL(request.conv.response.data, request.conv.response.used, 1); \
865 } \
866 }
867
868 /* {{{ proto string http_get(string url[, array options[, array &info]])
869 Performs an HTTP GET request on the supplied url. */
870 PHP_FUNCTION(http_get)
871 {
872 zval *options = NULL, *info = NULL;
873 char *URL;
874 int URL_len;
875 http_request request;
876
877 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
878 RETURN_FALSE;
879 }
880
881 if (info) {
882 zval_dtor(info);
883 array_init(info);
884 }
885
886 RETVAL_FALSE;
887
888 http_request_init_ex(&request, NULL, HTTP_GET, URL);
889 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
890 http_request_exec(&request);
891 if (info) {
892 http_request_info(&request, Z_ARRVAL_P(info));
893 }
894 RETVAL_RESPONSE_OR_BODY(request);
895 }
896 http_request_dtor(&request);
897 }
898 /* }}} */
899
900 /* {{{ proto string http_head(string url[, array options[, array &info]])
901 Performs an HTTP HEAD request on the supplied url. */
902 PHP_FUNCTION(http_head)
903 {
904 zval *options = NULL, *info = NULL;
905 char *URL;
906 int URL_len;
907 http_request request;
908
909 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
910 RETURN_FALSE;
911 }
912
913 if (info) {
914 zval_dtor(info);
915 array_init(info);
916 }
917
918 RETVAL_FALSE;
919
920 http_request_init_ex(&request, NULL, HTTP_HEAD, URL);
921 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
922 http_request_exec(&request);
923 if (info) {
924 http_request_info(&request, Z_ARRVAL_P(info));
925 }
926 RETVAL_RESPONSE_OR_BODY(request);
927 }
928 http_request_dtor(&request);
929 }
930 /* }}} */
931
932 /* {{{ proto string http_post_data(string url, string data[, array options[, array &info]])
933 Performs an HTTP POST request on the supplied url. */
934 PHP_FUNCTION(http_post_data)
935 {
936 zval *options = NULL, *info = NULL;
937 char *URL, *postdata;
938 int postdata_len, URL_len;
939 http_request_body body;
940 http_request request;
941
942 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
943 RETURN_FALSE;
944 }
945
946 if (info) {
947 zval_dtor(info);
948 array_init(info);
949 }
950
951 RETVAL_FALSE;
952
953 http_request_init_ex(&request, NULL, HTTP_POST, URL);
954 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, postdata, postdata_len, 0);
955 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
956 http_request_exec(&request);
957 if (info) {
958 http_request_info(&request, Z_ARRVAL_P(info));
959 }
960 RETVAL_RESPONSE_OR_BODY(request);
961 }
962 http_request_dtor(&request);
963 }
964 /* }}} */
965
966 /* {{{ proto string http_post_fields(string url, array data[, array files[, array options[, array &info]]])
967 Performs an HTTP POST request on the supplied url. */
968 PHP_FUNCTION(http_post_fields)
969 {
970 zval *options = NULL, *info = NULL, *fields = NULL, *files = NULL;
971 char *URL;
972 int URL_len;
973 http_request_body body;
974 http_request request;
975
976 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa!|a!a/!z", &URL, &URL_len, &fields, &files, &options, &info) != SUCCESS) {
977 RETURN_FALSE;
978 }
979
980 if (!http_request_body_fill(&body, fields ? Z_ARRVAL_P(fields) : NULL, files ? Z_ARRVAL_P(files) : NULL)) {
981 RETURN_FALSE;
982 }
983
984 if (info) {
985 zval_dtor(info);
986 array_init(info);
987 }
988
989 RETVAL_FALSE;
990
991 http_request_init_ex(&request, NULL, HTTP_POST, URL);
992 request.body = &body;
993 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
994 http_request_exec(&request);
995 if (info) {
996 http_request_info(&request, Z_ARRVAL_P(info));
997 }
998 RETVAL_RESPONSE_OR_BODY(request);
999 }
1000 http_request_dtor(&request);
1001 }
1002 /* }}} */
1003
1004 /* {{{ proto string http_put_file(string url, string file[, array options[, array &info]])
1005 Performs an HTTP PUT request on the supplied url. */
1006 PHP_FUNCTION(http_put_file)
1007 {
1008 char *URL, *file;
1009 int URL_len, f_len;
1010 zval *options = NULL, *info = NULL;
1011 php_stream *stream;
1012 php_stream_statbuf ssb;
1013 http_request_body body;
1014 http_request request;
1015
1016 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &file, &f_len, &options, &info)) {
1017 RETURN_FALSE;
1018 }
1019
1020 if (!(stream = php_stream_open_wrapper_ex(file, "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT))) {
1021 RETURN_FALSE;
1022 }
1023 if (php_stream_stat(stream, &ssb)) {
1024 php_stream_close(stream);
1025 RETURN_FALSE;
1026 }
1027
1028 if (info) {
1029 zval_dtor(info);
1030 array_init(info);
1031 }
1032
1033 RETVAL_FALSE;
1034
1035 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1036 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
1037 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1038 http_request_exec(&request);
1039 if (info) {
1040 http_request_info(&request, Z_ARRVAL_P(info));
1041 }
1042 RETVAL_RESPONSE_OR_BODY(request);
1043 }
1044 http_request_dtor(&request);
1045 }
1046 /* }}} */
1047
1048 /* {{{ proto string http_put_stream(string url, resource stream[, array options[, array &info]])
1049 Performs an HTTP PUT request on the supplied url. */
1050 PHP_FUNCTION(http_put_stream)
1051 {
1052 zval *resource, *options = NULL, *info = NULL;
1053 char *URL;
1054 int URL_len;
1055 php_stream *stream;
1056 php_stream_statbuf ssb;
1057 http_request_body body;
1058 http_request request;
1059
1060 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr|a/!z", &URL, &URL_len, &resource, &options, &info)) {
1061 RETURN_FALSE;
1062 }
1063
1064 php_stream_from_zval(stream, &resource);
1065 if (php_stream_stat(stream, &ssb)) {
1066 RETURN_FALSE;
1067 }
1068
1069 if (info) {
1070 zval_dtor(info);
1071 array_init(info);
1072 }
1073
1074 RETVAL_FALSE;
1075
1076 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1077 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 0);
1078 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1079 http_request_exec(&request);
1080 if (info) {
1081 http_request_info(&request, Z_ARRVAL_P(info));
1082 }
1083 RETVAL_RESPONSE_OR_BODY(request);
1084 }
1085 http_request_dtor(&request);
1086 }
1087 /* }}} */
1088
1089 /* {{{ proto string http_put_data(string url, string data[, array options[, array &info]])
1090 Performs an HTTP PUT request on the supplied url. */
1091 PHP_FUNCTION(http_put_data)
1092 {
1093 char *URL, *data;
1094 int URL_len, data_len;
1095 zval *options = NULL, *info = NULL;
1096 http_request_body body;
1097 http_request request;
1098
1099 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &data, &data_len, &options, &info)) {
1100 RETURN_FALSE;
1101 }
1102
1103 if (info) {
1104 zval_dtor(info);
1105 array_init(info);
1106 }
1107
1108 RETVAL_FALSE;
1109
1110 http_request_init_ex(&request, NULL, HTTP_PUT, URL);
1111 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, data, data_len, 0);
1112 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1113 http_request_exec(&request);
1114 if (info) {
1115 http_request_info(&request, Z_ARRVAL_P(info));
1116 }
1117 RETVAL_RESPONSE_OR_BODY(request);
1118 }
1119 http_request_dtor(&request);
1120 }
1121 /* }}} */
1122
1123 /* {{{ proto string http_request(int method, string url[, string body[, array options[, array &info]]])
1124 Performs a custom HTTP request on the supplied url. */
1125 PHP_FUNCTION(http_request)
1126 {
1127 long meth;
1128 char *URL, *data = NULL;
1129 int URL_len, data_len = 0;
1130 zval *options = NULL, *info = NULL;
1131 http_request_body body;
1132 http_request request;
1133
1134 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|sa/!z", &meth, &URL, &URL_len, &data, &data_len, &options, &info)) {
1135 RETURN_FALSE;
1136 }
1137
1138 if (info) {
1139 zval_dtor(info);
1140 array_init(info);
1141 }
1142
1143 RETVAL_FALSE;
1144
1145 http_request_init_ex(&request, NULL, meth, URL);
1146 request.body = http_request_body_init_ex(&body, HTTP_REQUEST_BODY_CSTRING, data, data_len, 0);
1147 if (SUCCESS == http_request_prepare(&request, options?Z_ARRVAL_P(options):NULL)) {
1148 http_request_exec(&request);
1149 if (info) {
1150 http_request_info(&request, Z_ARRVAL_P(info));
1151 }
1152 RETVAL_RESPONSE_OR_BODY(request);
1153 }
1154 http_request_dtor(&request);
1155 }
1156 /* }}} */
1157
1158 /* {{{ proto string http_request_body_encode(array fields, array files)
1159 Generate x-www-form-urlencoded resp. form-data encoded request body. */
1160 PHP_FUNCTION(http_request_body_encode)
1161 {
1162 zval *fields = NULL, *files = NULL;
1163 HashTable *fields_ht, *files_ht;
1164 http_request_body body;
1165 char *buf;
1166 size_t len;
1167
1168 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!", &fields, &files)) {
1169 RETURN_FALSE;
1170 }
1171
1172 fields_ht = (fields && Z_TYPE_P(fields) == IS_ARRAY) ? Z_ARRVAL_P(fields) : NULL;
1173 files_ht = (files && Z_TYPE_P(files) == IS_ARRAY) ? Z_ARRVAL_P(files) : NULL;
1174 if (http_request_body_fill(&body, fields_ht, files_ht) && (SUCCESS == http_request_body_encode(&body, &buf, &len))) {
1175 RETVAL_STRINGL(buf, len, 0);
1176 } else {
1177 http_error(HE_WARNING, HTTP_E_RUNTIME, "Could not encode request body");
1178 RETVAL_FALSE;
1179 }
1180 http_request_body_dtor(&body);
1181 }
1182 #endif /* HTTP_HAVE_CURL */
1183 /* }}} HAVE_CURL */
1184
1185 /* {{{ proto int http_request_method_register(string method)
1186 Register a custom request method. */
1187 PHP_FUNCTION(http_request_method_register)
1188 {
1189 char *method;
1190 int method_len;
1191 ulong existing;
1192
1193 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
1194 RETURN_FALSE;
1195 }
1196 if ((existing = http_request_method_exists(1, 0, method))) {
1197 RETURN_LONG((long) existing);
1198 }
1199
1200 RETVAL_LONG((long) http_request_method_register(method, method_len));
1201 }
1202 /* }}} */
1203
1204 /* {{{ proto bool http_request_method_unregister(mixed method)
1205 Unregister a previously registered custom request method. */
1206 PHP_FUNCTION(http_request_method_unregister)
1207 {
1208 zval *method;
1209
1210 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1211 RETURN_FALSE;
1212 }
1213
1214 switch (Z_TYPE_P(method)) {
1215 case IS_OBJECT:
1216 convert_to_string(method);
1217 case IS_STRING:
1218 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1219 convert_to_long(method);
1220 } else {
1221 int mn;
1222 if (!(mn = http_request_method_exists(1, 0, Z_STRVAL_P(method)))) {
1223 RETURN_FALSE;
1224 }
1225 zval_dtor(method);
1226 ZVAL_LONG(method, (long)mn);
1227 }
1228 case IS_LONG:
1229 RETURN_SUCCESS(http_request_method_unregister(Z_LVAL_P(method)));
1230 default:
1231 RETURN_FALSE;
1232 }
1233 }
1234 /* }}} */
1235
1236 /* {{{ proto int http_request_method_exists(mixed method)
1237 Check if a request method is registered (or available by default). */
1238 PHP_FUNCTION(http_request_method_exists)
1239 {
1240 if (return_value_used) {
1241 zval *method;
1242
1243 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &method)) {
1244 RETURN_FALSE;
1245 }
1246
1247 switch (Z_TYPE_P(method)) {
1248 case IS_OBJECT:
1249 convert_to_string(method);
1250 case IS_STRING:
1251 if (is_numeric_string(Z_STRVAL_P(method), Z_STRLEN_P(method), NULL, NULL, 1)) {
1252 convert_to_long(method);
1253 } else {
1254 RETURN_LONG((long) http_request_method_exists(1, 0, Z_STRVAL_P(method)));
1255 }
1256 case IS_LONG:
1257 RETURN_LONG((long) http_request_method_exists(0, (int) Z_LVAL_P(method), NULL));
1258 default:
1259 RETURN_FALSE;
1260 }
1261 }
1262 }
1263 /* }}} */
1264
1265 /* {{{ proto string http_request_method_name(int method)
1266 Get the literal string representation of a standard or registered request method. */
1267 PHP_FUNCTION(http_request_method_name)
1268 {
1269 if (return_value_used) {
1270 long method;
1271
1272 if ((SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method)) || (method < 0)) {
1273 RETURN_FALSE;
1274 }
1275
1276 RETURN_STRING(estrdup(http_request_method_name((int) method)), 0);
1277 }
1278 }
1279 /* }}} */
1280
1281 /* {{{ */
1282 #ifdef HTTP_HAVE_ZLIB
1283
1284 /* {{{ proto string http_deflate(string data[, int flags = 0])
1285 Compress data with gzip, zlib AKA deflate or raw deflate encoding. */
1286 PHP_FUNCTION(http_deflate)
1287 {
1288 char *data;
1289 int data_len;
1290 long flags = 0;
1291
1292 RETVAL_NULL();
1293
1294 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &flags)) {
1295 char *encoded;
1296 size_t encoded_len;
1297
1298 if (SUCCESS == http_encoding_deflate(flags, data, data_len, &encoded, &encoded_len)) {
1299 RETURN_STRINGL(encoded, (int) encoded_len, 0);
1300 }
1301 }
1302 }
1303 /* }}} */
1304
1305 /* {{{ proto string http_inflate(string data)
1306 Decompress data compressed with either gzip, deflate AKA zlib or raw deflate encoding. */
1307 PHP_FUNCTION(http_inflate)
1308 {
1309 char *data;
1310 int data_len;
1311
1312 RETVAL_NULL();
1313
1314 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
1315 char *decoded;
1316 size_t decoded_len;
1317
1318 if (SUCCESS == http_encoding_inflate(data, data_len, &decoded, &decoded_len)) {
1319 RETURN_STRINGL(decoded, (int) decoded_len, 0);
1320 }
1321 }
1322 }
1323 /* }}} */
1324
1325 /* {{{ proto string ob_deflatehandler(string data, int mode)
1326 For use with ob_start(). The deflate output buffer handler can only be used once. */
1327 PHP_FUNCTION(ob_deflatehandler)
1328 {
1329 char *data;
1330 int data_len;
1331 long mode;
1332
1333 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1334 RETURN_FALSE;
1335 }
1336
1337 http_ob_deflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1338 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1339 }
1340 /* }}} */
1341
1342 /* {{{ proto string ob_inflatehandler(string data, int mode)
1343 For use with ob_start(). Same restrictions as with ob_deflatehandler apply. */
1344 PHP_FUNCTION(ob_inflatehandler)
1345 {
1346 char *data;
1347 int data_len;
1348 long mode;
1349
1350 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1351 RETURN_FALSE;
1352 }
1353
1354 http_ob_inflatehandler(data, data_len, &Z_STRVAL_P(return_value), (uint *) &Z_STRLEN_P(return_value), mode);
1355 Z_TYPE_P(return_value) = Z_STRVAL_P(return_value) ? IS_STRING : IS_NULL;
1356 }
1357 /* }}} */
1358
1359 #endif /* HTTP_HAVE_ZLIB */
1360 /* }}} */
1361
1362 /* {{{ proto int http_support([int feature = 0])
1363 Check for feature that require external libraries. */
1364 PHP_FUNCTION(http_support)
1365 {
1366 long feature = 0;
1367
1368 RETVAL_LONG(0L);
1369
1370 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &feature)) {
1371 RETVAL_LONG(http_support(feature));
1372 }
1373 }
1374 /* }}} */
1375
1376 /*
1377 * Local variables:
1378 * tab-width: 4
1379 * c-basic-offset: 4
1380 * End:
1381 * vim600: noet sw=4 ts=4 fdm=marker
1382 * vim<600: noet sw=4 ts=4
1383 */
1384