1 /* 2 +----------------------------------------------------------------------+ 3 | Copyright (c) The PHP Group | 4 +----------------------------------------------------------------------+ 5 | This source file is subject to version 3.01 of the PHP license, | 6 | that is bundled with this package in the file LICENSE, and is | 7 | available through the world-wide-web at the following url: | 8 | https://www.php.net/license/3_01.txt | 9 | If you did not receive a copy of the PHP license and are unable to | 10 | obtain it through the world-wide-web, please send a note to | 11 | license@php.net so we can mail you a copy immediately. | 12 +----------------------------------------------------------------------+ 13 | Author: Zeev Suraski <zeev@php.net> | 14 +----------------------------------------------------------------------+ 15 */ 16 17 #ifndef SAPI_H 18 #define SAPI_H 19 20 #include "php.h" 21 #include "zend.h" 22 #include "zend_API.h" 23 #include "zend_llist.h" 24 #include "zend_operators.h" 25 #include <sys/stat.h> 26 27 #define SAPI_OPTION_NO_CHDIR 1 28 #define SAPI_POST_BLOCK_SIZE 0x4000 29 30 #ifdef PHP_WIN32 31 # ifdef SAPI_EXPORTS 32 # define SAPI_API __declspec(dllexport) 33 # else 34 # define SAPI_API __declspec(dllimport) 35 # endif 36 #elif defined(__GNUC__) && __GNUC__ >= 4 37 # define SAPI_API __attribute__ ((visibility("default"))) 38 #else 39 # define SAPI_API 40 #endif 41 42 #undef shutdown 43 44 typedef struct { 45 char *header; 46 size_t header_len; 47 } sapi_header_struct; 48 49 50 typedef struct { 51 zend_llist headers; 52 int http_response_code; 53 unsigned char send_default_content_type; 54 char *mimetype; 55 char *http_status_line; 56 } sapi_headers_struct; 57 58 59 typedef struct _sapi_post_entry sapi_post_entry; 60 typedef struct _sapi_module_struct sapi_module_struct; 61 62 BEGIN_EXTERN_C() 63 extern SAPI_API sapi_module_struct sapi_module; /* true global */ 64 END_EXTERN_C() 65 66 /* Some values in this structure needs to be filled in before 67 * calling sapi_activate(). We WILL change the `char *' entries, 68 * so make sure that you allocate a separate buffer for them 69 * and that you free them after sapi_deactivate(). 70 */ 71 72 typedef struct { 73 const char *request_method; 74 char *query_string; 75 char *cookie_data; 76 zend_long content_length; 77 78 char *path_translated; 79 char *request_uri; 80 81 /* Do not use request_body directly, but the php://input stream wrapper instead */ 82 struct _php_stream *request_body; 83 84 const char *content_type; 85 86 bool headers_only; 87 bool no_headers; 88 bool headers_read; 89 90 sapi_post_entry *post_entry; 91 92 char *content_type_dup; 93 94 /* for HTTP authentication */ 95 char *auth_user; 96 char *auth_password; 97 char *auth_digest; 98 99 /* this is necessary for the CGI SAPI module */ 100 char *argv0; 101 102 char *current_user; 103 int current_user_length; 104 105 /* this is necessary for CLI module */ 106 int argc; 107 char **argv; 108 int proto_num; 109 } sapi_request_info; 110 111 typedef struct { 112 bool throw_exceptions; 113 struct { 114 bool set; 115 zend_long value; 116 } options_cache[5]; 117 } sapi_request_parse_body_context; 118 119 typedef enum { 120 REQUEST_PARSE_BODY_OPTION_max_file_uploads = 0, 121 REQUEST_PARSE_BODY_OPTION_max_input_vars, 122 REQUEST_PARSE_BODY_OPTION_max_multipart_body_parts, 123 REQUEST_PARSE_BODY_OPTION_post_max_size, 124 REQUEST_PARSE_BODY_OPTION_upload_max_filesize, 125 } request_parse_body_option; 126 127 #define REQUEST_PARSE_BODY_OPTION_GET(name, fallback) \ 128 (SG(request_parse_body_context).options_cache[REQUEST_PARSE_BODY_OPTION_ ## name].set \ 129 ? SG(request_parse_body_context).options_cache[REQUEST_PARSE_BODY_OPTION_ ## name].value \ 130 : (fallback)) 131 132 typedef struct _sapi_globals_struct { 133 void *server_context; 134 sapi_request_info request_info; 135 sapi_headers_struct sapi_headers; 136 int64_t read_post_bytes; 137 unsigned char post_read; 138 unsigned char headers_sent; 139 zend_stat_t global_stat; 140 char *default_mimetype; 141 char *default_charset; 142 HashTable *rfc1867_uploaded_files; 143 zend_long post_max_size; 144 int options; 145 bool sapi_started; 146 double global_request_time; 147 HashTable known_post_content_types; 148 zval callback_func; 149 zend_fcall_info_cache fci_cache; 150 sapi_request_parse_body_context request_parse_body_context; 151 } sapi_globals_struct; 152 153 154 BEGIN_EXTERN_C() 155 #ifdef ZTS 156 # define SG(v) ZEND_TSRMG_FAST(sapi_globals_offset, sapi_globals_struct *, v) 157 SAPI_API extern int sapi_globals_id; 158 SAPI_API extern size_t sapi_globals_offset; 159 #else 160 # define SG(v) (sapi_globals.v) 161 extern SAPI_API sapi_globals_struct sapi_globals; 162 #endif 163 164 SAPI_API void sapi_startup(sapi_module_struct *sf); 165 SAPI_API void sapi_shutdown(void); 166 SAPI_API void sapi_activate(void); 167 SAPI_API void sapi_deactivate_module(void); 168 SAPI_API void sapi_deactivate_destroy(void); 169 SAPI_API void sapi_deactivate(void); 170 SAPI_API void sapi_initialize_empty_request(void); 171 SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg); 172 END_EXTERN_C() 173 174 /* 175 * This is the preferred and maintained API for 176 * operating on HTTP headers. 177 */ 178 179 /* 180 * Always specify a sapi_header_line this way: 181 * 182 * sapi_header_line ctr = {0}; 183 */ 184 185 typedef struct { 186 const char *line; /* If you allocated this, you need to free it yourself */ 187 size_t line_len; 188 zend_long response_code; /* long due to zend_parse_parameters compatibility */ 189 } sapi_header_line; 190 191 typedef enum { /* Parameter: */ 192 SAPI_HEADER_REPLACE, /* sapi_header_line* */ 193 SAPI_HEADER_ADD, /* sapi_header_line* */ 194 SAPI_HEADER_DELETE, /* sapi_header_line* */ 195 SAPI_HEADER_DELETE_ALL, /* void */ 196 SAPI_HEADER_SET_STATUS /* int */ 197 } sapi_header_op_enum; 198 199 BEGIN_EXTERN_C() 200 SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg); 201 202 /* Deprecated functions. Use sapi_header_op instead. */ 203 SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace); 204 #define sapi_add_header(a, b, c) sapi_add_header_ex((a),(b),(c),1) 205 206 207 SAPI_API int sapi_send_headers(void); 208 SAPI_API void sapi_free_header(sapi_header_struct *sapi_header); 209 SAPI_API void sapi_handle_post(void *arg); 210 SAPI_API void sapi_read_post_data(void); 211 SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen); 212 SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entry); 213 SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry); 214 SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry); 215 SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void)); 216 SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray)); 217 SAPI_API int sapi_register_input_filter(unsigned int (*input_filter)(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len), unsigned int (*input_filter_init)(void)); 218 219 SAPI_API int sapi_flush(void); 220 SAPI_API zend_stat_t *sapi_get_stat(void); 221 SAPI_API char *sapi_getenv(const char *name, size_t name_len); 222 223 SAPI_API char *sapi_get_default_content_type(void); 224 SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header); 225 SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len); 226 SAPI_API void sapi_activate_headers_only(void); 227 228 SAPI_API int sapi_get_fd(int *fd); 229 SAPI_API int sapi_force_http_10(void); 230 231 SAPI_API int sapi_get_target_uid(uid_t *); 232 SAPI_API int sapi_get_target_gid(gid_t *); 233 SAPI_API double sapi_get_request_time(void); 234 SAPI_API void sapi_terminate_process(void); 235 END_EXTERN_C() 236 237 struct _sapi_module_struct { 238 char *name; 239 char *pretty_name; 240 241 int (*startup)(struct _sapi_module_struct *sapi_module); 242 int (*shutdown)(struct _sapi_module_struct *sapi_module); 243 244 int (*activate)(void); 245 int (*deactivate)(void); 246 247 size_t (*ub_write)(const char *str, size_t str_length); 248 void (*flush)(void *server_context); 249 zend_stat_t *(*get_stat)(void); 250 char *(*getenv)(const char *name, size_t name_len); 251 252 void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); 253 254 int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers); 255 int (*send_headers)(sapi_headers_struct *sapi_headers); 256 void (*send_header)(sapi_header_struct *sapi_header, void *server_context); 257 258 size_t (*read_post)(char *buffer, size_t count_bytes); 259 char *(*read_cookies)(void); 260 261 void (*register_server_variables)(zval *track_vars_array); 262 void (*log_message)(const char *message, int syslog_type_int); 263 zend_result (*get_request_time)(double *request_time); 264 void (*terminate_process)(void); 265 266 char *php_ini_path_override; 267 268 void (*default_post_reader)(void); 269 void (*treat_data)(int arg, char *str, zval *destArray); 270 char *executable_location; 271 272 int php_ini_ignore; 273 int php_ini_ignore_cwd; /* don't look for php.ini in the current directory */ 274 275 int (*get_fd)(int *fd); 276 277 int (*force_http_10)(void); 278 279 int (*get_target_uid)(uid_t *); 280 int (*get_target_gid)(gid_t *); 281 282 unsigned int (*input_filter)(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len); 283 284 void (*ini_defaults)(HashTable *configuration_hash); 285 int phpinfo_as_text; 286 287 const char *ini_entries; 288 const zend_function_entry *additional_functions; 289 unsigned int (*input_filter_init)(void); 290 }; 291 292 struct _sapi_post_entry { 293 char *content_type; 294 uint32_t content_type_len; 295 void (*post_reader)(void); 296 void (*post_handler)(char *content_type_dup, void *arg); 297 }; 298 299 /* header_handler() constants */ 300 #define SAPI_HEADER_ADD (1<<0) 301 302 303 #define SAPI_HEADER_SENT_SUCCESSFULLY 1 304 #define SAPI_HEADER_DO_SEND 2 305 #define SAPI_HEADER_SEND_FAILED 3 306 307 #define SAPI_DEFAULT_MIMETYPE "text/html" 308 #define SAPI_DEFAULT_CHARSET PHP_DEFAULT_CHARSET 309 #define SAPI_PHP_VERSION_HEADER "X-Powered-By: PHP/" PHP_VERSION 310 311 #define SAPI_POST_READER_FUNC(post_reader) void post_reader(void) 312 #define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg) 313 314 #define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray) 315 #define SAPI_INPUT_FILTER_FUNC(input_filter) unsigned int input_filter(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len) 316 317 BEGIN_EXTERN_C() 318 SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data); 319 SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader); 320 SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data); 321 SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter); 322 END_EXTERN_C() 323 324 #define STANDARD_SAPI_MODULE_PROPERTIES \ 325 NULL, /* php_ini_path_override */ \ 326 NULL, /* default_post_reader */ \ 327 NULL, /* treat_data */ \ 328 NULL, /* executable_location */ \ 329 0, /* php_ini_ignore */ \ 330 0, /* php_ini_ignore_cwd */ \ 331 NULL, /* get_fd */ \ 332 NULL, /* force_http_10 */ \ 333 NULL, /* get_target_uid */ \ 334 NULL, /* get_target_gid */ \ 335 NULL, /* input_filter */ \ 336 NULL, /* ini_defaults */ \ 337 0, /* phpinfo_as_text; */ \ 338 NULL, /* ini_entries; */ \ 339 NULL, /* additional_functions */ \ 340 NULL /* input_filter_init */ 341 342 #endif /* SAPI_H */ 343