ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/corbaservices/uk/org/iscream/cms/corbaservices/MiniWebServer.java
Revision: 1.10
Committed: Sun Aug 1 10:40:20 2004 UTC (19 years, 9 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.9: +3 -3 lines
Log Message:
Catch a lot of old URL's and update them. Also remove a couple of old files
that aren't used.

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * http://www.i-scream.org
4 * 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 //---PACKAGE DECLARATION---
22 package uk.org.iscream.cms.corbaservices;
23
24 //---IMPORTS---
25 import java.net.*;
26 import java.io.*;
27 import java.util.*;
28
29 /**
30 * MiniWebServer
31 *
32 * This provides a very basic webserver for serving IOR files.
33 * This could easily serve other text files, but not binaries.
34 *
35 * @author $Author: tdb $
36 * @version $Id: MiniWebServer.java,v 1.9 2002/05/21 16:47:11 tdb Exp $
37 */
38 public class MiniWebServer extends Thread {
39
40 //---FINAL ATTRIBUTES---
41
42 /**
43 * The current CVS revision of this class
44 */
45 public static final String REVISION = "$Revision: 1.9 $";
46
47 //---STATIC METHODS---
48
49 //---CONSTRUCTORS---
50
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 public MiniWebServer(int port, String okUrlsConfig, String webDir) {
59 _port = port;
60 _okUrlsConfig = okUrlsConfig;
61 _webDir = webDir;
62 }
63
64 //---PUBLIC METHODS---
65
66 /**
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 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 // get a new client and give it to a Handler
87 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 /**
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 private String readUrl(String url) {
109 // trim off the initial /
110 if(url.startsWith("/")) {
111 url = url.substring(1);
112 }
113 try {
114 // need to prefix with web directory
115 File file = new File(_webDir+"/"+url);
116 if(file.exists() && file.canRead()) {
117 // read in all the data
118 String data = "";
119 BufferedReader reader = new BufferedReader(new FileReader(file));
120 while(reader.ready()) {
121 String line = reader.readLine();
122 if(line != null) {
123 data += line + "\n";
124 }
125 }
126 return data;
127 }
128 else {
129 // return null if we can't read it
130 return null;
131 }
132 } catch (IOException e) {
133 System.out.println(e);
134 e.printStackTrace();
135 return null;
136 }
137 }
138
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 private boolean okUrl(String url) {
147 // recheck the list - this means the list can be
148 // changed on the fly :)
149 readOkUrls();
150 Iterator i = _okUrls.iterator();
151 while(i.hasNext()) {
152 if(url.equals((String)i.next())) {
153 // matched url against our list
154 return true;
155 }
156 }
157 return false;
158 }
159
160 /**
161 * Reads the list of URLs into a list.
162 */
163 private void readOkUrls() {
164 File file = new File(_okUrlsConfig);
165 long lastMod = file.lastModified();
166 // only want to re-read the list if it's changed
167 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 // ignore comments
176 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
192 /**
193 * The port we're bound to.
194 */
195 private int _port;
196
197 /**
198 * List of URLs that are permitted
199 */
200 private LinkedList _okUrls;
201
202 /**
203 * Config file for the URL list
204 */
205 private String _okUrlsConfig;
206
207 /**
208 * Timestamp of the URL config file
209 */
210 private long _okUrlsConfigStamp = 0;
211
212 /**
213 * Web directory
214 */
215 private String _webDir;
216
217 //---STATIC ATTRIBUTES---
218
219 //---INNER CLASSES---
220
221 /**
222 * A Handler to deal with each client. This is an inner
223 * class because I felt like trying it out :)
224 */
225 private class Handler extends Thread {
226
227 /**
228 * Construct a new Handler
229 *
230 * @param socket The socket connected to the client
231 */
232 public Handler(Socket socket) {
233 _socket = socket;
234 }
235
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 public void run() {
243 BufferedReader reader = null;
244 PrintWriter writer = null;
245 try {
246 // setup the reader and writer
247 reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
248 writer = new PrintWriter(_socket.getOutputStream(), true);
249 String line= "";
250 String url = "";
251 // keep reading from the client until it sends no more...
252 do {
253 line = reader.readLine();
254 // ah, this contains the URL
255 if(line.startsWith("GET ")) {
256 // strip off "GET "
257 url = line.substring(4);
258 // strip off anything past the URL
259 if(url.lastIndexOf(' ') != -1) {
260 url = url.substring(0, url.lastIndexOf(' '));
261 }
262 }
263 } while(!line.equals(""));
264 // check the URL is allowed
265 if(okUrl(url)) {
266 // read the file
267 String data = readUrl(url);
268 // check the file could be read, null means it couldn't
269 if(data != null) {
270 writer.println(data);
271 }
272 else {
273 // oh dear, couldn't find it, send a 404
274 writer.println("HTTP/1.1 404 Not Found\n");
275 writer.println("404 Not Found");
276 }
277 }
278 else {
279 // oh dear, not allowed, send a 403
280 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 // clean up and close
288 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
298 /**
299 * The socket connected to the client
300 */
301 private Socket _socket;
302 }
303
304 }