A simple web server mainly based on ideas from Jasper Schuurmans Netduino web server

Dependents:   RdBlindsServer SpideyWallWeb RdGasUseMonitor

A fast and reliable web server for MBED! http://robdobson.com/2015/08/a-reliable-mbed-webserver/

It has a very neat way to implement REST commands and can serve files from local storage (on LPC1768 for instance) and from SD cards. It also has a caching facility which is particularly useful for serving files from local storage.

The server can be run in the main() thread (and has a sub-2ms response time if this is done) or in a mbed-rtos thread which increases the response time to (a still respectable) 30ms or so.

The latest project that uses this is here - https://developer.mbed.org/users/Bobty/code/SpideyWallWeb/

int main (void)
{
    // Ethernet interface
    EthernetInterface::init();

    // Connect ethernet
    EthernetInterface::connect();

    // Init the web server
    pc.printf("Starting web server\r\n");
    char* baseWebFolder = "/sd/";  // should be /sd/ for SDcard files - not used for local file system
    RdWebServer webServer;
    
    // Add commands to handle the home page and favicon
    webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, "index.htm", true);
    webServer.addCommand("favicon.ico", RdWebServerCmdDef::CMD_LOCALFILE, NULL, NULL, true);
    
    // Add the lightwall control commands
    webServer.addCommand("name", RdWebServerCmdDef::CMD_CALLBACK, &lightwallGetSystemName);
    webServer.addCommand("clear", RdWebServerCmdDef::CMD_CALLBACK, &lightwallClear);
    webServer.addCommand("rawfill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallRawFill);
    webServer.addCommand("fill", RdWebServerCmdDef::CMD_CALLBACK, &lightwallFill);
    webServer.addCommand("showleds", RdWebServerCmdDef::CMD_CALLBACK, &lightwallShowLeds);
    
    // Start the server
    webServer.init(WEBPORT, &led4, baseWebFolder);
    webServer.run();

}

// Get system name - No arguments required
char* lightwallGetSystemName(int method, char*cmdStr, char* argStr, char* msgBuffer, int msgLen, 
                int contentLen, unsigned char* pPayload, int payloadLen, int splitPayloadPos)
{
    // Perform any required actions here ....

    // ...

    // Return the system name
    return systemName;
}

This server was originally based on a Netduino web server from Jasper Schuurmans but has been optimised for speed.

