split off property proxy
[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_querystring)
169 || SUCCESS != PHP_MINIT_CALL(http_client_interface)
170 || SUCCESS != PHP_MINIT_CALL(http_client)
171 || SUCCESS != PHP_MINIT_CALL(http_client_request)
172 || SUCCESS != PHP_MINIT_CALL(http_client_response)
173 || SUCCESS != PHP_MINIT_CALL(http_client_datashare)
174 || SUCCESS != PHP_MINIT_CALL(http_client_pool)
175 || SUCCESS != PHP_MINIT_CALL(http_client_factory)
176 #if PHP_HTTP_HAVE_CURL
177 || SUCCESS != PHP_MINIT_CALL(http_curl)
178 || SUCCESS != PHP_MINIT_CALL(http_curl_client)
179 || SUCCESS != PHP_MINIT_CALL(http_curl_client_pool)
180 || SUCCESS != PHP_MINIT_CALL(http_curl_client_datashare)
181 #endif
182 || SUCCESS != PHP_MINIT_CALL(http_url)
183 || SUCCESS != PHP_MINIT_CALL(http_env)
184 || SUCCESS != PHP_MINIT_CALL(http_env_request)
185 || SUCCESS != PHP_MINIT_CALL(http_env_response)
186 || SUCCESS != PHP_MINIT_CALL(http_params)
187 ) {
188 return FAILURE;
189 }
190
191 return SUCCESS;
192 }
193
194
195
196 PHP_MSHUTDOWN_FUNCTION(http)
197 {
198 UNREGISTER_INI_ENTRIES();
199
200 if (0
201 || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
202 #if PHP_HTTP_HAVE_CURL
203 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl_client)
204 || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
205 #endif
206 || SUCCESS != PHP_MSHUTDOWN_CALL(http_client_factory)
207 ) {
208 return FAILURE;
209 }
210
211 zend_hash_destroy(&http_module_classes);
212
213 return SUCCESS;
214 }
215
216 PHP_RINIT_FUNCTION(http)
217 {
218 if (0
219 || SUCCESS != PHP_RINIT_CALL(http_env)
220 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
221 || SUCCESS != PHP_RINIT_CALL(http_curl_client_pool)
222 #endif
223 ) {
224 return FAILURE;
225 }
226
227 return SUCCESS;
228 }
229
230 PHP_RSHUTDOWN_FUNCTION(http)
231 {
232 if (0
233 #if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT
234 || SUCCESS != PHP_RSHUTDOWN_CALL(http_curl_client_pool)
235 #endif
236 || SUCCESS != PHP_RSHUTDOWN_CALL(http_env)
237 ) {
238 return FAILURE;
239 }
240
241 return SUCCESS;
242 }
243
244 PHP_MINFO_FUNCTION(http)
245 {
246 unsigned i;
247 php_http_buffer_t buf;
248
249 php_http_buffer_init(&buf);
250
251 php_info_print_table_start();
252 php_info_print_table_header(2, "HTTP Support", "enabled");
253 php_info_print_table_row(2, "Extension Version", PHP_HTTP_EXT_VERSION);
254 php_info_print_table_end();
255
256 php_info_print_table_start();
257 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
258 php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
259 #if PHP_HTTP_HAVE_CURL
260 {
261 curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
262 php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
263 }
264 #else
265 php_info_print_table_row(3, "libcurl", "disabled", "disabled");
266 #endif
267
268 #if PHP_HTTP_HAVE_EVENT
269 php_info_print_table_row(3, "libevent",
270 # ifdef LIBEVENT_VERSION
271 LIBEVENT_VERSION,
272 # else
273 PHP_HTTP_EVENT_VERSION,
274 # endif
275 event_get_version());
276 #else
277 php_info_print_table_row(3, "libevent", "disabled", "disabled");
278 #endif
279
280 #if PHP_HTTP_HAVE_SERF
281 {
282 int v[3];
283
284 serf_lib_version(&v[0], &v[1], &v[2]);
285 php_http_buffer_appendf(&buf, "%d.%d.%d", v[0], v[1], v[2]);
286 php_http_buffer_fix(&buf);
287 php_info_print_table_row(3, "libserf", SERF_VERSION_STRING, buf.data);
288 php_http_buffer_reset(&buf);
289 }
290 #else
291 php_info_print_table_row(3, "libserf", "disabled", "disabled");
292 #endif
293 php_info_print_table_end();
294
295 php_info_print_table_start();
296 php_info_print_table_colspan_header(2, "Registered API");
297 for (i = 0; http_functions[i].fname; ++i) {
298 if (buf.used) {
299 php_http_buffer_appends(&buf, ", ");
300 }
301 php_http_buffer_appendl(&buf, http_functions[i].fname);
302 }
303 php_http_buffer_fix(&buf);
304 php_info_print_table_row(2, "Functions", buf.data);
305 php_http_buffer_reset(&buf);
306 php_http_registered_classes(&buf, ZEND_ACC_INTERFACE);
307 php_info_print_table_row(2, "Interfaces", buf.data);
308 php_http_buffer_reset(&buf);
309 php_http_registered_classes(&buf, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
310 php_info_print_table_row(2, "Abstract Classes", buf.data);
311 php_http_buffer_reset(&buf);
312 php_http_registered_classes(&buf, 0);
313 php_info_print_table_row(2, "Implemented Classes", buf.data);
314 php_http_buffer_reset(&buf);
315 php_http_registered_classes(&buf, ZEND_ACC_FINAL_CLASS);
316 php_info_print_table_row(2, "Final Classes", buf.data);
317 php_http_buffer_dtor(&buf);
318
319 php_info_print_table_row(2, "Stream Filters", "http.chunked_encode, http.chunked_decode, http.inflate, http.deflate");
320 php_info_print_table_end();
321
322
323 DISPLAY_INI_ENTRIES();
324 }
325
326
327 /*
328 * Local variables:
329 * tab-width: 4
330 * c-basic-offset: 4
331 * End:
332 * vim600: noet sw=4 ts=4 fdm=marker
333 * vim<600: noet sw=4 ts=4
334 */
335