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