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