Main Server - Listen to different ports and write information to SD card, synchronize RTC with NTP over the internet, and stream events over serial

Fork of HoneyPot by Shlomi Ruder

Honeypot Server - WIZnet W7500

/media/uploads/proxytype/honeypot.png

Main server library responsible for detecting and report unwanted activities.

Features:

  • listen up to 7 different ports.
  • synchronize RTC with NTP service.
  • support http chunks responses.
  • write connection details to log file on SD card.
  • inject data to pattern html file (like real server).
  • administrator control panel.
  • visitors display message over http.

Http Server Upgrade

the based code of wiznet http server example load file from the sd card and return it to the user, using content-length to define the size of the package, because reading from file the size is fixed so it's very easy to handle but if you want to use the file as pattern and then inject dynamic data directly like a real server it's become more difficult because memory limitation of the device, to solved this we must to remove the content length header and replace it with transfer-encoding, this way we can stream the data to the client without knowing the content length only the chunks size.

 header["Transfer-Encoding"] = "chunked";
Committer:
proxytype
Date:
Fri Sep 01 23:37:29 2017 +0000
Revision:
3:1d6096332358
Parent:
2:f52734664057

        

Who changed what in which revision?

UserRevisionLine numberNew 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;
proxytype 2:f52734664057 107 friend class HoneypotServer;
hjjeon 0:e59cc54df17c 108
hjjeon 0:e59cc54df17c 109 TCPSocketConnection m_Tcp;
hjjeon 0:e59cc54df17c 110 HTTPMessage m_Msg;
hjjeon 0:e59cc54df17c 111
hjjeon 0:e59cc54df17c 112 /** parse a HTTP request line from the content of the buffer.
hjjeon 0:e59cc54df17c 113 * @param buffer : the request received from the client in the form <RequestType> <uri> <Http Version>
hjjeon 0:e59cc54df17c 114 * @returns -1 if the request is invalid or 0 if the request is valid
hjjeon 0:e59cc54df17c 115 */
hjjeon 0:e59cc54df17c 116 int parse(char *buffer);
hjjeon 0:e59cc54df17c 117
hjjeon 0:e59cc54df17c 118 /** parses the received string in \c buffer for HTTP request headers.
hjjeon 0:e59cc54df17c 119 * @param buffer : buffer which contains the string to parse for headers.
hjjeon 0:e59cc54df17c 120 * @returns -1 in case of an error, otherwise returns 0
hjjeon 0:e59cc54df17c 121 */
hjjeon 0:e59cc54df17c 122 int parseHeader(char *buffer);
hjjeon 0:e59cc54df17c 123 int receiveHeaders(const char* buffer, int nBuffSize);
hjjeon 0:e59cc54df17c 124
hjjeon 0:e59cc54df17c 125 /** Function which receives a line from the current Socket
hjjeon 0:e59cc54df17c 126 * @param szline : will contain one line received from the socket
hjjeon 0:e59cc54df17c 127 * @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 128 * @param nTimeout : if the timeout elapses, only the portion that has been received within this time will be returned.
hjjeon 0:e59cc54df17c 129 * @param szLineTerm : the \c end-of-line character to be used to detect the end of the line.
hjjeon 0:e59cc54df17c 130 * @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 131 */
hjjeon 0:e59cc54df17c 132 int receiveLine(char* szLine, int nMaxLen, int nTimeout = -1, char szLineTerm = '\n');
hjjeon 0:e59cc54df17c 133
hjjeon 0:e59cc54df17c 134
hjjeon 0:e59cc54df17c 135 /** parse the receoved \c uri_string for arguments which will be stored in the \c args map.
hjjeon 0:e59cc54df17c 136 * @parse uri_string : the uri string which was received from the client.
hjjeon 0:e59cc54df17c 137 * @parse args : the args map which will receive the argument:value touples from the \c uri_string.
hjjeon 0:e59cc54df17c 138 * @returns -1 if an error occured, otherwise returns 0.
hjjeon 0:e59cc54df17c 139 */
hjjeon 0:e59cc54df17c 140 int parseUriArgs(char *uri_string, map<string,string>& args);
hjjeon 0:e59cc54df17c 141
hjjeon 0:e59cc54df17c 142 };
hjjeon 0:e59cc54df17c 143
hjjeon 0:e59cc54df17c 144 #endif // __HTTPConnection_H__