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