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