Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Thu Mar 03 08:45:49 2011 +0000
Revision:
0:8d62137f7ff4
Child:
1:b26ab2467b1a
For FRIENDs.

Who changed what in which revision?

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