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 #if 0 /* just to make Emacs auto-indent happy */
31 }
32 #endif
33 
34 class WebRequest;
35 
36 class WebReply
37 {
38  public:
39  /** HTTP response code. */
40  typedef enum {
41  HTTP_CONTINUE = 100, /**< CONTINUE */
42  HTTP_SWITCHING_PROTOCOLS = 101, /**< SWITCHING_PROTOCOLS */
43  HTTP_PROCESSING = 102, /**< PROCESSING */
44 
45  HTTP_OK = 200, /**< OK */
46  HTTP_CREATED = 201, /**< CREATED */
47  HTTP_ACCEPTED = 202, /**< ACCEPTED */
48  HTTP_NON_AUTHORITATIVE_INFORMATION = 203, /**< NON_AUTHORITATIVE_INFORMATION */
49  HTTP_NO_CONTENT = 204, /**< NO_CONTENT */
50  HTTP_RESET_CONTENT = 205, /**< RESET_CONTENT */
51  HTTP_PARTIAL_CONTENT = 206, /**< PARTIAL_CONTENT */
52  HTTP_MULTI_STATUS = 207, /**< MULTI_STATUS */
53 
54  HTTP_MULTIPLE_CHOICES = 300, /**< MULTIPLE_CHOICES */
55  HTTP_MOVED_PERMANENTLY = 301, /**< MOVED_PERMANENTLY */
56  HTTP_FOUND = 302, /**< FOUND */
57  HTTP_SEE_OTHER = 303, /**< SEE_OTHER */
58  HTTP_NOT_MODIFIED = 304, /**< NOT_MODIFIED */
59  HTTP_USE_PROXY = 305, /**< USE_PROXY */
60  HTTP_SWITCH_PROXY = 306, /**< SWITCH_PROXY */
61  HTTP_TEMPORARY_REDIRECT = 307, /**< TEMPORARY_REDIRECT */
62 
63  HTTP_BAD_REQUEST = 400, /**< BAD_REQUEST */
64  HTTP_UNAUTHORIZED = 401, /**< UNAUTHORIZED */
65  HTTP_PAYMENT_REQUIRED = 402, /**< PAYMENT_REQUIRED */
66  HTTP_FORBIDDEN = 403, /**< FORBIDDEN */
67  HTTP_NOT_FOUND = 404, /**< NOT_FOUND */
68  HTTP_METHOD_NOT_ALLOWED = 405, /**< METHOD_NOT_ALLOWED */
69  HTTP_METHOD_NOT_ACCEPTABLE = 406, /**< METHOD_NOT_ACCEPTABLE */
70  HTTP_PROXY_AUTHENTICATION_REQUIRED = 407, /**< PROXY_AUTHENTICATION_REQUIRED */
71  HTTP_REQUEST_TIMEOUT = 408, /**< REQUEST_TIMEOUT */
72  HTTP_CONFLICT = 409, /**< CONFLICT */
73  HTTP_GONE = 410, /**< GONE */
74  HTTP_LENGTH_REQUIRED = 411, /**< LENGTH_REQUIRED */
75  HTTP_PRECONDITION_FAILED = 412, /**< PRECONDITION_FAILED */
76  HTTP_REQUEST_ENTITY_TOO_LARGE = 413, /**< REQUEST_ENTITY_TOO_LARGE */
77  HTTP_REQUEST_URI_TOO_LONG = 414, /**< REQUEST_URI_TOO_LONG */
78  HTTP_UNSUPPORTED_MEDIA_TYPE = 415, /**< UNSUPPORTED_MEDIA_TYPE */
79  HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416, /**< REQUESTED_RANGE_NOT_SATISFIABLE */
80  HTTP_EXPECTATION_FAILED = 417, /**< EXPECTATION_FAILED */
81  HTTP_UNPROCESSABLE_ENTITY = 422, /**< UNPROCESSABLE_ENTITY */
82  HTTP_LOCKED = 423, /**< LOCKED */
83  HTTP_FAILED_DEPENDENCY = 424, /**< FAILED_DEPENDENCY */
84  HTTP_UNORDERED_COLLECTION = 425, /**< UNORDERED_COLLECTION */
85  HTTP_UPGRADE_REQUIRED = 426, /**< UPGRADE_REQUIRED */
86  HTTP_RETRY_WITH = 449, /**< RETRY_WITH */
87 
88  HTTP_INTERNAL_SERVER_ERROR = 500, /**< INTERNAL_SERVER_ERROR */
89  HTTP_NOT_IMPLEMENTED = 501, /**< NOT_IMPLEMENTED */
90  HTTP_BAD_GATEWAY = 502, /**< BAD_GATEWAY */
91  HTTP_SERVICE_UNAVAILABLE = 503, /**< SERVICE_UNAVAILABLE */
92  HTTP_GATEWAY_TIMEOUT = 504, /**< GATEWAY_TIMEOUT */
93  HTTP_HTTP_VERSION_NOT_SUPPORTED = 505, /**< HTTP_VERSION_NOT_SUPPORTED */
94  HTTP_VARIANT_ALSO_NEGOTIATES = 506, /**< VARIANT_ALSO_NEGOTIATES */
95  HTTP_INSUFFICIENT_STORAGE = 507, /**< INSUFFICIENT_STORAGE */
96  HTTP_BANDWIDTH_LIMIT_EXCEEDED = 509, /**< BANDWIDTH_LIMIT_EXCEEDED */
97  HTTP_NOT_EXTENDED = 510 /**< NOT_EXTENDED */
98  } Code;
99 
100  /** Map of headers. */
101  typedef std::map<std::string, std::string> HeaderMap;
102 
103  WebReply(Code code);
104  virtual ~WebReply();
105 
106  Code code() const;
107  void add_header(std::string header, std::string content);
108  void add_header(std::string header_string);
109  const HeaderMap & headers() const;
110 
111  static void set_caching(bool caching);
112 
113  void set_request(WebRequest *request);
114  WebRequest * get_request() const;
115 
116  private:
117  Code __code;
118  HeaderMap __headers;
119  static bool __caching;
120  WebRequest *__request;
121 };
122 
123 class DynamicWebReply : public WebReply
124 {
125  public:
127 
128  virtual size_t chunk_size();
129  virtual size_t size() = 0;
130  virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size) = 0;
131 };
132 
133 class StaticWebReply : public WebReply
134 {
135  public:
136  StaticWebReply(Code code, std::string body = "");
137 
138  void append_body(const char *format, ...);
139  StaticWebReply & operator+=(std::string text);
140 
141  virtual const std::string & body();
142  virtual std::string::size_type body_length();
143 
144  virtual void pack();
145  protected:
146  /** Body of the reply. */
147  std::string _body;
148 };
149 
150 } // end namespace fawkes
151 
152 #endif
const HeaderMap & headers() const
get headers.
Definition: reply.cpp:129
void set_request(WebRequest *request)
Set associated request.
Definition: reply.cpp:150
virtual ~WebReply()
Destructor.
Definition: reply.cpp:65
WebRequest * get_request() const
Get associated request.
Definition: reply.cpp:140
Fawkes library namespace.
void add_header(std::string header, std::string content)
Add a HTTP header.
Definition: reply.cpp:97
WebReply(Code code)
Constructor.
Definition: reply.cpp:50
std::string _body
Body of the reply.
Definition: reply.h:147
BANDWIDTH_LIMIT_EXCEEDED.
Definition: reply.h:96
NON_AUTHORITATIVE_INFORMATION.
Definition: reply.h:48
Code code() const
Get response code.
Definition: reply.cpp:86
HTTP_VERSION_NOT_SUPPORTED.
Definition: reply.h:93
Dynamic web reply.
Definition: reply.h:123
static void set_caching(bool caching)
Enable or disable caching for all consecutive replies.
Definition: reply.cpp:77
REQUEST_ENTITY_TOO_LARGE.
Definition: reply.h:76
std::map< std::string, std::string > HeaderMap
Map of headers.
Definition: reply.h:101
Web request meta data carrier.
Definition: request.h:42
Basic web reply.
Definition: reply.h:36
Code
HTTP response code.
Definition: reply.h:40
PROXY_AUTHENTICATION_REQUIRED.
Definition: reply.h:70
REQUESTED_RANGE_NOT_SATISFIABLE.
Definition: reply.h:79
Static web reply.
Definition: reply.h:133