- add gzip header verification
[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 #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_entry */
135 zend_module_entry http_module_entry = {
136 #if ZEND_MODULE_API_NO >= 20010901
137 STANDARD_MODULE_HEADER,
138 #endif
139 "http",
140 http_functions,
141 PHP_MINIT(http),
142 PHP_MSHUTDOWN(http),
143 PHP_RINIT(http),
144 PHP_RSHUTDOWN(http),
145 PHP_MINFO(http),
146 #if ZEND_MODULE_API_NO >= 20010901
147 HTTP_PEXT_VERSION,
148 #endif
149 STANDARD_MODULE_PROPERTIES
150 };
151 /* }}} */
152
153 int http_module_number;
154
155 /* {{{ http_globals */
156 static void http_globals_init_once(zend_http_globals *G)
157 {
158 memset(G, 0, sizeof(zend_http_globals));
159 }
160
161 static inline void http_globals_init(zend_http_globals *G)
162 {
163 G->send.buffer_size = HTTP_SENDBUF_SIZE;
164 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
165 #ifdef HTTP_HAVE_CURL
166 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
167 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
168 zend_llist_init(&G->request.copies.contexts, sizeof(http_request_callback_ctx *), http_request_data_free_context, 0);
169 zend_llist_init(&G->request.copies.convs, sizeof(http_request_conv *), http_request_data_free_conv, 0);
170 #endif
171 }
172
173 static inline void http_globals_free(zend_http_globals *G)
174 {
175 STR_SET(G->send.content_type, NULL);
176 STR_SET(G->send.unquoted_etag, NULL);
177 zend_hash_destroy(&G->request.methods.custom);
178 #ifdef HTTP_HAVE_CURL
179 zend_llist_clean(&G->request.copies.strings);
180 zend_llist_clean(&G->request.copies.slists);
181 zend_llist_clean(&G->request.copies.contexts);
182 zend_llist_clean(&G->request.copies.convs);
183 #endif
184 }
185 /* }}} */
186
187 /* {{{ static inline void http_check_allowed_methods(char *, int) */
188 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
189 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
190 {
191 if (length && SG(request_info).request_method) {
192 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
193 char *header = emalloc(length + sizeof("Allow: "));
194 sprintf(header, "Allow: %s", methods);
195 http_exit(405, header);
196 }
197 }
198 }
199 /* }}} */
200
201 /* {{{ PHP_INI */
202 PHP_INI_MH(http_update_allowed_methods)
203 {
204 http_check_allowed_methods(new_value, new_value_length);
205 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
206 }
207
208 PHP_INI_DISP(http_etag_mode_displayer)
209 {
210 long value;
211
212 if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
213 value = (ini_entry->orig_value) ? atoi(ini_entry->orig_value) : HTTP_ETAG_MD5;
214 } else if (ini_entry->value) {
215 value = (ini_entry->value[0]) ? atoi(ini_entry->value) : HTTP_ETAG_MD5;
216 } else {
217 value = HTTP_ETAG_MD5;
218 }
219
220 switch (value)
221 {
222 case HTTP_ETAG_CRC32:
223 ZEND_WRITE("HTTP_ETAG_CRC32", lenof("HTTP_ETAG_CRC32"));
224 break;
225
226 case HTTP_ETAG_SHA1:
227 ZEND_WRITE("HTTP_ETAG_SHA1", lenof("HTTP_ETAG_SHA1"));
228 break;
229
230 case HTTP_ETAG_MD5:
231 #ifndef HTTP_HAVE_MHASH
232 default:
233 #endif
234 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
235 break;
236
237 #ifdef HTTP_HAVE_MHASH
238 default:
239 {
240 const char *hash_name = mhash_get_hash_name_static(value);
241
242 if (!hash_name) {
243 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
244 } else {
245 ZEND_WRITE("HTTP_ETAG_MHASH_", lenof("HTTP_ETAG_MHASH_"));
246 ZEND_WRITE(hash_name, strlen(hash_name));
247 }
248 }
249 break;
250 #endif
251 }
252 }
253
254 #ifndef ZEND_ENGINE_2
255 # define OnUpdateLong OnUpdateInt
256 #endif
257
258 PHP_INI_BEGIN()
259 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
260 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
261 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
262 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
263 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
264 #ifdef ZEND_ENGINE_2
265 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
266 #endif
267 HTTP_PHP_INI_ENTRY_EX("http.etag_mode", "-2", PHP_INI_ALL, OnUpdateLong, http_etag_mode_displayer, etag.mode)
268 PHP_INI_END()
269 /* }}} */
270
271 /* {{{ PHP_MINIT_FUNCTION */
272 PHP_MINIT_FUNCTION(http)
273 {
274 http_module_number = module_number;
275
276 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL)
277
278 REGISTER_INI_ENTRIES();
279
280 if ( (SUCCESS != PHP_MINIT_CALL(http_support)) ||
281 (SUCCESS != PHP_MINIT_CALL(http_headers)) ||
282 (SUCCESS != PHP_MINIT_CALL(http_cache)) ||
283 #ifdef HTTP_HAVE_CURL
284 (SUCCESS != PHP_MINIT_CALL(http_request)) ||
285 #endif /* HTTP_HAVE_CURL */
286 (SUCCESS != PHP_MINIT_CALL(http_request_method))) {
287 return FAILURE;
288 }
289
290 #ifdef ZEND_ENGINE_2
291 if ( (SUCCESS != PHP_MINIT_CALL(http_util_object)) ||
292 (SUCCESS != PHP_MINIT_CALL(http_message_object)) ||
293 # ifndef WONKY
294 (SUCCESS != PHP_MINIT_CALL(http_response_object)) ||
295 # endif /* WONKY */
296 # ifdef HTTP_HAVE_CURL
297 (SUCCESS != PHP_MINIT_CALL(http_request_object)) ||
298 (SUCCESS != PHP_MINIT_CALL(http_requestpool_object)) ||
299 # endif /* HTTP_HAVE_CURL */
300 (SUCCESS != PHP_MINIT_CALL(http_exception_object))) {
301 return FAILURE;
302 }
303 #endif /* ZEND_ENGINE_2 */
304
305 return SUCCESS;
306 }
307 /* }}} */
308
309 /* {{{ PHP_MSHUTDOWN_FUNCTION */
310 PHP_MSHUTDOWN_FUNCTION(http)
311 {
312 UNREGISTER_INI_ENTRIES();
313 #ifdef HTTP_HAVE_CURL
314 return PHP_MSHUTDOWN_CALL(http_request);
315 #endif
316 return SUCCESS;
317 }
318 /* }}} */
319
320 /* {{{ PHP_RINIT_FUNCTION */
321 PHP_RINIT_FUNCTION(http)
322 {
323 char *m;
324
325 if (m = INI_STR("http.allowed_methods")) {
326 http_check_allowed_methods(m, strlen(m));
327 }
328
329 http_globals_init(HTTP_GLOBALS);
330 return SUCCESS;
331 }
332 /* }}} */
333
334 /* {{{ PHP_RSHUTDOWN_FUNCTION */
335 PHP_RSHUTDOWN_FUNCTION(http)
336 {
337 STATUS status = SUCCESS;
338
339 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
340 status = PHP_RSHUTDOWN_CALL(http_request_method);
341 #endif
342
343 http_globals_free(HTTP_GLOBALS);
344 return status;
345 }
346 /* }}} */
347
348 /* {{{ PHP_MINFO_FUNCTION */
349 PHP_MINFO_FUNCTION(http)
350 {
351 php_info_print_table_start();
352 {
353 php_info_print_table_row(2, "Extended HTTP support", "enabled");
354 php_info_print_table_row(2, "Extension Version", HTTP_PEXT_VERSION);
355 #ifdef HTTP_HAVE_CURL
356 php_info_print_table_row(2, "cURL HTTP Requests", curl_version());
357 #else
358 php_info_print_table_row(2, "cURL HTTP Requests", "disabled");
359 #endif
360 #ifdef HTTP_HAVE_ZLIB
361 {
362 char my_zlib_version[64] = {0};
363
364 strlcat(my_zlib_version, "zlib/", 63);
365 strlcat(my_zlib_version, zlibVersion(), 63);
366 php_info_print_table_row(2, "zlib GZIP Encodings", my_zlib_version);
367 }
368 #else
369 php_info_print_table_row(2, "zlib GZIP Encodings", "disabled");
370 #endif
371 #ifdef HTTP_HAVE_MHASH
372 {
373 char mhash_info[32];
374
375 snprintf(mhash_info, 32, "libmhash/%d", MHASH_API_VERSION);
376 php_info_print_table_row(2, "mhash ETag Generator", mhash_info);
377 }
378 #else
379 php_info_print_table_row(2, "mhash ETag Generator", "disabled");
380 #endif
381 #if defined(HTTP_HAVE_MAGIC) && !defined(WONKY)
382 php_info_print_table_row(2, "magic MIME Guessing", "libmagic/unknown");
383 #else
384 php_info_print_table_row(2, "magic MIME Guessing", "disabled");
385 #endif
386 php_info_print_table_row(2, "Registered Classes",
387 #ifndef ZEND_ENGINE_2
388 "none"
389 #else
390 "HttpUtil, "
391 "HttpMessage, "
392 # ifdef HTTP_HAVE_CURL
393 "HttpRequest, "
394 "HttpRequestPool, "
395 # endif
396 # ifndef WONKY
397 "HttpResponse"
398 # endif
399 #endif
400 );
401 }
402 php_info_print_table_end();
403
404 php_info_print_table_start();
405 php_info_print_table_colspan_header(2, "Supported ETag Hash Algorithms");
406 {
407
408 php_info_print_table_row(2, "PHP", "CRC32, MD5, SHA1");
409 #ifdef HTTP_HAVE_MHASH
410 {
411 phpstr *algos = phpstr_new();
412 int i, c = mhash_count();
413
414 for (i = 0; i <= c; ++i) {
415 const char *hash = mhash_get_hash_name_static(i);
416
417 if (hash) {
418 phpstr_appendf(algos, "%s, ", hash);
419 }
420 }
421 phpstr_fix(algos);
422 php_info_print_table_row(2, "MHASH", PHPSTR_VAL(algos));
423 phpstr_free(&algos);
424 }
425 #else
426 php_info_print_table_row(2, "MHASH", "not available");
427 #endif
428 }
429 php_info_print_table_end();
430
431 php_info_print_table_start();
432 php_info_print_table_colspan_header(2, "Request Methods");
433 {
434 unsigned i;
435 zval **custom_method;
436 phpstr *known_request_methods = phpstr_new();
437 phpstr *custom_request_methods = phpstr_new();
438
439 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
440 phpstr_appendl(known_request_methods, http_request_method_name(i));
441 phpstr_appends(known_request_methods, ", ");
442 }
443 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
444 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
445 phpstr_appends(custom_request_methods, ", ");
446 }
447
448 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
449 phpstr_fix(known_request_methods);
450 phpstr_fix(custom_request_methods);
451
452 php_info_print_table_row(2, "Known", PHPSTR_VAL(known_request_methods));
453 php_info_print_table_row(2, "Custom",
454 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
455 php_info_print_table_row(2, "Allowed", strlen(HTTP_G(request).methods.allowed) ? HTTP_G(request).methods.allowed : "(ANY)");
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