This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
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     }
50     catch(IOException e){
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;
55     }
56
57     if(resp.returnCode == 200){
58       // call the appropriate hanndler
59       switch(method(in)){
60
61       case 0:
62         if (webinterface.specialRequest(fileName)) {
63           String newfile=webinterface.handleresponse(fileName, out, resp);
64           if (newfile!=null) {
65             HTTPServices.GET_handler(newfile, out, resp);
66           }
67         } else
68           HTTPServices.GET_handler(fileName, out, resp);
69         break;
70
71       case 1:
72         HTTPServices.HEAD_handler(fileName, out, resp);
73         break;
74
75       case 2:
76         HTTPServices.POST_handler(fileName, out, resp);
77         break;
78
79       default:
80         resp.returnCode = 501;         //error
81       }
82
83       try{
84         out.flush();
85         if (logging)
86           LogFile.write_log(client,methodType,fileName,httpVersion,
87                             resp.returnCode,resp.sentBytes);
88
89         out.close();
90         in.close();
91         client.close();
92       }
93       catch(IOException e){
94         ;         // do nothing
95       }
96     }
97
98     //  System.out.println(fileName + " is going to finish"); // debug
99   }
100
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
105 //           GET operation.
106 // Calls:
107 // Returns:  Boolean value for success or failure
108 //*****************************************************************************
109
110   private int method(BufferedReader in) {
111     int ret = -1;
112
113     try{
114       String line;
115
116       // read just the first line
117       line = in.readLine();
118       // only spaces used
119       StringTokenizer tok = new StringTokenizer(line, " ");
120       if (tok.hasMoreTokens()){     // make sure there is a request
121         String str = tok.nextToken();
122
123         if ( str.equals("GET") ){
124           ret = 0;
125           methodType = "GET";
126         } else if ( str.equals("HEAD") )   {
127           ret = 1;
128           methodType = "HEAD";
129         } else if ( str.equals("POST") )   {
130           ret = 2;
131           methodType = "POST";
132         } else{
133           System.out.println("501 - unsupported request:" +str);
134           return -1;
135         }
136       } else{
137         // System.out.println("Request from browser was empty!");
138         return -1;
139       }
140
141       // get the filename
142       if (tok.hasMoreTokens()){
143         fileName = tok.nextToken();
144         if(fileName.equals("/")){
145           fileName = "/index.html";
146         }
147       } else
148       {
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);
153       }
154
155       // read the http version number
156       // - right now nothing is done with this information
157       if (tok.hasMoreTokens()){
158         httpVersion = tok.nextToken();
159       } else
160       {
161         httpVersion = "http/1.0";                       // default
162       }
163
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," ");
168
169         // do processing here
170         if(!token.hasMoreTokens()){
171           break;
172         }
173       }
174     }
175     catch(Exception e){
176       System.err.println(e);
177       return -1;
178     }
179
180     return ret;
181   }
182 }