This programme Sets a web server using light weigh ip (using Donatien Garnier's server code) which reads a voltage and saves it value in an htm file to be displayed on local page. Server 2 2nd edition Two issues here 1 ) the compiler throws a warning about assignment in RPCHandler.cpp and 2) the local .htm file created on the mbed flash memory is always date stamped with the default date 01/01/2008 11:00

Dependencies:   EthernetNetIf NTPClient_NetServices mbed

Committer:
pmr1
Date:
Sun Aug 08 22:00:39 2010 +0000
Revision:
0:03e1db2fe866

        

Who changed what in which revision?

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