SAPI.h 9.9 KB

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