Newer
Older
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_response.hpp"
#include <unistd.h>
#include <sys/socket.h>
#include "http_err.hpp"
HttpResponse::HttpResponse(int sockfd)
: sockfd{sockfd}
{
status = 200;
statusPhrase = "OK";
httpver = "HTTP/1.1";
headers.setHeader(HttpHeader::ContentType, "text/html; charset=utf-8");
}
void HttpResponse::setStatus(uint16_t statusCode, std::string _statusPhrase)
{
status = statusCode;
statusPhrase = _statusPhrase;
}
void HttpResponse::setHttpver(std::string _httpver)
{
httpver = _httpver;
}
HttpHeaders & HttpResponse::getHeadersWritable()
{
return headers;
}
void HttpResponse::rawWriteAll(int sockfd, const uint8_t *data, size_t dataLength)
{
size_t bytes_written_total = 0;
ssize_t bytes_written = 0;
do
{
bytes_written = write(sockfd, data + bytes_written_total, dataLength-bytes_written_total);
if (bytes_written < 0)
{
throw HttpException(HttpException::TcpSend);
}
bytes_written_total += bytes_written;
} while (bytes_written_total != dataLength);
}
void HttpResponse::sendHeader()
{
std::string head = httpver + " " + std::to_string(status) + " " + statusPhrase + "\r\n";
for (auto h : headers.getRawHeaders())
{
head += h.first + ": " + h.second.getValue() + "\r\n";
}
head += "\r\n";
rawWriteAll(sockfd, (uint8_t*)head.c_str(), head.size());
}
void HttpResponse::sendAll(const uint8_t *bodyData, size_t bodyLength)
{
sendHeader();
sendBody(bodyData, bodyLength);
}
void HttpResponse::send(const uint8_t *bodyData, size_t bodyLength)
{
sendAll(bodyData, bodyLength);
}
void HttpResponse::sendBody(const uint8_t *bodyData, size_t bodyLength)
{
rawWriteAll(sockfd, bodyData, bodyLength);
}
void HttpResponse::sendDefault404()
{
status = 404;
statusPhrase = "Not found";
headers.setHeader(HttpHeader::ContentType, "text/html; charset=utf-8");
char body[] = "404 Not found";
sendAll((uint8_t*)body, sizeof(body));
}