httpServer example program for WIZwiki-W7500 Only LED control

Dependents:   httpServer-WIZwiki-W7500

Embed: (wiki syntax)

« Back to documentation index

HTTPServer Class Reference

HTTPServer Class Reference

Class HTTPServer for use with WiflyInterface. More...

#include <HTTPServer.h>

Data Structures

struct  handlersComp
 Structure which will allow to order the stored handlers according to their associated path. More...

Public Member Functions

 HTTPServer ()
 Constructor for HTTPServer objects.
 ~HTTPServer ()
 Destructor for HTTPServer objects.
template<typename T >
void addHandler (const char *path)
 Adds a request handler to the handlers list.
void addErrorHandler (HTTPRequestHandlerFunction hdlFunc)
 Replaces the standard error Handler.
bool start (int port=80, EthernetInterface *pEthernet=NULL)
 Binds server to a specific port and starts listening.
int poll (bool blocking=true)
 Performs the regular polling of the server component.

Detailed Description

Class HTTPServer for use with WiflyInterface.

This class is a simple implementation of an HTTP Server for use with the WiFly interface. The class will listen for incoming connections on the (configurable) HTTP port. For each incoming connection, one request will be processed. After instantiating this class, add all required handlers using the addHandler function, replace the default error handler using addErrorHandler if needed and call the start method to initialize the class. You need to continuously poll for any new incoming connections after one request has been served using the poll member function.

Example:

 #include "mbed.h"
 #include "HTTPServer.h"
 #include "LocalFileSystem.h"

 LocalFileSystem local("local"); 

 void main(void)
 {
     HTTPServer svr;
     svr.mount("/local/", "/");
     svr.addHandler<HTTPFsRequestHandler>( "/" );
     svr.start();
     while(1)
     {
         if (svr.poll() < 0)
             exit(0);
     }
 }

An alternate approach e.g. if you need to perform additional tasks using the EthernetInterface there is the possibility to provide the EthernetInterface object in an initialized and connected state. __NOTE: You should choose this scenario for compatibility reasons.___

Example2:

 #include "mbed.h"
 #include "HTTPServer.h"
 #include "EthernetInterface.h"
 #include "LocalFileSystem.h"

 LocalFileSystem local("local");
 EthernetInterface eth;

 void main(void)
 {
     HTTPServer svr;
     //    Initialize the ethernet interface
     if (eth.init() != 0) {
         printf("Initialization of EthernetInterface failed !");
         exit(0);
     }
     //    Connect using DHCP
     if (eth.connect() !=0) {
         printf("Failed to connect using DHCP !");
         exit(0);
     }
     
     // Moint the local file system and provide a handler for 'root'.
     svr.mount("/local/", "/");
     svr.addHandler<HTTPFsRequestHandler>( "/" );
     // Start the server on port 80, providing our own ethernet interface object.
     svr.start(80, &eth);
     while(1)
     {
         if (svr.poll() < 0)
             exit(0);
     }
 }

Definition at line 117 of file HTTPServer.h.


Constructor & Destructor Documentation

HTTPServer (  )

Constructor for HTTPServer objects.

Definition at line 10 of file HTTPServer.cpp.

~HTTPServer (  )

Destructor for HTTPServer objects.

Definition at line 16 of file HTTPServer.cpp.


Member Function Documentation

void addErrorHandler ( HTTPRequestHandlerFunction  hdlFunc )

Replaces the standard error Handler.

The error Handler will be called everytime a request is not matching any of the registered paths or uris.

Parameters:
hdlFunc,:User specified handler function which will be used in error conditions.

Definition at line 165 of file HTTPServer.h.

void addHandler ( const char *  path )

Adds a request handler to the handlers list.

You will have to use one of the existing implementations. With each handler a uri or path is associated. Whenever a request is received the server will walk through all registered handlers and check which path is matching.

Parameters:
T: class which will be instanciated to serve these requests for the associated path.
path: request uri starting with this path will be served using this handler.

Definition at line 157 of file HTTPServer.h.

int poll ( bool  blocking = true )

Performs the regular polling of the server component.

Needs to be called cyclically. The function will internally check whether new connections are requested by a client and will also poll all existing client connections.

Parameters:
blocking: if true,
Returns:
-1 if there was a problem. If 0 is returned, the latest request was served successfully and the server is ready for processing the next request. Simply call poll as long as you want to serve new incoming requests.

Definition at line 70 of file HTTPServer.cpp.

bool start ( int  port = 80,
EthernetInterface *  pEthernet = NULL 
)

Binds server to a specific port and starts listening.

This member prepares the internal variables and the server socket and terminates after successfull initialization

Parameters:
port: port on which to listen for incoming connections
pEthernet: a pointer to an existing EthernetInterface object or NULL if the HTTPServer shall allocate the object. _Please note that for compatibility reasons your should consider to create the EthernetInterface as a static variable. Otherwise the the object will be created on the heap._
Returns:
: false if an unrecoverable error occured or if the ethernet interface was not set or not initialized correctly, or true if everything was ok.

Definition at line 36 of file HTTPServer.cpp.