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

Committer:
yueee_yt
Date:
Thu Feb 20 05:36:19 2014 +0000
Revision:
0:fdf9c2c5200f
Child:
4:1b6b021ee21d
Initialize version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:fdf9c2c5200f 1 /*
yueee_yt 0:fdf9c2c5200f 2 Permission is hereby granted, free of charge, to any person obtaining a copy
yueee_yt 0:fdf9c2c5200f 3 of this software and associated documentation files (the "Software"), to deal
yueee_yt 0:fdf9c2c5200f 4 in the Software without restriction, including without limitation the rights
yueee_yt 0:fdf9c2c5200f 5 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yueee_yt 0:fdf9c2c5200f 6 copies of the Software, and to permit persons to whom the Software is
yueee_yt 0:fdf9c2c5200f 7 furnished to do so, subject to the following conditions:
yueee_yt 0:fdf9c2c5200f 8
yueee_yt 0:fdf9c2c5200f 9 The above copyright notice and this permission notice shall be included in
yueee_yt 0:fdf9c2c5200f 10 all copies or substantial portions of the Software.
yueee_yt 0:fdf9c2c5200f 11
yueee_yt 0:fdf9c2c5200f 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yueee_yt 0:fdf9c2c5200f 13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yueee_yt 0:fdf9c2c5200f 14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yueee_yt 0:fdf9c2c5200f 15 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yueee_yt 0:fdf9c2c5200f 16 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yueee_yt 0:fdf9c2c5200f 17 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yueee_yt 0:fdf9c2c5200f 18 THE SOFTWARE.
yueee_yt 0:fdf9c2c5200f 19 */
yueee_yt 0:fdf9c2c5200f 20
yueee_yt 0:fdf9c2c5200f 21 #include "FSHandler.h"
yueee_yt 0:fdf9c2c5200f 22
yueee_yt 0:fdf9c2c5200f 23 //#define __DEBUG
yueee_yt 0:fdf9c2c5200f 24 //#include "dbg/dbg.h"
yueee_yt 0:fdf9c2c5200f 25
yueee_yt 0:fdf9c2c5200f 26 #define CHUNK_SIZE 128
yueee_yt 0:fdf9c2c5200f 27
yueee_yt 0:fdf9c2c5200f 28 #define DEFAULT_PAGE "/index.htm"
yueee_yt 0:fdf9c2c5200f 29
yueee_yt 0:fdf9c2c5200f 30 //*FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
yueee_yt 0:fdf9c2c5200f 31 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection), m_err404(false)
yueee_yt 0:fdf9c2c5200f 32 {}
yueee_yt 0:fdf9c2c5200f 33
yueee_yt 0:fdf9c2c5200f 34 FSHandler::~FSHandler()
yueee_yt 0:fdf9c2c5200f 35 {
yueee_yt 0:fdf9c2c5200f 36 if(m_fp)
yueee_yt 0:fdf9c2c5200f 37 fclose(m_fp);
yueee_yt 0:fdf9c2c5200f 38 printf("\r\nHandler destroyed\r\n");
yueee_yt 0:fdf9c2c5200f 39 }
yueee_yt 0:fdf9c2c5200f 40
yueee_yt 0:fdf9c2c5200f 41 //static init
yueee_yt 0:fdf9c2c5200f 42 map<string,string> FSHandler::m_lFsPath = map<string,string>();
yueee_yt 0:fdf9c2c5200f 43
yueee_yt 0:fdf9c2c5200f 44 void FSHandler::mount(const string& fsPath, const string& rootPath)
yueee_yt 0:fdf9c2c5200f 45 {
yueee_yt 0:fdf9c2c5200f 46 m_lFsPath[rootPath]=fsPath;
yueee_yt 0:fdf9c2c5200f 47 }
yueee_yt 0:fdf9c2c5200f 48
yueee_yt 0:fdf9c2c5200f 49 void FSHandler::doGet()
yueee_yt 0:fdf9c2c5200f 50 {
yueee_yt 0:fdf9c2c5200f 51 printf("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
yueee_yt 0:fdf9c2c5200f 52 //FIXME: Translate path to local/path
yueee_yt 0:fdf9c2c5200f 53 string checkedRootPath = rootPath();
yueee_yt 0:fdf9c2c5200f 54 if(checkedRootPath.empty())
yueee_yt 0:fdf9c2c5200f 55 checkedRootPath="/";
yueee_yt 0:fdf9c2c5200f 56 string filePath = m_lFsPath[checkedRootPath];
yueee_yt 0:fdf9c2c5200f 57 if (path().size() > 1)
yueee_yt 0:fdf9c2c5200f 58 {
yueee_yt 0:fdf9c2c5200f 59 filePath += path();
yueee_yt 0:fdf9c2c5200f 60 }
yueee_yt 0:fdf9c2c5200f 61 else
yueee_yt 0:fdf9c2c5200f 62 {
yueee_yt 0:fdf9c2c5200f 63 filePath += DEFAULT_PAGE;
yueee_yt 0:fdf9c2c5200f 64 }
yueee_yt 0:fdf9c2c5200f 65
yueee_yt 0:fdf9c2c5200f 66 printf("Trying to open %s\n", filePath.c_str());
yueee_yt 0:fdf9c2c5200f 67
yueee_yt 0:fdf9c2c5200f 68 m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
yueee_yt 0:fdf9c2c5200f 69
yueee_yt 0:fdf9c2c5200f 70 if(!m_fp)
yueee_yt 0:fdf9c2c5200f 71 {
yueee_yt 0:fdf9c2c5200f 72 m_err404 = true;
yueee_yt 0:fdf9c2c5200f 73 setErrCode(404);
yueee_yt 0:fdf9c2c5200f 74 const char* msg = "File not found.";
yueee_yt 0:fdf9c2c5200f 75 setContentLen(strlen(msg));
yueee_yt 0:fdf9c2c5200f 76 respHeaders()["Content-Type"] = "text/html";
yueee_yt 0:fdf9c2c5200f 77 respHeaders()["Connection"] = "close";
yueee_yt 0:fdf9c2c5200f 78 writeData(msg,strlen(msg)); //Only send header
yueee_yt 0:fdf9c2c5200f 79 printf("\r\nExit FSHandler::doGet() w Error 404\r\n");
yueee_yt 0:fdf9c2c5200f 80 return;
yueee_yt 0:fdf9c2c5200f 81 }
yueee_yt 0:fdf9c2c5200f 82
yueee_yt 0:fdf9c2c5200f 83 //Seek EOF to get length
yueee_yt 0:fdf9c2c5200f 84 fseek(m_fp, 0, SEEK_END);
yueee_yt 0:fdf9c2c5200f 85 setContentLen( ftell(m_fp) );
yueee_yt 0:fdf9c2c5200f 86 fseek(m_fp, 0, SEEK_SET); //Goto SOF
yueee_yt 0:fdf9c2c5200f 87
yueee_yt 0:fdf9c2c5200f 88 respHeaders()["Connection"] = "close";
yueee_yt 0:fdf9c2c5200f 89 onWriteable();
yueee_yt 0:fdf9c2c5200f 90 printf("\r\nExit SimpleHandler::doGet()\r\n");
yueee_yt 0:fdf9c2c5200f 91 }
yueee_yt 0:fdf9c2c5200f 92
yueee_yt 0:fdf9c2c5200f 93 void FSHandler::doPost()
yueee_yt 0:fdf9c2c5200f 94 {
yueee_yt 0:fdf9c2c5200f 95
yueee_yt 0:fdf9c2c5200f 96 }
yueee_yt 0:fdf9c2c5200f 97
yueee_yt 0:fdf9c2c5200f 98 void FSHandler::doHead()
yueee_yt 0:fdf9c2c5200f 99 {
yueee_yt 0:fdf9c2c5200f 100
yueee_yt 0:fdf9c2c5200f 101 }
yueee_yt 0:fdf9c2c5200f 102
yueee_yt 0:fdf9c2c5200f 103 void FSHandler::onReadable() //Data has been read
yueee_yt 0:fdf9c2c5200f 104 {
yueee_yt 0:fdf9c2c5200f 105
yueee_yt 0:fdf9c2c5200f 106 }
yueee_yt 0:fdf9c2c5200f 107
yueee_yt 0:fdf9c2c5200f 108 void FSHandler::onWriteable() //Data has been written & buf is free
yueee_yt 0:fdf9c2c5200f 109 {
yueee_yt 0:fdf9c2c5200f 110 printf("\r\nFSHandler::onWriteable() event\r\n");
yueee_yt 0:fdf9c2c5200f 111 if(m_err404)
yueee_yt 0:fdf9c2c5200f 112 {
yueee_yt 0:fdf9c2c5200f 113 //Error has been served, now exit
yueee_yt 0:fdf9c2c5200f 114 close();
yueee_yt 0:fdf9c2c5200f 115 return;
yueee_yt 0:fdf9c2c5200f 116 }
yueee_yt 0:fdf9c2c5200f 117
yueee_yt 0:fdf9c2c5200f 118 static char rBuf[CHUNK_SIZE];
yueee_yt 0:fdf9c2c5200f 119 while(true)
yueee_yt 0:fdf9c2c5200f 120 {
yueee_yt 0:fdf9c2c5200f 121 int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
yueee_yt 0:fdf9c2c5200f 122 if(len>0)
yueee_yt 0:fdf9c2c5200f 123 {
yueee_yt 0:fdf9c2c5200f 124 int writtenLen = writeData(rBuf, len);
yueee_yt 0:fdf9c2c5200f 125 if(writtenLen < 0) //Socket error
yueee_yt 0:fdf9c2c5200f 126 {
yueee_yt 0:fdf9c2c5200f 127 printf("FSHandler: Socket error %d\n", writtenLen);
yueee_yt 0:fdf9c2c5200f 128 /**
yueee_yt 0:fdf9c2c5200f 129 if(writtenLen == TCPSOCKET_MEM)
yueee_yt 0:fdf9c2c5200f 130 {
yueee_yt 0:fdf9c2c5200f 131 fseek(m_fp, -len, SEEK_CUR);
yueee_yt 0:fdf9c2c5200f 132 return; //Wait for the queued TCP segments to be transmitted
yueee_yt 0:fdf9c2c5200f 133 }
yueee_yt 0:fdf9c2c5200f 134 else
yueee_yt 0:fdf9c2c5200f 135 {
yueee_yt 0:fdf9c2c5200f 136 //This is a critical error
yueee_yt 0:fdf9c2c5200f 137 close();
yueee_yt 0:fdf9c2c5200f 138 return;
yueee_yt 0:fdf9c2c5200f 139 }
yueee_yt 0:fdf9c2c5200f 140 **/
yueee_yt 0:fdf9c2c5200f 141 }
yueee_yt 0:fdf9c2c5200f 142 else if(writtenLen < len) //Short write, socket's buffer is full
yueee_yt 0:fdf9c2c5200f 143 {
yueee_yt 0:fdf9c2c5200f 144 fseek(m_fp, writtenLen - len, SEEK_CUR);
yueee_yt 0:fdf9c2c5200f 145 return;
yueee_yt 0:fdf9c2c5200f 146 }
yueee_yt 0:fdf9c2c5200f 147 }
yueee_yt 0:fdf9c2c5200f 148 else
yueee_yt 0:fdf9c2c5200f 149 {
yueee_yt 0:fdf9c2c5200f 150 close(); //Data written, we can close the connection
yueee_yt 0:fdf9c2c5200f 151 return;
yueee_yt 0:fdf9c2c5200f 152 }
yueee_yt 0:fdf9c2c5200f 153 }
yueee_yt 0:fdf9c2c5200f 154 }
yueee_yt 0:fdf9c2c5200f 155
yueee_yt 0:fdf9c2c5200f 156 void FSHandler::onClose() //Connection is closing
yueee_yt 0:fdf9c2c5200f 157 {
yueee_yt 0:fdf9c2c5200f 158 if(m_fp)
yueee_yt 0:fdf9c2c5200f 159 fclose(m_fp);
yueee_yt 0:fdf9c2c5200f 160 }
yueee_yt 0:fdf9c2c5200f 161