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