xref: /PHP-5.6/main/php_content_types.c (revision 1b4570b7)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author:                                                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 /* $Id$ */
20 
21 #include "php.h"
22 #include "SAPI.h"
23 #include "rfc1867.h"
24 
25 #include "php_content_types.h"
26 
27 /* {{{ php_post_entries[]
28  */
29 static sapi_post_entry php_post_entries[] = {
30 	{ DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data,	php_std_post_handler },
31 	{ MULTIPART_CONTENT_TYPE,    sizeof(MULTIPART_CONTENT_TYPE)-1,    NULL,                         rfc1867_post_handler },
32 	{ NULL, 0, NULL, NULL }
33 };
34 /* }}} */
35 
populate_raw_post_data(TSRMLS_D)36 static zend_bool populate_raw_post_data(TSRMLS_D)
37 {
38 	if (!SG(request_info).request_body) {
39 		return (zend_bool) 0;
40 	}
41 
42 	if (!PG(always_populate_raw_post_data)) {
43 		return (zend_bool) !SG(request_info).post_entry;
44 	}
45 
46 	return (zend_bool) (PG(always_populate_raw_post_data) > 0);
47 }
48 
49 /* {{{ SAPI_POST_READER_FUNC
50  */
SAPI_POST_READER_FUNC(php_default_post_reader)51 SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader)
52 {
53 	if (!strcmp(SG(request_info).request_method, "POST")) {
54 		if (NULL == SG(request_info).post_entry) {
55 			/* no post handler registered, so we just swallow the data */
56 			sapi_read_standard_form_data(TSRMLS_C);
57 		}
58 
59 		if (populate_raw_post_data(TSRMLS_C)) {
60 			size_t length;
61 			char *data = NULL;
62 
63 			php_stream_rewind(SG(request_info).request_body);
64 			length = php_stream_copy_to_mem(SG(request_info).request_body, &data, PHP_STREAM_COPY_ALL, 0);
65 			php_stream_rewind(SG(request_info).request_body);
66 
67 			if (length > INT_MAX) {
68 				sapi_module.sapi_error(E_WARNING,
69 					"HTTP_RAW_POST_DATA truncated from %lu to %d bytes",
70 					(unsigned long) length, INT_MAX);
71 				length = INT_MAX;
72 			}
73 			if (!data) {
74 				data = STR_EMPTY_ALLOC();
75 			}
76 			SET_VAR_STRINGL("HTTP_RAW_POST_DATA", data, length);
77 
78 			sapi_module.sapi_error(E_DEPRECATED,
79 				"Automatically populating $HTTP_RAW_POST_DATA is deprecated and "
80 				"will be removed in a future version. To avoid this warning set "
81 				"'always_populate_raw_post_data' to '-1' in php.ini and use the "
82 				"php://input stream instead.");
83 		}
84 	}
85 }
86 /* }}} */
87 
88 /* {{{ php_startup_sapi_content_types
89  */
php_startup_sapi_content_types(TSRMLS_D)90 int php_startup_sapi_content_types(TSRMLS_D)
91 {
92 	sapi_register_default_post_reader(php_default_post_reader TSRMLS_CC);
93 	sapi_register_treat_data(php_default_treat_data TSRMLS_CC);
94 	sapi_register_input_filter(php_default_input_filter, NULL TSRMLS_CC);
95 	return SUCCESS;
96 }
97 /* }}} */
98 
99 /* {{{ php_setup_sapi_content_types
100  */
php_setup_sapi_content_types(TSRMLS_D)101 int php_setup_sapi_content_types(TSRMLS_D)
102 {
103 	sapi_register_post_entries(php_post_entries TSRMLS_CC);
104 
105 	return SUCCESS;
106 }
107 /* }}} */
108 
109 /*
110  * Local variables:
111  * tab-width: 4
112  * c-basic-offset: 4
113  * End:
114  * vim600: sw=4 ts=4 fdm=marker
115  * vim<600: sw=4 ts=4
116  */
117