Solved

HTTP1.1 keep-alive support for simple server

Posted on 2004-10-01
11
962 Views
Last Modified: 2008-01-09
Hello,

I would like to know if anyone can give me an example of how to extend this simple server to allow support for HTTP keep- alive (1.1 only)

import java.io.*;
import java.net.*;
import java.util.*;

public final class WebServer {
    public static void main(String args[]) throws Exception {

        //Establish the listen socket
        int PORT = 777;    
        ServerSocket listenSocket = new ServerSocket(PORT);
       

        //Process HTTP service requests in an infinite loop
        while(true) {
            //listen for TCP connection request
            //Construct an object to process the HTTP request message
            HttpRequest request = new HttpRequest(listenSocket.accept());
            Thread thread = new Thread(request);
            thread.start();
        }
    }
}
final class HttpRequest implements Runnable {

    final static String CRLF ="\r\n";
    Socket socket;

    public HttpRequest(Socket socket) throws Exception {
        this.socket = socket;
    }


    //Implement the run() method of the Runnable interface
    public void run() {
        try {
            processRequest();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private void processRequest() throws Exception {
        //Get references to sockets input and output streams
        InputStream is = this.socket.getInputStream();
        DataOutputStream os = new DataOutputStream(this.socket.getOutputStream());
       
        //Set up input stream filter
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
       
        //Get the request line of HTTP message
        String requestLine = br.readLine();
        //Make the server directory C:\Webserver
        String root_dir;
        root_dir = "C:\\Webserver\\";
// Extract the filename from the request line. Assume a GET command
        StringTokenizer tokens = new StringTokenizer(requestLine);
        tokens.nextToken();
        String fileName = tokens.nextToken();
        // Drop the slash at the begginning.
        if(fileName.startsWith("/"))
            fileName = root_dir + fileName.substring(1,fileName.length());

        // Open the requested file.  
        FileInputStream fis = null;
        boolean fileExists = true;
        try {
            fis = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
            fileExists = false;
        }
   
        // Construct the response message.
        String statusLine = null;
        String contentTypeLine = null;
        String entityBody = null;
           
        if (fileExists) {
            statusLine = "HTTP/1.0 200 OK" + CRLF;
            contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
        } else {
            statusLine = "HTTP/1.0 404 Not Found" + CRLF;
            contentTypeLine = "NONE";
            entityBody = "\n\n Sorry The File You Requested Is Not There!!";
        }
       
        // Send the status line.
        os.writeBytes(statusLine);
       
        // Send the content type line.
        os.writeBytes(contentTypeLine);
       
        // Send a blank line to indicate the end of the header lines.
        os.writeBytes(CRLF);
       
        // Send the entity body.
        if (fileExists) {
            sendBytes(fis, os);
            fis.close();
        } else {
            os.writeBytes(entityBody);
        }
       
        //Close the streams
        os.close();
        br.close();
        socket.close();
    }

    private String contentType(String fileName) {
        if(fileName.endsWith(".htm") || fileName.endsWith(".html"))
            return "text/html";
        else if(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))
            return "image/jpeg";
        else if(fileName.endsWith(".gif"))
            return "image/gif";
        else if(fileName.endsWith(".txt"))
            return "text/plain";
        else
            return "application/octet-stream";
    }
         
    private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception {
        // Construct a 1K buffer to hold bytes on their way to the socket.
        byte[] buffer = new byte[1024];
        int bytes = 0;
       
        // Copy requested file into the socket's output stream.
        while((bytes = fis.read(buffer)) != -1 )
            os.write(buffer, 0, bytes);
    }
}


I have found several sites on the subject but have not been able to find an in depth example.

Thanks

0
Comment
Question by:jmcdonald69124
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 1

Expert Comment

by:helloexpert
ID: 12200869
In HTTP 1.1, by default keep alive must be enabled, unless there is an HTTP header stating that
Connection: close

You must parse the http request headers to check for such a message from the client.

To keep alive, i.e, to not close the connection, you will have to modify the processRequest() method in your program.

Here, at the end of the method, you are closing the input streams, which essentially closes the socket connection. You must move the contents of this method into an infinite loop. Then try to read the stream using a non-blocking technique...i.e, check the availability of bytes to read. Otherwise sleep for a small time (say 100ms) and check again.  Have a time-out of say 60 seconds.  i.e, if there has been no activity for some 600 loops, then close the connection.

if the HTTP header requesting not to keep alive is set (Connection: close), then do not go into the loop. Break out after the first iteration.

That is a primitive way of doing things. But given the primitive web server, it should be good enough :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12204673
0
 
LVL 92

Expert Comment

by:objects
ID: 12204833
ignore my comment :)
0
 
