I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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