HttpServer Library for "mbed-os" which added a snapshot handler.
Dependents: GR-PEACH-webcam GR-Boards_WebCamera GR-Boards_WebCamera GR-Boards_WebCamera
Fork of HttpServer_snapshot by
Handler/FSHandler.cpp@18:673d663a1ed7, 2019-09-13 (annotated)
- Committer:
- dkato
- Date:
- Fri Sep 13 02:08:33 2019 +0000
- Revision:
- 18:673d663a1ed7
- Parent:
- 11:0700755d64ae
Add macro HTTP_SERVER_THREAD_MAX (default 1)
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 | 5:b8f6a11c70db | 20 | //#define _DEBUG_FS_HANDLER |
yueee_yt | 0:fdf9c2c5200f | 21 | |
yueee_yt | 0:fdf9c2c5200f | 22 | #include "FSHandler.h" |
yueee_yt | 0:fdf9c2c5200f | 23 | |
yueee_yt | 0:fdf9c2c5200f | 24 | #define CHUNK_SIZE 128 |
yueee_yt | 0:fdf9c2c5200f | 25 | |
yueee_yt | 0:fdf9c2c5200f | 26 | #define DEFAULT_PAGE "/index.htm" |
yueee_yt | 0:fdf9c2c5200f | 27 | |
1050186 | 8:a9fb8fe9b8a3 | 28 | Semaphore FSHandler::req_sem(1); |
1050186 | 8:a9fb8fe9b8a3 | 29 | |
1050186 | 8:a9fb8fe9b8a3 | 30 | |
dkato | 11:0700755d64ae | 31 | FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false) |
yueee_yt | 0:fdf9c2c5200f | 32 | {} |
yueee_yt | 0:fdf9c2c5200f | 33 | |
yueee_yt | 0:fdf9c2c5200f | 34 | FSHandler::~FSHandler() |
yueee_yt | 0:fdf9c2c5200f | 35 | { |
yueee_yt | 5:b8f6a11c70db | 36 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 37 | printf("\r\n+++FSHandler destroy\r\n"); |
yueee_yt | 5:b8f6a11c70db | 38 | #endif |
yueee_yt | 5:b8f6a11c70db | 39 | if(m_fp){ |
yueee_yt | 5:b8f6a11c70db | 40 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 41 | printf("\r\n+++FSHandler fclose start\r\n"); |
yueee_yt | 5:b8f6a11c70db | 42 | #endif |
yueee_yt | 4:1b6b021ee21d | 43 | fclose(m_fp); |
yueee_yt | 4:1b6b021ee21d | 44 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 45 | printf("\r\n+++FSHandler fclose end\r\n"); |
yueee_yt | 5:b8f6a11c70db | 46 | #endif |
yueee_yt | 5:b8f6a11c70db | 47 | |
yueee_yt | 5:b8f6a11c70db | 48 | } |
yueee_yt | 5:b8f6a11c70db | 49 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 4:1b6b021ee21d | 50 | printf("\r\nHandler destroyed\r\n"); |
yueee_yt | 4:1b6b021ee21d | 51 | #endif |
yueee_yt | 0:fdf9c2c5200f | 52 | } |
yueee_yt | 0:fdf9c2c5200f | 53 | |
yueee_yt | 0:fdf9c2c5200f | 54 | //static init |
yueee_yt | 0:fdf9c2c5200f | 55 | map<string,string> FSHandler::m_lFsPath = map<string,string>(); |
yueee_yt | 0:fdf9c2c5200f | 56 | |
yueee_yt | 0:fdf9c2c5200f | 57 | void FSHandler::mount(const string& fsPath, const string& rootPath) |
yueee_yt | 0:fdf9c2c5200f | 58 | { |
yueee_yt | 4:1b6b021ee21d | 59 | m_lFsPath[rootPath]=fsPath; |
yueee_yt | 0:fdf9c2c5200f | 60 | } |
yueee_yt | 0:fdf9c2c5200f | 61 | |
yueee_yt | 0:fdf9c2c5200f | 62 | void FSHandler::doGet() |
yueee_yt | 0:fdf9c2c5200f | 63 | { |
1050186 | 8:a9fb8fe9b8a3 | 64 | req_sem.wait(); |
yueee_yt | 4:1b6b021ee21d | 65 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 4:1b6b021ee21d | 66 | printf("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str()); |
yueee_yt | 4:1b6b021ee21d | 67 | #endif |
yueee_yt | 4:1b6b021ee21d | 68 | //FIXME: Translate path to local/path |
yueee_yt | 4:1b6b021ee21d | 69 | string checkedRootPath = rootPath(); |
yueee_yt | 4:1b6b021ee21d | 70 | if(checkedRootPath.empty()) |
yueee_yt | 4:1b6b021ee21d | 71 | checkedRootPath="/"; |
yueee_yt | 4:1b6b021ee21d | 72 | string filePath = m_lFsPath[checkedRootPath]; |
yueee_yt | 4:1b6b021ee21d | 73 | if (path().size() > 1) { |
yueee_yt | 4:1b6b021ee21d | 74 | filePath += path(); |
yueee_yt | 4:1b6b021ee21d | 75 | } else { |
yueee_yt | 4:1b6b021ee21d | 76 | filePath += DEFAULT_PAGE; |
yueee_yt | 4:1b6b021ee21d | 77 | } |
yueee_yt | 4:1b6b021ee21d | 78 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 4:1b6b021ee21d | 79 | printf("Trying to open %s\n", filePath.c_str()); |
yueee_yt | 4:1b6b021ee21d | 80 | #endif |
yueee_yt | 4:1b6b021ee21d | 81 | m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404 |
yueee_yt | 0:fdf9c2c5200f | 82 | |
yueee_yt | 4:1b6b021ee21d | 83 | if(!m_fp) { |
yueee_yt | 4:1b6b021ee21d | 84 | m_err404 = true; |
yueee_yt | 4:1b6b021ee21d | 85 | setErrCode(404); |
yueee_yt | 4:1b6b021ee21d | 86 | const char* msg = "File not found."; |
yueee_yt | 4:1b6b021ee21d | 87 | setContentLen(strlen(msg)); |
yueee_yt | 4:1b6b021ee21d | 88 | respHeaders()["Content-Type"] = "text/html"; |
yueee_yt | 4:1b6b021ee21d | 89 | respHeaders()["Connection"] = "close"; |
yueee_yt | 4:1b6b021ee21d | 90 | writeData(msg,strlen(msg)); //Only send header |
yueee_yt | 4:1b6b021ee21d | 91 | printf("\r\nExit FSHandler::doGet() w Error 404\r\n"); |
1050186 | 8:a9fb8fe9b8a3 | 92 | req_sem.release(); |
yueee_yt | 4:1b6b021ee21d | 93 | return; |
yueee_yt | 4:1b6b021ee21d | 94 | } |
yueee_yt | 4:1b6b021ee21d | 95 | |
yueee_yt | 4:1b6b021ee21d | 96 | //Seek EOF to get length |
yueee_yt | 4:1b6b021ee21d | 97 | fseek(m_fp, 0, SEEK_END); |
yueee_yt | 4:1b6b021ee21d | 98 | setContentLen( ftell(m_fp) ); |
yueee_yt | 4:1b6b021ee21d | 99 | fseek(m_fp, 0, SEEK_SET); //Goto SOF |
yueee_yt | 4:1b6b021ee21d | 100 | |
yueee_yt | 0:fdf9c2c5200f | 101 | respHeaders()["Connection"] = "close"; |
yueee_yt | 4:1b6b021ee21d | 102 | onWriteable(); |
yueee_yt | 4:1b6b021ee21d | 103 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 4:1b6b021ee21d | 104 | printf("\r\nExit SimpleHandler::doGet()\r\n"); |
yueee_yt | 4:1b6b021ee21d | 105 | #endif |
1050186 | 8:a9fb8fe9b8a3 | 106 | req_sem.release(); |
yueee_yt | 0:fdf9c2c5200f | 107 | } |
yueee_yt | 0:fdf9c2c5200f | 108 | |
yueee_yt | 0:fdf9c2c5200f | 109 | void FSHandler::doPost() |
yueee_yt | 0:fdf9c2c5200f | 110 | { |
yueee_yt | 0:fdf9c2c5200f | 111 | |
yueee_yt | 0:fdf9c2c5200f | 112 | } |
yueee_yt | 0:fdf9c2c5200f | 113 | |
yueee_yt | 0:fdf9c2c5200f | 114 | void FSHandler::doHead() |
yueee_yt | 0:fdf9c2c5200f | 115 | { |
yueee_yt | 0:fdf9c2c5200f | 116 | |
yueee_yt | 0:fdf9c2c5200f | 117 | } |
yueee_yt | 0:fdf9c2c5200f | 118 | |
yueee_yt | 0:fdf9c2c5200f | 119 | void FSHandler::onReadable() //Data has been read |
yueee_yt | 0:fdf9c2c5200f | 120 | { |
yueee_yt | 0:fdf9c2c5200f | 121 | |
yueee_yt | 0:fdf9c2c5200f | 122 | } |
yueee_yt | 0:fdf9c2c5200f | 123 | |
yueee_yt | 0:fdf9c2c5200f | 124 | void FSHandler::onWriteable() //Data has been written & buf is free |
yueee_yt | 0:fdf9c2c5200f | 125 | { |
yueee_yt | 4:1b6b021ee21d | 126 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 4:1b6b021ee21d | 127 | printf("\r\nFSHandler::onWriteable() event\r\n"); |
yueee_yt | 4:1b6b021ee21d | 128 | #endif |
yueee_yt | 4:1b6b021ee21d | 129 | if(m_err404) { |
yueee_yt | 4:1b6b021ee21d | 130 | //Error has been served, now exit |
yueee_yt | 4:1b6b021ee21d | 131 | close(); |
yueee_yt | 4:1b6b021ee21d | 132 | return; |
yueee_yt | 4:1b6b021ee21d | 133 | } |
yueee_yt | 4:1b6b021ee21d | 134 | |
yueee_yt | 4:1b6b021ee21d | 135 | static char rBuf[CHUNK_SIZE]; |
yueee_yt | 4:1b6b021ee21d | 136 | while(true) { |
yueee_yt | 4:1b6b021ee21d | 137 | int len = fread(rBuf, 1, CHUNK_SIZE, m_fp); |
yueee_yt | 4:1b6b021ee21d | 138 | if(len>0) { |
yueee_yt | 4:1b6b021ee21d | 139 | int writtenLen = writeData(rBuf, len); |
yueee_yt | 4:1b6b021ee21d | 140 | if(writtenLen < 0) { //Socket error |
yueee_yt | 4:1b6b021ee21d | 141 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 4:1b6b021ee21d | 142 | printf("FSHandler: Socket error %d\n", writtenLen); |
yueee_yt | 4:1b6b021ee21d | 143 | #endif |
yueee_yt | 5:b8f6a11c70db | 144 | /** Not Work |
yueee_yt | 5:b8f6a11c70db | 145 | if(writtenLen == TCPSOCKET_MEM) { |
yueee_yt | 5:b8f6a11c70db | 146 | fseek(m_fp, -len, SEEK_CUR); |
yueee_yt | 5:b8f6a11c70db | 147 | return; //Wait for the queued TCP segments to be transmitted |
yueee_yt | 5:b8f6a11c70db | 148 | } else { |
yueee_yt | 5:b8f6a11c70db | 149 | //This is a critical error |
yueee_yt | 5:b8f6a11c70db | 150 | **/ |
yueee_yt | 5:b8f6a11c70db | 151 | close(); |
yueee_yt | 5:b8f6a11c70db | 152 | return; |
yueee_yt | 5:b8f6a11c70db | 153 | /** |
yueee_yt | 5:b8f6a11c70db | 154 | } |
yueee_yt | 5:b8f6a11c70db | 155 | **/ |
yueee_yt | 4:1b6b021ee21d | 156 | } else if(writtenLen < len) { //Short write, socket's buffer is full |
yueee_yt | 4:1b6b021ee21d | 157 | fseek(m_fp, writtenLen - len, SEEK_CUR); |
yueee_yt | 4:1b6b021ee21d | 158 | return; |
yueee_yt | 4:1b6b021ee21d | 159 | } |
yueee_yt | 4:1b6b021ee21d | 160 | } else { |
yueee_yt | 4:1b6b021ee21d | 161 | close(); //Data written, we can close the connection |
yueee_yt | 4:1b6b021ee21d | 162 | return; |
yueee_yt | 0:fdf9c2c5200f | 163 | } |
yueee_yt | 0:fdf9c2c5200f | 164 | } |
yueee_yt | 0:fdf9c2c5200f | 165 | } |
yueee_yt | 0:fdf9c2c5200f | 166 | |
yueee_yt | 0:fdf9c2c5200f | 167 | void FSHandler::onClose() //Connection is closing |
yueee_yt | 0:fdf9c2c5200f | 168 | { |
yueee_yt | 5:b8f6a11c70db | 169 | /** |
yueee_yt | 5:b8f6a11c70db | 170 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 171 | printf("FSHandler: onClose start \r\n"); |
yueee_yt | 5:b8f6a11c70db | 172 | #endif |
yueee_yt | 5:b8f6a11c70db | 173 | if(m_fp){ |
yueee_yt | 5:b8f6a11c70db | 174 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 175 | printf("FSHandler: fclose start \r\n"); |
yueee_yt | 5:b8f6a11c70db | 176 | #endif |
yueee_yt | 5:b8f6a11c70db | 177 | fclose(m_fp); |
yueee_yt | 5:b8f6a11c70db | 178 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 179 | printf("FSHandler: fclose end \r\n"); |
yueee_yt | 5:b8f6a11c70db | 180 | #endif |
yueee_yt | 0:fdf9c2c5200f | 181 | } |
yueee_yt | 5:b8f6a11c70db | 182 | #ifdef _DEBUG_FS_HANDLER |
yueee_yt | 5:b8f6a11c70db | 183 | printf("FSHandler: onClose end \r\n"); |
yueee_yt | 5:b8f6a11c70db | 184 | #endif |
yueee_yt | 5:b8f6a11c70db | 185 | **/ |
yueee_yt | 5:b8f6a11c70db | 186 | } |
dkato | 11:0700755d64ae | 187 |