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