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