Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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