1 public class Request extends Socket {
12 String remote_host_name;
18 String errordescription;
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;
35 return request.length();
40 SC_CONNECTING_TO_HOST=1;
44 SC_INTERNAL_SERVER_ERROR=5;
46 SC_REMOTE_DEBUG_MODE=7;
47 SC_CONNECTION_CLOSED=8;
48 SC_HTTP_OPTIONS_THIS=9;
50 SC_MOVED_PERMANENTLY=11;
54 public Request(String request) {
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
64 public void parseRequest() {
65 String a=request.substring(0,request.indexOf('\n'));
68 int method_index=Jhttpp2Server.getHttpMethod(a);
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;
81 statuscode = SC_INTERNAL_SERVER_ERROR;
82 errordescription="This WWW proxy supports only the \"GET\" method while acting as webserver.";
86 f = a.substring(pos+3); //removes "http://"
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 + "\"";
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
98 url=f.substring(pos); // saves path without hostname
99 f=f.substring(0,pos); // reduce string to the hostname
101 else url="/"; // occurs with this request: "GET http://localhost HTTP/1.1"
102 pos = f.indexOf(":"); // check for the portnumber
104 String l_port =f.substring(pos+1);
105 if (l_port.indexOf(" ")!=-1)
106 l_port=l_port.substring(0,l_port.indexOf(" "));
108 i_port = Integer.parseInt(l_port);
109 f = f.substring(0,pos);
113 remote_host_name = f;
114 address = InetAddress.getByName(f);
117 public void connect() {
118 connect(address, port);
121 public String getRequest() {
122 return method + " "+url+" "+"HTTP/1.1"+"\r\n";
125 public void sendRequest() {
126 write(getRequest().getBytes());
127 write(request.substring(request.indexOf('\n')+1,request.length()).getBytes());