diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/http/server/response.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/http/server/response.h')
-rw-r--r-- | library/cpp/http/server/response.h | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/library/cpp/http/server/response.h b/library/cpp/http/server/response.h new file mode 100644 index 0000000000..a75cb85605 --- /dev/null +++ b/library/cpp/http/server/response.h @@ -0,0 +1,82 @@ +#pragma once + +#include <library/cpp/http/misc/httpcodes.h> +#include <library/cpp/http/io/stream.h> + +#include <util/generic/strbuf.h> +#include <util/string/cast.h> + +class THttpHeaders; +class IOutputStream; + +class THttpResponse { +public: + THttpResponse() noexcept + : Code(HTTP_OK) + { + } + + explicit THttpResponse(HttpCodes code) noexcept + : Code(code) + { + } + + template <typename ValueType> + THttpResponse& AddHeader(const TString& name, const ValueType& value) { + return AddHeader(THttpInputHeader(name, ToString(value))); + } + + THttpResponse& AddHeader(const THttpInputHeader& header) { + Headers.AddHeader(header); + + return *this; + } + + THttpResponse& AddMultipleHeaders(const THttpHeaders& headers); + + const THttpHeaders& GetHeaders() const { + return Headers; + } + + THttpResponse& SetContentType(const TStringBuf& contentType); + + /** + * @note If @arg content isn't empty its size is automatically added as a + * "Content-Length" header during output to IOutputStream. + * @see IOutputStream& operator << (IOutputStream&, const THttpResponse&) + */ + THttpResponse& SetContent(const TString& content) { + Content = content; + + return *this; + } + + TString GetContent() const { + return Content; + } + + /** + * @note If @arg content isn't empty its size is automatically added as a + * "Content-Length" header during output to IOutputStream. + * @see IOutputStream& operator << (IOutputStream&, const THttpResponse&) + */ + THttpResponse& SetContent(const TString& content, const TStringBuf& contentType) { + return SetContent(content).SetContentType(contentType); + } + + HttpCodes HttpCode() const { + return Code; + } + + THttpResponse& SetHttpCode(HttpCodes code) { + Code = code; + return *this; + } + + void OutTo(IOutputStream& out) const; + +private: + HttpCodes Code; + THttpHeaders Headers; + TString Content; +}; |