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