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