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

« Back to documentation index

Show/hide line numbers HTTPServer.h Source File

HTTPServer.h

00001 /*
00002  * HTTPServer.h
00003  * Copyright (c) 2020, ZHAW
00004  * All rights reserved.
00005  */
00006 
00007 #ifndef HTTP_SERVER_H_
00008 #define HTTP_SERVER_H_
00009 
00010 #include <string>
00011 #include <vector>
00012 #include <mbed.h>
00013 #include <EthernetInterface.h>
00014 
00015 class HTTPScript;
00016 
00017 /**
00018  * The <code>HTTPServer</code> class implements a simple webserver that is able to
00019  * transmit files over an ethernet connection and allows to call scripts that are
00020  * registered with the server.
00021  * <br/>
00022  * An http server can be created and started as follows:
00023  * <pre><code>
00024  *   EthernetInterface* ethernet = new EthernetInterface();  <span style="color:#008000">// init the TCP/IP stack</span>
00025  *   ethernet->set_network("192.168.0.10", "255.255.255.0", "192.168.0.1");
00026  *   ethernet->connect();
00027  *
00028  *   HTTPServer* httpServer = new HTTPServer(ethernet); <span style="color:#008000">// creates an http server</span>
00029  *   ...
00030  * </code></pre>
00031  * This http server allows to execute application specific code implemented as http
00032  * scripts. These scripts are objects derived from the <code>HTTPScript</code> superclass.
00033  * <br/>
00034  * An example of an application specific script is given below:
00035  * <pre><code>
00036  * class MyHTTPScript : public HTTPScript {
00037  *     public:
00038  *                  MyHTTPScript();
00039  *         virtual  ~MyHTTPScript();
00040  *         string   call(vector<string> names, vector<string> values);
00041  * };
00042  * 
00043  * string MyHTTPScript::call(vector<string> names, vector<string> values) {
00044  *   
00045  *   string response;
00046  *   
00047  *   response += "  <h2>";
00048  *   for (uint32_t i = 0; i < min(names.size(), values.size()); i++) {
00049  *     response += "  <p>"+names[i]+"="+values[i]+"</p>";
00050  *   }
00051  *   response += "  </h2>";
00052  * 
00053  *   return response;
00054  * }
00055  * </code></pre>
00056  * This script returns the parameters that were passed to it by the http server.
00057  * <br/>
00058  * Before this script can be used, it needs to be registered with the http server
00059  * with the <code>add()</code> method as follows:
00060  * <pre><code>
00061  *   httpServer->add("myScript", new MyHTTPScript());
00062  * </code></pre>
00063  * When the <code>call()</code> method of the script is called by the http server,
00064  * it receives two string vectors: a vector with the names of the arguments passed
00065  * in the URL, and a vector with the corresponding values of the arguments.
00066  * <br/>
00067  * An example of an http request calling this script is as follows:
00068  * <pre><code>
00069  *   http://192.168.1.10/cgi-bin/myScript?x=0.5&y=-0.1&z=0.2
00070  * </code></pre>
00071  * The vectors of arguments passed to the <code>call()</code> method are then
00072  * {'x', 'y', 'z'} for the names and {'0.5', '-0.1', '0.2'} for the values.
00073  * <br/>
00074  * The response of the <code>call()</code> method is a <code>string</code> object
00075  * which is placed within an xhtml page, which in turn is returned by the http
00076  * server to the requesting http client.
00077  * @see HTTPScript
00078  */
00079 class HTTPServer {
00080     
00081     public:
00082     
00083                     HTTPServer(EthernetInterface& ethernet);
00084         virtual     ~HTTPServer();
00085         void        add(std::string name, HTTPScript* httpScript);
00086         
00087     private:
00088         
00089         static const unsigned int   STACK_SIZE = 16384; // stack size of thread, given in [bytes]
00090         static const int            PORT_NUMBER = 80;   // port number of server to use
00091         static const unsigned int   INPUT_BUFFER_SIZE;  // size of receive buffer, given in [bytes]
00092         static const int            SOCKET_TIMEOUT;     // timeout of socket, given in [ms]
00093         
00094         EthernetInterface&          ethernet;
00095         TCPSocket                   server;
00096         std::vector<std::string>    httpScriptNames;
00097         std::vector<HTTPScript*>    httpScripts;
00098         Thread                      thread;
00099         
00100         string      urlDecoder(std::string url);
00101         void        run();
00102 };
00103 
00104 #endif /* HTTP_SERVER_H_ */
00105