Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FSHandler.cpp
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 #include "FSHandler.h" 00025 00026 //#define __DEBUG 00027 #include "dbg/dbg.h" 00028 00029 #define CHUNK_SIZE 512//128 00030 00031 //LocalFileSystem local("webfs"); 00032 //LocalFileSystem FSHandler_fs("webfs"); 00033 00034 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false) 00035 {} 00036 00037 FSHandler::~FSHandler() 00038 { 00039 if(m_fp) 00040 fclose(m_fp); 00041 DBG("Handler destroyed\r\n"); 00042 } 00043 00044 void FSHandler::doGet() 00045 { 00046 DBG("In FSHandler::doGet()\r\n"); 00047 //FIXME: Translate path to local/path 00048 m_fp = fopen(path().c_str(), "r"); //FIXME: if null, error 404 00049 00050 if(!m_fp) 00051 { 00052 m_err404 = true; 00053 setErrCode(404); 00054 const char* msg = "File not found."; 00055 setContentLen(strlen(msg)); 00056 respHeaders()["Content-Type"] = "text/html"; 00057 respHeaders()["Connection"] = "close"; 00058 writeData(msg,strlen(msg)); //Only send header 00059 DBG("Exit SimpleHandler::doGet() w Error 404\r\n"); 00060 return; 00061 } 00062 00063 //Seek EOF to get length 00064 fseek(m_fp, 0, SEEK_END); 00065 setContentLen( ftell(m_fp) ); 00066 fseek(m_fp, 0, SEEK_SET); //Goto SOF 00067 00068 respHeaders()["Connection"] = "close"; 00069 onWriteable(); 00070 DBG("Exit SimpleHandler::doGet()\r\n"); 00071 } 00072 00073 void FSHandler::doPost() 00074 { 00075 00076 } 00077 00078 void FSHandler::doHead() 00079 { 00080 00081 } 00082 00083 void FSHandler::onReadable() //Data has been read 00084 { 00085 00086 } 00087 00088 void FSHandler::onWriteable() //Data has been written & buf is free 00089 { 00090 DBG("FSHandler::onWriteable() event\r\n"); 00091 if(m_err404) 00092 { 00093 //Error has been served, now exit 00094 close(); 00095 return; 00096 } 00097 00098 static char rBuf[CHUNK_SIZE]; 00099 int len = fread(rBuf, 1, CHUNK_SIZE, m_fp); 00100 if(len>0) 00101 { 00102 writeData(rBuf, len); 00103 } 00104 else 00105 { 00106 close(); //Data written, we can close the connection 00107 } 00108 } 00109 00110 void FSHandler::onClose() //Connection is closing 00111 { 00112 if(m_fp) 00113 fclose(m_fp); 00114 }
Generated on Tue Jul 12 2022 21:10:25 by
1.7.2