switch to spaces only..
[IRC.git] / Robust / src / Interface / JhttpWorker.java
1 package Interface;
2 //****************************************************************************
3 // Programmer: Duane M. Gran, ragnar@cs.bsu.edu
4 // Program:    JhttpServer
5 // Date:       April 24, 1998
6 //****************************************************************************
7
8
9 import java.net.*;
10 import java.io.*;
11 import java.util.*;
12
13 //****************************************************************************
14 // Class:   JhttpWorker
15 // Purpose: Takes an HTTP request and executes it in a separate thread
16 //****************************************************************************
17
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;
26
27   public JhttpWorker(Socket client, boolean logging, WebInterface webinterface) {
28     this.client=client;
29     this.logging=logging;
30     this.webinterface=webinterface;
31   }
32
33   public void run() {
34     HTTPResponse resp = new HTTPResponse();
35
36     BufferedReader in = null;
37     OutputStream out = null;
38
39     resp.returnCode = 200;
40     resp.sentBytes = 0;
41
42     try {
43
44       in = new BufferedReader(
45         new InputStreamReader(
46           client.getInputStream()));
47
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;
54     }
55
56     if(resp.returnCode == 200) {
57       // call the appropriate hanndler
58       switch(method(in)) {
59
60       case 0:
61         if (webinterface.specialRequest(fileName)) {
62           String newfile=webinterface.handleresponse(fileName, out, resp);
63           if (newfile!=null) {
64             HTTPServices.GET_handler(newfile, out, resp);
65           }
66         } else
67           HTTPServices.GET_handler(fileName, out, resp);
68         break;
69
70       case 1:
71         HTTPServices.HEAD_handler(fileName, out, resp);
72         break;
73
74       case 2:
75         HTTPServices.POST_handler(fileName, out, resp);
76         break;
77
78       default:
79         resp.returnCode = 501;         //error
80       }
81
82       try {
83         out.flush();
84         if (logging)
85           LogFile.write_log(client,methodType,fileName,httpVersion,
86                             resp.returnCode,resp.sentBytes);
87
88         out.close();
89         in.close();
90         client.close();
91       } catch(IOException e) {
92         ;         // do nothing
93       }
94     }
95
96     //  System.out.println(fileName + " is going to finish"); // debug
97   }
98
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
103 //           GET operation.
104 // Calls:
105 // Returns:  Boolean value for success or failure
106 //*****************************************************************************
107
108   private int method(BufferedReader in) {
109     int ret = -1;
110
111     try {
112       String line;
113
114       // read just the first line
115       line = in.readLine();
116       // only spaces used
117       StringTokenizer tok = new StringTokenizer(line, " ");
118       if (tok.hasMoreTokens()) {    // make sure there is a request
119         String str = tok.nextToken();
120
121         if ( str.equals("GET") ) {
122           ret = 0;
123           methodType = "GET";
124         } else if ( str.equals("HEAD") ) {
125           ret = 1;
126           methodType = "HEAD";
127         } else if ( str.equals("POST") ) {
128           ret = 2;
129           methodType = "POST";
130         } else {
131           System.out.println("501 - unsupported request:" +str);
132           return -1;
133         }
134       } else {
135         // System.out.println("Request from browser was empty!");
136         return -1;
137       }
138
139       // get the filename
140       if (tok.hasMoreTokens()) {
141         fileName = tok.nextToken();
142         if(fileName.equals("/")) {
143           fileName = "/index.html";
144         }
145       } else
146       {
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);
151       }
152
153       // read the http version number
154       // - right now nothing is done with this information
155       if (tok.hasMoreTokens()) {
156         httpVersion = tok.nextToken();
157       } else
158       {
159         httpVersion = "http/1.0";                       // default
160       }
161
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," ");
166
167         // do processing here
168         if(!token.hasMoreTokens()) {
169           break;
170         }
171       }
172     } catch(Exception e) {
173       System.err.println(e);
174       return -1;
175     }
176
177     return ret;
178   }
179 }