FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
libhttpserver/src/httpserver/http_response_builder.hpp at master · a-pavlov/libhttpserver · GitHub
a-pavlov
/
libhttpserver
Public
forked from
etr/libhttpserver
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
libhttpserver
/
src
/
httpserver
/
http_response_builder.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
298 lines (261 loc) · 9.68 KB
Breadcrumbs
libhttpserver
/
src
/
httpserver
/
http_response_builder.hpp
Copy path
File metadata and controls
298 lines (261 loc) · 9.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#
if
!defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION)
#
error
"Only <httpserver.hpp> or <httpserverpp> can be included directly."
#
endif
#
ifndef
_HTTP_RESPONSE_BUILDER_HPP_
#
define
_HTTP_RESPONSE_BUILDER_HPP_
#
include
<
map
>
#
include
<
string
>
#
include
"
httpserver/http_response.hpp
"
struct
MHD_Connection
;
namespace
httpserver
{
class
webserver
;
namespace
http
{
class
header_comparator
;
};
namespace
details
{
struct
cache_entry
;
};
using
namespace
http
;
struct
byte_string
{
public:
byte_string
(
const
char
* content_hook,
size_t
content_length):
content_hook
(content_hook),
content_length
(content_length)
{
}
const
char
*
get_content_hook
()
const
{
return
content_hook;
}
size_t
get_content_length
()
const
{
return
content_length;
}
private:
const
char
* content_hook;
size_t
content_length;
};
class
http_response_builder
{
public:
explicit
http_response_builder
(
const
std::string& content_hook,
int
response_code =
200
,
const
std::string& content_type =
"
text/plain
"
,
bool
autodelete =
true
):
_content_hook(content_hook),
_response_code(response_code),
_autodelete(autodelete),
_realm(
"
"
),
_opaque(
"
"
),
_reload_nonce(
false
),
_headers(std::map<std::string, std::string, header_comparator>()),
_footers(std::map<std::string, std::string, header_comparator>()),
_cookies(std::map<std::string, std::string, header_comparator>()),
_topics(std::vector<std::string>()),
_keepalive_secs(-
1
),
_keepalive_msg(
"
"
),
_send_topic(
"
"
),
_ce(
0x0
),
_get_raw_response(&http_response::get_raw_response_str),
_decorate_response(&http_response::decorate_response_str),
_enqueue_response(&http_response::enqueue_response_str)
{
_headers[http_utils::http_header_content_type] = content_type;
}
http_response_builder
(
const
byte_string& content_hook,
int
response_code =
200
,
const
std::string& content_type =
"
text/plain
"
,
bool
autodelete =
true
):
_content_hook
(std::string(content_hook.get_content_hook(), content_hook.get_content_length())),
_response_code
(response_code),
_autodelete
(autodelete),
_realm
(
"
"
),
_opaque
(
"
"
),
_reload_nonce
(
false
),
_headers
(std::map<std::string, std::string, header_comparator>()),
_footers
(std::map<std::string, std::string, header_comparator>()),
_cookies
(std::map<std::string, std::string, header_comparator>()),
_topics
(std::vector<std::string>()),
_keepalive_secs
(-
1
),
_keepalive_msg
(
"
"
),
_send_topic
(
"
"
),
_ce
(
0x0
),
_get_raw_response
(&http_response::get_raw_response_str),
_decorate_response
(&http_response::decorate_response_str),
_enqueue_response
(&http_response::enqueue_response_str)
{
_headers[http_utils::http_header_content_type] = content_type;
}
http_response_builder
(
const
http_response_builder& b):
_content_hook
(b._content_hook),
_response_code
(b._response_code),
_autodelete
(b._autodelete),
_realm
(b._realm),
_opaque
(b._opaque),
_reload_nonce
(b._reload_nonce),
_headers
(b._headers),
_footers
(b._footers),
_cookies
(b._cookies),
_topics
(b._topics),
_keepalive_secs
(b._keepalive_secs),
_keepalive_msg
(b._keepalive_msg),
_send_topic
(b._send_topic),
_ce
(b._ce),
_get_raw_response
(b._get_raw_response),
_decorate_response
(b._decorate_response),
_enqueue_response
(b._enqueue_response)
{
}
http_response_builder&
operator
=(
const
http_response_builder& b)
{
_content_hook = b.
_content_hook
;
_response_code = b.
_response_code
;
_autodelete = b.
_autodelete
;
_realm = b.
_realm
;
_opaque = b.
_opaque
;
_reload_nonce = b.
_reload_nonce
;
_headers = b.
_headers
;
_footers = b.
_footers
;
_cookies = b.
_cookies
;
_topics = b.
_topics
;
_keepalive_secs = b.
_keepalive_secs
;
_keepalive_msg = b.
_keepalive_msg
;
_send_topic = b.
_send_topic
;
_ce = b.
_ce
;
_get_raw_response = b.
_get_raw_response
;
_decorate_response = b.
_decorate_response
;
_enqueue_response = b.
_enqueue_response
;
return
*
this
;
}
~http_response_builder
()
{
}
http_response_builder&
string_response
()
{
return
*
this
;
}
http_response_builder&
file_response
()
{
_get_raw_response = &http_response::get_raw_response_file;
return
*
this
;
}
http_response_builder&
basic_auth_fail_response
(
const
std::string& realm =
"
"
)
{
_realm = realm;
_enqueue_response = &http_response::enqueue_response_basic;
return
*
this
;
}
http_response_builder&
digest_auth_fail_response
(
const
std::string& realm =
"
"
,
const
std::string& opaque =
"
"
,
bool
reload_nonce =
false
)
{
_realm = realm;
_opaque = opaque;
_reload_nonce = reload_nonce;
_enqueue_response = &http_response::enqueue_response_digest;
return
*
this
;
}
http_response_builder&
long_polling_receive_response
(
const
std::vector<std::string>& topics,
int
keepalive_secs = -
1
,
std::string keepalive_msg =
"
"
)
{
_topics = topics;
_keepalive_secs = keepalive_secs;
_keepalive_msg = keepalive_msg;
_get_raw_response = &http_response::get_raw_response_lp_receive;
return
*
this
;
}
http_response_builder&
long_polling_send_response
(
const
std::string& send_topic)
{
_send_topic = send_topic;
_get_raw_response = &http_response::get_raw_response_lp_send;
return
*
this
;
}
http_response_builder&
cache_response
()
{
_get_raw_response = &http_response::get_raw_response_cache;
_decorate_response = &http_response::decorate_response_cache;
return
*
this
;
}
http_response_builder&
deferred_response
(cycle_callback_ptr cycle_callback)
{
_cycle_callback = cycle_callback;
_get_raw_response = &http_response::get_raw_response_deferred;
_decorate_response = &http_response::decorate_response_deferred;
return
*
this
;
}
http_response_builder&
shoutCAST_response
()
{
_response_code |= http_utils::shoutcast_response;
return
*
this
;
}
http_response_builder&
with_header
(
const
std::string& key,
const
std::string& value)
{
_headers[key] = value;
return
*
this
;
}
http_response_builder&
with_footer
(
const
std::string& key,
const
std::string& value)
{
_footers[key] = value;
return
*
this
;
}
http_response_builder&
with_cookie
(
const
std::string& key,
const
std::string& value)
{
_cookies[key] = value;
return
*
this
;
}
private:
std::string _content_hook;
int
_response_code;
bool
_autodelete;
std::string _realm;
std::string _opaque;
bool
_reload_nonce;
std::map<std::string, std::string, header_comparator> _headers;
std::map<std::string, std::string, header_comparator> _footers;
std::map<std::string, std::string, header_comparator> _cookies;
std::vector<std::string> _topics;
int
_keepalive_secs;
std::string _keepalive_msg;
std::string _send_topic;
cycle_callback_ptr _cycle_callback;
details::cache_entry* _ce;
void
(http_response::*_get_raw_response)(MHD_Response**, webserver*);
void
(http_response::*_decorate_response)(MHD_Response*);
int
(http_response::*_enqueue_response)(MHD_Connection*, MHD_Response*);
http_response_builder&
cache_response
(details::cache_entry* ce)
{
_ce = ce;
_get_raw_response = &http_response::get_raw_response_cache;
_decorate_response = &http_response::decorate_response_cache;
return
*
this
;
}
friend
class
http_response
;
};
};
#
endif
//
_HTTP_RESPONSE_BUILDER_HPP_
Back
|
FazBrowse Home
|
New Git URL