You are viewing an older revision! See the latest version
HTTP Server
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¶
- Simple Handler - Simple "Hello world" handler
- FSHandler - Filesystem handler
- RpcHandler - RPC 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>();
Net::setDefaultIf(eth);
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