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             case 1:
71                 HTTPServices.HEAD_handler(fileName, out, resp);
72                 break;
73             case 2:
74                 HTTPServices.POST_handler(fileName, out, resp);
75                 break;
76             default:
77                 resp.returnCode = 501; //error
78             }
79             
80             try{
81                 out.flush();
82                 if (logging)
83                     LogFile.write_log(client,methodType,fileName,httpVersion,
84                                       resp.returnCode,resp.sentBytes);
85
86                 out.close();
87                 in.close();
88                 client.close();
89             }
90             catch(IOException e){
91                 ; // do nothing
92             }
93         }
94
95         //      System.out.println(fileName + " is going to finish"); // debug 
96     }
97   
98 //*****************************************************************************
99 // Function: method()
100 // Purpose:  Open an InputStream and parse the request made.  
101 // Note:     Regardless of what method is requested, right now it performs a
102 //           GET operation.
103 // Calls:    
104 // Returns:  Boolean value for success or failure
105 //*****************************************************************************
106
107   private int method(BufferedReader in){
108       int ret = -1;
109
110       try{
111           String line;
112           
113           // read just the first line
114           line = in.readLine();
115           // only spaces used
116           StringTokenizer tok = new StringTokenizer(line, " ");  
117           if (tok.hasMoreTokens())  // make sure there is a request
118               {
119                   String str = tok.nextToken();
120           
121                   if ( str.equals("GET") ){
122                       ret = 0;
123                       methodType = "GET";
124                   }
125                   else if ( str.equals("HEAD") ){
126                       ret = 1;
127                       methodType = "HEAD";
128                   }
129                   else if ( str.equals("POST") ){
130                       ret = 2;
131                       methodType = "POST";
132                   }
133                   else{
134                       System.out.println("501 - unsupported request:" +str);
135                       return -1;
136                   } 
137               }      
138           else{
139               // System.out.println("Request from browser was empty!");
140               return -1;
141           }
142           
143           // get the filename
144           if (tok.hasMoreTokens())
145               {
146                   fileName = tok.nextToken();
147                   if(fileName.equals("/"))
148                       {
149                           fileName = "/index.html";
150                       }
151               }
152           else
153               {
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);
158               }  
159           
160           // read the http version number
161           // - right now nothing is done with this information
162           if (tok.hasMoreTokens())
163               {
164                   httpVersion = tok.nextToken();
165               }
166           else
167               {
168                   httpVersion = "http/1.0";              // default
169               }
170           
171           // read remainder of the browser's header
172           // - nothing done right now with this info... placeholder
173           while((line = in.readLine()) != null)
174               {
175                   StringTokenizer token = new StringTokenizer(line," ");
176                   
177                   // do processing here
178                   if(!token.hasMoreTokens())
179                       { 
180                           break;          
181                       }
182               }
183       }
184       catch(Exception e){
185           System.err.println(e);
186           return -1;
187       }
188       
189     return ret;
190   }
191 }