forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.java
More file actions
327 lines (303 loc) · 9.9 KB
/
Copy pathHttpServer.java
File metadata and controls
327 lines (303 loc) · 9.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge.http;
/*
* 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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import php.java.bridge.ISocketFactory;
import php.java.bridge.AppThreadPool;
import php.java.bridge.Util;
/**
* This class can be used to create a simple HTTP server. It is used
* when running local scripts like <code>e.eval(new
* StringReader('<?php phpinfo(); ?>'));</code>. For remote
* scripts use a HttpProxy and URLReader instead.
*
* @author jostb
*
* @see php.java.bridge.http.HttpRequest
* @see php.java.bridge.http.HttpResponse
*
* @see php.java.script.Continuation
* @see php.java.script.URLReader
* @see php.java.script.Continuation
*
*/
public abstract class HttpServer implements Runnable {
/** Request method GET */
public static final String PUT = "PUT";
/** Request method PUT */
public static final String GET = "GET";
/** Request method POST */
public static final String POST = "POST";
protected ISocketFactory socket;
protected Thread httpServer;
private AppThreadPool pool;
protected boolean isSecure;
/**
* Create a server socket.
* @param addr The host address, either INET:port or INET_LOCAL:port
* @return The server socket.
* @throws IOException
*/
public abstract ISocketFactory bind(String addr) throws IOException;
/**
* Create a server socket.
* @param addr The host address, either INET:port or INET_LOCAL:port
* @return The server socket.
* @throws IOException
*/
public abstract ISocketFactory bindSecure(String addr) throws IOException;
/**
* Create a new HTTP Server.
* @throws IOException
* @see HttpServer#destroy()
*/
protected HttpServer() throws IOException {
this(null);
}
/**
* Create a new HTTP Server.
* @param serverPort The port# as a string. Prefix may be INET: or INET_LOCAL:
* @param isSecure use https instead of http
* @throws IOException
* @see HttpServer#destroy()
*/
protected HttpServer(String serverPort, boolean isSecure) throws IOException {
this.isSecure = isSecure;
if(serverPort==null) serverPort = "0";
if(!serverPort.startsWith("INET")) serverPort = (Util.JAVABRIDGE_PROMISCUOUS ? "INET:" : "INET_LOCAL:") + serverPort;
socket = isSecure ? bindSecure(serverPort) : bind(serverPort);
try {
pool = createThreadPool("JavaBridgeHttpServerThreadPool");
} catch (SecurityException e) {/*ignore*/}
httpServer = new Util.Thread(this, "JavaBridgeHttpServer");
httpServer.start();
}
/**
* Create a new HTTP Server.
* @param serverPort The port# as a string. Prefix may be INET: or INET_LOCAL:
* @param isSecure
* @throws IOException
* @see HttpServer#destroy()
*/
protected HttpServer(String serverPort) throws IOException {
this(serverPort, false);
}
/**
* Same as Util.createThreadPool, but does not log anything at this stage.
* @param name The name of the pool
* @return The thread pool instance.
*/
private AppThreadPool createThreadPool(String name) {
AppThreadPool pool = null;
int maxSize = 20;
try { maxSize = Integer.parseInt(Util.THREAD_POOL_MAX_SIZE); } catch (Throwable t) {/*ignore*/}
if(maxSize>0) pool = new AppThreadPool(name, maxSize);
return pool;
}
/**
* Parse the header. After that <code>req</code> contains the body.
* @param req The HttpRequest
* @throws UnsupportedEncodingException
* @throws IOException
* @return true if the header has been parsed successfully, false otherwise
*/
protected boolean parseHeader(HttpRequest req) throws UnsupportedEncodingException, IOException {
byte buf[] = new byte[Util.BUF_SIZE];
InputStream natIn = req.getInputStream();
int i=0, n, s=0;
boolean eoh=false;
boolean rn = false;
String remain = null;
String line;
// the header and content
while((n = natIn.read(buf, i, buf.length-i)) !=-1 ) {
int N = i + n;
// header
while(!eoh && i<N) {
switch(buf[i++]) {
case '\n':
if(rn) {
eoh=true;
} else {
if (remain != null) {
line = remain + new String(buf, s, i-s, Util.ASCII);
line = line.substring(0, line.length()-2);
remain = null;
} else {
line = new String(buf, s, i-s-2, Util.ASCII);
}
req.addHeader(line);
s=i;
}
rn=true;
break;
case '\r': break;
default: rn=false;
}
}
// body
if(eoh) {
req.pushBack(buf, i, N-i);
return true;
} else {
if (remain != null) {
remain += new String(buf, s, i-s, Util.ASCII);
} else {
remain = new String(buf, s, i-s, Util.ASCII);
}
s = i = 0;
}
}
return false;
}
private class Runner implements Runnable {
private Socket sock;
private HttpRequest req;
private HttpResponse res;
public Runner(Socket sock) throws IOException {
this.sock = sock;
this.req = new HttpRequest(sock.getInputStream());
this.res = new HttpResponse(sock.getOutputStream());
}
public void run() {
try {
if(parseHeader(req)) service(req, res);
} catch (IOException e) {
Util.printStackTrace(e);
} finally {
try {this.req.close(); } catch (IOException e) {/*ignore*/}
try {this.res.close(); } catch (IOException e) {/*ignore*/}
try {this.sock.close();} catch (IOException e) {/*ignore*/}
}
}
}
/**
* accept, create a HTTP request and response, parse the header and body
* @throws IOException
*/
protected void doRun() throws IOException {
while(true) {
Socket sock;
try {
sock = socket.accept();
} catch (java.net.SocketException e) {
return; // socket closed
} catch (IOException e) {
Util.printStackTrace(e);
return;
}
Util.logDebug("Socket connection accepted");
if(pool==null) {
Util.logDebug("Starting new HTTP server thread");
(new Util.Thread(new Runner(sock), Util.EXTENSION_NAME+"HttpServerRunner")).start();
} else {
Util.logDebug("Starting HTTP server thread from thread pool");
pool.start(new Runner(sock));
}
}
}
protected static final byte[] ERROR_UNAVAIL = Util.toBytes(
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"+
"<html><head>" +
"<title>503 Service Unavailable</title>" +
"</head><body>" +
"<h1>Out of system resources</h1>" +
"<p>Try again shortly or use the Apache or IIS front end instead.</p>" +
"<hr>"+
"</body></html>");
protected void writeServiceUnavailable(HttpRequest req, HttpResponse res) throws IOException {
res.setContentLength(ERROR_UNAVAIL.length);
OutputStream out = res.getOutputStream();
out.write(ERROR_UNAVAIL);
}
protected static final byte[] ERROR = Util.toBytes(
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"+
"<html><head>" +
"<title>404 Not Found</title>" +
"</head><body>" +
"<h1>Not Found</h1>" +
"<p>The requested URL was not found on this server.</p>" +
"<hr>"+
"</body></html>");
protected void writeError(HttpRequest req, HttpResponse res) throws IOException {
res.setContentLength(ERROR.length);
OutputStream out = res.getOutputStream();
out.write(ERROR);
}
protected void doPost(HttpRequest req, HttpResponse res) throws IOException {
writeError(req, res);
}
protected void doGet(HttpRequest req, HttpResponse res) throws IOException {
writeError(req, res);
}
protected void doPut(HttpRequest req, HttpResponse res) throws IOException {
writeError(req, res);
}
/**
* Sets the content length but leaves the rest of the body untouched.
*/
protected void service(HttpRequest req, HttpResponse res) throws IOException {
String contentLength = req.getHeader("Content-Length");
if(contentLength==null) req.setContentLength(-1);
else req.setContentLength(Integer.parseInt(contentLength));
String method = req.getMethod();
if(method == PUT) doPut(req, res);
else if(method == GET) doGet(req, res);
else if(method == POST) doPost(req, res);
}
/**{@inheritDoc}*/
public void run() {
try {
doRun();
} catch (IOException e) {
Util.printStackTrace(e);
}
}
/**
* Stop the HTTP server.
*
*/
public void destroy() {
try {
socket.close();
} catch (Exception e) {
Util.printStackTrace(e);
}
try {
if(pool!=null) pool.destroy();
} catch (Exception e) {
Util.printStackTrace(e);
}
}
/**
* Returns the server socket.
* @return The server socket.
*/
public ISocketFactory getSocket() {
return socket;
}
}