add json content type handler if ext/json is present
[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 #if PHP_DEBUG
81 void _dpf(int type, const char *data, size_t length)
82 {
83 static const char _sym[] = "><><><";
84 if (type) {
85 int nwp = 0;
86 for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
87 int ip = PHP_HTTP_IS_CTYPE(print, *data);
88 if (!ip && *data != '\r' && *data != '\n') nwp = 1;
89 fprintf(stderr, ip?"%c":"\\x%02x", (int) (*data & 0xff));
90 if (!nwp && *data == '\n' && length) {
91 fprintf(stderr, "\n%c ", _sym[type-1]);
92 }
93 }
94 fprintf(stderr, "\n");
95 } else {
96 fprintf(stderr, "# %.*s\n", (int) length, data);
97 }
98 }
99 #endif
100
101 static void php_http_globals_init_once(zend_php_http_globals *G)
102 {
103 memset(G, 0, sizeof(*G));
104 }
105
106 static inline void php_http_globals_init(zend_php_http_globals *G TSRMLS_DC)
107 {
108 }
109
110 static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC)
111 {
112 }
113
114 #if defined(ZTS) && defined(PHP_DEBUG)
115 #if ZTS && PHP_DEBUG
116 zend_php_http_globals *php_http_globals(void)
117 {
118 TSRMLS_FETCH();
119 return PHP_HTTP_G;
120 }
121 #endif
122 #endif
123 PHP_INI_BEGIN()
124 PHP_HTTP_INI_ENTRY("http.etag.mode", "crc32b", PHP_INI_ALL, OnUpdateString, env.etag_mode)
125 PHP_HTTP_INI_ENTRY("http.persistent_handle.limit", "-1", PHP_INI_SYSTEM, OnUpdateLong, persistent_handle.limit)
126 PHP_INI_END()
127
128 PHP_MINIT_FUNCTION(http)
129 {
130 http_module_number = module_number;
131 ZEND_INIT_MODULE_GLOBALS(php_http, php_http_globals_init_once, NULL);
132 REGISTER_INI_ENTRIES();
133
134 if (0
135 || SUCCESS != PHP_MINIT_CALL(http_object)
136 || SUCCESS != PHP_MINIT_CALL(http_exception)
137 || SUCCESS != PHP_MINIT_CALL(http_persistent_handle)
138 || SUCCESS != PHP_MINIT_CALL(http_cookie)
139 || SUCCESS != PHP_MINIT_CALL(http_encoding)
140 || SUCCESS != PHP_MINIT_CALL(http_filter)
141 || SUCCESS != PHP_MINIT_CALL(http_header)
142 || SUCCESS != PHP_MINIT_CALL(http_message)
143 || SUCCESS != PHP_MINIT_CALL(http_message_body)
144 || SUCCESS != PHP_MINIT_CALL(http_property_proxy)
145 || SUCCESS != PHP_MINIT_CALL(http_querystring)
146 || SUCCESS != PHP_MINIT_CALL(http_client_interface)
147 || SUCCESS != PHP_MINIT_CALL(http_client)
148 || SUCCESS != PHP_MINIT_CALL(http_client_request)
149 || SUCCESS != PHP_MINIT_CALL(http_client_response)
150 || SUCCESS != PHP_MINIT_CALL(http_client_datashare)
151 || SUCCESS != PHP_MINIT_CALL(http_client_pool)
152 || SUCCESS != PHP_MINIT_CALL(http_client_factory)
153 #if PHP_HTTP_HAVE_CURL
154 || SUCCESS != PHP_MINIT_CALL(http_curl)
155 #endif
156 || SUCCESS != PHP_MINIT_CALL(http_curl_client)
157 || SUCCESS != PHP_MINIT_CALL(http_curl_client_pool)
158 || SUCCESS != PHP_MINIT_CALL(http_curl_client_datashare)
159 || SUCCESS != PHP_MINIT_CALL(http_url)
160 || SUCCESS != PHP_MINIT_CALL(http_env)
161 || SUCCESS != PHP_MINIT_CALL(http_env_request)
162 || SUCCESS != PHP_MINIT_CALL(http_env_response)
163 || SUCCESS != PHP_MINIT_CALL(http_params)
164 ) {
165 return FAILURE;
166 }
167
168 return SUCCESS;
169 }
170
171
172
173 PHP_MSHUTDOWN_FUNCTION(http)
174 {
175 UNREGISTER_INI_ENTRIES();
176
177 if (0
178 || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
179 #if PHP_HTTP_HAVE_CURL
180 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
181 #endif
182 || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle)
183 || SUCCESS != PHP_MSHUTDOWN_CALL(http_client_factory)
184 ) {
185 return FAILURE;
186 }
187
188 return SUCCESS;
189 }
190
191 PHP_RINIT_FUNCTION(http)
192 {
193 if (0
194 || SUCCESS != PHP_RINIT_CALL(http_env)
195 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
196 || SUCCESS != PHP_RINIT_CALL(http_curl_client_pool)
197 #endif
198 ) {
199 return FAILURE;
200 }
201
202 return SUCCESS;
203 }
204
205 PHP_RSHUTDOWN_FUNCTION(http)
206 {
207 if (0
208 || SUCCESS != PHP_RSHUTDOWN_CALL(http_env)
209 ) {
210 return FAILURE;
211 }
212
213 return SUCCESS;
214 }
215
216 PHP_MINFO_FUNCTION(http)
217 {
218 php_info_print_table_start();
219 php_info_print_table_header(2, "HTTP Support", "enabled");
220 php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
221 php_info_print_table_end();
222
223 php_info_print_table_start();
224 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
225 php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
226 #if PHP_HTTP_HAVE_CURL
227 {
228 curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
229 php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
230 }
231 #else
232 php_info_print_table_row(3, "libcurl", "disabled", "disabled");
233 #endif
234
235 #if PHP_HTTP_HAVE_EVENT
236 php_info_print_table_row(3, "libevent", PHP_HTTP_EVENT_VERSION, event_get_version());
237 #else
238 php_info_print_table_row(3, "libevent", "disabled", "disabled");
239 #endif
240
241 #if PHP_HTTP_HAVE_SERF
242 {
243 int v[3];
244 char sl_v[16] = {0};
245
246 serf_lib_version(&v[0], &v[1], &v[2]);
247 slprintf(sl_v, lenof(sl_v), "%d.%d.%d", v[0], v[1], v[2]);
248 php_info_print_table_row(3, "libserf", SERF_VERSION_STRING, sl_v);
249 }
250 #else
251 php_info_print_table_row(3, "libserf", "disabled", "disabled");
252 #endif
253 php_info_print_table_end();
254
255 php_info_print_table_start();
256 php_info_print_table_colspan_header(4, "Persistent Handles");
257 php_info_print_table_header(4, "Provider", "Ident", "Used", "Free");
258 {
259 HashTable *ht;
260 HashPosition pos1, pos2;
261 php_http_array_hashkey_t provider = php_http_array_hashkey_init(0), ident = php_http_array_hashkey_init(0);
262 zval **val, **sub, **zused, **zfree;
263
264 if ((ht = php_http_persistent_handle_statall(NULL TSRMLS_CC)) && zend_hash_num_elements(ht)) {
265 FOREACH_HASH_KEYVAL(pos1, ht, provider, val) {
266 if (zend_hash_num_elements(Z_ARRVAL_PP(val))) {
267 FOREACH_KEYVAL(pos2, *val, ident, sub) {
268 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("used"), (void *) &zused) &&
269 SUCCESS == zend_hash_find(Z_ARRVAL_PP(sub), ZEND_STRS("free"), (void *) &zfree)) {
270 zval *used = php_http_ztyp(IS_STRING, *zused);
271 zval *free = php_http_ztyp(IS_STRING, *zfree);
272 php_info_print_table_row(4, provider.str, ident.str, Z_STRVAL_P(used), Z_STRVAL_P(free));
273 zval_ptr_dtor(&used);
274 zval_ptr_dtor(&free);
275 } else {
276 php_info_print_table_row(4, provider.str, ident.str, "0", "0");
277 }
278 }
279 } else {
280 php_info_print_table_row(4, provider.str, "N/A", "0", "0");
281 }
282 }
283 } else {
284 php_info_print_table_row(4, "N/A", "N/A", "0", "0");
285 }
286 if (ht) {
287 zend_hash_destroy(ht);
288 FREE_HASHTABLE(ht);
289 }
290 }
291 php_info_print_table_end();
292
293 DISPLAY_INI_ENTRIES();
294 }
295
296
297 /*
298 * Local variables:
299 * tab-width: 4
300 * c-basic-offset: 4
301 * End:
302 * vim600: noet sw=4 ts=4 fdm=marker
303 * vim<600: noet sw=4 ts=4
304 */
305