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