start of new file
[IRC.git] / Robust / src / Benchmarks / Jhttpp2 / BR / Request.java
1 public class Request extends Socket {
2     flag first;
3     flag chained;
4     flag done;
5     flag sent;
6     flag received;
7     flag processed;
8
9     String response;
10     String request;
11     int port;
12     String remote_host_name;
13     InetAddress address;
14     int statuscode;
15     String url;
16     String method;
17     boolean ssl;
18     String errordescription;
19
20     public final int SC_OK;
21     public final int SC_CONNECTING_TO_HOST;
22     public final int SC_HOST_NOT_FOUND;
23     public final int SC_URL_BLOCKED;
24     public final int SC_CLIENT_ERROR;
25     public final int SC_INTERNAL_SERVER_ERROR;
26     public final int SC_NOT_SUPPORTED;
27     public final int SC_REMOTE_DEBUG_MODE;
28     public final int SC_CONNECTION_CLOSED;
29     public final int SC_HTTP_OPTIONS_THIS;
30     public final int SC_FILE_REQUEST;
31     public final int SC_MOVED_PERMANENTLY;
32     public final int SC_CONFIG_RQ;
33
34     public int length() {
35         return request.length();
36     }
37
38     void init() {
39          SC_OK=0;
40          SC_CONNECTING_TO_HOST=1;
41          SC_HOST_NOT_FOUND=2;
42          SC_URL_BLOCKED=3;
43          SC_CLIENT_ERROR=4;
44          SC_INTERNAL_SERVER_ERROR=5;
45          SC_NOT_SUPPORTED=6;
46          SC_REMOTE_DEBUG_MODE=7;
47          SC_CONNECTION_CLOSED=8;
48          SC_HTTP_OPTIONS_THIS=9;
49          SC_FILE_REQUEST=10;
50          SC_MOVED_PERMANENTLY=11;
51          SC_CONFIG_RQ = 12;
52     }
53
54     public Request(String request) {
55         init();
56         this.request=request;
57     }
58
59     /**
60      * Parser for the first (!) line from the HTTP request<BR>
61      * Sets up the URL, method and remote hostname.
62      * @return an InetAddress for the hostname, null on errors with a statuscode!=SC_OK
63      */
64     public void parseRequest() {
65         String a=request.substring(0,request.indexOf('\n'));
66         String f; 
67         int pos; 
68         int method_index=Jhttpp2Server.getHttpMethod(a);
69         
70         if (ssl) {
71             url="";
72             f = a.substring(8);
73         } else {
74             method = a.substring(0,a.indexOf(" ")); //first word in the line
75             pos = a.indexOf(":"); // locate first :
76             if (pos == -1) { // occours with "GET / HTTP/1.1"
77                 url = a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "));
78                 if (method_index == 0) { // method_index==0 --> GET
79                     statuscode = SC_FILE_REQUEST;
80                 } else {
81                     statuscode = SC_INTERNAL_SERVER_ERROR;
82                     errordescription="This WWW proxy supports only the \"GET\" method while acting as webserver.";
83                 }
84                 return;
85             }
86             f = a.substring(pos+3); //removes "http://"
87         }
88         pos=f.indexOf(" "); // locate space, should be the space before "HTTP/1.1"
89         if (pos==-1) { // buggy request
90             statuscode = SC_CLIENT_ERROR;
91             errordescription="Your browser sent an invalid request: \""+ a + "\"";
92             return;
93         }
94         f = f.substring(0,pos); //removes all after space
95         // if the url contains a space... it's not our mistake...(url's must never contain a space character)
96         pos=f.indexOf("/"); // locate the first slash
97         if (pos!=-1) {
98             url=f.substring(pos); // saves path without hostname
99             f=f.substring(0,pos); // reduce string to the hostname
100         }
101         else url="/"; // occurs with this request: "GET http://localhost HTTP/1.1"
102         pos = f.indexOf(":"); // check for the portnumber
103         if (pos!=-1) {
104             String l_port =f.substring(pos+1);
105             if (l_port.indexOf(" ")!=-1)
106                     l_port=l_port.substring(0,l_port.indexOf(" "));
107             int i_port=80;
108             i_port = Integer.parseInt(l_port);
109             f = f.substring(0,pos);
110             port=i_port;
111         } else
112             port=80;
113         remote_host_name = f;
114         address = InetAddress.getByName(f);
115     }
116
117     public void connect() {
118         connect(address, port);
119     }
120
121     public String getRequest() {
122         return method + " "+url+" "+"HTTP/1.1"+"\r\n";
123     }
124
125     public void sendRequest() {
126         write(getRequest().getBytes());
127         write(request.substring(request.indexOf('\n')+1,request.length()).getBytes());
128     }
129 }