0acb05c33c85462ddcbc9e1b9214626082f185d4
[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 #include "php_http_request_api.h"
33
34 #ifdef ZEND_ENGINE_2
35 # include "php_http_util_object.h"
36 # include "php_http_message_object.h"
37 # include "php_http_response_object.h"
38 # ifdef HTTP_HAVE_CURL
39 # include "php_http_request_object.h"
40 # include "php_http_requestpool_object.h"
41 # endif
42 # include "php_http_exception_object.h"
43 #endif
44
45 #include "phpstr/phpstr.h"
46
47 #ifdef HTTP_HAVE_CURL
48 # ifdef PHP_WIN32
49 # include <winsock2.h>
50 # endif
51 # include <curl/curl.h>
52 #endif
53
54 #include <ctype.h>
55
56 ZEND_DECLARE_MODULE_GLOBALS(http);
57 HTTP_DECLARE_ARG_PASS_INFO();
58
59 #ifdef COMPILE_DL_HTTP
60 ZEND_GET_MODULE(http)
61 #endif
62
63 /* {{{ http_functions[] */
64 function_entry http_functions[] = {
65 PHP_FE(http_test, NULL)
66 PHP_FE(http_date, NULL)
67 PHP_FE(http_absolute_uri, NULL)
68 PHP_FE(http_negotiate_language, NULL)
69 PHP_FE(http_negotiate_charset, NULL)
70 PHP_FE(http_redirect, NULL)
71 PHP_FE(http_throttle, NULL)
72 PHP_FE(http_send_status, NULL)
73 PHP_FE(http_send_last_modified, NULL)
74 PHP_FE(http_send_content_type, NULL)
75 PHP_FE(http_send_content_disposition, NULL)
76 PHP_FE(http_match_modified, NULL)
77 PHP_FE(http_match_etag, NULL)
78 PHP_FE(http_cache_last_modified, NULL)
79 PHP_FE(http_cache_etag, NULL)
80 PHP_FE(http_send_data, NULL)
81 PHP_FE(http_send_file, NULL)
82 PHP_FE(http_send_stream, NULL)
83 PHP_FE(http_chunked_decode, NULL)
84 PHP_FE(http_split_response, NULL)
85 PHP_FE(http_parse_headers, NULL)
86 PHP_FE(http_get_request_headers, NULL)
87 PHP_FE(http_get_request_body, NULL)
88 PHP_FE(http_match_request_header, NULL)
89 #ifdef HTTP_HAVE_CURL
90 PHP_FE(http_get, http_arg_pass_ref_3)
91 PHP_FE(http_head, http_arg_pass_ref_3)
92 PHP_FE(http_post_data, http_arg_pass_ref_4)
93 PHP_FE(http_post_fields, http_arg_pass_ref_5)
94 PHP_FE(http_put_file, http_arg_pass_ref_4)
95 PHP_FE(http_put_stream, http_arg_pass_ref_4)
96 PHP_FE(http_request_method_register, NULL)
97 PHP_FE(http_request_method_unregister, NULL)
98 PHP_FE(http_request_method_exists, NULL)
99 PHP_FE(http_request_method_name, NULL)
100 #endif
101 PHP_FE(http_auth_basic, NULL)
102 PHP_FE(http_auth_basic_cb, NULL)
103 #ifndef ZEND_ENGINE_2
104 PHP_FE(http_build_query, NULL)
105 #endif
106 PHP_FE(ob_etaghandler, NULL)
107 {NULL, NULL, NULL}
108 };
109 /* }}} */
110
111 /* {{{ http_module_entry */
112 zend_module_entry http_module_entry = {
113 #if ZEND_MODULE_API_NO >= 20010901
114 STANDARD_MODULE_HEADER,
115 #endif
116 "http",
117 http_functions,
118 PHP_MINIT(http),
119 PHP_MSHUTDOWN(http),
120 PHP_RINIT(http),
121 PHP_RSHUTDOWN(http),
122 PHP_MINFO(http),
123 #if ZEND_MODULE_API_NO >= 20010901
124 HTTP_PEXT_VERSION,
125 #endif
126 STANDARD_MODULE_PROPERTIES
127 };
128 /* }}} */
129
130 int http_module_number;
131
132 /* {{{ http_globals */
133 static inline void http_globals_init(zend_http_globals *G)
134 {
135 memset(G, 0, sizeof(zend_http_globals));
136 G->send.buffer_size = HTTP_SENDBUF_SIZE;
137 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
138 #ifdef HTTP_HAVE_CURL
139 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
140 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
141 zend_llist_init(&G->request.copies.contexts, sizeof(http_curl_callback_ctx *), http_request_data_free_context, 0);
142 zend_llist_init(&G->request.copies.convs, sizeof(http_curl_conv *), http_request_data_free_conv, 0);
143 #endif
144 }
145
146 static inline void http_globals_free(zend_http_globals *G)
147 {
148 STR_FREE(G->send.content_type);
149 STR_FREE(G->send.unquoted_etag);
150 zend_hash_destroy(&G->request.methods.custom);
151 #ifdef HTTP_HAVE_CURL
152 zend_llist_clean(&G->request.copies.strings);
153 zend_llist_clean(&G->request.copies.slists);
154 zend_llist_clean(&G->request.copies.contexts);
155 zend_llist_clean(&G->request.copies.convs);
156 #endif
157 }
158 /* }}} */
159
160 /* {{{ static inline void http_check_allowed_methods(char *, int) */
161 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
162 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
163 {
164 if (length && SG(request_info).request_method) {
165 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
166 char *header = emalloc(length + sizeof("Allow: "));
167 sprintf(header, "Allow: %s", methods);
168 http_exit(405, header);
169 }
170 }
171 }
172 /* }}} */
173
174 /* {{{ PHP_INI */
175 PHP_INI_MH(http_update_allowed_methods)
176 {
177 http_check_allowed_methods(new_value, new_value_length);
178 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
179 }
180
181 PHP_INI_BEGIN()
182 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
183 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
184 #ifdef ZEND_ENGINE_2
185 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
186 #endif
187 PHP_INI_END()
188 /* }}} */
189
190
191 /* {{{ PHP_MINIT_FUNCTION */
192 PHP_MINIT_FUNCTION(http)
193 {
194 http_module_number = module_number;
195
196 #ifdef ZTS
197 ZEND_INIT_MODULE_GLOBALS(http, NULL, NULL)
198 #endif
199
200 REGISTER_INI_ENTRIES();
201
202 #ifdef HTTP_HAVE_CURL
203 if (SUCCESS != http_request_global_init()) {
204 return FAILURE;
205 }
206 #endif /* HTTP_HAVE_CURL */
207
208 #ifdef ZEND_ENGINE_2
209 http_util_object_init();
210 http_message_object_init();
211 http_response_object_init();
212 # ifdef HTTP_HAVE_CURL
213 http_request_object_init();
214 http_requestpool_object_init();
215 # endif /* HTTP_HAVE_CURL */
216 http_exception_object_init();
217 #endif /* ZEND_ENGINE_2 */
218
219 return SUCCESS;
220 }
221 /* }}} */
222
223 /* {{{ PHP_MSHUTDOWN_FUNCTION */
224 PHP_MSHUTDOWN_FUNCTION(http)
225 {
226 UNREGISTER_INI_ENTRIES();
227 #ifdef HTTP_HAVE_CURL
228 curl_global_cleanup();
229 #endif
230 return SUCCESS;
231 }
232 /* }}} */
233
234 /* {{{ PHP_RINIT_FUNCTION */
235 PHP_RINIT_FUNCTION(http)
236 {
237 char *m;
238
239 if (m = INI_STR("http.allowed_methods")) {
240 http_check_allowed_methods(m, strlen(m));
241 }
242
243 http_globals_init(HTTP_GLOBALS);
244 return SUCCESS;
245 }
246 /* }}} */
247
248 /* {{{ PHP_RSHUTDOWN_FUNCTION */
249 PHP_RSHUTDOWN_FUNCTION(http)
250 {
251 http_globals_free(HTTP_GLOBALS);
252 return SUCCESS;
253 }
254 /* }}} */
255
256 /* {{{ PHP_MINFO_FUNCTION */
257 PHP_MINFO_FUNCTION(http)
258 {
259 #ifdef ZEND_ENGINE_2
260 # define HTTP_FUNC_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
261 #else
262 # define HTTP_FUNC_AVAIL(CLASS) "procedural"
263 #endif
264
265 #ifdef HTTP_HAVE_CURL
266 # define HTTP_CURL_VERSION curl_version()
267 # ifdef ZEND_ENGINE_2
268 # define HTTP_CURL_AVAIL(CLASS) "procedural, object oriented (" CLASS ")"
269 # else
270 # define HTTP_CURL_AVAIL(CLASS) "procedural"
271 # endif
272 #else
273 # define HTTP_CURL_VERSION "libcurl not available"
274 # define HTTP_CURL_AVAIL(CLASS) "libcurl not available"
275 #endif
276
277 #include "php_http_request_api.h"
278
279 php_info_print_table_start();
280 {
281 char full_version_string[1024] = {0};
282 snprintf(full_version_string, 1023, "%s (%s)", HTTP_PEXT_VERSION, HTTP_CURL_VERSION);
283
284 php_info_print_table_row(2, "Extended HTTP support:", "enabled");
285 php_info_print_table_row(2, "Extension Version:", full_version_string);
286 }
287 php_info_print_table_end();
288
289 php_info_print_table_start();
290 {
291 unsigned i;
292 zval **custom_method;
293 phpstr *known_request_methods = phpstr_new();
294 phpstr *custom_request_methods = phpstr_new();
295
296 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
297 phpstr_appendl(known_request_methods, http_request_method_name(i));
298 phpstr_appends(known_request_methods, ", ");
299 }
300 FOREACH_HASH_VAL(&HTTP_G(request).methods.custom, custom_method) {
301 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
302 phpstr_appends(custom_request_methods, ", ");
303 }
304
305 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
306 phpstr_fix(known_request_methods);
307 phpstr_fix(custom_request_methods);
308
309 php_info_print_table_row(2, "Known Request Methods:", PHPSTR_VAL(known_request_methods));
310 php_info_print_table_row(2, "Custom Request Methods:",
311 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
312
313 phpstr_free(known_request_methods);
314 phpstr_free(custom_request_methods);
315 }
316 php_info_print_table_end();
317
318 php_info_print_table_start();
319 {
320 php_info_print_table_header(2, "Functionality", "Availability");
321 php_info_print_table_row(2, "Miscellaneous Utilities:", HTTP_FUNC_AVAIL("HttpUtil, HttpMessage"));
322 php_info_print_table_row(2, "Extended HTTP Responses:", HTTP_FUNC_AVAIL("HttpResponse"));
323 php_info_print_table_row(2, "Extended HTTP Requests:", HTTP_CURL_AVAIL("HttpRequest, HttpRequestPool"));
324 }
325 php_info_print_table_end();
326
327 DISPLAY_INI_ENTRIES();
328 }
329 /* }}} */
330
331 /*
332 * Local variables:
333 * tab-width: 4
334 * c-basic-offset: 4
335 * End:
336 * vim600: noet sw=4 ts=4 fdm=marker
337 * vim<600: noet sw=4 ts=4
338 */
339