HttpServer Library for "mbed-os" which added a snapshot handler.

Dependents:   GR-PEACH-webcam GR-Boards_WebCamera GR-Boards_WebCamera GR-Boards_WebCamera

Fork of HttpServer_snapshot by Renesas

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FSHandler.cpp Source File

FSHandler.cpp

00001 /*
00002 Permission is hereby granted, free of charge, to any person obtaining a copy
00003 of this software and associated documentation files (the "Software"), to deal
00004 in the Software without restriction, including without limitation the rights
00005 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00006 copies of the Software, and to permit persons to whom the Software is
00007 furnished to do so, subject to the following conditions:
00008 
00009 The above copyright notice and this permission notice shall be included in
00010 all copies or substantial portions of the Software.
00011 
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00013 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00014 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00015 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00016 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00018 THE SOFTWARE.
00019 */
00020 //#define _DEBUG_FS_HANDLER
00021 
00022 #include "FSHandler.h"
00023 
00024 #define CHUNK_SIZE 128
00025 
00026 #define DEFAULT_PAGE "/index.htm"
00027 
00028 Semaphore FSHandler::req_sem(1);
00029 
00030 
00031 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
00032 {}
00033 
00034 FSHandler::~FSHandler()
00035 {
00036 #ifdef _DEBUG_FS_HANDLER
00037     printf("\r\n+++FSHandler destroy\r\n");
00038 #endif
00039     if(m_fp){
00040 #ifdef _DEBUG_FS_HANDLER
00041     printf("\r\n+++FSHandler fclose start\r\n");
00042 #endif
00043         fclose(m_fp);
00044 #ifdef _DEBUG_FS_HANDLER
00045     printf("\r\n+++FSHandler fclose end\r\n");
00046 #endif
00047        
00048         }
00049 #ifdef _DEBUG_FS_HANDLER
00050     printf("\r\nHandler destroyed\r\n");
00051 #endif
00052 }
00053 
00054 //static init
00055 map<string,string> FSHandler::m_lFsPath = map<string,string>();
00056 
00057 void FSHandler::mount(const string& fsPath, const string& rootPath)
00058 {
00059     m_lFsPath[rootPath]=fsPath;
00060 }
00061 
00062 void FSHandler::doGet()
00063 {
00064     req_sem.wait();
00065 #ifdef _DEBUG_FS_HANDLER
00066     printf("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
00067 #endif
00068     //FIXME: Translate path to local/path
00069     string checkedRootPath = rootPath();
00070     if(checkedRootPath.empty())
00071         checkedRootPath="/";
00072     string filePath = m_lFsPath[checkedRootPath];
00073     if (path().size() > 1) {
00074         filePath += path();
00075     } else {
00076         filePath += DEFAULT_PAGE;
00077     }
00078 #ifdef _DEBUG_FS_HANDLER
00079     printf("Trying to open %s\n", filePath.c_str());
00080 #endif
00081     m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
00082 
00083     if(!m_fp) {
00084         m_err404 = true;
00085         setErrCode(404);
00086         const char* msg = "File not found.";
00087         setContentLen(strlen(msg));
00088         respHeaders()["Content-Type"] = "text/html";
00089         respHeaders()["Connection"] = "close";
00090         writeData(msg,strlen(msg)); //Only send header
00091         printf("\r\nExit FSHandler::doGet() w Error 404\r\n");
00092         req_sem.release();
00093         return;
00094     }
00095 
00096     //Seek EOF to get length
00097     fseek(m_fp, 0, SEEK_END);
00098     setContentLen( ftell(m_fp) );
00099     fseek(m_fp, 0, SEEK_SET); //Goto SOF
00100 
00101     respHeaders()["Connection"] = "close";
00102     onWriteable();
00103 #ifdef _DEBUG_FS_HANDLER
00104     printf("\r\nExit SimpleHandler::doGet()\r\n");
00105 #endif
00106     req_sem.release();
00107 }
00108 
00109 void FSHandler::doPost()
00110 {
00111 
00112 }
00113 
00114 void FSHandler::doHead()
00115 {
00116 
00117 }
00118 
00119 void FSHandler::onReadable() //Data has been read
00120 {
00121 
00122 }
00123 
00124 void FSHandler::onWriteable() //Data has been written & buf is free
00125 {
00126 #ifdef _DEBUG_FS_HANDLER
00127     printf("\r\nFSHandler::onWriteable() event\r\n");
00128 #endif
00129     if(m_err404) {
00130         //Error has been served, now exit
00131         close();
00132         return;
00133     }
00134 
00135     static char rBuf[CHUNK_SIZE];
00136     while(true) {
00137         int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
00138         if(len>0) {
00139             int writtenLen = writeData(rBuf, len);
00140             if(writtenLen < 0) { //Socket error
00141 #ifdef _DEBUG_FS_HANDLER
00142                 printf("FSHandler: Socket error %d\n", writtenLen);
00143 #endif
00144                 /**  Not Work
00145                                 if(writtenLen == TCPSOCKET_MEM) {
00146                                     fseek(m_fp, -len, SEEK_CUR);
00147                                     return; //Wait for the queued TCP segments to be transmitted
00148                                 } else {
00149                                     //This is a critical error
00150                                     **/
00151                                     close();
00152                                     return;
00153                                     /**
00154                                 }
00155                 **/
00156             } else if(writtenLen < len) { //Short write, socket's buffer is full
00157                 fseek(m_fp, writtenLen - len, SEEK_CUR);
00158                 return;
00159             }
00160         } else {
00161             close(); //Data written, we can close the connection
00162             return;
00163         }
00164     }
00165 }
00166 
00167 void FSHandler::onClose() //Connection is closing
00168 {
00169     /**
00170 #ifdef _DEBUG_FS_HANDLER
00171         printf("FSHandler: onClose start \r\n");
00172 #endif
00173     if(m_fp){
00174 #ifdef _DEBUG_FS_HANDLER
00175         printf("FSHandler: fclose start \r\n");
00176 #endif
00177     fclose(m_fp);
00178 #ifdef _DEBUG_FS_HANDLER
00179     printf("FSHandler: fclose end \r\n");
00180 #endif
00181 }
00182 #ifdef _DEBUG_FS_HANDLER
00183         printf("FSHandler: onClose end \r\n");
00184 #endif
00185 **/
00186 }
00187