-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_printer.cpp
91 lines (78 loc) · 2.01 KB
/
http_printer.cpp
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
#include "http_printer.h"
#include <cassert>
namespace
{
char const* request_method_string(http_request_method method)
{
switch (method)
{
case http_request_method::OPTIONS:
return "OPTIONS";
case http_request_method::GET:
return "GET";
case http_request_method::HEAD:
return "HEAD";
case http_request_method::POST:
return "POST";
case http_request_method::PUT:
return "PUT";
case http_request_method::DELETE:
return "DELETE";
case http_request_method::TRACE:
return "TRACE";
case http_request_method::CONNECT:
return "CONNECT";
default:
assert(false);
return "GET";
}
}
const char* http_version_string(http_version version)
{
switch (version)
{
case http_version::HTTP_10:
return "HTTP/1.0";
case http_version::HTTP_11:
return "HTTP/1.1";
default:
assert(false);
return "HTTP/1.0";
}
}
}
std::ostream& operator<<(std::ostream& os, sub_string str)
{
os.write(str.data(), str.size());
return os;
}
std::ostream& operator<<(std::ostream& os, http_request_method method)
{
os << request_method_string(method);
return os;
}
std::ostream& operator<<(std::ostream& os, http_version version)
{
os << http_version_string(version);
return os;
}
std::ostream& operator<<(std::ostream& os, http_status_line const& status_line)
{
os << status_line.version << ' '
<< static_cast<unsigned>(status_line.status_code) << ' '
<< status_line.reason_phrase;
return os;
}
std::ostream& operator<<(std::ostream& os, http_response const& response)
{
os << response.status_line << '\n';
for (auto const& p : response.headers)
{
for (auto const& v : p.second)
{
os << p.first << ": " << v << '\n';
}
}
os << '\n';
return os;
}