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