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
Diff: Handler/FSHandler.cpp
- Revision:
- 4:1b6b021ee21d
- Parent:
- 0:fdf9c2c5200f
- Child:
- 5:b8f6a11c70db
diff -r 87c6439f4136 -r 1b6b021ee21d Handler/FSHandler.cpp --- a/Handler/FSHandler.cpp Thu Feb 20 13:11:34 2014 +0000 +++ b/Handler/FSHandler.cpp Fri Feb 21 07:10:30 2014 +0000 @@ -5,10 +5,10 @@ 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 @@ -17,6 +17,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#ifdef _DEBUG_ALL +#define _DEBUG_FS_HANDLER +#endif #include "FSHandler.h" @@ -33,9 +36,11 @@ FSHandler::~FSHandler() { - if(m_fp) - fclose(m_fp); - printf("\r\nHandler destroyed\r\n"); + if(m_fp) + fclose(m_fp); +#ifdef _DEBUG_FS_HANDLER + printf("\r\nHandler destroyed\r\n"); +#endif } //static init @@ -43,51 +48,51 @@ void FSHandler::mount(const string& fsPath, const string& rootPath) { - m_lFsPath[rootPath]=fsPath; + m_lFsPath[rootPath]=fsPath; } void FSHandler::doGet() { - printf("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str()); - //FIXME: Translate path to local/path - string checkedRootPath = rootPath(); - if(checkedRootPath.empty()) - checkedRootPath="/"; - string filePath = m_lFsPath[checkedRootPath]; - if (path().size() > 1) - { - filePath += path(); - } - else - { - filePath += DEFAULT_PAGE; - } - - printf("Trying to open %s\n", filePath.c_str()); +#ifdef _DEBUG_FS_HANDLER + printf("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str()); +#endif + //FIXME: Translate path to local/path + string checkedRootPath = rootPath(); + if(checkedRootPath.empty()) + checkedRootPath="/"; + string filePath = m_lFsPath[checkedRootPath]; + if (path().size() > 1) { + filePath += path(); + } else { + filePath += DEFAULT_PAGE; + } +#ifdef _DEBUG_FS_HANDLER + printf("Trying to open %s\n", filePath.c_str()); +#endif + m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404 - m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404 - - if(!m_fp) - { - m_err404 = true; - setErrCode(404); - const char* msg = "File not found."; - setContentLen(strlen(msg)); - respHeaders()["Content-Type"] = "text/html"; + if(!m_fp) { + m_err404 = true; + setErrCode(404); + const char* msg = "File not found."; + setContentLen(strlen(msg)); + respHeaders()["Content-Type"] = "text/html"; + respHeaders()["Connection"] = "close"; + writeData(msg,strlen(msg)); //Only send header + printf("\r\nExit FSHandler::doGet() w Error 404\r\n"); + return; + } + + //Seek EOF to get length + fseek(m_fp, 0, SEEK_END); + setContentLen( ftell(m_fp) ); + fseek(m_fp, 0, SEEK_SET); //Goto SOF + respHeaders()["Connection"] = "close"; - writeData(msg,strlen(msg)); //Only send header - printf("\r\nExit FSHandler::doGet() w Error 404\r\n"); - return; - } - - //Seek EOF to get length - fseek(m_fp, 0, SEEK_END); - setContentLen( ftell(m_fp) ); - fseek(m_fp, 0, SEEK_SET); //Goto SOF - - respHeaders()["Connection"] = "close"; - onWriteable(); - printf("\r\nExit SimpleHandler::doGet()\r\n"); + onWriteable(); +#ifdef _DEBUG_FS_HANDLER + printf("\r\nExit SimpleHandler::doGet()\r\n"); +#endif } void FSHandler::doPost() @@ -107,55 +112,47 @@ void FSHandler::onWriteable() //Data has been written & buf is free { - printf("\r\nFSHandler::onWriteable() event\r\n"); - if(m_err404) - { - //Error has been served, now exit - close(); - return; - } - - static char rBuf[CHUNK_SIZE]; - while(true) - { - int len = fread(rBuf, 1, CHUNK_SIZE, m_fp); - if(len>0) - { - int writtenLen = writeData(rBuf, len); - if(writtenLen < 0) //Socket error - { - printf("FSHandler: Socket error %d\n", writtenLen); - /** - if(writtenLen == TCPSOCKET_MEM) - { - fseek(m_fp, -len, SEEK_CUR); - return; //Wait for the queued TCP segments to be transmitted +#ifdef _DEBUG_FS_HANDLER + printf("\r\nFSHandler::onWriteable() event\r\n"); +#endif + if(m_err404) { + //Error has been served, now exit + close(); + return; + } + + static char rBuf[CHUNK_SIZE]; + while(true) { + int len = fread(rBuf, 1, CHUNK_SIZE, m_fp); + if(len>0) { + int writtenLen = writeData(rBuf, len); + if(writtenLen < 0) { //Socket error +#ifdef _DEBUG_FS_HANDLER + printf("FSHandler: Socket error %d\n", writtenLen); +#endif +/** Not Work + if(writtenLen == TCPSOCKET_MEM) { + fseek(m_fp, -len, SEEK_CUR); + return; //Wait for the queued TCP segments to be transmitted + } else { + //This is a critical error + close(); + return; + } +**/ + } else if(writtenLen < len) { //Short write, socket's buffer is full + fseek(m_fp, writtenLen - len, SEEK_CUR); + return; + } + } else { + close(); //Data written, we can close the connection + return; } - else - { - //This is a critical error - close(); - return; - } - **/ - } - else if(writtenLen < len) //Short write, socket's buffer is full - { - fseek(m_fp, writtenLen - len, SEEK_CUR); - return; - } } - else - { - close(); //Data written, we can close the connection - return; - } - } } void FSHandler::onClose() //Connection is closing { - if(m_fp) - fclose(m_fp); + if(m_fp) + fclose(m_fp); } -