- highly experimental stream wrapper
[m6w6/ext-http] / 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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_CURL
20 #define HTTP_WANT_ZLIB
21 #define HTTP_WANT_MAGIC
22 #include "php_http.h"
23
24 #include "SAPI.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27 #include "zend_extensions.h"
28
29 #include "php_http_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_cache_api.h"
32 #include "php_http_headers_api.h"
33 #include "php_http_message_api.h"
34 #include "php_http_request_method_api.h"
35 #ifdef HTTP_HAVE_CURL
36 # include "php_http_request_api.h"
37 # ifdef HTTP_HAVE_WRAPPER
38 # include "php_http_wrapper_api.h"
39 # endif
40 #endif
41
42 #ifdef ZEND_ENGINE_2
43 # include "php_http_filter_api.h"
44 # include "php_http_util_object.h"
45 # include "php_http_message_object.h"
46 # ifndef WONKY
47 # include "php_http_response_object.h"
48 # endif
49 # ifdef HTTP_HAVE_CURL
50 # include "php_http_request_object.h"
51 # include "php_http_requestpool_object.h"
52 # endif
53 # include "php_http_exception_object.h"
54 #endif
55
56
57 ZEND_DECLARE_MODULE_GLOBALS(http);
58 HTTP_DECLARE_ARG_PASS_INFO();
59
60 #ifdef COMPILE_DL_HTTP
61 ZEND_GET_MODULE(http)
62 #endif
63
64 /* {{{ http_functions[] */
65 zend_function_entry http_functions[] = {
66 PHP_FE(http_test, NULL)
67 PHP_FE(http_date, NULL)
68 PHP_FE(http_build_uri, NULL)
69 PHP_FALIAS(http_absolute_uri, http_build_uri, NULL)
70 PHP_FE(http_negotiate_language, http_arg_pass_ref_2)
71 PHP_FE(http_negotiate_charset, http_arg_pass_ref_2)
72 PHP_FE(http_negotiate_content_type, http_arg_pass_ref_2)
73 PHP_FE(http_redirect, NULL)
74 PHP_FE(http_throttle, NULL)
75 PHP_FE(http_send_status, NULL)
76 PHP_FE(http_send_last_modified, NULL)
77 PHP_FE(http_send_content_type, NULL)
78 PHP_FE(http_send_content_disposition, NULL)
79 PHP_FE(http_match_modified, NULL)
80 PHP_FE(http_match_etag, NULL)
81 PHP_FE(http_cache_last_modified, NULL)
82 PHP_FE(http_cache_etag, NULL)
83 PHP_FE(http_send_data, NULL)
84 PHP_FE(http_send_file, NULL)
85 PHP_FE(http_send_stream, NULL)
86 PHP_FE(http_chunked_decode, NULL)
87 PHP_FE(http_parse_message, NULL)
88 PHP_FE(http_parse_headers, NULL)
89 PHP_FE(http_parse_cookie, NULL)
90 PHP_FE(http_get_request_headers, NULL)
91 PHP_FE(http_get_request_body, NULL)
92 PHP_FE(http_match_request_header, NULL)
93 #ifdef HTTP_HAVE_CURL
94 PHP_FE(http_get, http_arg_pass_ref_3)
95 PHP_FE(http_head, http_arg_pass_ref_3)
96 PHP_FE(http_post_data, http_arg_pass_ref_4)
97 PHP_FE(http_post_fields, http_arg_pass_ref_5)
98 PHP_FE(http_put_file, http_arg_pass_ref_4)
99 PHP_FE(http_put_stream, http_arg_pass_ref_4)
100 #endif
101 PHP_FE(http_request_method_register, NULL)
102 PHP_FE(http_request_method_unregister, NULL)
103 PHP_FE(http_request_method_exists, NULL)
104 PHP_FE(http_request_method_name, NULL)
105 #ifndef ZEND_ENGINE_2
106 PHP_FE(http_build_query, NULL)
107 #endif
108 PHP_FE(ob_etaghandler, NULL)
109 #ifdef HTTP_HAVE_ZLIB
110 PHP_FE(http_gzencode, NULL)
111 PHP_FE(http_gzdecode, NULL)
112 PHP_FE(http_deflate, NULL)
113 PHP_FE(http_inflate, NULL)
114 #endif
115 PHP_FE(http_support, NULL)
116
117 EMPTY_FUNCTION_ENTRY
118 };
119 /* }}} */
120
121 /* {{{ http_module_dep */
122 #if ZEND_EXTENSION_API_NO >= 220050617
123 static zend_module_dep http_module_dep[] = {
124 # ifdef HAVE_SPL
125 ZEND_MOD_REQUIRED("spl")
126 # endif
127 # ifdef HTTP_HAVE_EXT_HASH
128 ZEND_MOD_REQUIRED("hash")
129 # endif
130 {NULL, NULL, NULL, 0}
131 };
132 #endif
133 /* }}} */
134
135 /* {{{ http_module_entry */
136 zend_module_entry http_module_entry = {
137 #if ZEND_EXTENSION_API_NO >= 220050617
138 STANDARD_MODULE_HEADER_EX, NULL,
139 http_module_dep,
140 #else
141 STANDARD_MODULE_HEADER,
142 #endif
143 "http",
144 http_functions,
145 PHP_MINIT(http),
146 PHP_MSHUTDOWN(http),
147 PHP_RINIT(http),
148 PHP_RSHUTDOWN(http),
149 PHP_MINFO(http),
150 PHP_EXT_HTTP_VERSION,
151 STANDARD_MODULE_PROPERTIES
152 };
153 /* }}} */
154
155 int http_module_number;
156
157 /* {{{ http_globals */
158 static void http_globals_init_once(zend_http_globals *G)
159 {
160 memset(G, 0, sizeof(zend_http_globals));
161 }
162
163 static inline void http_globals_init(zend_http_globals *G)
164 {
165 G->send.buffer_size = HTTP_SENDBUF_SIZE;
166 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
167 }
168
169 static inline void http_globals_free(zend_http_globals *G)
170 {
171 STR_SET(G->send.content_type, NULL);
172 STR_SET(G->send.unquoted_etag, NULL);
173 zend_hash_destroy(&G->request.methods.custom);
174 }
175 /* }}} */
176
177 /* {{{ static inline void http_check_allowed_methods(char *, int) */
178 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
179 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
180 {
181 if (length && SG(request_info).request_method) {
182 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
183 char *header = emalloc(length + sizeof("Allow: "));
184 sprintf(header, "Allow: %s", methods);
185 http_exit(405, header);
186 }
187 }
188 }
189 /* }}} */
190
191 /* {{{ PHP_INI */
192 PHP_INI_MH(http_update_allowed_methods)
193 {
194 http_check_allowed_methods(new_value, new_value_length);
195 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
196 }
197
198 #ifndef ZEND_ENGINE_2
199 # define OnUpdateLong OnUpdateInt
200 #endif
201
202 PHP_INI_BEGIN()
203 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
204 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
205 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
206 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
207 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
208 HTTP_PHP_INI_ENTRY("http.etag_mode", "MD5", PHP_INI_ALL, OnUpdateString, etag.mode)
209 #ifdef ZEND_ENGINE_2
210 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
211 #endif
212 HTTP_PHP_INI_ENTRY("http.force_exit", "1", PHP_INI_ALL, OnUpdateBool, force_exit)
213 PHP_INI_END()
214 /* }}} */
215
216 /* {{{ PHP_MINIT_FUNCTION */
217 PHP_MINIT_FUNCTION(http)
218 {
219 http_module_number = module_number;
220
221 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL)
222
223 REGISTER_INI_ENTRIES();
224
225 if ( (SUCCESS != PHP_MINIT_CALL(http_support)) ||
226 (SUCCESS != PHP_MINIT_CALL(http_headers)) ||
227 #ifdef HTTP_HAVE_CURL
228 (SUCCESS != PHP_MINIT_CALL(http_request)) ||
229 # ifdef HTTP_HAVE_WRAPPER
230 (SUCCESS != PHP_MINIT_CALL(http_wrapper)) ||
231 # endif
232 #endif /* HTTP_HAVE_CURL */
233 (SUCCESS != PHP_MINIT_CALL(http_request_method))) {
234 return FAILURE;
235 }
236
237 #ifdef ZEND_ENGINE_2
238 if ( (SUCCESS != PHP_MINIT_CALL(http_filter)) ||
239 (SUCCESS != PHP_MINIT_CALL(http_util_object)) ||
240 (SUCCESS != PHP_MINIT_CALL(http_message_object)) ||
241 # ifndef WONKY
242 (SUCCESS != PHP_MINIT_CALL(http_response_object)) ||
243 # endif /* WONKY */
244 # ifdef HTTP_HAVE_CURL
245 (SUCCESS != PHP_MINIT_CALL(http_request_object)) ||
246 (SUCCESS != PHP_MINIT_CALL(http_requestpool_object))||
247 # endif /* HTTP_HAVE_CURL */
248 (SUCCESS != PHP_MINIT_CALL(http_exception_object))) {
249 return FAILURE;
250 }
251 #endif /* ZEND_ENGINE_2 */
252
253 return SUCCESS;
254 }
255 /* }}} */
256
257 /* {{{ PHP_MSHUTDOWN_FUNCTION */
258 PHP_MSHUTDOWN_FUNCTION(http)
259 {
260 UNREGISTER_INI_ENTRIES();
261 #ifdef HTTP_HAVE_CURL
262 return PHP_MSHUTDOWN_CALL(http_request);
263 #endif
264 return SUCCESS;
265 }
266 /* }}} */
267
268 /* {{{ PHP_RINIT_FUNCTION */
269 PHP_RINIT_FUNCTION(http)
270 {
271 if (HTTP_G(request).methods.allowed) {
272 http_check_allowed_methods(HTTP_G(request).methods.allowed,
273 strlen(HTTP_G(request).methods.allowed));
274 }
275
276 http_globals_init(HTTP_GLOBALS);
277 return SUCCESS;
278 }
279 /* }}} */
280
281 /* {{{ PHP_RSHUTDOWN_FUNCTION */
282 PHP_RSHUTDOWN_FUNCTION(http)
283 {
284 STATUS status = SUCCESS;
285
286 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
287 status = PHP_RSHUTDOWN_CALL(http_request_method);
288 #endif
289
290 http_globals_free(HTTP_GLOBALS);
291 return status;
292 }
293 /* }}} */
294
295 /* {{{ PHP_MINFO_FUNCTION */
296 PHP_MINFO_FUNCTION(http)
297 {
298 php_info_print_table_start();
299 {
300 php_info_print_table_row(2, "Extended HTTP support", "enabled");
301 php_info_print_table_row(2, "Extension Version", PHP_EXT_HTTP_VERSION);
302 #ifdef HTTP_HAVE_CURL
303 php_info_print_table_row(2, "cURL HTTP Requests", curl_version());
304 #else
305 php_info_print_table_row(2, "cURL HTTP Requests", "disabled");
306 #endif
307 #ifdef HTTP_HAVE_ZLIB
308 {
309 char my_zlib_version[64] = {0};
310
311 strlcat(my_zlib_version, "zlib/", 63);
312 strlcat(my_zlib_version, zlibVersion(), 63);
313 php_info_print_table_row(2, "zlib GZIP Encodings", my_zlib_version);
314 }
315 #else
316 php_info_print_table_row(2, "zlib GZIP Encodings", "disabled");
317 #endif
318 #if defined(HTTP_HAVE_MAGIC) && !defined(WONKY)
319 php_info_print_table_row(2, "magic MIME Guessing", "libmagic/unknown");
320 #else
321 php_info_print_table_row(2, "magic MIME Guessing", "disabled");
322 #endif
323 php_info_print_table_row(2, "Registered Classes",
324 #ifndef ZEND_ENGINE_2
325 "none"
326 #else
327 "HttpUtil, "
328 "HttpMessage, "
329 # ifdef HTTP_HAVE_CURL
330 "HttpRequest, "
331 "HttpRequestPool, "
332 # endif
333 # ifndef WONKY
334 "HttpResponse"
335 # endif
336 #endif
337 );
338 }
339 php_info_print_table_end();
340
341 php_info_print_table_start();
342 php_info_print_table_colspan_header(2, "Request Methods");
343 {
344 unsigned i;
345 HashPosition pos;
346 zval **custom_method;
347 phpstr *known_request_methods = phpstr_new();
348 phpstr *custom_request_methods = phpstr_new();
349
350 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
351 phpstr_appendl(known_request_methods, http_request_method_name(i));
352 phpstr_appends(known_request_methods, ", ");
353 }
354 FOREACH_HASH_VAL(pos, &HTTP_G(request).methods.custom, custom_method) {
355 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
356 phpstr_appends(custom_request_methods, ", ");
357 }
358
359 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
360 phpstr_fix(known_request_methods);
361 phpstr_fix(custom_request_methods);
362
363 php_info_print_table_row(2, "Known", PHPSTR_VAL(known_request_methods));
364 php_info_print_table_row(2, "Custom",
365 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
366 php_info_print_table_row(2, "Allowed", strlen(HTTP_G(request).methods.allowed) ? HTTP_G(request).methods.allowed : "(ANY)");
367
368 phpstr_free(&known_request_methods);
369 phpstr_free(&custom_request_methods);
370 }
371 php_info_print_table_end();
372
373 DISPLAY_INI_ENTRIES();
374 }
375 /* }}} */
376
377 /*
378 * Local variables:
379 * tab-width: 4
380 * c-basic-offset: 4
381 * End:
382 * vim600: noet sw=4 ts=4 fdm=marker
383 * vim<600: noet sw=4 ts=4
384 */
385