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