Shlomi Ruder / HoneyPot

Fork of HoneyPot by Shlomi Ruder

Committer:
proxytype
Date:
Fri Sep 01 23:36:19 2017 +0000
Revision:
2:f52734664057
Parent:
0:e59cc54df17c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hjjeon 0:e59cc54df17c 1 /* HTTPRequestHandler.h */
hjjeon 0:e59cc54df17c 2 /*
hjjeon 0:e59cc54df17c 3 Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
proxytype 2:f52734664057 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:
proxytype 2:f52734664057 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.
proxytype 2:f52734664057 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 __HTTPREQUESTHANDLER_H__
hjjeon 0:e59cc54df17c 24 #define __HTTPREQUESTHANDLER_H__
hjjeon 0:e59cc54df17c 25
hjjeon 0:e59cc54df17c 26 #include "mbed.h"
hjjeon 0:e59cc54df17c 27 #include "HTTPConnection.h"
hjjeon 0:e59cc54df17c 28
hjjeon 0:e59cc54df17c 29 #include <map>
hjjeon 0:e59cc54df17c 30
hjjeon 0:e59cc54df17c 31 typedef std::map<const char*,const char*> HTTPHeaders;
hjjeon 0:e59cc54df17c 32
hjjeon 0:e59cc54df17c 33 typedef enum {
hjjeon 0:e59cc54df17c 34 HTTP_Continue = 100, // 100
hjjeon 0:e59cc54df17c 35 HTTP_SwitchingProtocols = 101, // 101
hjjeon 0:e59cc54df17c 36 HTTP_Ok = 200, // 200
hjjeon 0:e59cc54df17c 37 HTTP_Created = 201, // 201
hjjeon 0:e59cc54df17c 38 HTTP_Accepted = 202, // 202
hjjeon 0:e59cc54df17c 39 HTTP_NonAuthoritativeInformation = 203, // 203
hjjeon 0:e59cc54df17c 40 HTTP_NoContent = 204, // 204
hjjeon 0:e59cc54df17c 41 HTTP_ResetContent = 205, // 205
hjjeon 0:e59cc54df17c 42 HTTP_PartialContent = 206, // 206
hjjeon 0:e59cc54df17c 43 HTTP_MultipleChoices = 300, // 300
hjjeon 0:e59cc54df17c 44 HTTP_MovedPermanently = 301, // 301
hjjeon 0:e59cc54df17c 45 HTTP_Found = 302, // 302
hjjeon 0:e59cc54df17c 46 HTTP_SeeOther = 303, // 303
hjjeon 0:e59cc54df17c 47 HTTP_NotModified = 304, // 304
hjjeon 0:e59cc54df17c 48 HTTP_UseProxy = 305, // 305
hjjeon 0:e59cc54df17c 49 HTTP_TemporaryRedirect = 307, // 307
hjjeon 0:e59cc54df17c 50 HTTP_BadRequest = 400, // 400
hjjeon 0:e59cc54df17c 51 HTTP_Unauthorized = 401, // 401
hjjeon 0:e59cc54df17c 52 HTTP_PaymentRequired = 402, // 402
hjjeon 0:e59cc54df17c 53 HTTP_Forbidden = 403, // 403
hjjeon 0:e59cc54df17c 54 HTTP_NotFound = 404, // 404
hjjeon 0:e59cc54df17c 55 HTTP_MethodNotAllowed = 405, // 405
hjjeon 0:e59cc54df17c 56 HTTP_NotAcceptable = 406, // 406
hjjeon 0:e59cc54df17c 57 HTTP_ProxyAuthRequired = 407, // 407
hjjeon 0:e59cc54df17c 58 HTTP_RequestTimeOut = 408, // 408
hjjeon 0:e59cc54df17c 59 HTTP_Conflict = 409, // 409
hjjeon 0:e59cc54df17c 60 HTTP_Gone = 410, // 410
hjjeon 0:e59cc54df17c 61 HTTP_LengthRequired = 411, // 411
hjjeon 0:e59cc54df17c 62 HTTP_PreconditionFailed = 412, // 412
hjjeon 0:e59cc54df17c 63 HTTP_RequestEntityTooLarge = 413, // 413
hjjeon 0:e59cc54df17c 64 HTTP_RequestURITooLarge = 414, // 414
hjjeon 0:e59cc54df17c 65 HTTP_UnsupportedMediaType = 415, // 415
hjjeon 0:e59cc54df17c 66 HTTP_RequestedRangeNotSatisfiable = 416, // 416
hjjeon 0:e59cc54df17c 67 HTTP_ExpectationFailed = 417, // 417
hjjeon 0:e59cc54df17c 68 HTTP_InternalServerError = 500, // 500
hjjeon 0:e59cc54df17c 69 HTTP_NotImplemented = 501, // 501
hjjeon 0:e59cc54df17c 70 HTTP_BadGateway = 502, // 502
hjjeon 0:e59cc54df17c 71 HTTP_ServiceUnavailable = 503, // 503
hjjeon 0:e59cc54df17c 72 HTTP_GatewayTimeout = 504, // 504
hjjeon 0:e59cc54df17c 73 HTTP_HTTPVersionNotSupported = 505, // 505
hjjeon 0:e59cc54df17c 74 } HTTPResponseCode;
hjjeon 0:e59cc54df17c 75
hjjeon 0:e59cc54df17c 76 /** class HTTPRequestHandler is the base class for HTTP Handler request implementations.
hjjeon 0:e59cc54df17c 77 *
hjjeon 0:e59cc54df17c 78 */
hjjeon 0:e59cc54df17c 79 class HTTPRequestHandler
hjjeon 0:e59cc54df17c 80 {
proxytype 2:f52734664057 81
proxytype 2:f52734664057 82 protected:
proxytype 2:f52734664057 83 HTTPConnection::HTTPMessage& msg;
proxytype 2:f52734664057 84 TCPSocketConnection& tcp;
proxytype 2:f52734664057 85 int * m_ports;
proxytype 2:f52734664057 86 int m_portsCount;
proxytype 2:f52734664057 87 int * m_hits;
proxytype 2:f52734664057 88 int m_isMaster;
proxytype 2:f52734664057 89 public:
proxytype 2:f52734664057 90 /** Constructor for HTTPRequestHandler objects.
proxytype 2:f52734664057 91 */
proxytype 2:f52734664057 92 HTTPRequestHandler(HTTPConnection::HTTPMessage&, TCPSocketConnection&,int isMaster, int * ports, int portsCount, int * m_hits);
proxytype 2:f52734664057 93
proxytype 2:f52734664057 94 /** Destructor for HTTPRequestHandler objects.
proxytype 2:f52734664057 95 */
proxytype 2:f52734664057 96 virtual ~HTTPRequestHandler();
proxytype 2:f52734664057 97
proxytype 2:f52734664057 98 /** Handler function which will be used by the HTTPServer to serve requests.
proxytype 2:f52734664057 99 * The default version of this function will dispatch respective handler member
proxytype 2:f52734664057 100 * functions according to the request type given in the HTTPMessage object.
proxytype 2:f52734664057 101 * The list of possible functions dispatched is :
proxytype 2:f52734664057 102 * * handleGetRequest
proxytype 2:f52734664057 103 * * handlePostRequest
proxytype 2:f52734664057 104 * * handlePutRequest
proxytype 2:f52734664057 105 */
proxytype 2:f52734664057 106 virtual void handleRequest();
hjjeon 0:e59cc54df17c 107
proxytype 2:f52734664057 108 /** Handler function which will handle errors and return errors correctly
proxytype 2:f52734664057 109 * @param errorCode : The error code returned by the HTTP Server to represent the error condition.
proxytype 2:f52734664057 110 * @param msg : Request Message information.
proxytype 2:f52734664057 111 * @param tcp : The socket which represents the active connection to the client.
proxytype 2:f52734664057 112 */
proxytype 2:f52734664057 113 virtual void handleError(int errorCode, HTTPHeaders* header = NULL);
hjjeon 0:e59cc54df17c 114
proxytype 2:f52734664057 115 /** Function sends the response header which consists of the return code and the headers section
proxytype 2:f52734664057 116 * the response header also contains the length (in bytes) of the body. You need to send the body
proxytype 2:f52734664057 117 * right after calling this function. Please note that you do not need to consider the terminating
proxytype 2:f52734664057 118 * CR+LF after your last CR+LF. This will be sent automatically once \c endResponse is called. Also
proxytype 2:f52734664057 119 * the Length given in \c nLen does not need to consider this additional chars. It will also be
proxytype 2:f52734664057 120 * automatically added in \c startResponse. if you need to change the headers, please do NOT
proxytype 2:f52734664057 121 * specify the \c Content-Length Header. This is done automatically be the function.
proxytype 2:f52734664057 122 */
proxytype 2:f52734664057 123 void startResponse(int returnCode, HTTPHeaders* header = NULL);
proxytype 2:f52734664057 124 void processResponse(int nLen, char* body );
proxytype 2:f52734664057 125 void endResponse();
proxytype 2:f52734664057 126
proxytype 2:f52734664057 127 protected:
proxytype 2:f52734664057 128 /** Handler function to serve GET requests. Download ressource from server from \c uri location.
proxytype 2:f52734664057 129 */
proxytype 2:f52734664057 130 virtual int handleGetRequest() = 0;
proxytype 2:f52734664057 131
proxytype 2:f52734664057 132 /** Handler function to serve PUT requests. Upload ressource to server to \c uri location.
proxytype 2:f52734664057 133 */
proxytype 2:f52734664057 134 virtual int handlePutRequest() = 0;
proxytype 2:f52734664057 135
proxytype 2:f52734664057 136 /** Handler function to serve POST requests. Send data to webserver. Can also be appended to uri.
proxytype 2:f52734664057 137 */
proxytype 2:f52734664057 138 virtual int handlePostRequest() = 0;
proxytype 2:f52734664057 139
proxytype 2:f52734664057 140 void getStandardHeaders(HTTPHeaders& header, const char* fext = NULL);
hjjeon 0:e59cc54df17c 141 };
hjjeon 0:e59cc54df17c 142
hjjeon 0:e59cc54df17c 143 #endif // __HTTPREQUESTHANDLER_H__