Single instance HTTP Server using WiFly Interface.
Dependents: WiFlyHTTPServerSample MultiThreadingHTTPServer
HTTPRequestHandler.h
00001 /* HTTPRequestHandler.h */ 00002 /* 00003 Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 #ifndef __HTTPREQUESTHANDLER_H__ 00024 #define __HTTPREQUESTHANDLER_H__ 00025 00026 #include "mbed.h" 00027 #include "HTTPConnection.h" 00028 00029 #include <map> 00030 00031 typedef std::map<const char*,const char*> HTTPHeaders; 00032 00033 typedef enum { 00034 HTTP_Continue = 100, // 100 00035 HTTP_SwitchingProtocols = 101, // 101 00036 HTTP_Ok = 200, // 200 00037 HTTP_Created = 201, // 201 00038 HTTP_Accepted = 202, // 202 00039 HTTP_NonAuthoritativeInformation = 203, // 203 00040 HTTP_NoContent = 204, // 204 00041 HTTP_ResetContent = 205, // 205 00042 HTTP_PartialContent = 206, // 206 00043 HTTP_MultipleChoices = 300, // 300 00044 HTTP_MovedPermanently = 301, // 301 00045 HTTP_Found = 302, // 302 00046 HTTP_SeeOther = 303, // 303 00047 HTTP_NotModified = 304, // 304 00048 HTTP_UseProxy = 305, // 305 00049 HTTP_TemporaryRedirect = 307, // 307 00050 HTTP_BadRequest = 400, // 400 00051 HTTP_Unauthorized = 401, // 401 00052 HTTP_PaymentRequired = 402, // 402 00053 HTTP_Forbidden = 403, // 403 00054 HTTP_NotFound = 404, // 404 00055 HTTP_MethodNotAllowed = 405, // 405 00056 HTTP_NotAcceptable = 406, // 406 00057 HTTP_ProxyAuthRequired = 407, // 407 00058 HTTP_RequestTimeOut = 408, // 408 00059 HTTP_Conflict = 409, // 409 00060 HTTP_Gone = 410, // 410 00061 HTTP_LengthRequired = 411, // 411 00062 HTTP_PreconditionFailed = 412, // 412 00063 HTTP_RequestEntityTooLarge = 413, // 413 00064 HTTP_RequestURITooLarge = 414, // 414 00065 HTTP_UnsupportedMediaType = 415, // 415 00066 HTTP_RequestedRangeNotSatisfiable = 416, // 416 00067 HTTP_ExpectationFailed = 417, // 417 00068 HTTP_InternalServerError = 500, // 500 00069 HTTP_NotImplemented = 501, // 501 00070 HTTP_BadGateway = 502, // 502 00071 HTTP_ServiceUnavailable = 503, // 503 00072 HTTP_GatewayTimeout = 504, // 504 00073 HTTP_HTTPVersionNotSupported = 505, // 505 00074 } HTTPResponseCode; 00075 00076 /** class HTTPRequestHandler is the base class for HTTP Handler request implementations. 00077 * 00078 */ 00079 class HTTPRequestHandler 00080 { 00081 protected: 00082 HTTPConnection::HTTPMessage& msg; 00083 00084 public: 00085 /** Constructor for HTTPRequestHandler objects. 00086 */ 00087 HTTPRequestHandler(HTTPConnection::HTTPMessage&); 00088 00089 /** Destructor for HTTPRequestHandler objects. 00090 */ 00091 virtual ~HTTPRequestHandler(); 00092 00093 /** Handler function which will be used by the HTTPServer to serve requests. 00094 * The default version of this function will dispatch respective handler member 00095 * functions according to the request type given in the HTTPMessage object. 00096 * The list of possible functions dispatched is : 00097 * * handleGetRequest 00098 * * handlePostRequest 00099 * * handlePutRequest 00100 */ 00101 virtual void handleRequest(); 00102 00103 /** Handler function which will handle errors and return errors correctly 00104 * @param errorCode : The error code returned by the HTTP Server to represent the error condition. 00105 * @param msg : Request Message information. 00106 * @param tcp : The socket which represents the active connection to the client. 00107 */ 00108 virtual void handleError(int errorCode, HTTPHeaders* header = NULL); 00109 00110 /** Function sends the response header which consists of the return code and the headers section 00111 * the response header also contains the length (in bytes) of the body. You need to send the body 00112 * right after calling this function. Please note that you do not need to consider the terminating 00113 * CR+LF after your last CR+LF. This will be sent automatically once \c endResponse is called. Also 00114 * the Length given in \c nLen does not need to consider this additional chars. It will also be 00115 * automatically added in \c startResponse. if you need to change the headers, please do NOT 00116 * specify the \c Content-Length Header. This is done automatically be the function. 00117 */ 00118 void startResponse(int returnCode, long nLen, HTTPHeaders* header = NULL); 00119 void processResponse(int nLen, char* body ); 00120 void endResponse(); 00121 00122 protected: 00123 /** Handler function to serve GET requests. Download ressource from server from \c uri location. 00124 */ 00125 virtual int handleGetRequest() = 0; 00126 00127 /** Handler function to serve PUT requests. Upload ressource to server to \c uri location. 00128 */ 00129 virtual int handlePutRequest() = 0; 00130 00131 /** Handler function to serve POST requests. Send data to webserver. Can also be appended to uri. 00132 */ 00133 virtual int handlePostRequest() = 0; 00134 00135 void getStandardHeaders(HTTPHeaders& header, const char* fext = NULL); 00136 }; 00137 00138 #endif // __HTTPREQUESTHANDLER_H__
Generated on Tue Jul 12 2022 16:18:49 by 1.7.2