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

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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