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