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