Used in Live Traffic Update Nokia LCD Display Project

Fork of NetServices by Segundo Equipo

Committer:
rrajan8
Date:
Wed Mar 06 19:07:23 2013 +0000
Revision:
8:92b57208ab99
Parent:
0:ac1725ba162c
This project utilizes mbed's networking features to display live traffic updates on the Nokia LCD using the MapQuest API's Traffic Web Service.

Who changed what in which revision?

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