平成24年中国四国技術職員研修用

Dependencies:   MSCUsbHost NetServices RPCInterface TextLCD mbed

Committer:
yueee_yt
Date:
Wed Aug 22 05:10:09 2012 +0000
Revision:
1:f2088fbce73f
Parent:
0:fd83f3540b1a
???????LM35??????WebServer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:fd83f3540b1a 1
yueee_yt 0:fd83f3540b1a 2 /*
yueee_yt 0:fd83f3540b1a 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
yueee_yt 0:fd83f3540b1a 4
yueee_yt 0:fd83f3540b1a 5 Permission is hereby granted, free of charge, to any person obtaining a copy
yueee_yt 0:fd83f3540b1a 6 of this software and associated documentation files (the "Software"), to deal
yueee_yt 0:fd83f3540b1a 7 in the Software without restriction, including without limitation the rights
yueee_yt 0:fd83f3540b1a 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yueee_yt 0:fd83f3540b1a 9 copies of the Software, and to permit persons to whom the Software is
yueee_yt 0:fd83f3540b1a 10 furnished to do so, subject to the following conditions:
yueee_yt 0:fd83f3540b1a 11
yueee_yt 0:fd83f3540b1a 12 The above copyright notice and this permission notice shall be included in
yueee_yt 0:fd83f3540b1a 13 all copies or substantial portions of the Software.
yueee_yt 0:fd83f3540b1a 14
yueee_yt 0:fd83f3540b1a 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yueee_yt 0:fd83f3540b1a 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yueee_yt 0:fd83f3540b1a 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yueee_yt 0:fd83f3540b1a 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yueee_yt 0:fd83f3540b1a 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yueee_yt 0:fd83f3540b1a 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yueee_yt 0:fd83f3540b1a 21 THE SOFTWARE.
yueee_yt 0:fd83f3540b1a 22 */
yueee_yt 0:fd83f3540b1a 23
yueee_yt 0:fd83f3540b1a 24 /**
yueee_yt 0:fd83f3540b1a 25 HTTP Request Handler header file.
yueee_yt 0:fd83f3540b1a 26 */
yueee_yt 0:fd83f3540b1a 27
yueee_yt 0:fd83f3540b1a 28 #ifndef HTTP_REQUEST_HANDLER_H
yueee_yt 0:fd83f3540b1a 29 #define HTTP_REQUEST_HANDLER_H
yueee_yt 0:fd83f3540b1a 30
yueee_yt 0:fd83f3540b1a 31 #include "api/TCPSocket.h"
yueee_yt 0:fd83f3540b1a 32 //#include "HTTPServer.h"
yueee_yt 0:fd83f3540b1a 33
yueee_yt 0:fd83f3540b1a 34 #include "mbed.h"
yueee_yt 0:fd83f3540b1a 35 #include "core/netservice.h"
yueee_yt 0:fd83f3540b1a 36
yueee_yt 0:fd83f3540b1a 37 #include <string>
yueee_yt 0:fd83f3540b1a 38 using std::string;
yueee_yt 0:fd83f3540b1a 39
yueee_yt 0:fd83f3540b1a 40 #include <map>
yueee_yt 0:fd83f3540b1a 41 using std::map;
yueee_yt 0:fd83f3540b1a 42
yueee_yt 0:fd83f3540b1a 43 ///HTTP Server's generic request handler
yueee_yt 0:fd83f3540b1a 44 class HTTPRequestHandler : public NetService
yueee_yt 0:fd83f3540b1a 45 {
yueee_yt 0:fd83f3540b1a 46 public:
yueee_yt 0:fd83f3540b1a 47 ///Instantiated by the HTTP Server
yueee_yt 0:fd83f3540b1a 48 HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
yueee_yt 0:fd83f3540b1a 49 virtual ~HTTPRequestHandler();
yueee_yt 0:fd83f3540b1a 50
yueee_yt 0:fd83f3540b1a 51 //protected:
yueee_yt 0:fd83f3540b1a 52 virtual void doGet() = 0;
yueee_yt 0:fd83f3540b1a 53 virtual void doPost() = 0;
yueee_yt 0:fd83f3540b1a 54 virtual void doHead() = 0;
yueee_yt 0:fd83f3540b1a 55
yueee_yt 0:fd83f3540b1a 56 virtual void onReadable() = 0; //Data has been read
yueee_yt 0:fd83f3540b1a 57 virtual void onWriteable() = 0; //Data has been written & buf is free
yueee_yt 0:fd83f3540b1a 58 virtual void onTimeout(); //Connection has timed out
yueee_yt 0:fd83f3540b1a 59 virtual void onClose() = 0; //Connection is closing
yueee_yt 0:fd83f3540b1a 60
yueee_yt 0:fd83f3540b1a 61 virtual void close(); //Close socket and destroy data
yueee_yt 0:fd83f3540b1a 62
yueee_yt 0:fd83f3540b1a 63 protected:
yueee_yt 0:fd83f3540b1a 64 map<string, string>& reqHeaders() /*const*/;
yueee_yt 0:fd83f3540b1a 65 string& path() /*const*/;
yueee_yt 0:fd83f3540b1a 66 int dataLen() const;
yueee_yt 0:fd83f3540b1a 67 int readData(char* buf, int len);
yueee_yt 0:fd83f3540b1a 68 string& rootPath() /*const*/;
yueee_yt 0:fd83f3540b1a 69
yueee_yt 0:fd83f3540b1a 70 void setErrCode(int errc);
yueee_yt 0:fd83f3540b1a 71 void setContentLen(int len);
yueee_yt 0:fd83f3540b1a 72
yueee_yt 0:fd83f3540b1a 73 map<string, string>& respHeaders();
yueee_yt 0:fd83f3540b1a 74 int writeData(const char* buf, int len);
yueee_yt 0:fd83f3540b1a 75
yueee_yt 0:fd83f3540b1a 76 void setTimeout(int ms);
yueee_yt 0:fd83f3540b1a 77 void resetTimeout();
yueee_yt 0:fd83f3540b1a 78
yueee_yt 0:fd83f3540b1a 79 private:
yueee_yt 0:fd83f3540b1a 80 void readHeaders(); //Called at instanciation
yueee_yt 0:fd83f3540b1a 81 void writeHeaders(); //Called at the first writeData call
yueee_yt 0:fd83f3540b1a 82 void onTCPSocketEvent(TCPSocketEvent e);
yueee_yt 0:fd83f3540b1a 83
yueee_yt 0:fd83f3540b1a 84 TCPSocket* m_pTCPSocket;
yueee_yt 0:fd83f3540b1a 85 map<string, string> m_reqHeaders;
yueee_yt 0:fd83f3540b1a 86 map<string, string> m_respHeaders;
yueee_yt 0:fd83f3540b1a 87 string m_rootPath;
yueee_yt 0:fd83f3540b1a 88 string m_path;
yueee_yt 0:fd83f3540b1a 89 int m_errc; //Response code
yueee_yt 0:fd83f3540b1a 90
yueee_yt 0:fd83f3540b1a 91 Timeout m_watchdog;
yueee_yt 0:fd83f3540b1a 92 int m_timeout;
yueee_yt 0:fd83f3540b1a 93
yueee_yt 0:fd83f3540b1a 94 bool m_closed;
yueee_yt 0:fd83f3540b1a 95 bool m_headersSent;
yueee_yt 0:fd83f3540b1a 96
yueee_yt 0:fd83f3540b1a 97 int readLine(char* str, int maxLen);
yueee_yt 0:fd83f3540b1a 98
yueee_yt 0:fd83f3540b1a 99 };
yueee_yt 0:fd83f3540b1a 100
yueee_yt 0:fd83f3540b1a 101 #endif