src/method.cpp

96.7% Lines (178/184) 66.7% Functions (2/3)
src/method.cpp
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2021 Vinnie Falco ([email protected])
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/http
8 //
9
10 #include <boost/http/method.hpp>
11 #include <boost/http/detail/sv.hpp>
12 #include <boost/throw_exception.hpp>
13 #include <ostream>
14
15 namespace boost {
16 namespace http {
17
18 core::string_view
19 119 to_string(method v)
20 {
21 using namespace detail::string_literals;
22 119 switch(v)
23 {
24 3 case method::delete_: return "DELETE"_sv;
25 81 case method::get: return "GET"_sv;
26 1 case method::head: return "HEAD"_sv;
27 3 case method::post: return "POST"_sv;
28 1 case method::put: return "PUT"_sv;
29 1 case method::connect: return "CONNECT"_sv;
30 1 case method::options: return "OPTIONS"_sv;
31 1 case method::trace: return "TRACE"_sv;
32
33 1 case method::copy: return "COPY"_sv;
34 1 case method::lock: return "LOCK"_sv;
35 1 case method::mkcol: return "MKCOL"_sv;
36 1 case method::move: return "MOVE"_sv;
37 1 case method::propfind: return "PROPFIND"_sv;
38 1 case method::proppatch: return "PROPPATCH"_sv;
39 1 case method::search: return "SEARCH"_sv;
40 1 case method::unlock: return "UNLOCK"_sv;
41 1 case method::bind: return "BIND"_sv;
42 1 case method::rebind: return "REBIND"_sv;
43 1 case method::unbind: return "UNBIND"_sv;
44 1 case method::acl: return "ACL"_sv;
45
46 1 case method::report: return "REPORT"_sv;
47 1 case method::mkactivity: return "MKACTIVITY"_sv;
48 1 case method::checkout: return "CHECKOUT"_sv;
49 1 case method::merge: return "MERGE"_sv;
50
51 1 case method::msearch: return "M-SEARCH"_sv;
52 1 case method::notify: return "NOTIFY"_sv;
53 1 case method::subscribe: return "SUBSCRIBE"_sv;
54 1 case method::unsubscribe: return "UNSUBSCRIBE"_sv;
55
56 1 case method::patch: return "PATCH"_sv;
57 1 case method::purge: return "PURGE"_sv;
58
59 1 case method::mkcalendar: return "MKCALENDAR"_sv;
60
61 1 case method::link: return "LINK"_sv;
62 1 case method::unlink: return "UNLINK"_sv;
63
64 1 case method::unknown:
65 1 return "<unknown>"_sv;
66 }
67
68 2 BOOST_THROW_EXCEPTION(
69 std::invalid_argument("unknown method"));
70 }
71
72 method
73 9461 string_to_method(
74 core::string_view v)
75 {
76 /*
77 ACL
78 BIND
79 CHECKOUT
80 CONNECT
81 COPY
82 DELETE
83 GET
84 HEAD
85 LINK
86 LOCK
87 M-SEARCH
88 MERGE
89 MKACTIVITY
90 MKCALENDAR
91 MKCOL
92 MOVE
93 NOTIFY
94 OPTIONS
95 PATCH
96 POST
97 PROPFIND
98 PROPPATCH
99 PURGE
100 PUT
101 REBIND
102 REPORT
103 SEARCH
104 SUBSCRIBE
105 TRACE
106 UNBIND
107 UNLINK
108 UNLOCK
109 UNSUBSCRIBE
110 */
111 using namespace detail::string_literals;
112 9461 if(v.size() < 3)
113 return method::unknown;
114 9461 auto c = v[0];
115 9461 v.remove_prefix(1);
116 9461 switch(c)
117 {
118 2 case 'A':
119 2 if(v == "CL"_sv)
120 1 return method::acl;
121 1 break;
122
123 2 case 'B':
124 2 if(v == "IND"_sv)
125 1 return method::bind;
126 1 break;
127
128 11 case 'C':
129 11 c = v[0];
130 11 v.remove_prefix(1);
131 11 switch(c)
132 {
133 2 case 'H':
134 2 if(v == "ECKOUT"_sv)
135 1 return method::checkout;
136 1 break;
137
138 5 case 'O':
139 5 if(v == "NNECT"_sv)
140 2 return method::connect;
141 3 if(v == "PY"_sv)
142 1 return method::copy;
143 BOOST_FALLTHROUGH;
144
145 default:
146 6 break;
147 }
148 7 break;
149
150 5 case 'D':
151 5 if(v == "ELETE"_sv)
152 4 return method::delete_;
153 1 break;
154
155 9330 case 'G':
156 9330 if(v == "ET"_sv)
157 9329 return method::get;
158 1 break;
159
160 2 case 'H':
161 2 if(v == "EAD"_sv)
162 1 return method::head;
163 1 break;
164
165 4 case 'L':
166 4 if(v == "INK"_sv)
167 1 return method::link;
168 3 if(v == "OCK"_sv)
169 1 return method::lock;
170 2 break;
171
172 12 case 'M':
173 12 c = v[0];
174 12 v.remove_prefix(1);
175 12 switch(c)
176 {
177 2 case '-':
178 2 if(v == "SEARCH"_sv)
179 1 return method::msearch;
180 1 break;
181
182 2 case 'E':
183 2 if(v == "RGE"_sv)
184 1 return method::merge;
185 1 break;
186
187 6 case 'K':
188 6 if(v == "ACTIVITY"_sv)
189 1 return method::mkactivity;
190 5 if(v[0] == 'C')
191 {
192 4 v.remove_prefix(1);
193 4 if(v == "ALENDAR"_sv)
194 1 return method::mkcalendar;
195 3 if(v == "OL"_sv)
196 1 return method::mkcol;
197 2 break;
198 }
199 1 break;
200
201 2 case 'O':
202 2 if(v == "VE"_sv)
203 1 return method::move;
204 BOOST_FALLTHROUGH;
205
206 default:
207 1 break;
208 }
209 6 break;
210
211 2 case 'N':
212 2 if(v == "OTIFY"_sv)
213 1 return method::notify;
214 1 break;
215
216 3 case 'O':
217 3 if(v == "PTIONS"_sv)
218 2 return method::options;
219 1 break;
220
221 61 case 'P':
222 61 c = v[0];
223 61 v.remove_prefix(1);
224 61 switch(c)
225 {
226 2 case 'A':
227 2 if(v == "TCH"_sv)
228 1 return method::patch;
229 1 break;
230
231 49 case 'O':
232 49 if(v == "ST"_sv)
233 48 return method::post;
234 1 break;
235
236 4 case 'R':
237 4 if(v == "OPFIND"_sv)
238 1 return method::propfind;
239 3 if(v == "OPPATCH"_sv)
240 1 return method::proppatch;
241 2 break;
242
243 6 case 'U':
244 6 if(v == "RGE"_sv)
245 1 return method::purge;
246 5 if(v == "T"_sv)
247 3 return method::put;
248 BOOST_FALLTHROUGH;
249
250 default:
251 2 break;
252 }
253 6 break;
254
255 4 case 'R':
256 4 if(v[0] != 'E')
257 break;
258 4 v.remove_prefix(1);
259 4 if(v == "BIND"_sv)
260 1 return method::rebind;
261 3 if(v == "PORT"_sv)
262 1 return method::report;
263 2 break;
264
265 10 case 'S':
266 10 if(v == "EARCH"_sv)
267 1 return method::search;
268 9 if(v == "UBSCRIBE"_sv)
269 1 return method::subscribe;
270 8 break;
271
272 2 case 'T':
273 2 if(v == "RACE"_sv)
274 1 return method::trace;
275 1 break;
276
277 8 case 'U':
278 8 if(v[0] != 'N')
279 break;
280 8 v.remove_prefix(1);
281 8 if(v == "BIND"_sv)
282 1 return method::unbind;
283 7 if(v == "LINK"_sv)
284 1 return method::unlink;
285 6 if(v == "LOCK"_sv)
286 1 return method::unlock;
287 5 if(v == "SUBSCRIBE"_sv)
288 1 return method::unsubscribe;
289 4 break;
290
291 3 default:
292 3 break;
293 }
294
295 46 return method::unknown;
296 }
297
298 std::ostream&
299 operator<<(
300 std::ostream& os,
301 method v)
302 {
303 os << to_string(v);
304 return os;
305 }
306
307 } // http
308 } // boost
309