You are viewing an older revision! See the latest version

HTTP Server

This page explains how to implement an HTTP Server on your mbed. It uses the Networking stack.

Hello World!

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HttpServer.h"

EthernetNetIf eth;  
HttpServer svr;

DigitalOut led1(LED1);

int main() {
  printf("\r\nSetting up...\r\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("\r\nSetup OK\r\n");
  
  svr.addHandler<SimpleHandler>("/"); //Default handler
  svr.bind(80);
  
  printf("\r\nListening...\r\n");
    
  Timer tm;
  tm.start();
  //Listen indefinitely
  while(true)
  {
    Net::poll();
    if(tm.read()>.5)
    {
      led1=!led1; //Show that we are alive
      tm.start();
    }
  }
  
  return 0;
}

This program is available here: http://mbed.org/users/donatien/programs/NetHttpServerHelloWorld.

Packages

Precompiled version: http://mbed.org/users/donatien/programs/NetServicesLPC1768

Library

Architecture

The HttpServer is composed of:

  • The actual server (HttpServer)
  • A request dispatcher, instanciated on each request (HttpRequestDispatcher)
  • Request handlers instanciated by the dispatcher(deriving from HttpRequestHandler)

Requests Handler

You can also Create your custom handler.

Includes

#include "HttpServer.h"

Reference

HttpServer()

Instantiate the HTTP server.

template<typename T>
void addHandler(const char* path)

Append an handler of type T to the handlers list, for requests of path starting with path.

void bind(int port = 80) 

Bind the server to port port and start listening. This function returns immediately. You must call Net::poll() regularly after this so that the server serves requests properly.

Examples

This example sets the server up and demonstrates the use of all-three kinds of handlers.

The local file system is available under the /webfs/ path. Since FSHandler is the default handler here, the mbed file system (/) is made available on the root path of the server as well, so mbed.htm is available at the URL : http://a.b.c.d/webfs/mbed.htm, where a.b.c.d is your mbed's IP address.

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HttpServer.h"

DigitalOut led1(LED1, "led1");
DigitalOut led2(LED2, "led2");
DigitalOut led3(LED3, "led3");
DigitalOut led4(LED4, "led4");

LocalFileSystem fs("webfs");

EthernetNetIf eth;  
HttpServer svr;

int main() {
  Base::add_rpc_class<DigitalOut>();

  printf("\r\nSetting up...\r\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("\r\nSetup OK\r\n");
  
  svr.addHandler<SimpleHandler>("/hello");
  svr.addHandler<RpcHandler>("/rpc");
  svr.addHandler<FSHandler>(""); //Default handler
  //Example : Access to mbed.htm : http://a.b.c.d/webfs/mbed.htm
  
  svr.bind(80);
  
  printf("\r\nListening...\r\n");
    
  Timer tm;
  tm.start();
  //Listen indefinitely
  while(true)
  {
    Net::poll();
    if(tm.read()>.5)
    {
      led1=!led1; //Show that we are alive
      tm.start();
    }
  }
  
  return 0;

}

You can also directly import this example there: http://mbed.org/users/donatien/programs/NetHttpServerExample

Robustness

Although this HTTP Server should still be considered in beta phasis, it has been built for robustness. A good test is this web page that you can use with the previous example: put it on your mbed and open it in your browser (at an address like http://a.b.c.d/webfs/stress.htm): http://mbed.org/media/uploads/donatien/stress.htm.

It will start an asynchronous HTTP Request every 100ms, sending RPC commands to set the leds on and off. You will see that all requests are not successfully processed, but the server will not crash and will remain up and running properly after you close the page.


All wikipages