forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastCGIProxy.java
More file actions
249 lines (211 loc) · 7.99 KB
/
Copy pathFastCGIProxy.java
File metadata and controls
249 lines (211 loc) · 7.99 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.script;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import php.java.bridge.ILogger;
import php.java.bridge.Util;
import php.java.bridge.http.FCGIConnectException;
import php.java.bridge.http.FCGIConnection;
import php.java.bridge.http.FCGIConnectionFactory;
import php.java.bridge.http.FCGIConnectionPool;
import php.java.bridge.http.FCGIIOFactory;
import php.java.bridge.http.FCGIInputStream;
import php.java.bridge.http.FCGIOutputStream;
import php.java.bridge.http.FCGIUtil;
import php.java.bridge.http.HeaderParser;
import php.java.bridge.http.IFCGIProcess;
import php.java.bridge.http.IFCGIProcessFactory;
import php.java.bridge.http.OutputStreamFactory;
/**
* This class can be used to run (and to connect to) a FastCGI server.
*
* @author jostb
*
* @see php.java.script.servlet.HttpFastCGIProxy
*/
public class FastCGIProxy extends Continuation implements IFCGIProcessFactory {
private static final String PROCESSES = Util.THREAD_POOL_MAX_SIZE; // PROCESSES must == Util.THREAD_POOL_MAX_SIZE
private static final String MAX_REQUESTS = FCGIUtil.PHP_FCGI_MAX_REQUESTS;
private static final String CGI_DIR = Util.TMPDIR.getAbsolutePath();
private static final boolean PHP_INCLUDE_JAVA = false; // servlet option
public FastCGIProxy(Reader reader, Map env, OutputStream out,
OutputStream err, HeaderParser headerParser,
ResultProxy resultProxy, ILogger logger) {
super(env, out, err, headerParser, resultProxy);
}
private FCGIConnectionFactory channelName;
static final HashMap PROCESS_ENVIRONMENT = getProcessEnvironment();
private static HashMap getProcessEnvironment() {
HashMap map = new HashMap(Util.COMMON_ENVIRONMENT);
return map;
}
private final FCGIIOFactory defaultPoolFactory = new FCGIIOFactory() {
public InputStream createInputStream() { return new FCGIInputStream(FastCGIProxy.this); }
public OutputStream createOutputStream() { return new FCGIOutputStream(); }
public FCGIConnection connect(FCGIConnectionFactory name) throws FCGIConnectException {
return name.connect();
}
};
private FCGIConnectionPool createConnectionPool(int children) throws FCGIConnectException {
channelName = FCGIConnectionFactory.createChannelFactory(this, false);
channelName.findFreePort(true);
channelName.initialize();
File cgiOsDir = Util.TMPDIR;
File javaIncFile = new File (cgiOsDir, "launcher.sh");
if (Util.USE_SH_WRAPPER) {
try {
if (!javaIncFile.exists()) {
Field f = Util.LAUNCHER_UNIX.getField("bytes");
byte[] buf = (byte[]) f.get(Util.LAUNCHER_UNIX);
OutputStream out = new FileOutputStream (javaIncFile);
out.write(buf);
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
File javaProxyFile = new File (cgiOsDir, "launcher.exe");
if (!Util.USE_SH_WRAPPER) {
try {
if (!javaProxyFile.exists()) {
Field f = Util.LAUNCHER_WINDOWS.getField("bytes");
Field f2 = Util.LAUNCHER_WINDOWS2.getField("bytes");
Field f3 = Util.LAUNCHER_WINDOWS3.getField("bytes");
Field f4 = Util.LAUNCHER_WINDOWS4.getField("bytes");
byte[] buf = (byte[]) f.get(Util.LAUNCHER_WINDOWS);
byte[] buf2 = (byte[]) f2.get(Util.LAUNCHER_WINDOWS2);
byte[] buf3 = (byte[]) f3.get(Util.LAUNCHER_WINDOWS3);
byte[] buf4 = (byte[]) f4.get(Util.LAUNCHER_WINDOWS4);
OutputStream out = new FileOutputStream (javaProxyFile);
out.write(buf);
out.write(buf2);
out.write(buf3);
out.write(buf4);
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Start the launcher.exe or launcher.sh
Map map = (Map) PROCESS_ENVIRONMENT.clone();
map.put("PHP_FCGI_CHILDREN", PROCESSES);
map.put("PHP_FCGI_MAX_REQUESTS", MAX_REQUESTS);
channelName.startServer(Util.getLogger());
return new FCGIConnectionPool(channelName, children,
Integer.parseInt(MAX_REQUESTS),
defaultPoolFactory,
Integer.parseInt(FCGIUtil.PHP_FCGI_CONNECTION_POOL_TIMEOUT));
}
private static final Object globalCtxLock = new Object();
private static FCGIConnectionPool fcgiConnectionPool = null;
protected void setupFastCGIServer() throws FCGIConnectException {
synchronized(globalCtxLock) {
if(null == fcgiConnectionPool) {
Util.fcgiConnectionPool = fcgiConnectionPool = createConnectionPool(Integer.parseInt(PROCESSES));
}
}
}
protected void doRun() throws IOException, Util.Process.PhpException {
byte[] buf = new byte[FCGIUtil.FCGI_BUF_SIZE];
setupFastCGIServer();
FCGIInputStream natIn = null;
FCGIOutputStream natOut = null;
FCGIConnectionPool.Connection connection = null;
try {
connection = fcgiConnectionPool.openConnection();
natOut = (FCGIOutputStream) connection.getOutputStream();
natIn = (FCGIInputStream) connection.getInputStream();
natOut.writeBegin();
natOut.writeParams(env);
natOut.write(FCGIUtil.FCGI_STDIN, FCGIUtil.FCGI_EMPTY_RECORD);
natOut.close(); natOut = null;
HeaderParser.parseBody(buf, natIn, new OutputStreamFactory() { public OutputStream getOutputStream() throws IOException {return out;}}, headerParser);
natIn.close(); natIn = null;
connection = null;
} catch (InterruptedException e) {
/*ignore*/
} catch (Throwable t) {
Util.printStackTrace(t);
} finally {
if(connection!=null) connection.setIsClosed();
if(natIn!=null) try {natIn.close();} catch (IOException e) {}
if(natOut!=null) try {natOut.close();} catch (IOException e) {}
}
}
/** required by IFCGIProcessFactory */
/** {@inheritDoc} */
public IFCGIProcess createFCGIProcess(String[] args, boolean includeJava, File home, Map env) throws IOException {
return new FCGIProcess(args, includeJava, getCgiDir(), getPearDir(), getWebInfDir(), home, env,
getCgiDir(), true, true);
}
/** {@inheritDoc} */
public boolean canStartFCGI() {
return true;
}
/** {@inheritDoc} */
public String getCgiDir() {
return CGI_DIR;
}
/** {@inheritDoc} */
public HashMap getEnvironment() {
return getProcessEnvironment();
}
/** {@inheritDoc} */
public String getPearDir() {
return CGI_DIR;
}
/** {@inheritDoc} */
public String getPhp() {
return null;
}
/** {@inheritDoc} */
public String getPhpConnectionPoolSize() {
return PROCESSES;
}
/** {@inheritDoc} */
public boolean getPhpIncludeJava() {
return PHP_INCLUDE_JAVA;
}
/** {@inheritDoc} */
public String getPhpMaxRequests() {
return MAX_REQUESTS;
}
/** {@inheritDoc} */
public String getWebInfDir() {
return CGI_DIR;
}
/** {@inheritDoc} */
public void log(String msg) {
Util.logMessage(msg);
}
}