-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResponse.cpp
More file actions
85 lines (72 loc) · 1.59 KB
/
HttpResponse.cpp
File metadata and controls
85 lines (72 loc) · 1.59 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
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
#include "HttpResponse.h"
void HttpResponse::setStatusCode(HttpStatusCode code)
{
statusCode_=code;
}
void HttpResponse::setStatusMessage(const string& message)
{
statusMessage_ = message;
}
void HttpResponse::setCloseConnection(bool on)
{
closeConnection_ = on;
}
bool HttpResponse::closeConnection() const
{
return closeConnection_;
}
void HttpResponse::setContentType(const string& contentType)
{
addHeader("Content-Type", contentType);
}
void HttpResponse::addHeader(const string& key, const string& value)
{
headers_[key] = value;
}
void HttpResponse::setBody(const string& body)
{
body_ = body;
}
void HttpResponse::setCgi(int c)
{
cgi_=c;
}
int HttpResponse::cgi()const
{
return cgi_;
}
void HttpResponse::appendToBuffer(muduo::net::Buffer* output,int cgi) const
{
char buf[32];
//构造响应行
snprintf(buf, sizeof buf, "HTTP/1.1 %d ", statusCode_);
output->append(buf);
output->append(statusMessage_);
output->append("\r\n");
if (closeConnection_)
{
//
output->append("Connection: close\r\n");
}
else
{
//Keep-Alive需要Content-Length
snprintf(buf, sizeof buf, "Content-Length: %zd\r\n", body_.size());
output->append(buf);
output->append("Connection: Keep-Alive\r\n");
}
for (std::map<string, string>::const_iterator it = headers_.begin();
it != headers_.end();
++it)
{
//迭代构造响应头
output->append(it->first);
output->append(": ");
output->append(it->second);
output->append("\r\n");
}
if(!cgi)
output->append("\r\n");
//响应报文
output->append(body_);
}