EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Mon Jun 14 10:33:54 2010 +0000
Revision:
1:abb442332fa8
Parent:
0:632c9925f013
Child:
2:a4f97773c90f

        

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 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 0:632c9925f013 44 string rootPath;
donatien 1:abb442332fa8 45 string subPath;
donatien 0:632c9925f013 46 string meth;
donatien 0:632c9925f013 47 HTTP_METH methCode;
donatien 0:632c9925f013 48
donatien 0:632c9925f013 49 DBG("\r\nDispatching req\r\n");
donatien 0:632c9925f013 50
donatien 1:abb442332fa8 51 if( !getRequest(&rootPath, &subPath, &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 0:632c9925f013 75 DBG("\r\nLooking for a handler\r\n");
donatien 0:632c9925f013 76
donatien 0:632c9925f013 77 map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it;
donatien 0:632c9925f013 78 it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that
donatien 0:632c9925f013 79 if(it == m_pSvr->m_lpHandlers.end())
donatien 0:632c9925f013 80 {
donatien 0:632c9925f013 81 it = m_pSvr->m_lpHandlers.find(""); //Use default handler if it exists
donatien 0:632c9925f013 82 }
donatien 0:632c9925f013 83 if(it == m_pSvr->m_lpHandlers.end())
donatien 0:632c9925f013 84 {
donatien 0:632c9925f013 85 close(); //No handler found
donatien 0:632c9925f013 86 return;
donatien 0:632c9925f013 87 }
donatien 0:632c9925f013 88
donatien 0:632c9925f013 89 DBG("\r\nHandler found.\r\n");
donatien 0:632c9925f013 90
donatien 1:abb442332fa8 91 HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket);
donatien 0:632c9925f013 92 m_pTCPSocket = NULL; //We don't own it anymore
donatien 0:632c9925f013 93
donatien 0:632c9925f013 94 switch(methCode)
donatien 0:632c9925f013 95 {
donatien 0:632c9925f013 96 case HTTP_GET:
donatien 0:632c9925f013 97 pHdlr->doGet();
donatien 0:632c9925f013 98 break;
donatien 0:632c9925f013 99 case HTTP_POST:
donatien 0:632c9925f013 100 pHdlr->doPost();
donatien 0:632c9925f013 101 break;
donatien 0:632c9925f013 102 case HTTP_HEAD:
donatien 0:632c9925f013 103 pHdlr->doHead();
donatien 0:632c9925f013 104 break;
donatien 0:632c9925f013 105 }
donatien 0:632c9925f013 106
donatien 0:632c9925f013 107 DBG("\r\nReq handled (or being handled)\r\n");
donatien 0:632c9925f013 108 close();
donatien 0:632c9925f013 109 }
donatien 0:632c9925f013 110
donatien 0:632c9925f013 111 void HTTPRequestDispatcher::close() //Close socket and destroy data
donatien 0:632c9925f013 112 {
donatien 0:632c9925f013 113 if(m_closed)
donatien 0:632c9925f013 114 return;
donatien 0:632c9925f013 115 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
donatien 0:632c9925f013 116 m_watchdog.detach();
donatien 0:632c9925f013 117 if(m_pTCPSocket) //m_pTCPSocket Should only be destroyed if ownership not passed to an handler
donatien 0:632c9925f013 118 {
donatien 0:632c9925f013 119 m_pTCPSocket->resetOnEvent();
donatien 0:632c9925f013 120 m_pTCPSocket->close();
donatien 0:632c9925f013 121 delete m_pTCPSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE
donatien 0:632c9925f013 122 }
donatien 0:632c9925f013 123 NetService::close();
donatien 0:632c9925f013 124 }
donatien 0:632c9925f013 125
donatien 0:632c9925f013 126
donatien 0:632c9925f013 127 void HTTPRequestDispatcher::onTimeout() //Connection has timed out
donatien 0:632c9925f013 128 {
donatien 0:632c9925f013 129 close();
donatien 0:632c9925f013 130 }
donatien 0:632c9925f013 131
donatien 1:abb442332fa8 132 bool HTTPRequestDispatcher::getRequest(string* rootPath, string* subPath, string* meth)
donatien 0:632c9925f013 133 {
donatien 0:632c9925f013 134 char req[128];
donatien 0:632c9925f013 135 char c_path[128];
donatien 0:632c9925f013 136 char c_meth[128];
donatien 0:632c9925f013 137 const int maxLen = 128;
donatien 0:632c9925f013 138 char* p = req;
donatien 0:632c9925f013 139 //Read Line
donatien 0:632c9925f013 140 int ret;
donatien 0:632c9925f013 141 int len = 0;
donatien 0:632c9925f013 142 for(int i = 0; i < maxLen - 1; i++)
donatien 0:632c9925f013 143 {
donatien 0:632c9925f013 144 ret = m_pTCPSocket->recv(p, 1);
donatien 0:632c9925f013 145 if(!ret)
donatien 0:632c9925f013 146 {
donatien 0:632c9925f013 147 break;
donatien 0:632c9925f013 148 }
donatien 0:632c9925f013 149 if( (len > 1) && *(p-1)=='\r' && *p=='\n' )
donatien 0:632c9925f013 150 {
donatien 0:632c9925f013 151 p--;
donatien 0:632c9925f013 152 len-=2;
donatien 0:632c9925f013 153 break;
donatien 0:632c9925f013 154 }
donatien 0:632c9925f013 155 else if( *p=='\n' )
donatien 0:632c9925f013 156 {
donatien 0:632c9925f013 157 len--;
donatien 0:632c9925f013 158 break;
donatien 0:632c9925f013 159 }
donatien 0:632c9925f013 160 p++;
donatien 0:632c9925f013 161 len++;
donatien 0:632c9925f013 162 }
donatien 0:632c9925f013 163 *p = 0;
donatien 0:632c9925f013 164
donatien 0:632c9925f013 165 DBG("\r\nParsing request : %s\r\n", req);
donatien 0:632c9925f013 166
donatien 0:632c9925f013 167 ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path);
donatien 0:632c9925f013 168 if(ret !=2)
donatien 0:632c9925f013 169 return false;
donatien 0:632c9925f013 170
donatien 0:632c9925f013 171 *meth = string(c_meth);
donatien 1:abb442332fa8 172 *subPath = string(c_path);
donatien 0:632c9925f013 173
donatien 0:632c9925f013 174 c_path[0]= '/';
donatien 1:abb442332fa8 175 if(!strchr(c_path+1, '/'))
donatien 1:abb442332fa8 176 {
donatien 1:abb442332fa8 177 //Not found, so this is the root path
donatien 1:abb442332fa8 178 c_path[1]=0;
donatien 1:abb442332fa8 179 *rootPath = string(c_path);
donatien 1:abb442332fa8 180 }
donatien 1:abb442332fa8 181 else
donatien 0:632c9925f013 182 {
donatien 1:abb442332fa8 183 ret = sscanf(req, "%*s /%[^/ ]/%*s HTTP/%*d.%*d", c_path+1);
donatien 1:abb442332fa8 184 if(ret !=1)
donatien 1:abb442332fa8 185 {
donatien 1:abb442332fa8 186 //This is the root path
donatien 1:abb442332fa8 187 DBG("Default path\n");
donatien 1:abb442332fa8 188 }
donatien 1:abb442332fa8 189 *rootPath = string(c_path);
donatien 1:abb442332fa8 190 subPath->erase(0,rootPath->length());
donatien 0:632c9925f013 191 }
donatien 1:abb442332fa8 192
donatien 0:632c9925f013 193
donatien 1:abb442332fa8 194 DBG("\r\nParse OK :\r\nRoot Path: %s\r\nSub Path: %s\r\nMethod: %s\r\n", rootPath->c_str(), subPath->c_str(), meth->c_str());
donatien 0:632c9925f013 195
donatien 0:632c9925f013 196 return true;
donatien 0:632c9925f013 197
donatien 0:632c9925f013 198 }
donatien 0:632c9925f013 199
donatien 0:632c9925f013 200
donatien 0:632c9925f013 201 void HTTPRequestDispatcher::onTCPSocketEvent(TCPSocketEvent e)
donatien 0:632c9925f013 202 {
donatien 0:632c9925f013 203
donatien 0:632c9925f013 204 DBG("\r\nEvent %d\r\n", e);
donatien 0:632c9925f013 205
donatien 0:632c9925f013 206 if(m_closed)
donatien 0:632c9925f013 207 {
donatien 0:632c9925f013 208 DBG("\r\nWARN: Discarded\r\n");
donatien 0:632c9925f013 209 return;
donatien 0:632c9925f013 210 }
donatien 0:632c9925f013 211
donatien 0:632c9925f013 212 switch(e)
donatien 0:632c9925f013 213 {
donatien 0:632c9925f013 214 case TCPSOCKET_READABLE:
donatien 0:632c9925f013 215 m_watchdog.detach();
donatien 0:632c9925f013 216 m_pTCPSocket->resetOnEvent();
donatien 0:632c9925f013 217 //Req arrived, dispatch :
donatien 0:632c9925f013 218 dispatchRequest();
donatien 0:632c9925f013 219 break;
donatien 0:632c9925f013 220 case TCPSOCKET_CONTIMEOUT:
donatien 0:632c9925f013 221 case TCPSOCKET_CONRST:
donatien 0:632c9925f013 222 case TCPSOCKET_CONABRT:
donatien 0:632c9925f013 223 case TCPSOCKET_ERROR:
donatien 0:632c9925f013 224 case TCPSOCKET_DISCONNECTED:
donatien 0:632c9925f013 225 close();
donatien 0:632c9925f013 226 break;
donatien 0:632c9925f013 227 }
donatien 0:632c9925f013 228
donatien 0:632c9925f013 229 }