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