fix build with old curl version
[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_JSON
55 ZEND_MOD_REQUIRED("json")
56 #endif
57 #ifdef PHP_HTTP_HAVE_EVENT
58 ZEND_MOD_CONFLICTS("event")
59 #endif
60 {NULL, NULL, NULL, 0}
61 };
62
63 zend_module_entry http_module_entry = {
64 STANDARD_MODULE_HEADER_EX,
65 NULL,
66 http_module_deps,
67 "http",
68 http_functions,
69 PHP_MINIT(http),
70 PHP_MSHUTDOWN(http),
71 PHP_RINIT(http),
72 PHP_RSHUTDOWN(http),
73 PHP_MINFO(http),
74 PHP_HTTP_EXT_VERSION,
75 STANDARD_MODULE_PROPERTIES
76 };
77
78 int http_module_number;
79
80 static HashTable http_module_classes;
81 void php_http_register_class(zend_class_entry *(*get_ce)(void))
82 {
83 zend_hash_next_index_insert(&http_module_classes, &get_ce, sizeof(get_ce), NULL);
84 }
85 static void php_http_registered_classes(php_http_buffer_t *buf, unsigned flags)
86 {
87 HashPosition pos;
88 zend_class_entry *(**get_ce)(void);
89
90 FOREACH_HASH_VAL(pos, &http_module_classes, get_ce) {
91 zend_class_entry *ce = (*get_ce)();
92 if ((flags && (ce->ce_flags & flags)) || (!flags && !(ce->ce_flags & 0x0fff))) {
93 if (buf->used) {
94 php_http_buffer_appends(buf, ", ");
95 }
96 php_http_buffer_append(buf, ce->name, ce->name_length);
97 }
98 }
99 php_http_buffer_fix(buf);
100 }
101
102 #if PHP_DEBUG && !HAVE_GCOV
103 void _dpf(int type, const char *data, size_t length)
104 {
105 static const char _sym[] = "><><><";
106 if (type) {
107 int nwp = 0;
108 for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
109 int ip = PHP_HTTP_IS_CTYPE(print, *data);
110 if (!ip && *data != '\r' && *data != '\n') nwp = 1;
111 fprintf(stderr, ip?"%c":"\\x%02x", (int) (*data & 0xff));
112 if (!nwp && *data == '\n' && length) {
113 fprintf(stderr, "\n%c ", _sym[type-1]);
114 }
115 }
116 fprintf(stderr, "\n");
117 } else {
118 fprintf(stderr, "# %.*s\n", (int) length, data);
119 }
120 }
121 #endif
122
123 static void php_http_globals_init_once(zend_php_http_globals *G)
124 {
125 memset(G, 0, sizeof(*G));
126 }
127
128 #if 0
129 static inline void php_http_globals_init(zend_php_http_globals *G TSRMLS_DC)
130 {
131 }
132
133 static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC)
134 {
135 }
136 #endif
137
138 #if ZTS && PHP_DEBUG && !HAVE_GCOV
139 zend_php_http_globals *php_http_globals(void)
140 {
141 TSRMLS_FETCH();
142 return PHP_HTTP_G;
143 }
144 #endif
145
146 PHP_INI_BEGIN()
147 PHP_HTTP_INI_ENTRY("http.etag.mode", "crc32b", PHP_INI_ALL, OnUpdateString, env.etag_mode)
148 PHP_HTTP_INI_ENTRY("http.persistent_handle.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_handle.limit)
149 PHP_INI_END()
150
151 PHP_MINIT_FUNCTION(http)
152 {
153 http_module_number = module_number;
154 ZEND_INIT_MODULE_GLOBALS(php_http, php_http_globals_init_once, NULL);
155 REGISTER_INI_ENTRIES();
156
157 zend_hash_init(&http_module_classes, 0, NULL, NULL, 1);
158
159 if (0
160 || SUCCESS != PHP_MINIT_CALL(http_object)
161 || SUCCESS != PHP_MINIT_CALL(http_exception)
162 || SUCCESS != PHP_MINIT_CALL(http_persistent_handle)
163 || SUCCESS != PHP_MINIT_CALL(http_cookie)
164 || SUCCESS != PHP_MINIT_CALL(http_encoding)
165 || SUCCESS != PHP_MINIT_CALL(http_filter)
166 || SUCCESS != PHP_MINIT_CALL(http_header)
167 || SUCCESS != PHP_MINIT_CALL(http_message)
168 || SUCCESS != PHP_MINIT_CALL(http_message_body)
169 || SUCCESS != PHP_MINIT_CALL(http_property_proxy)
170 || SUCCESS != PHP_MINIT_CALL(http_querystring)
171 || SUCCESS != PHP_MINIT_CALL(http_client_interface)
172 || SUCCESS != PHP_MINIT_CALL(http_client)
173 || SUCCESS != PHP_MINIT_CALL(http_client_request)
174 || SUCCESS != PHP_MINIT_CALL(http_client_response)
175 || SUCCESS != PHP_MINIT_CALL(http_client_datashare)
176 || SUCCESS != PHP_MINIT_CALL(http_client_pool)
177 || SUCCESS != PHP_MINIT_CALL(http_client_factory)
178 #if PHP_HTTP_HAVE_CURL
179 || SUCCESS != PHP_MINIT_CALL(http_curl)
180 || SUCCESS != PHP_MINIT_CALL(http_curl_client)
181 || SUCCESS != PHP_MINIT_CALL(http_curl_client_pool)
182 || SUCCESS != PHP_MINIT_CALL(http_curl_client_datashare)
183 #endif
184 || SUCCESS != PHP_MINIT_CALL(http_url)
185 || SUCCESS != PHP_MINIT_CALL(http_env)
186 || SUCCESS != PHP_MINIT_CALL(http_env_request)
187 || SUCCESS != PHP_MINIT_CALL(http_env_response)
188 || SUCCESS != PHP_MINIT_CALL(http_params)
189 ) {
190 return FAILURE;
191 }
192
193 return SUCCESS;
194 }
195
196
197
198 PHP_MSHUTDOWN_FUNCTION(http)
199 {
200 UNREGISTER_INI_ENTRIES();
201
202 if (0
203 || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
204 #if PHP_HTTP_HAVE_CURL
205 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl_client)
206 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
207 #endif
208 || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle)
209 || SUCCESS != PHP_MSHUTDOWN_CALL(http_client_factory)
210 ) {
211 return FAILURE;
212 }
213
214 zend_hash_destroy(&http_module_classes);
215
216 return SUCCESS;
217 }
218
219 PHP_RINIT_FUNCTION(http)
220 {
221 if (0
222 || SUCCESS != PHP_RINIT_CALL(http_env)
223 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
224 || SUCCESS != PHP_RINIT_CALL(http_curl_client_pool)
225 #endif
226 ) {
227 return FAILURE;
228 }
229
230 return SUCCESS;
231 }
232
233 PHP_RSHUTDOWN_FUNCTION(http)
234 {
235 if (0
236 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
237 || SUCCESS != PHP_RSHUTDOWN_CALL(http_curl_client_pool)
238 #endif
239 || SUCCESS != PHP_RSHUTDOWN_CALL(http_env)
240 ) {
241 return FAILURE;
242 }
243
244 return SUCCESS;
245 }
246
247 PHP_MINFO_FUNCTION(http)
248 {
249 unsigned i;
250 php_http_buffer_t buf;
251
252 php_http_buffer_init(&buf);
253
254 php_info_print_table_start();
255 php_info_print_table_header(2, "HTTP Support", "enabled");
256 php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
257 php_info_print_table_end();
258
259 php_info_print_table_start();
260 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
261 php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
262 #if PHP_HTTP_HAVE_CURL
263 {
264 curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
265 php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
266 }
267 #else
268 php_info_print_table_row(3, "libcurl", "disabled", "disabled");
269 #endif
270
271 #if PHP_HTTP_HAVE_EVENT
272 php_info_print_table_row(3, "libevent",
273 # ifdef LIBEVENT_VERSION
274 LIBEVENT_VERSION,
275 # else
276 PHP_HTTP_EVENT_VERSION,
277 # endif
278 event_get_version());
279 #else
280 php_info_print_table_row(3, "libevent", "disabled", "disabled");
281 #endif
282
283 #if PHP_HTTP_HAVE_SERF
284 {
285 int v[3];
286
287 serf_lib_version(&v[0], &v[1], &v[2]);
288 php_http_buffer_appendf(&buf, "%d.%d.%d", v[0], v[1], v[2]);
289 php_http_buffer_fix(&buf);
290 php_info_print_table_row(3, "libserf", SERF_VERSION_STRING, buf.data);
291 php_http_buffer_reset(&buf);
292 }
293 #else
294 php_info_print_table_row(3, "libserf", "disabled", "disabled");
295 #endif
296 php_info_print_table_end();
297
298 php_info_print_table_start();
299 php_info_print_table_colspan_header(2, "Registered API");
300 for (i = 0; http_functions[i].fname; ++i) {
301 if (buf.used) {
302 php_http_buffer_appends(&buf, ", ");
303 }
304 php_http_buffer_appendl(&buf, http_functions[i].fname);
305 }
306 php_http_buffer_fix(&buf);
307 php_info_print_table_row(2, "Functions", buf.data);
308 php_http_buffer_reset(&buf);
309 php_http_registered_classes(&buf, ZEND_ACC_INTERFACE);
310 php_info_print_table_row(2, "Interfaces", buf.data);
311 php_http_buffer_reset(&buf);
312 php_http_registered_classes(&buf, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
313 php_info_print_table_row(2, "Abstract Classes", buf.data);
314 php_http_buffer_reset(&buf);
315 php_http_registered_classes(&buf, 0);
316 php_info_print_table_row(2, "Implemented Classes", buf.data);
317 php_http_buffer_reset(&buf);
318 php_http_registered_classes(&buf, ZEND_ACC_FINAL_CLASS);
319 php_info_print_table_row(2, "Final Classes", buf.data);
320 php_http_buffer_reset(&buf);
321
322 php_info_print_table_row(2, "Stream Filters", "http.chunked_encode, http.chunked_decode, http.inflate, http.deflate");
323 php_info_print_table_end();
324
325 php_info_print_table_start();
326 php_info_print_table_colspan_header(4, "Persistent Handles");
327 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
328 {
329 HashTable *ht;
330 HashPosition pos1, pos2;
331 php_http_array_hashkey_t provider = php_http_array_hashkey_init(0), ident = php_http_array_hashkey_init(0);
332 zval **val, **sub, **zused, **zfree;
333
334 if ((ht = php_http_persistent_handle_statall(NULL TSRMLS_CC)) && zend_hash_num_elements(ht)) {
335 FOREACH_HASH_KEYVAL(pos1, ht, provider, val) {
336 if (zend_hash_num_elements(Z_ARRVAL_PP(val))) {
337 FOREACH_KEYVAL(pos2, *val, ident, sub) {
338 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("used"), (void *) &zused) &&
339 SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("free"), (void *) &zfree)) {
340 zval *used = php_http_ztyp(IS_STRING, *zused);
341 zval *free = php_http_ztyp(IS_STRING, *zfree);
342 php_info_print_table_row(4, provider.str, ident.str, Z_STRVAL_P(used), Z_STRVAL_P(free));
343 zval_ptr_dtor(&used);
344 zval_ptr_dtor(&free);
345 } else {
346 php_info_print_table_row(4, provider.str, ident.str, "0", "0");
347 }
348 }
349 } else {
350 php_info_print_table_row(4, provider.str, "N/A", "0", "0");
351 }
352 }
353 } else {
354 php_info_print_table_row(4, "N/A", "N/A", "0", "0");
355 }
356 if (ht) {
357 zend_hash_destroy(ht);
358 FREE_HASHTABLE(ht);
359 }
360 }
361 php_info_print_table_end();
362
363 php_http_buffer_dtor(&buf);
364
365 DISPLAY_INI_ENTRIES();
366 }
367
368
369 /*
370 * Local variables:
371 * tab-width: 4
372 * c-basic-offset: 4
373 * End:
374 * vim600: noet sw=4 ts=4 fdm=marker
375 * vim<600: noet sw=4 ts=4
376 */
377