Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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