Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

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