NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Thu Aug 05 14:32:43 2010 +0000
Revision:
9:c79fa4034f5b
Parent:
3:95e0bc00a1bb

        

Who changed what in which revision?

UserRevisionLine numberNew 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 9:c79fa4034f5b 24 #include "core/netservice.h"
donatien 0:632c9925f013 25 #include "HTTPRequestDispatcher.h"
donatien 0:632c9925f013 26 #include "HTTPRequestHandler.h"
donatien 0:632c9925f013 27 #include <string.h>
donatien 0:632c9925f013 28
donatien 0:632c9925f013 29 //#define __DEBUG
donatien 0:632c9925f013 30 #include "dbg/dbg.h"
donatien 0:632c9925f013 31
donatien 0:632c9925f013 32 HTTPRequestDispatcher::HTTPRequestDispatcher(HTTPServer* pSvr, TCPSocket* pTCPSocket) : NetService(), m_pSvr(pSvr), m_pTCPSocket(pTCPSocket), m_watchdog(), m_closed(false)
donatien 0:632c9925f013 33 {
donatien 0:632c9925f013 34 m_pTCPSocket->setOnEvent(this, &HTTPRequestDispatcher::onTCPSocketEvent);
donatien 0:632c9925f013 35 m_watchdog.attach_us<HTTPRequestDispatcher>(this, &HTTPRequestDispatcher::onTimeout, HTTP_REQUEST_TIMEOUT * 1000);
donatien 0:632c9925f013 36 }
donatien 0:632c9925f013 37
donatien 0:632c9925f013 38 HTTPRequestDispatcher::~HTTPRequestDispatcher()
donatien 0:632c9925f013 39 {
donatien 0:632c9925f013 40 close();
donatien 0:632c9925f013 41 }
donatien 0:632c9925f013 42
donatien 0:632c9925f013 43 void HTTPRequestDispatcher::dispatchRequest()
donatien 0:632c9925f013 44 {
donatien 2:a4f97773c90f 45 string path;
donatien 0:632c9925f013 46 string meth;
donatien 0:632c9925f013 47 HTTP_METH methCode;
donatien 0:632c9925f013 48
donatien 2:a4f97773c90f 49 DBG("Dispatching req\r\n");
donatien 0:632c9925f013 50
donatien 2:a4f97773c90f 51 if( !getRequest(&path, &meth ) )
donatien 0:632c9925f013 52 {
donatien 0:632c9925f013 53 close();
donatien 0:632c9925f013 54 return; //Invalid request
donatien 0:632c9925f013 55 }
donatien 0:632c9925f013 56
donatien 0:632c9925f013 57 if( !meth.compare("GET") )
donatien 0:632c9925f013 58 {
donatien 0:632c9925f013 59 methCode = HTTP_GET;
donatien 0:632c9925f013 60 }
donatien 0:632c9925f013 61 else if( !meth.compare("POST") )
donatien 0:632c9925f013 62 {
donatien 0:632c9925f013 63 methCode = HTTP_POST;
donatien 0:632c9925f013 64 }
donatien 0:632c9925f013 65 else if( !meth.compare("HEAD") )
donatien 0:632c9925f013 66 {
donatien 0:632c9925f013 67 methCode = HTTP_HEAD;
donatien 0:632c9925f013 68 }
donatien 0:632c9925f013 69 else
donatien 0:632c9925f013 70 {
donatien 0:632c9925f013 71 close(); //Parse error
donatien 0:632c9925f013 72 return;
donatien 0:632c9925f013 73 }
donatien 0:632c9925f013 74
donatien 2:a4f97773c90f 75 DBG("Looking for a handler\r\n");
donatien 0:632c9925f013 76
donatien 0:632c9925f013 77 map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it;
donatien 2:a4f97773c90f 78 // it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that
donatien 2:a4f97773c90f 79 // NEW CODE START:
donatien 2:a4f97773c90f 80 int root_len = 0;
donatien 2:a4f97773c90f 81 for (it = m_pSvr->m_lpHandlers.begin(); it != m_pSvr->m_lpHandlers.end(); it++)
donatien 0:632c9925f013 82 {
donatien 2:a4f97773c90f 83 DBG("Checking %s...\n", (*it).first.c_str());
donatien 2:a4f97773c90f 84 root_len = (*it).first.length();
donatien 2:a4f97773c90f 85 if ( root_len &&
donatien 2:a4f97773c90f 86 !path.compare( 0, root_len, (*it).first ) &&
donatien 2:a4f97773c90f 87 (path[root_len] == '/' || path[root_len] == '\0'))
donatien 2:a4f97773c90f 88 {
donatien 2:a4f97773c90f 89 DBG("Found (%s)\n", (*it).first.c_str());
donatien 2:a4f97773c90f 90 // Found!
donatien 2:a4f97773c90f 91 break; // for
donatien 2:a4f97773c90f 92 }
donatien 2:a4f97773c90f 93 }
donatien 2:a4f97773c90f 94 // NEW CODE END
donatien 2:a4f97773c90f 95 if((it == m_pSvr->m_lpHandlers.end()) && !(m_pSvr->m_lpHandlers.empty()))
donatien 2:a4f97773c90f 96 {
donatien 2:a4f97773c90f 97 DBG("Using default handler\n");
donatien 2:a4f97773c90f 98 it = m_pSvr->m_lpHandlers.end();
donatien 2:a4f97773c90f 99 it--; //Get the last element
donatien 3:95e0bc00a1bb 100 if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler
donatien 2:a4f97773c90f 101 it = m_pSvr->m_lpHandlers.end();
donatien 2:a4f97773c90f 102 root_len = 0;
donatien 0:632c9925f013 103 }
donatien 0:632c9925f013 104 if(it == m_pSvr->m_lpHandlers.end())
donatien 0:632c9925f013 105 {
donatien 2:a4f97773c90f 106 DBG("No handler found\n");
donatien 0:632c9925f013 107 close(); //No handler found
donatien 0:632c9925f013 108 return;
donatien 0:632c9925f013 109 }
donatien 0:632c9925f013 110
donatien 2:a4f97773c90f 111 DBG("Handler found.\r\n");
donatien 0:632c9925f013 112
donatien 2:a4f97773c90f 113 //HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket);
donatien 2:a4f97773c90f 114 //NEW CODE 1 LINE:
donatien 2:a4f97773c90f 115 HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, m_pTCPSocket);
donatien 0:632c9925f013 116 m_pTCPSocket = NULL; //We don't own it anymore
donatien 0:632c9925f013 117
donatien 0:632c9925f013 118 switch(methCode)
donatien 0:632c9925f013 119 {
donatien 0:632c9925f013 120 case HTTP_GET:
donatien 0:632c9925f013 121 pHdlr->doGet();
donatien 0:632c9925f013 122 break;
donatien 0:632c9925f013 123 case HTTP_POST:
donatien 0:632c9925f013 124 pHdlr->doPost();
donatien 0:632c9925f013 125 break;
donatien 0:632c9925f013 126 case HTTP_HEAD:
donatien 0:632c9925f013 127 pHdlr->doHead();
donatien 0:632c9925f013 128 break;
donatien 0:632c9925f013 129 }
donatien 0:632c9925f013 130
donatien 2:a4f97773c90f 131 DBG("Req handled (or being handled)\r\n");
donatien 0:632c9925f013 132 close();
donatien 0:632c9925f013 133 }
donatien 0:632c9925f013 134
donatien 0:632c9925f013 135 void HTTPRequestDispatcher::close() //Close socket and destroy data
donatien 0:632c9925f013 136 {
donatien 0:632c9925f013 137 if(m_closed)
donatien 0:632c9925f013 138 return;
donatien 0:632c9925f013 139 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
donatien 0:632c9925f013 140 m_watchdog.detach();
donatien 0:632c9925f013 141 if(m_pTCPSocket) //m_pTCPSocket Should only be destroyed if ownership not passed to an handler
donatien 0:632c9925f013 142 {
donatien 0:632c9925f013 143 m_pTCPSocket->resetOnEvent();
donatien 0:632c9925f013 144 m_pTCPSocket->close();
donatien 0:632c9925f013 145 delete m_pTCPSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE
donatien 0:632c9925f013 146 }
donatien 0:632c9925f013 147 NetService::close();
donatien 0:632c9925f013 148 }
donatien 0:632c9925f013 149
donatien 0:632c9925f013 150
donatien 0:632c9925f013 151 void HTTPRequestDispatcher::onTimeout() //Connection has timed out
donatien 0:632c9925f013 152 {
donatien 0:632c9925f013 153 close();
donatien 0:632c9925f013 154 }
donatien 0:632c9925f013 155
donatien 2:a4f97773c90f 156 bool HTTPRequestDispatcher::getRequest(string* path, string* meth)
donatien 0:632c9925f013 157 {
donatien 0:632c9925f013 158 char req[128];
donatien 0:632c9925f013 159 char c_path[128];
donatien 0:632c9925f013 160 char c_meth[128];
donatien 0:632c9925f013 161 const int maxLen = 128;
donatien 0:632c9925f013 162 char* p = req;
donatien 0:632c9925f013 163 //Read Line
donatien 0:632c9925f013 164 int ret;
donatien 0:632c9925f013 165 int len = 0;
donatien 0:632c9925f013 166 for(int i = 0; i < maxLen - 1; i++)
donatien 0:632c9925f013 167 {
donatien 0:632c9925f013 168 ret = m_pTCPSocket->recv(p, 1);
donatien 0:632c9925f013 169 if(!ret)
donatien 0:632c9925f013 170 {
donatien 0:632c9925f013 171 break;
donatien 0:632c9925f013 172 }
donatien 0:632c9925f013 173 if( (len > 1) && *(p-1)=='\r' && *p=='\n' )
donatien 0:632c9925f013 174 {
donatien 0:632c9925f013 175 p--;
donatien 0:632c9925f013 176 len-=2;
donatien 0:632c9925f013 177 break;
donatien 0:632c9925f013 178 }
donatien 0:632c9925f013 179 else if( *p=='\n' )
donatien 0:632c9925f013 180 {
donatien 0:632c9925f013 181 len--;
donatien 0:632c9925f013 182 break;
donatien 0:632c9925f013 183 }
donatien 0:632c9925f013 184 p++;
donatien 0:632c9925f013 185 len++;
donatien 0:632c9925f013 186 }
donatien 0:632c9925f013 187 *p = 0;
donatien 0:632c9925f013 188
donatien 2:a4f97773c90f 189 DBG("Parsing request : %s\r\n", req);
donatien 0:632c9925f013 190
donatien 0:632c9925f013 191 ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path);
donatien 0:632c9925f013 192 if(ret !=2)
donatien 0:632c9925f013 193 return false;
donatien 0:632c9925f013 194
donatien 0:632c9925f013 195 *meth = string(c_meth);
donatien 2:a4f97773c90f 196 // NEW CODE (old code removed):
donatien 2:a4f97773c90f 197 *path = string(c_path);
donatien 0:632c9925f013 198 return true;
donatien 2:a4f97773c90f 199 }
donatien 0:632c9925f013 200
donatien 0:632c9925f013 201
donatien 0:632c9925f013 202
donatien 0:632c9925f013 203 void HTTPRequestDispatcher::onTCPSocketEvent(TCPSocketEvent e)
donatien 0:632c9925f013 204 {
donatien 0:632c9925f013 205
donatien 0:632c9925f013 206 DBG("\r\nEvent %d\r\n", e);
donatien 0:632c9925f013 207
donatien 0:632c9925f013 208 if(m_closed)
donatien 0:632c9925f013 209 {
donatien 0:632c9925f013 210 DBG("\r\nWARN: Discarded\r\n");
donatien 0:632c9925f013 211 return;
donatien 0:632c9925f013 212 }
donatien 0:632c9925f013 213
donatien 0:632c9925f013 214 switch(e)
donatien 0:632c9925f013 215 {
donatien 0:632c9925f013 216 case TCPSOCKET_READABLE:
donatien 0:632c9925f013 217 m_watchdog.detach();
donatien 0:632c9925f013 218 m_pTCPSocket->resetOnEvent();
donatien 0:632c9925f013 219 //Req arrived, dispatch :
donatien 0:632c9925f013 220 dispatchRequest();
donatien 0:632c9925f013 221 break;
donatien 0:632c9925f013 222 case TCPSOCKET_CONTIMEOUT:
donatien 0:632c9925f013 223 case TCPSOCKET_CONRST:
donatien 0:632c9925f013 224 case TCPSOCKET_CONABRT:
donatien 0:632c9925f013 225 case TCPSOCKET_ERROR:
donatien 0:632c9925f013 226 case TCPSOCKET_DISCONNECTED:
donatien 0:632c9925f013 227 close();
donatien 0:632c9925f013 228 break;
donatien 0:632c9925f013 229 }
donatien 0:632c9925f013 230
donatien 0:632c9925f013 231 }