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