Single instance HTTP Server using new Ethernet Interface.

Dependents:   EthHTTPServer if201410_section5 _PE2E_12-04_EthernetInterfaceServer MGAS_GR_Peach ... more

Fork of WiFlyHTTPServer by Henry Leinen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPRequestHandler.h Source File

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         TCPSocketConnection& tcp;
00084         
00085     public:
00086         /** Constructor for HTTPRequestHandler objects.
00087          */
00088         HTTPRequestHandler(HTTPConnection::HTTPMessage&, TCPSocketConnection&);
00089         
00090         /** Destructor for HTTPRequestHandler objects.
00091         */
00092         virtual ~HTTPRequestHandler();
00093         
00094         /** Handler function which will be used by the HTTPServer to serve requests.
00095         * The default version of this function will dispatch respective handler member
00096         * functions according to the request type given in the HTTPMessage object.
00097         * The list of possible functions dispatched is :
00098         * * handleGetRequest
00099         * * handlePostRequest
00100         * * handlePutRequest
00101         */
00102         virtual void handleRequest();
00103         
00104         /** Handler function which will handle errors and return errors correctly
00105         * @param errorCode : The error code returned by the HTTP Server to represent the error condition.
00106         * @param msg : Request Message information.
00107         * @param tcp : The socket which represents the active connection to the client.
00108         */
00109         virtual void handleError(int errorCode, HTTPHeaders* header = NULL);
00110 
00111         /** Function sends the response header which consists of the return code and the headers section
00112         * the response header also contains the length (in bytes) of the body. You need to send the body
00113         * right after calling this function. Please note that you do not need to consider the terminating
00114         * CR+LF after your last CR+LF. This will be sent automatically once \c endResponse is called. Also
00115         * the Length given in \c nLen does not need to consider this additional chars. It will also be
00116         * automatically added in \c startResponse. if you need to change the headers, please do NOT 
00117         * specify the \c Content-Length Header. This is done automatically be the function.
00118         */
00119         void startResponse(int returnCode, long nLen, HTTPHeaders* header = NULL);
00120         void processResponse(int nLen, char* body );
00121         void endResponse();
00122 
00123     protected:
00124         /** Handler function to serve GET requests. Download ressource from server from \c uri location.
00125         */
00126         virtual int handleGetRequest() = 0;
00127         
00128         /** Handler function to serve PUT requests. Upload ressource to server to \c uri location.
00129         */
00130         virtual int handlePutRequest() = 0;
00131         
00132         /** Handler function to serve POST requests. Send data to webserver. Can also be appended to uri.
00133         */
00134         virtual int handlePostRequest() = 0;
00135         
00136         void getStandardHeaders(HTTPHeaders& header, const char* fext = NULL);
00137 };
00138 
00139 #endif //   __HTTPREQUESTHANDLER_H__