Marco Oehler / Mbed OS Lab7
Embed: (wiki syntax)

« Back to documentation index

HTTPServer Class Reference

HTTPServer Class Reference

The HTTPServer class implements a simple webserver that is able to transmit files over an ethernet connection and allows to call scripts that are registered with the server. More...

#include <HTTPServer.h>

Public Member Functions

 HTTPServer (EthernetInterface &ethernet)
 Create and initialize an http server object.
virtual ~HTTPServer ()
 Delete the http server object.

Detailed Description

The HTTPServer class implements a simple webserver that is able to transmit files over an ethernet connection and allows to call scripts that are registered with the server.


An http server can be created and started as follows:


   EthernetInterface* ethernet = new EthernetInterface();  // init the TCP/IP stack
   ethernet->set_network("192.168.0.10", "255.255.255.0", "192.168.0.1");
   ethernet->connect();
   HTTPServer* httpServer = new HTTPServer(ethernet); // creates an http server
   ...
 

This http server allows to execute application specific code implemented as http scripts. These scripts are objects derived from the HTTPScript superclass.
An example of an application specific script is given below:


 class MyHTTPScript : public HTTPScript {
     public:
                  MyHTTPScript();
         virtual  ~MyHTTPScript();
         string   call(vector<string> names, vector<string> values);
 };
 string MyHTTPScript::call(vector<string> names, vector<string> values) {
   string response;
   response += "  <h2>";
   for (uint32_t i = 0; i < min(names.size(), values.size()); i++) {
     response += "  <p>"+names[i]+"="+values[i]+"</p>";
   }
   response += "  </h2>";
   return response;
 }
 

This script returns the parameters that were passed to it by the http server.
Before this script can be used, it needs to be registered with the http server with the add() method as follows:


   httpServer->add("myScript", new MyHTTPScript());
 

When the call() method of the script is called by the http server, it receives two string vectors: a vector with the names of the arguments passed in the URL, and a vector with the corresponding values of the arguments.
An example of an http request calling this script is as follows:


   http://192.168.1.10/cgi-bin/myScript?x=0.5&y=-0.1&z=0.2
 

The vectors of arguments passed to the call() method are then {'x', 'y', 'z'} for the names and {'0.5', '-0.1', '0.2'} for the values.
The response of the call() method is a string object which is placed within an xhtml page, which in turn is returned by the http server to the requesting http client.

See also:
HTTPScript

Definition at line 79 of file HTTPServer.h.


Constructor & Destructor Documentation

HTTPServer ( EthernetInterface &  ethernet )

Create and initialize an http server object.

Parameters:
etherneta reference to the embedded TCP/IP stack to use.

Definition at line 28 of file HTTPServer.cpp.

~HTTPServer (  ) [virtual]

Delete the http server object.

Definition at line 38 of file HTTPServer.cpp.