diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/http/misc/httpreqdata.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/http/misc/httpreqdata.cpp')
-rw-r--r-- | library/cpp/http/misc/httpreqdata.cpp | 260 |
1 files changed, 130 insertions, 130 deletions
diff --git a/library/cpp/http/misc/httpreqdata.cpp b/library/cpp/http/misc/httpreqdata.cpp index ecc55e754d..f6951f68cd 100644 --- a/library/cpp/http/misc/httpreqdata.cpp +++ b/library/cpp/http/misc/httpreqdata.cpp @@ -1,51 +1,51 @@ -#include "httpreqdata.h" - +#include "httpreqdata.h" + #include <util/stream/mem.h> TBaseServerRequestData::TBaseServerRequestData(SOCKET s) : Addr(nullptr) - , Host() - , Port() + , Host() + , Port() , Path(nullptr) , Search(nullptr) - , SearchLength(0) - , Socket(s) - , BeginTime(MicroSeconds()) -{ -} - + , SearchLength(0) + , Socket(s) + , BeginTime(MicroSeconds()) +{ +} + TBaseServerRequestData::TBaseServerRequestData(const char* qs, SOCKET s) : Addr(nullptr) - , Host() - , Port() + , Host() + , Port() , Path(nullptr) - , Search((char*)qs) - , SearchLength(qs ? strlen(qs) : 0) + , Search((char*)qs) + , SearchLength(qs ? strlen(qs) : 0) , OrigSearch(Search, SearchLength) - , Socket(s) - , BeginTime(MicroSeconds()) -{ -} - + , Socket(s) + , BeginTime(MicroSeconds()) +{ +} + void TBaseServerRequestData::AppendQueryString(const char* str, size_t length) { - if (Y_UNLIKELY(Search)) { + if (Y_UNLIKELY(Search)) { Y_ASSERT(strlen(Search) == SearchLength); ModifiedQueryString.Reserve(SearchLength + length + 2); ModifiedQueryString.Assign(Search, SearchLength); - if (SearchLength > 0 && Search[SearchLength - 1] != '&' && - length > 0 && str[0] != '&') { + if (SearchLength > 0 && Search[SearchLength - 1] != '&' && + length > 0 && str[0] != '&') { ModifiedQueryString.Append('&'); - } + } ModifiedQueryString.Append(str, length); - } else { + } else { ModifiedQueryString.Reserve(length + 1); ModifiedQueryString.Assign(str, length); - } + } ModifiedQueryString.Append('\0'); Search = ModifiedQueryString.data(); SearchLength = ModifiedQueryString.size() - 1; // ignore terminator -} - +} + void TBaseServerRequestData::SetRemoteAddr(TStringBuf addr) { TMemoryOutput out(AddrData, Y_ARRAY_SIZE(AddrData) - 1); out.Write(addr.substr(0, Y_ARRAY_SIZE(AddrData) - 1)); @@ -55,142 +55,142 @@ void TBaseServerRequestData::SetRemoteAddr(TStringBuf addr) { } const char* TBaseServerRequestData::RemoteAddr() const { - if (!Addr) { - *AddrData = 0; - GetRemoteAddr(Socket, AddrData, sizeof(AddrData)); - Addr = AddrData; - } - - return Addr; -} - + if (!Addr) { + *AddrData = 0; + GetRemoteAddr(Socket, AddrData, sizeof(AddrData)); + Addr = AddrData; + } + + return Addr; +} + const char* TBaseServerRequestData::HeaderIn(TStringBuf key) const { auto it = HeadersIn_.find(key); - + if (it == HeadersIn_.end()) { return nullptr; - } - + } + return it->second.data(); -} - +} + TString TBaseServerRequestData::HeaderByIndex(size_t n) const noexcept { - if (n >= HeadersCount()) { + if (n >= HeadersCount()) { return nullptr; - } - + } + THttpHeadersContainer::const_iterator i = HeadersIn_.begin(); - - while (n) { - ++i; - --n; - } - + + while (n) { + ++i; + --n; + } + return TString(i->first) + TStringBuf(": ") + i->second; -} - +} + const char* TBaseServerRequestData::Environment(const char* key) const { - if (stricmp(key, "REMOTE_ADDR") == 0) { - const char* ip = HeaderIn("X-Real-IP"); - if (ip) - return ip; - return RemoteAddr(); - } else if (stricmp(key, "QUERY_STRING") == 0) { - return QueryString(); - } else if (stricmp(key, "SERVER_NAME") == 0) { + if (stricmp(key, "REMOTE_ADDR") == 0) { + const char* ip = HeaderIn("X-Real-IP"); + if (ip) + return ip; + return RemoteAddr(); + } else if (stricmp(key, "QUERY_STRING") == 0) { + return QueryString(); + } else if (stricmp(key, "SERVER_NAME") == 0) { return ServerName().data(); - } else if (stricmp(key, "SERVER_PORT") == 0) { + } else if (stricmp(key, "SERVER_PORT") == 0) { return ServerPort().data(); - } else if (stricmp(key, "SCRIPT_NAME") == 0) { - return ScriptName(); - } + } else if (stricmp(key, "SCRIPT_NAME") == 0) { + return ScriptName(); + } return nullptr; -} - +} + void TBaseServerRequestData::Clear() { HeadersIn_.clear(); Addr = Path = Search = nullptr; OrigSearch = {}; - SearchLength = 0; - Host.clear(); - Port.clear(); - CurPage.remove(); + SearchLength = 0; + Host.clear(); + Port.clear(); + CurPage.remove(); ParseBuf.Clear(); - BeginTime = MicroSeconds(); -} - + BeginTime = MicroSeconds(); +} + const char* TBaseServerRequestData::GetCurPage() const { - if (!CurPage && Host) { - CurPage = "http://"; - CurPage += Host; - if (Port) { - CurPage += ':'; - CurPage += Port; - } - CurPage += Path; - if (Search) { - CurPage += '?'; - CurPage += Search; - } - } + if (!CurPage && Host) { + CurPage = "http://"; + CurPage += Host; + if (Port) { + CurPage += ':'; + CurPage += Port; + } + CurPage += Path; + if (Search) { + CurPage += '?'; + CurPage += Search; + } + } return CurPage.data(); -} - +} + bool TBaseServerRequestData::Parse(const char* origReq) { - size_t origReqLength = strlen(origReq); + size_t origReqLength = strlen(origReq); ParseBuf.Assign(origReq, origReqLength + 1); char* req = ParseBuf.Data(); - - while (*req == ' ' || *req == '\t') - req++; - if (*req != '/') - return false; // we are not a proxy - while (req[1] == '/') // remove redundant slashes - req++; - - // detect url end (can contain some garbage after whitespace, e.g. 'HTTP 1.1') - char* urlEnd = req; - while (*urlEnd && *urlEnd != ' ' && *urlEnd != '\t') - urlEnd++; - if (*urlEnd) - *urlEnd = 0; - - // cut fragment if exists - char* fragment = strchr(req, '#'); - if (fragment) - *fragment = 0; // ignore fragment - else - fragment = urlEnd; - Path = req; - - // calculate Search length without additional strlen-ing - Search = strchr(Path, '?'); - if (Search) { - *Search++ = 0; - ptrdiff_t delta = fragment - Search; - // indeed, second case is a parse error - SearchLength = (delta >= 0) ? delta : (urlEnd - Search); + + while (*req == ' ' || *req == '\t') + req++; + if (*req != '/') + return false; // we are not a proxy + while (req[1] == '/') // remove redundant slashes + req++; + + // detect url end (can contain some garbage after whitespace, e.g. 'HTTP 1.1') + char* urlEnd = req; + while (*urlEnd && *urlEnd != ' ' && *urlEnd != '\t') + urlEnd++; + if (*urlEnd) + *urlEnd = 0; + + // cut fragment if exists + char* fragment = strchr(req, '#'); + if (fragment) + *fragment = 0; // ignore fragment + else + fragment = urlEnd; + Path = req; + + // calculate Search length without additional strlen-ing + Search = strchr(Path, '?'); + if (Search) { + *Search++ = 0; + ptrdiff_t delta = fragment - Search; + // indeed, second case is a parse error + SearchLength = (delta >= 0) ? delta : (urlEnd - Search); Y_ASSERT(strlen(Search) == SearchLength); - } else { - SearchLength = 0; - } + } else { + SearchLength = 0; + } OrigSearch = {Search, SearchLength}; - return true; -} - + return true; +} + void TBaseServerRequestData::AddHeader(const TString& name, const TString& value) { HeadersIn_[name] = value; - + if (stricmp(name.data(), "Host") == 0) { size_t hostLen = strcspn(value.data(), ":"); if (value[hostLen] == ':') Port = value.substr(hostLen + 1); Host = value.substr(0, hostLen); - } -} - + } +} + void TBaseServerRequestData::SetPath(const TString& path) { PathStorage = TBuffer(path.data(), path.size() + 1); Path = PathStorage.Data(); -} +} |