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