The Web|web service Parseuri method obtains the URI from the request line. Listing 1.3 shows the use of the Parseuri method. The first and second empty cells in the Parseuri reduction request obtain the URI.
Listing 1.3. The Request class ' Parseuri method
private string Parseuri (string requeststring) {
int index1, INDEX2;
Index1 = Requeststring.indexof (");
if (index1!=-1) {
Index2 = Requeststring.indexof (', index1 + 1);
if (Index2 > Index1)
Return requeststring.substring (index1 + 1, index2);
}
return null;
}
The response class response represents an HTTP response. Its constructor accepts a OutputStream object, such as the following:
Public Response (OutputStream output) {
This.output = output;
}
The Response object is constructed by the await method of the Httpserver class, which is passed a parameter that is the OutputStream object that is obtained from the socket.
The response class has two public methods: Setrequest and Sendstaticresource. The Setrequest method passes a request object to the response object. The code in Listing 1.4 shows this:
Listing 1.4. The Response class ' Setrequest method
public void Setrequest (Request request) {
This.request = Request;
}
The Sendstaticresource method is used to send a static resource, such as an HTML file. Listing 1.5 gives its implementation process:
Listing 1.5. The Response class ' Sendstaticresource method
public void Sendstaticresource () throws IOException {
byte[] bytes = new Byte[buffer_size];
FileInputStream FIS = null;
try {
File File = new file (Httpserver.web_root, Request.geturi ());
if (file.exists ()) {
FIS = new FileInputStream (file);
int ch = fis.read (bytes, 0, buffer_size);
while (Ch!=-1) {
Output.write (bytes, 0, ch);
ch = fis.read (bytes, 0, buffer_size);
}
}
else {
File not found
String errormessage = "http/1.1 404 File Not found\r\n" +
"Content-type:text/html\r\n" +
"Content-length:23\r\n" +
"\ r \ n" +
"Output.write (Errormessage.getbytes ());
}
}
catch (Exception e) {
Thrown if cannot instantiate a File object
System.out.println (E.tostring ());
}
finally {
if (FIS!= null)
Fis.close ();
}
}