for pet feeder system
Dependents: PetfeederWebServer
Fork of httpServer_with_Ethernt by
HTTPConnection.h@0:e59cc54df17c, 2015-06-29 (annotated)
- Committer:
- hjjeon
- Date:
- Mon Jun 29 09:03:40 2015 +0000
- Revision:
- 0:e59cc54df17c
httpServer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hjjeon | 0:e59cc54df17c | 1 | /* HTTPConnection.h */ |
hjjeon | 0:e59cc54df17c | 2 | /* |
hjjeon | 0:e59cc54df17c | 3 | Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de) |
hjjeon | 0:e59cc54df17c | 4 | |
hjjeon | 0:e59cc54df17c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
hjjeon | 0:e59cc54df17c | 6 | of this software and associated documentation files (the "Software"), to deal |
hjjeon | 0:e59cc54df17c | 7 | in the Software without restriction, including without limitation the rights |
hjjeon | 0:e59cc54df17c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hjjeon | 0:e59cc54df17c | 9 | copies of the Software, and to permit persons to whom the Software is |
hjjeon | 0:e59cc54df17c | 10 | furnished to do so, subject to the following conditions: |
hjjeon | 0:e59cc54df17c | 11 | |
hjjeon | 0:e59cc54df17c | 12 | The above copyright notice and this permission notice shall be included in |
hjjeon | 0:e59cc54df17c | 13 | all copies or substantial portions of the Software. |
hjjeon | 0:e59cc54df17c | 14 | |
hjjeon | 0:e59cc54df17c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hjjeon | 0:e59cc54df17c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hjjeon | 0:e59cc54df17c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hjjeon | 0:e59cc54df17c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hjjeon | 0:e59cc54df17c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hjjeon | 0:e59cc54df17c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hjjeon | 0:e59cc54df17c | 21 | THE SOFTWARE. |
hjjeon | 0:e59cc54df17c | 22 | */ |
hjjeon | 0:e59cc54df17c | 23 | #ifndef __HTTPConnection_H__ |
hjjeon | 0:e59cc54df17c | 24 | #define __HTTPConnection_H__ |
hjjeon | 0:e59cc54df17c | 25 | |
hjjeon | 0:e59cc54df17c | 26 | #include "mbed.h" |
hjjeon | 0:e59cc54df17c | 27 | #include "TCPSocketConnection.h" |
hjjeon | 0:e59cc54df17c | 28 | |
hjjeon | 0:e59cc54df17c | 29 | #include <string> |
hjjeon | 0:e59cc54df17c | 30 | #include <map> |
hjjeon | 0:e59cc54df17c | 31 | |
hjjeon | 0:e59cc54df17c | 32 | class HTTPServer; |
hjjeon | 0:e59cc54df17c | 33 | |
hjjeon | 0:e59cc54df17c | 34 | /** Type HTTPRequestType enumerates request types |
hjjeon | 0:e59cc54df17c | 35 | */ |
hjjeon | 0:e59cc54df17c | 36 | typedef enum |
hjjeon | 0:e59cc54df17c | 37 | { |
hjjeon | 0:e59cc54df17c | 38 | HTTP_RT_GET, /*!< GET request */ |
hjjeon | 0:e59cc54df17c | 39 | HTTP_RT_POST, /*!< POST request */ |
hjjeon | 0:e59cc54df17c | 40 | HTTP_RT_PUT, /*!< PUT request */ |
hjjeon | 0:e59cc54df17c | 41 | HTTP_RT_OPTIONS, /*!< OPTIONS request */ |
hjjeon | 0:e59cc54df17c | 42 | HTTP_RT_HEAD, /*!< HEAD request */ |
hjjeon | 0:e59cc54df17c | 43 | HTTP_RT_DELETE, /*!< DELETE request */ |
hjjeon | 0:e59cc54df17c | 44 | HTTP_RT_TRACE, /*!< TRACE request */ |
hjjeon | 0:e59cc54df17c | 45 | HTTP_RT_CONNECT /*!< CONNECT request */ |
hjjeon | 0:e59cc54df17c | 46 | } HTTPRequestType; |
hjjeon | 0:e59cc54df17c | 47 | |
hjjeon | 0:e59cc54df17c | 48 | /** class HTTPConnection, encapsulates one connection being made throught the HTTPServer |
hjjeon | 0:e59cc54df17c | 49 | * |
hjjeon | 0:e59cc54df17c | 50 | */ |
hjjeon | 0:e59cc54df17c | 51 | class HTTPConnection { |
hjjeon | 0:e59cc54df17c | 52 | public: |
hjjeon | 0:e59cc54df17c | 53 | |
hjjeon | 0:e59cc54df17c | 54 | /** HTTPMessage contains all the details of the request received by external HTTP client. |
hjjeon | 0:e59cc54df17c | 55 | */ |
hjjeon | 0:e59cc54df17c | 56 | typedef struct |
hjjeon | 0:e59cc54df17c | 57 | { |
hjjeon | 0:e59cc54df17c | 58 | /** Specifies the request type received |
hjjeon | 0:e59cc54df17c | 59 | */ |
hjjeon | 0:e59cc54df17c | 60 | HTTPRequestType request; |
hjjeon | 0:e59cc54df17c | 61 | /** The uri associated with the request. |
hjjeon | 0:e59cc54df17c | 62 | */ |
hjjeon | 0:e59cc54df17c | 63 | std::string uri; |
hjjeon | 0:e59cc54df17c | 64 | /** Contains the HTTP/1.1 or HTTP/1.0 version requested by client. |
hjjeon | 0:e59cc54df17c | 65 | */ |
hjjeon | 0:e59cc54df17c | 66 | std::string version; |
hjjeon | 0:e59cc54df17c | 67 | /** Map of headers provided by the client in the form <HeaderName>:<HeaderValue> |
hjjeon | 0:e59cc54df17c | 68 | */ |
hjjeon | 0:e59cc54df17c | 69 | std::map<std::string, std::string> headers; |
hjjeon | 0:e59cc54df17c | 70 | /** Map of arguments that came with the uri string |
hjjeon | 0:e59cc54df17c | 71 | */ |
hjjeon | 0:e59cc54df17c | 72 | std::map<std::string, std::string> args; |
hjjeon | 0:e59cc54df17c | 73 | |
hjjeon | 0:e59cc54df17c | 74 | //attribute data |
hjjeon | 0:e59cc54df17c | 75 | char attri[50]; |
hjjeon | 0:e59cc54df17c | 76 | //attribute length |
hjjeon | 0:e59cc54df17c | 77 | uint32_t attri_len; |
hjjeon | 0:e59cc54df17c | 78 | } HTTPMessage; |
hjjeon | 0:e59cc54df17c | 79 | |
hjjeon | 0:e59cc54df17c | 80 | /** Public constructor for HTTPConnection objects. |
hjjeon | 0:e59cc54df17c | 81 | * |
hjjeon | 0:e59cc54df17c | 82 | */ |
hjjeon | 0:e59cc54df17c | 83 | HTTPConnection (TCPSocketConnection& clnt); |
hjjeon | 0:e59cc54df17c | 84 | |
hjjeon | 0:e59cc54df17c | 85 | /** Destructor for HTTPConnection objects. |
hjjeon | 0:e59cc54df17c | 86 | * |
hjjeon | 0:e59cc54df17c | 87 | */ |
hjjeon | 0:e59cc54df17c | 88 | ~HTTPConnection(); |
hjjeon | 0:e59cc54df17c | 89 | |
hjjeon | 0:e59cc54df17c | 90 | /** Query if this connection is already closed and can be deleted. |
hjjeon | 0:e59cc54df17c | 91 | @returns true, if connection is closed. |
hjjeon | 0:e59cc54df17c | 92 | */ |
hjjeon | 0:e59cc54df17c | 93 | bool is_closed(); |
hjjeon | 0:e59cc54df17c | 94 | |
hjjeon | 0:e59cc54df17c | 95 | /** Polling function for the connection. |
hjjeon | 0:e59cc54df17c | 96 | * @returns -1 if connection is not required anymore. In the current version of this library this should be the normal case. This may change in future versions. |
hjjeon | 0:e59cc54df17c | 97 | */ |
hjjeon | 0:e59cc54df17c | 98 | int poll(); |
hjjeon | 0:e59cc54df17c | 99 | |
hjjeon | 0:e59cc54df17c | 100 | protected: |
hjjeon | 0:e59cc54df17c | 101 | |
hjjeon | 0:e59cc54df17c | 102 | /** Function to close this connection. To be called from internally. |
hjjeon | 0:e59cc54df17c | 103 | */ |
hjjeon | 0:e59cc54df17c | 104 | void close(); |
hjjeon | 0:e59cc54df17c | 105 | |
hjjeon | 0:e59cc54df17c | 106 | friend class HTTPServer; |
hjjeon | 0:e59cc54df17c | 107 | |
hjjeon | 0:e59cc54df17c | 108 | TCPSocketConnection m_Tcp; |
hjjeon | 0:e59cc54df17c | 109 | HTTPMessage m_Msg; |
hjjeon | 0:e59cc54df17c | 110 | |
hjjeon | 0:e59cc54df17c | 111 | /** parse a HTTP request line from the content of the buffer. |
hjjeon | 0:e59cc54df17c | 112 | * @param buffer : the request received from the client in the form <RequestType> <uri> <Http Version> |
hjjeon | 0:e59cc54df17c | 113 | * @returns -1 if the request is invalid or 0 if the request is valid |
hjjeon | 0:e59cc54df17c | 114 | */ |
hjjeon | 0:e59cc54df17c | 115 | int parse(char *buffer); |
hjjeon | 0:e59cc54df17c | 116 | |
hjjeon | 0:e59cc54df17c | 117 | /** parses the received string in \c buffer for HTTP request headers. |
hjjeon | 0:e59cc54df17c | 118 | * @param buffer : buffer which contains the string to parse for headers. |
hjjeon | 0:e59cc54df17c | 119 | * @returns -1 in case of an error, otherwise returns 0 |
hjjeon | 0:e59cc54df17c | 120 | */ |
hjjeon | 0:e59cc54df17c | 121 | int parseHeader(char *buffer); |
hjjeon | 0:e59cc54df17c | 122 | int receiveHeaders(const char* buffer, int nBuffSize); |
hjjeon | 0:e59cc54df17c | 123 | |
hjjeon | 0:e59cc54df17c | 124 | /** Function which receives a line from the current Socket |
hjjeon | 0:e59cc54df17c | 125 | * @param szline : will contain one line received from the socket |
hjjeon | 0:e59cc54df17c | 126 | * @param nMaxLen : the size of the buffer. If the line is longer than the buffer the line is cropped at the end. |
hjjeon | 0:e59cc54df17c | 127 | * @param nTimeout : if the timeout elapses, only the portion that has been received within this time will be returned. |
hjjeon | 0:e59cc54df17c | 128 | * @param szLineTerm : the \c end-of-line character to be used to detect the end of the line. |
hjjeon | 0:e59cc54df17c | 129 | * @returns -1 in case of an error or timeout, -2 in case of an empty line or the number of received characters. |
hjjeon | 0:e59cc54df17c | 130 | */ |
hjjeon | 0:e59cc54df17c | 131 | int receiveLine(char* szLine, int nMaxLen, int nTimeout = -1, char szLineTerm = '\n'); |
hjjeon | 0:e59cc54df17c | 132 | |
hjjeon | 0:e59cc54df17c | 133 | |
hjjeon | 0:e59cc54df17c | 134 | /** parse the receoved \c uri_string for arguments which will be stored in the \c args map. |
hjjeon | 0:e59cc54df17c | 135 | * @parse uri_string : the uri string which was received from the client. |
hjjeon | 0:e59cc54df17c | 136 | * @parse args : the args map which will receive the argument:value touples from the \c uri_string. |
hjjeon | 0:e59cc54df17c | 137 | * @returns -1 if an error occured, otherwise returns 0. |
hjjeon | 0:e59cc54df17c | 138 | */ |
hjjeon | 0:e59cc54df17c | 139 | int parseUriArgs(char *uri_string, map<string,string>& args); |
hjjeon | 0:e59cc54df17c | 140 | |
hjjeon | 0:e59cc54df17c | 141 | }; |
hjjeon | 0:e59cc54df17c | 142 | |
hjjeon | 0:e59cc54df17c | 143 | #endif // __HTTPConnection_H__ |