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