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