Repository navigation
Expand file tree
/
Copy pathHTTPServerResponse.cpp
More file actions
152 lines (124 loc) · 4.72 KB
/
Copy pathHTTPServerResponse.cpp
File metadata and controls
152 lines (124 loc) · 4.72 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
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
#include <Server/HTTP/HTTPServerResponse.h>
#include <IO/AutoFinalizedWriteBuffer.h>
#include <IO/WriteBuffer.h>
#include <Server/HTTP/HTTPServerRequest.h>
#include <Poco/DateTimeFormat.h>
#include <Poco/DateTimeFormatter.h>
#include <Poco/Net/HTTPChunkedStream.h>
#include <Poco/Net/HTTPFixedLengthStream.h>
#include <Poco/Net/HTTPHeaderStream.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPStream.h>
#include <IO/NullWriteBuffer.h>
namespace DB
{
HTTPServerResponse::HTTPServerResponse(Poco::Net::HTTPServerSession & session_, const ProfileEvents::Event & write_event_)
: session(session_)
, write_event(write_event_)
{
}
void HTTPServerResponse::sendContinue()
{
Poco::Net::HTTPHeaderOutputStream hs(session);
hs << getVersion() << " 100 Continue\r\n\r\n";
}
std::shared_ptr<WriteBuffer> HTTPServerResponse::send()
{
poco_assert(!stream);
if (request && request->getMethod() == HTTPRequest::HTTP_HEAD)
{
// HTTP_HEAD is a special case
// client usually reads nothing from socket after headers even when 'Contex-Lenght' is sent
// if server wrote a message to the connection with enabled 'Connection: Keep-Alive'
// the connection would be poisoned.
// Next request over that connection reads previously unreaded message as a HTTP status line
// make sure that nothing is sent to the client if it was HTTP_HEAD request
stream = std::make_shared<NullWriteBuffer>(write_event);
}
else if (getStatus() < 200 || getStatus() == HTTPResponse::HTTP_NOT_MODIFIED || getStatus() == HTTPResponse::HTTP_NO_CONTENT)
{
// I really do not know why do we consider this cases as special one
// but if we do, then it is safer to close the connection at the end
setKeepAlive(false);
stream = std::make_shared<AutoFinalizedWriteBuffer<WriteBufferFromPocoSocket>>(session.socket(), write_event);
}
else if (getChunkedTransferEncoding())
{
stream = std::make_shared<AutoFinalizedWriteBuffer<HTTPWriteBufferChunked>>(session.socket(), write_event);
}
else if (hasContentLength())
{
stream = std::make_shared<AutoFinalizedWriteBuffer<HTTPWriteBufferFixedLength>>(session.socket(), getContentLength(), write_event);
}
else
{
setKeepAlive(false);
stream = std::make_shared<AutoFinalizedWriteBuffer<WriteBufferFromPocoSocket>>(session.socket(), write_event);
}
Poco::Net::HTTPHeaderOutputStream hs(session);
writeStatusAndHeaders(hs);
return stream;
}
/// Only this method is called inside WriteBufferFromHTTPServerResponse
void HTTPServerResponse::writeStatus(std::ostream & ostr)
{
ostr << getVersion() << " " << static_cast<int>(getStatus()) << " " << getReason() << "\r\n";
ostr.flush();
}
/// Only this method is called inside WriteBufferFromHTTPServerResponse
void HTTPServerResponse::writeHeaders(std::ostream & ostr)
{
allowKeepAliveIFFRequestIsFullyRead();
Poco::Net::HTTPMessage::write(ostr); // NOLINT (bugprone-parent-virtual-call)
ostr << "\r\n";
ostr.flush();
send_started = true;
}
void HTTPServerResponse::writeStatusAndHeaders(std::ostream & ostr)
{
writeStatus(ostr);
writeHeaders(ostr);
}
void HTTPServerResponse::sendBuffer(const void * buffer, std::size_t length)
{
setContentLength(static_cast<int>(length));
setChunkedTransferEncoding(false);
// Send header
Poco::Net::HTTPHeaderOutputStream hs(session);
writeStatusAndHeaders(hs);
if (request && request->getMethod() != HTTPRequest::HTTP_HEAD)
{
auto wb = WriteBufferFromPocoSocket(session.socket(), write_event);
wb.write(static_cast<const char *>(buffer), length);
wb.finalize();
}
}
void HTTPServerResponse::requireAuthentication(const std::string & realm)
{
poco_assert(!stream);
setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED);
std::string auth("Basic realm=\"");
auth.append(realm);
auth.append("\"");
set("WWW-Authenticate", auth);
}
void HTTPServerResponse::redirect(const std::string & uri, HTTPStatus status)
{
poco_assert(!stream);
setContentLength(0);
setChunkedTransferEncoding(false);
setStatusAndReason(status);
set("Location", uri);
// Send header
Poco::Net::HTTPHeaderOutputStream hs(session);
writeStatusAndHeaders(hs);
}
void HTTPServerResponse::allowKeepAliveIFFRequestIsFullyRead()
{
/// Connection can only be reused if we've fully read the previous request and all its POST data.
/// Otherwise we'd misinterpret the leftover data as part of the next request's header.
/// HTTPServerRequest::canKeepAlive() checks that request stream is bounded and is fully read.
if (!request || !request->canKeepAlive())
setKeepAlive(false);
}
}