|
| 1 | +// |
| 2 | +// AbstractHTTPRequestHandler.h |
| 3 | +// |
| 4 | +// $Id: //poco/Main/Net/include/Poco/Net/AbstractHTTPRequestHandler.h#2 $ |
| 5 | +// |
| 6 | +// Library: Net |
| 7 | +// Package: HTTPServer |
| 8 | +// Module: AbstractHTTPRequestHandler |
| 9 | +// |
| 10 | +// Definition of the AbstractHTTPRequestHandler class. |
| 11 | +// |
| 12 | +// Copyright (c) 2007, Applied Informatics Software Engineering GmbH. |
| 13 | +// and Contributors. |
| 14 | +// |
| 15 | +// Permission is hereby granted, free of charge, to any person or organization |
| 16 | +// obtaining a copy of the software and accompanying documentation covered by |
| 17 | +// this license (the "Software") to use, reproduce, display, distribute, |
| 18 | +// execute, and transmit the Software, and to prepare derivative works of the |
| 19 | +// Software, and to permit third-parties to whom the Software is furnished to |
| 20 | +// do so, all subject to the following: |
| 21 | +// |
| 22 | +// The copyright notices in the Software and this entire statement, including |
| 23 | +// the above license grant, this restriction and the following disclaimer, |
| 24 | +// must be included in all copies of the Software, in whole or in part, and |
| 25 | +// all derivative works of the Software, unless such copies or derivative |
| 26 | +// works are solely in the form of machine-executable object code generated by |
| 27 | +// a source language processor. |
| 28 | +// |
| 29 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
| 32 | +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
| 33 | +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
| 34 | +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 35 | +// DEALINGS IN THE SOFTWARE. |
| 36 | +// |
| 37 | + |
| 38 | + |
| 39 | +#ifndef Net_AbstractHTTPRequestHandler_INCLUDED |
| 40 | +#define Net_AbstractHTTPRequestHandler_INCLUDED |
| 41 | + |
| 42 | + |
| 43 | +#include "Poco/Net/HTTPRequestHandler.h" |
| 44 | +#include "Poco/Net/HTTPResponse.h" |
| 45 | + |
| 46 | + |
| 47 | +namespace Poco { |
| 48 | +namespace Net { |
| 49 | + |
| 50 | + |
| 51 | +class HTMLForm; |
| 52 | + |
| 53 | + |
| 54 | +class Net_API AbstractHTTPRequestHandler: public HTTPRequestHandler |
| 55 | + /// The abstract base class for AbstractHTTPRequestHandlers |
| 56 | + /// created by HTTPServer. |
| 57 | + /// |
| 58 | + /// Derived classes must override the run() method. |
| 59 | + |
| 60 | + /// Contrary to a HTTPRequestHandler, an AbstractHTTPRequestHandler |
| 61 | + /// stores request and response as member variables to avoid having |
| 62 | + /// to pass them around as method parameters. Additionally, a |
| 63 | + /// HTMLForm object is created for use by subclasses. |
| 64 | + /// |
| 65 | + /// The run() method must perform the complete handling |
| 66 | + /// of the HTTP request connection. As soon as the run() |
| 67 | + /// method returns, the request handler object is destroyed. |
| 68 | + /// |
| 69 | + /// A new AbstractHTTPRequestHandler object will be created for |
| 70 | + /// each new HTTP request that is received by the HTTPServer. |
| 71 | +{ |
| 72 | +public: |
| 73 | + AbstractHTTPRequestHandler(); |
| 74 | + /// Creates the AbstractHTTPRequestHandler. |
| 75 | + |
| 76 | + virtual ~AbstractHTTPRequestHandler(); |
| 77 | + /// Destroys the AbstractHTTPRequestHandler. |
| 78 | + |
| 79 | + void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response); |
| 80 | + /// This class implements some common behavior, |
| 81 | + /// before calling run() to actually handle the request: |
| 82 | + /// - save request and response objects; |
| 83 | + /// - call authorize(); |
| 84 | + /// - if authorize() returns true call run(), |
| 85 | + /// else send 401 (Unauthorized) response. |
| 86 | + |
| 87 | + HTTPServerRequest& request(); |
| 88 | + /// Returns the request. |
| 89 | + |
| 90 | + HTTPServerResponse& response(); |
| 91 | + /// Returns the response. |
| 92 | + |
| 93 | + HTMLForm& form(); |
| 94 | + /// Returns a HTMLForm for the given request. |
| 95 | + /// The HTMLForm object is created when this |
| 96 | + /// member function is executed the first time. |
| 97 | + |
| 98 | + void sendErrorResponse(HTTPResponse::HTTPStatus status, const std::string& message); |
| 99 | + /// Sends a HTML error page for the given status code. |
| 100 | + /// The given message is added to the page: |
| 101 | + /// <HTML> |
| 102 | + /// <HEAD> |
| 103 | + /// <TITLE>status - reason</TITLE> |
| 104 | + /// </HEAD> |
| 105 | + /// <BODY> |
| 106 | + /// <H1>status - reason</H1> |
| 107 | + /// <P>message</P> |
| 108 | + /// </BODY> |
| 109 | + /// </HTML> |
| 110 | + |
| 111 | +protected: |
| 112 | + virtual void run() = 0; |
| 113 | + /// Must be overridden by subclasses. |
| 114 | + /// |
| 115 | + /// Handles the given request. |
| 116 | + |
| 117 | + virtual bool authenticate(); |
| 118 | + /// Check authentication; returns true if okay, false if failed to authenticate. |
| 119 | + /// The default implementation always returns true. |
| 120 | + /// |
| 121 | + /// Subclasses can override this member function to perform |
| 122 | + /// some form of client or request authentication before |
| 123 | + /// the request is actually handled. |
| 124 | + |
| 125 | +private: |
| 126 | + HTTPServerRequest* _pRequest; |
| 127 | + HTTPServerResponse* _pResponse; |
| 128 | + HTMLForm* _pForm; |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +// |
| 133 | +// inlines |
| 134 | +// |
| 135 | +inline HTTPServerRequest& AbstractHTTPRequestHandler::request() |
| 136 | +{ |
| 137 | + poco_check_ptr (_pRequest); |
| 138 | + |
| 139 | + return *_pRequest; |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +inline HTTPServerResponse& AbstractHTTPRequestHandler::response() |
| 144 | +{ |
| 145 | + poco_check_ptr (_pResponse); |
| 146 | + |
| 147 | + return *_pResponse; |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +} } // namespace Poco::Net |
| 152 | + |
| 153 | + |
| 154 | +#endif // Net_AbstractHTTPRequestHandler_INCLUDED |
0 commit comments