SAPI.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #ifndef SAPI_H
  17. #define SAPI_H
  18. #include "php.h"
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. #include "zend_llist.h"
  22. #include "zend_operators.h"
  23. #include <sys/stat.h>
  24. #define SAPI_OPTION_NO_CHDIR 1
  25. #define SAPI_POST_BLOCK_SIZE 0x4000
  26. #ifdef PHP_WIN32
  27. # ifdef SAPI_EXPORTS
  28. # define SAPI_API __declspec(dllexport)
  29. # else
  30. # define SAPI_API __declspec(dllimport)
  31. # endif
  32. #elif defined(__GNUC__) && __GNUC__ >= 4
  33. # define SAPI_API __attribute__ ((visibility("default")))
  34. #else
  35. # define SAPI_API
  36. #endif
  37. #undef shutdown
  38. typedef struct {
  39. char *header;
  40. size_t header_len;
  41. } sapi_header_struct;
  42. typedef struct {
  43. zend_llist headers;
  44. int http_response_code;
  45. unsigned char send_default_content_type;
  46. char *mimetype;
  47. char *http_status_line;
  48. } sapi_headers_struct;
  49. typedef struct _sapi_post_entry sapi_post_entry;
  50. typedef struct _sapi_module_struct sapi_module_struct;
  51. BEGIN_EXTERN_C()
  52. extern SAPI_API sapi_module_struct sapi_module; /* true global */
  53. END_EXTERN_C()
  54. /* Some values in this structure needs to be filled in before
  55. * calling sapi_activate(). We WILL change the `char *' entries,
  56. * so make sure that you allocate a separate buffer for them
  57. * and that you free them after sapi_deactivate().
  58. */
  59. typedef struct {
  60. const char *request_method;
  61. char *query_string;
  62. char *cookie_data;
  63. zend_long content_length;
  64. char *path_translated;
  65. char *request_uri;
  66. /* Do not use request_body directly, but the php://input stream wrapper instead */
  67. struct _php_stream *request_body;
  68. const char *content_type;
  69. bool headers_only;
  70. bool no_headers;
  71. bool headers_read;
  72. sapi_post_entry *post_entry;
  73. char *content_type_dup;
  74. /* for HTTP authentication */
  75. char *auth_user;
  76. char *auth_password;
  77. char *auth_digest;
  78. /* this is necessary for the CGI SAPI module */
  79. char *argv0;
  80. char *current_user;
  81. int current_user_length;
  82. /* this is necessary for CLI module */
  83. int argc;
  84. char **argv;
  85. int proto_num;
  86. } sapi_request_info;
  87. typedef struct _sapi_globals_struct {
  88. void *server_context;
  89. sapi_request_info request_info;
  90. sapi_headers_struct sapi_headers;
  91. int64_t read_post_bytes;
  92. unsigned char post_read;
  93. unsigned char headers_sent;
  94. zend_stat_t global_stat;
  95. char *default_mimetype;
  96. char *default_charset;
  97. HashTable *rfc1867_uploaded_files;
  98. zend_long post_max_size;
  99. int options;
  100. bool sapi_started;
  101. double global_request_time;
  102. HashTable known_post_content_types;
  103. zval callback_func;
  104. zend_fcall_info_cache fci_cache;
  105. } sapi_globals_struct;
  106. BEGIN_EXTERN_C()
  107. #ifdef ZTS
  108. # define SG(v) ZEND_TSRMG_FAST(sapi_globals_offset, sapi_globals_struct *, v)
  109. SAPI_API extern int sapi_globals_id;
  110. SAPI_API extern size_t sapi_globals_offset;
  111. #else
  112. # define SG(v) (sapi_globals.v)
  113. extern SAPI_API sapi_globals_struct sapi_globals;
  114. #endif
  115. SAPI_API void sapi_startup(sapi_module_struct *sf);
  116. SAPI_API void sapi_shutdown(void);
  117. SAPI_API void sapi_activate(void);
  118. SAPI_API void sapi_deactivate_module(void);
  119. SAPI_API void sapi_deactivate_destroy(void);
  120. SAPI_API void sapi_deactivate(void);
  121. SAPI_API void sapi_initialize_empty_request(void);
  122. SAPI_API void sapi_add_request_header(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg);
  123. END_EXTERN_C()
  124. /*
  125. * This is the preferred and maintained API for
  126. * operating on HTTP headers.
  127. */
  128. /*
  129. * Always specify a sapi_header_line this way:
  130. *
  131. * sapi_header_line ctr = {0};
  132. */
  133. typedef struct {
  134. const char *line; /* If you allocated this, you need to free it yourself */
  135. size_t line_len;
  136. zend_long response_code; /* long due to zend_parse_parameters compatibility */
  137. } sapi_header_line;
  138. typedef enum { /* Parameter: */
  139. SAPI_HEADER_REPLACE, /* sapi_header_line* */
  140. SAPI_HEADER_ADD, /* sapi_header_line* */
  141. SAPI_HEADER_DELETE, /* sapi_header_line* */
  142. SAPI_HEADER_DELETE_ALL, /* void */
  143. SAPI_HEADER_SET_STATUS /* int */
  144. } sapi_header_op_enum;
  145. BEGIN_EXTERN_C()
  146. SAPI_API int sapi_header_op(sapi_header_op_enum op, void *arg);
  147. /* Deprecated functions. Use sapi_header_op instead. */
  148. SAPI_API int sapi_add_header_ex(const char *header_line, size_t header_line_len, bool duplicate, bool replace);
  149. #define sapi_add_header(a, b, c) sapi_add_header_ex((a),(b),(c),1)
  150. SAPI_API int sapi_send_headers(void);
  151. SAPI_API void sapi_free_header(sapi_header_struct *sapi_header);
  152. SAPI_API void sapi_handle_post(void *arg);
  153. SAPI_API size_t sapi_read_post_block(char *buffer, size_t buflen);
  154. SAPI_API int sapi_register_post_entries(const sapi_post_entry *post_entry);
  155. SAPI_API int sapi_register_post_entry(const sapi_post_entry *post_entry);
  156. SAPI_API void sapi_unregister_post_entry(const sapi_post_entry *post_entry);
  157. SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(void));
  158. SAPI_API int sapi_register_treat_data(void (*treat_data)(int arg, char *str, zval *destArray));
  159. 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));
  160. SAPI_API int sapi_flush(void);
  161. SAPI_API zend_stat_t *sapi_get_stat(void);
  162. SAPI_API char *sapi_getenv(const char *name, size_t name_len);
  163. SAPI_API char *sapi_get_default_content_type(void);
  164. SAPI_API void sapi_get_default_content_type_header(sapi_header_struct *default_header);
  165. SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len);
  166. SAPI_API void sapi_activate_headers_only(void);
  167. SAPI_API int sapi_get_fd(int *fd);
  168. SAPI_API int sapi_force_http_10(void);
  169. SAPI_API int sapi_get_target_uid(uid_t *);
  170. SAPI_API int sapi_get_target_gid(gid_t *);
  171. SAPI_API double sapi_get_request_time(void);
  172. SAPI_API void sapi_terminate_process(void);
  173. END_EXTERN_C()
  174. struct _sapi_module_struct {
  175. char *name;
  176. char *pretty_name;
  177. int (*startup)(struct _sapi_module_struct *sapi_module);
  178. int (*shutdown)(struct _sapi_module_struct *sapi_module);
  179. int (*activate)(void);
  180. int (*deactivate)(void);
  181. size_t (*ub_write)(const char *str, size_t str_length);
  182. void (*flush)(void *server_context);
  183. zend_stat_t *(*get_stat)(void);
  184. char *(*getenv)(const char *name, size_t name_len);
  185. void (*sapi_error)(int type, const char *error_msg, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
  186. int (*header_handler)(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers);
  187. int (*send_headers)(sapi_headers_struct *sapi_headers);
  188. void (*send_header)(sapi_header_struct *sapi_header, void *server_context);
  189. size_t (*read_post)(char *buffer, size_t count_bytes);
  190. char *(*read_cookies)(void);
  191. void (*register_server_variables)(zval *track_vars_array);
  192. void (*log_message)(const char *message, int syslog_type_int);
  193. double (*get_request_time)(void);
  194. void (*terminate_process)(void);
  195. char *php_ini_path_override;
  196. void (*default_post_reader)(void);
  197. void (*treat_data)(int arg, char *str, zval *destArray);
  198. char *executable_location;
  199. int php_ini_ignore;
  200. int php_ini_ignore_cwd; /* don't look for php.ini in the current directory */
  201. int (*get_fd)(int *fd);
  202. int (*force_http_10)(void);
  203. int (*get_target_uid)(uid_t *);
  204. int (*get_target_gid)(gid_t *);
  205. unsigned int (*input_filter)(int arg, const char *var, char **val, size_t val_len, size_t *new_val_len);
  206. void (*ini_defaults)(HashTable *configuration_hash);
  207. int phpinfo_as_text;
  208. char *ini_entries;
  209. const zend_function_entry *additional_functions;
  210. unsigned int (*input_filter_init)(void);
  211. };
  212. struct _sapi_post_entry {
  213. char *content_type;
  214. uint32_t content_type_len;
  215. void (*post_reader)(void);
  216. void (*post_handler)(char *content_type_dup, void *arg);
  217. };
  218. /* header_handler() constants */
  219. #define SAPI_HEADER_ADD (1<<0)
  220. #define SAPI_HEADER_SENT_SUCCESSFULLY 1
  221. #define SAPI_HEADER_DO_SEND 2
  222. #define SAPI_HEADER_SEND_FAILED 3
  223. #define SAPI_DEFAULT_MIMETYPE "text/html"
  224. #define SAPI_DEFAULT_CHARSET PHP_DEFAULT_CHARSET
  225. #define SAPI_PHP_VERSION_HEADER "X-Powered-By: PHP/" PHP_VERSION
  226. #define SAPI_POST_READER_FUNC(post_reader) void post_reader(void)
  227. #define SAPI_POST_HANDLER_FUNC(post_handler) void post_handler(char *content_type_dup, void *arg)
  228. #define SAPI_TREAT_DATA_FUNC(treat_data) void treat_data(int arg, char *str, zval* destArray)
  229. #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)
  230. BEGIN_EXTERN_C()
  231. SAPI_API SAPI_POST_READER_FUNC(sapi_read_standard_form_data);
  232. SAPI_API SAPI_POST_READER_FUNC(php_default_post_reader);
  233. SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data);
  234. SAPI_API SAPI_INPUT_FILTER_FUNC(php_default_input_filter);
  235. END_EXTERN_C()
  236. #define STANDARD_SAPI_MODULE_PROPERTIES \
  237. NULL, /* php_ini_path_override */ \
  238. NULL, /* default_post_reader */ \
  239. NULL, /* treat_data */ \
  240. NULL, /* executable_location */ \
  241. 0, /* php_ini_ignore */ \
  242. 0, /* php_ini_ignore_cwd */ \
  243. NULL, /* get_fd */ \
  244. NULL, /* force_http_10 */ \
  245. NULL, /* get_target_uid */ \
  246. NULL, /* get_target_gid */ \
  247. NULL, /* input_filter */ \
  248. NULL, /* ini_defaults */ \
  249. 0, /* phpinfo_as_text; */ \
  250. NULL, /* ini_entries; */ \
  251. NULL, /* additional_functions */ \
  252. NULL /* input_filter_init */
  253. #endif /* SAPI_H */