This is a rudimentary HTTP server which makes embedding a web server into your project a doddle.
Simply create the HTTPServer object, add it to the network stack, add callbacks for the pages you want to serve, and tell it to listen. The server function then serves a single page at a time (call it in a loop):
HTTPServer http;
then
Network.addPort(&http); http.addPage("/",root); http.addPage("/index.html",root); http.addPage("/page1.html",page1); http.addPage("/page2.html",page2); http.listen(80);
When a page is requested and is found to match the path specified by addPage, the function in that addPage is called.
The format of these functions are:
int pageFunction(TCPClient *client, struct get *getVars, int getVarCount)
You send data back to the browser using the client object (using .print, .println etc), and the parameters to the get request (the bits after the ? in a url, such as index.html?param1=39¶m2=993) are in the getVars array. The number of parameters is in the getVarCount variable.
You can access the individual parameters and their values using the .key and .val entries in the structure, for example:
for(int i=0; i<getVarCount; i++) { if(strcmp("param1",getVars[i].key)==0) if(getVars[i].val != NULL) param1 = atoi(getVars[i].val); if(strcmp("param2",getVars[i].key)==0) if(getVars[i].val != NULL) param2 = atoi(getVars[i].val); }
You return the HTTP status code (usually 200) from the function. Currently this doesn't actually get used, but it would be nice to be able to send different codes to enable such things as redirects, authentication, etc.
Running the web server itself is done with the server() method, which has to be run continuously in order to serve pages:
void loop() { http.server(); }