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
33using Poco::File;
34using Poco::Timestamp;
35using Poco::NumberFormatter;
36using Poco::StreamCopier;
37using Poco::OpenFileException;
38using Poco::DateTimeFormatter;
39using Poco::DateTimeFormat;
40
41
42namespace Poco {
43namespace Net {
44
45
46HTTPServerResponseImpl::HTTPServerResponseImpl(HTTPServerSession& session):
47 _session(session),
48 _pRequest(0),
49 _pStream(0)
50{
51}
52
53
54HTTPServerResponseImpl::~HTTPServerResponseImpl()
55{
56 delete _pStream;
57}
58
59
60void HTTPServerResponseImpl::sendContinue()
61{
62 HTTPHeaderOutputStream hs(_session);
63 hs << getVersion() << " 100 Continue\r\n\r\n";
64}
65
66
67std::ostream& HTTPServerResponseImpl::send()
68{
69 poco_assert (!_pStream);
70
71 if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) ||
72 getStatus() < 200 ||
73 getStatus() == HTTPResponse::HTTP_NO_CONTENT ||
74 getStatus() == HTTPResponse::HTTP_NOT_MODIFIED)
75 {
76 Poco::CountingOutputStream cs;
77 write(cs);
78 _pStream = new HTTPFixedLengthOutputStream(_session, cs.chars());
79 write(*_pStream);
80 }
81 else if (getChunkedTransferEncoding())
82 {
83 HTTPHeaderOutputStream hs(_session);
84 write(hs);
85 _pStream = new HTTPChunkedOutputStream(_session);
86 }
87 else if (hasContentLength())
88 {
89 Poco::CountingOutputStream cs;
90 write(cs);
91#if defined(POCO_HAVE_INT64)
92 _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength64() + cs.chars());
93#else
94 _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength() + cs.chars());
95#endif
96 write(*_pStream);
97 }
98 else
99 {
100 _pStream = new HTTPOutputStream(_session);
101 setKeepAlive(false);
102 write(*_pStream);
103 }
104 return *_pStream;
105}
106
107
108void HTTPServerResponseImpl::sendFile(const std::string& path, const std::string& mediaType)
109{
110 poco_assert (!_pStream);
111
112 File f(path);
113 Timestamp dateTime = f.getLastModified();
114 File::FileSize length = f.getSize();
115 set("Last-Modified", DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT));
116#if defined(POCO_HAVE_INT64)
117 setContentLength64(length);
118#else
119 setContentLength(static_cast<int>(length));
120#endif
121 setContentType(mediaType);
122 setChunkedTransferEncoding(false);
123
124 Poco::FileInputStream istr(path);
125 if (istr.good())
126 {
127 _pStream = new HTTPHeaderOutputStream(_session);
128 write(*_pStream);
129 if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD)
130 {
131 StreamCopier::copyStream(istr, *_pStream);
132 }
133 }
134 else throw OpenFileException(path);
135}
136
137
138void HTTPServerResponseImpl::sendBuffer(const void* pBuffer, std::size_t length)
139{
140 poco_assert (!_pStream);
141
142 setContentLength(static_cast<int>(length));
143 setChunkedTransferEncoding(false);
144
145 _pStream = new HTTPHeaderOutputStream(_session);
146 write(*_pStream);
147 if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD)
148 {
149 _pStream->write(static_cast<const char*>(pBuffer), static_cast<std::streamsize>(length));
150 }
151}
152
153
154void HTTPServerResponseImpl::redirect(const std::string& uri, HTTPStatus status)
155{
156 poco_assert (!_pStream);
157
158 setContentLength(0);
159 setChunkedTransferEncoding(false);
160
161 setStatusAndReason(status);
162 set("Location", uri);
163
164 _pStream = new HTTPHeaderOutputStream(_session);
165 write(*_pStream);
166}
167
168
169void HTTPServerResponseImpl::requireAuthentication(const std::string& realm)
170{
171 poco_assert (!_pStream);
172
173 setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED);
174 std::string auth("Basic realm=\"");
175 auth.append(realm);
176 auth.append("\"");
177 set("WWW-Authenticate", auth);
178}
179
180
181} } // namespace Poco::Net
182