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.4
Committed: Mon Feb 26 22:51:17 2001 UTC (23 years, 2 months ago) by tdb
Branch: MAIN
Changes since 1.3: +6 -4 lines
Log Message:
We were getting a null line on the end of served files.

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.ac.ukc.iscream.corbaservices;
3
4 //---IMPORTS---
5 import java.net.*;
6 import java.io.*;
7 import java.util.*;
8
9 /**
10 * MiniWebServer
11 *
12 * This provides a very basic webserver for serving IOR files.
13 * This could easily serve other text files, but not binaries.
14 *
15 * @author $Author: tdb1 $
16 * @version $Id: MiniWebServer.java,v 1.3 2001/02/26 22:41:15 tdb1 Exp $
17 */
18 public class MiniWebServer extends Thread {
19
20 //---FINAL ATTRIBUTES---
21
22 /**
23 * The current CVS revision of this class
24 */
25 public static final String REVISION = "$Revision: 1.3 $";
26
27 //---STATIC METHODS---
28
29 //---CONSTRUCTORS---
30
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 public MiniWebServer(int port, String okUrlsConfig, String webDir) {
39 _port = port;
40 _okUrlsConfig = okUrlsConfig;
41 _webDir = webDir;
42 }
43
44 //---PUBLIC METHODS---
45
46 /**
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 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 // get a new client and give it to a Handler
67 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 /**
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 private String readUrl(String url) {
89 // trim off the initial /
90 if(url.startsWith("/")) {
91 url = url.substring(1);
92 }
93 try {
94 // need to prefix with web directory
95 File file = new File(_webDir+"/"+url);
96 if(file.exists() && file.canRead()) {
97 // read in all the data
98 String data = "";
99 BufferedReader reader = new BufferedReader(new FileReader(file));
100 while(reader.ready()) {
101 String line = reader.readLine();
102 if(line != null) {
103 data += line + "\n";
104 }
105 }
106 return data;
107 }
108 else {
109 // return null if we can't read it
110 return null;
111 }
112 } catch (IOException e) {
113 System.out.println(e);
114 e.printStackTrace();
115 return null;
116 }
117 }
118
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 private boolean okUrl(String url) {
127 // recheck the list - this means the list can be
128 // changed on the fly :)
129 readOkUrls();
130 Iterator i = _okUrls.iterator();
131 while(i.hasNext()) {
132 if(url.equals((String)i.next())) {
133 // matched url against our list
134 return true;
135 }
136 }
137 return false;
138 }
139
140 /**
141 * Reads the list of URLs into a list.
142 */
143 private void readOkUrls() {
144 File file = new File(_okUrlsConfig);
145 long lastMod = file.lastModified();
146 // only want to re-read the list if it's changed
147 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 // ignore comments
156 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
172 /**
173 * The port we're bound to.
174 */
175 private int _port;
176
177 /**
178 * List of URLs that are permitted
179 */
180 private LinkedList _okUrls;
181
182 /**
183 * Config file for the URL list
184 */
185 private String _okUrlsConfig;
186
187 /**
188 * Timestamp of the URL config file
189 */
190 private long _okUrlsConfigStamp = 0;
191
192 /**
193 * Web directory
194 */
195 private String _webDir;
196
197 //---STATIC ATTRIBUTES---
198
199 //---INNER CLASSES---
200
201 /**
202 * A Handler to deal with each client. This is an inner
203 * class because I felt like trying it out :)
204 */
205 private class Handler extends Thread {
206
207 /**
208 * Construct a new Handler
209 *
210 * @param socket The socket connected to the client
211 */
212 public Handler(Socket socket) {
213 _socket = socket;
214 }
215
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 public void run() {
223 BufferedReader reader = null;
224 PrintWriter writer = null;
225 try {
226 // setup the reader and writer
227 reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
228 writer = new PrintWriter(_socket.getOutputStream(), true);
229 String line= "";
230 String url = "";
231 // keep reading from the client until it sends no more...
232 do {
233 line = reader.readLine();
234 // ah, this contains the URL
235 if(line.startsWith("GET")) {
236 // strip off "GET "
237 url = line.substring(4);
238 // strip off anything past the URL
239 url = url.substring(0, url.lastIndexOf(' '));
240 }
241 } while(!line.equals(""));
242 // check the URL is allowed
243 if(okUrl(url)) {
244 // read the file
245 String data = readUrl(url);
246 // check the file could be read, null means it couldn't
247 if(data != null) {
248 writer.println(data);
249 }
250 else {
251 // oh dear, couldn't find it, send a 404
252 writer.println("HTTP/1.1 404 Not Found\n");
253 writer.println("404 Not Found");
254 }
255 }
256 else {
257 // oh dear, not allowed, send a 403
258 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 // clean up and close
266 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
276 /**
277 * The socket connected to the client
278 */
279 private Socket _socket;
280 }
281
282 }