Fawkes API  Fawkes Development Version
reply.h
1 
2 /***************************************************************************
3  * reply.h - Web request reply
4  *
5  * Created: Wed Oct 22 18:49:35 2008
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifndef _LIBS_WEBVIEW_REPLY_H_
24 #define _LIBS_WEBVIEW_REPLY_H_
25 
26 #include <map>
27 #include <string>
28 
29 namespace fawkes {
30 
31 class WebRequest;
32 
33 class WebReply
34 {
35 public:
36  /** HTTP response code. */
37  typedef enum {
38  HTTP_CONTINUE = 100, /**< CONTINUE */
39  HTTP_SWITCHING_PROTOCOLS = 101, /**< SWITCHING_PROTOCOLS */
40  HTTP_PROCESSING = 102, /**< PROCESSING */
41 
42  HTTP_OK = 200, /**< OK */
43  HTTP_CREATED = 201, /**< CREATED */
44  HTTP_ACCEPTED = 202, /**< ACCEPTED */
45  HTTP_NON_AUTHORITATIVE_INFORMATION = 203, /**< NON_AUTHORITATIVE_INFORMATION */
46  HTTP_NO_CONTENT = 204, /**< NO_CONTENT */
47  HTTP_RESET_CONTENT = 205, /**< RESET_CONTENT */
48  HTTP_PARTIAL_CONTENT = 206, /**< PARTIAL_CONTENT */
49  HTTP_MULTI_STATUS = 207, /**< MULTI_STATUS */
50 
51  HTTP_MULTIPLE_CHOICES = 300, /**< MULTIPLE_CHOICES */
52  HTTP_MOVED_PERMANENTLY = 301, /**< MOVED_PERMANENTLY */
53  HTTP_FOUND = 302, /**< FOUND */
54  HTTP_SEE_OTHER = 303, /**< SEE_OTHER */
55  HTTP_NOT_MODIFIED = 304, /**< NOT_MODIFIED */
56  HTTP_USE_PROXY = 305, /**< USE_PROXY */
57  HTTP_SWITCH_PROXY = 306, /**< SWITCH_PROXY */
58  HTTP_TEMPORARY_REDIRECT = 307, /**< TEMPORARY_REDIRECT */
59 
60  HTTP_BAD_REQUEST = 400, /**< BAD_REQUEST */
61  HTTP_UNAUTHORIZED = 401, /**< UNAUTHORIZED */
62  HTTP_PAYMENT_REQUIRED = 402, /**< PAYMENT_REQUIRED */
63  HTTP_FORBIDDEN = 403, /**< FORBIDDEN */
64  HTTP_NOT_FOUND = 404, /**< NOT_FOUND */
65  HTTP_METHOD_NOT_ALLOWED = 405, /**< METHOD_NOT_ALLOWED */
66  HTTP_METHOD_NOT_ACCEPTABLE = 406, /**< METHOD_NOT_ACCEPTABLE */
67  HTTP_PROXY_AUTHENTICATION_REQUIRED = 407, /**< PROXY_AUTHENTICATION_REQUIRED */
68  HTTP_REQUEST_TIMEOUT = 408, /**< REQUEST_TIMEOUT */
69  HTTP_CONFLICT = 409, /**< CONFLICT */
70  HTTP_GONE = 410, /**< GONE */
71  HTTP_LENGTH_REQUIRED = 411, /**< LENGTH_REQUIRED */
72  HTTP_PRECONDITION_FAILED = 412, /**< PRECONDITION_FAILED */
73  HTTP_REQUEST_ENTITY_TOO_LARGE = 413, /**< REQUEST_ENTITY_TOO_LARGE */
74  HTTP_REQUEST_URI_TOO_LONG = 414, /**< REQUEST_URI_TOO_LONG */
75  HTTP_UNSUPPORTED_MEDIA_TYPE = 415, /**< UNSUPPORTED_MEDIA_TYPE */
76  HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416, /**< REQUESTED_RANGE_NOT_SATISFIABLE */
77  HTTP_EXPECTATION_FAILED = 417, /**< EXPECTATION_FAILED */
78  HTTP_UNPROCESSABLE_ENTITY = 422, /**< UNPROCESSABLE_ENTITY */
79  HTTP_LOCKED = 423, /**< LOCKED */
80  HTTP_FAILED_DEPENDENCY = 424, /**< FAILED_DEPENDENCY */
81  HTTP_UNORDERED_COLLECTION = 425, /**< UNORDERED_COLLECTION */
82  HTTP_UPGRADE_REQUIRED = 426, /**< UPGRADE_REQUIRED */
83  HTTP_RETRY_WITH = 449, /**< RETRY_WITH */
84 
85  HTTP_INTERNAL_SERVER_ERROR = 500, /**< INTERNAL_SERVER_ERROR */
86  HTTP_NOT_IMPLEMENTED = 501, /**< NOT_IMPLEMENTED */
87  HTTP_BAD_GATEWAY = 502, /**< BAD_GATEWAY */
88  HTTP_SERVICE_UNAVAILABLE = 503, /**< SERVICE_UNAVAILABLE */
89  HTTP_GATEWAY_TIMEOUT = 504, /**< GATEWAY_TIMEOUT */
90  HTTP_HTTP_VERSION_NOT_SUPPORTED = 505, /**< HTTP_VERSION_NOT_SUPPORTED */
91  HTTP_VARIANT_ALSO_NEGOTIATES = 506, /**< VARIANT_ALSO_NEGOTIATES */
92  HTTP_INSUFFICIENT_STORAGE = 507, /**< INSUFFICIENT_STORAGE */
93  HTTP_BANDWIDTH_LIMIT_EXCEEDED = 509, /**< BANDWIDTH_LIMIT_EXCEEDED */
94  HTTP_NOT_EXTENDED = 510 /**< NOT_EXTENDED */
95  } Code;
96 
97  /** Map of headers. */
98  typedef std::map<std::string, std::string> HeaderMap;
99 
101  virtual ~WebReply();
102 
103  Code code() const;
105  void add_header(const std::string &header, const std::string &content);
106  void add_header(const std::string &header_string);
107  const HeaderMap &headers() const;
108 
109  void set_caching(bool caching);
110  static void set_caching_default(bool caching);
111 
112  void set_request(WebRequest *request);
113  WebRequest *get_request() const;
114 
115  void pack_caching();
116 
117 private:
118  Code code_;
119  HeaderMap headers_;
120  bool caching_;
121  static bool caching_default_;
122  WebRequest *request_;
123 };
124 
125 class DynamicWebReply : public WebReply
126 {
127 public:
129 
130  virtual size_t chunk_size();
131  virtual size_t size() = 0;
132  virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size) = 0;
133 };
134 
135 class StaticWebReply : public WebReply
136 {
137 public:
138  StaticWebReply(Code code, std::string body = "");
139 
140  void append_body(const char *format, ...);
141  void append_body(const std::string &s);
142  StaticWebReply &operator+=(std::string text);
143 
144  virtual const std::string & body();
145  virtual std::string::size_type body_length();
146 
147  virtual void pack();
148 
149 protected:
150  /** Body of the reply. */
151  std::string _body;
152 };
153 
154 extern WebReply *no_caching(WebReply *reply);
155 
156 } // end namespace fawkes
157 
158 #endif
fawkes::WebReply::HTTP_NOT_IMPLEMENTED
@ HTTP_NOT_IMPLEMENTED
NOT_IMPLEMENTED.
Definition: reply.h:92
fawkes::WebReply::HTTP_UNPROCESSABLE_ENTITY
@ HTTP_UNPROCESSABLE_ENTITY
UNPROCESSABLE_ENTITY.
Definition: reply.h:84
fawkes::WebReply::HTTP_GONE
@ HTTP_GONE
GONE.
Definition: reply.h:76
fawkes::WebReply::HTTP_GATEWAY_TIMEOUT
@ HTTP_GATEWAY_TIMEOUT
GATEWAY_TIMEOUT.
Definition: reply.h:95
fawkes::WebReply::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
@ HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
REQUESTED_RANGE_NOT_SATISFIABLE.
Definition: reply.h:82
fawkes::WebRequest
Definition: request.h:41
fawkes::WebReply::HTTP_HTTP_VERSION_NOT_SUPPORTED
@ HTTP_HTTP_VERSION_NOT_SUPPORTED
HTTP_VERSION_NOT_SUPPORTED.
Definition: reply.h:96
fawkes::WebReply::HTTP_SWITCH_PROXY
@ HTTP_SWITCH_PROXY
SWITCH_PROXY.
Definition: reply.h:63
fawkes::WebReply::set_code
void set_code(Code code)
Set response code.
Definition: reply.cpp:119
fawkes::WebReply::HTTP_MULTIPLE_CHOICES
@ HTTP_MULTIPLE_CHOICES
MULTIPLE_CHOICES.
Definition: reply.h:57
fawkes::DynamicWebReply::next_chunk
virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size)=0
fawkes::WebReply::HTTP_UNAUTHORIZED
@ HTTP_UNAUTHORIZED
UNAUTHORIZED.
Definition: reply.h:67
fawkes::WebReply::HTTP_SWITCHING_PROTOCOLS
@ HTTP_SWITCHING_PROTOCOLS
SWITCHING_PROTOCOLS.
Definition: reply.h:45
fawkes::WebReply::HTTP_BAD_GATEWAY
@ HTTP_BAD_GATEWAY
BAD_GATEWAY.
Definition: reply.h:93
fawkes::DynamicWebReply::chunk_size
virtual size_t chunk_size()
Chunksize.
Definition: reply.cpp:232
fawkes::WebReply::set_request
void set_request(WebRequest *request)
Set associated request.
Definition: reply.cpp:178
fawkes::WebReply::HTTP_PAYMENT_REQUIRED
@ HTTP_PAYMENT_REQUIRED
PAYMENT_REQUIRED.
Definition: reply.h:68
fawkes::WebReply::HTTP_BANDWIDTH_LIMIT_EXCEEDED
@ HTTP_BANDWIDTH_LIMIT_EXCEEDED
BANDWIDTH_LIMIT_EXCEEDED.
Definition: reply.h:99
fawkes::WebReply::HTTP_CONTINUE
@ HTTP_CONTINUE
CONTINUE.
Definition: reply.h:44
fawkes::WebReply::HTTP_NOT_FOUND
@ HTTP_NOT_FOUND
NOT_FOUND.
Definition: reply.h:70
fawkes::WebReply::HTTP_EXPECTATION_FAILED
@ HTTP_EXPECTATION_FAILED
EXPECTATION_FAILED.
Definition: reply.h:83
fawkes::WebReply::set_caching
void set_caching(bool caching)
Enable or disable caching for this specific reply.
Definition: reply.cpp:101
fawkes::WebReply::HTTP_RETRY_WITH
@ HTTP_RETRY_WITH
RETRY_WITH.
Definition: reply.h:89
fawkes::DynamicWebReply::DynamicWebReply
DynamicWebReply(Code code)
Constructor.
Definition: reply.cpp:222
fawkes::WebReply::HTTP_ACCEPTED
@ HTTP_ACCEPTED
ACCEPTED.
Definition: reply.h:50
fawkes::WebReply::HTTP_CREATED
@ HTTP_CREATED
CREATED.
Definition: reply.h:49
fawkes::WebReply::HTTP_UNSUPPORTED_MEDIA_TYPE
@ HTTP_UNSUPPORTED_MEDIA_TYPE
UNSUPPORTED_MEDIA_TYPE.
Definition: reply.h:81
fawkes::StaticWebReply::operator+=
StaticWebReply & operator+=(std::string text)
Append simple text line.
Definition: reply.cpp:285
fawkes::WebReply::Code
Code
HTTP response code.
Definition: reply.h:43
fawkes::WebReply::HTTP_PRECONDITION_FAILED
@ HTTP_PRECONDITION_FAILED
PRECONDITION_FAILED.
Definition: reply.h:78
fawkes::StaticWebReply::pack
virtual void pack()
Pack the data.
Definition: reply.cpp:315
fawkes::WebReply::WebReply
WebReply(Code code)
Constructor.
Definition: reply.cpp:72
fawkes::WebReply::HTTP_LOCKED
@ HTTP_LOCKED
LOCKED.
Definition: reply.h:85
fawkes::WebReply::pack_caching
void pack_caching()
Called just before the reply is sent.
Definition: reply.cpp:187
fawkes::WebReply::headers
const HeaderMap & headers() const
get headers.
Definition: reply.cpp:159
fawkes::WebReply::HTTP_OK
@ HTTP_OK
OK.
Definition: reply.h:48
fawkes::StaticWebReply::body_length
virtual std::string::size_type body_length()
Get length of body.
Definition: reply.cpp:304
fawkes::WebReply::HTTP_SERVICE_UNAVAILABLE
@ HTTP_SERVICE_UNAVAILABLE
SERVICE_UNAVAILABLE.
Definition: reply.h:94
fawkes::WebReply::HTTP_SEE_OTHER
@ HTTP_SEE_OTHER
SEE_OTHER.
Definition: reply.h:60
fawkes::WebReply::HTTP_MULTI_STATUS
@ HTTP_MULTI_STATUS
MULTI_STATUS.
Definition: reply.h:55
fawkes::WebReply::HTTP_NOT_MODIFIED
@ HTTP_NOT_MODIFIED
NOT_MODIFIED.
Definition: reply.h:61
fawkes::WebReply::HTTP_NO_CONTENT
@ HTTP_NO_CONTENT
NO_CONTENT.
Definition: reply.h:52
fawkes::WebReply::HTTP_BAD_REQUEST
@ HTTP_BAD_REQUEST
BAD_REQUEST.
Definition: reply.h:66
fawkes
fawkes::WebReply::HTTP_FOUND
@ HTTP_FOUND
FOUND.
Definition: reply.h:59
fawkes::StaticWebReply::append_body
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:259
fawkes::WebReply::HTTP_FORBIDDEN
@ HTTP_FORBIDDEN
FORBIDDEN.
Definition: reply.h:69
fawkes::WebReply::HTTP_UPGRADE_REQUIRED
@ HTTP_UPGRADE_REQUIRED
UPGRADE_REQUIRED.
Definition: reply.h:88
fawkes::WebReply::HTTP_CONFLICT
@ HTTP_CONFLICT
CONFLICT.
Definition: reply.h:75
fawkes::WebReply::HTTP_PROXY_AUTHENTICATION_REQUIRED
@ HTTP_PROXY_AUTHENTICATION_REQUIRED
PROXY_AUTHENTICATION_REQUIRED.
Definition: reply.h:73
fawkes::WebReply::add_header
void add_header(const std::string &header, const std::string &content)
Add a HTTP header.
Definition: reply.cpp:129
fawkes::WebReply::set_caching_default
static void set_caching_default(bool caching)
Enable or disable caching default for replies.
Definition: reply.cpp:92
fawkes::WebReply::HTTP_REQUEST_URI_TOO_LONG
@ HTTP_REQUEST_URI_TOO_LONG
REQUEST_URI_TOO_LONG.
Definition: reply.h:80
fawkes::WebReply::HTTP_LENGTH_REQUIRED
@ HTTP_LENGTH_REQUIRED
LENGTH_REQUIRED.
Definition: reply.h:77
fawkes::WebReply::HTTP_NOT_EXTENDED
@ HTTP_NOT_EXTENDED
NOT_EXTENDED.
Definition: reply.h:100
fawkes::WebReply::HTTP_METHOD_NOT_ACCEPTABLE
@ HTTP_METHOD_NOT_ACCEPTABLE
METHOD_NOT_ACCEPTABLE.
Definition: reply.h:72
fawkes::WebReply::HeaderMap
std::map< std::string, std::string > HeaderMap
Map of headers.
Definition: reply.h:104
fawkes::WebReply::HTTP_RESET_CONTENT
@ HTTP_RESET_CONTENT
RESET_CONTENT.
Definition: reply.h:53
fawkes::WebReply::HTTP_USE_PROXY
@ HTTP_USE_PROXY
USE_PROXY.
Definition: reply.h:62
fawkes::WebReply::HTTP_UNORDERED_COLLECTION
@ HTTP_UNORDERED_COLLECTION
UNORDERED_COLLECTION.
Definition: reply.h:87
fawkes::WebReply::HTTP_METHOD_NOT_ALLOWED
@ HTTP_METHOD_NOT_ALLOWED
METHOD_NOT_ALLOWED.
Definition: reply.h:71
fawkes::WebReply::HTTP_VARIANT_ALSO_NEGOTIATES
@ HTTP_VARIANT_ALSO_NEGOTIATES
VARIANT_ALSO_NEGOTIATES.
Definition: reply.h:97
fawkes::StaticWebReply
Definition: reply.h:141
fawkes::no_caching
WebReply * no_caching(WebReply *reply)
Disable caching on a reply.
Definition: reply.cpp:53
fawkes::StaticWebReply::body
virtual const std::string & body()
Get body.
Definition: reply.cpp:295
fawkes::WebReply::code
Code code() const
Get response code.
Definition: reply.cpp:110
fawkes::StaticWebReply::_body
std::string _body
Body of the reply.
Definition: reply.h:157
fawkes::WebReply::HTTP_NON_AUTHORITATIVE_INFORMATION
@ HTTP_NON_AUTHORITATIVE_INFORMATION
NON_AUTHORITATIVE_INFORMATION.
Definition: reply.h:51
fawkes::WebReply::HTTP_MOVED_PERMANENTLY
@ HTTP_MOVED_PERMANENTLY
MOVED_PERMANENTLY.
Definition: reply.h:58
fawkes::WebReply::HTTP_TEMPORARY_REDIRECT
@ HTTP_TEMPORARY_REDIRECT
TEMPORARY_REDIRECT.
Definition: reply.h:64
fawkes::DynamicWebReply::size
virtual size_t size()=0
fawkes::WebReply::get_request
WebRequest * get_request() const
Get associated request.
Definition: reply.cpp:169
fawkes::StaticWebReply::StaticWebReply
StaticWebReply(Code code, std::string body="")
Constructor.
Definition: reply.cpp:249
fawkes::WebReply::~WebReply
virtual ~WebReply()
Destructor.
Definition: reply.cpp:81
fawkes::WebReply::HTTP_PARTIAL_CONTENT
@ HTTP_PARTIAL_CONTENT
PARTIAL_CONTENT.
Definition: reply.h:54
fawkes::WebReply
Definition: reply.h:39
fawkes::WebReply::HTTP_PROCESSING
@ HTTP_PROCESSING
PROCESSING.
Definition: reply.h:46
fawkes::WebReply::HTTP_INTERNAL_SERVER_ERROR
@ HTTP_INTERNAL_SERVER_ERROR
INTERNAL_SERVER_ERROR.
Definition: reply.h:91
fawkes::DynamicWebReply
Definition: reply.h:131
fawkes::WebReply::HTTP_REQUEST_TIMEOUT
@ HTTP_REQUEST_TIMEOUT
REQUEST_TIMEOUT.
Definition: reply.h:74
fawkes::WebReply::HTTP_FAILED_DEPENDENCY
@ HTTP_FAILED_DEPENDENCY
FAILED_DEPENDENCY.
Definition: reply.h:86
fawkes::WebReply::HTTP_INSUFFICIENT_STORAGE
@ HTTP_INSUFFICIENT_STORAGE
INSUFFICIENT_STORAGE.
Definition: reply.h:98
fawkes::WebReply::HTTP_REQUEST_ENTITY_TOO_LARGE
@ HTTP_REQUEST_ENTITY_TOO_LARGE
REQUEST_ENTITY_TOO_LARGE.
Definition: reply.h:79