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();
51 // I'm not too good at HTTP. Normally, we should put some
52 // error code here. Anyway, I have assumed that an error
53 // is equivalent to an unhandled request / method (501)
54 resp.returnCode = 501;
57 if(resp.returnCode == 200){
58 // call the appropriate hanndler
62 if (webinterface.specialRequest(fileName)) {
63 String newfile=webinterface.handleresponse(fileName, out, resp);
65 HTTPServices.GET_handler(newfile, out, resp);
68 HTTPServices.GET_handler(fileName, out, resp);
71 HTTPServices.HEAD_handler(fileName, out, resp);
74 HTTPServices.POST_handler(fileName, out, resp);
77 resp.returnCode = 501; //error
83 LogFile.write_log(client,methodType,fileName,httpVersion,
84 resp.returnCode,resp.sentBytes);
95 // System.out.println(fileName + " is going to finish"); // debug
98 //*****************************************************************************
100 // Purpose: Open an InputStream and parse the request made.
101 // Note: Regardless of what method is requested, right now it performs a
104 // Returns: Boolean value for success or failure
105 //*****************************************************************************
107 private int method(BufferedReader in){
113 // read just the first line
114 line = in.readLine();
116 StringTokenizer tok = new StringTokenizer(line, " ");
117 if (tok.hasMoreTokens()) // make sure there is a request
119 String str = tok.nextToken();
121 if ( str.equals("GET") ){
125 else if ( str.equals("HEAD") ){
129 else if ( str.equals("POST") ){
134 System.out.println("501 - unsupported request:" +str);
139 // System.out.println("Request from browser was empty!");
144 if (tok.hasMoreTokens())
146 fileName = tok.nextToken();
147 if(fileName.equals("/"))
149 fileName = "/index.html";
154 // this is weird... why am i taking the first character of
155 // the filename if there are no more tokens?
156 // - catch should take care of this
157 fileName = fileName.substring(1);
160 // read the http version number
161 // - right now nothing is done with this information
162 if (tok.hasMoreTokens())
164 httpVersion = tok.nextToken();
168 httpVersion = "http/1.0"; // default
171 // read remainder of the browser's header
172 // - nothing done right now with this info... placeholder
173 while((line = in.readLine()) != null)
175 StringTokenizer token = new StringTokenizer(line," ");
177 // do processing here
178 if(!token.hasMoreTokens())
185 System.err.println(e);