Adaptation of the HttpServer by user yueee_yt. This version has improved handling of the HTTP headers (**NOTE**: There are limitations with this implementation and it is not fully functional. Use it only as a starting point.)
Dependents: DMSupport DMSupport DMSupport DMSupport
Fork of DM_HttpServer by
Revision 10:c1c8276af541, committed 2019-10-23
- Comitter:
- embeddedartists
- Date:
- Wed Oct 23 06:58:32 2019 +0000
- Parent:
- 9:10b4d4075fbb
- Child:
- 11:9dcff8cf906a
- Commit message:
- Updates related to mbed OS 5
Changed in this revision
--- a/HTTPRequestHandler.cpp Mon Jan 26 10:10:46 2015 +0100 +++ b/HTTPRequestHandler.cpp Wed Oct 23 06:58:32 2019 +0000 @@ -32,7 +32,7 @@ // m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), // m_rootPath(rootPath), m_path(path), m_errc(200), // m_watchdog(), m_timeout(0),**/ m_closed(false), m_headersSent(false) //OK -HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : +HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) : m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), m_rootPath(rootPath), m_path(path), m_errc(200), m_closed(false), m_headersSent(false) { @@ -96,7 +96,7 @@ int HTTPRequestHandler::readData(char* buf, int len) { - return m_pTCPSocketConnection->receive(buf, len); + return m_pTCPSocketConnection->recv(buf, len); } string& HTTPRequestHandler::rootPath() //const @@ -194,7 +194,7 @@ int ret; int len = 0; for(int i = 0; i < maxLen - 1; i++) { - ret = m_pTCPSocketConnection->receive(str, 1); + ret = m_pTCPSocketConnection->recv(str, 1); if(!ret) { break; } @@ -224,7 +224,7 @@ while (i < maxLen && !found) { if (cache->num <= cache->pos) { // get something to process - int ret = m_pTCPSocketConnection->receive(cache->buff, cache->size); + int ret = m_pTCPSocketConnection->recv(cache->buff, cache->size); if(ret == -1) { // error break;
--- a/HTTPRequestHandler.h Mon Jan 26 10:10:46 2015 +0100 +++ b/HTTPRequestHandler.h Wed Oct 23 06:58:32 2019 +0000 @@ -46,7 +46,7 @@ { public: ///Instantiated by the HTTP Server - HTTPRequestHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection); + HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection); virtual ~HTTPRequestHandler(); //protected: @@ -82,7 +82,7 @@ void writeHeaders(); //Called at the first writeData call //**void onTCPSocketEvent(/**TCPSocketEvent e**/); - TCPSocketConnection* m_pTCPSocketConnection; + TCPSocket* m_pTCPSocketConnection; map<string, string> m_reqHeaders; map<string, string> m_respHeaders; string m_rootPath;
--- a/HTTPServer.h Mon Jan 26 10:10:46 2015 +0100 +++ b/HTTPServer.h Wed Oct 23 06:58:32 2019 +0000 @@ -279,7 +279,7 @@ // Thread::wait(100); } } -#include "Handler/RPCHandler.h" +//#include "Handler/RPCHandler.h" #include "Handler/FSHandler.h" #include "Handler/SimpleHandler.h"
--- a/Handler/FSHandler.cpp Mon Jan 26 10:10:46 2015 +0100 +++ b/Handler/FSHandler.cpp Wed Oct 23 06:58:32 2019 +0000 @@ -27,7 +27,7 @@ #define DEFAULT_PAGE "/index.htm" //*FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false) -FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection), m_err404(false) +FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection), m_err404(false) {} FSHandler::~FSHandler()
--- a/Handler/FSHandler.h Mon Jan 26 10:10:46 2015 +0100 +++ b/Handler/FSHandler.h Wed Oct 23 06:58:32 2019 +0000 @@ -33,13 +33,13 @@ class FSHandler : public HTTPRequestHandler { public: - FSHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection); + FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection); virtual ~FSHandler(); static void mount(const string& fsPath, const string& rootPath); //protected: - static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) { return new FSHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) { return new FSHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one virtual void doGet(); virtual void doPost();
--- a/Handler/RPCHandler.cpp Mon Jan 26 10:10:46 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,135 +0,0 @@ -/* -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -//#define _DEBUG_RPC_HANDLER - -#include "RPCHandler.h" -#include "mbed_rpc.h" -#include "DMBoard.h" - -#define RPC_DATA_LEN 128 - -RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection) -{} - -RPCHandler::~RPCHandler() -{ -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()-> printf("++++(RPC Handler)Handler destroyed\r\n"); -#endif -} - -void RPCHandler::doGet() -{ -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)doGet\r\n"); -#endif - char resp[RPC_DATA_LEN] = {0}; - char req[RPC_DATA_LEN] = {0}; - -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)Path : %s\r\n", path().c_str()); - DMBoard::instance().logger()->printf("++++(RPC Handler)Root Path : %s\r\n", rootPath().c_str()); -#endif - //Remove path - strncpy(req, path().c_str(), RPC_DATA_LEN-1); -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)RPC req(before) : %s\r\n", req); -#endif - //Remove "%20", "+", "," from req - cleanReq(req); -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)RPC req(after) : %s\r\n", req); -#endif - //Do RPC Call - RPC::call(req, resp); //FIXME: Use bool result -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)Response %s \r\n",resp); -#endif - //Response - setContentLen( strlen(resp) ); - - //Make sure that the browser won't cache this request - respHeaders()["Cache-control"]="no-cache;no-store"; - respHeaders()["Pragma"]="no-cache"; - respHeaders()["Expires"]="0"; - - //Write data - respHeaders()["Connection"] = "close"; - writeData(resp, strlen(resp)); -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)Exit RPCHandler::doGet()\r\n"); -#endif -} - -void RPCHandler::doPost() -{ - -} - -void RPCHandler::doHead() -{ - -} - - -void RPCHandler::onReadable() //Data has been read -{ - -} - -void RPCHandler::onWriteable() //Data has been written & buf is free -{ -#ifdef _DEBUG_RPC_HANDLER - DMBoard::instance().logger()->printf("++++(RPC Handler)onWriteable event\r\n"); -#endif - // close(); //Data written, we can close the connection -} - -void RPCHandler::onClose() //Connection is closing -{ - //Nothing to do -} - -void RPCHandler::cleanReq(char* data) -{ - char* p; - if((p = strstr(data, "+"))!=NULL)memset((void*) p, ' ', 1); - else if((p = strstr(data, ","))!=NULL)memset((void*) p, ' ', 1); - else if((p = strstr(data, "%20"))!=NULL) { - memset((void*) p, ' ', 1); - while(*(p+2)!=NULL) { - p++; - memset((void*) p,*(p+2),1); - } - } - - if((p = strstr(data, "+"))!=NULL)memset((void*) p, ' ', 1); - else if((p = strstr(data, ","))!=NULL)memset((void*) p, ' ', 1); - else if((p = strstr(data, "%20"))!=NULL) { - memset((void*) p, ' ', 1); - while(*(p+2)!=NULL) { - p++; - memset((void*) p,*(p+2),1); - } - } -} - -
--- a/Handler/RPCHandler.h Mon Jan 26 10:10:46 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ - -/* -Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - -#ifndef RPC_HANDLER_H -#define RPC_HANDLER_H - -#include "../HTTPRequestHandler.h" - -class RPCHandler : public HTTPRequestHandler -{ -public: - RPCHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection); - virtual ~RPCHandler(); - -//protected: - static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) { return new RPCHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one - - virtual void doGet(); - virtual void doPost(); - virtual void doHead(); - - virtual void onReadable(); //Data has been read - virtual void onWriteable(); //Data has been written & buf is free - virtual void onClose(); //Connection is closing - -protected: - void cleanReq(char* data); -}; - -#endif -
--- a/Handler/SimpleHandler.cpp Mon Jan 26 10:10:46 2015 +0100 +++ b/Handler/SimpleHandler.cpp Wed Oct 23 06:58:32 2019 +0000 @@ -24,7 +24,7 @@ #include "SimpleHandler.h" #include "DMBoard.h" -SimpleHandler::SimpleHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection) +SimpleHandler::SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection) { #ifdef _DEBUG_SIMPLE_HANDLER DMBoard::instance().logger()->printf("++++(SimpleHeader)Initialize\r\n");
--- a/Handler/SimpleHandler.h Mon Jan 26 10:10:46 2015 +0100 +++ b/Handler/SimpleHandler.h Wed Oct 23 06:58:32 2019 +0000 @@ -29,11 +29,11 @@ class SimpleHandler : public HTTPRequestHandler { public: - SimpleHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection); + SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection); virtual ~SimpleHandler(); //protected: - static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) { return new SimpleHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) { return new SimpleHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one virtual void doGet(); virtual void doPost();