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