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