-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.cpp
156 lines (144 loc) · 2.73 KB
/
HttpRequest.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
#include "HttpRequest.h"
HttpRequest::HttpRequest(): method_(INVALID),
version_(UNKNOWN)
{
}
void HttpRequest::setVersion(Version v)
{
version_=v;
}
HttpRequest::Version HttpRequest::getVersion()const
{
return version_;
}
bool HttpRequest::setMethod(const char* start, const char* end)
{
assert(method_ == INVALID);
string m(start, end);
if (m == "GET")
{
method_ = GET;
}
else if (m == "POST")
{
method_ = POST;
}
else if (m == "HEAD")
{
method_ = HEAD;
}
else if (m == "PUT")
{
method_ = PUT;
}
else if (m == "DELETE")
{
method_ = DELETE;
}
else
{
method_ = INVALID;
}
return method_ != INVALID;
}
HttpRequest::Method HttpRequest::method() const
{
return method_;
}
const char* HttpRequest::methodString() const
{
const char* result = "UNKNOWN";
switch(method_)
{
case GET:
result = "GET";
break;
case POST:
result = "POST";
break;
case HEAD:
result = "HEAD";
break;
case PUT:
result = "PUT";
break;
case DELETE:
result = "DELETE";
break;
default:
break;
}
return result;
}
void HttpRequest::setPath(const char* start, const char* end)
{
path_.assign(start, end);
}
const string& HttpRequest::path() const
{
return path_;
}
void HttpRequest::setQuery(const char* start, const char* end)
{
query_.assign(start, end);
}
const string& HttpRequest::query() const
{
return query_;
}
void HttpRequest::setReceiveTime(muduo::Timestamp t)
{
receiveTime_ = t;
}
muduo::Timestamp HttpRequest::receiveTime() const
{
return receiveTime_;
}
void HttpRequest::addHeader(const char* start,
const char* colon,
const char* end)
{
string field(start, colon);
++colon;
while (colon < end && isspace(*colon))
{
++colon;
}
string value(colon, end);
while (!value.empty() && isspace(value[value.size()-1]))
{
value.resize(value.size()-1);
}
headers_[field] = value;//将解析出来的头信息放入map中
}
string HttpRequest::getHeader(const string& field) const
{
string result;
std::map<string, string>::const_iterator it = headers_.find(field);
if (it != headers_.end())
{
result = it->second;
}
return result;
}
const std::map<string, string>& HttpRequest::headers() const
{
return headers_;
}
void HttpRequest::swap(HttpRequest& that)
{
std::swap(method_, that.method_);
path_.swap(that.path_);
query_.swap(that.query_);
receiveTime_.swap(that.receiveTime_);
headers_.swap(that.headers_);
body_.swap(that.body_);
}
void HttpRequest::setBody(const char*start,const char*end)
{
body_.assign(start,end);
}
string HttpRequest::getBody() const
{
return body_;
}