1 | // |
2 | // HTTPServerResponseImpl.cpp |
3 | // |
4 | // Library: Net |
5 | // Package: HTTPServer |
6 | // Module: HTTPServerResponseImpl |
7 | // |
8 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Net/HTTPServerResponseImpl.h" |
16 | #include "Poco/Net/HTTPServerRequestImpl.h" |
17 | #include "Poco/Net/HTTPServerSession.h" |
18 | #include "Poco/Net/HTTPHeaderStream.h" |
19 | #include "Poco/Net/HTTPStream.h" |
20 | #include "Poco/Net/HTTPFixedLengthStream.h" |
21 | #include "Poco/Net/HTTPChunkedStream.h" |
22 | #include "Poco/File.h" |
23 | #include "Poco/Timestamp.h" |
24 | #include "Poco/NumberFormatter.h" |
25 | #include "Poco/StreamCopier.h" |
26 | #include "Poco/CountingStream.h" |
27 | #include "Poco/Exception.h" |
28 | #include "Poco/FileStream.h" |
29 | #include "Poco/DateTimeFormatter.h" |
30 | #include "Poco/DateTimeFormat.h" |
31 | |
32 | |
33 | using Poco::File; |
34 | using Poco::Timestamp; |
35 | using Poco::NumberFormatter; |
36 | using Poco::StreamCopier; |
37 | using Poco::OpenFileException; |
38 | using Poco::DateTimeFormatter; |
39 | using Poco::DateTimeFormat; |
40 | |
41 | |
42 | namespace Poco { |
43 | namespace Net { |
44 | |
45 | |
46 | HTTPServerResponseImpl::HTTPServerResponseImpl(HTTPServerSession& session): |
47 | _session(session), |
48 | _pRequest(0), |
49 | _pStream(0), |
50 | _pHeaderStream(0) |
51 | { |
52 | } |
53 | |
54 | |
55 | HTTPServerResponseImpl::~HTTPServerResponseImpl() |
56 | { |
57 | if (_pHeaderStream && _pHeaderStream != _pStream) |
58 | delete _pHeaderStream; |
59 | if (_pStream) |
60 | delete _pStream; |
61 | } |
62 | |
63 | |
64 | void HTTPServerResponseImpl::sendContinue() |
65 | { |
66 | HTTPHeaderOutputStream hs(_session); |
67 | hs << getVersion() << " 100 Continue\r\n\r\n" ; |
68 | } |
69 | |
70 | |
71 | std::ostream& HTTPServerResponseImpl::send() |
72 | { |
73 | poco_assert (!_pStream); |
74 | |
75 | if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) || |
76 | getStatus() < 200 || |
77 | getStatus() == HTTPResponse::HTTP_NO_CONTENT || |
78 | getStatus() == HTTPResponse::HTTP_NOT_MODIFIED) |
79 | { |
80 | Poco::CountingOutputStream cs; |
81 | write(cs); |
82 | _pStream = new HTTPFixedLengthOutputStream(_session, cs.chars()); |
83 | write(*_pStream); |
84 | } |
85 | else if (getChunkedTransferEncoding()) |
86 | { |
87 | HTTPHeaderOutputStream hs(_session); |
88 | write(hs); |
89 | _pStream = new HTTPChunkedOutputStream(_session); |
90 | } |
91 | else if (hasContentLength()) |
92 | { |
93 | Poco::CountingOutputStream cs; |
94 | write(cs); |
95 | #if defined(POCO_HAVE_INT64) |
96 | _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength64() + cs.chars()); |
97 | #else |
98 | _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength() + cs.chars()); |
99 | #endif |
100 | write(*_pStream); |
101 | } |
102 | else |
103 | { |
104 | _pStream = new HTTPOutputStream(_session); |
105 | setKeepAlive(false); |
106 | write(*_pStream); |
107 | } |
108 | return *_pStream; |
109 | } |
110 | |
111 | |
112 | std::pair<std::ostream *, std::ostream *> HTTPServerResponseImpl::beginSend() |
113 | { |
114 | poco_assert (!_pStream); |
115 | poco_assert (!_pHeaderStream); |
116 | |
117 | // NOTE Code is not exception safe. |
118 | |
119 | if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) || |
120 | getStatus() < 200 || |
121 | getStatus() == HTTPResponse::HTTP_NO_CONTENT || |
122 | getStatus() == HTTPResponse::HTTP_NOT_MODIFIED) |
123 | { |
124 | throw Exception("HTTPServerResponse::beginSend is invalid for HEAD request" ); |
125 | } |
126 | else if (getChunkedTransferEncoding()) |
127 | { |
128 | _pHeaderStream = new HTTPHeaderOutputStream(_session); |
129 | beginWrite(*_pHeaderStream); |
130 | _pStream = new HTTPChunkedOutputStream(_session); |
131 | } |
132 | else if (hasContentLength()) |
133 | { |
134 | throw Exception("HTTPServerResponse::beginSend is invalid for response with Content-Length header" ); |
135 | } |
136 | else |
137 | { |
138 | _pStream = new HTTPOutputStream(_session); |
139 | _pHeaderStream = _pStream; |
140 | setKeepAlive(false); |
141 | beginWrite(*_pStream); |
142 | } |
143 | |
144 | return std::make_pair(_pHeaderStream, _pStream); |
145 | } |
146 | |
147 | |
148 | void HTTPServerResponseImpl::sendFile(const std::string& path, const std::string& mediaType) |
149 | { |
150 | poco_assert (!_pStream); |
151 | |
152 | File f(path); |
153 | Timestamp dateTime = f.getLastModified(); |
154 | File::FileSize length = f.getSize(); |
155 | set("Last-Modified" , DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT)); |
156 | #if defined(POCO_HAVE_INT64) |
157 | setContentLength64(length); |
158 | #else |
159 | setContentLength(static_cast<int>(length)); |
160 | #endif |
161 | setContentType(mediaType); |
162 | setChunkedTransferEncoding(false); |
163 | |
164 | Poco::FileInputStream istr(path); |
165 | if (istr.good()) |
166 | { |
167 | _pStream = new HTTPHeaderOutputStream(_session); |
168 | write(*_pStream); |
169 | if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) |
170 | { |
171 | StreamCopier::copyStream(istr, *_pStream); |
172 | } |
173 | } |
174 | else throw OpenFileException(path); |
175 | } |
176 | |
177 | |
178 | void HTTPServerResponseImpl::sendBuffer(const void* pBuffer, std::size_t length) |
179 | { |
180 | poco_assert (!_pStream); |
181 | |
182 | setContentLength(static_cast<int>(length)); |
183 | setChunkedTransferEncoding(false); |
184 | |
185 | _pStream = new HTTPHeaderOutputStream(_session); |
186 | write(*_pStream); |
187 | if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) |
188 | { |
189 | _pStream->write(static_cast<const char*>(pBuffer), static_cast<std::streamsize>(length)); |
190 | } |
191 | } |
192 | |
193 | |
194 | void HTTPServerResponseImpl::redirect(const std::string& uri, HTTPStatus status) |
195 | { |
196 | poco_assert (!_pStream); |
197 | |
198 | setContentLength(0); |
199 | setChunkedTransferEncoding(false); |
200 | |
201 | setStatusAndReason(status); |
202 | set("Location" , uri); |
203 | |
204 | _pStream = new HTTPHeaderOutputStream(_session); |
205 | write(*_pStream); |
206 | } |
207 | |
208 | |
209 | void HTTPServerResponseImpl::requireAuthentication(const std::string& realm) |
210 | { |
211 | poco_assert (!_pStream); |
212 | |
213 | setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED); |
214 | std::string auth("Basic realm=\"" ); |
215 | auth.append(realm); |
216 | auth.append("\"" ); |
217 | set("WWW-Authenticate" , auth); |
218 | } |
219 | |
220 | |
221 | } } // namespace Poco::Net |
222 | |