Servlet.h
1/*
2 * Copyright (C) 2011 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_Http_Servlet_h
30#define Pt_Http_Servlet_h
31
32#include <Pt/Http/Api.h>
33#include <Pt/NonCopyable.h>
34#include <string>
35
36namespace Pt {
37
38namespace Http {
39
40class Authorizer;
41class Request;
42class Reply;
43class Server;
44class Service;
45
79class PT_HTTP_API Servlet : private NonCopyable
80{
81 // @internal
82 friend class Server;
83
84 public:
88
92
95 virtual ~Servlet();
96
99 void setShutdown(bool shutdown = true);
100
103 bool isIdle();
104
107 void detach();
108
111 bool isMapped(const Request& request) const
112 { return this->onRequest(request); }
113
117 { return _service; }
118
122 { return _auth; }
123
124 protected:
127 virtual bool onRequest(const Request& request) const = 0;
128
129 private:
130 // @internal
131 void registerServer(Server& server);
132
133 // @internal
134 void unregisterServer(Server& server);
135
136 private:
137 Server* _server;
138 Service* _service;
139 Authorizer* _auth;
140};
141
146class PT_HTTP_API MapUrl : public Servlet
147{
148 public:
151 MapUrl(const std::string& url, Service& s)
152 : Servlet(s)
153 , _url(url)
154 {}
155
158 MapUrl(const std::string& url, Service& s, Authorizer& a)
159 : Servlet(s, a)
160 , _url(url)
161 {}
162
163 protected:
164 bool onRequest(const Request& request) const;
165
166 private:
167 std::string _url;
168};
169
174class PT_HTTP_API MapAny : public Servlet
175{
176 public:
180 : Servlet(s)
181 {}
182
186 : Servlet(s, a)
187 {}
188
189 protected:
190 bool onRequest(const Request& request) const;
191};
192
193} // namespace Http
194
195} // namespace Pt
196
197#endif // Pt_Http_Servlet_h
Server-side authorization.
Definition Authorizer.h:145
bool onRequest(const Request &request) const
Returns true if the servlet should process the request.
MapAny(Service &s)
Construct with service.
Definition Servlet.h:179
MapAny(Service &s, Authorizer &a)
Construct with service and authorizer.
Definition Servlet.h:185
bool onRequest(const Request &request) const
Returns true if the servlet should process the request.
MapUrl(const std::string &url, Service &s)
Construct with url to map to a service.
Definition Servlet.h:151
MapUrl(const std::string &url, Service &s, Authorizer &a)
Construct with url to map to a service.
Definition Servlet.h:158
HTTP reply message.
Definition Reply.h:64
HTTP request message.
Definition Request.h:65
Listening HTTP server.
Definition Server.h:83
Factory for request responders.
Definition Service.h:77
void setShutdown(bool shutdown=true)
Set shutdown flag.
Authorizer * authorizer()
Returns the authorizer.
Definition Servlet.h:121
Servlet(Service &s)
Construct with service.
bool isIdle()
Returns true if not in use.
bool isMapped(const Request &request) const
Returns true if request is mapped to the service.
Definition Servlet.h:111
Service * service()
Returns the service to map requests to.
Definition Servlet.h:116
void detach()
Detach from server.
virtual bool onRequest(const Request &request) const =0
Returns true if the servlet should process the request.
virtual ~Servlet()
Destructor.
Servlet(Service &s, Authorizer &a)
Construct with service and authorizer.
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
HTTP clients and servers.
Core module.
Definition Allocator.h:33