forked from chunyi1994/cppevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_message.cpp
178 lines (151 loc) · 3.85 KB
/
http_message.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <assert.h>
#include "http_message.h"
#include "utils.h"
namespace cppevent{
HttpMessage::HttpMessage() :
headersMap_(),
path_(),
version_(),
method_(),
status_(),
statusCode_(0)
{
}
std::string &HttpMessage::operator[](const std::string &key)
{
return headersMap_[key];
}
void HttpMessage::addHeaders(const std::string &key, const std::string &value)
{
headersMap_[key] = value;
}
void HttpMessage::setStatusLine(const std::string &version, int statusCode, const std::string &status)
{
type_ = TYPE_HTTP_RESPONSE;
version_ = version;
statusCode_ = statusCode;
status_ = status;
}
void HttpMessage::setRequestLine(const std::string &method, const std::string &path, const std::string &version)
{
type_ = TYPE_HTTP_REQUEST;
method_ = method;
path_ = path;
version_ = version;
}
std::string HttpMessage::toString() const
{
std::string line;
std::string msg;
if(type_ == TYPE_HTTP_REQUEST){
line = method_ + " " + path_ + " " + version_ + "\r\n";
}else{
line = version_ + " " + int2string(statusCode_) + " " + status_ + "\r\n";
}
msg.append(line);
for(const auto &w : headersMap_){
line = w.first + ": " + w.second + "\r\n";
msg.append(line);
}
msg.append("\r\n");
return msg;
}
std::string HttpMessage::method() const
{
return method_;
}
std::string HttpMessage::path() const
{
return path_;
}
std::string HttpMessage::version() const
{
return version_;
}
int HttpMessage::statusCode() const
{
return statusCode_;
}
void HttpMessage::parse(const std::string &content, int type)
{
type_ = type;
assert(content.length() > 0);
std::string line;
std::stringstream ss(content);
std::getline(ss, line, '\r');
size_t pos;
size_t left;
if(type_ == TYPE_HTTP_REQUEST){
//method
pos = line.find(" ");
assert(pos != std::string::npos);
method_ = line.substr(0, pos);
//path
left = pos + 1;
pos = line.find(" ", left);
assert(pos != std::string::npos);
path_ = line.substr(left, pos - left);
//version
left = pos + 1;
pos = line.length();
version_ = line.substr(left, pos - left);
}else{
//version
pos = line.find(" ");
assert(pos != std::string::npos);
version_ = line.substr(0, pos);
//status code
left = pos + 1;
pos = line.find(" ", left);
assert(pos != std::string::npos);
std::stringstream codess;
codess << line.substr(left, pos);
codess >> statusCode_;
}
//让getline用\r来当换行符,则下一行的开头会是'\n'.
//因为设置以后他不具备换行作用了.
headersMap_.clear();
while(getline(ss, line, '\r')){
//name
pos = line.find(":");
if(pos == std::string::npos){
break;
}
std::string name = line.substr(1, pos - 1);
//value
left = pos + 2;
pos = line.length() - 1;
std::string value = line.substr(left, pos);
trim(value);//去掉左右空格
headersMap_[name] = value;
}
}
HttpMessage::HeadersMap::iterator HttpMessage::find(const std::__cxx11::string &key)
{
return headersMap_.find(key);
}
HttpMessage::HeadersMap::iterator HttpMessage::begin()
{
return headersMap_.begin();
}
HttpMessage::HeadersMap::iterator HttpMessage::end()
{
return headersMap_.end();
}
HttpMessage::HeadersMap::const_iterator HttpMessage::begin() const
{
return headersMap_.cbegin();
}
HttpMessage::HeadersMap::const_iterator HttpMessage::end() const
{
return headersMap_.cend();
}
HttpMessage::HeadersMap::const_iterator HttpMessage::cbegin() const
{
return headersMap_.cbegin();
}
HttpMessage::HeadersMap::const_iterator HttpMessage::cend() const
{
return headersMap_.cend();
}
}