Solved

HTTP1.1 keep-alive support for simple server

Posted on 2004-10-01
11
986 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 56
simple java question 3 56
servlet example 17 32
Running JavaFX on JDeveloper 12C 1 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 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