Coverage Report

Created: 2023-12-13 20:05

/src/php-src/main/SAPI.h
Line
Count
Source (jump to first uncovered line)
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
396k
#define SAPI_OPTION_NO_CHDIR 1
28
0
#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
112
typedef struct _sapi_globals_struct {
113
  void *server_context;
114
  sapi_request_info request_info;
115
  sapi_headers_struct sapi_headers;
116
  int64_t read_post_bytes;
117
  unsigned char post_read;
118
  unsigned char headers_sent;
119
  zend_stat_t global_stat;
120
  char *default_mimetype;
121
  char *default_charset;
122
  HashTable *rfc1867_uploaded_files;
123
  zend_long post_max_size;
124
  int options;
125
  bool sapi_started;
126
  double global_request_time;
127
  HashTable known_post_content_types;
128
  zval callback_func;
129
  zend_fcall_info_cache fci_cache;
130
} sapi_globals_struct;
131
132
133
BEGIN_EXTERN_C()
134
#ifdef ZTS
135
# define SG(v) ZEND_TSRMG_FAST(sapi_globals_offset, sapi_globals_struct *, v)
136
SAPI_API extern int sapi_globals_id;
137
SAPI_API extern size_t sapi_globals_offset;
138
#else
139
29.5M
# define SG(v) (sapi_globals.v)
140
extern SAPI_API sapi_globals_struct sapi_globals;
141
#endif
142
143
SAPI_API void sapi_startup(sapi_module_struct *sf);
144
SAPI_API void sapi_shutdown(void);
145
SAPI_API void sapi_activate(void);
146
SAPI_API void sapi_deactivate(void);
147
SAPI_API void sapi_initialize_empty_request(void);
148
SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg);
149
END_EXTERN_C()
150
151
/*
152
 * This is the preferred and maintained API for
153
 * operating on HTTP headers.
154
 */
155
156
/*
157
 * Always specify a sapi_header_line this way:
158
 *
159
 *     sapi_header_line ctr = {0};
160
 */
161
162
typedef struct {
163
  const char *line; /* If you allocated this, you need to free it yourself */
164
  size_t line_len;
165
  zend_long response_code; /* long due to zend_parse_parameters compatibility */
166
} sapi_header_line;
167
168
typedef enum {          /* Parameter:       */
169
  SAPI_HEADER_REPLACE,    /* sapi_header_line*  */
170
  SAPI_HEADER_ADD,      /* sapi_header_line*  */
171
  SAPI_HEADER_DELETE,     /* sapi_header_line*  */
172
  SAPI_HEADER_DELETE_ALL,   /* void         */
173
  SAPI_HEADER_SET_STATUS    /* int          */
174
} sapi_header_op_enum;
175
176
BEGIN_EXTERN_C()
177
SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg);
178
179
/* Deprecated functions. Use sapi_header_op instead. */
180
SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace);
181
396k
#define sapi_add_header(a, b, c) sapi_add_header_ex((a),(b),(c),1)
182
183
184
SAPI_API int sapi_send_headers(void);
185
SAPI_API void sapi_free_header(sapi_header_struct *sapi_header);
186
SAPI_API void sapi_handle_post(void *arg);
187
SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen);
188
SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entry);
189
SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry);
190
SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry);
191
SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void));
192
SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray));
193
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));
194
195
SAPI_API int sapi_flush(void);
196
SAPI_API zend_stat_t *sapi_get_stat(void);
197
SAPI_API char *sapi_getenv(const char *name, size_t name_len);
198
199
SAPI_API char *sapi_get_default_content_type(void);
200
SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header);
201
SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len);
202
SAPI_API void sapi_activate_headers_only(void);
203
204
SAPI_API int sapi_get_fd(int *fd);
205
SAPI_API int sapi_force_http_10(void);
206
207
SAPI_API int sapi_get_target_uid(uid_t *);
208
SAPI_API int sapi_get_target_gid(gid_t *);
209
SAPI_API double sapi_get_request_time(void);
210
SAPI_API void sapi_terminate_process(void);
211
END_EXTERN_C()
212
213
struct _sapi_module_struct {
214
  char *name;
215
  char *pretty_name;
216
217
  int (*startup)(struct _sapi_module_struct *sapi_module);
218
  int (*shutdown)(struct _sapi_module_struct *sapi_module);
219
220
  int (*activate)(void);
221
  int (*deactivate)(void);
222
223
  size_t (*ub_write)(const char *str, size_t str_length);
224
  void (*flush)(void *server_context);
225
  zend_stat_t *(*get_stat)(void);
226
  char *(*getenv)(const char *name, size_t name_len);
227
228
  void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
229
230
  int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers);
