mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
response.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_RESPONSE_H
9 #define SERVLET_RESPONSE_H
10 
11 #include <ostream>
12 #include <chrono>
13 #include <experimental/string_view>
14 
15 #include <servlet/cookie.h>
16 #include <servlet/lib/io.h>
17 #include <servlet/lib/io_filter.h>
18 #include <servlet/lib/optional.h>
19 
20 namespace servlet
21 {
22 
23 using std::experimental::string_view;
24 
34 {
35 public:
36  virtual ~http_response() noexcept {}
37 
44  virtual void add_cookie(const cookie& c) = 0;
45 
55  virtual void add_header(const std::string &name, const std::string &value) = 0;
56 
68  template<typename Clock, typename Dur>
69  void add_date_header(const std::string &name, const std::chrono::time_point<Clock, Dur> &date)
70  {
71  add_date_header(name, std::chrono::duration_cast<std::chrono::seconds>(date.time_since_epoch()).count());
72  }
73 
83  virtual void add_date_header(const std::string &name, long timeSec) = 0;
84 
97  virtual void set_header(const std::string &name, const std::string &value) = 0;
98 
113  template<typename Clock, typename Dur>
114  void set_date_header(const std::string &name, const std::chrono::time_point<Clock, Dur> &date)
115  {
116  set_date_header(name, std::chrono::duration_cast<std::chrono::seconds>(date.time_since_epoch()).count());
117  }
130  virtual void set_date_header(const std::string &name, long date) = 0;
131 
140  virtual bool contains_header(const std::string &name) const = 0;
141 
155  virtual string_view get_header(const std::string& name) const = 0;
156 
167  virtual long get_date_header(const std::string& name) const = 0;
168 
178  virtual void get_headers(const std::string& name, std::vector<std::string>& headers) const = 0;
179 
185  virtual void get_headers(std::vector<std::pair<std::string, std::string>>& headers) const = 0;
186 
196  virtual string_view get_content_type() const = 0;
197 
213  virtual void set_content_type(const std::string &type) = 0;
214 
215 
224  virtual void set_content_length(std::size_t len) = 0;
225 
241  virtual void send_redirect(const std::string &redirectURL) = 0;
242 
253  virtual void set_status(int sc) = 0;
254 
260  virtual int get_status() const = 0;
261 
268  virtual std::ostream& get_output_stream() = 0;
269 
270  /*
271  * Server status codes; see RFC 2068.
272  */
273 
277  static constexpr int SC_CONTINUE = 100;
278 
283  static constexpr int SC_SWITCHING_PROTOCOLS = 101;
284 
285 
289  static constexpr int SC_OK = 200;
290 
295  static constexpr int SC_CREATED = 201;
296 
301  static constexpr int SC_ACCEPTED = 202;
302 
307  static constexpr int SC_NON_AUTHORITATIVE_INFORMATION = 203;
308 
313  static constexpr int SC_NO_CONTENT = 204;
314 
319  static constexpr int SC_RESET_CONTENT = 205;
320 
325  static constexpr int SC_PARTIAL_CONTENT = 206;
326 
327 
332  static constexpr int SC_MULTIPLE_CHOICES = 300;
333 
338  static constexpr int SC_MOVED_PERMANENTLY = 301;
339 
346  static constexpr int SC_MOVED_TEMPORARILY = 302;
347 
355  static constexpr int SC_FOUND = 302;
356 
361  static constexpr int SC_SEE_OTHER = 303;
362 
367  static constexpr int SC_NOT_MODIFIED = 304;
368 
373  static constexpr int SC_USE_PROXY = 305;
374 
380  static constexpr int SC_TEMPORARY_REDIRECT = 307;
381 
382 
387  static constexpr int SC_BAD_REQUEST = 400;
388 
393  static constexpr int SC_UNAUTHORIZED = 401;
394 
398  static constexpr int SC_PAYMENT_REQUIRED = 402;
399 
404  static constexpr int SC_FORBIDDEN = 403;
405 
410  static constexpr int SC_NOT_FOUND = 404;
411 
417  static constexpr int SC_METHOD_NOT_ALLOWED = 405;
418 
425  static constexpr int SC_NOT_ACCEPTABLE = 406;
426 
431  static constexpr int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
432 
437  static constexpr int SC_REQUEST_TIMEOUT = 408;
438 
443  static constexpr int SC_CONFLICT = 409;
444 
450  static constexpr int SC_GONE = 410;
451 
456  static constexpr int SC_LENGTH_REQUIRED = 411;
457 
463  static constexpr int SC_PRECONDITION_FAILED = 412;
464 
470  static constexpr int SC_REQUEST_ENTITY_TOO_LARGE = 413;
471 
477  static constexpr int SC_REQUEST_URI_TOO_LONG = 414;
478 
484  static constexpr int SC_UNSUPPORTED_MEDIA_TYPE = 415;
485 
490  static constexpr int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
491 
496  static constexpr int SC_EXPECTATION_FAILED = 417;
497 
498 
503  static constexpr int SC_INTERNAL_SERVER_ERROR = 500;
504 
509  static constexpr int SC_NOT_IMPLEMENTED = 501;
510 
515  static constexpr int SC_BAD_GATEWAY = 502;
516 
521  static constexpr int SC_SERVICE_UNAVAILABLE = 503;
522 
527  static constexpr int SC_GATEWAY_TIMEOUT = 504;
528 
533  static constexpr int SC_HTTP_VERSION_NOT_SUPPORTED = 505;
534 };
535 
546 {
547 public:
548 
554  http_response_wrapper(http_response&resp) : _resp{resp} {}
559 
564  http_response& get_wrapped_request() { return _resp; }
569  const http_response& get_wrapped_request() const { return _resp; }
570 
571  void add_cookie(const cookie& c) override { _resp.add_cookie(c); }
572  void add_header(const std::string &name, const std::string &value) override { _resp.add_header(name, value); }
573  void add_date_header(const std::string &name, long timeSec) override { _resp.add_date_header(name, timeSec); }
574  void set_header(const std::string &name, const std::string &value) override { _resp.set_header(name, value); }
575  void set_date_header(const std::string &name, long timeSec) override { _resp.set_date_header(name, timeSec); }
576  bool contains_header(const std::string &name) const override { return _resp.contains_header(name); }
577 
578  string_view get_header(const std::string& name) const override { return _resp.get_header(name); }
579  long get_date_header(const std::string& name) const override { return _resp.get_date_header(name); }
580  void get_headers(const std::string& name, std::vector<std::string>& headers) const override
581  { _resp.get_headers(name, headers); }
582  void get_headers(std::vector<std::pair<std::string, std::string>>& headers) const override
583  { _resp.get_headers(headers); }
584 
585  string_view get_content_type() const override { return _resp.get_content_type(); }
586  void set_content_type(const std::string &content_type) override { _resp.set_content_type(content_type); }
587  void set_content_length(std::size_t content_length) override { _resp.set_content_length(content_length); }
588 
589  void send_redirect(const std::string &redirectURL) override { _resp.send_redirect(redirectURL); }
590 
591  void set_status(int sc) override { _resp.set_status(sc); }
592  int get_status() const override { return _resp.get_status(); }
593 
594  std::ostream& get_output_stream() override;
595 protected:
596 
611  virtual out_filter *filter() { return nullptr; }
612 
613 private:
614  http_response& _resp;
616 };
617 
618 } // end of servlet namespace
619 
620 #endif // SERVLET_RESPONSE_H
http_response & get_wrapped_request()
Returns wrapped response innstance.
Definition: response.h:564
static constexpr int SC_FOUND
Status code (302) indicating that the resource reside temporarily under a different URI...
Definition: response.h:355
virtual out_filter * filter()
Provides output filter for the http_response::get_output_stream.
Definition: response.h:611
virtual string_view get_content_type() const =0
Returns the content type used for the MIME body sent in this response.
void add_header(const std::string &name, const std::string &value) override
Adds a response header with the given name and value.
Definition: response.h:572
virtual bool contains_header(const std::string &name) const =0
Returns a boolean indicating whether the named response header has already been set.
static constexpr int SC_BAD_REQUEST
Status code (400) indicating the request sent by the client was syntactically incorrect.
Definition: response.h:387
Defines optional container objects and related methods.
static constexpr int SC_MULTIPLE_CHOICES
Status code (300) indicating that the requested resource corresponds to any one of a set of represent...
Definition: response.h:332
http_response_wrapper(http_response &resp)
Constructs a response adaptor wrapping the given response.
Definition: response.h:554
void add_date_header(const std::string &name, const std::chrono::time_point< Clock, Dur > &date)
Adds a response header with the given name and date-value.
Definition: response.h:69
virtual string_view get_header(const std::string &name) const =0
Return the value for the specified header, or empty string if this header has not been set...
static constexpr int SC_HTTP_VERSION_NOT_SUPPORTED
Status code (505) indicating that the server does not support or refuses to support the HTTP protocol...
Definition: response.h:533
static constexpr int SC_NOT_ACCEPTABLE
Status code (406) indicating that the resource identified by the request is only capable of generatin...
Definition: response.h:425
virtual void set_content_length(std::size_t len)=0
Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Conte...
static constexpr int SC_UNSUPPORTED_MEDIA_TYPE
Status code (415) indicating that the server is refusing to service the request because the entity of...
Definition: response.h:484
virtual void set_status(int sc)=0
Sets the status code for this response.
void set_date_header(const std::string &name, long timeSec) override
Sets a response header with the given name and date-value.
Definition: response.h:575
static constexpr int SC_RESET_CONTENT
Status code (205) indicating that the agent SHOULD reset the document view which caused the request t...
Definition: response.h:319
virtual void set_header(const std::string &name, const std::string &value)=0
Sets a response header with the given name and value.
static constexpr int SC_PRECONDITION_FAILED
Status code (412) indicating that the precondition given in one or more of the request-header fields ...
Definition: response.h:463
static constexpr int SC_REQUEST_ENTITY_TOO_LARGE
Status code (413) indicating that the server is refusing to process the request because the request e...
Definition: response.h:470
static constexpr int SC_INTERNAL_SERVER_ERROR
Status code (500) indicating an error inside the HTTP server which prevented it from fulfilling the r...
Definition: response.h:503
void set_content_length(std::size_t content_length) override
Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Conte...
Definition: response.h:587
static constexpr int SC_NO_CONTENT
Status code (204) indicating that the request succeeded but that there was no new information to retu...
Definition: response.h:313
Definitions for custom implementation of std::istream and std::ostream objects.
static constexpr int SC_PAYMENT_REQUIRED
Status code (402) reserved for future use.
Definition: response.h:398
void set_header(const std::string &name, const std::string &value) override
Sets a response header with the given name and value.
Definition: response.h:574
int get_status() const override
Get the HTTP status code for this Response.
Definition: response.h:592
void set_date_header(const std::string &name, const std::chrono::time_point< Clock, Dur > &date)
Sets a response header with the given name and date-value.
Definition: response.h:114
static constexpr int SC_LENGTH_REQUIRED
Status code (411) indicating that the request cannot be handled without a defined Content-Length...
Definition: response.h:456
Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server.
Definition: cookie.h:46
string_view get_content_type() const override
Returns the content type used for the MIME body sent in this response.
Definition: response.h:585
static constexpr int SC_NOT_MODIFIED
Status code (304) indicating that a conditional GET operation found that the resource was available a...
Definition: response.h:367
virtual void send_redirect(const std::string &redirectURL)=0
Sends a temporary redirect response to the client using the specified redirect location URL...
~http_response_wrapper() override
Overridden destructor.
Definition: response.h:558
void send_redirect(const std::string &redirectURL) override
Sends a temporary redirect response to the client using the specified redirect location URL...
Definition: response.h:589
static constexpr int SC_SEE_OTHER
Status code (303) indicating that the response to the request can be found under a different URI...
Definition: response.h:361
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...
static constexpr int SC_PROXY_AUTHENTICATION_REQUIRED
Status code (407) indicating that the client MUST first authenticate itself with the proxy...
Definition: response.h:431
long get_date_header(const std::string &name) const override
Return the date value for the specified header, or -1 if this header has not been set...
Definition: response.h:579
virtual std::ostream & get_output_stream()=0
Returns a std::ostream suitable for writing binary data in the response.
static constexpr int SC_GONE
Status code (410) indicating that the resource is no longer available at the server and no forwarding...
Definition: response.h:450
const http_response & get_wrapped_request() const
Returns wrapped response innstance.
Definition: response.h:569
static constexpr int SC_CONTINUE
Status code (100) indicating the client can continue.
Definition: response.h:277
static constexpr int SC_REQUEST_TIMEOUT
Status code (408) indicating that the client did not produce a request within the time that the serve...
Definition: response.h:437
static constexpr int SC_CONFLICT
Status code (409) indicating that the request could not be completed due to a conflict with the curre...
Definition: response.h:443
virtual void set_content_type(const std::string &type)=0
Sets the content type of the response being sent to the client, if the response has not been committe...
static constexpr int SC_SERVICE_UNAVAILABLE
Status code (503) indicating that the HTTP server is temporarily overloaded, and unable to handle the...
Definition: response.h:521
static constexpr int SC_REQUEST_URI_TOO_LONG
Status code (414) indicating that the server is refusing to service the request because the Request-U...
Definition: response.h:477
static constexpr int SC_EXPECTATION_FAILED
Status code (417) indicating that the server could not meet the expectation given in the Expect reque...
Definition: response.h:496
static constexpr int SC_FORBIDDEN
Status code (403) indicating the server understood the request but refused to fulfill it...
Definition: response.h:404
bool contains_header(const std::string &name) const override
Returns a boolean indicating whether the named response header has already been set.
Definition: response.h:576
static constexpr int SC_PARTIAL_CONTENT
Status code (206) indicating that the server has fulfilled the partial GET request for the resource...
Definition: response.h:325
void get_headers(std::vector< std::pair< std::string, std::string >> &headers) const override
Fills the std::vector with all the response header values.
Definition: response.h:582
static constexpr int SC_TEMPORARY_REDIRECT
Status code (307) indicating that the requested resource resides temporarily under a different URI...
Definition: response.h:380
static constexpr int SC_OK
Status code (200) indicating the request succeeded normally.
Definition: response.h:289
static constexpr int SC_UNAUTHORIZED
Status code (401) indicating that the request requires HTTP authentication.
Definition: response.h:393
void set_content_type(const std::string &content_type) override
Sets the content type of the response being sent to the client, if the response has not been committe...
Definition: response.h:586
Abstract interface for output filter.
Definition: io_filter.h:72
static constexpr int SC_CREATED
Status code (201) indicating the request succeeded and created a new resource on the server...
Definition: response.h:295
virtual long get_date_header(const std::string &name) const =0
Return the date value for the specified header, or -1 if this header has not been set...
static constexpr int SC_MOVED_PERMANENTLY
Status code (301) indicating that the resource has permanently moved to a new location, and that future references should use a new URI with their requests.
Definition: response.h:338
std::ostream & get_output_stream() override
Returns a std::ostream suitable for writing binary data in the response.
Definitions for input and output filters and related types.
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: response.h:580
void set_status(int sc) override
Sets the status code for this response.
Definition: response.h:591
Provides a convenient implementation of the http_response interface that can be subclassed by develop...
Definition: response.h:545
static constexpr int SC_GATEWAY_TIMEOUT
Status code (504) indicating that the server did not receive a timely response from the upstream serv...
Definition: response.h:527
virtual void add_cookie(const cookie &c)=0
Adds the specified cookie to the response.
static constexpr int SC_REQUESTED_RANGE_NOT_SATISFIABLE
Status code (416) indicating that the server cannot serve the requested byte range.
Definition: response.h:490
static constexpr int SC_NON_AUTHORITATIVE_INFORMATION
Status code (203) indicating that the meta information presented by the client did not originate from...
Definition: response.h:307
static constexpr int SC_NOT_FOUND
Status code (404) indicating that the requested resource is not available.
Definition: response.h:410
string_view get_header(const std::string &name) const override
Return the value for the specified header, or empty string if this header has not been set...
Definition: response.h:578
Defines an object to assist a servlet in sending a response to the client.
Definition: response.h:33
static constexpr int SC_METHOD_NOT_ALLOWED
Status code (405) indicating that the method specified in the Request-Line is not allowed for the res...
Definition: response.h:417
static constexpr int SC_ACCEPTED
Status code (202) indicating that a request was accepted for processing, but was not completed...
Definition: response.h:301
virtual void add_header(const std::string &name, const std::string &value)=0
Adds a response header with the given name and value.
void add_cookie(const cookie &c) override
Adds the specified cookie to the response.
Definition: response.h:571
void add_date_header(const std::string &name, long timeSec) override
Adds a response header with the given name and date-value.
Definition: response.h:573
static constexpr int SC_BAD_GATEWAY
Status code (502) indicating that the HTTP server received an invalid response from a server it consu...
Definition: response.h:515
virtual int get_status() const =0
Get the HTTP status code for this Response.
static constexpr int SC_USE_PROXY
Status code (305) indicating that the requested resource MUST be accessed through the proxy given by ...
Definition: response.h:373
static constexpr int SC_SWITCHING_PROTOCOLS
Status code (101) indicating the server is switching protocols according to Upgrade header...
Definition: response.h:283
static constexpr int SC_NOT_IMPLEMENTED
Status code (501) indicating the HTTP server does not support the functionality needed to fulfill the...
Definition: response.h:509
static constexpr int SC_MOVED_TEMPORARILY
Status code (302) indicating that the resource has temporarily moved to another location, but that future references should still use the original URI to access the resource.
Definition: response.h:346