-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathKHttpResponse.cpp
More file actions
39 lines (34 loc) · 988 Bytes
/
Copy pathKHttpResponse.cpp
File metadata and controls
39 lines (34 loc) · 988 Bytes
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
#include "KHttpResponse.h"
#include "webserver/tcp/KSelectedBuffer.h"
#include <stdio.h>
using namespace kback;
void HttpResponse::appendToBuffer(Buffer *output) 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 {
if (sendFile_ == false) {
snprintf(buf, sizeof buf, "Content-Length: %zu\r\n", body_.size());
} else {
//
snprintf(buf, sizeof buf, "Content-Length: %zu\r\n", fileSize_);
}
output->append(buf);
output->append("Connection: Keep-Alive\r\n");
}
for (const auto &header : headers_) {
output->append(header.first);
output->append(": ");
output->append(header.second);
output->append("\r\n");
}
output->append("\r\n");
if (sendFile_ == false) // 不发送文件,则添加body_
{
output->append(body_);
}
}