src/method.cpp

96.7% Lines (178/184) 66.7% List of functions (2/3)
method.cpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
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 119x to_string(method v)
20 {
21 using namespace detail::string_literals;
22 119x switch(v)
23 {
24 3x case method::delete_: return "DELETE"_sv;
25 81x case method::get: return "GET"_sv;
26 1x case method::head: return "HEAD"_sv;
27 3x case method::post: return "POST"_sv;
28 1x case method::put: return "PUT"_sv;
29 1x case method::connect: return "CONNECT"_sv;
30 1x case method::options: return "OPTIONS"_sv;
31 1x case method::trace: return "TRACE"_sv;
32
33 1x case method::copy: return "COPY"_sv;
34 1x case method::lock: return "LOCK"_sv;
35 1x case method::mkcol: return "MKCOL"_sv;
36 1x case method::move: return "MOVE"_sv;
37 1x case method::propfind: return "PROPFIND"_sv;
38 1x case method::proppatch: return "PROPPATCH"_sv;
39 1x case method::search: return "SEARCH"_sv;
40 1x case method::unlock: return "UNLOCK"_sv;
41 1x case method::bind: return "BIND"_sv;
42 1x case method::rebind: return "REBIND"_sv;
43 1x case method::unbind: return "UNBIND"_sv;
44 1x case method::acl: return "ACL"_sv;
45
46 1x case method::report: return "REPORT"_sv;
47 1x case method::mkactivity: return "MKACTIVITY"_sv;
48 1x case method::checkout: return "CHECKOUT"_sv;
49 1x case method::merge: return "MERGE"_sv;
50
51 1x case method::msearch: return "M-SEARCH"_sv;
52 1x case method::notify: return "NOTIFY"_sv;
53 1x case method::subscribe: return "SUBSCRIBE"_sv;
54 1x case method::unsubscribe: return "UNSUBSCRIBE"_sv;
55
56 1x case method::patch: return "PATCH"_sv;
57 1x case method::purge: return "PURGE"_sv;
58
59 1x case method::mkcalendar: return "MKCALENDAR"_sv;
60
61 1x case method::link: return "LINK"_sv;
62 1x case method::unlink: return "UNLINK"_sv;
63
64 1x case method::unknown:
65 1x return "<unknown>"_sv;
66 }
67
68 2x BOOST_THROW_EXCEPTION(
69 std::invalid_argument("unknown method"));
70 }
71
72 method
73 9461x 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 9461x if(v.size() < 3)
113 return method::unknown;
114 9461x auto c = v[0];
115 9461x v.remove_prefix(1);
116 9461x switch(c)
117 {
118 2x case 'A':
119 2x if(v == "CL"_sv)
120 1x return method::acl;
121 1x break;
122
123 2x case 'B':
124 2x if(v == "IND"_sv)
125 1x return method::bind;
126 1x break;
127
128 11x case 'C':
129 11x c = v[0];
130 11x v.remove_prefix(1);
131 11x switch(c)
132 {
133 2x case 'H':
134 2x if(v == "ECKOUT"_sv)
135 1x return method::checkout;
136 1x break;
137
138 5x case 'O':
139 5x if(v == "NNECT"_sv)
140 2x return method::connect;
141 3x if(v == "PY"_sv)
142 1x return method::copy;
143 BOOST_FALLTHROUGH;
144
145 default:
146 6x break;
147 }
148 7x break;
149
150 5x case 'D':
151 5x if(v == "ELETE"_sv)
152 4x return method::delete_;
153 1x break;
154
155 9330x case 'G':
156 9330x if(v == "ET"_sv)
157 9329x return method::get;
158 1x break;
159
160 2x case 'H':
161 2x if(v == "EAD"_sv)
162 1x return method::head;
163 1x break;
164
165 4x case 'L':
166 4x if(v == "INK"_sv)
167 1x return method::link;
168 3x if(v == "OCK"_sv)
169 1x return method::lock;
170 2x break;
171
172 12x case 'M':
173 12x c = v[0];
174 12x v.remove_prefix(1);
175 12x switch(c)
176 {
177 2x case '-':
178 2x if(v == "SEARCH"_sv)
179 1x return method::msearch;
180 1x break;
181
182 2x case 'E':
183 2x if(v == "RGE"_sv)
184 1x return method::merge;
185 1x break;
186
187 6x case 'K':
188 6x if(v == "ACTIVITY"_sv)
189 1x return method::mkactivity;
190 5x if(v[0] == 'C')
191 {
192 4x v.remove_prefix(1);
193 4x if(v == "ALENDAR"_sv)
194 1x return method::mkcalendar;
195 3x if(v == "OL"_sv)
196 1x return method::mkcol;
197 2x break;
198 }
199 1x break;
200
201 2x case 'O':
202 2x if(v == "VE"_sv)
203 1x return method::move;
204 BOOST_FALLTHROUGH;
205
206 default:
207 1x break;
208 }
209 6x break;
210
211 2x case 'N':
212 2x if(v == "OTIFY"_sv)
213 1x return method::notify;
214 1x break;
215
216 3x case 'O':
217 3x if(v == "PTIONS"_sv)
218 2x return method::options;
219 1x break;
220
221 61x case 'P':
222 61x c = v[0];
223 61x v.remove_prefix(1);
224 61x switch(c)
225 {
226 2x case 'A':
227 2x if(v == "TCH"_sv)
228 1x return method::patch;
229 1x break;
230
231 49x case 'O':
232 49x if(v == "ST"_sv)
233 48x return method::post;
234 1x break;
235
236 4x case 'R':
237 4x if(v == "OPFIND"_sv)
238 1x return method::propfind;
239 3x if(v == "OPPATCH"_sv)
240 1x return method::proppatch;
241 2x break;
242
243 6x case 'U':
244 6x if(v == "RGE"_sv)
245 1x return method::purge;
246 5x if(v == "T"_sv)
247 3x return method::put;
248 BOOST_FALLTHROUGH;
249
250 default:
251 2x break;
252 }
253 6x break;
254
255 4x case 'R':
256 4x if(v[0] != 'E')
257 break;
258 4x v.remove_prefix(1);
259 4x if(v == "BIND"_sv)
260 1x return method::rebind;
261 3x if(v == "PORT"_sv)
262 1x return method::report;
263 2x break;
264
265 10x case 'S':
266 10x if(v == "EARCH"_sv)
267 1x return method::search;
268 9x if(v == "UBSCRIBE"_sv)
269 1x return method::subscribe;
270 8x break;
271
272 2x case 'T':
273 2x if(v == "RACE"_sv)
274 1x return method::trace;
275 1x break;
276
277 8x case 'U':
278 8x if(v[0] != 'N')
279 break;
280 8x v.remove_prefix(1);
281 8x if(v == "BIND"_sv)
282 1x return method::unbind;
283 7x if(v == "LINK"_sv)
284 1x return method::unlink;
285 6x if(v == "LOCK"_sv)
286 1x return method::unlock;
287 5x if(v == "SUBSCRIBE"_sv)
288 1x return method::unsubscribe;
289 4x break;
290
291 3x default:
292 3x break;
293 }
294
295 46x 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