com.dalsemi.tininet.http
Class HTTPServer

java.lang.Object
  |
  +--com.dalsemi.tininet.http.HTTPServer

public class HTTPServer
extends Object

This class implements an HTTP server. This class currently only supports the GET method. The serviceRequests method blocks on a ServerSocket accept call. A new thread is spawned to service each new connection. The method serviceRequest(Object lock) can be used to synchronize access, using the lock object, to a requested web page. This might be useful if the server dynamically updates a page.

Usage

Example


 boolean    webServerEnabled = true;
 Object     lock = new Object();
 HTTPServer httpServer = new HTTPServer();
 boolean    loggingFailed = false;

 //...

 try
 {
   // create an instance of HTTPServer on port httpPort
   httpServer  = new HTTPServer(httpPort);
   // override the default index page
   httpServer.setIndexPage(webIndex);
   // override the default HTTP root
   httpServer.setHTTPRoot(webRoot);
   // override the default log file name
   httpServer.setLogFilename(webLog);
 }
 catch(HTTPServerException h)
 {
   System.out.println(h.toString());
 }

 try
 {
   // enable logging
   // NOTE: the log file is always appended and
   //       will eventually consume all free memory
   //       if it is not managed.
   httpServer.setLogging(true);
 }
 catch(HTTPServerException h)
 {
   // problem with log file
   loggingFailed = true;

   if(debugOn)
   {
     System.out.println(h.toString());
   }
 }

 while(webServerEnabled)
 {
   httpServer.serviceRequests(lock);
   //...
 }
 


Field Summary
static int DEFAULT_HTTP_PORT
          Default port number (port 80)
static int DELETE
          DELETE request
static int GET
          GET request
static int HEAD
          HEAD request
static int HTTP_BAD_REQUEST
          Standard HTTP_BAD_REQUEST response
static int HTTP_CREATED
          Standard HTTP_CREATED response
static int HTTP_FORBIDDEN
          Standard HTTP_FORBIDDEN response
static int HTTP_INTERNAL_ERROR
          Standard HTTP_INTERNAL_ERROR response
static int HTTP_NOT_FOUND
          Standard HTTP_NOT_FOUND response
static int HTTP_OK
          Standard HTTP_OK response
static int HTTP_SERVER_ERROR
          Standard HTTP_SERVER_ERROR response
static int HTTP_UNAUTHORIZED
          Standard HTTP_UNAUTHORIZED response
static int HTTP_UNSUPPORTED_TYPE
          Standard HTTP_UNSUPPORTED_TYPE response
static int OPTIONS
          OPTIONS request
static int POST
          POST request
static int PUT
          PUT request
static int TRACE
          TRACE request
static int TYPE_FULL_REQUEST
          Standard type TYPE_FULL_REQUEST
static int TYPE_FULL_RESPONSE
          Standard type TYPE_FULL_RESPONSE
static int TYPE_SIMPLE_REQUEST
          Standard type TYPE_SIMPLE_REQUEST
static int UNSUPPORTED
          Request unsupported
 
Constructor Summary
HTTPServer()
          Default constructor.
HTTPServer(int httpPort)
          Creates an HTTPServer using port httpPort.
HTTPServer(int httpPort, boolean logEnabled)
          Creates an HTTPServer using port httpPort.
 
Method Summary
 String getHTTPRoot()
          Returns the HTTP root of the server.
 String getIndexPage()
          Returns the server's default index page.
 String getLogFilename()
          Returns the name of the log file.
 boolean getLogging()
          Returns the logging status.
 int getPortNumber()
          Returns the server's current port number.
 int serviceRequests()
          Checks for incoming client HTTP request and services supported requests that are detected.
 int serviceRequests(Object lock)
          Checks for incoming client HTTP request and services supported requests that are detected.
static void setBitmapMimeType(String newMimeType)
          Allows user to change the mime type for bitmaps.
 void setHTTPRoot(String httpRoot)
          Sets the http root.
 void setIndexPage(String indexPage)
          Sets the server's index page.
 void setLogFilename(String logFileName)
          Sets the log file name.
 void setLogging(boolean logEnabled)
          Sets the logging status.
 void setPortNumber(int httpPort)
          Sets the server's port number.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_OK

public static final int HTTP_OK
Standard HTTP_OK response

HTTP_CREATED

public static final int HTTP_CREATED
Standard HTTP_CREATED response

HTTP_BAD_REQUEST

public static final int HTTP_BAD_REQUEST
Standard HTTP_BAD_REQUEST response

HTTP_UNAUTHORIZED

public static final int HTTP_UNAUTHORIZED
Standard HTTP_UNAUTHORIZED response

HTTP_FORBIDDEN

public static final int HTTP_FORBIDDEN
Standard HTTP_FORBIDDEN response

HTTP_NOT_FOUND

public static final int HTTP_NOT_FOUND
Standard HTTP_NOT_FOUND response

