EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Thu Aug 05 14:32:43 2010 +0000
Revision:
9:c79fa4034f5b
Parent:
0:632c9925f013

        

Who changed what in which revision?

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