com.dalsemi.tininet.http
Class HTTPServer

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

public class HTTPServer
extends java.lang.Object

This class implements an HTTP server. This class currently only supports the GET and POST methods. 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);
   //...
 }
 

POST details:

POST only accepts content of type: application/x-www-form-urlencoded Default input max value is 1000 bytes for post. This can be increased up to 3000 bytes by calling the setBufferSize method. The POST handling class must either be included in the .tini file with the server implementation, or in the TINI's file system. The handling class must implement the interface class PostScript, and its method handlePost.

The handling class must be named in the server's index page as follows: If the handler is named myHandler.class the index page should use ACTION="myHandler" in the form. handlePost takes as a parameter the socket output stream from the connection. This allows the designer to dynamically create an response page of any type desired. This allow for increased flexibility.

For a simple example using this POST functionality, look to the examples directory -> POSTExample


Field Summary
 int buffSize
          This is the size for the incoming data buffer
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_ENTITY_TOO_LARGE
          Standard HTTP_ENITY_TOO_LARGE 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 java.lang.String MIME_APP_OS
          MIME_APP_OS Content type
static java.lang.String MIME_IMAGE_BMP
          MIME_IMAGE_BMP Content type there seem to be multiple mime types for this so we cannot make this final
static java.lang.String MIME_IMAGE_GIF
          MIME_TEXT_PLAIN Content type
static java.lang.String MIME_IMAGE_JPG
          MIME_TEXT_PLAIN Content type
static java.lang.String MIME_TEXT_HTML
          MIME_TEXT_PLAIN Content type
static java.lang.String MIME_TEXT_PLAIN
          MIME_TEXT_PLAIN Content type
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
 java.lang.String getHTTPRoot()
          Returns the HTTP root of the server.
 java.lang.String getIndexPage()
          Returns the server's default index page.
 java.lang.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(java.lang.Object lock)
          Checks for incoming client HTTP request and services supported requests that are detected.
static void setBitmapMimeType(java.lang.String newMimeType)
          Allows user to change the mime type for bitmaps.
 void setBufferSize(int newSize)
          This method is used to modify the size of the recive buffer.
 void setHTTPRoot(java.lang.String httpRoot)
          Sets the http root.
 void setIndexPage(java.lang.String indexPage)
          Sets the server's index page.
 void setLogFilename(java.lang.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_ENTITY_TOO_LARGE

public static final int HTTP_ENTITY_TOO_LARGE
Standard HTTP_ENITY_TOO_LARGE 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

MIME_TEXT_PLAIN

public static final java.lang.String MIME_TEXT_PLAIN
MIME_TEXT_PLAIN Content type

MIME_TEXT_HTML

public static final java.lang.String MIME_TEXT_HTML
MIME_TEXT_PLAIN Content type

MIME_IMAGE_GIF

public static final java.lang.String MIME_IMAGE_GIF
MIME_TEXT_PLAIN Content type

MIME_IMAGE_JPG

public static final java.lang.String MIME_IMAGE_JPG
MIME_TEXT_PLAIN Content type

MIME_IMAGE_BMP

public static java.lang.String MIME_IMAGE_BMP
MIME_IMAGE_BMP Content type there seem to be multiple mime types for this so we cannot make this final

MIME_APP_OS

public static final java.lang.String MIME_APP_OS
MIME_APP_OS Content type

buffSize

public int buffSize
This is the size for the incoming data buffer
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(java.lang.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 java.lang.String getLogFilename()
Returns the name of the log file. This represents the file name including path.
Returns:
log file name

setLogFilename

public void setLogFilename(java.lang.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 java.lang.String getHTTPRoot()
Returns the HTTP root of the server.
Returns:
server root

setHTTPRoot

public void setHTTPRoot(java.lang.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 java.lang.String getIndexPage()
Returns the server's default index page.
Returns:
current index page

setIndexPage

public void setIndexPage(java.lang.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

setBufferSize

public void setBufferSize(int newSize)
This method is used to modify the size of the recive buffer. Minimum size 1000, Maximum size 3000
Parameters:
newSize - the desired size of the buffer

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(java.lang.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


Also see:
o TINI 1.16 API
o TINI Home Page
o 1-Wire API

o JDK 1.1

Last update Wed Jun 8 17:19:36 CDT 2005