Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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