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.5
Committed: Thu Mar 15 00:33:44 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.4: +3 -3 lines
Log Message:
Changed package structure from;
uk.ac.ukc.iscream.corbaservices
to;
uk.org.iscream.corbaservices

File Contents

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