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 "HTTPRequestHandler.h"
tax 0:66300c77c6e9 26
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 #define HTTP_REQUEST_TIMEOUT 5000
tax 0:66300c77c6e9 33
tax 0:66300c77c6e9 34 HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(),
tax 0:66300c77c6e9 35 m_pTCPSocket(pTCPSocket), m_reqHeaders(), m_respHeaders(),
tax 0:66300c77c6e9 36 m_rootPath(rootPath), m_path(path), m_errc(200),
tax 0:66300c77c6e9 37 m_watchdog(), m_timeout(0), m_closed(false), m_headersSent(false) //OK
tax 0:66300c77c6e9 38 {
tax 0:66300c77c6e9 39 //Read & parse headers
tax 0:66300c77c6e9 40 readHeaders();
tax 0:66300c77c6e9 41 m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
tax 0:66300c77c6e9 42 setTimeout(HTTP_REQUEST_TIMEOUT);
tax 0:66300c77c6e9 43 }
tax 0:66300c77c6e9 44
tax 0:66300c77c6e9 45 HTTPRequestHandler::~HTTPRequestHandler()
tax 0:66300c77c6e9 46 {
tax 0:66300c77c6e9 47 close();
tax 0:66300c77c6e9 48 }
tax 0:66300c77c6e9 49
tax 0:66300c77c6e9 50 void HTTPRequestHandler::onTimeout() //Connection has timed out
tax 0:66300c77c6e9 51 {
tax 0:66300c77c6e9 52 close();
tax 0:66300c77c6e9 53 }
tax 0:66300c77c6e9 54
tax 0:66300c77c6e9 55 void HTTPRequestHandler::close() //Close socket and destroy data
tax 0:66300c77c6e9 56 {
tax 0:66300c77c6e9 57 if(m_closed)
tax 0:66300c77c6e9 58 return;
tax 0:66300c77c6e9 59 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
tax 0:66300c77c6e9 60 m_watchdog.detach();
tax 0:66300c77c6e9 61 onClose();
tax 0:66300c77c6e9 62 m_pTCPSocket->resetOnEvent();
tax 0:66300c77c6e9 63 m_pTCPSocket->close();
tax 0:66300c77c6e9 64 delete m_pTCPSocket; //Can safely destroy socket
tax 0:66300c77c6e9 65 NetService::close();
tax 0:66300c77c6e9 66 }
tax 0:66300c77c6e9 67
tax 0:66300c77c6e9 68 map<string, string>& HTTPRequestHandler::reqHeaders() //const
tax 0:66300c77c6e9 69 {
tax 0:66300c77c6e9 70 return m_reqHeaders;
tax 0:66300c77c6e9 71 }
tax 0:66300c77c6e9 72
tax 0:66300c77c6e9 73 string& HTTPRequestHandler::path() //const
tax 0:66300c77c6e9 74 {
tax 0:66300c77c6e9 75 return m_path;
tax 0:66300c77c6e9 76 }
tax 0:66300c77c6e9 77
tax 0:66300c77c6e9 78 int HTTPRequestHandler::dataLen() const
tax 0:66300c77c6e9 79 {
tax 0:66300c77c6e9 80 map<string,string>::iterator it;
tax 0:66300c77c6e9 81 it = m_reqHeaders.find("Content-Length");
tax 0:66300c77c6e9 82 if( it == m_reqHeaders.end() )
tax 0:66300c77c6e9 83 {
tax 0:66300c77c6e9 84 return 0;
tax 0:66300c77c6e9 85 }
tax 0:66300c77c6e9 86 return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine
tax 0:66300c77c6e9 87 }
tax 0:66300c77c6e9 88
tax 0:66300c77c6e9 89 int HTTPRequestHandler::readData(char* buf, int len)
tax 0:66300c77c6e9 90 {
tax 0:66300c77c6e9 91 return m_pTCPSocket->recv(buf, len);
tax 0:66300c77c6e9 92 }
tax 0:66300c77c6e9 93
tax 0:66300c77c6e9 94 string& HTTPRequestHandler::rootPath() //const
tax 0:66300c77c6e9 95 {
tax 0:66300c77c6e9 96 return m_rootPath;
tax 0:66300c77c6e9 97 }
tax 0:66300c77c6e9 98
tax 0:66300c77c6e9 99 void HTTPRequestHandler::setErrCode(int errc)
tax 0:66300c77c6e9 100 {
tax 0:66300c77c6e9 101 m_errc = errc;
tax 0:66300c77c6e9 102 }
tax 0:66300c77c6e9 103
tax 0:66300c77c6e9 104 void HTTPRequestHandler::setContentLen(int len)
tax 0:66300c77c6e9 105 {
tax 0:66300c77c6e9 106 char len_str[6] = {0};
tax 0:66300c77c6e9 107 sprintf(len_str, "%d", len);
tax 0:66300c77c6e9 108 respHeaders()["Content-Length"] = len_str;
tax 0:66300c77c6e9 109 }
tax 0:66300c77c6e9 110
tax 0:66300c77c6e9 111 map<string, string>& HTTPRequestHandler::respHeaders()
tax 0:66300c77c6e9 112 {
tax 0:66300c77c6e9 113 return m_respHeaders;
tax 0:66300c77c6e9 114 }
tax 0:66300c77c6e9 115
tax 0:66300c77c6e9 116 int HTTPRequestHandler::writeData(const char* buf, int len)
tax 0:66300c77c6e9 117 {
tax 0:66300c77c6e9 118 if(!m_headersSent)
tax 0:66300c77c6e9 119 {
tax 0:66300c77c6e9 120 m_headersSent = true;
tax 0:66300c77c6e9 121 writeHeaders();
tax 0:66300c77c6e9 122 }
tax 0:66300c77c6e9 123
tax 0:66300c77c6e9 124 return m_pTCPSocket->send(buf, len);
tax 0:66300c77c6e9 125 }
tax 0:66300c77c6e9 126
tax 0:66300c77c6e9 127 void HTTPRequestHandler::setTimeout(int ms)
tax 0:66300c77c6e9 128 {
tax 0:66300c77c6e9 129 m_timeout = 1000*ms;
tax 0:66300c77c6e9 130 resetTimeout();
tax 0:66300c77c6e9 131 }
tax 0:66300c77c6e9 132
tax 0:66300c77c6e9 133 void HTTPRequestHandler::resetTimeout()
tax 0:66300c77c6e9 134 {
tax 0:66300c77c6e9 135 m_watchdog.detach();
tax 0:66300c77c6e9 136 m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout);
tax 0:66300c77c6e9 137 }
tax 0:66300c77c6e9 138
tax 0:66300c77c6e9 139
tax 0:66300c77c6e9 140 void HTTPRequestHandler::readHeaders()
tax 0:66300c77c6e9 141 {
tax 0:66300c77c6e9 142 static char line[128];
tax 0:66300c77c6e9 143 static char key[128];
tax 0:66300c77c6e9 144 static char value[128];
tax 0:66300c77c6e9 145 while( readLine(line, 128) > 0) //if == 0, it is an empty line = end of headers
tax 0:66300c77c6e9 146 {
tax 0:66300c77c6e9 147 int n = sscanf(line, "%[^:]: %[^\n]", key, value);
tax 0:66300c77c6e9 148 if ( n == 2 )
tax 0:66300c77c6e9 149 {
tax 0:66300c77c6e9 150 DBG("\r\nRead header : %s : %s\r\n", key, value);
tax 0:66300c77c6e9 151 m_reqHeaders[key] = value;
tax 0:66300c77c6e9 152 }
tax 0:66300c77c6e9 153 //TODO: Impl n==1 case (part 2 of previous header)
tax 0:66300c77c6e9 154 }
tax 0:66300c77c6e9 155 }
tax 0:66300c77c6e9 156
tax 0:66300c77c6e9 157 void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
tax 0:66300c77c6e9 158 {
tax 0:66300c77c6e9 159 static char line[128];
tax 0:66300c77c6e9 160
tax 0:66300c77c6e9 161 //Response line
tax 0:66300c77c6e9 162 sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
tax 0:66300c77c6e9 163 m_pTCPSocket->send(line, strlen(line));
tax 0:66300c77c6e9 164
tax 0:66300c77c6e9 165 map<string,string>::iterator it;
tax 0:66300c77c6e9 166 while( !m_respHeaders.empty() )
tax 0:66300c77c6e9 167 {
tax 0:66300c77c6e9 168 it = m_respHeaders.begin();
tax 0:66300c77c6e9 169 sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
tax 0:66300c77c6e9 170 DBG("\r\n%s", line);
tax 0:66300c77c6e9 171 m_pTCPSocket->send(line, strlen(line));
tax 0:66300c77c6e9 172 m_respHeaders.erase(it);
tax 0:66300c77c6e9 173 }
tax 0:66300c77c6e9 174 m_pTCPSocket->send("\r\n",2); //End of head
tax 0:66300c77c6e9 175 }
tax 0:66300c77c6e9 176
tax 0:66300c77c6e9 177 int HTTPRequestHandler::readLine(char* str, int maxLen)
tax 0:66300c77c6e9 178 {
tax 0:66300c77c6e9 179 int ret;
tax 0:66300c77c6e9 180 int len = 0;
tax 0:66300c77c6e9 181 for(int i = 0; i < maxLen - 1; i++)
tax 0:66300c77c6e9 182 {
tax 0:66300c77c6e9 183 ret = m_pTCPSocket->recv(str, 1);
tax 0:66300c77c6e9 184 if(!ret)
tax 0:66300c77c6e9 185 {
tax 0:66300c77c6e9 186 break;
tax 0:66300c77c6e9 187 }
tax 0:66300c77c6e9 188 if( (len > 1) && *(str-1)=='\r' && *str=='\n' )
tax 0:66300c77c6e9 189 {
tax 0:66300c77c6e9 190 str--;
tax 0:66300c77c6e9 191 len-=2;
tax 0:66300c77c6e9 192 break;
tax 0:66300c77c6e9 193 }
tax 0:66300c77c6e9 194 else if( *str=='\n' )
tax 0:66300c77c6e9 195 {
tax 0:66300c77c6e9 196 len--;
tax 0:66300c77c6e9 197 break;
tax 0:66300c77c6e9 198 }
tax 0:66300c77c6e9 199 str++;
tax 0:66300c77c6e9 200 len++;
tax 0:66300c77c6e9 201 }
tax 0:66300c77c6e9 202 *str = 0;
tax 0:66300c77c6e9 203 return len;
tax 0:66300c77c6e9 204 }
tax 0:66300c77c6e9 205
tax 0:66300c77c6e9 206 void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e)
tax 0:66300c77c6e9 207 {
tax 0:66300c77c6e9 208
tax 0:66300c77c6e9 209 DBG("\r\nEvent %d in HTTPRequestHandler\r\n", e);
tax 0:66300c77c6e9 210
tax 0:66300c77c6e9 211 if(m_closed)
tax 0:66300c77c6e9 212 {
tax 0:66300c77c6e9 213 DBG("\r\nWARN: Discarded\r\n");
tax 0:66300c77c6e9 214 return;
tax 0:66300c77c6e9 215 }
tax 0:66300c77c6e9 216
tax 0:66300c77c6e9 217 switch(e)
tax 0:66300c77c6e9 218 {
tax 0:66300c77c6e9 219 case TCPSOCKET_READABLE:
tax 0:66300c77c6e9 220 resetTimeout();
tax 0:66300c77c6e9 221 onReadable();
tax 0:66300c77c6e9 222 break;
tax 0:66300c77c6e9 223 case TCPSOCKET_WRITEABLE:
tax 0:66300c77c6e9 224 resetTimeout();
tax 0:66300c77c6e9 225 onWriteable();
tax 0:66300c77c6e9 226 break;
tax 0:66300c77c6e9 227 case TCPSOCKET_CONTIMEOUT:
tax 0:66300c77c6e9 228 case TCPSOCKET_CONRST:
tax 0:66300c77c6e9 229 case TCPSOCKET_CONABRT:
tax 0:66300c77c6e9 230 case TCPSOCKET_ERROR:
tax 0:66300c77c6e9 231 case TCPSOCKET_DISCONNECTED:
tax 0:66300c77c6e9 232 DBG("\r\nConnection error in handler\r\n");
tax 0:66300c77c6e9 233 close();
tax 0:66300c77c6e9 234 break;
tax 0:66300c77c6e9 235 }
tax 0:66300c77c6e9 236
tax 0:66300c77c6e9 237 }