LVL 92

Expert Comment

by:objects
ID: 12204872
perhaps have a look at jigsawand see how they handle it:
http://www.w3.org/Jigsaw/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12205236
You would be better off doing this with NIO:

http://javaalmanac.com/egs/java.nio/NbServer.html
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 250 total points
ID: 12340698
/*
 * Created on Oct 18, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

/**
 * @author Tommy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;

public final class WebServer {
    public static void main(String args[]) throws Exception {

        //Establish the listen socket
        int PORT = 777;    
        ServerSocket listenSocket = new ServerSocket(PORT);
       

        //Process HTTP service requests in an infinite loop
        while(true) {
            //listen for TCP connection request
            //Construct an object to process the HTTP request message
               HttpConnection request = new HttpConnection(listenSocket.accept());
            //HttpRequest request = new HttpRequest(listenSocket.accept());
            Thread thread = new Thread(request);
            thread.start();
        }
    }
}

/**
 * Defaults to keeping the connection alive
 *
 * @author Tommy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
class HttpConnection implements Runnable
{
    private Socket connection;
    private boolean keepAlive;
   
    public HttpConnection(Socket connection)
    {
        this.connection = connection;
        this.keepAlive = true;
    }
   
    public HttpConnection(Socket connection, boolean keepAlive)
    {
        this(connection);
        this.keepAlive = keepAlive;
    }
   
    public void run()
    {
        while (keepAlive)
        {
            HttpRequest req = new HttpRequest(this);
            try
            {
                req.processRequest();
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try
        {
            connection.close();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public Socket getConnection()
    {
        return connection;
    }
   
    /**
     * @return Returns the keepAlive.
     */
    public boolean isKeepAlive()
    {
        return keepAlive;
    }
   
    /**
     * @param keepAlive The keepAlive to set.
     */
    public void setKeepAlive(boolean keepAlive)
    {
        this.keepAlive = keepAlive;
    }
}

final class HttpRequest implements Runnable {

    final static String CRLF ="\r\n";
    HttpConnection conn;

    public HttpRequest(HttpConnection conn) {
        this.conn = conn;
    }


