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