Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

HTTP1.1 keep-alive support for simple server

Posted on 2004-10-01
11
Medium Priority
?
1,028 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Expert Comment

by:Tommy Braas
ID: 12349476
=-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

598 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