    //Implement the run() method of the Runnable interface
    public void run() {
        try {
            processRequest();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void processRequest() throws Exception {
        //Get references to sockets input and output streams
        InputStream is = this.conn.getConnection().getInputStream();
        DataOutputStream os = new DataOutputStream(this.conn.getConnection().getOutputStream());
       
        //Set up input stream filter
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
       
        //Get the request line of HTTP message
        String requestLine = br.readLine();
        //Make the server directory C:\Webserver
        String root_dir;
        root_dir = "C:\\Webserver\\";
// Extract the filename from the request line. Assume a GET command
        StringTokenizer tokens = new StringTokenizer(requestLine);
        tokens.nextToken();
        String fileName = tokens.nextToken();
        // Drop the slash at the begginning.
        if(fileName.startsWith("/"))
            fileName = root_dir + fileName.substring(1,fileName.length());

        // Open the requested file.  
        FileInputStream fis = null;
        boolean fileExists = true;
        try {
            fis = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
            fileExists = false;
        }
   
        // Construct the response message.
        String statusLine = null;
        String contentTypeLine = null;
        String entityBody = null;
           
        if (fileExists) {
            statusLine = "HTTP/1.0 200 OK" + CRLF;
            contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
        } else {
            statusLine = "HTTP/1.0 404 Not Found" + CRLF;
            contentTypeLine = "NONE";
            entityBody = "\n\n Sorry The File You Requested Is Not There!!";
        }
       
        // Send the status line.
        os.writeBytes(statusLine);
       
        // Send the content type line.
        os.writeBytes(contentTypeLine);
       
        // Send a blank line to indicate the end of the header lines.
        os.writeBytes(CRLF);
       
        // Send the entity body.
        if (fileExists) {
            sendBytes(fis, os);
            fis.close();
        } else {
            os.writeBytes(entityBody);
        }
       
        //Close the streams
        os.close();
        br.close();
//        socket.close();
    }

    private String contentType(String fileName) {
        if(fileName.endsWith(".htm") || fileName.endsWith(".html"))
            return "text/html";
        else if(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))
            return "image/jpeg";
        else if(fileName.endsWith(".gif"))
            return "image/gif";
        else if(fileName.endsWith(".txt"))
            return "text/plain";
        else
            return "application/octet-stream";
    }
         
    private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception {
        // Construct a 1K buffer to hold bytes on their way to the socket.
        byte[] buffer = new byte[1024];
        int bytes = 0;
       
        // Copy requested file into the socket's output stream.
        while((bytes = fis.read(buffer)) != -1 )
            os.write(buffer, 0, bytes);
    }
}


\t
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12340704
The changes models the connection to the client as something separate from the request itself. You will need to make changes in the HttpRequest class, or the HttpConnection class to cater for whether keepAlive should be on. Currently the default setting is keepAlive = true.

\t
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 250 total points
ID: 12346119
Hi jmcdonald69124,

Currently, i'm develloping a HTTP/1.1 web server for about 4 months, and have implemented the keep-alive feature already (it work very well).

Firstly, you need to read the header from the request, otheriwse, you will not be able to read further request with the same connection.

Secondly, you have to inform the client about the connection.

Thirdly, you must put 2 CRLF before the content to separate it from the header.

( see RFC2616 http://www.ietf.org/rfc/rfc2616.txt )

       
        //Get the request line of HTTP message
        String requestLine = br.readLine();

        HashMap hm_header=new HashMap(); // <-- for input header
        String line;
        while ( (line=br.readLine()).length()>0)
        {
              int i=line.indexOf(':');
              if (i>0) hm_header.put(line.substring(0,i),line.substring(i+1).trim());
        }

        //Make the server directory C:\Webserver
        String root_dir;
        root_dir = "C:\\Webserver\\";
// Extract the filename from the request line. Assume a GET command
        StringTokenizer tokens = new StringTokenizer(requestLine);
        tokens.nextToken();
        String fileName = tokens.nextToken();
        // Drop the slash at the begginning.
        if(fileName.startsWith("/"))
            fileName = root_dir + fileName.substring(1,fileName.length());

        // Open the requested file.  
        FileInputStream fis = null;
        boolean fileExists = true;
        try {
            fis = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
            fileExists = false;
        }
   
        // Construct the response message.
        String statusLine = null;
        String contentTypeLine = null;
        String entityBody = null;

        if (fileExists) {
            statusLine = "HTTP/1.0 200 OK" + CRLF + "Connection: Keep-Alive" + CRLF + CRLF; // <---
            contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
        } else {
            statusLine = "HTTP/1.0 404 Not Found" + CRLF + CRLF;  // <---
            contentTypeLine = "NONE";
            entityBody = "\n\n Sorry The File You Requested Is Not There!!";
        }

        // Send the status line.
        os.writeBytes(statusLine);

0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12346481
:-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12346586
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12349476
=-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now