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.
RdWebServer.cpp
- Committer:
- Bobty
- Date:
- 2015-08-29
- Revision:
- 22:598a21373539
- Parent:
- 21:2dfb56648b93
- Child:
- 23:0fc3d7b5e596
File content as of revision 22:598a21373539:
// RdWebServer - Simple Web Server for MBED // Copyright (C) Rob Dobson 2013-2015, MIT License // Inspired by Jasper Schuurmans multi-threaded web server for Netduino which now seems to have gone from ... // http://www.schuurmans.cc/multi-threaded-web-server-for-netduino-plus // More details at http://robdobson.com/2013/10/moving-my-window-shades-control-to-mbed/ // Setting RDWEB_DEBUG to 4 causes all debugging to be shown #define RDWEB_DEBUG 2 // Change the settings below to support a local file system (not available on some MBEDs) #define SUPPORT_LOCAL_FILESYSTEM 1 #define SUPPORT_LOCAL_FILE_CACHE 1 // Change this to support display of files on the server //#define SUPPORT_FOLDER_VIEW 1 #include "RdWebServer.h" // Limits - note particularly the MAX_FILENAME_LEN const int MAX_CMDSTR_LEN = 100; const int MAX_ARGSTR_LEN = 100; const int MAX_FILENAME_LEN = (MAX_ARGSTR_LEN + 20); // Constructor RdWebServer::RdWebServer() { _initOk = false; _pStatusLed = NULL; } // Destructor RdWebServer::~RdWebServer() { // Clean-up - probably never called as we're on a microcontroller! for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it) delete (*it); } // Init bool RdWebServer::init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder) { // Settings _port = port; _pStatusLed = pStatusLed; _pBaseWebFolder = pBaseWebFolder; // The sample web server on MBED site turns off blocking after the accept has happened // I've tried this but it doesn't seem to yield a reliable server _blockingOnAccept = true; _blockingOnReceive = true; // This is the same as the default in the socket.cpp file _timeoutOnBlocking = 1500; // Currently tested using close connection after send with a single file which works fine // If closeConnAfterSend is set false then the socket connection remains open and only // one client can access the server at a time // Need to test with the closeConnAfterSend seetting true when trying to download multiple files // for a website as previous experience would indicate that requests might be missed in this scenario // although all other settings may not have been the same _closeConnAfterSend = true; _closeConnOnReceiveFail = true; // Setup tcp socket _serverSocket.set_blocking(true); if(_serverSocket.bind(port)< 0) { RD_WARN("TCP server bind fail"); return false; } if(_serverSocket.listen(1) < 0) { RD_WARN("TCP server listen fail"); return false; } RD_INFO("TCP server is listening..."); _initOk = true; return true; } // Run - never returns so run in a thread void RdWebServer::run() { // Check initialised ok if (!_initOk) return; // Start accepting connections while (true) { TCPSocketConnection clientSocketConn; // Accept connection if available RD_INFO("Waiting for TCP connection"); clientSocketConn.set_blocking(_blockingOnAccept, _timeoutOnBlocking); if(_serverSocket.accept(clientSocketConn)<0) { RD_WARN("TCP Socket failed to accept connection"); continue; } // Connection RD_INFO("Connection from IP: %s", clientSocketConn.get_address()); if (_pStatusLed != NULL) *_pStatusLed = true; // While connected bool forcedClosed = false; while(clientSocketConn.is_connected() && !forcedClosed) { // Receive data clientSocketConn.set_blocking(_blockingOnReceive, _timeoutOnBlocking); int rxLen = clientSocketConn.receive(_buffer, HTTPD_MAX_REQ_LENGTH); if (rxLen == -1) { RD_DBG("clientSocketConn.receive() returned %d", rxLen); if (_closeConnOnReceiveFail) { int closeRet = clientSocketConn.close(); RD_DBG("Failed receive connection close() ret %d is connected %d", closeRet, clientSocketConn.is_connected()); forcedClosed = true; } continue; } if (rxLen == 0) { RD_DBG("clientSocketConn.receive() returned %d - ignoring - is connected %d", rxLen, clientSocketConn.is_connected()); continue; } if (rxLen > HTTPD_MAX_REQ_LENGTH) { RD_DBG("clientSocketConn.receive() returned %d - too long", rxLen); formHTTPHeader("413 Request Entity Too Large", "text/plain", 0); int sentRet = clientSocketConn.send(_httpHeader,strlen(_httpHeader)); continue; } // Handle received message _buffer[rxLen] = '\0'; if (handleReceivedHttp(clientSocketConn)) { // OK } else { // FAIL } // Close connection if required if (_closeConnAfterSend) { int closeRet = clientSocketConn.close(); RD_DBG("After send connection close() ret %d is connected %d", closeRet, clientSocketConn.is_connected()); forcedClosed = true; } } } } // Handle a request bool RdWebServer::handleReceivedHttp(TCPSocketConnection &clientSocketConn) { bool handledOk = false; RD_DBG("Received Data: %d\n\r\n\r%.*s",strlen(_buffer),strlen(_buffer),_buffer); int method = METHOD_OTHER; if (strncmp(_buffer, "GET ", 4) == 0) method = METHOD_GET; if (strncmp(_buffer, "POST", 4) == 0) method = METHOD_POST; char cmdStr[MAX_CMDSTR_LEN]; char argStr[MAX_ARGSTR_LEN]; if (extractCmdArgs(_buffer+3, cmdStr, MAX_CMDSTR_LEN, argStr, MAX_ARGSTR_LEN)) { RD_DBG("CmdStr %s", cmdStr); RD_DBG("ArgStr %s", argStr); bool cmdFound = false; for (std::vector<RdWebServerCmdDef*>::iterator it = _commands.begin() ; it != _commands.end(); ++it) { RD_DBG("Testing <<%s>> with <<%s>>", (*it)->_pCmdStr, cmdStr); if (strcasecmp((*it)->_pCmdStr, cmdStr) == 0) { RD_DBG("FoundCmd <%s> Type %d", cmdStr, (*it)->_cmdType); cmdFound = true; if ((*it)->_cmdType == RdWebServerCmdDef::CMD_CALLBACK) { char* respStr = ((*it)->_callback)(method, cmdStr, argStr, _buffer); clientSocketConn.send(respStr, strlen(respStr)); } else if ( ((*it)->_cmdType == RdWebServerCmdDef::CMD_LOCALFILE) || ((*it)->_cmdType == RdWebServerCmdDef::CMD_SDORUSBFILE) ) { char combinedFileName[MAX_FILENAME_LEN]; strcpy(combinedFileName, (*it)->_substFileName); if (strlen(combinedFileName) == 0) strcpy(combinedFileName, cmdStr); else if (combinedFileName[strlen(combinedFileName)-1] == '*') strcpy(combinedFileName+strlen(combinedFileName)-1, argStr); if ((*it)->_cmdType == RdWebServerCmdDef::CMD_LOCALFILE) handledOk = handleLocalFileRequest(combinedFileName, argStr, clientSocketConn, (*it)->_bCacheIfPossible); else handledOk = handleSDFileRequest(combinedFileName, argStr, clientSocketConn); } break; } } // If command not found see if it is a local file if (!cmdFound) { char combinedFileName[MAX_FILENAME_LEN]; strcpy(combinedFileName, cmdStr); strcat(combinedFileName, "/"); strcat(combinedFileName, argStr); handledOk = handleSDFileRequest(cmdStr, argStr, clientSocketConn); } } return handledOk; } // Add a command to the server void RdWebServer::addCommand(char* pCmdStr, int cmdType, CmdCallbackType callback, char* substFileName, bool cacheIfPossible) { _commands.push_back(new RdWebServerCmdDef(pCmdStr, cmdType, callback, substFileName, cacheIfPossible)); } // Form a header to respond void RdWebServer::formHTTPHeader(const char* rsltCode, const char* contentType, int contentLen) { const char* closeConnStr = "\r\nConnection: Close"; if(contentLen != -1) sprintf(_httpHeader, "HTTP/1.1 %s\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: %s\r\nContent-Length: %d%s\r\n\r\n", rsltCode, contentType, contentLen, _closeConnAfterSend ? closeConnStr : ""); else sprintf(_httpHeader, "HTTP/1.1 %s\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: %s%s\r\n\r\n", rsltCode, contentType, _closeConnAfterSend ? closeConnStr : ""); } // Use file extension to determine MIME type char* getMimeTypeStr(char* filename) { char* mimeType = "text/plain"; char *pDot = strrchr(filename, '.'); if (pDot && (!strcmp(pDot, ".html") || !strcmp(pDot, ".htm"))) mimeType = "text/html"; else if (pDot && !strcmp(pDot, ".css")) mimeType = "text/css"; else if (pDot && !strcmp(pDot, ".js")) mimeType = "application/javascript"; else if (pDot && !strcmp(pDot, ".png")) mimeType = "image/png"; else if (pDot && (!strcmp(pDot, ".jpeg") || !strcmp(pDot, ".jpeg"))) mimeType = "image/jpeg"; return mimeType; } // Handle request for files/listing from SDCard bool RdWebServer::handleSDFileRequest(char* inFileName, char* argStr, TCPSocketConnection &clientSocketConn) { bool handledOk = false; const int HTTPD_MAX_FNAME_LENGTH = 127; char filename[HTTPD_MAX_FNAME_LENGTH+1]; RD_INFO("Requesting file %s", inFileName); #ifdef SUPPORT_FOLDER_VIEW if ((strlen(inFileName) > 0) && (inFileName[strlen(inFileName)-1] == '/')) { RD_INFO("Request directory %s%s", _pBaseWebFolder, inFileName); sprintf(filename, "%s%s", _pBaseWebFolder, inFileName); DIR *d = opendir(filename); if (d != NULL) { formHTTPHeader("200 OK", "text/html", -1); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); sprintf(_httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", inFileName); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); struct dirent *p; while((p = readdir(d)) != NULL) { RD_INFO("%s", p->d_name); sprintf(_httpHeader,"<li>%s</li>", p->d_name); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); } } closedir(d); RD_DBG("Directory closed\n"); sprintf(_httpHeader,"</ul></body></html>"); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); handledOk = true; } else #endif { sprintf(filename, "%s%s", _pBaseWebFolder, inFileName); RD_INFO("Filename %s", filename); FILE* fp = fopen(filename, "r"); if (fp == NULL) { RD_WARN("Filename %s not found", filename); formHTTPHeader("404 Not Found", "text/plain", 0); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); handledOk = true; } else { RD_INFO("Sending file %s", filename); // Find file length fseek(fp, 0L, SEEK_END); int sz = ftell(fp); fseek(fp, 0L, SEEK_SET); // Get mime type char* mimeType = getMimeTypeStr(filename); RD_DBG("MIME TYPE %s", mimeType); // Form header formHTTPHeader("200 OK", mimeType, sz); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); // Read file in blocks and send int rdCnt = 0; char fileBuf[1024]; while ((rdCnt = fread(fileBuf, sizeof( char ), 1024, fp)) == 1024) { clientSocketConn.send(fileBuf, rdCnt); } clientSocketConn.send(fileBuf, rdCnt); fclose(fp); handledOk = true; } } return handledOk; } // Send a file from cache - used for local files only #ifdef SUPPORT_LOCAL_FILE_CACHE void RdWebServer::sendFromCache(RdFileCacheEntry* pCacheEntry, TCPSocketConnection &clientSocketConn) { RD_INFO("Sending file %s from cache %d bytes", pCacheEntry->_fileName, pCacheEntry->_nFileLen); // Get mime type char* mimeType = getMimeTypeStr(pCacheEntry->_fileName); RD_INFO("MIME TYPE %s", mimeType); // Form header formHTTPHeader("200 OK", mimeType, pCacheEntry->_nFileLen); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); char* pMem = pCacheEntry->_pFileContent; int nLenLeft = pCacheEntry->_nFileLen; int blkSize = 2048; while(nLenLeft > 0) { if (blkSize > nLenLeft) blkSize = nLenLeft; clientSocketConn.send(pMem, blkSize); pMem += blkSize; nLenLeft -= blkSize; } } #endif // Handle a request for a local file bool RdWebServer::handleLocalFileRequest(char* inFileName, char* argStr, TCPSocketConnection &clientSocketConn, bool bCacheIfPossible) { #ifdef SUPPORT_LOCAL_FILESYSTEM const int HTTPD_MAX_FNAME_LENGTH = 127; char localFilename[HTTPD_MAX_FNAME_LENGTH+1]; char reqFileNameStr[HTTPD_MAX_FNAME_LENGTH+1]; RD_INFO("Requesting local file %s", inFileName); sprintf(reqFileNameStr, "/%s", inFileName); sprintf(localFilename, "/local/%s", inFileName); if (bCacheIfPossible) { // Check if file already cached bool bTryToCache = true; for (std::vector<RdFileCacheEntry*>::iterator it = _cachedFiles.begin() ; it != _cachedFiles.end(); ++it) { if (strcmp(inFileName, (*it)->_fileName) == 0) { if ((*it)->_bCacheValid) { sendFromCache(*it, clientSocketConn); return true; } bTryToCache = false; // don't try to cache as cacheing must have already failed } } // See if we can cache the file if (bTryToCache) { RdFileCacheEntry* pCacheEntry = new RdFileCacheEntry(inFileName); if (pCacheEntry) { bool bCacheSuccess = pCacheEntry->readLocalFileIntoCache(localFilename); // Store the cache entry even if reading failed (mem alloc or file not found, etc) so we don't try to cache again _cachedFiles.push_back(pCacheEntry); if (bCacheSuccess) { sendFromCache(pCacheEntry, clientSocketConn); return true; } } } } LocalFileSystem local("local"); FILE* fp = fopen(localFilename, "r"); if (fp == NULL) { RD_WARN("Local file %s not found", localFilename); formHTTPHeader("404 Not Found", "text/plain", 0); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); return true; } else { RD_INFO("Sending file %s from disk", localFilename); // Find file length fseek(fp, 0L, SEEK_END); int sz = ftell(fp); fseek(fp, 0L, SEEK_SET); // Get mime type char* mimeType = getMimeTypeStr(localFilename); RD_DBG("MIME TYPE %s", mimeType); // Form header formHTTPHeader("200 OK", mimeType, sz); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); int rdCnt = 0; while ((rdCnt = fread(_httpHeader, sizeof( char ), sizeof(_httpHeader), fp)) == sizeof(_httpHeader)) { clientSocketConn.send(_httpHeader, rdCnt); } if (rdCnt != 0) clientSocketConn.send(_httpHeader, rdCnt); fclose(fp); return true; } #else RD_WARN("Local file system not supported"); formHTTPHeader("404 Not Found", "text/plain", 0); clientSocketConn.send(_httpHeader,strlen(_httpHeader)); return true; #endif } // Read file into cache #ifdef SUPPORT_LOCAL_FILE_CACHE bool RdFileCacheEntry::readLocalFileIntoCache(char* fileName) { #ifdef SUPPORT_LOCAL_FILESYSTEM RD_INFO("Reading into cache %s", fileName); LocalFileSystem local("local"); FILE* fp = fopen(fileName, "r"); if (fp == NULL) { RD_WARN("Failed to open file"); return false; } RD_DBG("Seeking "); fseek(fp, 0, SEEK_END); _nFileLen = (int)ftell(fp); RD_DBG("Len %d ", _nFileLen); _pFileContent = new char[_nFileLen]; RD_DBG("Buf %08x ", _pFileContent); if (!_pFileContent) { RD_WARN("Failed to allocate %lu", _nFileLen); fclose(fp); return false; } RD_DBG("Allocated"); memset(_pFileContent, 0, _nFileLen); fseek(fp, 0, SEEK_SET); char* pMem = _pFileContent; char fileBuf[100]; int totCnt = 0; int rdCnt = 0; RD_DBG("Reading"); while (true) { int toRead = _nFileLen - totCnt; if (toRead > sizeof(fileBuf)) toRead = sizeof(fileBuf); if (toRead <= 0) break; rdCnt = fread(fileBuf, sizeof(char), toRead, fp); if (rdCnt <= 0) break; RD_DBG("Read %d tot %d of %d", rdCnt, totCnt, _nFileLen); memcpy(pMem, fileBuf, rdCnt); pMem += rdCnt; totCnt += rdCnt; } RD_DBG("Done read"); fclose(fp); RD_DBG("Success in caching %d bytes (read %d)", _nFileLen, totCnt); _bCacheValid = true; return true; #else return false; #endif } #endif // Extract arguments from command bool RdWebServer::extractCmdArgs(char* buf, char* pCmdStr, int maxCmdStrLen, char* pArgStr, int maxArgStrLen) { *pCmdStr = '\0'; *pArgStr = '\0'; int cmdStrLen = 0; int argStrLen = 0; if (buf == NULL) return false; // Check for first slash char* pSlash1 = strchr(buf, '/'); if (pSlash1 == NULL) return false; pSlash1++; // Extract command while(*pSlash1) { if (cmdStrLen >= maxCmdStrLen-1) break; if ((*pSlash1 == '/') || (*pSlash1 == ' ') || (*pSlash1 == '\n') || (*pSlash1 == '?') || (*pSlash1 == '&')) break; *pCmdStr++ = *pSlash1++; *pCmdStr = '\0'; cmdStrLen++; } if ((*pSlash1 == '\0') || (*pSlash1 == ' ') || (*pSlash1 == '\n')) return true; // Now args *pSlash1++; while(*pSlash1) { if (argStrLen >= maxArgStrLen-1) break; if ((*pSlash1 == ' ') || (*pSlash1 == '\n')) break; *pArgStr++ = *pSlash1++; *pArgStr = '\0'; argStrLen++; } return true; } unsigned char* RdWebServer::getPayloadDataFromMsg(char* msgBuf) { char* ptr = strstr(msgBuf, "\r\n\r\n"); if (ptr) return (unsigned char*) (ptr+4); return (unsigned char*) "\0"; } int RdWebServer::getPayloadLengthFromMsg(char* msgBuf) { char* ptr = strstr(msgBuf, "Content-Length:"); if (ptr) { ptr += 15; int contentLen = atoi(ptr); if (contentLen >= 0) return contentLen; } return 0; }