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 
100  WebReply(Code code);
101  virtual ~WebReply();
102 
103  Code code() const;
104  void set_code(Code code);
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
const HeaderMap & headers() const
get headers.
Definition: reply.cpp:153
void set_request(WebRequest *request)
Set associated request.
Definition: reply.cpp:172
virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size)=0
Get data of next chunk.
virtual size_t chunk_size()
Chunksize.
Definition: reply.cpp:226
virtual ~WebReply()
Destructor.
Definition: reply.cpp:75
WebRequest * get_request() const
Get associated request.
Definition: reply.cpp:163
virtual size_t size()=0
Total size of the web reply.
Fawkes library namespace.
DynamicWebReply(Code code)
Constructor.
Definition: reply.cpp:216
WebReply(Code code)
Constructor.
Definition: reply.cpp:66
std::string _body
Body of the reply.
Definition: reply.h:151
virtual const std::string & body()
Get body.
Definition: reply.cpp:289
NON_AUTHORITATIVE_INFORMATION.
Definition: reply.h:45
Code code() const
Get response code.
Definition: reply.cpp:104
StaticWebReply(Code code, std::string body="")
Constructor.
Definition: reply.cpp:243
Dynamic web reply.
Definition: reply.h:125
void set_caching(bool caching)
Enable or disable caching for this specific reply.
Definition: reply.cpp:95
WebReply * no_caching(WebReply *reply)
Disable caching on a reply.
Definition: reply.cpp:47
static void set_caching_default(bool caching)
Enable or disable caching default for replies.
Definition: reply.cpp:86
std::map< std::string, std::string > HeaderMap
Map of headers.
Definition: reply.h:98
void set_code(Code code)
Set response code.
Definition: reply.cpp:113
Web request meta data carrier.
Definition: request.h:41
Basic web reply.
Definition: reply.h:33
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:253
void add_header(const std::string &header, const std::string &content)
Add a HTTP header.
Definition: reply.cpp:123
Code
HTTP response code.
Definition: reply.h:37
StaticWebReply & operator+=(std::string text)
Append simple text line.
Definition: reply.cpp:279
virtual void pack()
Pack the data.
Definition: reply.cpp:309
PROXY_AUTHENTICATION_REQUIRED.
Definition: reply.h:67
REQUESTED_RANGE_NOT_SATISFIABLE.
Definition: reply.h:76
virtual std::string::size_type body_length()
Get length of body.
Definition: reply.cpp:298
Static web reply.
Definition: reply.h:135
void pack_caching()
Called just before the reply is sent.
Definition: reply.cpp:181