diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp | |
parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
download | ydb-2d37894b1b037cf24231090eda8589bbb44fb6fc.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp')
-rw-r--r-- | contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp | 442 |
1 files changed, 221 insertions, 221 deletions
diff --git a/contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp b/contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp index d3e5b04837..fb6783c633 100644 --- a/contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp +++ b/contrib/libs/poco/Net/src/HTTPServerResponseImpl.cpp @@ -1,221 +1,221 @@ -// -// HTTPServerResponseImpl.cpp -// -// Library: Net -// Package: HTTPServer -// Module: HTTPServerResponseImpl -// -// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. -// and Contributors. -// -// SPDX-License-Identifier: BSL-1.0 -// - - -#include "Poco/Net/HTTPServerResponseImpl.h" -#include "Poco/Net/HTTPServerRequestImpl.h" -#include "Poco/Net/HTTPServerSession.h" -#include "Poco/Net/HTTPHeaderStream.h" -#include "Poco/Net/HTTPStream.h" -#include "Poco/Net/HTTPFixedLengthStream.h" -#include "Poco/Net/HTTPChunkedStream.h" -#include "Poco/File.h" -#include "Poco/Timestamp.h" -#include "Poco/NumberFormatter.h" -#include "Poco/StreamCopier.h" -#include "Poco/CountingStream.h" -#include "Poco/Exception.h" -#include "Poco/FileStream.h" -#include "Poco/DateTimeFormatter.h" -#include "Poco/DateTimeFormat.h" - - -using Poco::File; -using Poco::Timestamp; -using Poco::NumberFormatter; -using Poco::StreamCopier; -using Poco::OpenFileException; -using Poco::DateTimeFormatter; -using Poco::DateTimeFormat; - - -namespace Poco { -namespace Net { - - -HTTPServerResponseImpl::HTTPServerResponseImpl(HTTPServerSession& session): - _session(session), - _pRequest(0), - _pStream(0), - _pHeaderStream(0) -{ -} - - -HTTPServerResponseImpl::~HTTPServerResponseImpl() -{ - if (_pHeaderStream && _pHeaderStream != _pStream) - delete _pHeaderStream; - if (_pStream) - delete _pStream; -} - - -void HTTPServerResponseImpl::sendContinue() -{ - HTTPHeaderOutputStream hs(_session); - hs << getVersion() << " 100 Continue\r\n\r\n"; -} - - -std::ostream& HTTPServerResponseImpl::send() -{ - poco_assert (!_pStream); - - if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) || - getStatus() < 200 || - getStatus() == HTTPResponse::HTTP_NO_CONTENT || - getStatus() == HTTPResponse::HTTP_NOT_MODIFIED) - { - Poco::CountingOutputStream cs; - write(cs); - _pStream = new HTTPFixedLengthOutputStream(_session, cs.chars()); - write(*_pStream); - } - else if (getChunkedTransferEncoding()) - { - HTTPHeaderOutputStream hs(_session); - write(hs); - _pStream = new HTTPChunkedOutputStream(_session); - } - else if (hasContentLength()) - { - Poco::CountingOutputStream cs; - write(cs); -#if defined(POCO_HAVE_INT64) - _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength64() + cs.chars()); -#else - _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength() + cs.chars()); -#endif - write(*_pStream); - } - else - { - _pStream = new HTTPOutputStream(_session); - setKeepAlive(false); - write(*_pStream); - } - return *_pStream; -} - - -std::pair<std::ostream *, std::ostream *> HTTPServerResponseImpl::beginSend() -{ - poco_assert (!_pStream); - poco_assert (!_pHeaderStream); - - // NOTE Code is not exception safe. - - if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) || - getStatus() < 200 || - getStatus() == HTTPResponse::HTTP_NO_CONTENT || - getStatus() == HTTPResponse::HTTP_NOT_MODIFIED) - { - throw Exception("HTTPServerResponse::beginSend is invalid for HEAD request"); - } - else if (getChunkedTransferEncoding()) - { - _pHeaderStream = new HTTPHeaderOutputStream(_session); - beginWrite(*_pHeaderStream); - _pStream = new HTTPChunkedOutputStream(_session); - } - else if (hasContentLength()) - { - throw Exception("HTTPServerResponse::beginSend is invalid for response with Content-Length header"); - } - else - { - _pStream = new HTTPOutputStream(_session); - _pHeaderStream = _pStream; - setKeepAlive(false); - beginWrite(*_pStream); - } - - return std::make_pair(_pHeaderStream, _pStream); -} - - -void HTTPServerResponseImpl::sendFile(const std::string& path, const std::string& mediaType) -{ - poco_assert (!_pStream); - - File f(path); - Timestamp dateTime = f.getLastModified(); - File::FileSize length = f.getSize(); - set("Last-Modified", DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT)); -#if defined(POCO_HAVE_INT64) - setContentLength64(length); -#else - setContentLength(static_cast<int>(length)); -#endif - setContentType(mediaType); - setChunkedTransferEncoding(false); - - Poco::FileInputStream istr(path); - if (istr.good()) - { - _pStream = new HTTPHeaderOutputStream(_session); - write(*_pStream); - if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) - { - StreamCopier::copyStream(istr, *_pStream); - } - } - else throw OpenFileException(path); -} - - -void HTTPServerResponseImpl::sendBuffer(const void* pBuffer, std::size_t length) -{ - poco_assert (!_pStream); - - setContentLength(static_cast<int>(length)); - setChunkedTransferEncoding(false); - - _pStream = new HTTPHeaderOutputStream(_session); - write(*_pStream); - if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) - { - _pStream->write(static_cast<const char*>(pBuffer), static_cast<std::streamsize>(length)); - } -} - - -void HTTPServerResponseImpl::redirect(const std::string& uri, HTTPStatus status) -{ - poco_assert (!_pStream); - - setContentLength(0); - setChunkedTransferEncoding(false); - - setStatusAndReason(status); - set("Location", uri); - - _pStream = new HTTPHeaderOutputStream(_session); - write(*_pStream); -} - - -void HTTPServerResponseImpl::requireAuthentication(const std::string& realm) -{ - poco_assert (!_pStream); - - setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED); - std::string auth("Basic realm=\""); - auth.append(realm); - auth.append("\""); - set("WWW-Authenticate", auth); -} - - -} } // namespace Poco::Net +// +// HTTPServerResponseImpl.cpp +// +// Library: Net +// Package: HTTPServer +// Module: HTTPServerResponseImpl +// +// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#include "Poco/Net/HTTPServerResponseImpl.h" +#include "Poco/Net/HTTPServerRequestImpl.h" +#include "Poco/Net/HTTPServerSession.h" +#include "Poco/Net/HTTPHeaderStream.h" +#include "Poco/Net/HTTPStream.h" +#include "Poco/Net/HTTPFixedLengthStream.h" +#include "Poco/Net/HTTPChunkedStream.h" +#include "Poco/File.h" +#include "Poco/Timestamp.h" +#include "Poco/NumberFormatter.h" +#include "Poco/StreamCopier.h" +#include "Poco/CountingStream.h" +#include "Poco/Exception.h" +#include "Poco/FileStream.h" +#include "Poco/DateTimeFormatter.h" +#include "Poco/DateTimeFormat.h" + + +using Poco::File; +using Poco::Timestamp; +using Poco::NumberFormatter; +using Poco::StreamCopier; +using Poco::OpenFileException; +using Poco::DateTimeFormatter; +using Poco::DateTimeFormat; + + +namespace Poco { +namespace Net { + + +HTTPServerResponseImpl::HTTPServerResponseImpl(HTTPServerSession& session): + _session(session), + _pRequest(0), + _pStream(0), + _pHeaderStream(0) +{ +} + + +HTTPServerResponseImpl::~HTTPServerResponseImpl() +{ + if (_pHeaderStream && _pHeaderStream != _pStream) + delete _pHeaderStream; + if (_pStream) + delete _pStream; +} + + +void HTTPServerResponseImpl::sendContinue() +{ + HTTPHeaderOutputStream hs(_session); + hs << getVersion() << " 100 Continue\r\n\r\n"; +} + + +std::ostream& HTTPServerResponseImpl::send() +{ + poco_assert (!_pStream); + + if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) || + getStatus() < 200 || + getStatus() == HTTPResponse::HTTP_NO_CONTENT || + getStatus() == HTTPResponse::HTTP_NOT_MODIFIED) + { + Poco::CountingOutputStream cs; + write(cs); + _pStream = new HTTPFixedLengthOutputStream(_session, cs.chars()); + write(*_pStream); + } + else if (getChunkedTransferEncoding()) + { + HTTPHeaderOutputStream hs(_session); + write(hs); + _pStream = new HTTPChunkedOutputStream(_session); + } + else if (hasContentLength()) + { + Poco::CountingOutputStream cs; + write(cs); +#if defined(POCO_HAVE_INT64) + _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength64() + cs.chars()); +#else + _pStream = new HTTPFixedLengthOutputStream(_session, getContentLength() + cs.chars()); +#endif + write(*_pStream); + } + else + { + _pStream = new HTTPOutputStream(_session); + setKeepAlive(false); + write(*_pStream); + } + return *_pStream; +} + + +std::pair<std::ostream *, std::ostream *> HTTPServerResponseImpl::beginSend() +{ + poco_assert (!_pStream); + poco_assert (!_pHeaderStream); + + // NOTE Code is not exception safe. + + if ((_pRequest && _pRequest->getMethod() == HTTPRequest::HTTP_HEAD) || + getStatus() < 200 || + getStatus() == HTTPResponse::HTTP_NO_CONTENT || + getStatus() == HTTPResponse::HTTP_NOT_MODIFIED) + { + throw Exception("HTTPServerResponse::beginSend is invalid for HEAD request"); + } + else if (getChunkedTransferEncoding()) + { + _pHeaderStream = new HTTPHeaderOutputStream(_session); + beginWrite(*_pHeaderStream); + _pStream = new HTTPChunkedOutputStream(_session); + } + else if (hasContentLength()) + { + throw Exception("HTTPServerResponse::beginSend is invalid for response with Content-Length header"); + } + else + { + _pStream = new HTTPOutputStream(_session); + _pHeaderStream = _pStream; + setKeepAlive(false); + beginWrite(*_pStream); + } + + return std::make_pair(_pHeaderStream, _pStream); +} + + +void HTTPServerResponseImpl::sendFile(const std::string& path, const std::string& mediaType) +{ + poco_assert (!_pStream); + + File f(path); + Timestamp dateTime = f.getLastModified(); + File::FileSize length = f.getSize(); + set("Last-Modified", DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT)); +#if defined(POCO_HAVE_INT64) + setContentLength64(length); +#else + setContentLength(static_cast<int>(length)); +#endif + setContentType(mediaType); + setChunkedTransferEncoding(false); + + Poco::FileInputStream istr(path); + if (istr.good()) + { + _pStream = new HTTPHeaderOutputStream(_session); + write(*_pStream); + if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) + { + StreamCopier::copyStream(istr, *_pStream); + } + } + else throw OpenFileException(path); +} + + +void HTTPServerResponseImpl::sendBuffer(const void* pBuffer, std::size_t length) +{ + poco_assert (!_pStream); + + setContentLength(static_cast<int>(length)); + setChunkedTransferEncoding(false); + + _pStream = new HTTPHeaderOutputStream(_session); + write(*_pStream); + if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD) + { + _pStream->write(static_cast<const char*>(pBuffer), static_cast<std::streamsize>(length)); + } +} + + +void HTTPServerResponseImpl::redirect(const std::string& uri, HTTPStatus status) +{ + poco_assert (!_pStream); + + setContentLength(0); + setChunkedTransferEncoding(false); + + setStatusAndReason(status); + set("Location", uri); + + _pStream = new HTTPHeaderOutputStream(_session); + write(*_pStream); +} + + +void HTTPServerResponseImpl::requireAuthentication(const std::string& realm) +{ + poco_assert (!_pStream); + + setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED); + std::string auth("Basic realm=\""); + auth.append(realm); + auth.append("\""); + set("WWW-Authenticate", auth); +} + + +} } // namespace Poco::Net |