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