mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
servlet.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2016 Alexei Novakov
3 https://github.com/novalexei
4 
5 Distributed under the Boost Software License, Version 1.0.
6 http://boost.org/LICENSE_1_0.txt
7 */
8 #ifndef SERVLET_SERVLET_H
9 #define SERVLET_SERVLET_H
10 
16 #include <string>
17 
18 #include <servlet/request.h>
19 #include <servlet/response.h>
20 #include <servlet/context.h>
21 
27 #define SERVLET_EXPORT(factoryName, className) extern "C" servlet::http_servlet* factoryName() { return new className{}; }
28 
29 namespace servlet
30 {
31 
94 {
95 public:
96  http_servlet() = default;
97  virtual ~http_servlet() noexcept = default;
98 
116  virtual void init(servlet_config& config);
117 
125  virtual void init() {}
126 
136  virtual void service(http_request& req, http_response& resp);
137 
150  const servlet_config &get_servlet_config() const { return *_cfg; }
151 
160  const std::string &get_servlet_name() const { return _cfg->get_servlet_name(); }
161 
174  const std::map<std::string, std::string, std::less<>>& get_init_parameters() const
175  { return _cfg->get_init_parameters(); };
176 
190  template <typename KeyType>
192  { return _cfg->get_init_parameter(name); }
193 
194 protected:
233  virtual void do_get(http_request& req, http_response& resp);
234 
271  virtual void do_post(http_request& req, http_response& resp);
272 
294  virtual void do_put(http_request& req, http_response& resp);
295 
313  virtual void do_delete(http_request& req, http_response& resp);
332  virtual void do_head(http_request& req, http_response& resp);
333 
344  virtual void do_trace(http_request& req, http_response& resp);
345 
362  virtual void do_options(http_request& req, http_response& resp);
363 
380  virtual long get_last_modified(http_request& req);
381 
386  constexpr static int GET_ALLOWED = 1;
391  constexpr static int POST_ALLOWED = 1 << 1;
396  constexpr static int PUT_ALLOWED = 1 << 2;
401  constexpr static int DELETE_ALLOWED = 1 << 3;
406  constexpr static int HEAD_ALLOWED = 1 << 4;
411  constexpr static int TRACE_ALLOWED = 1 << 5;
416  constexpr static int OPTIONS_ALLOWED = 1 << 6;
417 
434  virtual int get_allowed_methods();
435 
436 private:
437  void _maybe_set_last_modified(http_response &resp, long lastModifiedSec);
438 
439  static const std::string METHOD_DELETE;
440  static const std::string METHOD_HEAD;
441  static const std::string METHOD_GET;
442  static const std::string METHOD_OPTIONS;
443  static const std::string METHOD_POST;
444  static const std::string METHOD_PUT;
445  static const std::string METHOD_TRACE;
446 
447  static const std::string HEADER_IFMODSINCE;
448  static const std::string HEADER_LASTMOD;
449 
450  servlet_config* _cfg;
451 };
452 
453 }
454 
455 #endif // SERVLET_SERVLET_H
virtual void do_trace(http_request &req, http_response &resp)
Called by the server (via the service method) to allow a servlet to handle a TRACE request...
Defines an object to provide client request information to a servlet.
Definition: request.h:35
static constexpr int HEAD_ALLOWED
Constant to indicate that HEAD method is allowed.
Definition: servlet.h:406
static constexpr int DELETE_ALLOWED
Constant to indicate that DELETE method is allowed.
Definition: servlet.h:401
Optional reference implementation.
Definition: optional.h:221
virtual int get_allowed_methods()
This method is needed by do_options, which adds all allowed by the servlet methods.
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site...
Definition: servlet.h:93
virtual void do_delete(http_request &req, http_response &resp)
Called by the server (via the service method) to allow a servlet to handle a DELETE request...
static constexpr int POST_ALLOWED
Constant to indicate that POST method is allowed.
Definition: servlet.h:391
optional_ref< const std::string > get_init_parameter(const KeyType &name) const
Returns an optional std::string containing the value of the named initialization parameter, or null if the parameter does not exist.
Definition: servlet.h:191
virtual void do_put(http_request &req, http_response &resp)
Called by the server (via the service method) to allow a servlet to handle a PUT request.
virtual long get_last_modified(http_request &req)
Returns the time the http_request object was last modified, in milliseconds since midnight January 1...
static constexpr int OPTIONS_ALLOWED
Constant to indicate that OPTIONS method is allowed.
Definition: servlet.h:416
virtual void do_post(http_request &req, http_response &resp)
Called by the server (via the service method) to allow a servlet to handle a POST request...
const std::map< std::string, std::string, std::less<> > & get_init_parameters() const
Returns the all of the servlet's initialization parameters as an tree_map of std::string objects...
Definition: servlet.h:174
virtual void do_head(http_request &req, http_response &resp)
const std::string & get_servlet_name() const
Returns the name of this servlet instance.
Definition: servlet.h:160
const std::string & get_servlet_name() const
Returns the name of this servlet instance.
Definition: context.h:272
static constexpr int TRACE_ALLOWED
Constant to indicate that TRACE method is allowed.
Definition: servlet.h:411
A servlet configuration object used by a servlet container to pass information to a servlet during in...
Definition: context.h:243
virtual void do_get(http_request &req, http_response &resp)
Called by the server (via the service method) to allow a servlet to handle a GET request.
const servlet_config & get_servlet_config() const
Returns a servlet_config object, which contains initialization and startup parameters for this servle...
Definition: servlet.h:150
static constexpr int PUT_ALLOWED
Constant to indicate that PUT method is allowed.
Definition: servlet.h:396
virtual void init()
A convenience method which can be overridden so that there's no need to call init(config).
Definition: servlet.h:125
const std::map< std::string, std::string, std::less<> > & get_init_parameters() const
Returns all the servlet's initialization parameters as a tree_map.
Definition: context.h:304
virtual void service(http_request &req, http_response &resp)
Receives standard HTTP requests from the public service method and dispatches them to the do_method m...
Defines an object to assist a servlet in sending a response to the client.
Definition: response.h:33
optional_ref< const std::string > get_init_parameter(const KeyType &key) const
Returns a std::string containing the value of the named initialization parameter, or empty_reference ...
Definition: context.h:295
virtual void do_options(http_request &req, http_response &resp)
Called by the server (via the service method) to allow a servlet to handle a OPTIONS request...
static constexpr int GET_ALLOWED
Constant to indicate that GET method is allowed.
Definition: servlet.h:386