HTTP_UNSUPPORTED_TYPE

public static final int HTTP_UNSUPPORTED_TYPE
Standard HTTP_UNSUPPORTED_TYPE response

HTTP_SERVER_ERROR

public static final int HTTP_SERVER_ERROR
Standard HTTP_SERVER_ERROR response

HTTP_INTERNAL_ERROR

public static final int HTTP_INTERNAL_ERROR
Standard HTTP_INTERNAL_ERROR response

TYPE_SIMPLE_REQUEST

public static final int TYPE_SIMPLE_REQUEST
Standard type TYPE_SIMPLE_REQUEST

TYPE_FULL_REQUEST

public static final int TYPE_FULL_REQUEST
Standard type TYPE_FULL_REQUEST

TYPE_FULL_RESPONSE

public static final int TYPE_FULL_RESPONSE
Standard type TYPE_FULL_RESPONSE

DEFAULT_HTTP_PORT

public static final int DEFAULT_HTTP_PORT
Default port number (port 80)

UNSUPPORTED

public static final int UNSUPPORTED
Request unsupported

GET

public static final int GET
GET request

POST

public static final int POST
POST request

HEAD

public static final int HEAD
HEAD request

OPTIONS

public static final int OPTIONS
OPTIONS request

PUT

public static final int PUT
PUT request

DELETE

public static final int DELETE
DELETE request

TRACE

public static final int TRACE
TRACE request
Constructor Detail

HTTPServer

public HTTPServer()
           throws HTTPServerException
Default constructor. Uses the default port and disables logging.
Throws:
HTTPServerException - if server instantiation error occurs

HTTPServer

public HTTPServer(int httpPort)
           throws HTTPServerException
Creates an HTTPServer using port httpPort. Logging is disabled.
Parameters:
httpPort - port number for server
Throws:
HTTPServerException - if server instantiation error occurs

HTTPServer

public HTTPServer(int httpPort,
                  boolean logEnabled)
           throws HTTPServerException
Creates an HTTPServer using port httpPort. Logging is enabled if logEnabled is true.
Parameters:
httpPort - port number for server
logEnabled - set logging option
Throws:
HTTPServerException - if server instantiation error occurs
Method Detail

setBitmapMimeType

public static void setBitmapMimeType(String newMimeType)
Allows user to change the mime type for bitmaps. There seem to be several mime-types for bmp files for different browsers/servers.

getLogging

public boolean getLogging()
Returns the logging status.
Returns:
true if logging is enabled

setLogging

public void setLogging(boolean logEnabled)
                throws HTTPServerException
Sets the logging status. If logging is enabled the server will attempt to open a new log file if one does not exist. If logging is disabled the server will attempt to close the log file. Log files are always appended.

NOTE: Logging is discouraged since the memory on TINI is limited

Parameters:
logEnabled - true if the server is to write to a default log file
Throws:
HTTPServerException - if a log file error occurs

getLogFilename

public String getLogFilename()
Returns the name of the log file. This represents the file name including path.
Returns:
log file name

setLogFilename

public void setLogFilename(String logFileName)
Sets the log file name. If the path is not given, the file will be written to the current directory.
Parameters:
logFileName - name of the log file

getHTTPRoot

public String getHTTPRoot()
Returns the HTTP root of the server.
Returns:
server root

setHTTPRoot

public void setHTTPRoot(String httpRoot)
Sets the http root. All files from httpRoot to the leaves of the directory tree can be accessed by HTTP requests. Files between the root of the file system and httpRoot cannot be accessed with HTTP requests.
Parameters:
httpRoot - path indicating the root of the server

getIndexPage

public String getIndexPage()
Returns the server's default index page.
Returns:
current index page

setIndexPage

public void setIndexPage(String indexPage)
Sets the server's index page.
Parameters:
indexPage - page to be used by the server as the default index page

getPortNumber

public int getPortNumber()
Returns the server's current port number.
Returns:
current port number

setPortNumber

public void setPortNumber(int httpPort)
                   throws HTTPServerException
Sets the server's port number. Note: The server will close any open ports and opens the new port.
Parameters:
httpPort - number of the port to be opened
Throws:
HTTPServerException - if the port is opened by another server

serviceRequests

public int serviceRequests()
                    throws HTTPServerException
Checks for incoming client HTTP request and services supported requests that are detected.
Returns:
0 is returned (return value is legacy from pre-Thread TINI APIs)
Throws:
HTTPServerException - if a server error occurs

serviceRequests

public int serviceRequests(Object lock)
                    throws HTTPServerException
Checks for incoming client HTTP request and services supported requests that are detected. The lock is used to syncronize access to a web page.
Parameters:
lock - lock for exclusive access to web page
Returns:
0 is returned (return value is legacy from pre-Thread TINI APIs)
Throws:
HTTPServerException - if a server error occurs