NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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