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