1 |
tdb |
1.8 |
/* |
2 |
tdb |
1.9 |
* i-scream central monitoring system |
3 |
|
|
* http://www.i-scream.org.uk |
4 |
tdb |
1.8 |
* Copyright (C) 2000-2002 i-scream |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
tdb |
1.1 |
//---PACKAGE DECLARATION--- |
22 |
tdb |
1.6 |
package uk.org.iscream.cms.corbaservices; |
23 |
tdb |
1.1 |
|
24 |
|
|
//---IMPORTS--- |
25 |
|
|
import java.net.*; |
26 |
|
|
import java.io.*; |
27 |
|
|
import java.util.*; |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* MiniWebServer |
31 |
|
|
* |
32 |
tdb |
1.3 |
* This provides a very basic webserver for serving IOR files. |
33 |
|
|
* This could easily serve other text files, but not binaries. |
34 |
|
|
* |
35 |
tdb |
1.7 |
* @author $Author: tdb $ |
36 |
tdb |
1.9 |
* @version $Id: MiniWebServer.java,v 1.8 2002/05/18 18:15:56 tdb Exp $ |
37 |
tdb |
1.1 |
*/ |
38 |
|
|
public class MiniWebServer extends Thread { |
39 |
|
|
|
40 |
|
|
//---FINAL ATTRIBUTES--- |
41 |
|
|
|
42 |
|
|
/** |
43 |
|
|
* The current CVS revision of this class |
44 |
|
|
*/ |
45 |
tdb |
1.9 |
public static final String REVISION = "$Revision: 1.8 $"; |
46 |
tdb |
1.1 |
|
47 |
|
|
//---STATIC METHODS--- |
48 |
|
|
|
49 |
|
|
//---CONSTRUCTORS--- |
50 |
tdb |
1.3 |
|
51 |
|
|
/** |
52 |
|
|
* Construct a new MiniWebServer |
53 |
|
|
* |
54 |
|
|
* @param port The port to bind to |
55 |
|
|
* @param okUrlsConfig The URL list config file |
56 |
|
|
* @param webDir The web directory |
57 |
|
|
*/ |
58 |
tdb |
1.2 |
public MiniWebServer(int port, String okUrlsConfig, String webDir) { |
59 |
tdb |
1.1 |
_port = port; |
60 |
|
|
_okUrlsConfig = okUrlsConfig; |
61 |
tdb |
1.2 |
_webDir = webDir; |
62 |
tdb |
1.1 |
} |
63 |
|
|
|
64 |
|
|
//---PUBLIC METHODS--- |
65 |
|
|
|
66 |
tdb |
1.3 |
/** |
67 |
|
|
* Main thread of execution. Keeps looping spawning off |
68 |
|
|
* Handler's to deal with each client. This should keep |
69 |
|
|
* going, and extra care has been taking with error |
70 |
|
|
* catching. |
71 |
|
|
*/ |
72 |
tdb |
1.1 |
public void run() { |
73 |
|
|
// carry on forever |
74 |
|
|
while(true) { |
75 |
|
|
boolean run = true; |
76 |
|
|
ServerSocket serverSocket = null; |
77 |
|
|
try { |
78 |
|
|
serverSocket = new ServerSocket(_port); |
79 |
|
|
} catch (IOException e) { |
80 |
|
|
run = false; |
81 |
|
|
System.out.println(e); |
82 |
|
|
e.printStackTrace(); |
83 |
|
|
} |
84 |
|
|
while(run) { |
85 |
|
|
try { |
86 |
tdb |
1.3 |
// get a new client and give it to a Handler |
87 |
tdb |
1.1 |
Socket socket = serverSocket.accept(); |
88 |
|
|
Handler handler = new Handler(socket); |
89 |
|
|
handler.start(); |
90 |
|
|
} catch (IOException e) { |
91 |
|
|
run=false; |
92 |
|
|
System.out.println(e); |
93 |
|
|
e.printStackTrace(); |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
//---PRIVATE METHODS--- |
100 |
|
|
|
101 |
tdb |
1.3 |
/** |
102 |
|
|
* Reads a file in given an URL. Very basic, and has only |
103 |
|
|
* been tested to work for URL's in the / directory. |
104 |
|
|
* |
105 |
|
|
* @param url The URL given to the webserver |
106 |
|
|
* @return A String containing data from the file |
107 |
|
|
*/ |
108 |
tdb |
1.1 |
private String readUrl(String url) { |
109 |
tdb |
1.3 |
// trim off the initial / |
110 |
tdb |
1.1 |
if(url.startsWith("/")) { |
111 |
|
|
url = url.substring(1); |
112 |
|
|
} |
113 |
|
|
try { |
114 |
tdb |
1.3 |
// need to prefix with web directory |
115 |
tdb |
1.2 |
File file = new File(_webDir+"/"+url); |
116 |
tdb |
1.1 |
if(file.exists() && file.canRead()) { |
117 |
tdb |
1.3 |
// read in all the data |
118 |
tdb |
1.1 |
String data = ""; |
119 |
|
|
BufferedReader reader = new BufferedReader(new FileReader(file)); |
120 |
|
|
while(reader.ready()) { |
121 |
tdb |
1.4 |
String line = reader.readLine(); |
122 |
|
|
if(line != null) { |
123 |
|
|
data += line + "\n"; |
124 |
|
|
} |
125 |
tdb |
1.1 |
} |
126 |
|
|
return data; |
127 |
|
|
} |
128 |
|
|
else { |
129 |
tdb |
1.3 |
// return null if we can't read it |
130 |
tdb |
1.1 |
return null; |
131 |
|
|
} |
132 |
|
|
} catch (IOException e) { |
133 |
|
|
System.out.println(e); |
134 |
|
|
e.printStackTrace(); |
135 |
|
|
return null; |
136 |
|
|
} |
137 |
|
|
} |
138 |
tdb |
1.3 |
|
139 |
|
|
/** |
140 |
|
|
* Checks if an URL is allowed. Failure of this method |
141 |
|
|
* indicates a client should be sent a 403. |
142 |
|
|
* |
143 |
|
|
* @param url the url to check |
144 |
|
|
* @return whether the url is permitted |
145 |
|
|
*/ |
146 |
tdb |
1.1 |
private boolean okUrl(String url) { |
147 |
tdb |
1.3 |
// recheck the list - this means the list can be |
148 |
|
|
// changed on the fly :) |
149 |
tdb |
1.1 |
readOkUrls(); |
150 |
|
|
Iterator i = _okUrls.iterator(); |
151 |
|
|
while(i.hasNext()) { |
152 |
|
|
if(url.equals((String)i.next())) { |
153 |
tdb |
1.3 |
// matched url against our list |
154 |
tdb |
1.1 |
return true; |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
return false; |
158 |
|
|
} |
159 |
|
|
|
160 |
tdb |
1.3 |
/** |
161 |
|
|
* Reads the list of URLs into a list. |
162 |
|
|
*/ |
163 |
tdb |
1.1 |
private void readOkUrls() { |
164 |
|
|
File file = new File(_okUrlsConfig); |
165 |
|
|
long lastMod = file.lastModified(); |
166 |
tdb |
1.3 |
// only want to re-read the list if it's changed |
167 |
tdb |
1.1 |
if(lastMod > _okUrlsConfigStamp) { |
168 |
|
|
_okUrlsConfigStamp = lastMod; |
169 |
|
|
_okUrls = new LinkedList(); |
170 |
|
|
if(file.exists() && file.canRead()) { |
171 |
|
|
try { |
172 |
|
|
BufferedReader reader = new BufferedReader(new FileReader(file)); |
173 |
|
|
while(reader.ready()) { |
174 |
|
|
String line = reader.readLine(); |
175 |
tdb |
1.3 |
// ignore comments |
176 |
tdb |
1.1 |
if(!line.startsWith("#")) { |
177 |
|
|
_okUrls.add(line); |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
} catch(IOException e) { |
181 |
|
|
System.out.println(e); |
182 |
|
|
e.printStackTrace(); |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
189 |
|
|
|
190 |
|
|
//---ATTRIBUTES--- |
191 |
tdb |
1.3 |
|
192 |
|
|
/** |
193 |
|
|
* The port we're bound to. |
194 |
|
|
*/ |
195 |
tdb |
1.1 |
private int _port; |
196 |
tdb |
1.3 |
|
197 |
|
|
/** |
198 |
|
|
* List of URLs that are permitted |
199 |
|
|
*/ |
200 |
tdb |
1.1 |
private LinkedList _okUrls; |
201 |
tdb |
1.3 |
|
202 |
|
|
/** |
203 |
|
|
* Config file for the URL list |
204 |
|
|
*/ |
205 |
tdb |
1.1 |
private String _okUrlsConfig; |
206 |
tdb |
1.3 |
|
207 |
|
|
/** |
208 |
|
|
* Timestamp of the URL config file |
209 |
|
|
*/ |
210 |
tdb |
1.1 |
private long _okUrlsConfigStamp = 0; |
211 |
tdb |
1.3 |
|
212 |
|
|
/** |
213 |
|
|
* Web directory |
214 |
|
|
*/ |
215 |
tdb |
1.2 |
private String _webDir; |
216 |
tdb |
1.1 |
|
217 |
|
|
//---STATIC ATTRIBUTES--- |
218 |
|
|
|
219 |
|
|
//---INNER CLASSES--- |
220 |
tdb |
1.3 |
|
221 |
|
|
/** |
222 |
|
|
* A Handler to deal with each client. This is an inner |
223 |
|
|
* class because I felt like trying it out :) |
224 |
|
|
*/ |
225 |
tdb |
1.1 |
private class Handler extends Thread { |
226 |
tdb |
1.3 |
|
227 |
|
|
/** |
228 |
|
|
* Construct a new Handler |
229 |
|
|
* |
230 |
|
|
* @param socket The socket connected to the client |
231 |
|
|
*/ |
232 |
tdb |
1.1 |
public Handler(Socket socket) { |
233 |
|
|
_socket = socket; |
234 |
|
|
} |
235 |
tdb |
1.3 |
|
236 |
|
|
/** |
237 |
|
|
* Start the Handler running. This will read the URL |
238 |
|
|
* from the client, and respond accordinly. |
239 |
|
|
* |
240 |
|
|
* This does not try to deal with bad data |
241 |
|
|
*/ |
242 |
tdb |
1.1 |
public void run() { |
243 |
|
|
BufferedReader reader = null; |
244 |
|
|
PrintWriter writer = null; |
245 |
|
|
try { |
246 |
tdb |
1.3 |
// setup the reader and writer |
247 |
tdb |
1.1 |
reader = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
248 |
|
|
writer = new PrintWriter(_socket.getOutputStream(), true); |
249 |
|
|
String line= ""; |
250 |
|
|
String url = ""; |
251 |
tdb |
1.3 |
// keep reading from the client until it sends no more... |
252 |
tdb |
1.1 |
do { |
253 |
|
|
line = reader.readLine(); |
254 |
tdb |
1.3 |
// ah, this contains the URL |
255 |
tdb |
1.7 |
if(line.startsWith("GET ")) { |
256 |
tdb |
1.3 |
// strip off "GET " |
257 |
tdb |
1.1 |
url = line.substring(4); |
258 |
tdb |
1.3 |
// strip off anything past the URL |
259 |
tdb |
1.7 |
if(url.lastIndexOf(' ') != -1) { |
260 |
|
|
url = url.substring(0, url.lastIndexOf(' ')); |
261 |
|
|
} |
262 |
tdb |
1.1 |
} |
263 |
|
|
} while(!line.equals("")); |
264 |
tdb |
1.3 |
// check the URL is allowed |
265 |
tdb |
1.1 |
if(okUrl(url)) { |
266 |
tdb |
1.3 |
// read the file |
267 |
tdb |
1.1 |
String data = readUrl(url); |
268 |
tdb |
1.3 |
// check the file could be read, null means it couldn't |
269 |
tdb |
1.1 |
if(data != null) { |
270 |
|
|
writer.println(data); |
271 |
|
|
} |
272 |
|
|
else { |
273 |
tdb |
1.3 |
// oh dear, couldn't find it, send a 404 |
274 |
tdb |
1.1 |
writer.println("HTTP/1.1 404 Not Found\n"); |
275 |
|
|
writer.println("404 Not Found"); |
276 |
|
|
} |
277 |
|
|
} |
278 |
|
|
else { |
279 |
tdb |
1.3 |
// oh dear, not allowed, send a 403 |
280 |
tdb |
1.1 |
writer.println("HTTP/1.1 403 Permission Denied\n"); |
281 |
|
|
writer.println("403 Permission Denied"); |
282 |
|
|
} |
283 |
|
|
} catch(IOException e) { |
284 |
|
|
System.out.println(e); |
285 |
|
|
e.printStackTrace(); |
286 |
|
|
} |
287 |
tdb |
1.3 |
// clean up and close |
288 |
tdb |
1.1 |
try { |
289 |
|
|
reader.close(); |
290 |
|
|
writer.close(); |
291 |
|
|
_socket.close(); |
292 |
|
|
} catch(IOException e) { |
293 |
|
|
System.out.println(e); |
294 |
|
|
e.printStackTrace(); |
295 |
|
|
} |
296 |
|
|
} |
297 |
tdb |
1.3 |
|
298 |
|
|
/** |
299 |
|
|
* The socket connected to the client |
300 |
|
|
*/ |
301 |
tdb |
1.1 |
private Socket _socket; |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
} |