* hopefully satisfying snaps.php.net
[m6w6/ext-http] / http.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "snprintf.h"
28 #include "ext/standard/info.h"
29 #include "ext/session/php_session.h"
30 #include "ext/standard/php_string.h"
31 #include "ext/standard/php_smart_str.h"
32
33 #include "SAPI.h"
34
35 #include "php_http.h"
36 #include "php_http_api.h"
37
38 #ifdef ZEND_ENGINE_2
39 # include "ext/standard/php_http.h"
40 #endif
41
42 #ifdef HTTP_HAVE_CURL
43
44 # ifdef PHP_WIN32
45 # include <winsock2.h>
46 # include <sys/types.h>
47 # endif
48
49 # include <curl/curl.h>
50
51 /* {{{ ARG_INFO */
52 # ifdef ZEND_BEGIN_ARG_INFO
53 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3, 0)
54 ZEND_ARG_PASS_INFO(0)
55 ZEND_ARG_PASS_INFO(0)
56 ZEND_ARG_PASS_INFO(1)
57 ZEND_END_ARG_INFO();
58
59 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4, 0)
60 ZEND_ARG_PASS_INFO(0)
61 ZEND_ARG_PASS_INFO(0)
62 ZEND_ARG_PASS_INFO(0)
63 ZEND_ARG_PASS_INFO(1)
64 ZEND_END_ARG_INFO();
65 # else
66 static unsigned char http_request_info_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
67 static unsigned char http_request_info_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
68 # endif
69 /* }}} ARG_INFO */
70
71 #endif /* HTTP_HAVE_CURL */
72
73 ZEND_DECLARE_MODULE_GLOBALS(http)
74
75 #ifdef COMPILE_DL_HTTP
76 ZEND_GET_MODULE(http)
77 #endif
78
79 /* {{{ http_functions[] */
80 function_entry http_functions[] = {
81 PHP_FE(http_date, NULL)
82 PHP_FE(http_absolute_uri, NULL)
83 PHP_FE(http_negotiate_language, NULL)
84 PHP_FE(http_negotiate_charset, NULL)
85 PHP_FE(http_redirect, NULL)
86 PHP_FE(http_send_status, NULL)
87 PHP_FE(http_send_last_modified, NULL)
88 PHP_FE(http_send_content_type, NULL)
89 PHP_FE(http_send_content_disposition, NULL)
90 PHP_FE(http_match_modified, NULL)
91 PHP_FE(http_match_etag, NULL)
92 PHP_FE(http_cache_last_modified, NULL)
93 PHP_FE(http_cache_etag, NULL)
94 PHP_FE(http_send_data, NULL)
95 PHP_FE(http_send_file, NULL)
96 PHP_FE(http_send_stream, NULL)
97 PHP_FE(http_chunked_decode, NULL)
98 PHP_FE(http_split_response, NULL)
99 PHP_FE(http_parse_headers, NULL)
100 PHP_FE(http_get_request_headers, NULL)
101 #ifdef HTTP_HAVE_CURL
102 PHP_FE(http_get, http_request_info_ref_3)
103 PHP_FE(http_head, http_request_info_ref_3)
104 PHP_FE(http_post_data, http_request_info_ref_4)
105 PHP_FE(http_post_array, http_request_info_ref_4)
106 #endif
107 PHP_FE(http_auth_basic, NULL)
108 PHP_FE(http_auth_basic_cb, NULL)
109 #ifndef ZEND_ENGINE_2
110 PHP_FE(http_build_query, NULL)
111 #endif
112 PHP_FE(ob_httpetaghandler, NULL)
113 {NULL, NULL, NULL}
114 };
115 /* }}} */
116
117 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
118 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
119
120 #ifdef ZEND_ENGINE_2
121
122 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
123 { \
124 zend_class_entry ce; \
125 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
126 ce.create_object = name## _new_object; \
127 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
128 name## _ce->ce_flags |= flags; \
129 memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
130 name## _object_handlers.clone_obj = NULL; \
131 name## _declare_default_properties(name## _ce); \
132 }
133
134 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
135 { \
136 zend_class_entry ce; \
137 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
138 ce.create_object = NULL; \
139 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
140 name## _ce->ce_flags |= flags; \
141 }
142
143 # define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC))
144 # define OBJ_PROP(o) o->zo.properties
145 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
146 # define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC)
147 # define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC)
148 # define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC)
149
150 /* {{{ HTTPi */
151
152 zend_class_entry *httpi_ce;
153
154 #define HTTPi_ME(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
155
156 zend_function_entry httpi_class_methods[] = {
157 HTTPi_ME(date, http_date, NULL)
158 HTTPi_ME(absoluteURI, http_absolute_uri, NULL)
159 HTTPi_ME(negotiateLanguage, http_negotiate_language, NULL)
160 HTTPi_ME(negotiateCharset, http_negotiate_charset, NULL)
161 HTTPi_ME(redirect, http_redirect, NULL)
162 HTTPi_ME(sendStatus, http_send_status, NULL)
163 HTTPi_ME(sendLastModified, http_send_last_modified, NULL)
164 HTTPi_ME(sendContentType, http_send_content_type, NULL)
165 HTTPi_ME(sendContentDisposition, http_send_content_disposition, NULL)
166 HTTPi_ME(matchModified, http_match_modified, NULL)
167 HTTPi_ME(matchEtag, http_match_etag, NULL)
168 HTTPi_ME(cacheLastModified, http_cache_last_modified, NULL)
169 HTTPi_ME(cacheEtag, http_cache_etag, NULL)
170 HTTPi_ME(chunkedDecode, http_chunked_decode, NULL)
171 HTTPi_ME(splitResponse, http_split_response, NULL)
172 HTTPi_ME(parseHeaders, http_parse_headers, NULL)
173 HTTPi_ME(getRequestHeaders, http_get_request_headers, NULL)
174 #ifdef HTTP_HAVE_CURL
175 HTTPi_ME(get, http_get, http_request_info_ref_3)
176 HTTPi_ME(head, http_head, http_request_info_ref_3)
177 HTTPi_ME(postData, http_post_data, http_request_info_ref_4)
178 HTTPi_ME(postArray, http_post_array, http_request_info_ref_4)
179 #endif
180 HTTPi_ME(authBasic, http_auth_basic, NULL)
181 HTTPi_ME(authBasicCallback, http_auth_basic_cb, NULL)
182 {NULL, NULL, NULL}
183 };
184 /* }}} HTTPi */
185
186 /* {{{ HTTPi_Response */
187
188 zend_class_entry *httpi_response_ce;
189 static zend_object_handlers httpi_response_object_handlers;
190
191 typedef struct {
192 zend_object zo;
193 } httpi_response_object;
194
195 #define httpi_response_declare_default_properties(ce) _httpi_response_declare_default_properties(ce TSRMLS_CC)
196 static inline void _httpi_response_declare_default_properties(zend_class_entry *ce TSRMLS_DC)
197 {
198 DCL_PROP(PROTECTED, string, contentType, "application/x-octetstream");
199 DCL_PROP(PROTECTED, string, eTag, "");
200 DCL_PROP(PROTECTED, string, dispoFile, "");
201 DCL_PROP(PROTECTED, string, cacheControl, "public");
202 DCL_PROP(PROTECTED, string, data, "");
203 DCL_PROP(PROTECTED, string, file, "");
204 DCL_PROP(PROTECTED, long, stream, 0);
205 DCL_PROP(PROTECTED, long, lastModified, 0);
206 DCL_PROP(PROTECTED, long, dispoInline, 0);
207 DCL_PROP(PROTECTED, long, cache, 0);
208 DCL_PROP(PROTECTED, long, gzip, 0);
209
210 DCL_PROP(PRIVATE, long, raw_cache_header, 0);
211 DCL_PROP(PRIVATE, long, send_mode, -1);
212 }
213
214 #define httpi_response_destroy_object _httpi_response_destroy_object
215 void _httpi_response_destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
216 {
217 httpi_response_object *o = object;
218 if (OBJ_PROP(o)) {
219 zend_hash_destroy(OBJ_PROP(o));
220 FREE_HASHTABLE(OBJ_PROP(o));
221 }
222 efree(o);
223 }
224
225 #define httpi_response_new_object _httpi_response_new_object
226 zend_object_value _httpi_response_new_object(zend_class_entry *ce TSRMLS_DC)
227 {
228 zend_object_value ov;
229 httpi_response_object *o;
230
231 o = ecalloc(sizeof(httpi_response_object), 1);
232 o->zo.ce = ce;
233
234 ALLOC_HASHTABLE(OBJ_PROP(o));
235 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
236 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
237
238 ov.handle = zend_objects_store_put(o, httpi_response_destroy_object, NULL, NULL TSRMLS_CC);
239 ov.handlers = &httpi_response_object_handlers;
240
241 return ov;
242 }
243
244 zend_function_entry httpi_response_class_methods[] = {
245 PHP_ME(HTTPi_Response, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
246 /* PHP_ME(HTTPi_Response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
247 */
248 PHP_ME(HTTPi_Response, setETag, NULL, ZEND_ACC_PUBLIC)
249 PHP_ME(HTTPi_Response, getETag, NULL, ZEND_ACC_PUBLIC)
250
251 PHP_ME(HTTPi_Response, setContentDisposition, NULL, ZEND_ACC_PUBLIC)
252 PHP_ME(HTTPi_Response, getContentDisposition, NULL, ZEND_ACC_PUBLIC)
253
254 PHP_ME(HTTPi_Response, setContentType, NULL, ZEND_ACC_PUBLIC)
255 PHP_ME(HTTPi_Response, getContentType, NULL, ZEND_ACC_PUBLIC)
256
257 PHP_ME(HTTPi_Response, setCache, NULL, ZEND_ACC_PUBLIC)
258 PHP_ME(HTTPi_Response, getCache, NULL, ZEND_ACC_PUBLIC)
259
260 PHP_ME(HTTPi_Response, setCacheControl, NULL, ZEND_ACC_PUBLIC)
261 PHP_ME(HTTPi_Response, getCacheControl, NULL, ZEND_ACC_PUBLIC)
262
263 PHP_ME(HTTPi_Response, setGzip, NULL, ZEND_ACC_PUBLIC)
264 PHP_ME(HTTPi_Response, getGzip, NULL, ZEND_ACC_PUBLIC)
265
266 PHP_ME(HTTPi_Response, setData, NULL, ZEND_ACC_PUBLIC)
267 PHP_ME(HTTPi_Response, getData, NULL, ZEND_ACC_PUBLIC)
268
269 PHP_ME(HTTPi_Response, setFile, NULL, ZEND_ACC_PUBLIC)
270 PHP_ME(HTTPi_Response, getFile, NULL, ZEND_ACC_PUBLIC)
271
272 PHP_ME(HTTPi_Response, setStream, NULL, ZEND_ACC_PUBLIC)
273 PHP_ME(HTTPi_Response, getStream, NULL, ZEND_ACC_PUBLIC)
274
275 PHP_ME(HTTPi_Response, send, NULL, ZEND_ACC_PUBLIC)
276
277 {NULL, NULL, NULL}
278 };
279
280 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
281 *
282 */
283 PHP_METHOD(HTTPi_Response, __construct)
284 {
285 zend_bool do_cache = 0, do_gzip = 0;
286 getObject(httpi_response_object, obj);
287
288 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
289 // throw exception
290 return;
291 }
292
293 UPD_PROP(obj, long, cache, do_cache);
294 UPD_PROP(obj, long, gzip, do_gzip);
295 }
296 /* }}} */
297
298 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
299 *
300 */
301 PHP_METHOD(HTTPi_Response, setCache)
302 {
303 zend_bool do_cache = 0;
304 getObject(httpi_response_object, obj);
305
306 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
307 RETURN_FALSE;
308 }
309
310 UPD_PROP(obj, long, cache, do_cache);
311 RETURN_TRUE;
312 }
313 /* }}} */
314
315 /* {{{ proto bool HTTPi_Response::getCache()
316 *
317 */
318 PHP_METHOD(HTTPi_Response, getCache)
319 {
320 zval *do_cache = NULL;
321 getObject(httpi_response_object, obj);
322
323 if (ZEND_NUM_ARGS()) {
324 WRONG_PARAM_COUNT;
325 }
326
327 do_cache = GET_PROP(obj, cache);
328 RETURN_BOOL(Z_LVAL_P(do_cache));
329 }
330 /* }}}*/
331
332 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
333 *
334 */
335 PHP_METHOD(HTTPi_Response, setGzip)
336 {
337 zend_bool do_gzip = 0;
338 getObject(httpi_response_object, obj);
339
340 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
341 RETURN_FALSE;
342 }
343
344 UPD_PROP(obj, long, gzip, do_gzip);
345 RETURN_TRUE;
346 }
347 /* }}} */
348
349 /* {{{ proto bool HTTPi_Response::getGzip()
350 *
351 */
352 PHP_METHOD(HTTPi_Response, getGzip)
353 {
354 zval *do_gzip = NULL;
355 getObject(httpi_response_object, obj);
356
357 if (ZEND_NUM_ARGS()) {
358 WRONG_PARAM_COUNT;
359 }
360
361 do_gzip = GET_PROP(obj, gzip);
362 RETURN_BOOL(Z_LVAL_P(do_gzip));
363 }
364 /* }}} */
365
366 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
367 *
368 */
369 PHP_METHOD(HTTPi_Response, setCacheControl)
370 {
371 char *ccontrol;
372 int cc_len;
373 zend_bool raw = 0;
374 getObject(httpi_response_object, obj);
375
376 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
377 RETURN_FALSE;
378 }
379
380 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
381 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
382 RETURN_FALSE;
383 }
384
385 UPD_PROP(obj, long, raw_cache_header, raw);
386 UPD_PROP(obj, string, cacheControl, ccontrol);
387 RETURN_TRUE;
388 }
389 /* }}} */
390
391 /* {{{ proto string HTTPi_Response::getCacheControl()
392 *
393 */
394 PHP_METHOD(HTTPi_Response, getCacheControl)
395 {
396 zval *ccontrol;
397 getObject(httpi_response_object, obj);
398
399 if (ZEND_NUM_ARGS()) {
400 WRONG_PARAM_COUNT;
401 }
402
403 ccontrol = GET_PROP(obj, cacheControl);
404 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
405 }
406 /* }}} */
407
408 /* {{{ proto bool HTTPi::setContentType(string content_type)
409 *
410 */
411 PHP_METHOD(HTTPi_Response, setContentType)
412 {
413 char *ctype;
414 int ctype_len;
415 getObject(httpi_response_object, obj);
416
417 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
418 RETURN_FALSE;
419 }
420
421 if (!strchr(ctype, '/')) {
422 php_error_docref(NULL TSRMLS_CC, E_WARNING,
423 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
424 RETURN_FALSE;
425 }
426
427 UPD_PROP(obj, string, contentType, ctype);
428
429 RETURN_TRUE;
430 }
431 /* }}} */
432
433 /* {{{ proto string HTTPi_Response::getContentType()
434 *
435 */
436 PHP_METHOD(HTTPi_Response, getContentType)
437 {
438 zval *ctype;
439 getObject(httpi_response_object, obj);
440
441 if (ZEND_NUM_ARGS()) {
442 WRONG_PARAM_COUNT;
443 }
444
445 ctype = GET_PROP(obj, contentType);
446 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
447 }
448 /* }}} */
449
450 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
451 *
452 */
453 PHP_METHOD(HTTPi_Response, setContentDisposition)
454 {
455 char *file;
456 int file_len;
457 zend_bool is_inline = 0;
458 getObject(httpi_response_object, obj);
459
460 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
461 RETURN_FALSE;
462 }
463
464 UPD_PROP(obj, string, dispoFile, file);
465 UPD_PROP(obj, long, dispoInline, is_inline);
466 RETURN_TRUE;
467 }
468 /* }}} */
469
470 /* {{{ proto array HTTPi_Response::getContentDisposition()
471 *
472 */
473 PHP_METHOD(HTTPi_Response, getContentDisposition)
474 {
475 zval *file;
476 zval *is_inline;
477 getObject(httpi_response_object, obj);
478
479 if (ZEND_NUM_ARGS()) {
480 WRONG_PARAM_COUNT;
481 }
482
483 file = GET_PROP(obj, dispoFile);
484 is_inline = GET_PROP(obj, dispoInline);
485
486 array_init(return_value);
487 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
488 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
489 }
490 /* }}} */
491
492 /* {{{ proto bool HTTPi_Response::setETag(string etag)
493 *
494 */
495 PHP_METHOD(HTTPi_Response, setETag)
496 {
497 char *etag;
498 int etag_len;
499 getObject(httpi_response_object, obj);
500
501 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
502 RETURN_FALSE;
503 }
504
505 UPD_PROP(obj, string, eTag, etag);
506 RETURN_TRUE;
507 }
508 /* }}} */
509
510 /* {{{ proto string HTTPi_Response::getETag()
511 *
512 */
513 PHP_METHOD(HTTPi_Response, getETag)
514 {
515 zval *etag;
516 getObject(httpi_response_object, obj);
517
518 if (ZEND_NUM_ARGS()) {
519 WRONG_PARAM_COUNT;
520 }
521
522 etag = GET_PROP(obj, eTag);
523 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
524 }
525 /* }}} */
526
527 /* {{{ proto bool HTTPi_Response::setData(string data)
528 *
529 */
530 PHP_METHOD(HTTPi_Response, setData)
531 {
532 zval *the_data;
533 char *etag;
534 getObject(httpi_response_object, obj);
535
536 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
537 RETURN_FALSE;
538 }
539
540 convert_to_string_ex(&the_data);
541 SET_PROP(obj, data, the_data);
542 UPD_PROP(obj, long, lastModified, http_lmod(the_data, SEND_DATA));
543 UPD_PROP(obj, long, send_mode, SEND_DATA);
544 RETURN_TRUE;
545 }
546 /* }}} */
547
548 /* {{{ proto string HTTPi_Response::getData()
549 *
550 */
551 PHP_METHOD(HTTPi_Response, getData)
552 {
553 zval *the_data;
554 getObject(httpi_response_object, obj);
555
556 if (ZEND_NUM_ARGS()) {
557 WRONG_PARAM_COUNT;
558 }
559
560 the_data = GET_PROP(obj, data);
561 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
562 }
563 /* }}} */
564
565 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
566 *
567 */
568 PHP_METHOD(HTTPi_Response, setStream)
569 {
570 zval *the_stream;
571 php_stream *the_real_stream;
572 char *etag;
573 getObject(httpi_response_object, obj);
574
575 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
576 RETURN_FALSE;
577 }
578
579 php_stream_from_zval(the_real_stream, &the_stream);
580
581 SET_PROP(obj, stream, the_stream);
582 UPD_PROP(obj, long, lastModified, http_lmod(the_real_stream, SEND_RSRC));
583 UPD_PROP(obj, long, send_mode, SEND_RSRC);
584 RETURN_TRUE;
585 }
586 /* }}} */
587
588 /* {{{ proto resource HTTPi_Response::getStream()
589 *
590 */
591 PHP_METHOD(HTTPi_Response, getStream)
592 {
593 zval *the_stream;
594 getObject(httpi_response_object, obj);
595
596 if (ZEND_NUM_ARGS()) {
597 WRONG_PARAM_COUNT;
598 }
599
600 the_stream = GET_PROP(obj, stream);
601 RETURN_RESOURCE(Z_LVAL_P(the_stream));
602 }
603 /* }}} */
604
605 /* {{{ proto bool HTTPi_Response::setFile(string file)
606 *
607 */
608 PHP_METHOD(HTTPi_Response, setFile)
609 {
610 zval *the_file;
611 getObject(httpi_response_object, obj);
612
613 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
614 RETURN_FALSE;
615 }
616
617 convert_to_string_ex(&the_file);
618
619 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
620 UPD_PROP(obj, long, lastModified, http_lmod(the_file, -1));
621 UPD_PROP(obj, long, send_mode, -1);
622 RETURN_TRUE;
623 }
624 /* }}} */
625
626 /* {{{ proto string HTTPi_Response::getFile()
627 *
628 */
629 PHP_METHOD(HTTPi_Response, getFile)
630 {
631 zval *the_file;
632 getObject(httpi_response_object, obj);
633
634 if (ZEND_NUM_ARGS()) {
635 WRONG_PARAM_COUNT;
636 }
637
638 the_file = GET_PROP(obj, file);
639 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
640 }
641 /* }}} */
642
643 PHP_METHOD(HTTPi_Response, send)
644 {
645 zval *do_cache, *do_gzip;
646 getObject(httpi_response_object, obj);
647
648 do_cache = GET_PROP(obj, cache);
649 do_gzip = GET_PROP(obj, gzip);
650
651 /* caching */
652 if (Z_LVAL_P(do_cache)) {
653 zval *cctrl, *etag, *lmod, *ccraw;
654
655 etag = GET_PROP(obj, eTag);
656 lmod = GET_PROP(obj, lastModified);
657 cctrl = GET_PROP(obj, cacheControl);
658 ccraw = GET_PROP(obj, raw_cache_header);
659
660 if (Z_LVAL_P(ccraw)) {
661 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
662 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
663 } else {
664 char cc_header[42] = {0};
665 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
666 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
667 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
668 }
669 }
670
671 /* gzip */
672 if (Z_LVAL_P(do_gzip)) {
673 /* ... */
674 }
675
676 /* content type */
677 {
678 zval *ctype = GET_PROP(obj, contentType);
679 if (Z_STRLEN_P(ctype)) {
680 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
681 } else {
682 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
683 }
684 }
685
686 /* content disposition */
687 {
688 zval *dispo_file = GET_PROP(obj, dispoFile);
689 if (Z_STRLEN_P(dispo_file)) {
690 zval *dispo_inline = GET_PROP(obj, dispoInline);
691 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), Z_LVAL_P(dispo_inline));
692 }
693 }
694
695 /* send */
696 {
697 zval *send_mode = GET_PROP(obj, send_mode);
698 switch (Z_LVAL_P(send_mode))
699 {
700 case SEND_DATA:
701 {
702 RETURN_SUCCESS(http_send_data(GET_PROP(obj, data)));
703 }
704
705 case SEND_RSRC:
706 {
707 php_stream *the_real_stream;
708 zval *the_stream = GET_PROP(obj, stream);
709 php_stream_from_zval(the_real_stream, &the_stream);
710 RETURN_SUCCESS(http_send_stream(the_real_stream));
711 }
712
713 default:
714 {
715 RETURN_SUCCESS(http_send_file(GET_PROP(obj, file)));
716 }
717 }
718 }
719 }
720
721 #endif /* ZEND_ENGINE_2 */
722
723 /* {{{ http_module_entry */
724 zend_module_entry http_module_entry = {
725 #if ZEND_MODULE_API_NO >= 20010901
726 STANDARD_MODULE_HEADER,
727 #endif
728 "http",
729 http_functions,
730 PHP_MINIT(http),
731 PHP_MSHUTDOWN(http),
732 PHP_RINIT(http),
733 PHP_RSHUTDOWN(http),
734 PHP_MINFO(http),
735 #if ZEND_MODULE_API_NO >= 20010901
736 PHP_EXT_HTTP_VERSION,
737 #endif
738 STANDARD_MODULE_PROPERTIES
739 };
740 /* }}} */
741
742 /* {{{ proto string http_date([int timestamp])
743 *
744 * This function returns a valid HTTP date regarding RFC 822/1123
745 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
746 *
747 */
748 PHP_FUNCTION(http_date)
749 {
750 long t = -1;
751
752 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
753 RETURN_FALSE;
754 }
755
756 if (t == -1) {
757 t = (long) time(NULL);
758 }
759
760 RETURN_STRING(http_date(t), 0);
761 }
762 /* }}} */
763
764 /* {{{ proto string http_absolute_uri(string url[, string proto])
765 *
766 * This function returns an absolute URI constructed from url.
767 * If the url is already abolute but a different proto was supplied,
768 * only the proto part of the URI will be updated. If url has no
769 * path specified, the path of the current REQUEST_URI will be taken.
770 * The host will be taken either from the Host HTTP header of the client
771 * the SERVER_NAME or just localhost if prior are not available.
772 *
773 * Some examples:
774 * <pre>
775 * url = "page.php" => http://www.example.com/current/path/page.php
776 * url = "/page.php" => http://www.example.com/page.php
777 * url = "/page.php", proto = "https" => https://www.example.com/page.php
778 * </pre>
779 *
780 */
781 PHP_FUNCTION(http_absolute_uri)
782 {
783 char *url = NULL, *proto = NULL;
784 int url_len = 0, proto_len = 0;
785
786 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
787 RETURN_FALSE;
788 }
789
790 RETURN_STRING(http_absolute_uri(url, proto), 0);
791 }
792 /* }}} */
793
794 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
795 *
796 * This function negotiates the clients preferred language based on its
797 * Accept-Language HTTP header. It returns the negotiated language or
798 * the default language if none match.
799 *
800 * The qualifier is recognized and languages without qualifier are rated highest.
801 *
802 * The supported parameter is expected to be an array having
803 * the supported languages as array values.
804 *
805 * Example:
806 * <pre>
807 * <?php
808 * $langs = array(
809 * 'en-US',// default
810 * 'fr',
811 * 'fr-FR',
812 * 'de',
813 * 'de-DE',
814 * 'de-AT',
815 * 'de-CH',
816 * );
817 * include './langs/'. http_negotiate_language($langs) .'.php';
818 * ?>
819 * </pre>
820 *
821 */
822 PHP_FUNCTION(http_negotiate_language)
823 {
824 zval *supported;
825 char *def = NULL;
826 int def_len = 0;
827
828 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
829 RETURN_FALSE;
830 }
831
832 if (!def) {
833 def = "en-US";
834 }
835
836 RETURN_STRING(http_negotiate_language(supported, def), 0);
837 }
838 /* }}} */
839
840 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
841 *
842 * This function negotiates the clients preferred charset based on its
843 * Accept-Charset HTTP header. It returns the negotiated charset or
844 * the default charset if none match.
845 *
846 * The qualifier is recognized and charset without qualifier are rated highest.
847 *
848 * The supported parameter is expected to be an array having
849 * the supported charsets as array values.
850 *
851 * Example:
852 * <pre>
853 * <?php
854 * $charsets = array(
855 * 'iso-8859-1', // default
856 * 'iso-8859-2',
857 * 'iso-8859-15',
858 * 'utf-8'
859 * );
860 * $pref = http_negotiate_charset($charsets);
861 * if (!strcmp($pref, 'iso-8859-1')) {
862 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
863 * iconv_set_encoding('output_encoding', $pref);
864 * ob_start('ob_iconv_handler');
865 * }
866 * ?>
867 * </pre>
868 */
869 PHP_FUNCTION(http_negotiate_charset)
870 {
871 zval *supported;
872 char *def = NULL;
873 int def_len = 0;
874
875 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
876 RETURN_FALSE;
877 }
878
879 if (!def) {
880 def = "iso-8859-1";
881 }
882
883 RETURN_STRING(http_negotiate_charset(supported, def), 0);
884 }
885 /* }}} */
886
887 /* {{{ proto bool http_send_status(int status)
888 *
889 * Send HTTP status code.
890 *
891 */
892 PHP_FUNCTION(http_send_status)
893 {
894 int status = 0;
895
896 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
897 RETURN_FALSE;
898 }
899 if (status < 100 || status > 510) {
900 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
901 RETURN_FALSE;
902 }
903
904 RETURN_SUCCESS(http_send_status(status));
905 }
906 /* }}} */
907
908 /* {{{ proto bool http_send_last_modified([int timestamp])
909 *
910 * This converts the given timestamp to a valid HTTP date and
911 * sends it as "Last-Modified" HTTP header. If timestamp is
912 * omitted, current time is sent.
913 *
914 */
915 PHP_FUNCTION(http_send_last_modified)
916 {
917 long t = -1;
918
919 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
920 RETURN_FALSE;
921 }
922
923 if (t == -1) {
924 t = (long) time(NULL);
925 }
926
927 RETURN_SUCCESS(http_send_last_modified(t));
928 }
929 /* }}} */
930
931 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
932 *
933 * Sets the content type.
934 *
935 */
936 PHP_FUNCTION(http_send_content_type)
937 {
938 char *ct;
939 int ct_len = 0;
940
941 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
942 RETURN_FALSE;
943 }
944
945 if (!ct_len) {
946 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
947 }
948 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
949 }
950 /* }}} */
951
952 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
953 *
954 * Set the Content Disposition. The Content-Disposition header is very useful
955 * if the data actually sent came from a file or something similar, that should
956 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
957 *
958 */
959 PHP_FUNCTION(http_send_content_disposition)
960 {
961 char *filename;
962 int f_len;
963 zend_bool send_inline = 0;
964
965 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
966 RETURN_FALSE;
967 }
968 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
969 }
970 /* }}} */
971
972 /* {{{ proto bool http_match_modified([int timestamp])
973 *
974 * Matches the given timestamp against the clients "If-Modified-Since" resp.
975 * "If-Unmodified-Since" HTTP headers.
976 *
977 */
978 PHP_FUNCTION(http_match_modified)
979 {
980 long t = -1;
981
982 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
983 RETURN_FALSE;
984 }
985
986 // current time if not supplied (senseless though)
987 if (t == -1) {
988 t = (long) time(NULL);
989 }
990
991 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
992 }
993 /* }}} */
994
995 /* {{{ proto bool http_match_etag(string etag)
996 *
997 * This matches the given ETag against the clients
998 * "If-Match" resp. "If-None-Match" HTTP headers.
999 *
1000 */
1001 PHP_FUNCTION(http_match_etag)
1002 {
1003 int etag_len;
1004 char *etag;
1005
1006 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
1007 RETURN_FALSE;
1008 }
1009
1010 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
1011 }
1012 /* }}} */
1013
1014 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
1015 *
1016 * If timestamp_or_exires is greater than 0, it is handled as timestamp
1017 * and will be sent as date of last modification. If it is 0 or omitted,
1018 * the current time will be sent as Last-Modified date. If it's negative,
1019 * it is handled as expiration time in seconds, which means that if the
1020 * requested last modification date is not between the calculated timespan,
1021 * the Last-Modified header is updated and the actual body will be sent.
1022 *
1023 */
1024 PHP_FUNCTION(http_cache_last_modified)
1025 {
1026 long last_modified = 0, send_modified = 0, t;
1027 zval *zlm;
1028
1029 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
1030 RETURN_FALSE;
1031 }
1032
1033 t = (long) time(NULL);
1034
1035 /* 0 or omitted */
1036 if (!last_modified) {
1037 /* does the client have? (att: caching "forever") */
1038 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
1039 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
1040 /* send current time */
1041 } else {
1042 send_modified = t;
1043 }
1044 /* negative value is supposed to be expiration time */
1045 } else if (last_modified < 0) {
1046 last_modified += t;
1047 send_modified = t;
1048 /* send supplied time explicitly */
1049 } else {
1050 send_modified = last_modified;
1051 }
1052
1053 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
1054 }
1055 /* }}} */
1056
1057 /* {{{ proto bool http_cache_etag([string etag])
1058 *
1059 * This function attempts to cache the HTTP body based on an ETag,
1060 * either supplied or generated through calculation of the MD5
1061 * checksum of the output (uses output buffering).
1062 *
1063 * If clients "If-None-Match" header matches the supplied/calculated
1064 * ETag, the body is considered cached on the clients side and
1065 * a "304 Not Modified" status code is issued.
1066 *
1067 */
1068 PHP_FUNCTION(http_cache_etag)
1069 {
1070 char *etag;
1071 int etag_len = 0;
1072
1073 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
1074 RETURN_FALSE;
1075 }
1076
1077 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
1078 }
1079 /* }}} */
1080
1081 /* {{{ proto string ob_httpetaghandler(string data, int mode)
1082 *
1083 * For use with ob_start().
1084 * Note that this has to be started as first output buffer.
1085 * WARNING: Don't use with http_send_*().
1086 */
1087 PHP_FUNCTION(ob_httpetaghandler)
1088 {
1089 char *data;
1090 int data_len;
1091 long mode;
1092
1093 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1094 RETURN_FALSE;
1095 }
1096
1097 if (mode & PHP_OUTPUT_HANDLER_START) {
1098 if (HTTP_G(etag_started)) {
1099 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler can only be used once");
1100 RETURN_STRINGL(data, data_len, 1);
1101 }
1102 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL);
1103 HTTP_G(etag_started) = 1;
1104 }
1105
1106 if (OG(ob_nesting_level) > 1) {
1107 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler must be started prior to other output buffers");
1108 RETURN_STRINGL(data, data_len, 1);
1109 }
1110
1111 Z_TYPE_P(return_value) = IS_STRING;
1112 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
1113 }
1114 /* }}} */
1115
1116 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
1117 *
1118 * Redirect to a given url.
1119 * The supplied url will be expanded with http_absolute_uri(), the params array will
1120 * be treated with http_build_query() and the session identification will be appended
1121 * if session is true.
1122 *
1123 * Depending on permanent the redirection will be issued with a permanent
1124 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
1125 * status code.
1126 *
1127 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
1128 * if the client doesn't redirect immediatly.
1129 */
1130 PHP_FUNCTION(http_redirect)
1131 {
1132 int url_len;
1133 zend_bool session = 0, permanent = 0;
1134 zval *params = NULL;
1135 smart_str qstr = {0};
1136 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
1137
1138 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
1139 RETURN_FALSE;
1140 }
1141
1142 /* append session info */
1143 if (session && (PS(session_status) == php_session_active)) {
1144 if (!params) {
1145 MAKE_STD_ZVAL(params);
1146 array_init(params);
1147 }
1148 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
1149 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
1150 }
1151 }
1152
1153 /* treat params array with http_build_query() */
1154 if (params) {
1155 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
1156 if (qstr.c) {
1157 efree(qstr.c);
1158 }
1159 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
1160 RETURN_FALSE;
1161 }
1162 smart_str_0(&qstr);
1163 }
1164
1165 URI = http_absolute_uri(url, NULL);
1166 if (qstr.c) {
1167 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c);
1168 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, qstr.c, URI, qstr.c);
1169 efree(qstr.c);
1170 } else {
1171 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
1172 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
1173 }
1174 efree(URI);
1175
1176 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
1177 php_body_write(RED, strlen(RED) TSRMLS_CC);
1178 RETURN_TRUE;
1179 }
1180 RETURN_FALSE;
1181 }
1182 /* }}} */
1183
1184 /* {{{ proto bool http_send_data(string data)
1185 *
1186 * Sends raw data with support for (multiple) range requests.
1187 *
1188 */
1189 PHP_FUNCTION(http_send_data)
1190 {
1191 zval *zdata;
1192
1193 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
1194 RETURN_FALSE;
1195 }
1196
1197 convert_to_string_ex(&zdata);
1198 http_send_header("Accept-Ranges: bytes");
1199 RETURN_SUCCESS(http_send_data(zdata));
1200 }
1201 /* }}} */
1202
1203 /* {{{ proto bool http_send_file(string file)
1204 *
1205 * Sends a file with support for (multiple) range requests.
1206 *
1207 */
1208 PHP_FUNCTION(http_send_file)
1209 {
1210 zval *zfile;
1211
1212 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
1213 RETURN_FALSE;
1214 }
1215
1216 convert_to_string_ex(&zfile);
1217 http_send_header("Accept-Ranges: bytes");
1218 RETURN_SUCCESS(http_send_file(zfile));
1219 }
1220 /* }}} */
1221
1222 /* {{{ proto bool http_send_stream(resource stream)
1223 *
1224 * Sends an already opened stream with support for (multiple) range requests.
1225 *
1226 */
1227 PHP_FUNCTION(http_send_stream)
1228 {
1229 zval *zstream;
1230 php_stream *file;
1231
1232 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
1233 RETURN_FALSE;
1234 }
1235
1236 php_stream_from_zval(file, &zstream);
1237 http_send_header("Accept-Ranges: bytes");
1238 RETURN_SUCCESS(http_send_stream(file));
1239 }
1240 /* }}} */
1241
1242 /* {{{ proto string http_chunked_decode(string encoded)
1243 *
1244 * This function decodes a string that was HTTP-chunked encoded.
1245 * Returns false on failure.
1246 */
1247 PHP_FUNCTION(http_chunked_decode)
1248 {
1249 char *encoded = NULL, *decoded = NULL;
1250 int encoded_len = 0, decoded_len = 0;
1251
1252 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
1253 RETURN_FALSE;
1254 }
1255
1256 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
1257 RETURN_STRINGL(decoded, decoded_len, 0);
1258 } else {
1259 RETURN_FALSE;
1260 }
1261 }
1262 /* }}} */
1263
1264 /* {{{ proto array http_split_response(string http_response)
1265 *
1266 * This function splits an HTTP response into an array with headers and the
1267 * content body. The returned array may look simliar to the following example:
1268 *
1269 * <pre>
1270 * <?php
1271 * array(
1272 * 0 => array(
1273 * 'Status' => '200 Ok',
1274 * 'Content-Type' => 'text/plain',
1275 * 'Content-Language' => 'en-US'
1276 * ),
1277 * 1 => "Hello World!"
1278 * );
1279 * ?>
1280 * </pre>
1281 */
1282 PHP_FUNCTION(http_split_response)
1283 {
1284 zval *zresponse, *zbody, *zheaders;
1285
1286 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
1287 RETURN_FALSE;
1288 }
1289
1290 convert_to_string_ex(&zresponse);
1291
1292 MAKE_STD_ZVAL(zbody);
1293 MAKE_STD_ZVAL(zheaders);
1294 array_init(zheaders);
1295
1296 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
1297 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
1298 RETURN_FALSE;
1299 }
1300
1301 array_init(return_value);
1302 add_index_zval(return_value, 0, zheaders);
1303 add_index_zval(return_value, 1, zbody);
1304 }
1305 /* }}} */
1306
1307 /* {{{ proto array http_parse_headers(string header)
1308 *
1309 */
1310 PHP_FUNCTION(http_parse_headers)
1311 {
1312 char *header, *rnrn;
1313 int header_len;
1314
1315 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
1316 RETURN_FALSE;
1317 }
1318
1319 array_init(return_value);
1320
1321 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
1322 header_len = rnrn - header + 2;
1323 }
1324 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
1325 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
1326 zval_dtor(return_value);
1327 RETURN_FALSE;
1328 }
1329 }
1330 /* }}}*/
1331
1332 /* {{{ proto array http_get_request_headers(void)
1333 *
1334 */
1335 PHP_FUNCTION(http_get_request_headers)
1336 {
1337 if (ZEND_NUM_ARGS()) {
1338 WRONG_PARAM_COUNT;
1339 }
1340
1341 array_init(return_value);
1342 http_get_request_headers(return_value);
1343 }
1344 /* }}} */
1345
1346 /* {{{ HAVE_CURL */
1347 #ifdef HTTP_HAVE_CURL
1348
1349 /* {{{ proto string http_get(string url[, array options[, array &info]])
1350 *
1351 * Performs an HTTP GET request on the supplied url.
1352 *
1353 * The second parameter is expected to be an associative
1354 * array where the following keys will be recognized:
1355 * <pre>
1356 * - redirect: int, whether and how many redirects to follow
1357 * - unrestrictedauth: bool, whether to continue sending credentials on
1358 * redirects to a different host
1359 * - proxyhost: string, proxy host in "host[:port]" format
1360 * - proxyport: int, use another proxy port as specified in proxyhost
1361 * - proxyauth: string, proxy credentials in "user:pass" format
1362 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1363 * - httpauth: string, http credentials in "user:pass" format
1364 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1365 * - compress: bool, whether to allow gzip/deflate content encoding
1366 * (defaults to true)
1367 * - port: int, use another port as specified in the url
1368 * - referer: string, the referer to sends
1369 * - useragent: string, the user agent to send
1370 * (defaults to PECL::HTTP/version (PHP/version)))
1371 * - headers: array, list of custom headers as associative array
1372 * like array("header" => "value")
1373 * - cookies: array, list of cookies as associative array
1374 * like array("cookie" => "value")
1375 * - cookiestore: string, path to a file where cookies are/will be stored
1376 * </pre>
1377 *
1378 * The optional third parameter will be filled with some additional information
1379 * in form af an associative array, if supplied, like the following example:
1380 * <pre>
1381 * <?php
1382 * array (
1383 * 'effective_url' => 'http://localhost',
1384 * 'response_code' => 403,
1385 * 'total_time' => 0.017,
1386 * 'namelookup_time' => 0.013,
1387 * 'connect_time' => 0.014,
1388 * 'pretransfer_time' => 0.014,
1389 * 'size_upload' => 0,
1390 * 'size_download' => 202,
1391 * 'speed_download' => 11882,
1392 * 'speed_upload' => 0,
1393 * 'header_size' => 145,
1394 * 'request_size' => 62,
1395 * 'ssl_verifyresult' => 0,
1396 * 'filetime' => -1,
1397 * 'content_length_download' => 202,
1398 * 'content_length_upload' => 0,
1399 * 'starttransfer_time' => 0.017,
1400 * 'content_type' => 'text/html; charset=iso-8859-1',
1401 * 'redirect_time' => 0,
1402 * 'redirect_count' => 0,
1403 * 'private' => '',
1404 * 'http_connectcode' => 0,
1405 * 'httpauth_avail' => 0,
1406 * 'proxyauth_avail' => 0,
1407 * )
1408 * ?>
1409 * </pre>
1410 */
1411 PHP_FUNCTION(http_get)
1412 {
1413 char *URL, *data = NULL;
1414 size_t data_len = 0;
1415 int URL_len;
1416 zval *options = NULL, *info = NULL;
1417
1418 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1419 RETURN_FALSE;
1420 }
1421
1422 if (info) {
1423 zval_dtor(info);
1424 array_init(info);
1425 }
1426
1427 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1428 RETURN_STRINGL(data, data_len, 0);
1429 } else {
1430 RETURN_FALSE;
1431 }
1432 }
1433 /* }}} */
1434
1435 /* {{{ proto string http_head(string url[, array options[, array &info]])
1436 *
1437 * Performs an HTTP HEAD request on the suppied url.
1438 * Returns the HTTP response as string.
1439 * See http_get() for a full list of available options.
1440 */
1441 PHP_FUNCTION(http_head)
1442 {
1443 char *URL, *data = NULL;
1444 size_t data_len = 0;
1445 int URL_len;
1446 zval *options = NULL, *info = NULL;
1447
1448 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1449 RETURN_FALSE;
1450 }
1451
1452 if (info) {
1453 zval_dtor(info);
1454 array_init(info);
1455 }
1456
1457 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1458 RETURN_STRINGL(data, data_len, 0);
1459 } else {
1460 RETURN_FALSE;
1461 }
1462 }
1463 /* }}} */
1464
1465 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
1466 *
1467 * Performs an HTTP POST request, posting data.
1468 * Returns the HTTP response as string.
1469 * See http_get() for a full list of available options.
1470 */
1471 PHP_FUNCTION(http_post_data)
1472 {
1473 char *URL, *postdata, *data = NULL;
1474 size_t data_len = 0;
1475 int postdata_len, URL_len;
1476 zval *options = NULL, *info = NULL;
1477
1478 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1479 RETURN_FALSE;
1480 }
1481
1482 if (info) {
1483 zval_dtor(info);
1484 array_init(info);
1485 }
1486
1487 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1488 RETURN_STRINGL(data, data_len, 0);
1489 } else {
1490 RETURN_FALSE;
1491 }
1492 }
1493 /* }}} */
1494
1495 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
1496 *
1497 * Performs an HTTP POST request, posting www-form-urlencoded array data.
1498 * Returns the HTTP response as string.
1499 * See http_get() for a full list of available options.
1500 */
1501 PHP_FUNCTION(http_post_array)
1502 {
1503 char *URL, *data = NULL;
1504 size_t data_len = 0;
1505 int URL_len;
1506 zval *options = NULL, *info = NULL, *postdata;
1507
1508 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
1509 RETURN_FALSE;
1510 }
1511
1512 if (info) {
1513 zval_dtor(info);
1514 array_init(info);
1515 }
1516
1517 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1518 RETURN_STRINGL(data, data_len, 0);
1519 } else {
1520 RETURN_FALSE;
1521 }
1522 }
1523 /* }}} */
1524
1525 #endif
1526 /* }}} HAVE_CURL */
1527
1528
1529 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1530 *
1531 * Example:
1532 * <pre>
1533 * <?php
1534 * if (!http_auth_basic('mike', 's3c|r3t')) {
1535 * die('<h1>Authorization failed!</h1>');
1536 * }
1537 * ?>
1538 * </pre>
1539 */
1540 PHP_FUNCTION(http_auth_basic)
1541 {
1542 char *realm = NULL, *user, *pass, *suser, *spass;
1543 int r_len, u_len, p_len;
1544
1545 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
1546 RETURN_FALSE;
1547 }
1548
1549 if (!realm) {
1550 realm = "Restricted";
1551 }
1552
1553 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
1554 http_auth_header("Basic", realm);
1555 RETURN_FALSE;
1556 }
1557
1558 if (strcasecmp(suser, user)) {
1559 http_auth_header("Basic", realm);
1560 RETURN_FALSE;
1561 }
1562
1563 if (strcmp(spass, pass)) {
1564 http_auth_header("Basic", realm);
1565 RETURN_FALSE;
1566 }
1567
1568 RETURN_TRUE;
1569 }
1570 /* }}} */
1571
1572 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
1573 *
1574 * Example:
1575 * <pre>
1576 * <?php
1577 * function auth_cb($user, $pass)
1578 * {
1579 * global $db;
1580 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
1581 * if (strlen($realpass = $db->getOne($query)) {
1582 * return $pass === $realpass;
1583 * }
1584 * return false;
1585 * }
1586 *
1587 * if (!http_auth_basic_cb('auth_cb')) {
1588 * die('<h1>Authorization failed</h1>');
1589 * }
1590 * ?>
1591 * </pre>
1592 */
1593 PHP_FUNCTION(http_auth_basic_cb)
1594 {
1595 zval *cb;
1596 char *realm = NULL, *user, *pass;
1597 int r_len;
1598
1599 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1600 RETURN_FALSE;
1601 }
1602
1603 if (!realm) {
1604 realm = "Restricted";
1605 }
1606
1607 if (SUCCESS != http_auth_credentials(&user, &pass)) {
1608 http_auth_header("Basic", realm);
1609 RETURN_FALSE;
1610 }
1611 {
1612 zval *zparams[2] = {NULL, NULL}, retval;
1613 int result = 0;
1614
1615 MAKE_STD_ZVAL(zparams[0]);
1616 MAKE_STD_ZVAL(zparams[1]);
1617 ZVAL_STRING(zparams[0], user, 0);
1618 ZVAL_STRING(zparams[1], pass, 0);
1619
1620 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1621 &retval, 2, zparams TSRMLS_CC)) {
1622 result = Z_LVAL(retval);
1623 }
1624
1625 efree(user);
1626 efree(pass);
1627 efree(zparams[0]);
1628 efree(zparams[1]);
1629
1630 if (!result) {
1631 http_auth_header("Basic", realm);
1632 }
1633
1634 RETURN_BOOL(result);
1635 }
1636 }
1637 /* }}}*/
1638
1639
1640 /* {{{ php_http_init_globals(zend_http_globals *) */
1641 static void php_http_init_globals(zend_http_globals *http_globals)
1642 {
1643 http_globals->etag_started = 0;
1644 http_globals->ctype = NULL;
1645 http_globals->etag = NULL;
1646 http_globals->lmod = 0;
1647 #ifdef HTTP_HAVE_CURL
1648 http_globals->curlbuf.body.data = NULL;
1649 http_globals->curlbuf.body.used = 0;
1650 http_globals->curlbuf.body.free = 0;
1651 http_globals->curlbuf.hdrs.data = NULL;
1652 http_globals->curlbuf.hdrs.used = 0;
1653 http_globals->curlbuf.hdrs.free = 0;
1654 #endif
1655 http_globals->allowed_methods = NULL;
1656 }
1657 /* }}} */
1658
1659 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
1660 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
1661 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
1662 {
1663 if (length && SG(request_info).request_method && (!strstr(methods, SG(request_info).request_method))) {
1664 char *allow_header = emalloc(length + sizeof("Allow: "));
1665 sprintf(allow_header, "Allow: %s", methods);
1666 http_send_header(allow_header);
1667 efree(allow_header);
1668 http_send_status(405);
1669 zend_bailout();
1670 }
1671 }
1672 /* }}} */
1673
1674 /* {{{ PHP_INI */
1675 PHP_INI_MH(update_allowed_methods)
1676 {
1677 http_check_allowed_methods(new_value, new_value_length);
1678 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
1679 }
1680
1681 PHP_INI_BEGIN()
1682 STD_PHP_INI_ENTRY("http.allowed_methods", "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT", PHP_INI_ALL, update_allowed_methods, allowed_methods, zend_http_globals, http_globals)
1683 PHP_INI_END()
1684 /* }}} */
1685
1686 /* {{{ PHP_MINIT_FUNCTION */
1687 PHP_MINIT_FUNCTION(http)
1688 {
1689 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
1690 REGISTER_INI_ENTRIES();
1691
1692 #ifdef HTTP_HAVE_CURL
1693 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC, CONST_CS | CONST_PERSISTENT);
1694 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS | CONST_PERSISTENT);
1695 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM, CONST_CS | CONST_PERSISTENT);
1696 #endif
1697
1698 #ifdef ZEND_ENGINE_2
1699 /*
1700 REGISTER_LONG_CONSTANT("HTTP_GET", HTTP_GET, CONST_CS | CONST_PERSISTENT);
1701 REGISTER_LONG_CONSTANT("HTTP_HEAD", HTTP_HEAD, CONST_CS | CONST_PERSISTENT);
1702 REGISTER_LONG_CONSTANT("HTTP_POST", HTTP_POST, CONST_CS | CONST_PERSISTENT);
1703 */
1704 HTTP_REGISTER_CLASS(HTTPi, httpi, NULL, ZEND_ACC_FINAL_CLASS);
1705 HTTP_REGISTER_CLASS_EX(HTTPi_Response, httpi_response, NULL, 0);
1706 #endif
1707 return SUCCESS;
1708 }
1709 /* }}} */
1710
1711 /* {{{ PHP_MSHUTDOWN_FUNCTION */
1712 PHP_MSHUTDOWN_FUNCTION(http)
1713 {
1714 UNREGISTER_INI_ENTRIES();
1715 return SUCCESS;
1716 }
1717 /* }}} */
1718
1719 /* {{{ PHP_RINIT_FUNCTION */
1720 PHP_RINIT_FUNCTION(http)
1721 {
1722 char *allowed_methods = INI_STR("http.allowed_methods");
1723 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
1724 return SUCCESS;
1725 }
1726 /* }}} */
1727
1728 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1729 PHP_RSHUTDOWN_FUNCTION(http)
1730 {
1731 HTTP_G(etag_started) = 0;
1732 HTTP_G(lmod) = 0;
1733
1734 if (HTTP_G(etag)) {
1735 efree(HTTP_G(etag));
1736 HTTP_G(etag) = NULL;
1737 }
1738
1739 if (HTTP_G(ctype)) {
1740 efree(HTTP_G(ctype));
1741 HTTP_G(ctype) = NULL;
1742 }
1743 #ifdef HTTP_HAVE_CURL
1744 if (HTTP_G(curlbuf).body.data) {
1745 efree(HTTP_G(curlbuf).body.data);
1746 HTTP_G(curlbuf).body.data = NULL;
1747 }
1748 if (HTTP_G(curlbuf).hdrs.data) {
1749 efree(HTTP_G(curlbuf).hdrs.data);
1750 HTTP_G(curlbuf).hdrs.data = NULL;
1751 }
1752 #endif
1753 return SUCCESS;
1754 }
1755 /* }}} */
1756
1757 /* {{{ PHP_MINFO_FUNCTION */
1758 PHP_MINFO_FUNCTION(http)
1759 {
1760 php_info_print_table_start();
1761 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1762 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION);
1763 php_info_print_table_row(2, "cURL convenience functions:",
1764 #ifdef HTTP_HAVE_CURL
1765 "enabled"
1766 #else
1767 "disabled"
1768 #endif
1769 );
1770 php_info_print_table_end();
1771
1772 DISPLAY_INI_ENTRIES();
1773 }
1774 /* }}} */
1775
1776 /*
1777 * Local variables:
1778 * tab-width: 4
1779 * c-basic-offset: 4
1780 * End:
1781 * vim600: noet sw=4 ts=4 fdm=marker
1782 * vim<600: noet sw=4 ts=4
1783 */
1784