HTTPServer

Committer:
jphuc96
Date:
Sat Sep 16 02:39:55 2017 +0000
Revision:
0:caf5feddac47
v1

Who changed what in which revision?

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