mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
request.h
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_REQUEST_H
9 #define SERVLET_REQUEST_H
10 
11 #include <vector>
12 #include <memory>
13 
14 #include <servlet/uri.h>
15 #include <servlet/cookie.h>
16 #include <servlet/session.h>
17 #include <servlet/ssl.h>
18 #include <servlet/lib/io.h>
19 #include <servlet/lib/io_filter.h>
20 #include <servlet/lib/logger.h>
21 
22 namespace servlet
23 {
24 
25 class multipart_input;
26 
36 {
37 public:
38  virtual ~http_request() noexcept {}
39 
52  virtual tree_any_map& get_attributes() = 0;
53 
61  virtual const tree_any_map& get_attributes() const = 0;
62 
81  virtual const std::map<std::string, std::vector<std::string>, std::less<>>& get_parameters() = 0;
82 
93  virtual const std::map<string_view, string_view, std::less<>>& get_env() = 0;
94 
101  virtual bool is_secure() = 0;
102 
114  virtual std::shared_ptr<SSL_information> ssl_information() = 0;
115 
137  template<typename StringType>
138  const optional_ref<const std::string> get_parameter(const StringType& name)
139  {
140  const std::map<std::string, std::vector<std::string>, std::less<>>& params = get_parameters();
141  auto it = params.find(name);
142  return it == params.end() || it->second.empty() ? optional_ref<const std::string>{} :
143  optional_ref<const std::string>{it->second.front()};
144  }
145 
160  template<typename StringType>
162  {
163  const std::map<std::string, std::vector<std::string>, std::less<>>& params = get_parameters();
164  auto it = params.find(name);
165  return it == params.end() ? optional_ref<const std::vector<std::string>>{} :
167  }
168 
169 
184  virtual string_view get_auth_type() = 0;
185 
194  virtual const std::vector<cookie>& get_cookies() = 0;
195 
206  virtual string_view get_context_path() const = 0;
207 
222  virtual string_view get_servlet_path() const = 0;
223 
229  virtual const URI& get_request_uri() const = 0;
230 
247  virtual string_view get_path_info() const = 0;
248 
262  virtual string_view get_header(const std::string& name) const = 0;
263 
283  virtual long get_date_header(const std::string& name) const = 0;
284 
293  virtual string_view get_content_type() const = 0;
294 
303  virtual long get_content_length() const = 0;
304 
315  virtual void get_headers(const std::string& name, std::vector<std::string>& headers) const = 0;
316 
322  virtual void get_headers(std::vector<std::pair<std::string, std::string>>& headers) const = 0;
323 
332  virtual string_view get_method() const = 0;
333 
348  virtual string_view get_path_translated() const = 0;
349 
358  virtual string_view get_scheme() const = 0;
359 
369  virtual string_view get_protocol() const = 0;
370 
371 
380  virtual string_view get_client_addr() const = 0;
381 
392  virtual string_view get_client_host() const = 0;
393 
400  virtual uint16_t get_client_port() const = 0;
401 
412  virtual string_view get_remote_user() const = 0;
413 
421  virtual string_view get_local_addr() const = 0;
422 
430  virtual string_view get_local_host() const = 0;
431 
438  virtual uint16_t get_local_port() const = 0;
439 
440 
448  virtual string_view get_server_name() const = 0;
449 
457  virtual uint16_t get_server_port() const = 0;
458 
479  virtual void forward(const std::string &redirectURI, bool from_context_path = true) = 0;
480 
494  virtual int include(const std::string &includeURI, bool from_context_path = true) = 0;
495 
510  virtual http_session &get_session() = 0;
511 
518  virtual bool has_session() = 0;
519 
523  virtual void invalidate_session() = 0;
524 
536  virtual std::istream& get_input_stream() = 0;
537 
554  virtual multipart_input& get_multipart_input() = 0;
555 
567  virtual bool is_multipart() const = 0;
568 };
569 
596 {
597 public:
598  virtual ~multipart_input() noexcept = default;
599 
605  virtual const std::map<std::string, std::vector<std::string>, std::less<>>& get_headers() const = 0;
606 
617  template<typename StringType>
618  optional_ref<const std::string> get_header(const StringType& name) const
619  {
620  const std::map<std::string, std::vector<std::string>, std::less<>>& headers = get_headers();
621  auto it = headers.find(name);
622  return it == headers.end() || it->second.empty() ? optional_ref<const std::string>{} :
623  optional_ref<const std::string>{it->second.front()};
624  }
625 
635  template<typename StringType>
637  {
638  const std::map<std::string, std::vector<std::string>, std::less<>>& headers = get_headers();
639  auto it = headers.find(name);
640  return it == headers.end() ? optional_ref<const std::vector<std::string>>{} :
642  }
643 
650  optional_ref<const std::string> get_content_type() const { return get_header("Content-Type"); }
651 
658 
667 
674  virtual std::istream& get_input_stream() = 0;
675 
682  virtual bool to_next_part() = 0;
683 };
684 
697 {
698 public:
704  http_request_wrapper(http_request& req) : _req{req} {}
705 
706  ~http_request_wrapper() noexcept override {}
707 
712  http_request& get_wrapped_request() { return _req; }
717  const http_request& get_wrapped_request() const { return _req; }
718 
719  tree_any_map& get_attributes() override { return _req.get_attributes(); }
720  const tree_any_map& get_attributes() const override { return _req.get_attributes(); }
721 
722  const std::map<std::string, std::vector<std::string>, std::less<>>& get_parameters() override
723  { return _req.get_parameters(); }
724  const std::map<string_view, string_view, std::less<>>& get_env() override { return _req.get_env(); };
725  bool is_secure() override { return _req.is_secure(); }
726  std::shared_ptr<SSL_information> ssl_information() override { return _req.ssl_information(); }
727 
728  string_view get_auth_type() override { return _req.get_auth_type(); }
729  const std::vector<cookie>& get_cookies() override { return _req.get_cookies(); }
730  string_view get_context_path() const override { return _req.get_context_path(); }
731  string_view get_servlet_path() const override { return _req.get_servlet_path(); }
732  const URI& get_request_uri() const override { return _req.get_request_uri(); }
733  string_view get_path_info() const override { return _req.get_path_info(); }
734  string_view get_header(const std::string& name) const override { return _req.get_header(name); }
735  long get_date_header(const std::string& name) const override { return _req.get_date_header(name); }
736 
737  string_view get_content_type() const override { return _req.get_content_type(); }
738  long get_content_length() const override { return _req.get_content_length(); }
739 
740  void get_headers(const std::string& name, std::vector<std::string>& headers) const override
741  { return _req.get_headers(name, headers); }
742  void get_headers(std::vector<std::pair<std::string, std::string>>& headers) const override
743  { return _req.get_headers(headers); }
744 
745  string_view get_method() const override { return _req.get_method(); }
746 
747  string_view get_path_translated() const override { return _req.get_method(); }
748  string_view get_scheme() const override { return _req.get_method(); }
749  string_view get_protocol() const override { return _req.get_method(); }
750 
751  string_view get_client_addr() const override { return _req.get_client_addr(); }
752  string_view get_client_host() const override { return _req.get_client_host(); }
753  uint16_t get_client_port() const override { return _req.get_client_port(); }
754  string_view get_remote_user() const override { return _req.get_remote_user(); }
755 
756  string_view get_local_addr() const override { return _req.get_local_addr(); }
757  string_view get_local_host() const override { return _req.get_local_host(); }
758  uint16_t get_local_port() const override { return _req.get_local_port(); }
759 
760  uint16_t get_server_port() const override { return _req.get_server_port(); }
761  string_view get_server_name() const override { return _req.get_server_name(); }
762 
763  void forward(const std::string &redirectURL, bool from_context_path = true) override
764  { _req.forward(redirectURL, from_context_path); }
765  int include(const std::string &includeURL, bool from_context_path = true) override
766  { return _req.include(includeURL, from_context_path); }
767 
768  http_session &get_session() override { return _req.get_session(); }
769  bool has_session() override { return _req.has_session(); }
770  void invalidate_session() override { _req.invalidate_session(); }
771 
772  bool is_multipart() const override { return _req.is_multipart(); }
773 
774  std::istream& get_input_stream() override;
776 
777 protected:
778 
797  virtual in_filter* filter() { return nullptr; }
798 
799 private:
800  http_request& _req;
802  optional_ptr<multipart_input> _multipart_in;
803 };
804 
805 } // end of servlet namespace
806 
807 #endif // SERVLET_REQUEST_H
virtual multipart_input & get_multipart_input()=0
Retrieves the body of the request as binary data if the input is a multipart stream.
bool has_session() override
Returns true if the http_session associated with this request exists.
Definition: request.h:769
string_view get_local_host() const override
Returns the host name of the Internet Protocol (IP) interface on which the request was received...
Definition: request.h:757
long get_date_header(const std::string &name) const override
Returns the value of the specified request header as a long value that represents a Date object...
Definition: request.h:735
multipart_input & get_multipart_input() override
Retrieves the body of the request as binary data if the input is a multipart stream.
virtual bool to_next_part()=0
Moves this multipart_input to the next part.
optional_ref< const std::string > get_name() const
Obtain the name of the field in the multipart form corresponding to this part.
Definition: request.h:657
std::istream & get_input_stream() override
Retrieves the body of the request as binary data using a std::istream.
const optional_ref< const std::string > get_parameter(const StringType &name)
Returns the value of a request parameter as a reference to std::string, or null if the parameter does...
Definition: request.h:138
virtual string_view get_client_addr() const =0
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request...
Defines an object to provide client request information to a servlet.
Definition: request.h:35
const URI & get_request_uri() const override
Returns the URI> with which this request was called.
Definition: request.h:732
virtual string_view get_protocol() const =0
Returns the name and version of the protocol the request uses in the form protocol/majorVersion.minorVersion, for example, HTTP/1.1.
string_view get_path_translated() const override
Returns any extra path information after the servlet name but before the query string, and translates it to a real path.
Definition: request.h:747
string_view get_method() const override
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
Definition: request.h:745
virtual const std::vector< cookie > & get_cookies()=0
Returns an array containing all of the cookie objects the client sent with this request.
virtual long get_content_length() const =0
Returns the length, in bytes, of the request body and made available by the input stream...
Optional reference implementation.
Definition: optional.h:221
virtual string_view get_scheme() const =0
Returns the name of the scheme used to make this request, for example, http or https.
string_view get_servlet_path() const override
Returns the part of this request's URL that calls the servlet.
Definition: request.h:731
optional_ref< const std::string > get_header(const StringType &name) const
Obtains the value of the specified part header as a reference to a std::string.
Definition: request.h:618
string_view get_content_type() const override
Returns the MIME type of the body of the request, or empty string if the type is not known...
Definition: request.h:737
Contains definition of URI class and related structures and operations.
string_view get_header(const std::string &name) const override
Returns the value of the specified request header as a std::string.
Definition: request.h:734
virtual string_view get_server_name() const =0
Returns the host name of the server to which the request was sent.
virtual uint16_t get_client_port() const =0
Returns the Internet Protocol (IP) source port of the client or last proxy that sent the request...
virtual string_view get_path_info() const =0
Returns any extra path information associated with the URL the client sent when it made this request...
optional_ref< const std::string > get_content_type() const
Obtain the content type passed by the browser.
Definition: request.h:650
void get_headers(std::vector< std::pair< std::string, std::string >> &headers) const override
Fills the std::vector with all the request header values.
Definition: request.h:742
uint16_t get_server_port() const override
Returns the port number to which the request was sent.
Definition: request.h:760
Definitions for custom implementation of std::istream and std::ostream objects.
const optional_ref< const std::vector< std::string > > get_parameters(const StringType &name)
Returns all values of a request parameter as a reference to std::vector, or null if the parameters do...
Definition: request.h:161
long get_content_length() const override
Returns the length, in bytes, of the request body and made available by the input stream...
Definition: request.h:738
virtual std::istream & get_input_stream()=0
Obtain an std::itream that can be used to retrieve the contents of the current part.
virtual string_view get_path_translated() const =0
Returns any extra path information after the servlet name but before the query string, and translates it to a real path.
virtual bool has_session()=0
Returns true if the http_session associated with this request exists.
Covenience class on top of associative container to facilitate work with value type std::any...
Definition: any_map.h:84
virtual string_view get_content_type() const =0
Returns the MIME type of the body of the request, or empty string if the type is not known...
virtual const URI & get_request_uri() const =0
Returns the URI> with which this request was called.
string_view get_local_addr() const override
Returns the Internet Protocol (IP) address of the interface on which the request was received...
Definition: request.h:756
string_view get_client_addr() const override
Returns the Internet Protocol (IP) address of the client or last proxy that sent the request...
Definition: request.h:751
const tree_any_map & get_attributes() const override
Const version of call get_attributes() const.
Definition: request.h:720
Provides a convenient implementation of the http_request interface that can be subclassed by develope...
Definition: request.h:696
virtual in_filter * filter()
Provides input filter for the http_request::get_input_stream or http_request::get_multipart_input.
Definition: request.h:797
uint16_t get_client_port() const override
Returns the Internet Protocol (IP) source port of the client or last proxy that sent the request...
Definition: request.h:753
std::shared_ptr< SSL_information > ssl_information() override
Returns information about SSL connection if it is established.
Definition: request.h:726
virtual void get_headers(const std::string &name, std::vector< std::string > &headers) const =0
Fills the std::vector with all the header values associated with the specified header name...
string_view get_client_host() const override
Returns the fully qualified name of the client or the last proxy that sent the request if available...
Definition: request.h:752
tree_any_map & get_attributes() override
Returns the attributes map associated with this request.
Definition: request.h:719
virtual std::istream & get_input_stream()=0
Retrieves the body of the request as binary data using a std::istream.
string_view get_remote_user() const override
Returns the login of the user making this request, if the user has been authenticated, or empty view if the user has not been authenticated.
Definition: request.h:754
const std::vector< cookie > & get_cookies() override
Returns an array containing all of the cookie objects the client sent with this request.
Definition: request.h:729
bool is_multipart() const override
Returns true if current request is multipart.
Definition: request.h:772
virtual tree_any_map & get_attributes()=0
Returns the attributes map associated with this request.
const http_request & get_wrapped_request() const
Returns wrapped request innstance.
Definition: request.h:717
void get_headers(const std::string &name, std::vector< std::string > &headers) const override
Fills the std::vector with all the header values associated with the specified header name...
Definition: request.h:740
http_request_wrapper(http_request &req)
Constructs a request object wrapping the given request.
Definition: request.h:704
Provides a way to identify a user across more than one page request or visit to a Web site and to sto...
Definition: session.h:56
virtual void invalidate_session()=0
Invalidates the session assosiated with this request if it exists.
virtual bool is_multipart() const =0
Returns true if current request is multipart.
virtual string_view get_remote_user() const =0
Returns the login of the user making this request, if the user has been authenticated, or empty view if the user has not been authenticated.
string_view get_path_info() const override
Returns any extra path information associated with the URL the client sent when it made this request...
Definition: request.h:733
virtual void forward(const std::string &redirectURI, bool from_context_path=true)=0
Redirects the request to a specified local URI within the server.
virtual uint16_t get_local_port() const =0
Returns the Internet Protocol (IP) port number of the interface on which the request was received...
int include(const std::string &includeURL, bool from_context_path=true) override
Includes the response of the specified local URI into the current response.
Definition: request.h:765
Represents a Uniform Resource Identifier (URI) reference.
Definition: uri.h:258
virtual string_view get_method() const =0
Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
This class represents a multipart input stream of a multipart/form-data request body.
Definition: request.h:595
void invalidate_session() override
Invalidates the session assosiated with this request if it exists.
Definition: request.h:770
virtual string_view get_header(const std::string &name) const =0
Returns the value of the specified request header as a std::string.
bool is_secure() override
Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
Definition: request.h:725
string_view get_scheme() const override
Returns the name of the scheme used to make this request, for example, http or https.
Definition: request.h:748
virtual int include(const std::string &includeURI, bool from_context_path=true)=0
Includes the response of the specified local URI into the current response.
Contains definitions for the logging framework.
http_request & get_wrapped_request()
Returns wrapped request innstance.
Definition: request.h:712
string_view get_context_path() const override
Returns the portion of the request URI that indicates the context of the request. ...
Definition: request.h:730
optional_ref< const std::string > get_submitted_filename() const
If this part represents an uploaded file, gets the file name submitted in the upload.
virtual string_view get_client_host() const =0
Returns the fully qualified name of the client or the last proxy that sent the request if available...
virtual const std::map< string_view, string_view, std::less<> > & get_env()=0
Returns all the environment variables accessible from this request.
virtual string_view get_local_addr() const =0
Returns the Internet Protocol (IP) address of the interface on which the request was received...
virtual string_view get_context_path() const =0
Returns the portion of the request URI that indicates the context of the request. ...
optional_ref< const std::vector< std::string > > get_headers(const StringType &name) const
Obtain all the values of the specified part header.
Definition: request.h:636
string_view get_auth_type() override
Returns the name of the authentication scheme used to protect the servlet.
Definition: request.h:728
string_view get_protocol() const override
Returns the name and version of the protocol the request uses in the form protocol/majorVersion.minorVersion, for example, HTTP/1.1.
Definition: request.h:749
string_view get_server_name() const override
Returns the host name of the server to which the request was sent.
Definition: request.h:761
Definitions for input and output filters and related types.
const std::map< string_view, string_view, std::less<> > & get_env() override
Returns all the environment variables accessible from this request.
Definition: request.h:724
http_session & get_session() override
Returns the current http_session associated with this request or, if there is no current session retu...
Definition: request.h:768
virtual string_view get_local_host() const =0
Returns the host name of the Internet Protocol (IP) interface on which the request was received...
virtual http_session & get_session()=0
Returns the current http_session associated with this request or, if there is no current session retu...
void forward(const std::string &redirectURL, bool from_context_path=true) override
Redirects the request to a specified local URI within the server.
Definition: request.h:763
uint16_t get_local_port() const override
Returns the Internet Protocol (IP) port number of the interface on which the request was received...
Definition: request.h:758
virtual bool is_secure()=0
Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
virtual const std::map< std::string, std::vector< std::string >, std::less<> > & get_parameters()=0
Returns all the parameters of this request.
virtual string_view get_servlet_path() const =0
Returns the part of this request's URL that calls the servlet.
virtual const std::map< std::string, std::vector< std::string >, std::less<> > & get_headers() const =0
Obtain all the headers of the curent part.
virtual std::shared_ptr< SSL_information > ssl_information()=0
Returns information about SSL connection if it is established.
virtual uint16_t get_server_port() const =0
Returns the port number to which the request was sent.
virtual string_view get_auth_type()=0
Returns the name of the authentication scheme used to protect the servlet.
virtual long get_date_header(const std::string &name) const =0
Returns the value of the specified request header as a long value that represents a Date object...
Abstract interface for input filter.
Definition: io_filter.h:93
const std::map< std::string, std::vector< std::string >, std::less<> > & get_parameters() override
Returns all the parameters of this request.
Definition: request.h:722