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