Committer:
Bobty
Date:
Thu Sep 19 12:06:18 2013 +0000
Revision:
1:75bb184de749
Parent:
0:b5b4d07f7827
Child:
2:9d8793c23b46
Added code to support commands - still a lot of test code in place though

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobty 0:b5b4d07f7827 1 #include "RdWebServer.h"
Bobty 0:b5b4d07f7827 2
Bobty 1:75bb184de749 3 #define MAX_CMDSTR_LEN 100
Bobty 1:75bb184de749 4 #define MAX_ARGSTR_LEN 100
Bobty 1:75bb184de749 5
Bobty 0:b5b4d07f7827 6 RdWebServer::RdWebServer()
Bobty 0:b5b4d07f7827 7 {
Bobty 0:b5b4d07f7827 8 _serverIsListening = false;
Bobty 0:b5b4d07f7827 9 _pStatusLed = NULL;
Bobty 0:b5b4d07f7827 10 }
Bobty 0:b5b4d07f7827 11
Bobty 0:b5b4d07f7827 12 RdWebServer::~RdWebServer()
Bobty 0:b5b4d07f7827 13 {
Bobty 0:b5b4d07f7827 14 }
Bobty 0:b5b4d07f7827 15
Bobty 0:b5b4d07f7827 16 bool RdWebServer::init(int port, DigitalOut* pStatusLed)
Bobty 0:b5b4d07f7827 17 {
Bobty 0:b5b4d07f7827 18 _port = port;
Bobty 0:b5b4d07f7827 19 _pStatusLed = pStatusLed;
Bobty 0:b5b4d07f7827 20
Bobty 0:b5b4d07f7827 21 // _socketSrv.set_blocking(true);
Bobty 0:b5b4d07f7827 22
Bobty 0:b5b4d07f7827 23
Bobty 0:b5b4d07f7827 24 //setup tcp socket
Bobty 0:b5b4d07f7827 25 if(_socketSrv.bind(port)< 0)
Bobty 0:b5b4d07f7827 26 {
Bobty 0:b5b4d07f7827 27 printf("tcp server bind failed.\n\r");
Bobty 0:b5b4d07f7827 28 return false;
Bobty 0:b5b4d07f7827 29 }
Bobty 0:b5b4d07f7827 30 else
Bobty 0:b5b4d07f7827 31 {
Bobty 0:b5b4d07f7827 32 printf("tcp server bind successed.\n\r");
Bobty 0:b5b4d07f7827 33 _serverIsListening = true;
Bobty 0:b5b4d07f7827 34 }
Bobty 0:b5b4d07f7827 35
Bobty 0:b5b4d07f7827 36 if(_socketSrv.listen(1) < 0)
Bobty 0:b5b4d07f7827 37 {
Bobty 0:b5b4d07f7827 38 printf("tcp server listen failed.\n\r");
Bobty 0:b5b4d07f7827 39 return false;
Bobty 0:b5b4d07f7827 40 }
Bobty 0:b5b4d07f7827 41 else
Bobty 0:b5b4d07f7827 42 {
Bobty 0:b5b4d07f7827 43 printf("tcp server is listening...\n\r");
Bobty 0:b5b4d07f7827 44 }
Bobty 0:b5b4d07f7827 45
Bobty 0:b5b4d07f7827 46 return true;
Bobty 0:b5b4d07f7827 47 }
Bobty 0:b5b4d07f7827 48
Bobty 1:75bb184de749 49 bool RdWebServer::extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen)
Bobty 1:75bb184de749 50 {
Bobty 1:75bb184de749 51 *pCmdStr = '\0';
Bobty 1:75bb184de749 52 *pArgStr = '\0';
Bobty 1:75bb184de749 53 int cmdStrLen = 0;
Bobty 1:75bb184de749 54 int argStrLen = 0;
Bobty 1:75bb184de749 55 if (buf == NULL)
Bobty 1:75bb184de749 56 return false;
Bobty 1:75bb184de749 57 char* pSlash1 = strchr(buf, '/');
Bobty 1:75bb184de749 58 if (pSlash1 == NULL)
Bobty 1:75bb184de749 59 return false;
Bobty 1:75bb184de749 60 pSlash1++;
Bobty 1:75bb184de749 61 while(*pSlash1)
Bobty 1:75bb184de749 62 {
Bobty 1:75bb184de749 63 if (cmdStrLen >= maxCmdStrLen-1)
Bobty 1:75bb184de749 64 break;
Bobty 1:75bb184de749 65 if ((*pSlash1 == '/') || (*pSlash1 == ' ') || (*pSlash1 == '\n'))
Bobty 1:75bb184de749 66 break;
Bobty 1:75bb184de749 67 *pCmdStr++ = *pSlash1++;
Bobty 1:75bb184de749 68 *pCmdStr = '\0';
Bobty 1:75bb184de749 69 cmdStrLen++;
Bobty 1:75bb184de749 70 }
Bobty 1:75bb184de749 71 if ((*pSlash1 == '\0') || (*pSlash1 == ' ') || (*pSlash1 == '\n'))
Bobty 1:75bb184de749 72 return true;
Bobty 1:75bb184de749 73 *pSlash1++;
Bobty 1:75bb184de749 74 while(*pSlash1)
Bobty 1:75bb184de749 75 {
Bobty 1:75bb184de749 76 if (argStrLen >= maxArgStrLen-1)
Bobty 1:75bb184de749 77 break;
Bobty 1:75bb184de749 78 if ((*pSlash1 == ' ') || (*pSlash1 == '\n'))
Bobty 1:75bb184de749 79 break;
Bobty 1:75bb184de749 80 *pArgStr++ = *pSlash1++;
Bobty 1:75bb184de749 81 *pArgStr = '\0';
Bobty 1:75bb184de749 82 argStrLen++;
Bobty 1:75bb184de749 83 }
Bobty 1:75bb184de749 84 return true;
Bobty 1:75bb184de749 85 }
Bobty 0:b5b4d07f7827 86
Bobty 0:b5b4d07f7827 87 void RdWebServer::run()
Bobty 0:b5b4d07f7827 88 {
Bobty 0:b5b4d07f7827 89 TCPSocketConnection client;
Bobty 0:b5b4d07f7827 90 bool clientIsConnected = false;
Bobty 0:b5b4d07f7827 91
Bobty 0:b5b4d07f7827 92 //listening for http GET request
Bobty 0:b5b4d07f7827 93 while (isListening())
Bobty 0:b5b4d07f7827 94 {
Bobty 0:b5b4d07f7827 95 //blocking mode(never timeout)
Bobty 0:b5b4d07f7827 96 if(_socketSrv.accept(client)<0)
Bobty 0:b5b4d07f7827 97 {
Bobty 0:b5b4d07f7827 98 printf("failed to accept connection.\n\r");
Bobty 0:b5b4d07f7827 99 }
Bobty 0:b5b4d07f7827 100 else
Bobty 0:b5b4d07f7827 101 {
Bobty 0:b5b4d07f7827 102 printf("connection success!\n\rIP: %s\n\r",client.get_address());
Bobty 0:b5b4d07f7827 103 clientIsConnected = true;
Bobty 0:b5b4d07f7827 104 if (_pStatusLed != NULL)
Bobty 0:b5b4d07f7827 105 *_pStatusLed = true;
Bobty 0:b5b4d07f7827 106
Bobty 0:b5b4d07f7827 107 while(clientIsConnected)
Bobty 0:b5b4d07f7827 108 {
Bobty 0:b5b4d07f7827 109 char buffer[1024] = {};
Bobty 0:b5b4d07f7827 110 switch(client.receive(buffer, 1023))
Bobty 0:b5b4d07f7827 111 {
Bobty 0:b5b4d07f7827 112 case 0:
Bobty 0:b5b4d07f7827 113 printf("recieved buffer is empty.\n\r");
Bobty 0:b5b4d07f7827 114 clientIsConnected = false;
Bobty 0:b5b4d07f7827 115 break;
Bobty 0:b5b4d07f7827 116 case -1:
Bobty 0:b5b4d07f7827 117 printf("failed to read data from client.\n\r");
Bobty 0:b5b4d07f7827 118 clientIsConnected = false;
Bobty 0:b5b4d07f7827 119 break;
Bobty 0:b5b4d07f7827 120 default:
Bobty 0:b5b4d07f7827 121 printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
Bobty 0:b5b4d07f7827 122 if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' )
Bobty 0:b5b4d07f7827 123 {
Bobty 0:b5b4d07f7827 124 printf("GET request incomming.\n\r");
Bobty 1:75bb184de749 125 char cmdStr[MAX_CMDSTR_LEN];
Bobty 1:75bb184de749 126 char argStr[MAX_ARGSTR_LEN];
Bobty 1:75bb184de749 127 if (extractCmdArgs(buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN))
Bobty 1:75bb184de749 128 {
Bobty 1:75bb184de749 129 printf("CmdStr %s\n\r", cmdStr);
Bobty 1:75bb184de749 130 printf("ArgStr %s\n\r", argStr);
Bobty 1:75bb184de749 131 for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it)
Bobty 1:75bb184de749 132 {
Bobty 1:75bb184de749 133 if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0)
Bobty 1:75bb184de749 134 {
Bobty 1:75bb184de749 135 printf("FoundCmd %s\n\r", cmdStr);
Bobty 1:75bb184de749 136 ((*it)->_callback)(argStr);
Bobty 1:75bb184de749 137 }
Bobty 1:75bb184de749 138 }
Bobty 1:75bb184de749 139 }
Bobty 0:b5b4d07f7827 140
Bobty 0:b5b4d07f7827 141 //setup http response header & data
Bobty 0:b5b4d07f7827 142 char echoHeader[256] = {};
Bobty 0:b5b4d07f7827 143 sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
Bobty 0:b5b4d07f7827 144 client.send(echoHeader,strlen(echoHeader));
Bobty 1:75bb184de749 145 client.send(cmdStr,strlen(cmdStr));
Bobty 0:b5b4d07f7827 146 clientIsConnected = false;
Bobty 1:75bb184de749 147 printf("done.\n\r");
Bobty 0:b5b4d07f7827 148 }
Bobty 0:b5b4d07f7827 149 break;
Bobty 0:b5b4d07f7827 150 }
Bobty 0:b5b4d07f7827 151 }
Bobty 0:b5b4d07f7827 152 printf("close connection.\n\rtcp server is listening...\n\r");
Bobty 0:b5b4d07f7827 153 client.close();
Bobty 0:b5b4d07f7827 154 if (_pStatusLed != NULL)
Bobty 0:b5b4d07f7827 155 *_pStatusLed = false;
Bobty 0:b5b4d07f7827 156 }
Bobty 0:b5b4d07f7827 157 }
Bobty 0:b5b4d07f7827 158 }
Bobty 0:b5b4d07f7827 159
Bobty 1:75bb184de749 160 void RdWebServer::addCommand(char* pCmdStr, CmdCallbackType callback)
Bobty 1:75bb184de749 161 {
Bobty 1:75bb184de749 162 _commands.push_back(new RdWebServerCmdDef(pCmdStr, callback));
Bobty 1:75bb184de749 163 }
Bobty 1:75bb184de749 164