forked from wnewbery/cpphttp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethod.cpp
More file actions
44 lines (43 loc) · 1.15 KB
/
Copy pathMethod.cpp
File metadata and controls
44 lines (43 loc) · 1.15 KB
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
#include "Method.hpp"
#include <stdexcept>
#include <unordered_map>
namespace http
{
namespace
{
const std::unordered_map<std::string, Method> STR_METHOD =
{
{ "GET", GET },
{ "HEAD", HEAD },
{ "POST", POST },
{ "PUT", PUT },
{ "DELETE", DELETE },
{ "TRACE", TRACE },
{ "OPTIONS", OPTIONS },
{ "CONNECT", CONNECT },
{ "PATCH", PATCH }
};
}
Method method_from_string(const std::string &str)
{
auto it = STR_METHOD.find(str);
if (it != STR_METHOD.end()) return it->second;
else throw std::runtime_error("Invalid HTTP method " + str);
}
std::string to_string(Method method)
{
switch (method)
{
case GET: return "GET";
case HEAD: return "HEAD";
case POST: return "POST";
case PUT: return "PUT";
case DELETE: return "DELETE";
case TRACE: return "TRACE";
case OPTIONS: return "OPTIONS";
case CONNECT: return "CONNECT";
case PATCH: return "PATCH";
default: std::terminate();
}
}
}