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);
72 HTTPServices.HEAD_handler(fileName, out, resp);
76 HTTPServices.POST_handler(fileName, out, resp);
80 resp.returnCode = 501; //error
86 LogFile.write_log(client,methodType,fileName,httpVersion,
87 resp.returnCode,resp.sentBytes);
98 // System.out.println(fileName + " is going to finish"); // debug
101 //*****************************************************************************
102 // Function: method()
103 // Purpose: Open an InputStream and parse the request made.
104 // Note: Regardless of what method is requested, right now it performs a
107 // Returns: Boolean value for success or failure
108 //*****************************************************************************
110 private int method(BufferedReader in) {
116 // read just the first line
117 line = in.readLine();
119 StringTokenizer tok = new StringTokenizer(line, " ");
120 if (tok.hasMoreTokens()){ // make sure there is a request
121 String str = tok.nextToken();
123 if ( str.equals("GET") ){
126 } else if ( str.equals("HEAD") ) {
129 } else if ( str.equals("POST") ) {
133 System.out.println("501 - unsupported request:" +str);
137 // System.out.println("Request from browser was empty!");
142 if (tok.hasMoreTokens()){
143 fileName = tok.nextToken();
144 if(fileName.equals("/")){
145 fileName = "/index.html";
149 // this is weird... why am i taking the first character of
150 // the filename if there are no more tokens?
151 // - catch should take care of this
152 fileName = fileName.substring(1);
155 // read the http version number
156 // - right now nothing is done with this information
157 if (tok.hasMoreTokens()){
158 httpVersion = tok.nextToken();
161 httpVersion = "http/1.0"; // default
164 // read remainder of the browser's header
165 // - nothing done right now with this info... placeholder
166 while((line = in.readLine()) != null){
167 StringTokenizer token = new StringTokenizer(line," ");
169 // do processing here
170 if(!token.hasMoreTokens()){
176 System.err.println(e);