diff options
author | ishfb <ishfb@yandex-team.ru> | 2022-02-10 16:48:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:08 +0300 |
commit | 069c69f58fd78de3cf2e6eb2304047281e21fb65 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 /library/cpp/http | |
parent | 0170772a2dbf133f32e17ca137ff64790d43831f (diff) | |
download | ydb-069c69f58fd78de3cf2e6eb2304047281e21fb65.tar.gz |
Restoring authorship annotation for <ishfb@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/http')
-rw-r--r-- | library/cpp/http/server/http.cpp | 28 | ||||
-rw-r--r-- | library/cpp/http/server/http.h | 48 | ||||
-rw-r--r-- | library/cpp/http/server/response.cpp | 56 | ||||
-rw-r--r-- | library/cpp/http/server/response.h | 76 | ||||
-rw-r--r-- | library/cpp/http/server/response_ut.cpp | 170 | ||||
-rw-r--r-- | library/cpp/http/server/ya.make | 2 |
6 files changed, 190 insertions, 190 deletions
diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index f1d0fcc3a1..128583bdd7 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -771,7 +771,7 @@ THttpServer* TClientRequest::HttpServ() const noexcept { const TSocket& TClientRequest::Socket() const noexcept { return Conn_->Socket_; } - + NAddr::IRemoteAddrRef TClientRequest::GetListenerSockAddrRef() const noexcept { return Conn_->ListenerSockAddrRef_; } @@ -780,21 +780,21 @@ TInstant TClientRequest::AcceptMoment() const noexcept { return Conn_->AcceptMoment; } -/* - * TRequestReplier - */ -TRequestReplier::TRequestReplier() { -} - -TRequestReplier::~TRequestReplier() { -} - -bool TRequestReplier::Reply(void* threadSpecificResource) { - const TReplyParams params = { +/* + * TRequestReplier + */ +TRequestReplier::TRequestReplier() { +} + +TRequestReplier::~TRequestReplier() { +} + +bool TRequestReplier::Reply(void* threadSpecificResource) { + const TReplyParams params = { threadSpecificResource, Input(), Output()}; - return DoReply(params); -} + return DoReply(params); +} bool TryToBindAddresses(const THttpServerOptions& options, const std::function<void(TSocket)>* callbackOnBoundAddress) { THttpServerOptions::TBindAddresses addrs; diff --git a/library/cpp/http/server/http.h b/library/cpp/http/server/http.h index 2993415bfe..b292d38f27 100644 --- a/library/cpp/http/server/http.h +++ b/library/cpp/http/server/http.h @@ -99,9 +99,9 @@ private: THolder<TImpl> Impl_; }; -/** - * @deprecated Use TRequestReplier instead - */ +/** + * @deprecated Use TRequestReplier instead + */ class TClientRequest: public IObjectInQueue { friend class THttpServer::TImpl; @@ -149,28 +149,28 @@ private: }; class TRequestReplier: public TClientRequest { -public: - TRequestReplier(); +public: + TRequestReplier(); ~TRequestReplier() override; - - struct TReplyParams { - void* ThreadSpecificResource; - THttpInput& Input; - THttpOutput& Output; - }; - - /* - * Processes the request after 'connection' been created and 'Headers' been read - * Returns 'false' if the processing must be continued by the next handler, - * 'true' otherwise ('this' will be deleted) - */ - virtual bool DoReply(const TReplyParams& params) = 0; - -private: + + struct TReplyParams { + void* ThreadSpecificResource; + THttpInput& Input; + THttpOutput& Output; + }; + + /* + * Processes the request after 'connection' been created and 'Headers' been read + * Returns 'false' if the processing must be continued by the next handler, + * 'true' otherwise ('this' will be deleted) + */ + virtual bool DoReply(const TReplyParams& params) = 0; + +private: bool Reply(void* threadSpecificResource) final; - - using TClientRequest::Input; - using TClientRequest::Output; -}; + + using TClientRequest::Input; + using TClientRequest::Output; +}; bool TryToBindAddresses(const THttpServerOptions& options, const std::function<void(TSocket)>* callbackOnBoundAddress = nullptr); diff --git a/library/cpp/http/server/response.cpp b/library/cpp/http/server/response.cpp index 52d5f28e5b..52d64c91ce 100644 --- a/library/cpp/http/server/response.cpp +++ b/library/cpp/http/server/response.cpp @@ -1,65 +1,65 @@ -#include "response.h" +#include "response.h" -#include <util/stream/output.h> +#include <util/stream/output.h> #include <util/stream/mem.h> #include <util/string/cast.h> - -THttpResponse& THttpResponse::AddMultipleHeaders(const THttpHeaders& headers) { - for (THttpHeaders::TConstIterator i = headers.Begin(); i != headers.End(); ++i) { - this->Headers.AddHeader(*i); - } - return *this; -} - + +THttpResponse& THttpResponse::AddMultipleHeaders(const THttpHeaders& headers) { + for (THttpHeaders::TConstIterator i = headers.Begin(); i != headers.End(); ++i) { + this->Headers.AddHeader(*i); + } + return *this; +} + THttpResponse& THttpResponse::SetContentType(const TStringBuf& contentType) { Headers.AddOrReplaceHeader(THttpInputHeader("Content-Type", ToString(contentType))); return *this; -} - +} + void THttpResponse::OutTo(IOutputStream& os) const { TVector<IOutputStream::TPart> parts; - const size_t FIRST_LINE_PARTS = 3; + const size_t FIRST_LINE_PARTS = 3; const size_t HEADERS_PARTS = Headers.Count() * 4; - const size_t CONTENT_PARTS = 5; - parts.reserve(FIRST_LINE_PARTS + HEADERS_PARTS + CONTENT_PARTS); - - // first line + const size_t CONTENT_PARTS = 5; + parts.reserve(FIRST_LINE_PARTS + HEADERS_PARTS + CONTENT_PARTS); + + // first line parts.push_back(IOutputStream::TPart(TStringBuf("HTTP/1.1 "))); parts.push_back(IOutputStream::TPart(HttpCodeStrEx(Code))); parts.push_back(IOutputStream::TPart::CrLf()); - - // headers + + // headers for (THttpHeaders::TConstIterator i = Headers.Begin(); i != Headers.End(); ++i) { parts.push_back(IOutputStream::TPart(i->Name())); parts.push_back(IOutputStream::TPart(TStringBuf(": "))); parts.push_back(IOutputStream::TPart(i->Value())); parts.push_back(IOutputStream::TPart::CrLf()); - } - + } + char buf[50]; if (!Content.empty()) { TMemoryOutput mo(buf, sizeof(buf)); - + mo << Content.size(); parts.push_back(IOutputStream::TPart(TStringBuf("Content-Length: "))); parts.push_back(IOutputStream::TPart(buf, mo.Buf() - buf)); parts.push_back(IOutputStream::TPart::CrLf()); - } - - // content + } + + // content parts.push_back(IOutputStream::TPart::CrLf()); if (!Content.empty()) { parts.push_back(IOutputStream::TPart(Content)); - } - + } + os.Write(parts.data(), parts.size()); } template <> void Out<THttpResponse>(IOutputStream& os, const THttpResponse& resp) { resp.OutTo(os); -} +} diff --git a/library/cpp/http/server/response.h b/library/cpp/http/server/response.h index 20751e9096..a75cb85605 100644 --- a/library/cpp/http/server/response.h +++ b/library/cpp/http/server/response.h @@ -1,69 +1,69 @@ -#pragma once +#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; +#include <util/generic/strbuf.h> +#include <util/string/cast.h> + +class THttpHeaders; class IOutputStream; - -class THttpResponse { -public: + +class THttpResponse { +public: THttpResponse() noexcept : Code(HTTP_OK) { } explicit THttpResponse(HttpCodes code) noexcept - : Code(code) - { - } - + : 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); - + 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 + /** + * @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; + Content = content; + + return *this; + } - return *this; - } - TString GetContent() const { return Content; } - /** - * @note If @arg content isn't empty its size is automatically added as a + /** + * @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; } @@ -75,8 +75,8 @@ public: void OutTo(IOutputStream& out) const; -private: - HttpCodes Code; - THttpHeaders Headers; +private: + HttpCodes Code; + THttpHeaders Headers; TString Content; -}; +}; diff --git a/library/cpp/http/server/response_ut.cpp b/library/cpp/http/server/response_ut.cpp index 6f2914d612..73e2112ad3 100644 --- a/library/cpp/http/server/response_ut.cpp +++ b/library/cpp/http/server/response_ut.cpp @@ -1,51 +1,51 @@ -#include "response.h" - +#include "response.h" + #include <library/cpp/testing/unittest/registar.h> - + #include <util/string/cast.h> Y_UNIT_TEST_SUITE(TestHttpResponse) { Y_UNIT_TEST(TestCodeOnly) { UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse()), "HTTP/1.1 200 Ok\r\n\r\n"); - UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_NOT_FOUND)), "HTTP/1.1 404 Not found\r\n\r\n"); - } - + UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_NOT_FOUND)), "HTTP/1.1 404 Not found\r\n\r\n"); + } + Y_UNIT_TEST(TestRedirect) { - THttpResponse resp = THttpResponse(HTTP_FOUND).AddHeader("Location", "yandex.ru"); - UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), "HTTP/1.1 302 Moved temporarily\r\n" - "Location: yandex.ru\r\n" - "\r\n"); - } - + THttpResponse resp = THttpResponse(HTTP_FOUND).AddHeader("Location", "yandex.ru"); + UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), "HTTP/1.1 302 Moved temporarily\r\n" + "Location: yandex.ru\r\n" + "\r\n"); + } + Y_UNIT_TEST(TestAddHeader) { - THttpResponse resp(HTTP_FORBIDDEN); - resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - resp.AddHeader("X-Header-2", 10); - resp.AddHeader("X-Header-3", true); - - const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" - "X-Header-1: ValueOne\r\n" - "X-Header-2: 10\r\n" - "X-Header-3: 1\r\n" - "\r\n"; - UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); - } - + THttpResponse resp(HTTP_FORBIDDEN); + resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + resp.AddHeader("X-Header-2", 10); + resp.AddHeader("X-Header-3", true); + + const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" + "X-Header-1: ValueOne\r\n" + "X-Header-2: 10\r\n" + "X-Header-3: 1\r\n" + "\r\n"; + UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); + } + Y_UNIT_TEST(TestAddMultipleHeaders) { - THttpHeaders headers; - headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); - headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); - - const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" - "X-Header-1: ValueOne\r\n" - "X-Header-2: ValueTwo\r\n" - "X-Header-3: ValueThree\r\n" - "\r\n"; - UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_FORBIDDEN).AddMultipleHeaders(headers)), - EXPECTED); - } - + THttpHeaders headers; + headers.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + headers.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); + headers.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); + + const char* EXPECTED = "HTTP/1.1 403 Forbidden\r\n" + "X-Header-1: ValueOne\r\n" + "X-Header-2: ValueTwo\r\n" + "X-Header-3: ValueThree\r\n" + "\r\n"; + UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse(HTTP_FORBIDDEN).AddMultipleHeaders(headers)), + EXPECTED); + } + Y_UNIT_TEST(TestGetHeaders) { THttpResponse resp(HTTP_FORBIDDEN); @@ -66,66 +66,66 @@ Y_UNIT_TEST_SUITE(TestHttpResponse) { Y_UNIT_TEST(TestSetContent) { - const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" - "Content-Length: 10\r\n" - "\r\n" - "0123456789"; + const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" + "Content-Length: 10\r\n" + "\r\n" + "0123456789"; UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse().SetContent("0123456789")), - EXPECTED); - } - + EXPECTED); + } + Y_UNIT_TEST(TestSetContentWithContentType) { - const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" - "Content-Type: text/xml\r\n" - "Content-Length: 28\r\n" - "\r\n" - "<xml><tag value=\"1\" /></xml>"; + const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" + "Content-Type: text/xml\r\n" + "Content-Length: 28\r\n" + "\r\n" + "<xml><tag value=\"1\" /></xml>"; THttpResponse resp; resp.SetContent("<xml><tag value=\"1\" /></xml>").SetContentType("text/xml"); - UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); - } - + UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); + } + Y_UNIT_TEST(TestCopyConstructor) { - THttpResponse resp(HTTP_FORBIDDEN); - resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")) - .AddHeader("X-Header-2", "ValueTwo") - .AddHeader(THttpInputHeader("X-Header-3", "ValueThree")) + THttpResponse resp(HTTP_FORBIDDEN); + resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")) + .AddHeader("X-Header-2", "ValueTwo") + .AddHeader(THttpInputHeader("X-Header-3", "ValueThree")) .SetContent("Some stuff") .SetContentType("text/plain"); - - THttpResponse copy = resp; - UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); - } - + + THttpResponse copy = resp; + UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); + } + Y_UNIT_TEST(TestAssignment) { - THttpResponse resp(HTTP_FORBIDDEN); - resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); - resp.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); - resp.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); + THttpResponse resp(HTTP_FORBIDDEN); + resp.AddHeader(THttpInputHeader("X-Header-1", "ValueOne")); + resp.AddHeader(THttpInputHeader("X-Header-2", "ValueTwo")); + resp.AddHeader(THttpInputHeader("X-Header-3", "ValueThree")); resp.SetContent("Some stuff").SetContentType("text/plain"); - + THttpResponse copy; - copy = resp; - UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); - } - + copy = resp; + UNIT_ASSERT_STRINGS_EQUAL(ToString(copy), ToString(resp)); + } + Y_UNIT_TEST(TestEmptyContent) { UNIT_ASSERT_STRINGS_EQUAL(ToString(THttpResponse().SetContent("")), "HTTP/1.1 200 Ok\r\n\r\n"); - } - + } + Y_UNIT_TEST(TestReturnReference) { THttpResponse resp; - UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader("Header1", 1)); - UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader(THttpInputHeader("Header2", "2"))); - - THttpHeaders headers; - headers.AddHeader(THttpInputHeader("Header3", "3")); - headers.AddHeader(THttpInputHeader("Header4", "4")); - UNIT_ASSERT_EQUAL(&resp, &resp.AddMultipleHeaders(headers)); - - UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some stuff")); + UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader("Header1", 1)); + UNIT_ASSERT_EQUAL(&resp, &resp.AddHeader(THttpInputHeader("Header2", "2"))); + + THttpHeaders headers; + headers.AddHeader(THttpInputHeader("Header3", "3")); + headers.AddHeader(THttpInputHeader("Header4", "4")); + UNIT_ASSERT_EQUAL(&resp, &resp.AddMultipleHeaders(headers)); + + UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some stuff")); UNIT_ASSERT_EQUAL(&resp, &resp.SetContent("some other stuff").SetContentType("text/plain")); - } + } Y_UNIT_TEST(TestSetContentType) { const char* EXPECTED = "HTTP/1.1 200 Ok\r\n" @@ -139,4 +139,4 @@ Y_UNIT_TEST_SUITE(TestHttpResponse) { .SetContentType("text/xml"); UNIT_ASSERT_STRINGS_EQUAL(ToString(resp), EXPECTED); } -} +} diff --git a/library/cpp/http/server/ya.make b/library/cpp/http/server/ya.make index d1821c0878..bae6f33306 100644 --- a/library/cpp/http/server/ya.make +++ b/library/cpp/http/server/ya.make @@ -13,7 +13,7 @@ SRCS( http.cpp http_ex.cpp options.cpp - response.cpp + response.cpp ) PEERDIR( |