- add libevent support
[m6w6/ext-http] / http.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_CURL
17 #define HTTP_WANT_ZLIB
18 #define HTTP_WANT_MAGIC
19 #include "php_http.h"
20
21 #include "php_ini.h"
22 #include "ext/standard/info.h"
23 #include "zend_extensions.h"
24
25 #include "php_http_api.h"
26 #include "php_http_cache_api.h"
27 #include "php_http_cookie_api.h"
28 #include "php_http_encoding_api.h"
29 #include "php_http_filter_api.h"
30 #include "php_http_message_api.h"
31 #include "php_http_persistent_handle_api.h"
32 #include "php_http_request_api.h"
33 #include "php_http_request_datashare_api.h"
34 #include "php_http_request_method_api.h"
35 #include "php_http_request_pool_api.h"
36 #include "php_http_send_api.h"
37 #include "php_http_url_api.h"
38
39 #include "php_http_deflatestream_object.h"
40 #include "php_http_exception_object.h"
41 #include "php_http_inflatestream_object.h"
42 #include "php_http_message_object.h"
43 #include "php_http_querystring_object.h"
44 #include "php_http_request_object.h"
45 #include "php_http_requestdatashare_object.h"
46 #include "php_http_requestpool_object.h"
47 #include "php_http_response_object.h"
48 #include "php_http_util_object.h"
49
50 ZEND_DECLARE_MODULE_GLOBALS(http);
51 HTTP_DECLARE_ARG_PASS_INFO();
52
53 #ifdef COMPILE_DL_HTTP
54 ZEND_GET_MODULE(http)
55 #endif
56
57 /* {{{ http_functions[] */
58 zend_function_entry http_functions[] = {
59 PHP_FE(http_date, NULL)
60 PHP_FE(http_build_url, http_arg_pass_ref_4)
61 PHP_FE(http_build_str, NULL)
62 #ifndef ZEND_ENGINE_2
63 PHP_FALIAS(http_build_query, http_build_str, NULL)
64 #endif
65 PHP_FE(http_negotiate_language, http_arg_pass_ref_2)
66 PHP_FE(http_negotiate_charset, http_arg_pass_ref_2)
67 PHP_FE(http_negotiate_content_type, http_arg_pass_ref_2)
68 PHP_FE(http_redirect, NULL)
69 PHP_FE(http_throttle, NULL)
70 PHP_FE(http_send_status, NULL)
71 PHP_FE(http_send_last_modified, NULL)
72 PHP_FE(http_send_content_type, NULL)
73 PHP_FE(http_send_content_disposition, NULL)
74 PHP_FE(http_match_modified, NULL)
75 PHP_FE(http_match_etag, NULL)
76 PHP_FE(http_cache_last_modified, NULL)
77 PHP_FE(http_cache_etag, NULL)
78 PHP_FE(http_send_data, NULL)
79 PHP_FE(http_send_file, NULL)
80 PHP_FE(http_send_stream, NULL)
81 PHP_FE(http_chunked_decode, NULL)
82 PHP_FE(http_parse_message, NULL)
83 PHP_FE(http_parse_headers, NULL)
84 PHP_FE(http_parse_cookie, NULL)
85 PHP_FE(http_build_cookie, NULL)
86 PHP_FE(http_parse_params, NULL)
87 PHP_FE(http_get_request_headers, NULL)
88 PHP_FE(http_get_request_body, NULL)
89 PHP_FE(http_get_request_body_stream, NULL)
90 PHP_FE(http_match_request_header, NULL)
91 PHP_FE(http_persistent_handles_count, NULL)
92 PHP_FE(http_persistent_handles_clean, NULL)
93 PHP_FE(http_persistent_handles_ident, NULL)
94 #ifdef HTTP_HAVE_CURL
95 PHP_FE(http_get, http_arg_pass_ref_3)
96 PHP_FE(http_head, http_arg_pass_ref_3)
97 PHP_FE(http_post_data, http_arg_pass_ref_4)
98 PHP_FE(http_post_fields, http_arg_pass_ref_5)
99 PHP_FE(http_put_data, http_arg_pass_ref_4)
100 PHP_FE(http_put_file, http_arg_pass_ref_4)
101 PHP_FE(http_put_stream, http_arg_pass_ref_4)
102 PHP_FE(http_request, http_arg_pass_ref_5)
103 PHP_FE(http_request_body_encode, NULL)
104 #endif
105 PHP_FE(http_request_method_register, NULL)
106 PHP_FE(http_request_method_unregister, NULL)
107 PHP_FE(http_request_method_exists, NULL)
108 PHP_FE(http_request_method_name, NULL)
109 PHP_FE(ob_etaghandler, NULL)
110 #ifdef HTTP_HAVE_ZLIB
111 PHP_FE(http_deflate, NULL)
112 PHP_FE(http_inflate, NULL)
113 PHP_FE(ob_deflatehandler, NULL)
114 PHP_FE(ob_inflatehandler, NULL)
115 #endif
116 PHP_FE(http_support, NULL)
117
118 EMPTY_FUNCTION_ENTRY
119 };
120 /* }}} */
121
122 PHP_MINIT_FUNCTION(http);
123 PHP_MSHUTDOWN_FUNCTION(http);
124 PHP_RINIT_FUNCTION(http);
125 PHP_RSHUTDOWN_FUNCTION(http);
126 PHP_MINFO_FUNCTION(http);
127
128 /* {{{ http_module_dep */
129 #if ZEND_EXTENSION_API_NO >= 220050617
130 static zend_module_dep http_module_deps[] = {
131 # ifdef HTTP_HAVE_SPL
132 ZEND_MOD_REQUIRED("spl")
133 # endif
134 # ifdef HTTP_HAVE_HASH
135 ZEND_MOD_REQUIRED("hash")
136 # endif
137 # ifdef HTTP_HAVE_SESSION
138 ZEND_MOD_REQUIRED("session")
139 # endif
140 # ifdef HTTP_HAVE_ICONV
141 ZEND_MOD_REQUIRED("iconv")
142 # endif
143 {NULL, NULL, NULL, 0}
144 };
145 #endif
146 /* }}} */
147
148 /* {{{ http_module_entry */
149 zend_module_entry http_module_entry = {
150 #if ZEND_EXTENSION_API_NO >= 220050617
151 STANDARD_MODULE_HEADER_EX, NULL,
152 http_module_deps,
153 #else
154 STANDARD_MODULE_HEADER,
155 #endif
156 "http",
157 http_functions,
158 PHP_MINIT(http),
159 PHP_MSHUTDOWN(http),
160 PHP_RINIT(http),
161 PHP_RSHUTDOWN(http),
162 PHP_MINFO(http),
163 PHP_EXT_HTTP_VERSION,
164 STANDARD_MODULE_PROPERTIES
165 };
166 /* }}} */
167
168 int http_module_number;
169
170 /* {{{ http_globals */
171 static void http_globals_init_once(zend_http_globals *G)
172 {
173 memset(G, 0, sizeof(zend_http_globals));
174 }
175
176 #define http_globals_init(g) _http_globals_init((g) TSRMLS_CC)
177 static inline void _http_globals_init(zend_http_globals *G TSRMLS_DC)
178 {
179 #ifdef HTTP_HAVE_SAPI_RTIME
180 G->request.time = sapi_get_request_time(TSRMLS_C);
181 #else
182 G->request.time = time(NULL);
183 #endif
184 G->send.buffer_size = 0;
185 G->read_post_data = 0;
186 }
187
188 #define http_globals_free(g) _http_globals_free((g) TSRMLS_CC)
189 static inline void _http_globals_free(zend_http_globals *G TSRMLS_DC)
190 {
191 if (G->request.headers) {
192 zend_hash_destroy(G->request.headers);
193 FREE_HASHTABLE(G->request.headers);
194 G->request.headers = NULL;
195 }
196 STR_SET(G->send.content_type, NULL);
197 STR_SET(G->send.unquoted_etag, NULL);
198 if (G->server_var) {
199 zval_ptr_dtor(&G->server_var);
200 G->server_var = NULL;
201 }
202 }
203
204 #if defined(ZTS) && defined(PHP_DEBUG)
205 #if ZTS && PHP_DEBUG
206 zend_http_globals *http_globals(void)
207 {
208 TSRMLS_FETCH();
209 return HTTP_G;
210 }
211 #endif
212 #endif
213 /* }}} */
214
215 /* {{{ static inline void http_check_allowed_methods(char *) */
216 #define http_check_allowed_methods(m) _http_check_allowed_methods((m) TSRMLS_CC)
217 static inline void _http_check_allowed_methods(const char *methods TSRMLS_DC)
218 {
219 if (*methods && SG(request_info).request_method) {
220 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
221 char *header;
222 spprintf(&header, 0, "Allow: %s", methods);
223 http_exit(405, header);
224 }
225 }
226 }
227 /* }}} */
228
229 /* {{{ PHP_INI */
230 PHP_INI_MH(http_update_allowed_methods)
231 {
232 if (*new_value) {
233 http_check_allowed_methods(new_value);
234 }
235 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
236 }
237 PHP_INI_MH(http_update_persistent_handle_ident)
238 {
239 HTTP_G->persistent.handles.ident.h = zend_hash_func(new_value, HTTP_G->persistent.handles.ident.l = new_value_length+1);
240 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
241 }
242
243 #ifndef ZEND_ENGINE_2
244 # define OnUpdateLong OnUpdateInt
245 #endif
246
247 PHP_INI_BEGIN()
248 HTTP_PHP_INI_ENTRY("http.etag.mode", "MD5", PHP_INI_ALL, OnUpdateString, etag.mode)
249 HTTP_PHP_INI_ENTRY("http.log.cache", "", PHP_INI_ALL, OnUpdateString, log.cache)
250 HTTP_PHP_INI_ENTRY("http.log.redirect", "", PHP_INI_ALL, OnUpdateString, log.redirect)
251 HTTP_PHP_INI_ENTRY("http.log.not_found", "", PHP_INI_ALL, OnUpdateString, log.not_found)
252 HTTP_PHP_INI_ENTRY("http.log.allowed_methods", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
253 HTTP_PHP_INI_ENTRY("http.log.composite", "", PHP_INI_ALL, OnUpdateString, log.composite)
254 HTTP_PHP_INI_ENTRY("http.request.methods.allowed", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
255 HTTP_PHP_INI_ENTRY("http.request.methods.custom", "", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateString, request.methods.custom)
256 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
257 HTTP_PHP_INI_ENTRY("http.request.datashare.cookie", "0", PHP_INI_SYSTEM, OnUpdateBool, request.datashare.cookie)
258 HTTP_PHP_INI_ENTRY("http.request.datashare.dns", "1", PHP_INI_SYSTEM, OnUpdateBool, request.datashare.dns)
259 HTTP_PHP_INI_ENTRY("http.request.datashare.ssl", "0", PHP_INI_SYSTEM, OnUpdateBool, request.datashare.ssl)
260 HTTP_PHP_INI_ENTRY("http.request.datashare.connect", "0", PHP_INI_SYSTEM, OnUpdateBool, request.datashare.connect)
261 #endif
262 #ifdef HTTP_HAVE_ZLIB
263 HTTP_PHP_INI_ENTRY("http.send.inflate.start_auto", "0", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, send.inflate.start_auto)
264 HTTP_PHP_INI_ENTRY("http.send.inflate.start_flags", "0", PHP_INI_ALL, OnUpdateLong, send.inflate.start_flags)
265 HTTP_PHP_INI_ENTRY("http.send.deflate.start_auto", "0", PHP_INI_PERDIR|PHP_INI_SYSTEM, OnUpdateBool, send.deflate.start_auto)
266 HTTP_PHP_INI_ENTRY("http.send.deflate.start_flags", "0", PHP_INI_ALL, OnUpdateLong, send.deflate.start_flags)
267 #endif
268 HTTP_PHP_INI_ENTRY("http.persistent.handles.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent.handles.limit)
269 HTTP_PHP_INI_ENTRY("http.persistent.handles.ident", "GLOBAL", PHP_INI_ALL, http_update_persistent_handle_ident, persistent.handles.ident.s)
270 HTTP_PHP_INI_ENTRY("http.send.not_found_404", "1", PHP_INI_ALL, OnUpdateBool, send.not_found_404)
271 #ifdef ZEND_ENGINE_2
272 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
273 #endif
274 HTTP_PHP_INI_ENTRY("http.force_exit", "1", PHP_INI_ALL, OnUpdateBool, force_exit)
275 PHP_INI_END()
276 /* }}} */
277
278 /* {{{ PHP_MINIT_FUNCTION */
279 PHP_MINIT_FUNCTION(http)
280 {
281 http_module_number = module_number;
282 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL);
283 REGISTER_INI_ENTRIES();
284
285 if (0
286 || SUCCESS != PHP_MINIT_CALL(http_persistent_handle) /* first */
287 || SUCCESS != PHP_MINIT_CALL(http_cookie)
288 #ifdef HTTP_HAVE_ZLIB
289 || SUCCESS != PHP_MINIT_CALL(http_encoding)
290 #endif
291 #ifdef HTTP_HAVE_CURL
292 || SUCCESS != PHP_MINIT_CALL(http_request)
293 # ifdef ZEND_ENGINE_2
294 # endif
295 #endif
296 || SUCCESS != PHP_MINIT_CALL(http_request_method)
297 || SUCCESS != PHP_MINIT_CALL(http_send)
298 || SUCCESS != PHP_MINIT_CALL(http_support)
299 || SUCCESS != PHP_MINIT_CALL(http_url)
300
301 #ifdef ZEND_ENGINE_2
302 || SUCCESS != PHP_MINIT_CALL(http_filter)
303 || SUCCESS != PHP_MINIT_CALL(http_exception_object)
304 # ifdef HTTP_HAVE_ZLIB
305 || SUCCESS != PHP_MINIT_CALL(http_deflatestream_object)
306 || SUCCESS != PHP_MINIT_CALL(http_inflatestream_object)
307 # endif
308 || SUCCESS != PHP_MINIT_CALL(http_message_object)
309 || SUCCESS != PHP_MINIT_CALL(http_querystring_object)
310 # ifdef HTTP_HAVE_CURL
311 || SUCCESS != PHP_MINIT_CALL(http_request_datashare)
312 || SUCCESS != PHP_MINIT_CALL(http_request_pool)
313 || SUCCESS != PHP_MINIT_CALL(http_request_object)
314 || SUCCESS != PHP_MINIT_CALL(http_requestdatashare_object)
315 || SUCCESS != PHP_MINIT_CALL(http_requestpool_object)
316 # endif
317 # ifndef WONKY
318 || SUCCESS != PHP_MINIT_CALL(http_response_object)
319 # endif
320 || SUCCESS != PHP_MINIT_CALL(http_util_object)
321 #endif
322 ) {
323 return FAILURE;
324 }
325
326 return SUCCESS;
327 }
328 /* }}} */
329
330 /* {{{ PHP_MSHUTDOWN_FUNCTION */
331 PHP_MSHUTDOWN_FUNCTION(http)
332 {
333 UNREGISTER_INI_ENTRIES();
334
335 if (0
336 #ifdef HTTP_HAVE_CURL
337 || SUCCESS != PHP_MSHUTDOWN_CALL(http_request)
338 # ifdef ZEND_ENGINE_2
339 || SUCCESS != PHP_MSHUTDOWN_CALL(http_request_datashare)
340 # endif
341 #endif
342 || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle) /* last */
343 ) {
344 return FAILURE;
345 }
346
347 return SUCCESS;
348 }
349 /* }}} */
350
351 /* {{{ PHP_RINIT_FUNCTION */
352 PHP_RINIT_FUNCTION(http)
353 {
354 http_globals_init(HTTP_G);
355
356 if (HTTP_G->request.methods.allowed && *HTTP_G->request.methods.allowed) {
357 http_check_allowed_methods(HTTP_G->request.methods.allowed);
358 }
359
360 if (0
361 #ifdef HTTP_HAVE_ZLIB
362 || SUCCESS != PHP_RINIT_CALL(http_encoding)
363 #endif
364 #ifdef HTTP_HAVE_CURL
365 # ifdef ZEND_ENGINE_2
366 # ifdef HTTP_HAVE_EVENT
367 || SUCCESS != PHP_RINIT_CALL(http_request_pool)
368 # endif
369 || SUCCESS != PHP_RINIT_CALL(http_request_datashare)
370 # endif
371 #endif
372 || SUCCESS != PHP_RINIT_CALL(http_request_method)
373 ) {
374 return FAILURE;
375 }
376
377 return SUCCESS;
378 }
379 /* }}} */
380
381 /* {{{ PHP_RSHUTDOWN_FUNCTION */
382 PHP_RSHUTDOWN_FUNCTION(http)
383 {
384 STATUS status = SUCCESS;
385
386 if (0
387 #ifdef HTTP_HAVE_ZLIB
388 || SUCCESS != PHP_RSHUTDOWN_CALL(http_encoding)
389 #endif
390 #ifdef HTTP_HAVE_CURL
391 # ifdef ZEND_ENGINE_2
392 || SUCCESS != PHP_RSHUTDOWN_CALL(http_request_datashare)
393 # endif
394 #endif
395 || SUCCESS != PHP_RSHUTDOWN_CALL(http_request_method)
396 ) {
397 status = FAILURE;
398 }
399
400 http_globals_free(HTTP_G);
401 return status;
402 }
403 /* }}} */
404
405 /* {{{ PHP_MINFO_FUNCTION */
406 PHP_MINFO_FUNCTION(http)
407 {
408 php_info_print_table_start();
409 {
410 php_info_print_table_row(2, "HTTP Support", "enabled");
411 php_info_print_table_row(2, "Extension Version", PHP_EXT_HTTP_VERSION);
412 php_info_print_table_row(2, "Registered Classes",
413 #ifndef ZEND_ENGINE_2
414 "none"
415 #else
416 "HttpUtil, "
417 "HttpMessage, "
418 # ifdef HTTP_HAVE_CURL
419 "HttpRequest, "
420 "HttpRequestPool, "
421 "HttpRequestDataShare, "
422 # endif
423 # ifdef HTTP_HAVE_ZLIB
424 "HttpDeflateStream, "
425 "HttpInflateStream, "
426 # endif
427 # ifndef WONKY
428 "HttpResponse, "
429 # endif
430 "HttpQueryString"
431 #endif
432 );
433 php_info_print_table_row(2, "Output Handlers", "ob_deflatehandler, ob_inflatehandler, ob_etaghandler");
434 php_info_print_table_row(2, "Stream Filters",
435 #ifndef ZEND_ENGINE_2
436 "none"
437 #else
438 "http.chunked_decode, http.chunked_encode, http.deflate, http.inflate"
439 #endif
440 );
441 }
442 php_info_print_table_end();
443
444 php_info_print_table_start();
445 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
446 {
447 #ifdef HTTP_HAVE_CURL
448 curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
449 php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
450 #else
451 php_info_print_table_row(2, "libcurl", "disabled", "disabled");
452 #endif
453 #ifdef HTTP_HAVE_ZLIB
454 php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
455 #else
456 php_info_print_table_row(3, "libz", "disabled", "disabled");
457 #endif
458 #if defined(HTTP_HAVE_MAGIC)
459 php_info_print_table_row(3, "libmagic", "unknown", "unknown");
460 #else
461 php_info_print_table_row(3, "libmagic", "disabled", "disabled");
462 #endif
463 }
464 php_info_print_table_end();
465
466 php_info_print_table_start();
467 php_info_print_table_colspan_header(4, "Persistent Handles");
468 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
469 {
470 HashTable *ht;
471 HashPosition pos1, pos2;
472 HashKey provider = initHashKey(0), ident = initHashKey(0);
473 zval **val, **sub, **zused, **zfree;
474
475 if ((ht = http_persistent_handle_statall()) && zend_hash_num_elements(ht)) {
476 FOREACH_HASH_KEYVAL(pos1, ht, provider, val) {
477 if (zend_hash_num_elements(Z_ARRVAL_PP(val))) {
478 FOREACH_KEYVAL(pos2, *val, ident, sub) {
479 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("used"), (void *) &zused) &&
480 SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("free"), (void *) &zfree)) {
481 convert_to_string(*zused);
482 convert_to_string(*zfree);
483 php_info_print_table_row(4, provider.str, ident.str, Z_STRVAL_PP(zused), Z_STRVAL_PP(zfree));
484 } else {
485 php_info_print_table_row(4, provider.str, ident.str, "0", "0");
486 }
487 }
488 } else {
489 php_info_print_table_row(4, provider.str, "N/A", "0", "0");
490 }
491 }
492 } else {
493 php_info_print_table_row(4, "N/A", "N/A", "0", "0");
494 }
495 if (ht) {
496 zend_hash_destroy(ht);
497 FREE_HASHTABLE(ht);
498 }
499 }
500 php_info_print_table_end();
501
502 php_info_print_table_start();
503 php_info_print_table_colspan_header(2, "Request Methods");
504 {
505 HashPosition pos;
506 phpstr *methods = phpstr_new();
507 char **name;
508
509 FOREACH_HASH_VAL(pos, &HTTP_G->request.methods.registered, name) {
510 if (pos->h) {
511 phpstr_appendf(methods, "%s, ", *name);
512 }
513 }
514 phpstr_fix(methods);
515 php_info_print_table_row(2, "Registered", PHPSTR_VAL(methods));
516 php_info_print_table_row(2, "Allowed", *HTTP_G->request.methods.allowed ? HTTP_G->request.methods.allowed : "(ANY)");
517 phpstr_free(&methods);
518 }
519 php_info_print_table_end();
520
521 DISPLAY_INI_ENTRIES();
522 }
523 /* }}} */
524
525 /*
526 * Local variables:
527 * tab-width: 4
528 * c-basic-offset: 4
529 * End:
530 * vim600: noet sw=4 ts=4 fdm=marker
531 * vim<600: noet sw=4 ts=4
532 */
533