ソースの整理中ですが、利用はできます。 大きなファイルはできないかもしれません。

Dependencies:   EthernetInterface HttpServer TextLCD expatlib mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Fork of giken9_HTMLServer_Sample by Yasushi TAUCHI

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 //*FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
00029 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection), m_err404(false)
00030 {}
00031 
00032 FSHandler::~FSHandler()
00033 {
00034 #ifdef _DEBUG_FS_HANDLER
00035     printf("++++(FSHandler) destroy\r\n");
00036 #endif
00037     if(m_fp) {
00038 #ifdef _DEBUG_FS_HANDLER
00039         printf("++++(FSHandler) fclose start\r\n");
00040 #endif
00041         fclose(m_fp);
00042 #ifdef _DEBUG_FS_HANDLER
00043         printf("+++(FSHandler) fclose end\r\n");
00044 #endif
00045 
00046     }
00047 #ifdef _DEBUG_FS_HANDLER
00048     printf("++++(FSHandler)Handler destroyed\r\n");
00049 #endif
00050 }
00051 
00052 //static init
00053 map<string,string> FSHandler::m_lFsPath = map<string,string>();
00054 
00055 void FSHandler::mount(const string& fsPath, const string& rootPath)
00056 {
00057     m_lFsPath[rootPath]=fsPath;
00058 }
00059 
00060 
00061 void FSHandler::doGet()
00062 {
00063 #ifdef _DEBUG_FS_HANDLER
00064     printf("++++(FSHandler)doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
00065 #endif
00066     //FIXME: Translate path to local/path
00067     string checkedRootPath = rootPath();
00068     if(checkedRootPath.empty())
00069         checkedRootPath="/";
00070     string filePath = m_lFsPath[checkedRootPath];
00071     if (path().size() > 1) {
00072         filePath += path();
00073     } else {
00074         filePath += DEFAULT_PAGE;
00075     }
00076 #ifdef _DEBUG_FS_HANDLER
00077     printf("++++(FSHandler)Trying to open %s\r\n", filePath.c_str());
00078 #endif
00079     m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
00080 #ifdef _DEBUG_FS_HANDLER
00081     printf("++++(FSHandler) opened %s\r\n", filePath.c_str());
00082 #endif
00083 
00084     if(!m_fp) {
00085 #ifdef _DEBUG_FS_HANDLER
00086         printf("++++(FSHandler)404Error\r\n");
00087 #endif
00088 
00089         m_err404 = true;
00090         setErrCode(404);
00091         const char* msg = "File not found.";
00092         setContentLen(strlen(msg));
00093 //        respHeaders()["Content-Type"] = "text/html";
00094 //        respHeaders()["Connection"] = "close";
00095         addRespHeaders("Content-Type", "text/html");
00096         addRespHeaders("Connection", "close");
00097         writeData(msg,strlen(msg)); //Only send header
00098         printf("++++(FSHandler)\r\nExit FSHandler::doGet() w Error 404\r\n");
00099         return;
00100     }
00101   //  printf("++++(FSHandler) Seek start\r\n");
00102     //Seek EOF to get length
00103     fseek(m_fp, 0, SEEK_END);
00104     setContentLen( ftell(m_fp) );
00105     fseek(m_fp, 0, SEEK_SET); //Goto SOF
00106 
00107 //    respHeaders()["Connection"] = "close";
00108     addRespHeaders("Connection", "close");
00109  //   printf("++++(FSHandler)Write start\r\n");
00110     onWriteable();
00111 #ifdef _DEBUG_FS_HANDLER
00112     printf("++++(FSHandler)Exit doGet()\r\n");
00113 #endif
00114 }
00115 
00116 void FSHandler::doPost()
00117 {
00118 
00119 }
00120 
00121 void FSHandler::doHead()
00122 {
00123 
00124 }
00125 
00126 void FSHandler::onReadable() //Data has been read
00127 {
00128 
00129 }
00130 
00131 void FSHandler::onWriteable() //Data has been written & buf is free
00132 {
00133 #ifdef _DEBUG_FS_HANDLER
00134     printf("++++(FSHandler)onWriteable() event\r\n");
00135 #endif
00136     if(m_err404) {
00137 #ifdef _DEBUG_FS_HANDLER
00138         printf("++++(FSHandler)m_err404\r\n");
00139 #endif
00140         //Error has been served, now exit
00141         close();
00142         return;
00143     }
00144 #ifdef _DEBUG_FS_HANDLER
00145     printf("++++(FSHandler)not m_err404\r\n");
00146 #endif
00147 
00148     static char rBuf[CHUNK_SIZE];
00149     while(true) {
00150         int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
00151         if(len>0) {
00152             int writtenLen = writeData(rBuf, len);
00153             if(writtenLen < 0) { //Socket error
00154 #ifdef _DEBUG_FS_HANDLER
00155                 printf("++++(FSHandler) Socket error %d\r\n", writtenLen);
00156 #endif
00157                 /**  Not Work
00158                                 if(writtenLen == TCPSOCKET_MEM) {
00159                                     #ifdef _DEBUG_FS_HANDLER
00160                 printf("FSHandler: Socket error %d\n", writtenLen);
00161                 #endif
00162                                     fseek(m_fp, -len, SEEK_CUR);
00163                                     return; //Wait for the queued TCP segments to be transmitted
00164                                 } else {
00165                                     //This is a critical error
00166                                     **/
00167 #ifdef _DEBUG_FS_HANDLER
00168                 printf("++++(FSHandler)critical error\r\n");
00169 #endif
00170                 close();
00171                 return;
00172                 /**
00173                 }
00174                 **/
00175             } else if(writtenLen < len) { //Short write, socket's buffer is full
00176 #ifdef _DEBUG_FS_HANDLER
00177                 printf("++++(FSHandler)Short write, socket's buffer is full\r\n");
00178 #endif
00179                 fseek(m_fp, writtenLen - len, SEEK_CUR);
00180                 return;
00181             }
00182         } else {
00183             close(); //Data written, we can close the connection
00184             return;
00185         }
00186     }
00187 }
00188 
00189 void FSHandler::onClose() //Connection is closing
00190 {
00191     /**
00192     #ifdef _DEBUG_FS_HANDLER
00193         printf("FSHandler: onClose start \r\n");
00194     #endif
00195     if(m_fp){
00196     #ifdef _DEBUG_FS_HANDLER
00197         printf("FSHandler: fclose start \r\n");
00198     #endif
00199     fclose(m_fp);
00200     #ifdef _DEBUG_FS_HANDLER
00201     printf("FSHandler: fclose end \r\n");
00202     #endif
00203     }
00204     #ifdef _DEBUG_FS_HANDLER
00205         printf("FSHandler: onClose end \r\n");
00206     #endif
00207     **/
00208 }
00209 
00210