2 //****************************************************************************
3 // Programmer: Duane M. Gran, ragnar@cs.bsu.edu
4 // Program: JhttpServer
5 // Date: April 24, 1998
6 //****************************************************************************
13 //****************************************************************************
15 // Purpose: Takes an HTTP request and executes it in a separate thread
16 //****************************************************************************
18 public class JhttpWorker extends Thread {
19 public String fileName = null;
20 public String methodType = null;
21 public String httpVersion = "http/1.0";
22 private Socket client;
23 public int fileLength, returnCode;
24 private boolean logging;
25 private WebInterface webinterface;
27 public JhttpWorker(Socket client, boolean logging, WebInterface webinterface) {
30 this.webinterface=webinterface;
34 HTTPResponse resp = new HTTPResponse();
36 BufferedReader in = null;
37 OutputStream out = null;
39 resp.returnCode = 200;
44 in = new BufferedReader(
45 new InputStreamReader(
46 client.getInputStream()));
48 out = client.getOutputStream();
49 } catch(IOException e) {
50 // I'm not too good at HTTP. Normally, we should put some
51 // error code here. Anyway, I have assumed that an error
52 // is equivalent to an unhandled request / method (501)
53 resp.returnCode = 501;
56 if(resp.returnCode == 200) {
57 // call the appropriate hanndler
61 if (webinterface.specialRequest(fileName)) {
62 String newfile=webinterface.handleresponse(fileName, out, resp);
64 HTTPServices.GET_handler(newfile, out, resp);
67 HTTPServices.GET_handler(fileName, out, resp);
71 HTTPServices.HEAD_handler(fileName, out, resp);
75 HTTPServices.POST_handler(fileName, out, resp);
79 resp.returnCode = 501; //error
85 LogFile.write_log(client,methodType,fileName,httpVersion,
86 resp.returnCode,resp.sentBytes);
91 } catch(IOException e) {
96 // System.out.println(fileName + " is going to finish"); // debug
99 //*****************************************************************************
100 // Function: method()
101 // Purpose: Open an InputStream and parse the request made.
102 // Note: Regardless of what method is requested, right now it performs a
105 // Returns: Boolean value for success or failure
106 //*****************************************************************************
108 private int method(BufferedReader in) {
114 // read just the first line
115 line = in.readLine();
117 StringTokenizer tok = new StringTokenizer(line, " ");
118 if (tok.hasMoreTokens()) { // make sure there is a request
119 String str = tok.nextToken();
121 if ( str.equals("GET") ) {
124 } else if ( str.equals("HEAD") ) {
127 } else if ( str.equals("POST") ) {
131 System.out.println("501 - unsupported request:" +str);
135 // System.out.println("Request from browser was empty!");
140 if (tok.hasMoreTokens()) {
141 fileName = tok.nextToken();
142 if(fileName.equals("/")) {
143 fileName = "/index.html";
147 // this is weird... why am i taking the first character of
148 // the filename if there are no more tokens?
149 // - catch should take care of this
150 fileName = fileName.substring(1);
153 // read the http version number
154 // - right now nothing is done with this information
155 if (tok.hasMoreTokens()) {
156 httpVersion = tok.nextToken();
159 httpVersion = "http/1.0"; // default
162 // read remainder of the browser's header
163 // - nothing done right now with this info... placeholder
164 while((line = in.readLine()) != null) {
165 StringTokenizer token = new StringTokenizer(line," ");
167 // do processing here
168 if(!token.hasMoreTokens()) {
172 } catch(Exception e) {
173 System.err.println(e);