WIZ820io(W5200) network interface、EthernetNetIf compatible.
example
#include "WIZ820ioNetIf.h" #include "HTTPClient.h" #include "HTTPServer.h" #if defined(TARGET_KL25Z) WIZ820ioNetIf eth(PTD2,PTD3,PTD1,PTD0,PTD5); #endif HTTPClient http; HTTPStream stream; void callback(HTTPResult r){ printf("callback %d %s\n", r, HTTPClient::ResultStr(r)); } int main() { int err = eth.setup(); if (err < 0) { printf("setup error %d\n", err); exit(-1); } HTTPServer svr; svr.addHandler<SimpleHandler>("/"); svr.bind(80); const char* uri = "http://va009039-mbed.appspot.com/kl25z/"; http.get(uri, &stream, callback); uint8_t buf[256]; int total = 0; stream.readNext(buf, sizeof(buf)); while(1) { if(stream.readable()) { int len = stream.readLen(); total += len; printf("%d %d\n", total, len); stream.readNext(buf, sizeof(buf)); } Net::poll(); } }
NetServices/services/http/server/HTTPRequestDispatcher.cpp@1:22b9052d864d, 2013-03-24 (annotated)
- Committer:
- va009039
- Date:
- Sun Mar 24 11:25:31 2013 +0000
- Revision:
- 1:22b9052d864d
WIZ820io(W8200) ethernet interface, EthernetNetIf compatible.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 1:22b9052d864d | 1 | |
va009039 | 1:22b9052d864d | 2 | /* |
va009039 | 1:22b9052d864d | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
va009039 | 1:22b9052d864d | 4 | |
va009039 | 1:22b9052d864d | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
va009039 | 1:22b9052d864d | 6 | of this software and associated documentation files (the "Software"), to deal |
va009039 | 1:22b9052d864d | 7 | in the Software without restriction, including without limitation the rights |
va009039 | 1:22b9052d864d | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
va009039 | 1:22b9052d864d | 9 | copies of the Software, and to permit persons to whom the Software is |
va009039 | 1:22b9052d864d | 10 | furnished to do so, subject to the following conditions: |
va009039 | 1:22b9052d864d | 11 | |
va009039 | 1:22b9052d864d | 12 | The above copyright notice and this permission notice shall be included in |
va009039 | 1:22b9052d864d | 13 | all copies or substantial portions of the Software. |
va009039 | 1:22b9052d864d | 14 | |
va009039 | 1:22b9052d864d | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
va009039 | 1:22b9052d864d | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
va009039 | 1:22b9052d864d | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
va009039 | 1:22b9052d864d | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
va009039 | 1:22b9052d864d | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 | 1:22b9052d864d | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
va009039 | 1:22b9052d864d | 21 | THE SOFTWARE. |
va009039 | 1:22b9052d864d | 22 | */ |
va009039 | 1:22b9052d864d | 23 | |
va009039 | 1:22b9052d864d | 24 | #include "core/netservice.h" |
va009039 | 1:22b9052d864d | 25 | #include "HTTPRequestDispatcher.h" |
va009039 | 1:22b9052d864d | 26 | #include "HTTPRequestHandler.h" |
va009039 | 1:22b9052d864d | 27 | #include <string.h> |
va009039 | 1:22b9052d864d | 28 | |
va009039 | 1:22b9052d864d | 29 | //#define __DEBUG |
va009039 | 1:22b9052d864d | 30 | #include "dbg/dbg.h" |
va009039 | 1:22b9052d864d | 31 | |
va009039 | 1:22b9052d864d | 32 | HTTPRequestDispatcher::HTTPRequestDispatcher(HTTPServer* pSvr, TCPSocket* pTCPSocket) : NetService(), m_pSvr(pSvr), m_pTCPSocket(pTCPSocket), m_watchdog(), m_closed(false) |
va009039 | 1:22b9052d864d | 33 | { |
va009039 | 1:22b9052d864d | 34 | m_pTCPSocket->setOnEvent(this, &HTTPRequestDispatcher::onTCPSocketEvent); |
va009039 | 1:22b9052d864d | 35 | m_watchdog.attach_us<HTTPRequestDispatcher>(this, &HTTPRequestDispatcher::onTimeout, HTTP_REQUEST_TIMEOUT * 1000); |
va009039 | 1:22b9052d864d | 36 | } |
va009039 | 1:22b9052d864d | 37 | |
va009039 | 1:22b9052d864d | 38 | HTTPRequestDispatcher::~HTTPRequestDispatcher() |
va009039 | 1:22b9052d864d | 39 | { |
va009039 | 1:22b9052d864d | 40 | close(); |
va009039 | 1:22b9052d864d | 41 | } |
va009039 | 1:22b9052d864d | 42 | |
va009039 | 1:22b9052d864d | 43 | void HTTPRequestDispatcher::dispatchRequest() |
va009039 | 1:22b9052d864d | 44 | { |
va009039 | 1:22b9052d864d | 45 | string path; |
va009039 | 1:22b9052d864d | 46 | string meth; |
va009039 | 1:22b9052d864d | 47 | HTTP_METH methCode; |
va009039 | 1:22b9052d864d | 48 | |
va009039 | 1:22b9052d864d | 49 | DBG("Dispatching req\r\n"); |
va009039 | 1:22b9052d864d | 50 | |
va009039 | 1:22b9052d864d | 51 | if( !getRequest(&path, &meth ) ) |
va009039 | 1:22b9052d864d | 52 | { |
va009039 | 1:22b9052d864d | 53 | close(); |
va009039 | 1:22b9052d864d | 54 | return; //Invalid request |
va009039 | 1:22b9052d864d | 55 | } |
va009039 | 1:22b9052d864d | 56 | |
va009039 | 1:22b9052d864d | 57 | if( !meth.compare("GET") ) |
va009039 | 1:22b9052d864d | 58 | { |
va009039 | 1:22b9052d864d | 59 | methCode = HTTP_GET; |
va009039 | 1:22b9052d864d | 60 | } |
va009039 | 1:22b9052d864d | 61 | else if( !meth.compare("POST") ) |
va009039 | 1:22b9052d864d | 62 | { |
va009039 | 1:22b9052d864d | 63 | methCode = HTTP_POST; |
va009039 | 1:22b9052d864d | 64 | } |
va009039 | 1:22b9052d864d | 65 | else if( !meth.compare("HEAD") ) |
va009039 | 1:22b9052d864d | 66 | { |
va009039 | 1:22b9052d864d | 67 | methCode = HTTP_HEAD; |
va009039 | 1:22b9052d864d | 68 | } |
va009039 | 1:22b9052d864d | 69 | else |
va009039 | 1:22b9052d864d | 70 | { |
va009039 | 1:22b9052d864d | 71 | close(); //Parse error |
va009039 | 1:22b9052d864d | 72 | return; |
va009039 | 1:22b9052d864d | 73 | } |
va009039 | 1:22b9052d864d | 74 | |
va009039 | 1:22b9052d864d | 75 | DBG("Looking for a handler\r\n"); |
va009039 | 1:22b9052d864d | 76 | |
va009039 | 1:22b9052d864d | 77 | map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it; |
va009039 | 1:22b9052d864d | 78 | // it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that |
va009039 | 1:22b9052d864d | 79 | // NEW CODE START: |
va009039 | 1:22b9052d864d | 80 | int root_len = 0; |
va009039 | 1:22b9052d864d | 81 | for (it = m_pSvr->m_lpHandlers.begin(); it != m_pSvr->m_lpHandlers.end(); it++) |
va009039 | 1:22b9052d864d | 82 | { |
va009039 | 1:22b9052d864d | 83 | DBG("Checking %s...\n", (*it).first.c_str()); |
va009039 | 1:22b9052d864d | 84 | root_len = (*it).first.length(); |
va009039 | 1:22b9052d864d | 85 | if ( root_len && |
va009039 | 1:22b9052d864d | 86 | !path.compare( 0, root_len, (*it).first ) && |
va009039 | 1:22b9052d864d | 87 | (path[root_len] == '/' || path[root_len] == '\0')) |
va009039 | 1:22b9052d864d | 88 | { |
va009039 | 1:22b9052d864d | 89 | DBG("Found (%s)\n", (*it).first.c_str()); |
va009039 | 1:22b9052d864d | 90 | // Found! |
va009039 | 1:22b9052d864d | 91 | break; // for |
va009039 | 1:22b9052d864d | 92 | } |
va009039 | 1:22b9052d864d | 93 | } |
va009039 | 1:22b9052d864d | 94 | // NEW CODE END |
va009039 | 1:22b9052d864d | 95 | if((it == m_pSvr->m_lpHandlers.end()) && !(m_pSvr->m_lpHandlers.empty())) |
va009039 | 1:22b9052d864d | 96 | { |
va009039 | 1:22b9052d864d | 97 | DBG("Using default handler\n"); |
va009039 | 1:22b9052d864d | 98 | it = m_pSvr->m_lpHandlers.end(); |
va009039 | 1:22b9052d864d | 99 | it--; //Get the last element |
va009039 | 1:22b9052d864d | 100 | if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler |
va009039 | 1:22b9052d864d | 101 | it = m_pSvr->m_lpHandlers.end(); |
va009039 | 1:22b9052d864d | 102 | root_len = 0; |
va009039 | 1:22b9052d864d | 103 | } |
va009039 | 1:22b9052d864d | 104 | if(it == m_pSvr->m_lpHandlers.end()) |
va009039 | 1:22b9052d864d | 105 | { |
va009039 | 1:22b9052d864d | 106 | DBG("No handler found\n"); |
va009039 | 1:22b9052d864d | 107 | close(); //No handler found |
va009039 | 1:22b9052d864d | 108 | return; |
va009039 | 1:22b9052d864d | 109 | } |
va009039 | 1:22b9052d864d | 110 | |
va009039 | 1:22b9052d864d | 111 | DBG("Handler found.\r\n"); |
va009039 | 1:22b9052d864d | 112 | |
va009039 | 1:22b9052d864d | 113 | //HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket); |
va009039 | 1:22b9052d864d | 114 | //NEW CODE 1 LINE: |
va009039 | 1:22b9052d864d | 115 | HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, m_pTCPSocket); |
va009039 | 1:22b9052d864d | 116 | m_pTCPSocket = NULL; //We don't own it anymore |
va009039 | 1:22b9052d864d | 117 | |
va009039 | 1:22b9052d864d | 118 | switch(methCode) |
va009039 | 1:22b9052d864d | 119 | { |
va009039 | 1:22b9052d864d | 120 | case HTTP_GET: |
va009039 | 1:22b9052d864d | 121 | pHdlr->doGet(); |
va009039 | 1:22b9052d864d | 122 | break; |
va009039 | 1:22b9052d864d | 123 | case HTTP_POST: |
va009039 | 1:22b9052d864d | 124 | pHdlr->doPost(); |
va009039 | 1:22b9052d864d | 125 | break; |
va009039 | 1:22b9052d864d | 126 | case HTTP_HEAD: |
va009039 | 1:22b9052d864d | 127 | pHdlr->doHead(); |
va009039 | 1:22b9052d864d | 128 | break; |
va009039 | 1:22b9052d864d | 129 | } |
va009039 | 1:22b9052d864d | 130 | |
va009039 | 1:22b9052d864d | 131 | DBG("Req handled (or being handled)\r\n"); |
va009039 | 1:22b9052d864d | 132 | close(); |
va009039 | 1:22b9052d864d | 133 | } |
va009039 | 1:22b9052d864d | 134 | |
va009039 | 1:22b9052d864d | 135 | void HTTPRequestDispatcher::close() //Close socket and destroy data |
va009039 | 1:22b9052d864d | 136 | { |
va009039 | 1:22b9052d864d | 137 | if(m_closed) |
va009039 | 1:22b9052d864d | 138 | return; |
va009039 | 1:22b9052d864d | 139 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
va009039 | 1:22b9052d864d | 140 | m_watchdog.detach(); |
va009039 | 1:22b9052d864d | 141 | if(m_pTCPSocket) //m_pTCPSocket Should only be destroyed if ownership not passed to an handler |
va009039 | 1:22b9052d864d | 142 | { |
va009039 | 1:22b9052d864d | 143 | m_pTCPSocket->resetOnEvent(); |
va009039 | 1:22b9052d864d | 144 | m_pTCPSocket->close(); |
va009039 | 1:22b9052d864d | 145 | delete m_pTCPSocket; //This fn might have been called by this socket (through an event), so DO NOT DESTROY IT HERE |
va009039 | 1:22b9052d864d | 146 | } |
va009039 | 1:22b9052d864d | 147 | NetService::close(); |
va009039 | 1:22b9052d864d | 148 | } |
va009039 | 1:22b9052d864d | 149 | |
va009039 | 1:22b9052d864d | 150 | |
va009039 | 1:22b9052d864d | 151 | void HTTPRequestDispatcher::onTimeout() //Connection has timed out |
va009039 | 1:22b9052d864d | 152 | { |
va009039 | 1:22b9052d864d | 153 | close(); |
va009039 | 1:22b9052d864d | 154 | } |
va009039 | 1:22b9052d864d | 155 | |
va009039 | 1:22b9052d864d | 156 | bool HTTPRequestDispatcher::getRequest(string* path, string* meth) |
va009039 | 1:22b9052d864d | 157 | { |
va009039 | 1:22b9052d864d | 158 | char req[128]; |
va009039 | 1:22b9052d864d | 159 | char c_path[128]; |
va009039 | 1:22b9052d864d | 160 | char c_meth[128]; |
va009039 | 1:22b9052d864d | 161 | const int maxLen = 128; |
va009039 | 1:22b9052d864d | 162 | char* p = req; |
va009039 | 1:22b9052d864d | 163 | //Read Line |
va009039 | 1:22b9052d864d | 164 | int ret; |
va009039 | 1:22b9052d864d | 165 | int len = 0; |
va009039 | 1:22b9052d864d | 166 | for(int i = 0; i < maxLen - 1; i++) |
va009039 | 1:22b9052d864d | 167 | { |
va009039 | 1:22b9052d864d | 168 | ret = m_pTCPSocket->recv(p, 1); |
va009039 | 1:22b9052d864d | 169 | if(!ret) |
va009039 | 1:22b9052d864d | 170 | { |
va009039 | 1:22b9052d864d | 171 | break; |
va009039 | 1:22b9052d864d | 172 | } |
va009039 | 1:22b9052d864d | 173 | if( (len > 1) && *(p-1)=='\r' && *p=='\n' ) |
va009039 | 1:22b9052d864d | 174 | { |
va009039 | 1:22b9052d864d | 175 | p--; |
va009039 | 1:22b9052d864d | 176 | len-=2; |
va009039 | 1:22b9052d864d | 177 | break; |
va009039 | 1:22b9052d864d | 178 | } |
va009039 | 1:22b9052d864d | 179 | else if( *p=='\n' ) |
va009039 | 1:22b9052d864d | 180 | { |
va009039 | 1:22b9052d864d | 181 | len--; |
va009039 | 1:22b9052d864d | 182 | break; |
va009039 | 1:22b9052d864d | 183 | } |
va009039 | 1:22b9052d864d | 184 | p++; |
va009039 | 1:22b9052d864d | 185 | len++; |
va009039 | 1:22b9052d864d | 186 | } |
va009039 | 1:22b9052d864d | 187 | *p = 0; |
va009039 | 1:22b9052d864d | 188 | |
va009039 | 1:22b9052d864d | 189 | DBG("Parsing request : %s\r\n", req); |
va009039 | 1:22b9052d864d | 190 | |
va009039 | 1:22b9052d864d | 191 | ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path); |
va009039 | 1:22b9052d864d | 192 | if(ret !=2) |
va009039 | 1:22b9052d864d | 193 | return false; |
va009039 | 1:22b9052d864d | 194 | |
va009039 | 1:22b9052d864d | 195 | *meth = string(c_meth); |
va009039 | 1:22b9052d864d | 196 | // NEW CODE (old code removed): |
va009039 | 1:22b9052d864d | 197 | *path = string(c_path); |
va009039 | 1:22b9052d864d | 198 | return true; |
va009039 | 1:22b9052d864d | 199 | } |
va009039 | 1:22b9052d864d | 200 | |
va009039 | 1:22b9052d864d | 201 | |
va009039 | 1:22b9052d864d | 202 | |
va009039 | 1:22b9052d864d | 203 | void HTTPRequestDispatcher::onTCPSocketEvent(TCPSocketEvent e) |
va009039 | 1:22b9052d864d | 204 | { |
va009039 | 1:22b9052d864d | 205 | |
va009039 | 1:22b9052d864d | 206 | DBG("\r\nEvent %d\r\n", e); |
va009039 | 1:22b9052d864d | 207 | |
va009039 | 1:22b9052d864d | 208 | if(m_closed) |
va009039 | 1:22b9052d864d | 209 | { |
va009039 | 1:22b9052d864d | 210 | DBG("\r\nWARN: Discarded\r\n"); |
va009039 | 1:22b9052d864d | 211 | return; |
va009039 | 1:22b9052d864d | 212 | } |
va009039 | 1:22b9052d864d | 213 | |
va009039 | 1:22b9052d864d | 214 | switch(e) |
va009039 | 1:22b9052d864d | 215 | { |
va009039 | 1:22b9052d864d | 216 | case TCPSOCKET_READABLE: |
va009039 | 1:22b9052d864d | 217 | m_watchdog.detach(); |
va009039 | 1:22b9052d864d | 218 | m_pTCPSocket->resetOnEvent(); |
va009039 | 1:22b9052d864d | 219 | //Req arrived, dispatch : |
va009039 | 1:22b9052d864d | 220 | dispatchRequest(); |
va009039 | 1:22b9052d864d | 221 | break; |
va009039 | 1:22b9052d864d | 222 | case TCPSOCKET_CONTIMEOUT: |
va009039 | 1:22b9052d864d | 223 | case TCPSOCKET_CONRST: |
va009039 | 1:22b9052d864d | 224 | case TCPSOCKET_CONABRT: |
va009039 | 1:22b9052d864d | 225 | case TCPSOCKET_ERROR: |
va009039 | 1:22b9052d864d | 226 | case TCPSOCKET_DISCONNECTED: |
va009039 | 1:22b9052d864d | 227 | close(); |
va009039 | 1:22b9052d864d | 228 | break; |
va009039 | 1:22b9052d864d | 229 | } |
va009039 | 1:22b9052d864d | 230 | |
va009039 | 1:22b9052d864d | 231 | } |