1 | // |
---|
2 | // AbstractHTTPRequestHandler.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/AbstractHTTPRequestHandler.h#1 $ |
---|
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 | /// If run() throws an exception and the response has not been |
---|
88 | /// sent yet, sends a 500 (Internal Server Error) response with |
---|
89 | /// the exception's display text. |
---|
90 | |
---|
91 | HTTPServerRequest& request(); |
---|
92 | /// Returns the request. |
---|
93 | |
---|
94 | HTTPServerResponse& response(); |
---|
95 | /// Returns the response. |
---|
96 | |
---|
97 | HTMLForm& form(); |
---|
98 | /// Returns a HTMLForm for the given request. |
---|
99 | /// The HTMLForm object is created when this |
---|
100 | /// member function is executed the first time. |
---|
101 | |
---|
102 | void sendErrorResponse(HTTPResponse::HTTPStatus status, const std::string& message); |
---|
103 | /// Sends a HTML error page for the given status code. |
---|
104 | /// The given message is added to the page: |
---|
105 | /// <HTML> |
---|
106 | /// <HEAD> |
---|
107 | /// <TITLE>status - reason</TITLE> |
---|
108 | /// </HEAD> |
---|
109 | /// <BODY> |
---|
110 | /// <H1>status - reason</H1> |
---|
111 | /// <P>message</P> |
---|
112 | /// </BODY> |
---|
113 | /// </HTML> |
---|
114 | |
---|
115 | protected: |
---|
116 | virtual void run() = 0; |
---|
117 | /// Must be overridden by subclasses. |
---|
118 | /// |
---|
119 | /// Handles the given request. |
---|
120 | |
---|
121 | virtual bool authenticate(); |
---|
122 | /// Check authentication; returns true if okay, false if failed to authenticate. |
---|
123 | /// The default implementation always returns true. |
---|
124 | /// |
---|
125 | /// Subclasses can override this member function to perform |
---|
126 | /// some form of client or request authentication before |
---|
127 | /// the request is actually handled. |
---|
128 | |
---|
129 | private: |
---|
130 | HTTPServerRequest* _pRequest; |
---|
131 | HTTPServerResponse* _pResponse; |
---|
132 | HTMLForm* _pForm; |
---|
133 | }; |
---|
134 | |
---|
135 | |
---|
136 | // |
---|
137 | // inlines |
---|
138 | // |
---|
139 | inline HTTPServerRequest& AbstractHTTPRequestHandler::request() |
---|
140 | { |
---|
141 | poco_check_ptr (_pRequest); |
---|
142 | |
---|
143 | return *_pRequest; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | inline HTTPServerResponse& AbstractHTTPRequestHandler::response() |
---|
148 | { |
---|
149 | poco_check_ptr (_pResponse); |
---|
150 | |
---|
151 | return *_pResponse; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | } } // namespace Poco::Net |
---|
156 | |
---|
157 | |
---|
158 | #endif // Net_AbstractHTTPRequestHandler_INCLUDED |
---|