- fix write access of HttpMessage properties
[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
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19 #include "php.h"
20
21 #include "zend_extensions.h"
22
23 #include "SAPI.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26
27 #include "php_http.h"
28 #include "php_http_std_defs.h"
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_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 #ifdef HTTP_HAVE_MHASH
61 # include <mhash.h>
62 #endif
63 #ifdef HTTP_HAVE_ZLIB
64 # include <zlib.h>
65 #endif
66
67 #include <ctype.h>
68
69 ZEND_DECLARE_MODULE_GLOBALS(http);
70 HTTP_DECLARE_ARG_PASS_INFO();
71
72 #ifdef COMPILE_DL_HTTP
73 ZEND_GET_MODULE(http)
74 #endif
75
76 /* {{{ http_functions[] */
77 zend_function_entry http_functions[] = {
78 PHP_FE(http_test, NULL)
79 PHP_FE(http_date, NULL)
80 PHP_FE(http_build_uri, NULL)
81 PHP_FALIAS(http_absolute_uri, http_build_uri, NULL)
82 PHP_FE(http_negotiate_language, http_arg_pass_ref_2)
83 PHP_FE(http_negotiate_charset, http_arg_pass_ref_2)
84 PHP_FE(http_redirect, NULL)
85 PHP_FE(http_throttle, NULL)
86 PHP_FE(http_send_status, NULL)
87 PHP_FE(http_send_last_modified, NULL)
88 PHP_FE(http_send_content_type, NULL)
89 PHP_FE(http_send_content_disposition, NULL)
90 PHP_FE(http_match_modified, NULL)
91 PHP_FE(http_match_etag, NULL)
92 PHP_FE(http_cache_last_modified, NULL)
93 PHP_FE(http_cache_etag, NULL)
94 PHP_FE(http_send_data, NULL)
95 PHP_FE(http_send_file, NULL)
96 PHP_FE(http_send_stream, NULL)
97 PHP_FE(http_chunked_decode, NULL)
98 PHP_FE(http_parse_message, NULL)
99 PHP_FE(http_parse_headers, NULL)
100 PHP_FE(http_get_request_headers, NULL)
101 PHP_FE(http_get_request_body, NULL)
102 PHP_FE(http_match_request_header, NULL)
103 #ifdef HTTP_HAVE_CURL
104 PHP_FE(http_get, http_arg_pass_ref_3)
105 PHP_FE(http_head, http_arg_pass_ref_3)
106 PHP_FE(http_post_data, http_arg_pass_ref_4)
107 PHP_FE(http_post_fields, http_arg_pass_ref_5)
108 PHP_FE(http_put_file, http_arg_pass_ref_4)
109 PHP_FE(http_put_stream, http_arg_pass_ref_4)
110 #endif
111 PHP_FE(http_request_method_register, NULL)
112 PHP_FE(http_request_method_unregister, NULL)
113 PHP_FE(http_request_method_exists, NULL)
114 PHP_FE(http_request_method_name, NULL)
115 #ifndef ZEND_ENGINE_2
116 PHP_FE(http_build_query, NULL)
117 #endif
118 PHP_FE(ob_etaghandler, NULL)
119 #ifdef HTTP_HAVE_ZLIB
120 PHP_FE(http_gzencode, NULL)
121 PHP_FE(http_gzdecode, NULL)
122 PHP_FE(http_deflate, NULL)
123 PHP_FE(http_inflate, NULL)
124 PHP_FE(http_compress, NULL)
125 PHP_FE(http_uncompress, NULL)
126 #endif
127 PHP_FE(http_support, NULL)
128
129 EMPTY_FUNCTION_ENTRY
130 };
131 /* }}} */
132
133 /* {{{ http_module_dep */
134 #if ZEND_EXTENSION_API_NO >= 220050617
135 static zend_module_dep http_module_dep[] = {
136 # ifdef HAVE_SPL
137 ZEND_MOD_REQUIRED("spl")
138 # endif
139 {NULL, NULL, NULL, 0}
140 };
141 #endif
142 /* }}} */
143
144 /* {{{ http_module_entry */
145 zend_module_entry http_module_entry = {
146 #if ZEND_EXTENSION_API_NO >= 220050617
147 STANDARD_MODULE_HEADER_EX, NULL,
148 http_module_dep,
149 #else
150 STANDARD_MODULE_HEADER,
151 #endif
152 "http",
153 http_functions,
154 PHP_MINIT(http),
155 PHP_MSHUTDOWN(http),
156 PHP_RINIT(http),
157 PHP_RSHUTDOWN(http),
158 PHP_MINFO(http),
159 HTTP_PEXT_VERSION,
160 STANDARD_MODULE_PROPERTIES
161 };
162 /* }}} */
163
164 int http_module_number;
165
166 /* {{{ http_globals */
167 static void http_globals_init_once(zend_http_globals *G)
168 {
169 memset(G, 0, sizeof(zend_http_globals));
170 }
171
172 static inline void http_globals_init(zend_http_globals *G)
173 {
174 G->send.buffer_size = HTTP_SENDBUF_SIZE;
175 zend_hash_init(&G->request.methods.custom, 0, NULL, ZVAL_PTR_DTOR, 0);
176 #ifdef HTTP_HAVE_CURL
177 zend_llist_init(&G->request.copies.strings, sizeof(char *), http_request_data_free_string, 0);
178 zend_llist_init(&G->request.copies.slists, sizeof(struct curl_slist *), http_request_data_free_slist, 0);
179 zend_llist_init(&G->request.copies.contexts, sizeof(http_request_callback_ctx *), http_request_data_free_context, 0);
180 zend_llist_init(&G->request.copies.convs, sizeof(http_request_conv *), http_request_data_free_conv, 0);
181 #endif
182 }
183
184 static inline void http_globals_free(zend_http_globals *G)
185 {
186 STR_SET(G->send.content_type, NULL);
187 STR_SET(G->send.unquoted_etag, NULL);
188 zend_hash_destroy(&G->request.methods.custom);
189 #ifdef HTTP_HAVE_CURL
190 zend_llist_clean(&G->request.copies.strings);
191 zend_llist_clean(&G->request.copies.slists);
192 zend_llist_clean(&G->request.copies.contexts);
193 zend_llist_clean(&G->request.copies.convs);
194 #endif
195 }
196 /* }}} */
197
198 /* {{{ static inline void http_check_allowed_methods(char *, int) */
199 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
200 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
201 {
202 if (length && SG(request_info).request_method) {
203 if (SUCCESS != http_check_method_ex(SG(request_info).request_method, methods)) {
204 char *header = emalloc(length + sizeof("Allow: "));
205 sprintf(header, "Allow: %s", methods);
206 http_exit(405, header);
207 }
208 }
209 }
210 /* }}} */
211
212 /* {{{ PHP_INI */
213 PHP_INI_MH(http_update_allowed_methods)
214 {
215 http_check_allowed_methods(new_value, new_value_length);
216 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
217 }
218
219 PHP_INI_DISP(http_etag_mode_displayer)
220 {
221 long value;
222
223 if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
224 value = (ini_entry->orig_value) ? atoi(ini_entry->orig_value) : HTTP_ETAG_MD5;
225 } else if (ini_entry->value) {
226 value = (ini_entry->value[0]) ? atoi(ini_entry->value) : HTTP_ETAG_MD5;
227 } else {
228 value = HTTP_ETAG_MD5;
229 }
230
231 switch (value)
232 {
233 case HTTP_ETAG_CRC32:
234 ZEND_WRITE("HTTP_ETAG_CRC32", lenof("HTTP_ETAG_CRC32"));
235 break;
236
237 case HTTP_ETAG_SHA1:
238 ZEND_WRITE("HTTP_ETAG_SHA1", lenof("HTTP_ETAG_SHA1"));
239 break;
240
241 case HTTP_ETAG_MD5:
242 #ifndef HTTP_HAVE_MHASH
243 default:
244 #endif
245 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
246 break;
247
248 #ifdef HTTP_HAVE_MHASH
249 default:
250 {
251 const char *hash_name = mhash_get_hash_name_static(value);
252
253 if (!hash_name) {
254 ZEND_WRITE("HTTP_ETAG_MD5", lenof("HTTP_ETAG_MD5"));
255 } else {
256 ZEND_WRITE("HTTP_ETAG_MHASH_", lenof("HTTP_ETAG_MHASH_"));
257 ZEND_WRITE(hash_name, strlen(hash_name));
258 }
259 }
260 break;
261 #endif
262 }
263 }
264
265 #ifndef ZEND_ENGINE_2
266 # define OnUpdateLong OnUpdateInt
267 #endif
268
269 PHP_INI_BEGIN()
270 HTTP_PHP_INI_ENTRY("http.allowed_methods", "", PHP_INI_ALL, http_update_allowed_methods, request.methods.allowed)
271 HTTP_PHP_INI_ENTRY("http.cache_log", "", PHP_INI_ALL, OnUpdateString, log.cache)
272 HTTP_PHP_INI_ENTRY("http.redirect_log", "", PHP_INI_ALL, OnUpdateString, log.redirect)
273 HTTP_PHP_INI_ENTRY("http.allowed_methods_log", "", PHP_INI_ALL, OnUpdateString, log.allowed_methods)
274 HTTP_PHP_INI_ENTRY("http.composite_log", "", PHP_INI_ALL, OnUpdateString, log.composite)
275 HTTP_PHP_INI_ENTRY_EX("http.etag_mode", "-2", PHP_INI_ALL, OnUpdateLong, http_etag_mode_displayer, etag.mode)
276 #ifdef ZEND_ENGINE_2
277 HTTP_PHP_INI_ENTRY("http.only_exceptions", "0", PHP_INI_ALL, OnUpdateBool, only_exceptions)
278 #endif
279 HTTP_PHP_INI_ENTRY("http.force_exit", "1", PHP_INI_ALL, OnUpdateBool, force_exit)
280 PHP_INI_END()
281 /* }}} */
282
283 /* {{{ PHP_MINIT_FUNCTION */
284 PHP_MINIT_FUNCTION(http)
285 {
286 http_module_number = module_number;
287
288 ZEND_INIT_MODULE_GLOBALS(http, http_globals_init_once, NULL)
289
290 REGISTER_INI_ENTRIES();
291
292 if ( (SUCCESS != PHP_MINIT_CALL(http_support)) ||
293 (SUCCESS != PHP_MINIT_CALL(http_headers)) ||
294 (SUCCESS != PHP_MINIT_CALL(http_cache)) ||
295 #ifdef HTTP_HAVE_CURL
296 (SUCCESS != PHP_MINIT_CALL(http_request)) ||
297 #endif /* HTTP_HAVE_CURL */
298 (SUCCESS != PHP_MINIT_CALL(http_request_method))) {
299 return FAILURE;
300 }
301
302 #ifdef ZEND_ENGINE_2
303 if ( (SUCCESS != PHP_MINIT_CALL(http_util_object)) ||
304 (SUCCESS != PHP_MINIT_CALL(http_message_object)) ||
305 # ifndef WONKY
306 (SUCCESS != PHP_MINIT_CALL(http_response_object)) ||
307 # endif /* WONKY */
308 # ifdef HTTP_HAVE_CURL
309 (SUCCESS != PHP_MINIT_CALL(http_request_object)) ||
310 (SUCCESS != PHP_MINIT_CALL(http_requestpool_object)) ||
311 # endif /* HTTP_HAVE_CURL */
312 (SUCCESS != PHP_MINIT_CALL(http_exception_object))) {
313 return FAILURE;
314 }
315 #endif /* ZEND_ENGINE_2 */
316
317 return SUCCESS;
318 }
319 /* }}} */
320
321 /* {{{ PHP_MSHUTDOWN_FUNCTION */
322 PHP_MSHUTDOWN_FUNCTION(http)
323 {
324 UNREGISTER_INI_ENTRIES();
325 #ifdef HTTP_HAVE_CURL
326 return PHP_MSHUTDOWN_CALL(http_request);
327 #endif
328 return SUCCESS;
329 }
330 /* }}} */
331
332 /* {{{ PHP_RINIT_FUNCTION */
333 PHP_RINIT_FUNCTION(http)
334 {
335 char *m;
336
337 if (m = INI_STR("http.allowed_methods")) {
338 http_check_allowed_methods(m, strlen(m));
339 }
340
341 http_globals_init(HTTP_GLOBALS);
342 return SUCCESS;
343 }
344 /* }}} */
345
346 /* {{{ PHP_RSHUTDOWN_FUNCTION */
347 PHP_RSHUTDOWN_FUNCTION(http)
348 {
349 STATUS status = SUCCESS;
350
351 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
352 status = PHP_RSHUTDOWN_CALL(http_request_method);
353 #endif
354
355 http_globals_free(HTTP_GLOBALS);
356 return status;
357 }
358 /* }}} */
359
360 /* {{{ PHP_MINFO_FUNCTION */
361 PHP_MINFO_FUNCTION(http)
362 {
363 php_info_print_table_start();
364 {
365 php_info_print_table_row(2, "Extended HTTP support", "enabled");
366 php_info_print_table_row(2, "Extension Version", HTTP_PEXT_VERSION);
367 #ifdef HTTP_HAVE_CURL
368 php_info_print_table_row(2, "cURL HTTP Requests", curl_version());
369 #else
370 php_info_print_table_row(2, "cURL HTTP Requests", "disabled");
371 #endif
372 #ifdef HTTP_HAVE_ZLIB
373 {
374 char my_zlib_version[64] = {0};
375
376 strlcat(my_zlib_version, "zlib/", 63);
377 strlcat(my_zlib_version, zlibVersion(), 63);
378 php_info_print_table_row(2, "zlib GZIP Encodings", my_zlib_version);
379 }
380 #else
381 php_info_print_table_row(2, "zlib GZIP Encodings", "disabled");
382 #endif
383 #ifdef HTTP_HAVE_MHASH
384 {
385 char mhash_info[32];
386
387 snprintf(mhash_info, 32, "libmhash/%d", MHASH_API_VERSION);
388 php_info_print_table_row(2, "mhash ETag Generator", mhash_info);
389 }
390 #else
391 php_info_print_table_row(2, "mhash ETag Generator", "disabled");
392 #endif
393 #if defined(HTTP_HAVE_MAGIC) && !defined(WONKY)
394 php_info_print_table_row(2, "magic MIME Guessing", "libmagic/unknown");
395 #else
396 php_info_print_table_row(2, "magic MIME Guessing", "disabled");
397 #endif
398 php_info_print_table_row(2, "Registered Classes",
399 #ifndef ZEND_ENGINE_2
400 "none"
401 #else
402 "HttpUtil, "
403 "HttpMessage, "
404 # ifdef HTTP_HAVE_CURL
405 "HttpRequest, "
406 "HttpRequestPool, "
407 # endif
408 # ifndef WONKY
409 "HttpResponse"
410 # endif
411 #endif
412 );
413 }
414 php_info_print_table_end();
415
416 php_info_print_table_start();
417 php_info_print_table_colspan_header(2, "Supported ETag Hash Algorithms");
418 {
419
420 php_info_print_table_row(2, "PHP", "CRC32, MD5, SHA1");
421 #ifdef HTTP_HAVE_MHASH
422 {
423 phpstr *algos = phpstr_new();
424 int i, c = mhash_count();
425
426 for (i = 0; i <= c; ++i) {
427 const char *hash = mhash_get_hash_name_static(i);
428
429 if (hash) {
430 phpstr_appendf(algos, "%s, ", hash);
431 }
432 }
433 phpstr_fix(algos);
434 php_info_print_table_row(2, "MHASH", PHPSTR_VAL(algos));
435 phpstr_free(&algos);
436 }
437 #else
438 php_info_print_table_row(2, "MHASH", "not available");
439 #endif
440 }
441 php_info_print_table_end();
442
443 php_info_print_table_start();
444 php_info_print_table_colspan_header(2, "Request Methods");
445 {
446 unsigned i;
447 HashPosition pos;
448 zval **custom_method;
449 phpstr *known_request_methods = phpstr_new();
450 phpstr *custom_request_methods = phpstr_new();
451
452 for (i = HTTP_NO_REQUEST_METHOD+1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
453 phpstr_appendl(known_request_methods, http_request_method_name(i));
454 phpstr_appends(known_request_methods, ", ");
455 }
456 FOREACH_HASH_VAL(pos, &HTTP_G(request).methods.custom, custom_method) {
457 phpstr_append(custom_request_methods, Z_STRVAL_PP(custom_method), Z_STRLEN_PP(custom_method));
458 phpstr_appends(custom_request_methods, ", ");
459 }
460
461 phpstr_append(known_request_methods, PHPSTR_VAL(custom_request_methods), PHPSTR_LEN(custom_request_methods));
462 phpstr_fix(known_request_methods);
463 phpstr_fix(custom_request_methods);
464
465 php_info_print_table_row(2, "Known", PHPSTR_VAL(known_request_methods));
466 php_info_print_table_row(2, "Custom",
467 PHPSTR_LEN(custom_request_methods) ? PHPSTR_VAL(custom_request_methods) : "none registered");
468 php_info_print_table_row(2, "Allowed", strlen(HTTP_G(request).methods.allowed) ? HTTP_G(request).methods.allowed : "(ANY)");
469
470 phpstr_free(&known_request_methods);
471 phpstr_free(&custom_request_methods);
472 }
473 php_info_print_table_end();
474
475 DISPLAY_INI_ENTRIES();
476 }
477 /* }}} */
478
479 /*
480 * Local variables:
481 * tab-width: 4
482 * c-basic-offset: 4
483 * End:
484 * vim600: noet sw=4 ts=4 fdm=marker
485 * vim<600: noet sw=4 ts=4
486 */
487