Friday 24 January 2014

HTML Web Server: Working and Implementation

In order to understand the working of an HTML web server, first we need to understand how the browser interacts with the server, what information browser sends to the server and how server uses that information to send response to the client(browser).
Here is a small code in JAVA which will show us the header information sent by the browser to the server.  //server.java
import java.net.*;
import java.io.*;
class server{
 public static void main(String[] args) throws Exception{
  ServerSocket server = new ServerSocket( 3000);
  Socket s = server.accept();
  InputStream in = s.getInputStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  String data = reader.readLine();
  while(!data.isEmpty()){
   System.out.println(data);
   data = reader.readLine();
  }
  s.close();
  server.close();
 }
}
Now when we'll run this program by typing 'java server' on terminal, it will wait for client to send request. Now we'll send the request from our web browser by typing 'localhost:3000' in the address bar. Then the header information sent by the browser will be displayed on terminal. Here is the output.
The first line says method of request is GET and protocol used is HTTP version 1.1, second line tells the IP address and port number of the host and similarly other information about the browser.

Now let's request a page from browser 'localhost:3000/p1.html' and again check the output on terminal.
Now we can see the name of requested page p1.html is coming in between the GET / and HTTP, so we can extract the name of page from this string and check if this page is present in our directory, if it is not present then simply send the information to the browser <h3>Page Not Found</h3> or if it is present then open a FileStream and send the content of file to the browser.

A very basic code shows how we can send the information to the browser after analysing its request.

import java.net.*;
import java.io.*;
class server{
 public static void main(String[] args) throws Exception{
  ServerSocket server = new ServerSocket( 3000);
  Socket s = server.accept();
  InputStream in = s.getInputStream();
  BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  String data = reader.readLine();
  while(!data.isEmpty()){
   System.out.println(data);
   data = reader.readLine();
  }
  OutputStream out = s.getOutputStream();
  PrintWriter writer = new PrintWriter(out, true);
  
  File file = new File("p1.html");
  if(!file.exists()){
   writer.write("<html><h3> Page Not Found</h3></html>");
  }
  else{
   BufferedReader fl = new BufferedReader(new FileReader("p1.html")) ;
   String datatosend = fl.readLine();
   while(datatosend != null){
    writer.write(datatosend);
    writer.flush();
    datatosend = fl.readLine();
   }
  }
  writer.close();
  s.close();
  server.close();
 }
}
The above code can be made more dynamic and also when we have urls containing spaces they are replaced with %20 which is ascii code of space in hexadecimal format(UTF format specifiactions), we have to take care of that also while building such a server.