EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
9:c79fa4034f5b

        

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 0:632c9925f013 24 #include "HTTPRequestHandler.h"
donatien 0:632c9925f013 25
donatien 0:632c9925f013 26 #include <string.h>
donatien 0:632c9925f013 27
donatien 0:632c9925f013 28 //#define __DEBUG
donatien 0:632c9925f013 29 #include "dbg/dbg.h"
donatien 0:632c9925f013 30
donatien 0:632c9925f013 31 #define HTTP_REQUEST_TIMEOUT 5000
donatien 0:632c9925f013 32
donatien 0:632c9925f013 33 HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(),
donatien 0:632c9925f013 34 m_pTCPSocket(pTCPSocket), m_reqHeaders(), m_respHeaders(),
donatien 0:632c9925f013 35 m_rootPath(rootPath), m_path(path), m_errc(200),
donatien 0:632c9925f013 36 m_watchdog(), m_timeout(0), m_closed(false), m_headersSent(false) //OK
donatien 0:632c9925f013 37 {
donatien 0:632c9925f013 38 //Read & parse headers
donatien 0:632c9925f013 39 readHeaders();
donatien 0:632c9925f013 40 m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
donatien 0:632c9925f013 41 setTimeout(HTTP_REQUEST_TIMEOUT);
donatien 0:632c9925f013 42 }
donatien 0:632c9925f013 43
donatien 0:632c9925f013 44 HTTPRequestHandler::~HTTPRequestHandler()
donatien 0:632c9925f013 45 {
donatien 0:632c9925f013 46 close();
donatien 0:632c9925f013 47 }
donatien 0:632c9925f013 48
donatien 0:632c9925f013 49 void HTTPRequestHandler::onTimeout() //Connection has timed out
donatien 0:632c9925f013 50 {
donatien 0:632c9925f013 51 close();
donatien 0:632c9925f013 52 }
donatien 0:632c9925f013 53
donatien 0:632c9925f013 54 void HTTPRequestHandler::close() //Close socket and destroy data
donatien 0:632c9925f013 55 {
donatien 0:632c9925f013 56 if(m_closed)
donatien 0:632c9925f013 57 return;
donatien 0:632c9925f013 58 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
donatien 0:632c9925f013 59 m_watchdog.detach();
donatien 0:632c9925f013 60 onClose();
donatien 0:632c9925f013 61 m_pTCPSocket->resetOnEvent();
donatien 0:632c9925f013 62 m_pTCPSocket->close();
donatien 0:632c9925f013 63 delete m_pTCPSocket; //Can safely destroy socket
donatien 0:632c9925f013 64 NetService::close();
donatien 0:632c9925f013 65 }
donatien 0:632c9925f013 66
donatien 0:632c9925f013 67 map<string, string>& HTTPRequestHandler::reqHeaders() //const
donatien 0:632c9925f013 68 {
donatien 0:632c9925f013 69 return m_reqHeaders;
donatien 0:632c9925f013 70 }
donatien 0:632c9925f013 71
donatien 0:632c9925f013 72 string& HTTPRequestHandler::path() //const
donatien 0:632c9925f013 73 {
donatien 0:632c9925f013 74 return m_path;
donatien 0:632c9925f013 75 }
donatien 0:632c9925f013 76
donatien 0:632c9925f013 77 int HTTPRequestHandler::dataLen() const
donatien 0:632c9925f013 78 {
donatien 0:632c9925f013 79 map<string,string>::iterator it;
donatien 0:632c9925f013 80 it = m_reqHeaders.find("Content-Length");
donatien 0:632c9925f013 81 if( it == m_reqHeaders.end() )
donatien 0:632c9925f013 82 {
donatien 0:632c9925f013 83 return 0;
donatien 0:632c9925f013 84 }
donatien 0:632c9925f013 85 return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine
donatien 0:632c9925f013 86 }
donatien 0:632c9925f013 87
donatien 0:632c9925f013 88 int HTTPRequestHandler::readData(char* buf, int len)
donatien 0:632c9925f013 89 {
donatien 0:632c9925f013 90 return m_pTCPSocket->recv(buf, len);
donatien 0:632c9925f013 91 }
donatien 0:632c9925f013 92
donatien 0:632c9925f013 93 string& HTTPRequestHandler::rootPath() //const
donatien 0:632c9925f013 94 {
donatien 0:632c9925f013 95 return m_rootPath;
donatien 0:632c9925f013 96 }
donatien 0:632c9925f013 97
donatien 0:632c9925f013 98 void HTTPRequestHandler::setErrCode(int errc)
donatien 0:632c9925f013 99 {
donatien 0:632c9925f013 100 m_errc = errc;
donatien 0:632c9925f013 101 }
donatien 0:632c9925f013 102
donatien 0:632c9925f013 103 void HTTPRequestHandler::setContentLen(int len)
donatien 0:632c9925f013 104 {
donatien 0:632c9925f013 105 char len_str[6] = {0};
donatien 0:632c9925f013 106 sprintf(len_str, "%d", len);
donatien 0:632c9925f013 107 respHeaders()["Content-Length"] = len_str;
donatien 0:632c9925f013 108 }
donatien 0:632c9925f013 109
donatien 0:632c9925f013 110 map<string, string>& HTTPRequestHandler::respHeaders()
donatien 0:632c9925f013 111 {
donatien 0:632c9925f013 112 return m_respHeaders;
donatien 0:632c9925f013 113 }
donatien 0:632c9925f013 114
donatien 0:632c9925f013 115 int HTTPRequestHandler::writeData(const char* buf, int len)
donatien 0:632c9925f013 116 {
donatien 0:632c9925f013 117 if(!m_headersSent)
donatien 0:632c9925f013 118 {
donatien 0:632c9925f013 119 m_headersSent = true;
donatien 0:632c9925f013 120 writeHeaders();
donatien 0:632c9925f013 121 }
donatien 0:632c9925f013 122
donatien 0:632c9925f013 123 return m_pTCPSocket->send(buf, len);
donatien 0:632c9925f013 124 }
donatien 0:632c9925f013 125
donatien 0:632c9925f013 126 void HTTPRequestHandler::setTimeout(int ms)
donatien 0:632c9925f013 127 {
donatien 0:632c9925f013 128 m_timeout = 1000*ms;
donatien 0:632c9925f013 129 resetTimeout();
donatien 0:632c9925f013 130 }
donatien 0:632c9925f013 131
donatien 0:632c9925f013 132 void HTTPRequestHandler::resetTimeout()
donatien 0:632c9925f013 133 {
donatien 0:632c9925f013 134 m_watchdog.detach();
donatien 0:632c9925f013 135 m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout);
donatien 0:632c9925f013 136 }
donatien 0:632c9925f013 137
donatien 0:632c9925f013 138
donatien 0:632c9925f013 139 void HTTPRequestHandler::readHeaders()
donatien 0:632c9925f013 140 {
donatien 0:632c9925f013 141 static char line[128];
donatien 0:632c9925f013 142 static char key[128];
donatien 0:632c9925f013 143 static char value[128];
donatien 0:632c9925f013 144 while( readLine(line, 128) > 0) //if == 0, it is an empty line = end of headers
donatien 0:632c9925f013 145 {
donatien 0:632c9925f013 146 int n = sscanf(line, "%[^:]: %[^\n]", key, value);
donatien 0:632c9925f013 147 if ( n == 2 )
donatien 0:632c9925f013 148 {
donatien 0:632c9925f013 149 DBG("\r\nRead header : %s : %s\r\n", key, value);
donatien 0:632c9925f013 150 m_reqHeaders[key] = value;
donatien 0:632c9925f013 151 }
donatien 0:632c9925f013 152 //TODO: Impl n==1 case (part 2 of previous header)
donatien 0:632c9925f013 153 }
donatien 0:632c9925f013 154 }
donatien 0:632c9925f013 155
donatien 0:632c9925f013 156 void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
donatien 0:632c9925f013 157 {
donatien 0:632c9925f013 158 static char line[128];
donatien 0:632c9925f013 159
donatien 0:632c9925f013 160 //Response line
donatien 0:632c9925f013 161 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 162 m_pTCPSocket->send(line, strlen(line));
donatien 0:632c9925f013 163
donatien 0:632c9925f013 164 map<string,string>::iterator it;
donatien 0:632c9925f013 165 while( !m_respHeaders.empty() )
donatien 0:632c9925f013 166 {
donatien 0:632c9925f013 167 it = m_respHeaders.begin();
donatien 0:632c9925f013 168 sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
donatien 0:632c9925f013 169 DBG("\r\n%s", line);
donatien 0:632c9925f013 170 m_pTCPSocket->send(line, strlen(line));
donatien 0:632c9925f013 171 m_respHeaders.erase(it);
donatien 0:632c9925f013 172 }
donatien 0:632c9925f013 173 m_pTCPSocket->send("\r\n",2); //End of head
donatien 0:632c9925f013 174 }
donatien 0:632c9925f013 175
donatien 0:632c9925f013 176 int HTTPRequestHandler::readLine(char* str, int maxLen)
donatien 0:632c9925f013 177 {
donatien 0:632c9925f013 178 int ret;
donatien 0:632c9925f013 179 int len = 0;
donatien 0:632c9925f013 180 for(int i = 0; i < maxLen - 1; i++)
donatien 0:632c9925f013 181 {
donatien 0:632c9925f013 182 ret = m_pTCPSocket->recv(str, 1);
donatien 0:632c9925f013 183 if(!ret)
donatien 0:632c9925f013 184 {
donatien 0:632c9925f013 185 break;
donatien 0:632c9925f013 186 }
donatien 0:632c9925f013 187 if( (len > 1) && *(str-1)=='\r' && *str=='\n' )
donatien 0:632c9925f013 188 {
donatien 0:632c9925f013 189 str--;
donatien 0:632c9925f013 190 len-=2;
donatien 0:632c9925f013 191 break;
donatien 0:632c9925f013 192 }
donatien 0:632c9925f013 193 else if( *str=='\n' )
donatien 0:632c9925f013 194 {
donatien 0:632c9925f013 195 len--;
donatien 0:632c9925f013 196 break;
donatien 0:632c9925f013 197 }
donatien 0:632c9925f013 198 str++;
donatien 0:632c9925f013 199 len++;
donatien 0:632c9925f013 200 }
donatien 0:632c9925f013 201 *str = 0;
donatien 0:632c9925f013 202 return len;
donatien 0:632c9925f013 203 }
donatien 0:632c9925f013 204
donatien 0:632c9925f013 205 void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e)
donatien 0:632c9925f013 206 {
donatien 0:632c9925f013 207
donatien 0:632c9925f013 208 DBG("\r\nEvent %d in HTTPRequestHandler\r\n", e);
donatien 0:632c9925f013 209
donatien 0:632c9925f013 210 if(m_closed)
donatien 0:632c9925f013 211 {
donatien 0:632c9925f013 212 DBG("\r\nWARN: Discarded\r\n");
donatien 0:632c9925f013 213 return;
donatien 0:632c9925f013 214 }
donatien 0:632c9925f013 215
donatien 0:632c9925f013 216 switch(e)
donatien 0:632c9925f013 217 {
donatien 0:632c9925f013 218 case TCPSOCKET_READABLE:
donatien 0:632c9925f013 219 resetTimeout();
donatien 0:632c9925f013 220 onReadable();
donatien 0:632c9925f013 221 break;
donatien 0:632c9925f013 222 case TCPSOCKET_WRITEABLE:
donatien 0:632c9925f013 223 resetTimeout();
donatien 0:632c9925f013 224 onWriteable();
donatien 0:632c9925f013 225 break;
donatien 0:632c9925f013 226 case TCPSOCKET_CONTIMEOUT:
donatien 0:632c9925f013 227 case TCPSOCKET_CONRST:
donatien 0:632c9925f013 228 case TCPSOCKET_CONABRT:
donatien 0:632c9925f013 229 case TCPSOCKET_ERROR:
donatien 0:632c9925f013 230 case TCPSOCKET_DISCONNECTED:
donatien 0:632c9925f013 231 DBG("\r\nConnection error in handler\r\n");
donatien 0:632c9925f013 232 close();
donatien 0:632c9925f013 233 break;
donatien 0:632c9925f013 234 }
donatien 0:632c9925f013 235
donatien 0:632c9925f013 236 }