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