- include missing.h in http.c for static property fixup
[m6w6/ext-http] / http.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 #include "php.h"
23
24 #include "SAPI.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27
28 #include "php_http.h"
29 #include "php_http_std_defs.h"
30 #include "php_http_api.h"
31 #include "php_http_send_api.h"
32 #ifdef HTTP_HAVE_CURL
33 # include "php_http_request_api.h"
34 #endif
35
36 #ifdef ZEND_ENGINE_2
37 # include "php_http_util_object.h"
38 # include "php_http_message_object.h"
39 # include "php_http_response_object.h"
40 # ifdef HTTP_HAVE_CURL
41 # include "php_http_request_object.h"
42 # include "php_http_requestpool_object.h"
43 # endif
44 # include "php_http_exception_object.h"
45 #endif
46
47 #include "missing.h"
48 #include "phpstr/phpstr.h"
49
50 #ifdef HTTP_HAVE_CURL
51 # ifdef PHP_WIN32
52 # include <winsock2.h>
53 # endif
54 # include <curl/curl.h>
55 #endif
56
57 #include <ctype.h>
58
59 ZEND_DECLARE_MODULE_GLOBALS(http);
60 HTTP_DECLARE_ARG_PASS_INFO();
61
62 #ifdef COMPILE_DL_HTTP
63 ZEND_GET_MODULE(http)
64 #endif
65
66 /* {{{ http_functions[] */
67 function_entry http_functions[] = {
68 PHP_FE(http_test, NULL)
69 PHP_FE(http_date, NULL)
70 PHP_FE(http_absolute_uri, NULL)
71 PHP_FE(http_negotiate_language, NULL)
72 PHP_FE(http_negotiate_charset, NULL)
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_get_request_headers, NULL)
90 PHP_FE(http_get_request_body, NULL)
91 PHP_FE(http_match_request_header, NULL)
92 #ifdef HTTP_HAVE_CURL
93 PHP_FE(http_get, http_arg_pass_ref_3)
94 PHP_FE(http_head, http_arg_pass_ref_3)
95 PHP_FE(http_post_data, http_arg_pass_ref_4)
96 PHP_FE(http_post_fields, http_arg_pass_ref_5)
97 PHP_FE(http_put_file, http_arg_pass_ref_4)
98 PHP_FE(http_put_stream, http_arg_pass_ref_4)
99 PHP_FE(http_request_method_register, NULL)
100 PHP_FE(http_request_method_unregister, NULL)
101 PHP_FE(http_request_method_exists, NULL)
102 PHP_FE(http_request_method_name, NULL)
103 #endif
104 PHP_FE(http_auth_basic, NULL)
105 PHP_FE(http_auth_basic_cb, NULL)
106 #ifndef ZEND_ENGINE_2
107 PHP_FE(http_build_query, NULL)
108 #endif
109 PHP_FE(ob_etaghandler, NULL)
110 {NULL, NULL, NULL}
111 };
112 /* }}} */
113
114 /* {{{ http_module_entry */
115 zend_module_entry http_module_entry = {
116 #if ZEND_MODULE_API_NO >= 20010901
117 STANDARD_MODULE_HEADER,
118 #endif
119 "http",
120 http_functions,
121 PHP_MINIT(http),
122 PHP_MSHUTDOWN(http),
123 PHP_RINIT(http),
124 PHP_RSHUTDOWN(http),
125 PHP_MINFO(http),
126 #if ZEND_MODULE_API_NO >= 20010901
127 HTTP_PEXT_VERSION,
128 #endif
129 STANDARD_MODULE_PROPERTIES
130 };
131 /* }}} */
132
133 int http_module_number;
134
135 /* {{{ http_globals */
136 static inline void http_globals_init(zend_http_globals *G)
137 {
138 memset(G, 0, sizeof(zend_http_globals));
139 G->send.buffer_size = HTTP_SENDBUF_SIZE;
140 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
141 #ifdef HTTP_HAVE_CURL
142 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
143 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
144 zend_llist_init(&G->request.copies.contexts, sizeof(http_curl_callback_ctx *), http_request_data_free_context, 0);
145 zend_llist_init(&G->request.copies.convs, sizeof(http_curl_conv *), http_request_data_free_conv, 0);
146 #endif
147 }
148
149 static inline void http_globals_free(zend_http_globals *G)
150 {
151 STR_FREE(G->send.content_type);
152 STR_FREE(G->send.unquoted_etag);
153 zend_hash_destroy(&G->request.methods.custom);
154 #ifdef HTTP_HAVE_CURL
155 zend_llist_clean(&G->request.copies.strings);
156 zend_llist_clean(&G->request.copies.slists);
157 zend_llist_clean(&G->request.copies.contexts);
158 zend_llist_clean(&G->request.copies.convs);
159 #endif
160 }
161 /* }}} */
162
163 /* {{{ static inline void http_check_allowed_methods(char *, int) */
164 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
165 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
166 {
167 if (length && SG(request_info).request_method) {
168 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
169 char *header = emalloc(length + sizeof("Allow: "));
170 sprintf(header, "Allow: %s", methods);
171 http_exit(405, header);
172 }
173 }
174 }
175 /* }}} */
176
177 /* {{{ PHP_INI */
178 PHP_INI_MH(http_update_allowed_methods)
179 {
180 http_check_allowed_methods(new_value, new_value_length);
181 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
182 }
183
184 PHP_INI_BEGIN()
185 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
186 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
187 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
188 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
189 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
190 #ifdef ZEND_ENGINE_2
191 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
192 #endif
193 PHP_INI_END()
194 /* }}} */
195
196
197 /* {{{ PHP_MINIT_FUNCTION */
198 PHP_MINIT_FUNCTION(http)
199 {
200 http_module_number = module_number;
201
202 #ifdef ZTS
203 ZEND_INIT_MODULE_GLOBALS(http, NULL, NULL)
204 #endif
205
206 REGISTER_INI_ENTRIES();
207
208 #ifdef HTTP_HAVE_CURL
209 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
210 return FAILURE;
211 }
212 #endif /* HTTP_HAVE_CURL */
213
214 #ifdef ZEND_ENGINE_2
215 http_util_object_init();
216 http_message_object_init();
217 http_response_object_init();
218 # ifdef HTTP_HAVE_CURL
219 http_request_object_init();
220 http_requestpool_object_init();
221 # endif /* HTTP_HAVE_CURL */
222 http_exception_object_init();
223 #endif /* ZEND_ENGINE_2 */
224
225 zend_hash_init_ex(&http_response_statics, 0, NULL, ZVAL_INTERNAL_PTR_DTOR, 1, 0);
226 zend_fix_static_properties(http_response_object_ce, &http_response_statics TSRMLS_CC);
227 return SUCCESS;
228 }
229 /* }}} */
230
231 /* {{{ PHP_MSHUTDOWN_FUNCTION */
232 PHP_MSHUTDOWN_FUNCTION(http)
233 {
234 zend_hash_destroy(&http_response_statics);
235 UNREGISTER_INI_ENTRIES();
236 #ifdef HTTP_HAVE_CURL
237 curl_global_cleanup();
238 #endif
239 return SUCCESS;
240 }
241 /* }}} */
242
243 /* {{{ PHP_RINIT_FUNCTION */
244 PHP_RINIT_FUNCTION(http)
245 {
246 char *m;
247
248 if (m = INI_STR("http.allowed_methods")) {
249 http_check_allowed_methods(m, strlen(m));
250 }
251
252 http_globals_init(HTTP_GLOBALS);
253 zend_init_static_properties(http_response_object_ce, &http_response_statics TSRMLS_CC);
254 return SUCCESS;
255 }
256 /* }}} */
257
258 /* {{{ PHP_RSHUTDOWN_FUNCTION */
259 PHP_RSHUTDOWN_FUNCTION(http)
260 {
261 zend_clean_static_properties(http_response_object_ce TSRMLS_CC);
262 http_globals_free(HTTP_GLOBALS);
263 return SUCCESS;
264 }
265 /* }}} */
266
267 /* {{{ PHP_MINFO_FUNCTION */
268 PHP_MINFO_FUNCTION(http)
269 {
270 #ifdef ZEND_ENGINE_2
271 # define HTTP_FUNC_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
272 #else
273 # define HTTP_FUNC_AVAIL(CLASS) "procedural"
274 #endif
275
276 #ifdef HTTP_HAVE_CURL
277 # define HTTP_CURL_VERSION curl_version()
278 # ifdef ZEND_ENGINE_2
279 # define HTTP_CURL_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
280 # else
281 # define HTTP_CURL_AVAIL(CLASS) "procedural"
282 # endif
283 #else
284 # define HTTP_CURL_VERSION "libcurl not available"
285 # define HTTP_CURL_AVAIL(CLASS) "libcurl not available"
286 #endif
287
288 #include "php_http_request_api.h"
289
290 php_info_print_table_start();
291 {
292 char full_version_string[1024] = {0};
293 snprintf(full_version_string, 1023, "%s (%s)", HTTP_PEXT_VERSION, HTTP_CURL_VERSION);
294
295 php_info_print_table_row(2, "Extended HTTP support:", "enabled");
296 php_info_print_table_row(2, "Extension Version:", full_version_string);
297 }
298 php_info_print_table_end();
299
300 #ifdef HTTP_HAVE_CURL
301 php_info_print_table_start();
302 {
303 unsigned i;
304 zval **custom_method;
305 phpstr *known_request_methods = phpstr_new();
306 phpstr *custom_request_methods = phpstr_new();
307
308 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
309 phpstr_appendl(known_request_methods, http_request_method_name(i));
310 phpstr_appends(known_request_methods, ", ");
311 }
312 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
313 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
314 phpstr_appends(custom_request_methods, ", ");
315 }
316
317 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
318 phpstr_fix(known_request_methods);
319 phpstr_fix(custom_request_methods);
320
321 php_info_print_table_row(2, "Known Request Methods:", PHPSTR_VAL(known_request_methods));
322 php_info_print_table_row(2, "Custom Request Methods:",
323 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
324
325 phpstr_free(&known_request_methods);
326 phpstr_free(&custom_request_methods);
327 }
328 php_info_print_table_end();
329 #endif
330
331 php_info_print_table_start();
332 {
333 php_info_print_table_header(2, "Functionality", "Availability");
334 php_info_print_table_row(2, "Miscellaneous Utilities:", HTTP_FUNC_AVAIL("HttpUtil, HttpMessage"));
335 php_info_print_table_row(2, "Extended HTTP Responses:", HTTP_FUNC_AVAIL("HttpResponse"));
336 php_info_print_table_row(2, "Extended HTTP Requests:", HTTP_CURL_AVAIL("HttpRequest, HttpRequestPool"));
337 }
338 php_info_print_table_end();
339
340 DISPLAY_INI_ENTRIES();
341 }
342 /* }}} */
343
344 /*
345 * Local variables:
346 * tab-width: 4
347 * c-basic-offset: 4
348 * End:
349 * vim600: noet sw=4 ts=4 fdm=marker
350 * vim<600: noet sw=4 ts=4
351 */
352