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
29namespace fawkes {
30
31class WebRequest;
32
34{
35public:
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 */
96
97 /** Map of headers. */
98 typedef std::map<std::string, std::string> HeaderMap;
99
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
117private:
118 Code code_;
119 HeaderMap headers_;
120 bool caching_;
121 static bool caching_default_;
122 WebRequest *request_;
123};
124
126{
127public:
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
136{
137public:
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
149protected:
150 /** Body of the reply. */
151 std::string _body;
152};
153
154extern WebReply *no_caching(WebReply *reply);
155
156} // end namespace fawkes
157
158#endif
Dynamic web reply.
Definition: reply.h:126
virtual size_t size()=0
Total size of the web reply.
DynamicWebReply(Code code)
Constructor.
Definition: reply.cpp:216
virtual size_t chunk_size()
Chunksize.
Definition: reply.cpp:226
virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size)=0
Get data of next chunk.
Static web reply.
Definition: reply.h:136
StaticWebReply & operator+=(std::string text)
Append simple text line.
Definition: reply.cpp:279
StaticWebReply(Code code, std::string body="")
Constructor.
Definition: reply.cpp:243
void append_body(const char *format,...)
Append to body.
Definition: reply.cpp:253
std::string _body
Body of the reply.
Definition: reply.h:151
virtual std::string::size_type body_length()
Get length of body.
Definition: reply.cpp:298
virtual const std::string & body()
Get body.
Definition: reply.cpp:289
virtual void pack()
Pack the data.
Definition: reply.cpp:309
Basic web reply.
Definition: reply.h:34
void set_code(Code code)
Set response code.
Definition: reply.cpp:113
virtual ~WebReply()
Destructor.
Definition: reply.cpp:75
std::map< std::string, std::string > HeaderMap
Map of headers.
Definition: reply.h:98
static void set_caching_default(bool caching)
Enable or disable caching default for replies.
Definition: reply.cpp:86
void set_caching(bool caching)
Enable or disable caching for this specific reply.
Definition: reply.cpp:95
void add_header(const std::string &header, const std::string &content)
Add a HTTP header.
Definition: reply.cpp:123
Code code() const
Get response code.
Definition: reply.cpp:104
Code
HTTP response code.
Definition: reply.h:37
@ HTTP_MULTIPLE_CHOICES
MULTIPLE_CHOICES.
Definition: reply.h:51
@ HTTP_NOT_MODIFIED
NOT_MODIFIED.
Definition: reply.h:55
@ HTTP_UNAUTHORIZED
UNAUTHORIZED.
Definition: reply.h:61
@ HTTP_BANDWIDTH_LIMIT_EXCEEDED
BANDWIDTH_LIMIT_EXCEEDED.
Definition: reply.h:93
@ HTTP_FOUND
FOUND.
Definition: reply.h:53
@ HTTP_UPGRADE_REQUIRED
UPGRADE_REQUIRED.
Definition: reply.h:82
@ HTTP_ACCEPTED
ACCEPTED.
Definition: reply.h:44
@ HTTP_UNPROCESSABLE_ENTITY
UNPROCESSABLE_ENTITY.
Definition: reply.h:78
@ HTTP_PROCESSING
PROCESSING.
Definition: reply.h:40
@ HTTP_PROXY_AUTHENTICATION_REQUIRED
PROXY_AUTHENTICATION_REQUIRED.
Definition: reply.h:67
@ HTTP_REQUEST_TIMEOUT
REQUEST_TIMEOUT.
Definition: reply.h:68
@ HTTP_MOVED_PERMANENTLY
MOVED_PERMANENTLY.
Definition: reply.h:52
@ HTTP_METHOD_NOT_ALLOWED
METHOD_NOT_ALLOWED.
Definition: reply.h:65
@ HTTP_UNSUPPORTED_MEDIA_TYPE
UNSUPPORTED_MEDIA_TYPE.
Definition: reply.h:75
@ HTTP_NON_AUTHORITATIVE_INFORMATION
NON_AUTHORITATIVE_INFORMATION.
Definition: reply.h:45
@ HTTP_VARIANT_ALSO_NEGOTIATES
VARIANT_ALSO_NEGOTIATES.
Definition: reply.h:91
@ HTTP_EXPECTATION_FAILED
EXPECTATION_FAILED.
Definition: reply.h:77
@ HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
REQUESTED_RANGE_NOT_SATISFIABLE.
Definition: reply.h:76
@ HTTP_USE_PROXY
USE_PROXY.
Definition: reply.h:56
@ HTTP_HTTP_VERSION_NOT_SUPPORTED
HTTP_VERSION_NOT_SUPPORTED.
Definition: reply.h:90
@ HTTP_OK
OK.
Definition: reply.h:42
@ HTTP_SWITCHING_PROTOCOLS
SWITCHING_PROTOCOLS.
Definition: reply.h:39
@ HTTP_MULTI_STATUS
MULTI_STATUS.
Definition: reply.h:49
@ HTTP_BAD_GATEWAY
BAD_GATEWAY.
Definition: reply.h:87
@ HTTP_REQUEST_ENTITY_TOO_LARGE
REQUEST_ENTITY_TOO_LARGE.
Definition: reply.h:73
@ HTTP_REQUEST_URI_TOO_LONG
REQUEST_URI_TOO_LONG.
Definition: reply.h:74
@ HTTP_INTERNAL_SERVER_ERROR
INTERNAL_SERVER_ERROR.
Definition: reply.h:85
@ HTTP_FORBIDDEN
FORBIDDEN.
Definition: reply.h:63
@ HTTP_RETRY_WITH
RETRY_WITH.
Definition: reply.h:83
@ HTTP_SEE_OTHER
SEE_OTHER.
Definition: reply.h:54
@ HTTP_METHOD_NOT_ACCEPTABLE
METHOD_NOT_ACCEPTABLE.
Definition: reply.h:66
@ HTTP_INSUFFICIENT_STORAGE
INSUFFICIENT_STORAGE.
Definition: reply.h:92
@ HTTP_SWITCH_PROXY
SWITCH_PROXY.
Definition: reply.h:57
@ HTTP_PARTIAL_CONTENT
PARTIAL_CONTENT.
Definition: reply.h:48
@ HTTP_NO_CONTENT
NO_CONTENT.
Definition: reply.h:46
@ HTTP_TEMPORARY_REDIRECT
TEMPORARY_REDIRECT.
Definition: reply.h:58
@ HTTP_PAYMENT_REQUIRED
PAYMENT_REQUIRED.
Definition: reply.h:62
@ HTTP_GONE
GONE.
Definition: reply.h:70
@ HTTP_LENGTH_REQUIRED
LENGTH_REQUIRED.
Definition: reply.h:71
@ HTTP_BAD_REQUEST
BAD_REQUEST.
Definition: reply.h:60
@ HTTP_PRECONDITION_FAILED
PRECONDITION_FAILED.
Definition: reply.h:72
@ HTTP_CREATED
CREATED.
Definition: reply.h:43
@ HTTP_UNORDERED_COLLECTION
UNORDERED_COLLECTION.
Definition: reply.h:81
@ HTTP_CONFLICT
CONFLICT.
Definition: reply.h:69
@ HTTP_NOT_EXTENDED
NOT_EXTENDED.
Definition: reply.h:94
@ HTTP_FAILED_DEPENDENCY
FAILED_DEPENDENCY.
Definition: reply.h:80
@ HTTP_RESET_CONTENT
RESET_CONTENT.
Definition: reply.h:47
@ HTTP_GATEWAY_TIMEOUT
GATEWAY_TIMEOUT.
Definition: reply.h:89
@ HTTP_NOT_IMPLEMENTED
NOT_IMPLEMENTED.
Definition: reply.h:86
@ HTTP_LOCKED
LOCKED.
Definition: reply.h:79
@ HTTP_CONTINUE
CONTINUE.
Definition: reply.h:38
@ HTTP_NOT_FOUND
NOT_FOUND.
Definition: reply.h:64
@ HTTP_SERVICE_UNAVAILABLE
SERVICE_UNAVAILABLE.
Definition: reply.h:88
void set_request(WebRequest *request)
Set associated request.
Definition: reply.cpp:172
WebReply(Code code)
Constructor.
Definition: reply.cpp:66
const HeaderMap & headers() const
get headers.
Definition: reply.cpp:153
WebRequest * get_request() const
Get associated request.
Definition: reply.cpp:163
void pack_caching()
Called just before the reply is sent.
Definition: reply.cpp:181
Web request meta data carrier.
Definition: request.h:42
Fawkes library namespace.
WebReply * no_caching(WebReply *reply)
Disable caching on a reply.
Definition: reply.cpp:47