src/examples/cpp03/http/server/reply.cpp | src/examples/cpp11/http/server/reply.cpp |
⋮ | ⋮ |
1 | // | 1 | // |
2 | //·reply.cpp | 2 | //·reply.cpp |
3 | //·~~~~~~~~~ | 3 | //·~~~~~~~~~ |
4 | // | 4 | // |
5 | //·Copyright·(c)·2003-2021·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) | 5 | //·Copyright·(c)·2003-2021·Christopher·M.·Kohlhoff·(chris·at·kohlhoff·dot·com) |
6 | // | 6 | // |
7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying | 7 | //·Distributed·under·the·Boost·Software·License,·Version·1.0.·(See·accompanying |
8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) | 8 | //·file·LICENSE_1_0.txt·or·copy·at·http://www.boost.org/LICENSE_1_0.txt) |
9 | // | 9 | // |
10 | | 10 | |
11 | #include·"reply.hpp" | 11 | #include·"reply.hpp" |
12 | #include·<string> | 12 | #include·<string> |
13 | #include·<boost/lexical_cast.hpp> | |
14 | | 13 | |
15 | namespace·http·{ | 14 | namespace·http·{ |
16 | namespace·server·{ | 15 | namespace·server·{ |
17 | | 16 | |
18 | namespace·status_strings·{ | 17 | namespace·status_strings·{ |
19 | | 18 | |
20 | const·std::string·ok·= | 19 | const·std::string·ok·= |
21 | ··"HTTP/1.0·200·OK\r\n"; | 20 | ··"HTTP/1.0·200·OK\r\n"; |
22 | const·std::string·created·= | 21 | const·std::string·created·= |
23 | ··"HTTP/1.0·201·Created\r\n"; | 22 | ··"HTTP/1.0·201·Created\r\n"; |
24 | const·std::string·accepted·= | 23 | const·std::string·accepted·= |
25 | ··"HTTP/1.0·202·Accepted\r\n"; | 24 | ··"HTTP/1.0·202·Accepted\r\n"; |
26 | const·std::string·no_content·= | 25 | const·std::string·no_content·= |
27 | ··"HTTP/1.0·204·No·Content\r\n"; | 26 | ··"HTTP/1.0·204·No·Content\r\n"; |
28 | const·std::string·multiple_choices·= | 27 | const·std::string·multiple_choices·= |
29 | ··"HTTP/1.0·300·Multiple·Choices\r\n"; | 28 | ··"HTTP/1.0·300·Multiple·Choices\r\n"; |
30 | const·std::string·moved_permanently·= | 29 | const·std::string·moved_permanently·= |
31 | ··"HTTP/1.0·301·Moved·Permanently\r\n"; | 30 | ··"HTTP/1.0·301·Moved·Permanently\r\n"; |
32 | const·std::string·moved_temporarily·= | 31 | const·std::string·moved_temporarily·= |
33 | ··"HTTP/1.0·302·Moved·Temporarily\r\n"; | 32 | ··"HTTP/1.0·302·Moved·Temporarily\r\n"; |
34 | const·std::string·not_modified·= | 33 | const·std::string·not_modified·= |
35 | ··"HTTP/1.0·304·Not·Modified\r\n"; | 34 | ··"HTTP/1.0·304·Not·Modified\r\n"; |
36 | const·std::string·bad_request·= | 35 | const·std::string·bad_request·= |
37 | ··"HTTP/1.0·400·Bad·Request\r\n"; | 36 | ··"HTTP/1.0·400·Bad·Request\r\n"; |
38 | const·std::string·unauthorized·= | 37 | const·std::string·unauthorized·= |
39 | ··"HTTP/1.0·401·Unauthorized\r\n"; | 38 | ··"HTTP/1.0·401·Unauthorized\r\n"; |
40 | const·std::string·forbidden·= | 39 | const·std::string·forbidden·= |
41 | ··"HTTP/1.0·403·Forbidden\r\n"; | 40 | ··"HTTP/1.0·403·Forbidden\r\n"; |
42 | const·std::string·not_found·= | 41 | const·std::string·not_found·= |
43 | ··"HTTP/1.0·404·Not·Found\r\n"; | 42 | ··"HTTP/1.0·404·Not·Found\r\n"; |
44 | const·std::string·internal_server_error·= | 43 | const·std::string·internal_server_error·= |
45 | ··"HTTP/1.0·500·Internal·Server·Error\r\n"; | 44 | ··"HTTP/1.0·500·Internal·Server·Error\r\n"; |
46 | const·std::string·not_implemented·= | 45 | const·std::string·not_implemented·= |
47 | ··"HTTP/1.0·501·Not·Implemented\r\n"; | 46 | ··"HTTP/1.0·501·Not·Implemented\r\n"; |
48 | const·std::string·bad_gateway·= | 47 | const·std::string·bad_gateway·= |
49 | ··"HTTP/1.0·502·Bad·Gateway\r\n"; | 48 | ··"HTTP/1.0·502·Bad·Gateway\r\n"; |
50 | const·std::string·service_unavailable·= | 49 | const·std::string·service_unavailable·= |
51 | ··"HTTP/1.0·503·Service·Unavailable\r\n"; | 50 | ··"HTTP/1.0·503·Service·Unavailable\r\n"; |
52 | | 51 | |
53 | asio::const_buffer·to_buffer(reply::status_type·status) | 52 | asio::const_buffer·to_buffer(reply::status_type·status) |
54 | { | 53 | { |
55 | ··switch·(status) | 54 | ··switch·(status) |
56 | ··{ | 55 | ··{ |
57 | ··case·reply::ok: | 56 | ··case·reply::ok: |
58 | ····return·asio::buffer(ok); | 57 | ····return·asio::buffer(ok); |
59 | ··case·reply::created: | 58 | ··case·reply::created: |
60 | ····return·asio::buffer(created); | 59 | ····return·asio::buffer(created); |
61 | ··case·reply::accepted: | 60 | ··case·reply::accepted: |
62 | ····return·asio::buffer(accepted); | 61 | ····return·asio::buffer(accepted); |
63 | ··case·reply::no_content: | 62 | ··case·reply::no_content: |
64 | ····return·asio::buffer(no_content); | 63 | ····return·asio::buffer(no_content); |
65 | ··case·reply::multiple_choices: | 64 | ··case·reply::multiple_choices: |
66 | ····return·asio::buffer(multiple_choices); | 65 | ····return·asio::buffer(multiple_choices); |
67 | ··case·reply::moved_permanently: | 66 | ··case·reply::moved_permanently: |
68 | ····return·asio::buffer(moved_permanently); | 67 | ····return·asio::buffer(moved_permanently); |
69 | ··case·reply::moved_temporarily: | 68 | ··case·reply::moved_temporarily: |
70 | ····return·asio::buffer(moved_temporarily); | 69 | ····return·asio::buffer(moved_temporarily); |
71 | ··case·reply::not_modified: | 70 | ··case·reply::not_modified: |
72 | ····return·asio::buffer(not_modified); | 71 | ····return·asio::buffer(not_modified); |
73 | ··case·reply::bad_request: | 72 | ··case·reply::bad_request: |
74 | ····return·asio::buffer(bad_request); | 73 | ····return·asio::buffer(bad_request); |
75 | ··case·reply::unauthorized: | 74 | ··case·reply::unauthorized: |
76 | ····return·asio::buffer(unauthorized); | 75 | ····return·asio::buffer(unauthorized); |
77 | ··case·reply::forbidden: | 76 | ··case·reply::forbidden: |
78 | ····return·asio::buffer(forbidden); | 77 | ····return·asio::buffer(forbidden); |
79 | ··case·reply::not_found: | 78 | ··case·reply::not_found: |
80 | ····return·asio::buffer(not_found); | 79 | ····return·asio::buffer(not_found); |
81 | ··case·reply::internal_server_error: | 80 | ··case·reply::internal_server_error: |
82 | ····return·asio::buffer(internal_server_error); | 81 | ····return·asio::buffer(internal_server_error); |
83 | ··case·reply::not_implemented: | 82 | ··case·reply::not_implemented: |
84 | ····return·asio::buffer(not_implemented); | 83 | ····return·asio::buffer(not_implemented); |
85 | ··case·reply::bad_gateway: | 84 | ··case·reply::bad_gateway: |
86 | ····return·asio::buffer(bad_gateway); | 85 | ····return·asio::buffer(bad_gateway); |
87 | ··case·reply::service_unavailable: | 86 | ··case·reply::service_unavailable: |
88 | ····return·asio::buffer(service_unavailable); | 87 | ····return·asio::buffer(service_unavailable); |
89 | ··default: | 88 | ··default: |
90 | ····return·asio::buffer(internal_server_error); | 89 | ····return·asio::buffer(internal_server_error); |
91 | ··} | 90 | ··} |
92 | } | 91 | } |
93 | | 92 | |
94 | }·//·namespace·status_strings | 93 | }·//·namespace·status_strings |
95 | | 94 | |
96 | namespace·misc_strings·{ | 95 | namespace·misc_strings·{ |
97 | | 96 | |
98 | const·char·name_value_separator[]·=·{·':',·'·'·}; | 97 | const·char·name_value_separator[]·=·{·':',·'·'·}; |
99 | const·char·crlf[]·=·{·'\r',·'\n'·}; | 98 | const·char·crlf[]·=·{·'\r',·'\n'·}; |
100 | | 99 | |
101 | }·//·namespace·misc_strings | 100 | }·//·namespace·misc_strings |
102 | | 101 | |
103 | std::vector<asio::const_buffer>·reply::to_buffers() | 102 | std::vector<asio::const_buffer>·reply::to_buffers() |
104 | { | 103 | { |
105 | ··std::vector<asio::const_buffer>·buffers; | 104 | ··std::vector<asio::const_buffer>·buffers; |
106 | ··buffers.push_back(status_strings::to_buffer(status)); | 105 | ··buffers.push_back(status_strings::to_buffer(status)); |
107 | ··for·(std::size_t·i·=·0;·i·<·headers.size();·++i) | 106 | ··for·(std::size_t·i·=·0;·i·<·headers.size();·++i) |
108 | ··{ | 107 | ··{ |
109 | ····header&·h·=·headers[i]; | 108 | ····header&·h·=·headers[i]; |
110 | ····buffers.push_back(asio::buffer(h.name)); | 109 | ····buffers.push_back(asio::buffer(h.name)); |
111 | ····buffers.push_back(asio::buffer(misc_strings::name_value_separator)); | 110 | ····buffers.push_back(asio::buffer(misc_strings::name_value_separator)); |
112 | ····buffers.push_back(asio::buffer(h.value)); | 111 | ····buffers.push_back(asio::buffer(h.value)); |
113 | ····buffers.push_back(asio::buffer(misc_strings::crlf)); | 112 | ····buffers.push_back(asio::buffer(misc_strings::crlf)); |
114 | ··} | 113 | ··} |
115 | ··buffers.push_back(asio::buffer(misc_strings::crlf)); | 114 | ··buffers.push_back(asio::buffer(misc_strings::crlf)); |
116 | ··buffers.push_back(asio::buffer(content)); | 115 | ··buffers.push_back(asio::buffer(content)); |
117 | ··return·buffers; | 116 | ··return·buffers; |
118 | } | 117 | } |
119 | | 118 | |
120 | namespace·stock_replies·{ | 119 | namespace·stock_replies·{ |
121 | | 120 | |
122 | const·char·ok[]·=·""; | 121 | const·char·ok[]·=·""; |
123 | const·char·created[]·= | 122 | const·char·created[]·= |
124 | ··"<html>" | 123 | ··"<html>" |
125 | ··"<head><title>Created</title></head>" | 124 | ··"<head><title>Created</title></head>" |
126 | ··"<body><h1>201·Created</h1></body>" | 125 | ··"<body><h1>201·Created</h1></body>" |
127 | ··"</html>"; | 126 | ··"</html>"; |
128 | const·char·accepted[]·= | 127 | const·char·accepted[]·= |
129 | ··"<html>" | 128 | ··"<html>" |
130 | ··"<head><title>Accepted</title></head>" | 129 | ··"<head><title>Accepted</title></head>" |
131 | ··"<body><h1>202·Accepted</h1></body>" | 130 | ··"<body><h1>202·Accepted</h1></body>" |
132 | ··"</html>"; | 131 | ··"</html>"; |
133 | const·char·no_content[]·= | 132 | const·char·no_content[]·= |
134 | ··"<html>" | 133 | ··"<html>" |
135 | ··"<head><title>No·Content</title></head>" | 134 | ··"<head><title>No·Content</title></head>" |
136 | ··"<body><h1>204·Content</h1></body>" | 135 | ··"<body><h1>204·Content</h1></body>" |
137 | ··"</html>"; | 136 | ··"</html>"; |
138 | const·char·multiple_choices[]·= | 137 | const·char·multiple_choices[]·= |
139 | ··"<html>" | 138 | ··"<html>" |
140 | ··"<head><title>Multiple·Choices</title></head>" | 139 | ··"<head><title>Multiple·Choices</title></head>" |
141 | ··"<body><h1>300·Multiple·Choices</h1></body>" | 140 | ··"<body><h1>300·Multiple·Choices</h1></body>" |
142 | ··"</html>"; | 141 | ··"</html>"; |
143 | const·char·moved_permanently[]·= | 142 | const·char·moved_permanently[]·= |
144 | ··"<html>" | 143 | ··"<html>" |
145 | ··"<head><title>Moved·Permanently</title></head>" | 144 | ··"<head><title>Moved·Permanently</title></head>" |
146 | ··"<body><h1>301·Moved·Permanently</h1></body>" | 145 | ··"<body><h1>301·Moved·Permanently</h1></body>" |
147 | ··"</html>"; | 146 | ··"</html>"; |
148 | const·char·moved_temporarily[]·= | 147 | const·char·moved_temporarily[]·= |
149 | ··"<html>" | 148 | ··"<html>" |
150 | ··"<head><title>Moved·Temporarily</title></head>" | 149 | ··"<head><title>Moved·Temporarily</title></head>" |
151 | ··"<body><h1>302·Moved·Temporarily</h1></body>" | 150 | ··"<body><h1>302·Moved·Temporarily</h1></body>" |
152 | ··"</html>"; | 151 | ··"</html>"; |
153 | const·char·not_modified[]·= | 152 | const·char·not_modified[]·= |
154 | ··"<html>" | 153 | ··"<html>" |
155 | ··"<head><title>Not·Modified</title></head>" | 154 | ··"<head><title>Not·Modified</title></head>" |
156 | ··"<body><h1>304·Not·Modified</h1></body>" | 155 | ··"<body><h1>304·Not·Modified</h1></body>" |
157 | ··"</html>"; | 156 | ··"</html>"; |
158 | const·char·bad_request[]·= | 157 | const·char·bad_request[]·= |
159 | ··"<html>" | 158 | ··"<html>" |
160 | ··"<head><title>Bad·Request</title></head>" | 159 | ··"<head><title>Bad·Request</title></head>" |
161 | ··"<body><h1>400·Bad·Request</h1></body>" | 160 | ··"<body><h1>400·Bad·Request</h1></body>" |
162 | ··"</html>"; | 161 | ··"</html>"; |
163 | const·char·unauthorized[]·= | 162 | const·char·unauthorized[]·= |
164 | ··"<html>" | 163 | ··"<html>" |
165 | ··"<head><title>Unauthorized</title></head>" | 164 | ··"<head><title>Unauthorized</title></head>" |
166 | ··"<body><h1>401·Unauthorized</h1></body>" | 165 | ··"<body><h1>401·Unauthorized</h1></body>" |
167 | ··"</html>"; | 166 | ··"</html>"; |
168 | const·char·forbidden[]·= | 167 | const·char·forbidden[]·= |
169 | ··"<html>" | 168 | ··"<html>" |
170 | ··"<head><title>Forbidden</title></head>" | 169 | ··"<head><title>Forbidden</title></head>" |
171 | ··"<body><h1>403·Forbidden</h1></body>" | 170 | ··"<body><h1>403·Forbidden</h1></body>" |
172 | ··"</html>"; | 171 | ··"</html>"; |
173 | const·char·not_found[]·= | 172 | const·char·not_found[]·= |
174 | ··"<html>" | 173 | ··"<html>" |
175 | ··"<head><title>Not·Found</title></head>" | 174 | ··"<head><title>Not·Found</title></head>" |
176 | ··"<body><h1>404·Not·Found</h1></body>" | 175 | ··"<body><h1>404·Not·Found</h1></body>" |
177 | ··"</html>"; | 176 | ··"</html>"; |
178 | const·char·internal_server_error[]·= | 177 | const·char·internal_server_error[]·= |
179 | ··"<html>" | 178 | ··"<html>" |
180 | ··"<head><title>Internal·Server·Error</title></head>" | 179 | ··"<head><title>Internal·Server·Error</title></head>" |
181 | ··"<body><h1>500·Internal·Server·Error</h1></body>" | 180 | ··"<body><h1>500·Internal·Server·Error</h1></body>" |
182 | ··"</html>"; | 181 | ··"</html>"; |
183 | const·char·not_implemented[]·= | 182 | const·char·not_implemented[]·= |
184 | ··"<html>" | 183 | ··"<html>" |
185 | ··"<head><title>Not·Implemented</title></head>" | 184 | ··"<head><title>Not·Implemented</title></head>" |
186 | ··"<body><h1>501·Not·Implemented</h1></body>" | 185 | ··"<body><h1>501·Not·Implemented</h1></body>" |
187 | ··"</html>"; | 186 | ··"</html>"; |
188 | const·char·bad_gateway[]·= | 187 | const·char·bad_gateway[]·= |
189 | ··"<html>" | 188 | ··"<html>" |
190 | ··"<head><title>Bad·Gateway</title></head>" | 189 | ··"<head><title>Bad·Gateway</title></head>" |
191 | ··"<body><h1>502·Bad·Gateway</h1></body>" | 190 | ··"<body><h1>502·Bad·Gateway</h1></body>" |
192 | ··"</html>"; | 191 | ··"</html>"; |
193 | const·char·service_unavailable[]·= | 192 | const·char·service_unavailable[]·= |
194 | ··"<html>" | 193 | ··"<html>" |
195 | ··"<head><title>Service·Unavailable</title></head>" | 194 | ··"<head><title>Service·Unavailable</title></head>" |
196 | ··"<body><h1>503·Service·Unavailable</h1></body>" | 195 | ··"<body><h1>503·Service·Unavailable</h1></body>" |
197 | ··"</html>"; | 196 | ··"</html>"; |
198 | | 197 | |
199 | std::string·to_string(reply::status_type·status) | 198 | std::string·to_string(reply::status_type·status) |
200 | { | 199 | { |
201 | ··switch·(status) | 200 | ··switch·(status) |
202 | ··{ | 201 | ··{ |
203 | ··case·reply::ok: | 202 | ··case·reply::ok: |
204 | ····return·ok; | 203 | ····return·ok; |
205 | ··case·reply::created: | 204 | ··case·reply::created: |
206 | ····return·created; | 205 | ····return·created; |
207 | ··case·reply::accepted: | 206 | ··case·reply::accepted: |
208 | ····return·accepted; | 207 | ····return·accepted; |
209 | ··case·reply::no_content: | 208 | ··case·reply::no_content: |
210 | ····return·no_content; | 209 | ····return·no_content; |
211 | ··case·reply::multiple_choices: | 210 | ··case·reply::multiple_choices: |
212 | ····return·multiple_choices; | 211 | ····return·multiple_choices; |
213 | ··case·reply::moved_permanently: | 212 | ··case·reply::moved_permanently: |
214 | ····return·moved_permanently; | 213 | ····return·moved_permanently; |
215 | ··case·reply::moved_temporarily: | 214 | ··case·reply::moved_temporarily: |
216 | ····return·moved_temporarily; | 215 | ····return·moved_temporarily; |
217 | ··case·reply::not_modified: | 216 | ··case·reply::not_modified: |
218 | ····return·not_modified; | 217 | ····return·not_modified; |
219 | ··case·reply::bad_request: | 218 | ··case·reply::bad_request: |
220 | ····return·bad_request; | 219 | ····return·bad_request; |
221 | ··case·reply::unauthorized: | 220 | ··case·reply::unauthorized: |
222 | ····return·unauthorized; | 221 | ····return·unauthorized; |
223 | ··case·reply::forbidden: | 222 | ··case·reply::forbidden: |
224 | ····return·forbidden; | 223 | ····return·forbidden; |
225 | ··case·reply::not_found: | 224 | ··case·reply::not_found: |
226 | ····return·not_found; | 225 | ····return·not_found; |
227 | ··case·reply::internal_server_error: | 226 | ··case·reply::internal_server_error: |
228 | ····return·internal_server_error; | 227 | ····return·internal_server_error; |
229 | ··case·reply::not_implemented: | 228 | ··case·reply::not_implemented: |
230 | ····return·not_implemented; | 229 | ····return·not_implemented; |
231 | ··case·reply::bad_gateway: | 230 | ··case·reply::bad_gateway: |
232 | ····return·bad_gateway; | 231 | ····return·bad_gateway; |
233 | ··case·reply::service_unavailable: | 232 | ··case·reply::service_unavailable: |
234 | ····return·service_unavailable; | 233 | ····return·service_unavailable; |
235 | ··default: | 234 | ··default: |
236 | ····return·internal_server_error; | 235 | ····return·internal_server_error; |
237 | ··} | 236 | ··} |
238 | } | 237 | } |
239 | | 238 | |
240 | }·//·namespace·stock_replies | 239 | }·//·namespace·stock_replies |
241 | | 240 | |
242 | reply·reply::stock_reply(reply::status_type·status) | 241 | reply·reply::stock_reply(reply::status_type·status) |
243 | { | 242 | { |
244 | ··reply·rep; | 243 | ··reply·rep; |
245 | ··rep.status·=·status; | 244 | ··rep.status·=·status; |
246 | ··rep.content·=·stock_replies::to_string(status); | 245 | ··rep.content·=·stock_replies::to_string(status); |
247 | ··rep.headers.resize(2); | 246 | ··rep.headers.resize(2); |
248 | ··rep.headers[0].name·=·"Content-Length"; | 247 | ··rep.headers[0].name·=·"Content-Length"; |
249 | ··rep.headers[0].value·=·boost::lexical_cast<std::string>(rep.content.size()); | 248 | ··rep.headers[0].value·=·std::to_string(rep.content.size()); |
250 | ··rep.headers[1].name·=·"Content-Type"; | 249 | ··rep.headers[1].name·=·"Content-Type"; |
251 | ··rep.headers[1].value·=·"text/html"; | 250 | ··rep.headers[1].value·=·"text/html"; |
252 | ··return·rep; | 251 | ··return·rep; |
253 | } | 252 | } |
254 | | 253 | |
255 | }·//·namespace·server | 254 | }·//·namespace·server |
256 | }·//·namespace·http | 255 | }·//·namespace·http |