231
  int (*send_headers)(sapi_headers_struct *sapi_headers);
232
  void (*send_header)(sapi_header_struct *sapi_header, void *server_context);
233
234
  size_t (*read_post)(char *buffer, size_t count_bytes);
235
  char *(*read_cookies)(void);
236
237
  void (*register_server_variables)(zval *track_vars_array);
238
  void (*log_message)(const char *message, int syslog_type_int);
239
  zend_result (*get_request_time)(double *request_time);
240
  void (*terminate_process)(void);
241
242
  char *php_ini_path_override;
243
244
  void (*default_post_reader)(void);
245
  void (*treat_data)(int arg, char *str, zval *destArray);
246
  char *executable_location;
247
248
  int php_ini_ignore;
249
  int php_ini_ignore_cwd; /* don't look for php.ini in the current directory */
250
251
  int (*get_fd)(int *fd);
252
253
  int (*force_http_10)(void);
254
255
  int (*get_target_uid)(uid_t *);
256
  int (*get_target_gid)(gid_t *);
257
258
  unsigned int (*input_filter)(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len);
259
260
  void (*ini_defaults)(HashTable *configuration_hash);
261
  int phpinfo_as_text;
262
263
  char *ini_entries;
264
  const zend_function_entry *additional_functions;
265
  unsigned int (*input_filter_init)(void);
266
};
267
268
struct _sapi_post_entry {
269
  char *content_type;
270
  uint32_t content_type_len;
271
  void (*post_reader)(void);
272
  void (*post_handler)(char *content_type_dup, void *arg);
273
};
274
275
/* header_handler() constants */
276
396k
#define SAPI_HEADER_ADD     (1<<0)
277
278
279
0
#define SAPI_HEADER_SENT_SUCCESSFULLY 1
280
793k
#define SAPI_HEADER_DO_SEND       2
281
0
#define SAPI_HEADER_SEND_FAILED     3
282
283
0
#define SAPI_DEFAULT_MIMETYPE   "text/html"
284
0
#define SAPI_DEFAULT_CHARSET    PHP_DEFAULT_CHARSET
285
#define SAPI_PHP_VERSION_HEADER   "X-Powered-By: PHP/" PHP_VERSION
286
287
#define SAPI_POST_READER_FUNC(post_reader) void post_reader(void)
288
#define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg)
289
290
#define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray)
291
#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)
292
293
BEGIN_EXTERN_C()
294
SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data);
295
SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader);
296
SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data);
297
SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter);
298
END_EXTERN_C()
299
300
#define STANDARD_SAPI_MODULE_PROPERTIES \
301
  NULL, /* php_ini_path_override   */ \
302
  NULL, /* default_post_reader     */ \
303
  NULL, /* treat_data              */ \
304
  NULL, /* executable_location     */ \
305
  0,    /* php_ini_ignore          */ \
306
  0,    /* php_ini_ignore_cwd      */ \
307
  NULL, /* get_fd                  */ \
308
  NULL, /* force_http_10           */ \
309
  NULL, /* get_target_uid          */ \
310
  NULL, /* get_target_gid          */ \
311
  NULL, /* input_filter            */ \
312
  NULL, /* ini_defaults            */ \
313
  0,    /* phpinfo_as_text;        */ \
314
  NULL, /* ini_entries;            */ \
315
  NULL, /* additional_functions    */ \
316
  NULL  /* input_filter_init       */
317
318
#endif /* SAPI_H */