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
(Generate patch)

Comparing projects/cms/source/corbaservices/uk/org/iscream/cms/corbaservices/MiniWebServer.java (file contents):
Revision 1.2 by tdb, Mon Feb 26 21:53:01 2001 UTC vs.
Revision 1.7 by tdb, Fri Apr 5 11:54:35 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.corbaservices;
2 > package uk.org.iscream.cms.corbaservices;
3  
4   //---IMPORTS---
5   import java.net.*;
# Line 9 | Line 9 | import java.util.*;
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$
16   * @version $Id$
17   */
# Line 24 | Line 27 | public class MiniWebServer extends Thread {
27   //---STATIC METHODS---
28  
29   //---CONSTRUCTORS---
30 <
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;
# Line 33 | Line 43 | public class MiniWebServer extends Thread {
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) {
# Line 47 | Line 63 | public class MiniWebServer extends Thread {
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();
# Line 61 | Line 78 | public class MiniWebServer extends Thread {
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 <                    data += reader.readLine();
102 <                    data += "\n";
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) {
# Line 85 | Line 115 | public class MiniWebServer extends Thread {
115              return null;
116          }
117      }
118 <
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();
# Line 108 | Line 152 | public class MiniWebServer extends Thread {
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                          }
# Line 123 | Line 168 | public class MiniWebServer extends Thread {
168   //---ACCESSOR/MUTATOR METHODS---
169  
170   //---ATTRIBUTES---
171 <
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 <
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 <                    if(line.startsWith("GET")) {
234 >                    // ah, this contains the URL
235 >                    if(line.startsWith("GET ")) {
236 >                        // strip off "GET "
237                          url = line.substring(4);
238 <                        url = url.substring(0, url.lastIndexOf(' '));
238 >                        // strip off anything past the URL
239 >                        if(url.lastIndexOf(' ') != -1) {
240 >                            url = url.substring(0, url.lastIndexOf(' '));
241 >                        }
242                      }
243                  } while(!line.equals(""));
244 +                // check the URL is allowed
245                  if(okUrl(url)) {
246 +                    // read the file
247                      String data = readUrl(url);
248 +                    // check the file could be read, null means it couldn't
249                      if(data != null) {
250                          writer.println(data);
251                      }
252                      else {
253 +                        // oh dear, couldn't find it, send a 404
254                          writer.println("HTTP/1.1 404 Not Found\n");
255                          writer.println("404 Not Found");
256                      }
257                  }
258                  else {
259 +                    // oh dear, not allowed, send a 403
260                      writer.println("HTTP/1.1 403 Permission Denied\n");
261                      writer.println("403 Permission Denied");
262                  }
# Line 171 | Line 264 | public class MiniWebServer extends Thread {
264                  System.out.println(e);
265                  e.printStackTrace();
266              }
267 +            // clean up and close
268              try {
269                  reader.close();
270                  writer.close();
# Line 180 | Line 274 | public class MiniWebServer extends Thread {
274                  e.printStackTrace();
275              }
276          }
277 +        
278 +        /**
279 +         * The socket connected to the client
280 +         */
281          private Socket _socket;
282      }
283      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines