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