diff options
author | vmordovin <vmordovin@yandex-team.ru> | 2022-02-10 16:48:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:14 +0300 |
commit | 6a1e535429145ec1ecfbc5f1efd3c95323261fb5 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 /library/cpp/http | |
parent | 7c7f9bbcf57e15838d15afa94b31d8254b5d7776 (diff) | |
download | ydb-6a1e535429145ec1ecfbc5f1efd3c95323261fb5.tar.gz |
Restoring authorship annotation for <vmordovin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/http')
-rw-r--r-- | library/cpp/http/io/headers.h | 2 | ||||
-rw-r--r-- | library/cpp/http/io/stream.cpp | 28 | ||||
-rw-r--r-- | library/cpp/http/io/stream.h | 6 | ||||
-rw-r--r-- | library/cpp/http/io/stream_ut.cpp | 100 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata.cpp | 36 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata.h | 50 | ||||
-rw-r--r-- | library/cpp/http/server/http_ex.cpp | 8 | ||||
-rw-r--r-- | library/cpp/http/server/http_ex.h | 30 |
8 files changed, 130 insertions, 130 deletions
diff --git a/library/cpp/http/io/headers.h b/library/cpp/http/io/headers.h index 1ce789faf9..a71793d1c6 100644 --- a/library/cpp/http/io/headers.h +++ b/library/cpp/http/io/headers.h @@ -112,7 +112,7 @@ public: /// Записывает все заголовки контейнера в поток. /// @details Каждый заголовк записывается в виде "имя параметра: значение\r\n". void OutTo(IOutputStream* stream) const; - + /// Обменивает наборы заголовков двух контейнеров. void Swap(THttpHeaders& headers) noexcept { Headers_.swap(headers.Headers_); diff --git a/library/cpp/http/io/stream.cpp b/library/cpp/http/io/stream.cpp index 2e4c6b3f2e..6689be684f 100644 --- a/library/cpp/http/io/stream.cpp +++ b/library/cpp/http/io/stream.cpp @@ -200,10 +200,10 @@ public: return ContentEncoded_; } - inline bool HasContent() const noexcept { - return HasContentLength_ || ChunkedInput_; - } - + inline bool HasContent() const noexcept { + return HasContentLength_ || ChunkedInput_; + } + inline bool HasExpect100Continue() const noexcept { return Expect100Continue_; } @@ -273,15 +273,15 @@ private: } } - inline bool IsRequest() const { + inline bool IsRequest() const { return strnicmp(FirstLine().data(), "get", 3) == 0 || strnicmp(FirstLine().data(), "post", 4) == 0 || strnicmp(FirstLine().data(), "put", 3) == 0 || strnicmp(FirstLine().data(), "patch", 5) == 0 || strnicmp(FirstLine().data(), "head", 4) == 0 || strnicmp(FirstLine().data(), "delete", 6) == 0; - } - + } + inline void BuildInputChain() { TParsedHeaders p; @@ -344,7 +344,7 @@ private: Buffered_.Reset(&Cnull); Input_ = Streams_.Add(new TMultiInput(&Buffered_, Slave_)); - if (IsRequest() || HasContentLength_) { + if (IsRequest() || HasContentLength_) { /* * TODO - we have other cases */ @@ -448,10 +448,10 @@ bool THttpInput::ContentEncoded() const noexcept { return Impl_->ContentEncoded(); } -bool THttpInput::HasContent() const noexcept { - return Impl_->HasContent(); -} - +bool THttpInput::HasContent() const noexcept { + return Impl_->HasContent(); +} + bool THttpInput::HasExpect100Continue() const noexcept { return Impl_->HasExpect100Continue(); } @@ -663,7 +663,7 @@ private: static inline size_t ParseHttpVersion(const TString& s) { if (s.empty()) { ythrow THttpParseException() << "malformed http stream"; - } + } size_t parsed_version = 0; @@ -804,7 +804,7 @@ private: return ret; } - + inline void RebuildStream() { bool keepAlive = false; const TCompressionCodecFactory::TEncoderConstructor* encoder = nullptr; diff --git a/library/cpp/http/io/stream.h b/library/cpp/http/io/stream.h index a552b36218..78ca4fc814 100644 --- a/library/cpp/http/io/stream.h +++ b/library/cpp/http/io/stream.h @@ -81,9 +81,9 @@ public: /// показывает объём запакованных данных, а из THttpInput мы будем вычитывать уже распакованные. bool ContentEncoded() const noexcept; - /// Returns true if Content-Length or Transfer-Encoding header received - bool HasContent() const noexcept; - + /// Returns true if Content-Length or Transfer-Encoding header received + bool HasContent() const noexcept; + bool HasExpect100Continue() const noexcept; private: diff --git a/library/cpp/http/io/stream_ut.cpp b/library/cpp/http/io/stream_ut.cpp index 9737467bfc..1ea35df675 100644 --- a/library/cpp/http/io/stream_ut.cpp +++ b/library/cpp/http/io/stream_ut.cpp @@ -1,5 +1,5 @@ #include "stream.h" -#include "chunk.h" +#include "chunk.h" #include <library/cpp/http/server/http_ex.h> @@ -331,23 +331,23 @@ Y_UNIT_TEST_SUITE(THttpStreamTest) { httpOut.Flush(); UNIT_ASSERT_VALUES_EQUAL(checkStr.size(), str.size()); } - + TString MakeHttpOutputBody(const char* body, bool encodingEnabled) { TString str; TStringOutput strOut(str); { TBufferedOutput bufOut(&strOut, 8192); THttpOutput httpOut(&bufOut); - + httpOut.EnableKeepAlive(true); httpOut.EnableCompression(true); httpOut.EnableBodyEncoding(encodingEnabled); - + httpOut << "POST / HTTP/1.1\r\n"; httpOut << "Host: yandex.ru\r\n"; httpOut << "Content-Encoding: gzip\r\n"; httpOut << "\r\n"; - + UNIT_ASSERT_VALUES_EQUAL(str.size(), 0u); httpOut << body; } @@ -376,29 +376,29 @@ Y_UNIT_TEST_SUITE(THttpStreamTest) { TString str; TStringOutput strOut(str); TBufferedOutput bufOut(&strOut, 8192); - THttpOutput httpOut(&bufOut); - - httpOut.EnableKeepAlive(true); - httpOut.EnableCompression(true); - + THttpOutput httpOut(&bufOut); + + httpOut.EnableKeepAlive(true); + httpOut.EnableCompression(true); + const char* header = "GET / HTTP/1.1\r\nHost: yandex.ru\r\n\r\n"; httpOut << header; - + unsigned curLen = str.size(); const char* body = "<html>Hello</html>"; - httpOut << body; + httpOut << body; UNIT_ASSERT_VALUES_EQUAL(curLen, str.size()); httpOut.Finish(); UNIT_ASSERT_VALUES_EQUAL(curLen + strlen(body), str.size()); - } - + } + Y_UNIT_TEST(TestMultilineHeaders) { const char* headerLine0 = "HTTP/1.1 200 OK"; const char* headerLine1 = "Content-Language: en"; const char* headerLine2 = "Vary: Accept-Encoding, "; const char* headerLine3 = "\tAccept-Language"; const char* headerLine4 = "Content-Length: 18"; - + TString endLine("\r\n"); TString r; r += headerLine0 + endLine; @@ -564,53 +564,53 @@ Y_UNIT_TEST_SUITE(THttpStreamTest) { UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), "GGLOL"); } } - + Y_UNIT_TEST(TestInputHasContent) { - { - TStringStream request; + { + TStringStream request; request << "POST / HTTP/1.1\r\n" "Host: yandex.ru\r\n" "\r\n"; - request << "HTTPDATA"; - - TStringInput input(request.Str()); - THttpInput httpInput(&input); - - UNIT_ASSERT(!httpInput.HasContent()); - UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), ""); - } - - { - TStringStream request; + request << "HTTPDATA"; + + TStringInput input(request.Str()); + THttpInput httpInput(&input); + + UNIT_ASSERT(!httpInput.HasContent()); + UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), ""); + } + + { + TStringStream request; request << "POST / HTTP/1.1\r\n" "Host: yandex.ru\r\n" "Content-Length: 8" "\r\n\r\n"; - request << "HTTPDATA"; - - TStringInput input(request.Str()); - THttpInput httpInput(&input); - - UNIT_ASSERT(httpInput.HasContent()); - UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), "HTTPDATA"); - } - - { - TStringStream request; + request << "HTTPDATA"; + + TStringInput input(request.Str()); + THttpInput httpInput(&input); + + UNIT_ASSERT(httpInput.HasContent()); + UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), "HTTPDATA"); + } + + { + TStringStream request; request << "POST / HTTP/1.1\r\n" "Host: yandex.ru\r\n" "Transfer-Encoding: chunked" "\r\n\r\n"; - request << "8\r\nHTTPDATA\r\n0\r\n"; - - TStringInput input(request.Str()); - THttpInput httpInput(&input); - - UNIT_ASSERT(httpInput.HasContent()); - UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), "HTTPDATA"); - } - } - + request << "8\r\nHTTPDATA\r\n0\r\n"; + + TStringInput input(request.Str()); + THttpInput httpInput(&input); + + UNIT_ASSERT(httpInput.HasContent()); + UNIT_ASSERT_VALUES_EQUAL(httpInput.ReadAll(), "HTTPDATA"); + } + } + Y_UNIT_TEST(TestHttpInputHeadRequest) { class THeadOnlyInput: public IInputStream { public: diff --git a/library/cpp/http/misc/httpreqdata.cpp b/library/cpp/http/misc/httpreqdata.cpp index 216afec6ff..f6951f68cd 100644 --- a/library/cpp/http/misc/httpreqdata.cpp +++ b/library/cpp/http/misc/httpreqdata.cpp @@ -2,7 +2,7 @@ #include <util/stream/mem.h> -TBaseServerRequestData::TBaseServerRequestData(SOCKET s) +TBaseServerRequestData::TBaseServerRequestData(SOCKET s) : Addr(nullptr) , Host() , Port() @@ -14,7 +14,7 @@ TBaseServerRequestData::TBaseServerRequestData(SOCKET s) { } -TBaseServerRequestData::TBaseServerRequestData(const char* qs, SOCKET s) +TBaseServerRequestData::TBaseServerRequestData(const char* qs, SOCKET s) : Addr(nullptr) , Host() , Port() @@ -27,21 +27,21 @@ TBaseServerRequestData::TBaseServerRequestData(const char* qs, SOCKET s) { } -void TBaseServerRequestData::AppendQueryString(const char* str, size_t length) { +void TBaseServerRequestData::AppendQueryString(const char* str, size_t length) { if (Y_UNLIKELY(Search)) { Y_ASSERT(strlen(Search) == SearchLength); - ModifiedQueryString.Reserve(SearchLength + length + 2); - ModifiedQueryString.Assign(Search, SearchLength); + ModifiedQueryString.Reserve(SearchLength + length + 2); + ModifiedQueryString.Assign(Search, SearchLength); if (SearchLength > 0 && Search[SearchLength - 1] != '&' && length > 0 && str[0] != '&') { - ModifiedQueryString.Append('&'); + ModifiedQueryString.Append('&'); } - ModifiedQueryString.Append(str, length); + ModifiedQueryString.Append(str, length); } else { - ModifiedQueryString.Reserve(length + 1); - ModifiedQueryString.Assign(str, length); + ModifiedQueryString.Reserve(length + 1); + ModifiedQueryString.Assign(str, length); } - ModifiedQueryString.Append('\0'); + ModifiedQueryString.Append('\0'); Search = ModifiedQueryString.data(); SearchLength = ModifiedQueryString.size() - 1; // ignore terminator } @@ -54,7 +54,7 @@ void TBaseServerRequestData::SetRemoteAddr(TStringBuf addr) { Addr = AddrData; } -const char* TBaseServerRequestData::RemoteAddr() const { +const char* TBaseServerRequestData::RemoteAddr() const { if (!Addr) { *AddrData = 0; GetRemoteAddr(Socket, AddrData, sizeof(AddrData)); @@ -89,7 +89,7 @@ TString TBaseServerRequestData::HeaderByIndex(size_t n) const noexcept { return TString(i->first) + TStringBuf(": ") + i->second; } -const char* TBaseServerRequestData::Environment(const char* key) const { +const char* TBaseServerRequestData::Environment(const char* key) const { if (stricmp(key, "REMOTE_ADDR") == 0) { const char* ip = HeaderIn("X-Real-IP"); if (ip) @@ -107,7 +107,7 @@ const char* TBaseServerRequestData::Environment(const char* key) const { return nullptr; } -void TBaseServerRequestData::Clear() { +void TBaseServerRequestData::Clear() { HeadersIn_.clear(); Addr = Path = Search = nullptr; OrigSearch = {}; @@ -115,11 +115,11 @@ void TBaseServerRequestData::Clear() { Host.clear(); Port.clear(); CurPage.remove(); - ParseBuf.Clear(); + ParseBuf.Clear(); BeginTime = MicroSeconds(); } -const char* TBaseServerRequestData::GetCurPage() const { +const char* TBaseServerRequestData::GetCurPage() const { if (!CurPage && Host) { CurPage = "http://"; CurPage += Host; @@ -136,10 +136,10 @@ const char* TBaseServerRequestData::GetCurPage() const { return CurPage.data(); } -bool TBaseServerRequestData::Parse(const char* origReq) { +bool TBaseServerRequestData::Parse(const char* origReq) { size_t origReqLength = strlen(origReq); - ParseBuf.Assign(origReq, origReqLength + 1); - char* req = ParseBuf.Data(); + ParseBuf.Assign(origReq, origReqLength + 1); + char* req = ParseBuf.Data(); while (*req == ' ' || *req == '\t') req++; diff --git a/library/cpp/http/misc/httpreqdata.h b/library/cpp/http/misc/httpreqdata.h index d4a12cf8cf..16e59c4d78 100644 --- a/library/cpp/http/misc/httpreqdata.h +++ b/library/cpp/http/misc/httpreqdata.h @@ -13,19 +13,19 @@ #include <util/system/yassert.h> #include <util/generic/string.h> #include <util/datetime/base.h> -#include <util/generic/buffer.h> +#include <util/generic/buffer.h> using THttpHeadersContainer = THashMap<TString, TString, TCIOps, TCIOps>; -class TBaseServerRequestData { +class TBaseServerRequestData { public: - TBaseServerRequestData(SOCKET s = INVALID_SOCKET); - TBaseServerRequestData(const char* qs, SOCKET s = INVALID_SOCKET); + TBaseServerRequestData(SOCKET s = INVALID_SOCKET); + TBaseServerRequestData(const char* qs, SOCKET s = INVALID_SOCKET); void SetHost(const TString& host, ui16 port) { - Host = host; - Port = ToString(port); - } + Host = host; + Port = ToString(port); + } const TString& ServerName() const { return Host; @@ -100,26 +100,26 @@ private: SOCKET Socket; ui64 BeginTime; mutable TString CurPage; - TBuffer ParseBuf; - TBuffer ModifiedQueryString; -}; + TBuffer ParseBuf; + TBuffer ModifiedQueryString; +}; class TServerRequestData: public TBaseServerRequestData { public: - TServerRequestData(SOCKET s = INVALID_SOCKET) - : TBaseServerRequestData(s) - { - } - TServerRequestData(const char* qs, SOCKET s = INVALID_SOCKET) - : TBaseServerRequestData(qs, s) - { - Scan(); - } - - void Scan() { - CgiParam.Scan(QueryStringBuf()); - } - -public: + TServerRequestData(SOCKET s = INVALID_SOCKET) + : TBaseServerRequestData(s) + { + } + TServerRequestData(const char* qs, SOCKET s = INVALID_SOCKET) + : TBaseServerRequestData(qs, s) + { + Scan(); + } + + void Scan() { + CgiParam.Scan(QueryStringBuf()); + } + +public: TCgiParameters CgiParam; }; diff --git a/library/cpp/http/server/http_ex.cpp b/library/cpp/http/server/http_ex.cpp index d90049387a..e07db22bfc 100644 --- a/library/cpp/http/server/http_ex.cpp +++ b/library/cpp/http/server/http_ex.cpp @@ -4,10 +4,10 @@ #include <util/generic/cast.h> #include <util/stream/null.h> -bool THttpClientRequestExtension::Parse(char* req, TBaseServerRequestData& rd) { - rd.SetSocket(Socket()); +bool THttpClientRequestExtension::Parse(char* req, TBaseServerRequestData& rd) { + rd.SetSocket(Socket()); - if (!rd.Parse(req)) { + if (!rd.Parse(req)) { Output() << "HTTP/1.1 403 Forbidden\r\n" "Content-Type: text/plain\r\n" "Content-Length: 39\r\n" @@ -20,7 +20,7 @@ bool THttpClientRequestExtension::Parse(char* req, TBaseServerRequestData& rd) { return true; } -bool THttpClientRequestExtension::ProcessHeaders(TBaseServerRequestData& rd, TBlob& postData) { +bool THttpClientRequestExtension::ProcessHeaders(TBaseServerRequestData& rd, TBlob& postData) { for (const auto& header : ParsedHeaders) { rd.AddHeader(header.first, header.second); } diff --git a/library/cpp/http/server/http_ex.h b/library/cpp/http/server/http_ex.h index ba982796d3..1ef43ea4fd 100644 --- a/library/cpp/http/server/http_ex.h +++ b/library/cpp/http/server/http_ex.h @@ -5,24 +5,24 @@ #include <library/cpp/http/misc/httpreqdata.h> class THttpClientRequestExtension: public TClientRequest { -public: - bool Parse(char* req, TBaseServerRequestData& rd); - bool ProcessHeaders(TBaseServerRequestData& rd, TBlob& postData); -}; +public: + bool Parse(char* req, TBaseServerRequestData& rd); + bool ProcessHeaders(TBaseServerRequestData& rd, TBlob& postData); +}; -template <class TRequestData> +template <class TRequestData> class THttpClientRequestExtImpl: public THttpClientRequestExtension { protected: - bool Parse(char* req) { - return THttpClientRequestExtension::Parse(req, RD); - } - bool ProcessHeaders() { - return THttpClientRequestExtension::ProcessHeaders(RD, Buf); - } + bool Parse(char* req) { + return THttpClientRequestExtension::Parse(req, RD); + } + bool ProcessHeaders() { + return THttpClientRequestExtension::ProcessHeaders(RD, Buf); + } -protected: - TRequestData RD; +protected: + TRequestData RD; TBlob Buf; }; - -using THttpClientRequestEx = THttpClientRequestExtImpl<TServerRequestData>; + +using THttpClientRequestEx = THttpClientRequestExtImpl<TServerRequestData>; |