split off raphf
[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("raphf")
48 ZEND_MOD_REQUIRED("spl")
49 #ifdef PHP_HTTP_HAVE_HASH
50 ZEND_MOD_REQUIRED("hash")
51 #endif
52 #ifdef PHP_HTTP_HAVE_ICONV
53 ZEND_MOD_REQUIRED("iconv")
54 #endif
55 #ifdef PHP_HTTP_HAVE_JSON
56 ZEND_MOD_REQUIRED("json")
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 static HashTable http_module_classes;
82 void php_http_register_class(zend_class_entry *(*get_ce)(void))
83 {
84 zend_hash_next_index_insert(&http_module_classes, &get_ce, sizeof(get_ce), NULL);
85 }
86 static void php_http_registered_classes(php_http_buffer_t *buf, unsigned flags)
87 {
88 HashPosition pos;
89 zend_class_entry *(**get_ce)(void);
90
91 FOREACH_HASH_VAL(pos, &http_module_classes, get_ce) {
92 zend_class_entry *ce = (*get_ce)();
93 if ((flags && (ce->ce_flags & flags)) || (!flags && !(ce->ce_flags & 0x0fff))) {
94 if (buf->used) {
95 php_http_buffer_appends(buf, ", ");
96 }
97 php_http_buffer_append(buf, ce->name, ce->name_length);
98 }
99 }
100 php_http_buffer_fix(buf);
101 }
102
103 #if PHP_DEBUG && !HAVE_GCOV
104 void _dpf(int type, const char *data, size_t length)
105 {
106 static const char _sym[] = "><><><";
107 if (type) {
108 int nwp = 0;
109 for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
110 int ip = PHP_HTTP_IS_CTYPE(print, *data);
111 if (!ip && *data != '\r' && *data != '\n') nwp = 1;
112 fprintf(stderr, ip?"%c":"\\x%02x", (int) (*data & 0xff));
113 if (!nwp && *data == '\n' && length) {
114 fprintf(stderr, "\n%c ", _sym[type-1]);
115 }
116 }
117 fprintf(stderr, "\n");
118 } else {
119 fprintf(stderr, "# %.*s\n", (int) length, data);
120 }
121 }
122 #endif
123
124 static void php_http_globals_init_once(zend_php_http_globals *G)
125 {
126 memset(G, 0, sizeof(*G));
127 }
128
129 #if 0
130 static inline void php_http_globals_init(zend_php_http_globals *G TSRMLS_DC)
131 {
132 }
133
134 static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC)
135 {
136 }
137 #endif
138
139 #if ZTS && PHP_DEBUG && !HAVE_GCOV
140 zend_php_http_globals *php_http_globals(void)
141 {
142 TSRMLS_FETCH();
143 return PHP_HTTP_G;
144 }
145 #endif
146
147 PHP_INI_BEGIN()
148 PHP_HTTP_INI_ENTRY("http.etag.mode", "crc32b", PHP_INI_ALL, OnUpdateString, env.etag_mode)
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_cookie)
163 || SUCCESS != PHP_MINIT_CALL(http_encoding)
164 || SUCCESS != PHP_MINIT_CALL(http_filter)
165 || SUCCESS != PHP_MINIT_CALL(http_header)
166 || SUCCESS != PHP_MINIT_CALL(http_message)
167 || SUCCESS != PHP_MINIT_CALL(http_message_body)
168 || SUCCESS != PHP_MINIT_CALL(http_property_proxy)
169 || SUCCESS != PHP_MINIT_CALL(http_querystring)
170 || SUCCESS != PHP_MINIT_CALL(http_client_interface)
171 || SUCCESS != PHP_MINIT_CALL(http_client)
172 || SUCCESS != PHP_MINIT_CALL(http_client_request)
173 || SUCCESS != PHP_MINIT_CALL(http_client_response)
174 || SUCCESS != PHP_MINIT_CALL(http_client_datashare)
175 || SUCCESS != PHP_MINIT_CALL(http_client_pool)
176 || SUCCESS != PHP_MINIT_CALL(http_client_factory)
177 #if PHP_HTTP_HAVE_CURL
178 || SUCCESS != PHP_MINIT_CALL(http_curl)
179 || SUCCESS != PHP_MINIT_CALL(http_curl_client)
180 || SUCCESS != PHP_MINIT_CALL(http_curl_client_pool)
181 || SUCCESS != PHP_MINIT_CALL(http_curl_client_datashare)
182 #endif
183 || SUCCESS != PHP_MINIT_CALL(http_url)
184 || SUCCESS != PHP_MINIT_CALL(http_env)
185 || SUCCESS != PHP_MINIT_CALL(http_env_request)
186 || SUCCESS != PHP_MINIT_CALL(http_env_response)
187 || SUCCESS != PHP_MINIT_CALL(http_params)
188 ) {
189 return FAILURE;
190 }
191
192 return SUCCESS;
193 }
194
195
196
197 PHP_MSHUTDOWN_FUNCTION(http)
198 {
199 UNREGISTER_INI_ENTRIES();
200
201 if (0
202 || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
203 #if PHP_HTTP_HAVE_CURL
204 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl_client)
205 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
206 #endif
207 || SUCCESS != PHP_MSHUTDOWN_CALL(http_client_factory)
208 ) {
209 return FAILURE;
210 }
211
212 zend_hash_destroy(&http_module_classes);
213
214 return SUCCESS;
215 }
216
217 PHP_RINIT_FUNCTION(http)
218 {
219 if (0
220 || SUCCESS != PHP_RINIT_CALL(http_env)
221 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
222 || SUCCESS != PHP_RINIT_CALL(http_curl_client_pool)
223 #endif
224 ) {
225 return FAILURE;
226 }
227
228 return SUCCESS;
229 }
230
231 PHP_RSHUTDOWN_FUNCTION(http)
232 {
233 if (0
234 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
235 || SUCCESS != PHP_RSHUTDOWN_CALL(http_curl_client_pool)
236 #endif
237 || SUCCESS != PHP_RSHUTDOWN_CALL(http_env)
238 ) {
239 return FAILURE;
240 }
241
242 return SUCCESS;
243 }
244
245 PHP_MINFO_FUNCTION(http)
246 {
247 unsigned i;
248 php_http_buffer_t buf;
249
250 php_http_buffer_init(&buf);
251
252 php_info_print_table_start();
253 php_info_print_table_header(2, "HTTP Support", "enabled");
254 php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
255 php_info_print_table_end();
256
257 php_info_print_table_start();
258 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
259 php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
260 #if PHP_HTTP_HAVE_CURL
261 {
262 curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
263 php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
264 }
265 #else
266 php_info_print_table_row(3, "libcurl", "disabled", "disabled");
267 #endif
268
269 #if PHP_HTTP_HAVE_EVENT
270 php_info_print_table_row(3, "libevent",
271 # ifdef LIBEVENT_VERSION
272 LIBEVENT_VERSION,
273 # else
274 PHP_HTTP_EVENT_VERSION,
275 # endif
276 event_get_version());
277 #else
278 php_info_print_table_row(3, "libevent", "disabled", "disabled");
279 #endif
280
281 #if PHP_HTTP_HAVE_SERF
282 {
283 int v[3];
284
285 serf_lib_version(&v[0], &v[1], &v[2]);
286 php_http_buffer_appendf(&buf, "%d.%d.%d", v[0], v[1], v[2]);
287 php_http_buffer_fix(&buf);
288 php_info_print_table_row(3, "libserf", SERF_VERSION_STRING, buf.data);
289 php_http_buffer_reset(&buf);
290 }
291 #else
292 php_info_print_table_row(3, "libserf", "disabled", "disabled");
293 #endif
294 php_info_print_table_end();
295
296 php_info_print_table_start();
297 php_info_print_table_colspan_header(2, "Registered API");
298 for (i = 0; http_functions[i].fname; ++i) {
299 if (buf.used) {
300 php_http_buffer_appends(&buf, ", ");
301 }
302 php_http_buffer_appendl(&buf, http_functions[i].fname);
303 }
304 php_http_buffer_fix(&buf);
305 php_info_print_table_row(2, "Functions", buf.data);
306 php_http_buffer_reset(&buf);
307 php_http_registered_classes(&buf, ZEND_ACC_INTERFACE);
308 php_info_print_table_row(2, "Interfaces", buf.data);
309 php_http_buffer_reset(&buf);
310 php_http_registered_classes(&buf, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
311 php_info_print_table_row(2, "Abstract Classes", buf.data);
312 php_http_buffer_reset(&buf);
313 php_http_registered_classes(&buf, 0);
314 php_info_print_table_row(2, "Implemented Classes", buf.data);
315 php_http_buffer_reset(&buf);
316 php_http_registered_classes(&buf, ZEND_ACC_FINAL_CLASS);
317 php_info_print_table_row(2, "Final Classes", buf.data);
318 php_http_buffer_dtor(&buf);
319
320 php_info_print_table_row(2, "Stream Filters", "http.chunked_encode, http.chunked_decode, http.inflate, http.deflate");
321 php_info_print_table_end();
322
323
324 DISPLAY_INI_ENTRIES();
325 }
326
327
328 /*
329 * Local variables:
330 * tab-width: 4
331 * c-basic-offset: 4
332 * End:
333 * vim600: noet sw=4 ts=4 fdm=marker
334 * vim<600: noet sw=4 ts=4
335 */
336