aa
Dependents: Internet-Piano_WIZwiki-W7500
Fork of httpServer by
HTTPRequestHandler.h@2:45877b9fab33, 2015-08-11 (annotated)
- Committer:
- ysy00700
- Date:
- Tue Aug 11 05:37:32 2015 +0000
- Revision:
- 2:45877b9fab33
- Parent:
- 0:e59cc54df17c
aa
Who changed what in which revision?
User | Revision | Line number | New 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) |
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 __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 | { |
hjjeon | 0:e59cc54df17c | 81 | protected: |
hjjeon | 0:e59cc54df17c | 82 | HTTPConnection::HTTPMessage& msg; |
hjjeon | 0:e59cc54df17c | 83 | TCPSocketConnection& tcp; |
hjjeon | 0:e59cc54df17c | 84 | |
hjjeon | 0:e59cc54df17c | 85 | public: |
hjjeon | 0:e59cc54df17c | 86 | /** Constructor for HTTPRequestHandler objects. |
hjjeon | 0:e59cc54df17c | 87 | */ |
hjjeon | 0:e59cc54df17c | 88 | HTTPRequestHandler(HTTPConnection::HTTPMessage&, TCPSocketConnection&); |
hjjeon | 0:e59cc54df17c | 89 | |
hjjeon | 0:e59cc54df17c | 90 | /** Destructor for HTTPRequestHandler objects. |
hjjeon | 0:e59cc54df17c | 91 | */ |
hjjeon | 0:e59cc54df17c | 92 | virtual ~HTTPRequestHandler(); |
hjjeon | 0:e59cc54df17c | 93 | |
hjjeon | 0:e59cc54df17c | 94 | /** Handler function which will be used by the HTTPServer to serve requests. |
hjjeon | 0:e59cc54df17c | 95 | * The default version of this function will dispatch respective handler member |
hjjeon | 0:e59cc54df17c | 96 | * functions according to the request type given in the HTTPMessage object. |
hjjeon | 0:e59cc54df17c | 97 | * The list of possible functions dispatched is : |
hjjeon | 0:e59cc54df17c | 98 | * * handleGetRequest |
hjjeon | 0:e59cc54df17c | 99 | * * handlePostRequest |
hjjeon | 0:e59cc54df17c | 100 | * * handlePutRequest |
hjjeon | 0:e59cc54df17c | 101 | */ |
hjjeon | 0:e59cc54df17c | 102 | virtual void handleRequest(); |
hjjeon | 0:e59cc54df17c | 103 | |
hjjeon | 0:e59cc54df17c | 104 | /** Handler function which will handle errors and return errors correctly |
hjjeon | 0:e59cc54df17c | 105 | * @param errorCode : The error code returned by the HTTP Server to represent the error condition. |
hjjeon | 0:e59cc54df17c | 106 | * @param msg : Request Message information. |
hjjeon | 0:e59cc54df17c | 107 | * @param tcp : The socket which represents the active connection to the client. |
hjjeon | 0:e59cc54df17c | 108 | */ |
hjjeon | 0:e59cc54df17c | 109 | virtual void handleError(int errorCode, HTTPHeaders* header = NULL); |
hjjeon | 0:e59cc54df17c | 110 | |
hjjeon | 0:e59cc54df17c | 111 | /** Function sends the response header which consists of the return code and the headers section |
hjjeon | 0:e59cc54df17c | 112 | * the response header also contains the length (in bytes) of the body. You need to send the body |
hjjeon | 0:e59cc54df17c | 113 | * right after calling this function. Please note that you do not need to consider the terminating |
hjjeon | 0:e59cc54df17c | 114 | * CR+LF after your last CR+LF. This will be sent automatically once \c endResponse is called. Also |
hjjeon | 0:e59cc54df17c | 115 | * the Length given in \c nLen does not need to consider this additional chars. It will also be |
hjjeon | 0:e59cc54df17c | 116 | * automatically added in \c startResponse. if you need to change the headers, please do NOT |
hjjeon | 0:e59cc54df17c | 117 | * specify the \c Content-Length Header. This is done automatically be the function. |
hjjeon | 0:e59cc54df17c | 118 | */ |
hjjeon | 0:e59cc54df17c | 119 | void startResponse(int returnCode, long nLen, HTTPHeaders* header = NULL); |
hjjeon | 0:e59cc54df17c | 120 | void processResponse(int nLen, char* body ); |
hjjeon | 0:e59cc54df17c | 121 | void endResponse(); |
hjjeon | 0:e59cc54df17c | 122 | |
hjjeon | 0:e59cc54df17c | 123 | protected: |
hjjeon | 0:e59cc54df17c | 124 | /** Handler function to serve GET requests. Download ressource from server from \c uri location. |
hjjeon | 0:e59cc54df17c | 125 | */ |
hjjeon | 0:e59cc54df17c | 126 | virtual int handleGetRequest() = 0; |
hjjeon | 0:e59cc54df17c | 127 | |
hjjeon | 0:e59cc54df17c | 128 | /** Handler function to serve PUT requests. Upload ressource to server to \c uri location. |
hjjeon | 0:e59cc54df17c | 129 | */ |
hjjeon | 0:e59cc54df17c | 130 | virtual int handlePutRequest() = 0; |
hjjeon | 0:e59cc54df17c | 131 | |
hjjeon | 0:e59cc54df17c | 132 | /** Handler function to serve POST requests. Send data to webserver. Can also be appended to uri. |
hjjeon | 0:e59cc54df17c | 133 | */ |
hjjeon | 0:e59cc54df17c | 134 | virtual int handlePostRequest() = 0; |
hjjeon | 0:e59cc54df17c | 135 | |
hjjeon | 0:e59cc54df17c | 136 | void getStandardHeaders(HTTPHeaders& header, const char* fext = NULL); |
hjjeon | 0:e59cc54df17c | 137 | }; |
hjjeon | 0:e59cc54df17c | 138 | |
hjjeon | 0:e59cc54df17c | 139 | #endif // __HTTPREQUESTHANDLER_H__ |