Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1
simon 0:350011bf8be7 2 /*
simon 0:350011bf8be7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
simon 0:350011bf8be7 4
simon 0:350011bf8be7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:350011bf8be7 6 of this software and associated documentation files (the "Software"), to deal
simon 0:350011bf8be7 7 in the Software without restriction, including without limitation the rights
simon 0:350011bf8be7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:350011bf8be7 9 copies of the Software, and to permit persons to whom the Software is
simon 0:350011bf8be7 10 furnished to do so, subject to the following conditions:
simon 0:350011bf8be7 11
simon 0:350011bf8be7 12 The above copyright notice and this permission notice shall be included in
simon 0:350011bf8be7 13 all copies or substantial portions of the Software.
simon 0:350011bf8be7 14
simon 0:350011bf8be7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:350011bf8be7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:350011bf8be7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:350011bf8be7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:350011bf8be7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:350011bf8be7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:350011bf8be7 21 THE SOFTWARE.
simon 0:350011bf8be7 22 */
simon 0:350011bf8be7 23
simon 0:350011bf8be7 24 #include "core/netservice.h"
simon 0:350011bf8be7 25 #include "HTTPRequestHandler.h"
simon 0:350011bf8be7 26
simon 0:350011bf8be7 27 #include <string.h>
simon 0:350011bf8be7 28
simon 0:350011bf8be7 29 //#define __DEBUG
simon 0:350011bf8be7 30 #include "dbg/dbg.h"
simon 0:350011bf8be7 31
simon 0:350011bf8be7 32 #define HTTP_REQUEST_TIMEOUT 5000
simon 0:350011bf8be7 33
simon 0:350011bf8be7 34 HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(),
simon 0:350011bf8be7 35 m_pTCPSocket(pTCPSocket), m_reqHeaders(), m_respHeaders(),
simon 0:350011bf8be7 36 m_rootPath(rootPath), m_path(path), m_errc(200),
simon 0:350011bf8be7 37 m_watchdog(), m_timeout(0), m_closed(false), m_headersSent(false) //OK
simon 0:350011bf8be7 38 {
simon 0:350011bf8be7 39 //Read & parse headers
simon 0:350011bf8be7 40 readHeaders();
simon 0:350011bf8be7 41 m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
simon 0:350011bf8be7 42 setTimeout(HTTP_REQUEST_TIMEOUT);
simon 0:350011bf8be7 43 }
simon 0:350011bf8be7 44
simon 0:350011bf8be7 45 HTTPRequestHandler::~HTTPRequestHandler()
simon 0:350011bf8be7 46 {
simon 0:350011bf8be7 47 close();
simon 0:350011bf8be7 48 }
simon 0:350011bf8be7 49
simon 0:350011bf8be7 50 void HTTPRequestHandler::onTimeout() //Connection has timed out
simon 0:350011bf8be7 51 {
simon 0:350011bf8be7 52 close();
simon 0:350011bf8be7 53 }
simon 0:350011bf8be7 54
simon 0:350011bf8be7 55 void HTTPRequestHandler::close() //Close socket and destroy data
simon 0:350011bf8be7 56 {
simon 0:350011bf8be7 57 if(m_closed)
simon 0:350011bf8be7 58 return;
simon 0:350011bf8be7 59 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
simon 0:350011bf8be7 60 m_watchdog.detach();
simon 0:350011bf8be7 61 onClose();
simon 0:350011bf8be7 62 m_pTCPSocket->resetOnEvent();
simon 0:350011bf8be7 63 m_pTCPSocket->close();
simon 0:350011bf8be7 64 delete m_pTCPSocket; //Can safely destroy socket
simon 0:350011bf8be7 65 NetService::close();
simon 0:350011bf8be7 66 }
simon 0:350011bf8be7 67
simon 0:350011bf8be7 68 map<string, string>& HTTPRequestHandler::reqHeaders() //const
simon 0:350011bf8be7 69 {
simon 0:350011bf8be7 70 return m_reqHeaders;
simon 0:350011bf8be7 71 }
simon 0:350011bf8be7 72
simon 0:350011bf8be7 73 string& HTTPRequestHandler::path() //const
simon 0:350011bf8be7 74 {
simon 0:350011bf8be7 75 return m_path;
simon 0:350011bf8be7 76 }
simon 0:350011bf8be7 77
simon 0:350011bf8be7 78 int HTTPRequestHandler::dataLen() const
simon 0:350011bf8be7 79 {
simon 0:350011bf8be7 80 map<string,string>::iterator it;
simon 0:350011bf8be7 81 it = m_reqHeaders.find("Content-Length");
simon 0:350011bf8be7 82 if( it == m_reqHeaders.end() )
simon 0:350011bf8be7 83 {
simon 0:350011bf8be7 84 return 0;
simon 0:350011bf8be7 85 }
simon 0:350011bf8be7 86 return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine
simon 0:350011bf8be7 87 }
simon 0:350011bf8be7 88
simon 0:350011bf8be7 89 int HTTPRequestHandler::readData(char* buf, int len)
simon 0:350011bf8be7 90 {
simon 0:350011bf8be7 91 return m_pTCPSocket->recv(buf, len);
simon 0:350011bf8be7 92 }
simon 0:350011bf8be7 93
simon 0:350011bf8be7 94 string& HTTPRequestHandler::rootPath() //const
simon 0:350011bf8be7 95 {
simon 0:350011bf8be7 96 return m_rootPath;
simon 0:350011bf8be7 97 }
simon 0:350011bf8be7 98
simon 0:350011bf8be7 99 void HTTPRequestHandler::setErrCode(int errc)
simon 0:350011bf8be7 100 {
simon 0:350011bf8be7 101 m_errc = errc;
simon 0:350011bf8be7 102 }
simon 0:350011bf8be7 103
simon 0:350011bf8be7 104 void HTTPRequestHandler::setContentLen(int len)
simon 0:350011bf8be7 105 {
simon 0:350011bf8be7 106 char len_str[6] = {0};
simon 0:350011bf8be7 107 sprintf(len_str, "%d", len);
simon 0:350011bf8be7 108 respHeaders()["Content-Length"] = len_str;
simon 0:350011bf8be7 109 }
simon 0:350011bf8be7 110
simon 0:350011bf8be7 111 map<string, string>& HTTPRequestHandler::respHeaders()
simon 0:350011bf8be7 112 {
simon 0:350011bf8be7 113 return m_respHeaders;
simon 0:350011bf8be7 114 }
simon 0:350011bf8be7 115
simon 0:350011bf8be7 116 int HTTPRequestHandler::writeData(const char* buf, int len)
simon 0:350011bf8be7 117 {
simon 0:350011bf8be7 118 if(!m_headersSent)
simon 0:350011bf8be7 119 {
simon 0:350011bf8be7 120 m_headersSent = true;
simon 0:350011bf8be7 121 writeHeaders();
simon 0:350011bf8be7 122 }
simon 0:350011bf8be7 123
simon 0:350011bf8be7 124 return m_pTCPSocket->send(buf, len);
simon 0:350011bf8be7 125 }
simon 0:350011bf8be7 126
simon 0:350011bf8be7 127 void HTTPRequestHandler::setTimeout(int ms)
simon 0:350011bf8be7 128 {
simon 0:350011bf8be7 129 m_timeout = 1000*ms;
simon 0:350011bf8be7 130 resetTimeout();
simon 0:350011bf8be7 131 }
simon 0:350011bf8be7 132
simon 0:350011bf8be7 133 void HTTPRequestHandler::resetTimeout()
simon 0:350011bf8be7 134 {
simon 0:350011bf8be7 135 m_watchdog.detach();
simon 0:350011bf8be7 136 m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout);
simon 0:350011bf8be7 137 }
simon 0:350011bf8be7 138
simon 0:350011bf8be7 139
simon 0:350011bf8be7 140 void HTTPRequestHandler::readHeaders()
simon 0:350011bf8be7 141 {
simon 0:350011bf8be7 142 static char line[128];
simon 0:350011bf8be7 143 static char key[128];
simon 0:350011bf8be7 144 static char value[128];
simon 0:350011bf8be7 145 while( readLine(line, 128) > 0) //if == 0, it is an empty line = end of headers
simon 0:350011bf8be7 146 {
simon 0:350011bf8be7 147 int n = sscanf(line, "%[^:]: %[^\n]", key, value);
simon 0:350011bf8be7 148 if ( n == 2 )
simon 0:350011bf8be7 149 {
simon 0:350011bf8be7 150 DBG("\r\nRead header : %s : %s\r\n", key, value);
simon 0:350011bf8be7 151 m_reqHeaders[key] = value;
simon 0:350011bf8be7 152 }
simon 0:350011bf8be7 153 //TODO: Impl n==1 case (part 2 of previous header)
simon 0:350011bf8be7 154 }
simon 0:350011bf8be7 155 }
simon 0:350011bf8be7 156
simon 0:350011bf8be7 157 void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
simon 0:350011bf8be7 158 {
simon 0:350011bf8be7 159 static char line[128];
simon 0:350011bf8be7 160
simon 0:350011bf8be7 161 //Response line
simon 0:350011bf8be7 162 sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
simon 0:350011bf8be7 163 m_pTCPSocket->send(line, strlen(line));
simon 0:350011bf8be7 164
simon 0:350011bf8be7 165 map<string,string>::iterator it;
simon 0:350011bf8be7 166 while( !m_respHeaders.empty() )
simon 0:350011bf8be7 167 {
simon 0:350011bf8be7 168 it = m_respHeaders.begin();
simon 0:350011bf8be7 169 sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
simon 0:350011bf8be7 170 DBG("\r\n%s", line);
simon 0:350011bf8be7 171 m_pTCPSocket->send(line, strlen(line));
simon 0:350011bf8be7 172 m_respHeaders.erase(it);
simon 0:350011bf8be7 173 }
simon 0:350011bf8be7 174 m_pTCPSocket->send("\r\n",2); //End of head
simon 0:350011bf8be7 175 }
simon 0:350011bf8be7 176
simon 0:350011bf8be7 177 int HTTPRequestHandler::readLine(char* str, int maxLen)
simon 0:350011bf8be7 178 {
simon 0:350011bf8be7 179 int ret;
simon 0:350011bf8be7 180 int len = 0;
simon 0:350011bf8be7 181 for(int i = 0; i < maxLen - 1; i++)
simon 0:350011bf8be7 182 {
simon 0:350011bf8be7 183 ret = m_pTCPSocket->recv(str, 1);
simon 0:350011bf8be7 184 if(!ret)
simon 0:350011bf8be7 185 {
simon 0:350011bf8be7 186 break;
simon 0:350011bf8be7 187 }
simon 0:350011bf8be7 188 if( (len > 1) && *(str-1)=='\r' && *str=='\n' )
simon 0:350011bf8be7 189 {
simon 0:350011bf8be7 190 str--;
simon 0:350011bf8be7 191 len-=2;
simon 0:350011bf8be7 192 break;
simon 0:350011bf8be7 193 }
simon 0:350011bf8be7 194 else if( *str=='\n' )
simon 0:350011bf8be7 195 {
simon 0:350011bf8be7 196 len--;
simon 0:350011bf8be7 197 break;
simon 0:350011bf8be7 198 }
simon 0:350011bf8be7 199 str++;
simon 0:350011bf8be7 200 len++;
simon 0:350011bf8be7 201 }
simon 0:350011bf8be7 202 *str = 0;
simon 0:350011bf8be7 203 return len;
simon 0:350011bf8be7 204 }
simon 0:350011bf8be7 205
simon 0:350011bf8be7 206 void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e)
simon 0:350011bf8be7 207 {
simon 0:350011bf8be7 208
simon 0:350011bf8be7 209 DBG("\r\nEvent %d in HTTPRequestHandler\r\n", e);
simon 0:350011bf8be7 210
simon 0:350011bf8be7 211 if(m_closed)
simon 0:350011bf8be7 212 {
simon 0:350011bf8be7 213 DBG("\r\nWARN: Discarded\r\n");
simon 0:350011bf8be7 214 return;
simon 0:350011bf8be7 215 }
simon 0:350011bf8be7 216
simon 0:350011bf8be7 217 switch(e)
simon 0:350011bf8be7 218 {
simon 0:350011bf8be7 219 case TCPSOCKET_READABLE:
simon 0:350011bf8be7 220 resetTimeout();
simon 0:350011bf8be7 221 onReadable();
simon 0:350011bf8be7 222 break;
simon 0:350011bf8be7 223 case TCPSOCKET_WRITEABLE:
simon 0:350011bf8be7 224 resetTimeout();
simon 0:350011bf8be7 225 onWriteable();
simon 0:350011bf8be7 226 break;
simon 0:350011bf8be7 227 case TCPSOCKET_CONTIMEOUT:
simon 0:350011bf8be7 228 case TCPSOCKET_CONRST:
simon 0:350011bf8be7 229 case TCPSOCKET_CONABRT:
simon 0:350011bf8be7 230 case TCPSOCKET_ERROR:
simon 0:350011bf8be7 231 case TCPSOCKET_DISCONNECTED:
simon 0:350011bf8be7 232 DBG("\r\nConnection error in handler\r\n");
simon 0:350011bf8be7 233 close();
simon 0:350011bf8be7 234 break;
simon 0:350011bf8be7 235 }
simon 0:350011bf8be7 236
simon 0:350011bf8be7 237 }