xref: /PHP-7.4/main/fastcgi.h (revision 92ac598a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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    | Authors: Dmitry Stogov <dmitry@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 /* FastCGI protocol */
20 
21 #define FCGI_VERSION_1 1
22 
23 #define FCGI_MAX_LENGTH 0xffff
24 
25 #define FCGI_KEEP_CONN  1
26 
27 /* this is near the perfect hash function for most useful FastCGI variables
28  * which combines efficiency and minimal hash collisions
29  */
30 
31 #define FCGI_HASH_FUNC(var, var_len) \
32 	(UNEXPECTED(var_len < 3) ? (unsigned int)var_len : \
33 		(((unsigned int)var[3]) << 2) + \
34 		(((unsigned int)var[var_len-2]) << 4) + \
35 		(((unsigned int)var[var_len-1]) << 2) + \
36 		var_len)
37 
38 #define FCGI_GETENV(request, name) \
39 	fcgi_quick_getenv(request, name, sizeof(name)-1, FCGI_HASH_FUNC(name, sizeof(name)-1))
40 
41 #define FCGI_PUTENV(request, name, value) \
42 	fcgi_quick_putenv(request, name, sizeof(name)-1, FCGI_HASH_FUNC(name, sizeof(name)-1), value)
43 
44 typedef enum _fcgi_role {
45 	FCGI_RESPONDER	= 1,
46 	FCGI_AUTHORIZER	= 2,
47 	FCGI_FILTER		= 3
48 } fcgi_role;
49 
50 enum {
51 	FCGI_DEBUG		= 1,
52 	FCGI_NOTICE		= 2,
53 	FCGI_WARNING	= 3,
54 	FCGI_ERROR		= 4,
55 	FCGI_ALERT		= 5,
56 };
57 
58 typedef enum _fcgi_request_type {
59 	FCGI_BEGIN_REQUEST		=  1, /* [in]                              */
60 	FCGI_ABORT_REQUEST		=  2, /* [in]  (not supported)             */
61 	FCGI_END_REQUEST		=  3, /* [out]                             */
62 	FCGI_PARAMS				=  4, /* [in]  environment variables       */
63 	FCGI_STDIN				=  5, /* [in]  post data                   */
64 	FCGI_STDOUT				=  6, /* [out] response                    */
65 	FCGI_STDERR				=  7, /* [out] errors                      */
66 	FCGI_DATA				=  8, /* [in]  filter data (not supported) */
67 	FCGI_GET_VALUES			=  9, /* [in]                              */
68 	FCGI_GET_VALUES_RESULT	= 10  /* [out]                             */
69 } fcgi_request_type;
70 
71 typedef enum _fcgi_protocol_status {
72 	FCGI_REQUEST_COMPLETE	= 0,
73 	FCGI_CANT_MPX_CONN		= 1,
74 	FCGI_OVERLOADED			= 2,
75 	FCGI_UNKNOWN_ROLE		= 3
76 } dcgi_protocol_status;
77 
78 /* FastCGI client API */
79 
80 typedef void (*fcgi_apply_func)(char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg);
81 
82 #define FCGI_HASH_TABLE_SIZE 128
83 #define FCGI_HASH_TABLE_MASK (FCGI_HASH_TABLE_SIZE - 1)
84 #define FCGI_HASH_SEG_SIZE   4096
85 
86 typedef struct _fcgi_request fcgi_request;
87 
88 int fcgi_init(void);
89 void fcgi_shutdown(void);
90 int fcgi_is_fastcgi(void);
91 int fcgi_is_closed(fcgi_request *req);
92 void fcgi_close(fcgi_request *req, int force, int destroy);
93 int fcgi_in_shutdown(void);
94 void fcgi_terminate(void);
95 int fcgi_listen(const char *path, int backlog);
96 fcgi_request* fcgi_init_request(int listen_socket, void(*on_accept)(), void(*on_read)(), void(*on_close)());
97 void fcgi_destroy_request(fcgi_request *req);
98 void fcgi_set_allowed_clients(char *ip);
99 int fcgi_accept_request(fcgi_request *req);
100 int fcgi_finish_request(fcgi_request *req, int force_close);
101 const char *fcgi_get_last_client_ip();
102 void fcgi_set_in_shutdown(int new_value);
103 void fcgi_request_set_keep(fcgi_request *req, int new_value);
104 
105 #ifndef HAVE_ATTRIBUTE_WEAK
106 typedef void (*fcgi_logger)(int type, const char *fmt, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
107 void fcgi_set_logger(fcgi_logger lg);
108 #endif
109 
110 int   fcgi_has_env(fcgi_request *req);
111 char* fcgi_getenv(fcgi_request *req, const char* var, int var_len);
112 char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val);
113 char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value);
114 char* fcgi_quick_putenv(fcgi_request *req, char* var, int var_len, unsigned int hash_value, char* val);
115 void  fcgi_loadenv(fcgi_request *req, fcgi_apply_func load_func, zval *array);
116 
117 int fcgi_read(fcgi_request *req, char *str, int len);
118 
119 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len);
120 int fcgi_flush(fcgi_request *req, int end);
121 int fcgi_end(fcgi_request *req);
122 
123 #ifdef PHP_WIN32
124 void fcgi_impersonate(void);
125 #endif
126 
127 void fcgi_set_mgmt_var(const char * name, size_t name_len, const char * value, size_t value_len);
128 void fcgi_free_mgmt_var_cb(zval *zv);
129