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 "HTTPRequestDispatcher.h"
ED7418 0:51f1ef89ec7b 26 #include "HTTPRequestHandler.h"
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 HTTPRequestDispatcher::HTTPRequestDispatcher(HTTPServer* pSvr, TCPSocket* pTCPSocket) : NetService(), m_pSvr(pSvr), m_pTCPSocket(pTCPSocket), m_watchdog(), m_closed(false)
ED7418 0:51f1ef89ec7b 33 {
ED7418 0:51f1ef89ec7b 34 m_pTCPSocket->setOnEvent(this, &HTTPRequestDispatcher::onTCPSocketEvent);
ED7418 0:51f1ef89ec7b 35 m_watchdog.attach_us<HTTPRequestDispatcher>(this, &HTTPRequestDispatcher::onTimeout, HTTP_REQUEST_TIMEOUT * 1000);
ED7418 0:51f1ef89ec7b 36 }
ED7418 0:51f1ef89ec7b 37
ED7418 0:51f1ef89ec7b 38 HTTPRequestDispatcher::~HTTPRequestDispatcher()
ED7418 0:51f1ef89ec7b 39 {
ED7418 0:51f1ef89ec7b 40 close();
ED7418 0:51f1ef89ec7b 41 }
ED7418 0:51f1ef89ec7b 42
ED7418 0:51f1ef89ec7b 43 void HTTPRequestDispatcher::dispatchRequest()
ED7418 0:51f1ef89ec7b 44 {
ED7418 0:51f1ef89ec7b 45 string path;
ED7418 0:51f1ef89ec7b 46 string meth;
ED7418 0:51f1ef89ec7b 47 HTTP_METH methCode;
ED7418 0:51f1ef89ec7b 48
ED7418 0:51f1ef89ec7b 49 DBG("Dispatching req\r\n");
ED7418 0:51f1ef89ec7b 50
ED7418 0:51f1ef89ec7b 51 if( !getRequest(&path, &meth ) )
ED7418 0:51f1ef89ec7b 52 {
ED7418 0:51f1ef89ec7b 53 close();
ED7418 0:51f1ef89ec7b 54 return; //Invalid request
ED7418 0:51f1ef89ec7b 55 }
ED7418 0:51f1ef89ec7b 56
ED7418 0:51f1ef89ec7b 57 if( !meth.compare("GET") )
ED7418 0:51f1ef89ec7b 58 {
ED7418 0:51f1ef89ec7b 59 methCode = HTTP_GET;
ED7418 0:51f1ef89ec7b 60 }
ED7418 0:51f1ef89ec7b 61 else if( !meth.compare("POST") )
ED7418 0:51f1ef89ec7b 62 {
ED7418 0:51f1ef89ec7b 63 methCode = HTTP_POST;
ED7418 0:51f1ef89ec7b 64 }
ED7418 0:51f1ef89ec7b 65 else if( !meth.compare("HEAD") )
ED7418 0:51f1ef89ec7b 66 {
ED7418 0:51f1ef89ec7b 67 methCode = HTTP_HEAD;
ED7418 0:51f1ef89ec7b 68 }
ED7418 0:51f1ef89ec7b 69 else
ED7418 0:51f1ef89ec7b 70 {
ED7418 0:51f1ef89ec7b 71 close(); //Parse error
ED7418 0:51f1ef89ec7b 72 return;
ED7418 0:51f1ef89ec7b 73 }
ED7418 0:51f1ef89ec7b 74
ED7418 0:51f1ef89ec7b 75 DBG("Looking for a handler\r\n");
ED7418 0:51f1ef89ec7b 76
ED7418 0:51f1ef89ec7b 77 map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it;
ED7418 0:51f1ef89ec7b 78 // it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that
ED7418 0:51f1ef89ec7b 79 // NEW CODE START:
ED7418 0:51f1ef89ec7b 80 int root_len = 0;
ED7418 0:51f1ef89ec7b 81 for (it = m_pSvr->m_lpHandlers.begin(); it != m_pSvr->m_lpHandlers.end(); it++)
ED7418 0:51f1ef89ec7b 82 {
ED7418 0:51f1ef89ec7b 83 DBG("Checking %s...\n", (*it).first.c_str());
ED7418 0:51f1ef89ec7b 84 root_len = (*it).first.length();
ED7418 0:51f1ef89ec7b 85 if ( root_len &&
ED7418 0:51f1ef89ec7b 86 !path.compare( 0, root_len, (*it).first ) &&
ED7418 0:51f1ef89ec7b 87 (path[root_len] == '/' || path[root_len] == '\0'))
ED7418 0:51f1ef89ec7b 88 {
ED7418 0:51f1ef89ec7b 89 DBG("Found (%s)\n", (*it).first.c_str());
ED7418 0:51f1ef89ec7b 90 // Found!
ED7418 0:51f1ef89ec7b 91 break; // for
ED7418 0:51f1ef89ec7b 92 }
ED7418 0:51f1ef89ec7b 93 }
ED7418 0:51f1ef89ec7b 94 // NEW CODE END
ED7418 0:51f1ef89ec7b 95 if((it == m_pSvr->m_lpHandlers.end()) && !(m_pSvr->m_lpHandlers.empty()))
ED7418 0:51f1ef89ec7b 96 {
ED7418 0:51f1ef89ec7b 97 DBG("Using default handler\n");
ED7418 0:51f1ef89ec7b 98 it = m_pSvr->m_lpHandlers.end();
ED7418 0:51f1ef89ec7b 99 it--; //Get the last element
ED7418 0:51f1ef89ec7b 100 if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler
ED7418 0:51f1ef89ec7b 101 it = m_pSvr->m_lpHandlers.end();
ED7418 0:51f1ef89ec7b 102 root_len = 0;
ED7418 0:51f1ef89ec7b 103 }
ED7418 0:51f1ef89ec7b 104 if(it == m_pSvr->m_lpHandlers.end())
ED7418 0:51f1ef89ec7b 105 {
ED7418 0:51f1ef89ec7b 106 DBG("No handler found\n");
ED7418 0:51f1ef89ec7b 107 close(); //No handler found
ED7418 0:51f1ef89ec7b 108 return;
ED7418 0:51f1ef89ec7b 109 }
ED7418 0:51f1ef89ec7b 110
ED7418 0:51f1ef89ec7b 111 DBG("Handler found.\r\n");
ED7418 0:51f1ef89ec7b 112
ED7418 0:51f1ef89ec7b 113 //HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket);
ED7418 0:51f1ef89ec7b 114 //NEW CODE 1 LINE:
ED7418 0:51f1ef89ec7b 115 HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, m_pTCPSocket);
ED7418 0:51f1ef89ec7b 116 m_pTCPSocket = NULL; //We don't own it anymore
ED7418 0:51f1ef89ec7b 117
ED7418 0:51f1ef89ec7b 118 switch(methCode)
ED7418 0:51f1ef89ec7b 119 {
ED7418 0:51f1ef89ec7b 120 case HTTP_GET:
ED7418 0:51f1ef89ec7b 121 pHdlr->doGet();
ED7418 0:51f1ef89ec7b 122 break;
ED7418 0:51f1ef89ec7b 123 case HTTP_POST:
ED7418 0:51f1ef89ec7b 124 pHdlr->doPost();
ED7418 0:51f1ef89ec7b 125 break;
ED7418 0:51f1ef89ec7b 126 case HTTP_HEAD:
ED7418 0:51f1ef89ec7b 127 pHdlr->doHead();
ED7418 0:51f1ef89ec7b 128 break;
ED7418 0:51f1ef89ec7b 129 }
ED7418 0:51f1ef89ec7b 130
ED7418 0:51f1ef89ec7b 131 DBG("Req handled (or being handled)\r\n");
ED7418 0:51f1ef89ec7b 132 close();
ED7418 0:51f1ef89ec7b 133 }
ED7418 0:51f1ef89ec7b 134
ED7418 0:51f1ef89ec7b 135 void HTTPRequestDispatcher::close() //Close socket and destroy data
ED7418 0:51f1ef89ec7b 136 {
ED7418 0:51f1ef89ec7b 137 if(m_closed)
ED7418 0:51f1ef89ec7b 138 return;
ED7418 0:51f1ef89ec7b 139 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
ED7418 0:51f1ef89ec7b 140 m_watchdog.detach();
ED7418 0:51f1ef89ec7b 141 if(m_pTCPSocket) //m_pTCPSocket Should only be destroyed if ownership not passed to an handler
ED7418 0:51f1ef89ec7b 142 {
ED7418 0:51f1ef89ec7b 143 m_pTCPSocket->resetOnEvent();
ED7418 0:51f1ef89ec7b 144 m_pTCPSocket->close();
ED7418 0:51f1ef89ec7b 145 delete m_pTCPSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE
ED7418 0:51f1ef89ec7b 146 }
ED7418 0:51f1ef89ec7b 147 NetService::close();
ED7418 0:51f1ef89ec7b 148 }
ED7418 0:51f1ef89ec7b 149
ED7418 0:51f1ef89ec7b 150
ED7418 0:51f1ef89ec7b 151 void HTTPRequestDispatcher::onTimeout() //Connection has timed out
ED7418 0:51f1ef89ec7b 152 {
ED7418 0:51f1ef89ec7b 153 close();
ED7418 0:51f1ef89ec7b 154 }
ED7418 0:51f1ef89ec7b 155
ED7418 0:51f1ef89ec7b 156 bool HTTPRequestDispatcher::getRequest(string* path, string* meth)
ED7418 0:51f1ef89ec7b 157 {
ED7418 0:51f1ef89ec7b 158 char req[128];
ED7418 0:51f1ef89ec7b 159 char c_path[128];
ED7418 0:51f1ef89ec7b 160 char c_meth[128];
ED7418 0:51f1ef89ec7b 161 const int maxLen = 128;
ED7418 0:51f1ef89ec7b 162 char* p = req;
ED7418 0:51f1ef89ec7b 163 //Read Line
ED7418 0:51f1ef89ec7b 164 int ret;
ED7418 0:51f1ef89ec7b 165 int len = 0;
ED7418 0:51f1ef89ec7b 166 for(int i = 0; i < maxLen - 1; i++)
ED7418 0:51f1ef89ec7b 167 {
ED7418 0:51f1ef89ec7b 168 ret = m_pTCPSocket->recv(p, 1);
ED7418 0:51f1ef89ec7b 169 if(!ret)
ED7418 0:51f1ef89ec7b 170 {
ED7418 0:51f1ef89ec7b 171 break;
ED7418 0:51f1ef89ec7b 172 }
ED7418 0:51f1ef89ec7b 173 if( (len > 1) && *(p-1)=='\r' && *p=='\n' )
ED7418 0:51f1ef89ec7b 174 {
ED7418 0:51f1ef89ec7b 175 p--;
ED7418 0:51f1ef89ec7b 176 len-=2;
ED7418 0:51f1ef89ec7b 177 break;
ED7418 0:51f1ef89ec7b 178 }
ED7418 0:51f1ef89ec7b 179 else if( *p=='\n' )
ED7418 0:51f1ef89ec7b 180 {
ED7418 0:51f1ef89ec7b 181 len--;
ED7418 0:51f1ef89ec7b 182 break;
ED7418 0:51f1ef89ec7b 183 }
ED7418 0:51f1ef89ec7b 184 p++;
ED7418 0:51f1ef89ec7b 185 len++;
ED7418 0:51f1ef89ec7b 186 }
ED7418 0:51f1ef89ec7b 187 *p = 0;
ED7418 0:51f1ef89ec7b 188
ED7418 0:51f1ef89ec7b 189 DBG("Parsing request : %s\r\n", req);
ED7418 0:51f1ef89ec7b 190
ED7418 0:51f1ef89ec7b 191 ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path);
ED7418 0:51f1ef89ec7b 192 if(ret !=2)
ED7418 0:51f1ef89ec7b 193 return false;
ED7418 0:51f1ef89ec7b 194
ED7418 0:51f1ef89ec7b 195 *meth = string(c_meth);
ED7418 0:51f1ef89ec7b 196 // NEW CODE (old code removed):
ED7418 0:51f1ef89ec7b 197 *path = string(c_path);
ED7418 0:51f1ef89ec7b 198 return true;
ED7418 0:51f1ef89ec7b 199 }
ED7418 0:51f1ef89ec7b 200
ED7418 0:51f1ef89ec7b 201
ED7418 0:51f1ef89ec7b 202
ED7418 0:51f1ef89ec7b 203 void HTTPRequestDispatcher::onTCPSocketEvent(TCPSocketEvent e)
ED7418 0:51f1ef89ec7b 204 {
ED7418 0:51f1ef89ec7b 205
ED7418 0:51f1ef89ec7b 206 DBG("\r\nEvent %d\r\n", e);
ED7418 0:51f1ef89ec7b 207
ED7418 0:51f1ef89ec7b 208 if(m_closed)
ED7418 0:51f1ef89ec7b 209 {
ED7418 0:51f1ef89ec7b 210 DBG("\r\nWARN: Discarded\r\n");
ED7418 0:51f1ef89ec7b 211 return;
ED7418 0:51f1ef89ec7b 212 }
ED7418 0:51f1ef89ec7b 213
ED7418 0:51f1ef89ec7b 214 switch(e)
ED7418 0:51f1ef89ec7b 215 {
ED7418 0:51f1ef89ec7b 216 case TCPSOCKET_READABLE:
ED7418 0:51f1ef89ec7b 217 m_watchdog.detach();
ED7418 0:51f1ef89ec7b 218 m_pTCPSocket->resetOnEvent();
ED7418 0:51f1ef89ec7b 219 //Req arrived, dispatch :
ED7418 0:51f1ef89ec7b 220 dispatchRequest();
ED7418 0:51f1ef89ec7b 221 break;
ED7418 0:51f1ef89ec7b 222 case TCPSOCKET_CONTIMEOUT:
ED7418 0:51f1ef89ec7b 223 case TCPSOCKET_CONRST:
ED7418 0:51f1ef89ec7b 224 case TCPSOCKET_CONABRT:
ED7418 0:51f1ef89ec7b 225 case TCPSOCKET_ERROR:
ED7418 0:51f1ef89ec7b 226 case TCPSOCKET_DISCONNECTED:
ED7418 0:51f1ef89ec7b 227 close();
ED7418 0:51f1ef89ec7b 228 break;
ED7418 0:51f1ef89ec7b 229 }
ED7418 0:51f1ef89ec7b 230
ED7418 0:51f1ef89ec7b 231 }