- we need a bit more space for the footer too in http_gzencode()
[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
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 #include "php.h"
23
24 #include "SAPI.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27
28 #include "php_http.h"
29 #include "php_http_std_defs.h"
30 #include "php_http_api.h"
31 #include "php_http_send_api.h"
32 #include "php_http_cache_api.h"
33 #include "php_http_headers_api.h"
34 #include "php_http_request_method_api.h"
35 #ifdef HTTP_HAVE_CURL
36 # include "php_http_request_api.h"
37 #endif
38
39 #ifdef ZEND_ENGINE_2
40 # include "php_http_util_object.h"
41 # include "php_http_message_object.h"
42 # ifndef WONKY
43 # include "php_http_response_object.h"
44 # endif
45 # ifdef HTTP_HAVE_CURL
46 # include "php_http_request_object.h"
47 # include "php_http_requestpool_object.h"
48 # endif
49 # include "php_http_exception_object.h"
50 #endif
51
52 #include "missing.h"
53 #include "phpstr/phpstr.h"
54
55 #ifdef HTTP_HAVE_CURL
56 # ifdef PHP_WIN32
57 # include <winsock2.h>
58 # endif
59 # include <curl/curl.h>
60 #endif
61 #ifdef HTTP_HAVE_MHASH
62 # include <mhash.h>
63 #endif
64
65 #include <ctype.h>
66
67 ZEND_DECLARE_MODULE_GLOBALS(http);
68 HTTP_DECLARE_ARG_PASS_INFO();
69
70 #ifdef COMPILE_DL_HTTP
71 ZEND_GET_MODULE(http)
72 #endif
73
74 /* {{{ http_functions[] */
75 zend_function_entry http_functions[] = {
76 PHP_FE(http_test, NULL)
77 PHP_FE(http_date, NULL)
78 PHP_FE(http_build_uri, NULL)
79 PHP_FALIAS(http_absolute_uri, http_build_uri, NULL)
80 PHP_FE(http_negotiate_language, http_arg_pass_ref_2)
81 PHP_FE(http_negotiate_charset, http_arg_pass_ref_2)
82 PHP_FE(http_redirect, NULL)
83 PHP_FE(http_throttle, NULL)
84 PHP_FE(http_send_status, NULL)
85 PHP_FE(http_send_last_modified, NULL)
86 PHP_FE(http_send_content_type, NULL)
87 PHP_FE(http_send_content_disposition, NULL)
88 PHP_FE(http_match_modified, NULL)
89 PHP_FE(http_match_etag, NULL)
90 PHP_FE(http_cache_last_modified, NULL)
91 PHP_FE(http_cache_etag, NULL)
92 PHP_FE(http_send_data, NULL)
93 PHP_FE(http_send_file, NULL)
94 PHP_FE(http_send_stream, NULL)
95 PHP_FE(http_chunked_decode, NULL)
96 PHP_FE(http_parse_message, NULL)
97 PHP_FE(http_parse_headers, NULL)
98 PHP_FE(http_get_request_headers, NULL)
99 PHP_FE(http_get_request_body, NULL)
100 PHP_FE(http_match_request_header, NULL)
101 #ifdef HTTP_HAVE_CURL
102 PHP_FE(http_get, http_arg_pass_ref_3)
103 PHP_FE(http_head, http_arg_pass_ref_3)
104 PHP_FE(http_post_data, http_arg_pass_ref_4)
105 PHP_FE(http_post_fields, http_arg_pass_ref_5)
106 PHP_FE(http_put_file, http_arg_pass_ref_4)
107 PHP_FE(http_put_stream, http_arg_pass_ref_4)
108 #endif
109 PHP_FE(http_request_method_register, NULL)
110 PHP_FE(http_request_method_unregister, NULL)
111 PHP_FE(http_request_method_exists, NULL)
112 PHP_FE(http_request_method_name, NULL)
113 #ifndef ZEND_ENGINE_2
114 PHP_FE(http_build_query, NULL)
115 #endif
116 PHP_FE(ob_etaghandler, NULL)
117 #ifdef HTTP_HAVE_ZLIB
118 PHP_FE(http_gzencode, NULL)
119 PHP_FE(http_gzdecode, NULL)
120 PHP_FE(http_deflate, NULL)
121 PHP_FE(http_inflate, NULL)
122 PHP_FE(http_compress, NULL)
123 PHP_FE(http_uncompress, NULL)
124 #endif
125
126 EMPTY_FUNCTION_ENTRY
127 };
128 /* }}} */
129
130 /* {{{ http_module_entry */
131 zend_module_entry http_module_entry = {
132 #if ZEND_MODULE_API_NO >= 20010901
133 STANDARD_MODULE_HEADER,
134 #endif
135 "http",
136 http_functions,
137 PHP_MINIT(http),
138 PHP_MSHUTDOWN(http),
139 PHP_RINIT(http),
140 PHP_RSHUTDOWN(http),
141 PHP_MINFO(http),
142 #if ZEND_MODULE_API_NO >= 20010901
143 HTTP_PEXT_VERSION,
144 #endif
145 STANDARD_MODULE_PROPERTIES
146 };
147 /* }}} */
148
149 int http_module_number;
150
151 /* {{{ http_globals */
152 static void http_globals_init_once(zend_http_globals *G)
153 {
154 memset(G, 0, sizeof(zend_http_globals));
155 }
156
157 static inline void http_globals_init(zend_http_globals *G)
158 {
159 G->send.buffer_size = HTTP_SENDBUF_SIZE;
160 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
161 #ifdef HTTP_HAVE_CURL
162 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
163 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
164 zend_llist_init(&G->request.copies.contexts, sizeof(http_request_callback_ctx *), http_request_data_free_context, 0);
165 zend_llist_init(&G->request.copies.convs, sizeof(http_request_conv *), http_request_data_free_conv, 0);
166 #endif
167 }
168
169 static inline void http_globals_free(zend_http_globals *G)
170 {
171 STR_SET(G->send.content_type, NULL);
172 STR_SET(G->send.unquoted_etag, NULL);
173 zend_hash_destroy(&G->request.methods.custom);
174 #ifdef HTTP_HAVE_CURL
175 zend_llist_clean(&G->request.copies.strings);
176 zend_llist_clean(&G->request.copies.slists);
177 zend_llist_clean(&G->request.copies.contexts);
178 zend_llist_clean(&G->request.copies.convs);
179 #endif
180 }
181 /* }}} */
182
183 /* {{{ static inline void http_check_allowed_methods(char *, int) */
184 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
185 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
186 {
187 if (length && SG(request_info).request_method) {
188 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
189 char *header = emalloc(length + sizeof("Allow: "));
190 sprintf(header, "Allow: %s", methods);
191 http_exit(405, header);
192 }
193 }
194 }
195 /* }}} */
196
197 /* {{{ PHP_INI */
198 PHP_INI_MH(http_update_allowed_methods)
199 {
200 http_check_allowed_methods(new_value, new_value_length);
201 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
202 }
203
204 PHP_INI_DISP(http_etag_mode_displayer)
205 {
206 long value;
207
208 if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
209 value = (ini_entry->orig_value) ? atoi(ini_entry->orig_value) : HTTP_ETAG_MD5;
210 } else if (ini_entry->value) {
211 value = (ini_entry->value[0]) ? atoi(ini_entry->value) : HTTP_ETAG_MD5;
212 } else {
213 value = HTTP_ETAG_MD5;
214 }
215
216 switch (value)
217 {
218 case HTTP_ETAG_CRC32:
219 ZEND_WRITE("HTTP_ETAG_CRC32", lenof("HTTP_ETAG_CRC32"));
220 break;
221
222 case HTTP_ETAG_SHA1:
223 ZEND_WRITE("HTTP_ETAG_SHA1", lenof("HTTP_ETAG_SHA1"));
224 break;
225
226 case HTTP_ETAG_MD5:
227 #ifndef HTTP_HAVE_MHASH
228 default:
229 #endif
230 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
231 break;
232
233 #ifdef HTTP_HAVE_MHASH
234 default:
235 {
236 const char *hash_name = mhash_get_hash_name_static(value);
237
238 if (!hash_name) {
239 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
240 } else {
241 ZEND_WRITE("HTTP_ETAG_MHASH_", lenof("HTTP_ETAG_MHASH_"));
242 ZEND_WRITE(hash_name, strlen(hash_name));
243 }
244 }
245 break;
246 #endif
247 }
248 }
249
250 #ifndef ZEND_ENGINE_2
251 # define OnUpdateLong OnUpdateInt
252 #endif
253
254 PHP_INI_BEGIN()
255 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
256 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
257 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
258 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
259 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
260 #ifdef ZEND_ENGINE_2
261 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
262 #endif
263 HTTP_PHP_INI_ENTRY_EX("http.etag_mode", "-2", PHP_INI_ALL, OnUpdateLong, http_etag_mode_displayer, etag.mode)
264 PHP_INI_END()
265 /* }}} */
266
267 /* {{{ PHP_MINIT_FUNCTION */
268 PHP_MINIT_FUNCTION(http)
269 {
270 http_module_number = module_number;
271
272 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL)
273
274 REGISTER_INI_ENTRIES();
275
276 if (SUCCESS != http_headers_global_init()) {
277 return FAILURE;
278 }
279 if (SUCCESS != http_cache_global_init()) {
280 return FAILURE;
281 }
282 if (SUCCESS != http_request_method_global_init()) {
283 return FAILURE;
284 }
285 #ifdef HTTP_HAVE_CURL
286 if (SUCCESS != http_request_global_init()) {
287 return FAILURE;
288 }
289 #endif /* HTTP_HAVE_CURL */
290
291 #ifdef ZEND_ENGINE_2
292 http_util_object_init();
293 http_message_object_init();
294 # ifndef WONKY
295 http_response_object_init();
296 # endif
297 # ifdef HTTP_HAVE_CURL
298 http_request_object_init();
299 http_requestpool_object_init();
300 # endif /* HTTP_HAVE_CURL */
301 http_exception_object_init();
302 #endif /* ZEND_ENGINE_2 */
303
304 return SUCCESS;
305 }
306 /* }}} */
307
308 /* {{{ PHP_MSHUTDOWN_FUNCTION */
309 PHP_MSHUTDOWN_FUNCTION(http)
310 {
311 UNREGISTER_INI_ENTRIES();
312 #ifdef HTTP_HAVE_CURL
313 http_request_global_cleanup();
314 #endif
315 return SUCCESS;
316 }
317 /* }}} */
318
319 /* {{{ PHP_RINIT_FUNCTION */
320 PHP_RINIT_FUNCTION(http)
321 {
322 char *m;
323
324 if (m = INI_STR("http.allowed_methods")) {
325 http_check_allowed_methods(m, strlen(m));
326 }
327
328 http_globals_init(HTTP_GLOBALS);
329 return SUCCESS;
330 }
331 /* }}} */
332
333 /* {{{ PHP_RSHUTDOWN_FUNCTION */
334 PHP_RSHUTDOWN_FUNCTION(http)
335 {
336 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
337 int i, c = zend_hash_num_elements(&HTTP_G(request).methods.custom);
338
339 for (i = 0; i < c; ++i) {
340 http_request_method_unregister(HTTP_MAX_REQUEST_METHOD + i);
341 }
342 #endif
343 http_globals_free(HTTP_GLOBALS);
344 return SUCCESS;
345 }
346 /* }}} */
347
348 /* {{{ PHP_MINFO_FUNCTION */
349 PHP_MINFO_FUNCTION(http)
350 {
351 php_info_print_table_start();
352 {
353 php_info_print_table_row(2, "Extended HTTP support", "enabled");
354 php_info_print_table_row(2, "Extension Version", HTTP_PEXT_VERSION);
355 #ifdef HTTP_HAVE_CURL
356 php_info_print_table_row(2, "cURL HTTP Requests", curl_version());
357 #else
358 php_info_print_table_row(2, "cURL HTTP Requests", "disabled");
359 #endif
360 #ifdef HTTP_HAVE_MHASH
361 {
362 char mhash_info[32];
363
364 snprintf(mhash_info, 32, "libmhash/%d", MHASH_API_VERSION);
365 php_info_print_table_row(2, "mhash ETag Generator", mhash_info);
366 }
367 #else
368 php_info_print_table_row(2, "mhash ETag Generator", "disabled");
369 #endif
370 #if defined(HTTP_HAVE_MAGIC) && !defined(WONKY)
371 php_info_print_table_row(2, "magic MIME Guessing", "libmagic/unknown");
372 #else
373 php_info_print_table_row(2, "magic MIME Guessing", "disabled");
374 #endif
375 php_info_print_table_row(2, "Registered Classes",
376 #ifndef ZEND_ENGINE_2
377 "none"
378 #else
379 "HttpUtil, "
380 "HttpMessage, "
381 # ifdef HTTP_HAVE_CURL
382 "HttpRequest, "
383 "HttpRequestPool, "
384 # endif
385 # ifndef WONKY
386 "HttpResponse"
387 # endif
388 #endif
389 );
390 }
391 php_info_print_table_end();
392
393 php_info_print_table_start();
394 php_info_print_table_colspan_header(2, "Supported ETag Hash Algorithms");
395 {
396
397 php_info_print_table_row(2, "PHP", "CRC32, MD5, SHA1");
398 #ifdef HTTP_HAVE_MHASH
399 {
400 phpstr *algos = phpstr_new();
401 int i, c = mhash_count();
402
403 for (i = 0; i <= c; ++i) {
404 const char *hash = mhash_get_hash_name_static(i);
405
406 if (hash) {
407 phpstr_appendf(algos, "%s, ", hash);
408 }
409 }
410 phpstr_fix(algos);
411 php_info_print_table_row(2, "MHASH", PHPSTR_VAL(algos));
412 phpstr_free(&algos);
413 }
414 #else
415 php_info_print_table_row(2, "MHASH", "not available");
416 #endif
417 }
418 php_info_print_table_end();
419
420 php_info_print_table_start();
421 php_info_print_table_colspan_header(2, "Request Methods");
422 {
423 unsigned i;
424 zval **custom_method;
425 phpstr *known_request_methods = phpstr_new();
426 phpstr *custom_request_methods = phpstr_new();
427
428 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
429 phpstr_appendl(known_request_methods, http_request_method_name(i));
430 phpstr_appends(known_request_methods, ", ");
431 }
432 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
433 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
434 phpstr_appends(custom_request_methods, ", ");
435 }
436
437 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
438 phpstr_fix(known_request_methods);
439 phpstr_fix(custom_request_methods);
440
441 php_info_print_table_row(2, "Known", PHPSTR_VAL(known_request_methods));
442 php_info_print_table_row(2, "Custom",
443 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
444 php_info_print_table_row(2, "Allowed", strlen(HTTP_G(request).methods.allowed) ? HTTP_G(request).methods.allowed : "(ANY)");
445
446 phpstr_free(&known_request_methods);
447 phpstr_free(&custom_request_methods);
448 }
449 php_info_print_table_end();
450
451 DISPLAY_INI_ENTRIES();
452 }
453 /* }}} */
454
455 /*
456 * Local variables:
457 * tab-width: 4
458 * c-basic-offset: 4
459 * End:
460 * vim600: noet sw=4 ts=4 fdm=marker
461 * vim<600: noet sw=4 ts=4
462 */
463