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("Setting up...\n"); EthernetErr ethErr = eth.setup(); if(ethErr) { printf("Error %d in setup.\n", ethErr); return -1; } printf("Setup OK\n"); svr.addHandler<SimpleHandler>("/"); //Default handler svr.bind(80); printf("Listening...\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:
Packages¶
Precompiled version:
Import libraryHTTPServer
This library is deprecated.
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)
HTTP Server Requests Handlers¶
- Simple "Hello world" handler
- Filesystem handler
- RPC handler
You can also Create your custom handler.
Includes¶
#include "HTTPServer.h"
Reference¶
Import program
Public Member Functions |
|
HTTPServer () | |
Instantiates the HTTP Server.
|
|
template<typename T > | |
void | addHandler (const char *path) |
Adds a handler.
|
|
void | bind (int port=80) |
Starts listening.
|
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 either under the /files/ path. Since FSHandler is the default handler here, the mbed file system (/webfs/) is made available on the root path of the server as well, so mbed.htm is available at the URLs http://a.b.c.d/files/mbed.htm and http://a.b.c.d/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("Setting up...\n"); EthernetErr ethErr = eth.setup(); if(ethErr) { printf("Error %d in setup.\n", ethErr); return -1; } printf("Setup OK\n"); FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path svr.addHandler<SimpleHandler>("/hello"); svr.addHandler<RPCHandler>("/rpc"); svr.addHandler<FSHandler>("/files"); svr.addHandler<FSHandler>("/"); //Default handler //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm svr.bind(80); printf("Listening...\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:
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.