Lorcan Smith
/
Enet_SPI
SNMP agent attached to SPI slave
services/http/server/HTTPRequestDispatcher.cpp@0:2a53a4c3238c, 2012-08-13 (annotated)
- Committer:
- lorcansmith
- Date:
- Mon Aug 13 15:07:40 2012 +0000
- Revision:
- 0:2a53a4c3238c
v1.1 release includes ioAlarm traps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lorcansmith | 0:2a53a4c3238c | 1 | |
lorcansmith | 0:2a53a4c3238c | 2 | /* |
lorcansmith | 0:2a53a4c3238c | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
lorcansmith | 0:2a53a4c3238c | 4 | |
lorcansmith | 0:2a53a4c3238c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
lorcansmith | 0:2a53a4c3238c | 6 | of this software and associated documentation files (the "Software"), to deal |
lorcansmith | 0:2a53a4c3238c | 7 | in the Software without restriction, including without limitation the rights |
lorcansmith | 0:2a53a4c3238c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
lorcansmith | 0:2a53a4c3238c | 9 | copies of the Software, and to permit persons to whom the Software is |
lorcansmith | 0:2a53a4c3238c | 10 | furnished to do so, subject to the following conditions: |
lorcansmith | 0:2a53a4c3238c | 11 | |
lorcansmith | 0:2a53a4c3238c | 12 | The above copyright notice and this permission notice shall be included in |
lorcansmith | 0:2a53a4c3238c | 13 | all copies or substantial portions of the Software. |
lorcansmith | 0:2a53a4c3238c | 14 | |
lorcansmith | 0:2a53a4c3238c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
lorcansmith | 0:2a53a4c3238c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
lorcansmith | 0:2a53a4c3238c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
lorcansmith | 0:2a53a4c3238c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
lorcansmith | 0:2a53a4c3238c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
lorcansmith | 0:2a53a4c3238c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
lorcansmith | 0:2a53a4c3238c | 21 | THE SOFTWARE. |
lorcansmith | 0:2a53a4c3238c | 22 | */ |
lorcansmith | 0:2a53a4c3238c | 23 | |
lorcansmith | 0:2a53a4c3238c | 24 | #include "core/netservice.h" |
lorcansmith | 0:2a53a4c3238c | 25 | #include "HTTPRequestDispatcher.h" |
lorcansmith | 0:2a53a4c3238c | 26 | #include "HTTPRequestHandler.h" |
lorcansmith | 0:2a53a4c3238c | 27 | #include <string.h> |
lorcansmith | 0:2a53a4c3238c | 28 | |
lorcansmith | 0:2a53a4c3238c | 29 | //#define __DEBUG |
lorcansmith | 0:2a53a4c3238c | 30 | #include "dbg/dbg.h" |
lorcansmith | 0:2a53a4c3238c | 31 | |
lorcansmith | 0:2a53a4c3238c | 32 | HTTPRequestDispatcher::HTTPRequestDispatcher(HTTPServer* pSvr, TCPSocket* pTCPSocket) : NetService(), m_pSvr(pSvr), m_pTCPSocket(pTCPSocket), m_watchdog(), m_closed(false) |
lorcansmith | 0:2a53a4c3238c | 33 | { |
lorcansmith | 0:2a53a4c3238c | 34 | m_pTCPSocket->setOnEvent(this, &HTTPRequestDispatcher::onTCPSocketEvent); |
lorcansmith | 0:2a53a4c3238c | 35 | m_watchdog.attach_us<HTTPRequestDispatcher>(this, &HTTPRequestDispatcher::onTimeout, HTTP_REQUEST_TIMEOUT * 1000); |
lorcansmith | 0:2a53a4c3238c | 36 | } |
lorcansmith | 0:2a53a4c3238c | 37 | |
lorcansmith | 0:2a53a4c3238c | 38 | HTTPRequestDispatcher::~HTTPRequestDispatcher() |
lorcansmith | 0:2a53a4c3238c | 39 | { |
lorcansmith | 0:2a53a4c3238c | 40 | close(); |
lorcansmith | 0:2a53a4c3238c | 41 | } |
lorcansmith | 0:2a53a4c3238c | 42 | |
lorcansmith | 0:2a53a4c3238c | 43 | void HTTPRequestDispatcher::dispatchRequest() |
lorcansmith | 0:2a53a4c3238c | 44 | { |
lorcansmith | 0:2a53a4c3238c | 45 | string path; |
lorcansmith | 0:2a53a4c3238c | 46 | string meth; |
lorcansmith | 0:2a53a4c3238c | 47 | HTTP_METH methCode; |
lorcansmith | 0:2a53a4c3238c | 48 | |
lorcansmith | 0:2a53a4c3238c | 49 | DBG("Dispatching req\r\n"); |
lorcansmith | 0:2a53a4c3238c | 50 | |
lorcansmith | 0:2a53a4c3238c | 51 | if( !getRequest(&path, &meth ) ) |
lorcansmith | 0:2a53a4c3238c | 52 | { |
lorcansmith | 0:2a53a4c3238c | 53 | close(); |
lorcansmith | 0:2a53a4c3238c | 54 | return; //Invalid request |
lorcansmith | 0:2a53a4c3238c | 55 | } |
lorcansmith | 0:2a53a4c3238c | 56 | |
lorcansmith | 0:2a53a4c3238c | 57 | if( !meth.compare("GET") ) |
lorcansmith | 0:2a53a4c3238c | 58 | { |
lorcansmith | 0:2a53a4c3238c | 59 | methCode = HTTP_GET; |
lorcansmith | 0:2a53a4c3238c | 60 | } |
lorcansmith | 0:2a53a4c3238c | 61 | else if( !meth.compare("POST") ) |
lorcansmith | 0:2a53a4c3238c | 62 | { |
lorcansmith | 0:2a53a4c3238c | 63 | methCode = HTTP_POST; |
lorcansmith | 0:2a53a4c3238c | 64 | } |
lorcansmith | 0:2a53a4c3238c | 65 | else if( !meth.compare("HEAD") ) |
lorcansmith | 0:2a53a4c3238c | 66 | { |
lorcansmith | 0:2a53a4c3238c | 67 | methCode = HTTP_HEAD; |
lorcansmith | 0:2a53a4c3238c | 68 | } |
lorcansmith | 0:2a53a4c3238c | 69 | else |
lorcansmith | 0:2a53a4c3238c | 70 | { |
lorcansmith | 0:2a53a4c3238c | 71 | close(); //Parse error |
lorcansmith | 0:2a53a4c3238c | 72 | return; |
lorcansmith | 0:2a53a4c3238c | 73 | } |
lorcansmith | 0:2a53a4c3238c | 74 | |
lorcansmith | 0:2a53a4c3238c | 75 | DBG("Looking for a handler\r\n"); |
lorcansmith | 0:2a53a4c3238c | 76 | |
lorcansmith | 0:2a53a4c3238c | 77 | map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it; |
lorcansmith | 0:2a53a4c3238c | 78 | // it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that |
lorcansmith | 0:2a53a4c3238c | 79 | // NEW CODE START: |
lorcansmith | 0:2a53a4c3238c | 80 | int root_len = 0; |
lorcansmith | 0:2a53a4c3238c | 81 | for (it = m_pSvr->m_lpHandlers.begin(); it != m_pSvr->m_lpHandlers.end(); it++) |
lorcansmith | 0:2a53a4c3238c | 82 | { |
lorcansmith | 0:2a53a4c3238c | 83 | DBG("Checking %s...\n", (*it).first.c_str()); |
lorcansmith | 0:2a53a4c3238c | 84 | root_len = (*it).first.length(); |
lorcansmith | 0:2a53a4c3238c | 85 | if ( root_len && |
lorcansmith | 0:2a53a4c3238c | 86 | !path.compare( 0, root_len, (*it).first ) && |
lorcansmith | 0:2a53a4c3238c | 87 | (path[root_len] == '/' || path[root_len] == '\0')) |
lorcansmith | 0:2a53a4c3238c | 88 | { |
lorcansmith | 0:2a53a4c3238c | 89 | DBG("Found (%s)\n", (*it).first.c_str()); |
lorcansmith | 0:2a53a4c3238c | 90 | // Found! |
lorcansmith | 0:2a53a4c3238c | 91 | break; // for |
lorcansmith | 0:2a53a4c3238c | 92 | } |
lorcansmith | 0:2a53a4c3238c | 93 | } |
lorcansmith | 0:2a53a4c3238c | 94 | // NEW CODE END |
lorcansmith | 0:2a53a4c3238c | 95 | if((it == m_pSvr->m_lpHandlers.end()) && !(m_pSvr->m_lpHandlers.empty())) |
lorcansmith | 0:2a53a4c3238c | 96 | { |
lorcansmith | 0:2a53a4c3238c | 97 | DBG("Using default handler\n"); |
lorcansmith | 0:2a53a4c3238c | 98 | it = m_pSvr->m_lpHandlers.end(); |
lorcansmith | 0:2a53a4c3238c | 99 | it--; //Get the last element |
lorcansmith | 0:2a53a4c3238c | 100 | if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler |
lorcansmith | 0:2a53a4c3238c | 101 | it = m_pSvr->m_lpHandlers.end(); |
lorcansmith | 0:2a53a4c3238c | 102 | root_len = 0; |
lorcansmith | 0:2a53a4c3238c | 103 | } |
lorcansmith | 0:2a53a4c3238c | 104 | if(it == m_pSvr->m_lpHandlers.end()) |
lorcansmith | 0:2a53a4c3238c | 105 | { |
lorcansmith | 0:2a53a4c3238c | 106 | DBG("No handler found\n"); |
lorcansmith | 0:2a53a4c3238c | 107 | close(); //No handler found |
lorcansmith | 0:2a53a4c3238c | 108 | return; |
lorcansmith | 0:2a53a4c3238c | 109 | } |
lorcansmith | 0:2a53a4c3238c | 110 | |
lorcansmith | 0:2a53a4c3238c | 111 | DBG("Handler found.\r\n"); |
lorcansmith | 0:2a53a4c3238c | 112 | |
lorcansmith | 0:2a53a4c3238c | 113 | //HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket); |
lorcansmith | 0:2a53a4c3238c | 114 | //NEW CODE 1 LINE: |
lorcansmith | 0:2a53a4c3238c | 115 | HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, m_pTCPSocket); |
lorcansmith | 0:2a53a4c3238c | 116 | m_pTCPSocket = NULL; //We don't own it anymore |
lorcansmith | 0:2a53a4c3238c | 117 | |
lorcansmith | 0:2a53a4c3238c | 118 | switch(methCode) |
lorcansmith | 0:2a53a4c3238c | 119 | { |
lorcansmith | 0:2a53a4c3238c | 120 | case HTTP_GET: |
lorcansmith | 0:2a53a4c3238c | 121 | pHdlr->doGet(); |
lorcansmith | 0:2a53a4c3238c | 122 | break; |
lorcansmith | 0:2a53a4c3238c | 123 | case HTTP_POST: |
lorcansmith | 0:2a53a4c3238c | 124 | pHdlr->doPost(); |
lorcansmith | 0:2a53a4c3238c | 125 | break; |
lorcansmith | 0:2a53a4c3238c | 126 | case HTTP_HEAD: |
lorcansmith | 0:2a53a4c3238c | 127 | pHdlr->doHead(); |
lorcansmith | 0:2a53a4c3238c | 128 | break; |
lorcansmith | 0:2a53a4c3238c | 129 | } |
lorcansmith | 0:2a53a4c3238c | 130 | |
lorcansmith | 0:2a53a4c3238c | 131 | DBG("Req handled (or being handled)\r\n"); |
lorcansmith | 0:2a53a4c3238c | 132 | close(); |
lorcansmith | 0:2a53a4c3238c | 133 | } |
lorcansmith | 0:2a53a4c3238c | 134 | |
lorcansmith | 0:2a53a4c3238c | 135 | void HTTPRequestDispatcher::close() //Close socket and destroy data |
lorcansmith | 0:2a53a4c3238c | 136 | { |
lorcansmith | 0:2a53a4c3238c | 137 | if(m_closed) |
lorcansmith | 0:2a53a4c3238c | 138 | return; |
lorcansmith | 0:2a53a4c3238c | 139 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
lorcansmith | 0:2a53a4c3238c | 140 | m_watchdog.detach(); |
lorcansmith | 0:2a53a4c3238c | 141 | if(m_pTCPSocket) //m_pTCPSocket Should only be destroyed if ownership not passed to an handler |
lorcansmith | 0:2a53a4c3238c | 142 | { |
lorcansmith | 0:2a53a4c3238c | 143 | m_pTCPSocket->resetOnEvent(); |
lorcansmith | 0:2a53a4c3238c | 144 | m_pTCPSocket->close(); |
lorcansmith | 0:2a53a4c3238c | 145 | delete m_pTCPSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE |
lorcansmith | 0:2a53a4c3238c | 146 | } |
lorcansmith | 0:2a53a4c3238c | 147 | NetService::close(); |
lorcansmith | 0:2a53a4c3238c | 148 | } |
lorcansmith | 0:2a53a4c3238c | 149 | |
lorcansmith | 0:2a53a4c3238c | 150 | |
lorcansmith | 0:2a53a4c3238c | 151 | void HTTPRequestDispatcher::onTimeout() //Connection has timed out |
lorcansmith | 0:2a53a4c3238c | 152 | { |
lorcansmith | 0:2a53a4c3238c | 153 | close(); |
lorcansmith | 0:2a53a4c3238c | 154 | } |
lorcansmith | 0:2a53a4c3238c | 155 | |
lorcansmith | 0:2a53a4c3238c | 156 | bool HTTPRequestDispatcher::getRequest(string* path, string* meth) |
lorcansmith | 0:2a53a4c3238c | 157 | { |
lorcansmith | 0:2a53a4c3238c | 158 | char req[128]; |
lorcansmith | 0:2a53a4c3238c | 159 | char c_path[128]; |
lorcansmith | 0:2a53a4c3238c | 160 | char c_meth[128]; |
lorcansmith | 0:2a53a4c3238c | 161 | const int maxLen = 128; |
lorcansmith | 0:2a53a4c3238c | 162 | char* p = req; |
lorcansmith | 0:2a53a4c3238c | 163 | //Read Line |
lorcansmith | 0:2a53a4c3238c | 164 | int ret; |
lorcansmith | 0:2a53a4c3238c | 165 | int len = 0; |
lorcansmith | 0:2a53a4c3238c | 166 | for(int i = 0; i < maxLen - 1; i++) |
lorcansmith | 0:2a53a4c3238c | 167 | { |
lorcansmith | 0:2a53a4c3238c | 168 | ret = m_pTCPSocket->recv(p, 1); |
lorcansmith | 0:2a53a4c3238c | 169 | if(!ret) |
lorcansmith | 0:2a53a4c3238c | 170 | { |
lorcansmith | 0:2a53a4c3238c | 171 | break; |
lorcansmith | 0:2a53a4c3238c | 172 | } |
lorcansmith | 0:2a53a4c3238c | 173 | if( (len > 1) && *(p-1)=='\r' && *p=='\n' ) |
lorcansmith | 0:2a53a4c3238c | 174 | { |
lorcansmith | 0:2a53a4c3238c | 175 | p--; |
lorcansmith | 0:2a53a4c3238c | 176 | len-=2; |
lorcansmith | 0:2a53a4c3238c | 177 | break; |
lorcansmith | 0:2a53a4c3238c | 178 | } |
lorcansmith | 0:2a53a4c3238c | 179 | else if( *p=='\n' ) |
lorcansmith | 0:2a53a4c3238c | 180 | { |
lorcansmith | 0:2a53a4c3238c | 181 | len--; |
lorcansmith | 0:2a53a4c3238c | 182 | break; |
lorcansmith | 0:2a53a4c3238c | 183 | } |
lorcansmith | 0:2a53a4c3238c | 184 | p++; |
lorcansmith | 0:2a53a4c3238c | 185 | len++; |
lorcansmith | 0:2a53a4c3238c | 186 | } |
lorcansmith | 0:2a53a4c3238c | 187 | *p = 0; |
lorcansmith | 0:2a53a4c3238c | 188 | |
lorcansmith | 0:2a53a4c3238c | 189 | DBG("Parsing request : %s\r\n", req); |
lorcansmith | 0:2a53a4c3238c | 190 | |
lorcansmith | 0:2a53a4c3238c | 191 | ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path); |
lorcansmith | 0:2a53a4c3238c | 192 | if(ret !=2) |
lorcansmith | 0:2a53a4c3238c | 193 | return false; |
lorcansmith | 0:2a53a4c3238c | 194 | |
lorcansmith | 0:2a53a4c3238c | 195 | *meth = string(c_meth); |
lorcansmith | 0:2a53a4c3238c | 196 | // NEW CODE (old code removed): |
lorcansmith | 0:2a53a4c3238c | 197 | *path = string(c_path); |
lorcansmith | 0:2a53a4c3238c | 198 | return true; |
lorcansmith | 0:2a53a4c3238c | 199 | } |
lorcansmith | 0:2a53a4c3238c | 200 | |
lorcansmith | 0:2a53a4c3238c | 201 | |
lorcansmith | 0:2a53a4c3238c | 202 | |
lorcansmith | 0:2a53a4c3238c | 203 | void HTTPRequestDispatcher::onTCPSocketEvent(TCPSocketEvent e) |
lorcansmith | 0:2a53a4c3238c | 204 | { |
lorcansmith | 0:2a53a4c3238c | 205 | |
lorcansmith | 0:2a53a4c3238c | 206 | DBG("\r\nEvent %d\r\n", e); |
lorcansmith | 0:2a53a4c3238c | 207 | |
lorcansmith | 0:2a53a4c3238c | 208 | if(m_closed) |
lorcansmith | 0:2a53a4c3238c | 209 | { |
lorcansmith | 0:2a53a4c3238c | 210 | DBG("\r\nWARN: Discarded\r\n"); |
lorcansmith | 0:2a53a4c3238c | 211 | return; |
lorcansmith | 0:2a53a4c3238c | 212 | } |
lorcansmith | 0:2a53a4c3238c | 213 | |
lorcansmith | 0:2a53a4c3238c | 214 | switch(e) |
lorcansmith | 0:2a53a4c3238c | 215 | { |
lorcansmith | 0:2a53a4c3238c | 216 | case TCPSOCKET_READABLE: |
lorcansmith | 0:2a53a4c3238c | 217 | m_watchdog.detach(); |
lorcansmith | 0:2a53a4c3238c | 218 | m_pTCPSocket->resetOnEvent(); |
lorcansmith | 0:2a53a4c3238c | 219 | //Req arrived, dispatch : |
lorcansmith | 0:2a53a4c3238c | 220 | dispatchRequest(); |
lorcansmith | 0:2a53a4c3238c | 221 | break; |
lorcansmith | 0:2a53a4c3238c | 222 | case TCPSOCKET_CONTIMEOUT: |
lorcansmith | 0:2a53a4c3238c | 223 | case TCPSOCKET_CONRST: |
lorcansmith | 0:2a53a4c3238c | 224 | case TCPSOCKET_CONABRT: |
lorcansmith | 0:2a53a4c3238c | 225 | case TCPSOCKET_ERROR: |
lorcansmith | 0:2a53a4c3238c | 226 | case TCPSOCKET_DISCONNECTED: |
lorcansmith | 0:2a53a4c3238c | 227 | close(); |
lorcansmith | 0:2a53a4c3238c | 228 | break; |
lorcansmith | 0:2a53a4c3238c | 229 | } |
lorcansmith | 0:2a53a4c3238c | 230 | |
lorcansmith | 0:2a53a4c3238c | 231 | } |