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