Mac addr added ICRS

Dependencies:   mbed

Fork of Email2Screen by Oliver Mattos

Committer:
je310
Date:
Wed Nov 20 21:22:22 2013 +0000
Revision:
1:b38b745d1ea8
Parent:
0:1619a6b826d7
This is a maced version of olivers code ICRS

Who changed what in which revision?

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