-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibfcgi.c
More file actions
executable file
·223 lines (183 loc) · 5.9 KB
/
libfcgi.c
File metadata and controls
executable file
·223 lines (183 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_output.h"
#include "php_libfcgi.h"
#include "php_variables.h"
#include "sapi/cli/cli.h"
#include <fcgiapp.h>
#include "SAPI.h"
#include "ext/session/php_session.h"
static zend_function_entry libfcgi_functions[] = {
PHP_FE(fcgi_is_cgi, NULL)
PHP_FE(fcgi_accept, NULL)
PHP_FE(fcgi_finish, NULL)
{NULL, NULL, NULL}
};
zend_module_entry libfcgi_module_entry = {
STANDARD_MODULE_HEADER,
PHP_LIBFCGI_EXTNAME,
libfcgi_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
PHP_LIBFCGI_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_LIBFCGI
ZEND_GET_MODULE(libfcgi)
#endif
FCGX_Request request;
int libfcgi_write(const char *str, uint str_length TSRMLS_DC)
{
return FCGX_PutStr(str, str_length, request.out);
}
void libfcgi_flush(void *server_context)
{
FCGX_FFlush(request.out);
}
char *libfcgi_getenv(char *name, size_t name_len TSRMLS_DC)
{
return getenv(name);
}
int libfcgi_send_headers(sapi_headers_struct *sapi_headers TSRMLS_DC)
{
zend_llist_position pos;
sapi_header_struct *header;
if (SG(request_info).no_headers == 1) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
if (sapi_headers->http_status_line || sapi_headers->http_response_code) {
zend_bool has_status = 0;
char *s, buf[256];
int len;
if (sapi_headers->http_status_line &&
(s = strchr(sapi_headers->http_status_line, ' ')) != 0 &&
(s - sapi_headers->http_status_line) >= 5 &&
strncasecmp(sapi_headers->http_status_line, "HTTP/", 5) == 0
) {
len = slprintf(buf, sizeof(buf), "Status:%s\r\n", s);
has_status = 1;
}
if (!has_status && sapi_headers->http_response_code) {
len = slprintf(buf, sizeof(buf), "Status: %d\r\n", sapi_headers->http_response_code);
}
PHPWRITE_H(buf, len);
}
for (
header = zend_llist_get_first_ex(&sapi_headers->headers, &pos);
header;
header = zend_llist_get_next_ex(&sapi_headers->headers, &pos)
) {
if (!header->header_len) {
continue;
}
PHPWRITE_H(header->header, header->header_len);
PHPWRITE_H("\r\n", 2);
}
PHPWRITE_H("\r\n", 2);
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
int libfcgi_read_post(char *buffer, uint count_bytes TSRMLS_DC)
{
return FCGX_GetStr(buffer, count_bytes, request.in);
}
char *libfcgi_read_cookies(TSRMLS_D)
{
return FCGX_GetParam("HTTP_COOKIE", request.envp);
}
void libfcgi_register_server_variables(zval *track_vars_array TSRMLS_DC)
{
char **param;
for (param = request.envp; param && *param; param++) {
char *name, *value;
int offset = strcspn(*param, "=");
name = *param;
unsigned int len;
name[offset] = '\0';
value = name + offset + 1;
len = strlen(value);
if (sapi_module.input_filter(PARSE_SERVER, name, &value, len, &len TSRMLS_CC)) {
php_register_variable(name, value, track_vars_array TSRMLS_CC);
}
}
}
int libfcgi_auto_global_reset(zend_auto_global *auto_global TSRMLS_DC)
{
//fprintf(stderr, "Reset %s\n", auto_global->name);
if (auto_global->auto_global_callback) {
auto_global->auto_global_callback(auto_global->name, auto_global->name_len TSRMLS_CC);
}
return 0;
}
int libfcgi_request_startup(zend_module_entry *module TSRMLS_DC) {
if (module->request_startup_func) {
module->request_startup_func(module->type, module->module_number TSRMLS_CC);
}
}
int libfcgi_request_shutdown(zend_module_entry *module TSRMLS_DC) {
if (module->request_shutdown_func) {
module->request_shutdown_func(module->type, module->module_number TSRMLS_CC);
}
}
void libfcgi_finish(TSRMLS_D)
{
php_output_flush_all(TSRMLS_C);
if (PS(session_status) == php_session_active) {
// need to destroy the current session
}
zend_hash_reverse_apply(&module_registry, (apply_func_t) libfcgi_request_shutdown TSRMLS_CC);
zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_FILES]);
sapi_deactivate(TSRMLS_C);
}
PHP_FUNCTION(fcgi_is_cgi)
{
RETURN_LONG(FCGX_IsCGI());
}
PHP_FUNCTION(fcgi_accept)
{
static zend_bool fcgi_is_ready = 0;
if (!fcgi_is_ready) {
if (FCGX_IsCGI()) {
php_error(E_WARNING, "fcgi_accept(): PHP must be run as a FastCGI application.\n");
RETURN_FALSE;
}
if (FCGX_Init()) {
RETURN_FALSE;
}
if (FCGX_InitRequest(&request, 0, 0)) {
RETURN_FALSE;
}
sapi_module.ub_write = libfcgi_write;
sapi_module.flush = libfcgi_flush;
sapi_module.getenv = libfcgi_getenv;
sapi_module.header_handler = NULL;
sapi_module.send_headers = libfcgi_send_headers;
sapi_module.send_header = NULL;
sapi_module.read_post = libfcgi_read_post;
sapi_module.read_cookies = libfcgi_read_cookies;
sapi_module.register_server_variables = libfcgi_register_server_variables;
sapi_module.input_filter_init = NULL;
fcgi_is_ready = 1;
}
libfcgi_finish(TSRMLS_C);
if (FCGX_Accept_r(&request)) {
RETURN_FALSE;
}
SG(server_context) = &request;
SG(request_info).query_string = FCGX_GetParam("QUERY_STRING", request.envp);
SG(request_info).request_method = FCGX_GetParam("REQUEST_METHOD", request.envp);
SG(request_info).content_type = FCGX_GetParam("CONTENT_TYPE", request.envp);
sapi_activate(TSRMLS_C);
zend_hash_apply(&module_registry, (apply_func_t) libfcgi_request_startup TSRMLS_CC);
zend_hash_apply(CG(auto_globals), (apply_func_t) libfcgi_auto_global_reset TSRMLS_CC);
RETURN_TRUE;
}
PHP_FUNCTION(fcgi_finish)
{
libfcgi_finish(TSRMLS_C);
FCGX_Finish_r(&request);
}