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