separate php_http_env_response and implement content encoding
[m6w6/ext-http] / php_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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id: http.c 300300 2010-06-09 07:29:35Z mike $ */
14
15 #include "php_http.h"
16
17 #include <zlib.h>
18 #ifdef PHP_HTTP_HAVE_CURL
19 # include <curl/curl.h>
20 # ifdef PHP_HTTP_HAVE_EVENT
21 # include <event.h>
22 # endif
23 #endif
24 #ifdef PHP_HTTP_HAVE_NEON
25 # include "neon/ne_utils.h"
26 #endif
27
28 #include <main/php_ini.h>
29 #include <ext/standard/info.h>
30 #include <Zend/zend_extensions.h>
31
32 ZEND_DECLARE_MODULE_GLOBALS(php_http);
33
34 #ifdef COMPILE_DL_HTTP
35 ZEND_GET_MODULE(http)
36 #endif
37
38 zend_function_entry http_functions[] = {
39 EMPTY_FUNCTION_ENTRY
40 };
41
42 PHP_MINIT_FUNCTION(http);
43 PHP_MSHUTDOWN_FUNCTION(http);
44 PHP_RINIT_FUNCTION(http);
45 PHP_RSHUTDOWN_FUNCTION(http);
46 PHP_MINFO_FUNCTION(http);
47
48 static zend_module_dep http_module_deps[] = {
49 ZEND_MOD_REQUIRED("spl")
50 #ifdef PHP_HTTP_HAVE_HASH
51 ZEND_MOD_REQUIRED("hash")
52 #endif
53 #ifdef PHP_HTTP_HAVE_ICONV
54 ZEND_MOD_REQUIRED("iconv")
55 #endif
56 #ifdef PHP_HTTP_HAVE_EVENT
57 ZEND_MOD_CONFLICTS("event")
58 #endif
59 {NULL, NULL, NULL, 0}
60 };
61
62 zend_module_entry http_module_entry = {
63 STANDARD_MODULE_HEADER_EX,
64 NULL,
65 http_module_deps,
66 "http",
67 http_functions,
68 PHP_MINIT(http),
69 PHP_MSHUTDOWN(http),
70 PHP_RINIT(http),
71 PHP_RSHUTDOWN(http),
72 PHP_MINFO(http),
73 PHP_HTTP_EXT_VERSION,
74 STANDARD_MODULE_PROPERTIES
75 };
76
77 int http_module_number;
78
79 #if PHP_DEBUG
80 void _dpf(int type, const char *data, size_t length)
81 {
82 static const char _sym[] = "><><><";
83 if (type) {
84 int nwp = 0;
85 for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
86 int ip = PHP_HTTP_IS_CTYPE(print, *data);
87 if (!ip && *data != '\r' && *data != '\n') nwp = 1;
88 fprintf(stderr, ip?"%c":"\\x%02x", (int) (*data & 0xff));
89 if (!nwp && *data == '\n' && length) {
90 fprintf(stderr, "\n%c ", _sym[type-1]);
91 }
92 }
93 fprintf(stderr, "\n");
94 } else {
95 fprintf(stderr, "# %.*s\n", (int) length, data);
96 }
97 }
98 #endif
99
100 static void php_http_globals_init_once(zend_php_http_globals *G)
101 {
102 memset(G, 0, sizeof(*G));
103 }
104
105 static inline void php_http_globals_init(zend_php_http_globals *G TSRMLS_DC)
106 {
107 }
108
109 static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC)
110 {
111 }
112
113 #if defined(ZTS) && defined(PHP_DEBUG)
114 #if ZTS && PHP_DEBUG
115 zend_php_http_globals *php_http_globals(void)
116 {
117 TSRMLS_FETCH();
118 return PHP_HTTP_G;
119 }
120 #endif
121 #endif
122 PHP_INI_MH(http_update_persistent_handle_ident)
123 {
124 PHP_HTTP_G->persistent_handle.ident.h = zend_hash_func(new_value, PHP_HTTP_G->persistent_handle.ident.l = new_value_length+1);
125 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
126 }
127
128 PHP_INI_BEGIN()
129 PHP_HTTP_INI_ENTRY("http.etag.mode", "md5", PHP_INI_ALL, OnUpdateString, env.etag_mode)
130 PHP_HTTP_INI_ENTRY("http.request_datashare.cookie", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.cookie)
131 PHP_HTTP_INI_ENTRY("http.request_datashare.dns", "1", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.dns)
132 PHP_HTTP_INI_ENTRY("http.request_datashare.ssl", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.ssl)
133 PHP_HTTP_INI_ENTRY("http.request_datashare.connect", "0", PHP_INI_SYSTEM, OnUpdateBool, request_datashare.connect)
134 PHP_HTTP_INI_ENTRY("http.persistent_handle.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_handle.limit)
135 PHP_HTTP_INI_ENTRY("http.persistent_handle.ident", "GLOBAL", PHP_INI_ALL, http_update_persistent_handle_ident, persistent_handle.ident.s)
136 PHP_INI_END()
137
138 PHP_MINIT_FUNCTION(http)
139 {
140 http_module_number = module_number;
141 ZEND_INIT_MODULE_GLOBALS(php_http, php_http_globals_init_once, NULL);
142 REGISTER_INI_ENTRIES();
143
144 if (0
145 || SUCCESS != PHP_MINIT_CALL(http_fluently_callable)
146 || SUCCESS != PHP_MINIT_CALL(http_object)
147 || SUCCESS != PHP_MINIT_CALL(http_exception)
148 || SUCCESS != PHP_MINIT_CALL(http_persistent_handle)
149 || SUCCESS != PHP_MINIT_CALL(http_cookie)
150 || SUCCESS != PHP_MINIT_CALL(http_encoding)
151 || SUCCESS != PHP_MINIT_CALL(http_filter)
152 || SUCCESS != PHP_MINIT_CALL(http_message)
153 || SUCCESS != PHP_MINIT_CALL(http_message_body)
154 || SUCCESS != PHP_MINIT_CALL(http_property_proxy)
155 || SUCCESS != PHP_MINIT_CALL(http_querystring)
156 || SUCCESS != PHP_MINIT_CALL(http_request_factory)
157 || SUCCESS != PHP_MINIT_CALL(http_request)
158 || SUCCESS != PHP_MINIT_CALL(http_curl)
159 || SUCCESS != PHP_MINIT_CALL(http_neon)
160 || SUCCESS != PHP_MINIT_CALL(http_request_datashare)
161 || SUCCESS != PHP_MINIT_CALL(http_request_method)
162 || SUCCESS != PHP_MINIT_CALL(http_request_pool)
163 || SUCCESS != PHP_MINIT_CALL(http_url)
164 || SUCCESS != PHP_MINIT_CALL(http_env)
165 || SUCCESS != PHP_MINIT_CALL(http_env_response)
166 ) {
167 return FAILURE;
168 }
169
170 return SUCCESS;
171 }
172
173
174
175 PHP_MSHUTDOWN_FUNCTION(http)
176 {
177 UNREGISTER_INI_ENTRIES();
178
179 if (0
180 || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
181 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
182 || SUCCESS != PHP_MSHUTDOWN_CALL(http_neon)
183 || SUCCESS != PHP_MSHUTDOWN_CALL(http_request_datashare)
184 || SUCCESS != PHP_MSHUTDOWN_CALL(http_request_factory)
185 || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle)
186 ) {
187 return FAILURE;
188 }
189
190 return SUCCESS;
191 }
192
193 PHP_RINIT_FUNCTION(http)
194 {
195 if (0
196 || SUCCESS != PHP_RINIT_CALL(http_env)
197 || SUCCESS != PHP_RINIT_CALL(http_request_datashare)
198 || SUCCESS != PHP_RINIT_CALL(http_curl)
199 ) {
200 return FAILURE;
201 }
202
203 return SUCCESS;
204 }
205
206 PHP_RSHUTDOWN_FUNCTION(http)
207 {
208 if (0
209 || SUCCESS != PHP_RSHUTDOWN_CALL(http_env)
210 || SUCCESS != PHP_RSHUTDOWN_CALL(http_request_datashare)
211 ) {
212 return FAILURE;
213 }
214
215 return SUCCESS;
216 }
217
218 PHP_MINFO_FUNCTION(http)
219 {
220 php_info_print_table_start();
221 php_info_print_table_header(2, "HTTP Support", "enabled");
222 php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
223 php_info_print_table_end();
224
225 php_info_print_table_start();
226 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
227 php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
228 #ifdef PHP_HTTP_HAVE_CURL
229 {
230 curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
231 php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
232 }
233 #else
234 php_info_print_table_row(3, "libcurl", "disabled", "disabled");
235 #endif
236 #ifdef PHP_HTTP_HAVE_NEON
237 {
238 char ne_v[16] = {0};
239 sscanf(ne_version_string(), "neon %15[^ :]", &ne_v[0]);
240 php_info_print_table_row(3, "libneon", PHP_HTTP_NEON_VERSION, ne_v);
241 }
242 #else
243 php_info_print_table_row(3, "libneon", "disabled", "disabled");
244 #endif
245
246 #ifdef PHP_HTTP_HAVE_EVENT
247 php_info_print_table_row(3, "libevent", PHP_HTTP_EVENT_VERSION, event_get_version());
248 #else
249 php_info_print_table_row(3, "libevent", "disabled", "disabled");
250 #endif
251 php_info_print_table_end();
252
253 php_info_print_table_start();
254 php_info_print_table_colspan_header(4, "Persistent Handles");
255 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
256 {
257 HashTable *ht;
258 HashPosition pos1, pos2;
259 php_http_array_hashkey_t provider = php_http_array_hashkey_init(0), ident = php_http_array_hashkey_init(0);
260 zval **val, **sub, **zused, **zfree;
261
262 if ((ht = php_http_persistent_handle_statall(NULL TSRMLS_CC)) && zend_hash_num_elements(ht)) {
263 FOREACH_HASH_KEYVAL(pos1, ht, provider, val) {
264 if (zend_hash_num_elements(Z_ARRVAL_PP(val))) {
265 FOREACH_KEYVAL(pos2, *val, ident, sub) {
266 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("used"), (void *) &zused) &&
267 SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("free"), (void *) &zfree)) {
268 zval *used = php_http_ztyp(IS_STRING, *zused);
269 zval *free = php_http_ztyp(IS_STRING, *zfree);
270 php_info_print_table_row(4, provider.str, ident.str, Z_STRVAL_P(used), Z_STRVAL_P(free));
271 zval_ptr_dtor(&used);
272 zval_ptr_dtor(&free);
273 } else {
274 php_info_print_table_row(4, provider.str, ident.str, "0", "0");
275 }
276 }
277 } else {
278 php_info_print_table_row(4, provider.str, "N/A", "0", "0");
279 }
280 }
281 } else {
282 php_info_print_table_row(4, "N/A", "N/A", "0", "0");
283 }
284 if (ht) {
285 zend_hash_destroy(ht);
286 FREE_HASHTABLE(ht);
287 }
288 }
289 php_info_print_table_end();
290
291 DISPLAY_INI_ENTRIES();
292 }
293
294
295 /*
296 * Local variables:
297 * tab-width: 4
298 * c-basic-offset: 4
299 * End:
300 * vim600: noet sw=4 ts=4 fdm=marker
301 * vim<600: noet sw=4 ts=4
302 */
303