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 | |
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')
-rw-r--r-- | library/cpp/http/misc/httpcodes.cpp | 88 | ||||
-rw-r--r-- | library/cpp/http/misc/httpcodes.h | 84 | ||||
-rw-r--r-- | library/cpp/http/misc/httpdate.cpp | 16 | ||||
-rw-r--r-- | library/cpp/http/misc/httpdate.h | 8 | ||||
-rw-r--r-- | library/cpp/http/misc/httpdate_ut.cpp | 18 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata.cpp | 260 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata.h | 68 | ||||
-rw-r--r-- | library/cpp/http/misc/httpreqdata_ut.cpp | 90 | ||||
-rw-r--r-- | library/cpp/http/misc/parsed_request.cpp | 54 | ||||
-rw-r--r-- | library/cpp/http/misc/parsed_request.h | 50 | ||||
-rw-r--r-- | library/cpp/http/misc/parsed_request_ut.cpp | 48 | ||||
-rw-r--r-- | library/cpp/http/misc/ut/ya.make | 18 | ||||
-rw-r--r-- | library/cpp/http/misc/ya.make | 22 |
13 files changed, 412 insertions, 412 deletions
diff --git a/library/cpp/http/misc/httpcodes.cpp b/library/cpp/http/misc/httpcodes.cpp index fb9e43c9d3..ad8c80ac1e 100644 --- a/library/cpp/http/misc/httpcodes.cpp +++ b/library/cpp/http/misc/httpcodes.cpp @@ -2,26 +2,26 @@ TStringBuf HttpCodeStrEx(int code) noexcept { switch (code) { - case HTTP_CONTINUE: + case HTTP_CONTINUE: return TStringBuf("100 Continue"); - case HTTP_SWITCHING_PROTOCOLS: + case HTTP_SWITCHING_PROTOCOLS: return TStringBuf("101 Switching protocols"); case HTTP_PROCESSING: return TStringBuf("102 Processing"); - - case HTTP_OK: + + case HTTP_OK: return TStringBuf("200 Ok"); - case HTTP_CREATED: + case HTTP_CREATED: return TStringBuf("201 Created"); - case HTTP_ACCEPTED: + case HTTP_ACCEPTED: return TStringBuf("202 Accepted"); - case HTTP_NON_AUTHORITATIVE_INFORMATION: + case HTTP_NON_AUTHORITATIVE_INFORMATION: return TStringBuf("203 None authoritative information"); - case HTTP_NO_CONTENT: + case HTTP_NO_CONTENT: return TStringBuf("204 No content"); - case HTTP_RESET_CONTENT: + case HTTP_RESET_CONTENT: return TStringBuf("205 Reset content"); - case HTTP_PARTIAL_CONTENT: + case HTTP_PARTIAL_CONTENT: return TStringBuf("206 Partial content"); case HTTP_MULTI_STATUS: return TStringBuf("207 Multi status"); @@ -30,58 +30,58 @@ TStringBuf HttpCodeStrEx(int code) noexcept { case HTTP_IM_USED: return TStringBuf("226 IM used"); - case HTTP_MULTIPLE_CHOICES: + case HTTP_MULTIPLE_CHOICES: return TStringBuf("300 Multiple choices"); - case HTTP_MOVED_PERMANENTLY: + case HTTP_MOVED_PERMANENTLY: return TStringBuf("301 Moved permanently"); - case HTTP_FOUND: + case HTTP_FOUND: return TStringBuf("302 Moved temporarily"); - case HTTP_SEE_OTHER: + case HTTP_SEE_OTHER: return TStringBuf("303 See other"); - case HTTP_NOT_MODIFIED: + case HTTP_NOT_MODIFIED: return TStringBuf("304 Not modified"); - case HTTP_USE_PROXY: + case HTTP_USE_PROXY: return TStringBuf("305 Use proxy"); - case HTTP_TEMPORARY_REDIRECT: + case HTTP_TEMPORARY_REDIRECT: return TStringBuf("307 Temporarily redirect"); case HTTP_PERMANENT_REDIRECT: return TStringBuf("308 Permanent redirect"); - case HTTP_BAD_REQUEST: + case HTTP_BAD_REQUEST: return TStringBuf("400 Bad request"); - case HTTP_UNAUTHORIZED: + case HTTP_UNAUTHORIZED: return TStringBuf("401 Unauthorized"); - case HTTP_PAYMENT_REQUIRED: + case HTTP_PAYMENT_REQUIRED: return TStringBuf("402 Payment required"); - case HTTP_FORBIDDEN: + case HTTP_FORBIDDEN: return TStringBuf("403 Forbidden"); - case HTTP_NOT_FOUND: + case HTTP_NOT_FOUND: return TStringBuf("404 Not found"); - case HTTP_METHOD_NOT_ALLOWED: + case HTTP_METHOD_NOT_ALLOWED: return TStringBuf("405 Method not allowed"); - case HTTP_NOT_ACCEPTABLE: + case HTTP_NOT_ACCEPTABLE: return TStringBuf("406 Not acceptable"); - case HTTP_PROXY_AUTHENTICATION_REQUIRED: + case HTTP_PROXY_AUTHENTICATION_REQUIRED: return TStringBuf("407 Proxy Authentication required"); - case HTTP_REQUEST_TIME_OUT: + case HTTP_REQUEST_TIME_OUT: return TStringBuf("408 Request time out"); - case HTTP_CONFLICT: + case HTTP_CONFLICT: return TStringBuf("409 Conflict"); - case HTTP_GONE: + case HTTP_GONE: return TStringBuf("410 Gone"); - case HTTP_LENGTH_REQUIRED: + case HTTP_LENGTH_REQUIRED: return TStringBuf("411 Length required"); - case HTTP_PRECONDITION_FAILED: + case HTTP_PRECONDITION_FAILED: return TStringBuf("412 Precondition failed"); - case HTTP_REQUEST_ENTITY_TOO_LARGE: + case HTTP_REQUEST_ENTITY_TOO_LARGE: return TStringBuf("413 Request entity too large"); - case HTTP_REQUEST_URI_TOO_LARGE: + case HTTP_REQUEST_URI_TOO_LARGE: return TStringBuf("414 Request uri too large"); - case HTTP_UNSUPPORTED_MEDIA_TYPE: + case HTTP_UNSUPPORTED_MEDIA_TYPE: return TStringBuf("415 Unsupported media type"); - case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: + case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: return TStringBuf("416 Requested Range Not Satisfiable"); - case HTTP_EXPECTATION_FAILED: + case HTTP_EXPECTATION_FAILED: return TStringBuf("417 Expectation Failed"); case HTTP_I_AM_A_TEAPOT: return TStringBuf("418 I Am A Teapot"); @@ -89,7 +89,7 @@ TStringBuf HttpCodeStrEx(int code) noexcept { return TStringBuf("419 Authentication Timeout"); case HTTP_MISDIRECTED_REQUEST: return TStringBuf("421 Misdirected Request"); - case HTTP_UNPROCESSABLE_ENTITY: + case HTTP_UNPROCESSABLE_ENTITY: return TStringBuf("422 Unprocessable Entity"); case HTTP_LOCKED: return TStringBuf("423 Locked"); @@ -101,24 +101,24 @@ TStringBuf HttpCodeStrEx(int code) noexcept { return TStringBuf("426 Upgrade Required"); case HTTP_PRECONDITION_REQUIRED: return TStringBuf("428 Precondition Required"); - case HTTP_TOO_MANY_REQUESTS: + case HTTP_TOO_MANY_REQUESTS: return TStringBuf("429 Too Many Requests"); case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE: return TStringBuf("431 Request Header Fields Too Large"); case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS: return TStringBuf("451 Unavailable For Legal Reason"); - case HTTP_INTERNAL_SERVER_ERROR: + case HTTP_INTERNAL_SERVER_ERROR: return TStringBuf("500 Internal server error"); - case HTTP_NOT_IMPLEMENTED: + case HTTP_NOT_IMPLEMENTED: return TStringBuf("501 Not implemented"); - case HTTP_BAD_GATEWAY: + case HTTP_BAD_GATEWAY: return TStringBuf("502 Bad gateway"); - case HTTP_SERVICE_UNAVAILABLE: + case HTTP_SERVICE_UNAVAILABLE: return TStringBuf("503 Service unavailable"); - case HTTP_GATEWAY_TIME_OUT: + case HTTP_GATEWAY_TIME_OUT: return TStringBuf("504 Gateway time out"); - case HTTP_HTTP_VERSION_NOT_SUPPORTED: + case HTTP_HTTP_VERSION_NOT_SUPPORTED: return TStringBuf("505 HTTP version not supported"); case HTTP_VARIANT_ALSO_NEGOTIATES: return TStringBuf("506 Variant also negotiates"); @@ -135,7 +135,7 @@ TStringBuf HttpCodeStrEx(int code) noexcept { case HTTP_UNASSIGNED_512: return TStringBuf("512 Unassigned"); - default: + default: return TStringBuf("000 Unknown HTTP code"); } } diff --git a/library/cpp/http/misc/httpcodes.h b/library/cpp/http/misc/httpcodes.h index 2b7a3c4a80..cbfbaa1188 100644 --- a/library/cpp/http/misc/httpcodes.h +++ b/library/cpp/http/misc/httpcodes.h @@ -3,67 +3,67 @@ #include <util/generic/strbuf.h> enum HttpCodes { - HTTP_CONTINUE = 100, - HTTP_SWITCHING_PROTOCOLS = 101, + HTTP_CONTINUE = 100, + HTTP_SWITCHING_PROTOCOLS = 101, HTTP_PROCESSING = 102, - HTTP_OK = 200, - HTTP_CREATED = 201, - HTTP_ACCEPTED = 202, - HTTP_NON_AUTHORITATIVE_INFORMATION = 203, - HTTP_NO_CONTENT = 204, - HTTP_RESET_CONTENT = 205, - HTTP_PARTIAL_CONTENT = 206, + HTTP_OK = 200, + HTTP_CREATED = 201, + HTTP_ACCEPTED = 202, + HTTP_NON_AUTHORITATIVE_INFORMATION = 203, + HTTP_NO_CONTENT = 204, + HTTP_RESET_CONTENT = 205, + HTTP_PARTIAL_CONTENT = 206, HTTP_MULTI_STATUS = 207, HTTP_ALREADY_REPORTED = 208, HTTP_IM_USED = 226, - HTTP_MULTIPLE_CHOICES = 300, - HTTP_MOVED_PERMANENTLY = 301, - HTTP_FOUND = 302, - HTTP_SEE_OTHER = 303, - HTTP_NOT_MODIFIED = 304, - HTTP_USE_PROXY = 305, - HTTP_TEMPORARY_REDIRECT = 307, + HTTP_MULTIPLE_CHOICES = 300, + HTTP_MOVED_PERMANENTLY = 301, + HTTP_FOUND = 302, + HTTP_SEE_OTHER = 303, + HTTP_NOT_MODIFIED = 304, + HTTP_USE_PROXY = 305, + HTTP_TEMPORARY_REDIRECT = 307, HTTP_PERMANENT_REDIRECT = 308, - HTTP_BAD_REQUEST = 400, - HTTP_UNAUTHORIZED = 401, - HTTP_PAYMENT_REQUIRED = 402, - HTTP_FORBIDDEN = 403, - HTTP_NOT_FOUND = 404, - HTTP_METHOD_NOT_ALLOWED = 405, - HTTP_NOT_ACCEPTABLE = 406, - HTTP_PROXY_AUTHENTICATION_REQUIRED = 407, - HTTP_REQUEST_TIME_OUT = 408, - HTTP_CONFLICT = 409, - HTTP_GONE = 410, - HTTP_LENGTH_REQUIRED = 411, - HTTP_PRECONDITION_FAILED = 412, - HTTP_REQUEST_ENTITY_TOO_LARGE = 413, - HTTP_REQUEST_URI_TOO_LARGE = 414, - HTTP_UNSUPPORTED_MEDIA_TYPE = 415, - HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416, - HTTP_EXPECTATION_FAILED = 417, + HTTP_BAD_REQUEST = 400, + HTTP_UNAUTHORIZED = 401, + HTTP_PAYMENT_REQUIRED = 402, + HTTP_FORBIDDEN = 403, + HTTP_NOT_FOUND = 404, + HTTP_METHOD_NOT_ALLOWED = 405, + HTTP_NOT_ACCEPTABLE = 406, + HTTP_PROXY_AUTHENTICATION_REQUIRED = 407, + HTTP_REQUEST_TIME_OUT = 408, + HTTP_CONFLICT = 409, + HTTP_GONE = 410, + HTTP_LENGTH_REQUIRED = 411, + HTTP_PRECONDITION_FAILED = 412, + HTTP_REQUEST_ENTITY_TOO_LARGE = 413, + HTTP_REQUEST_URI_TOO_LARGE = 414, + HTTP_UNSUPPORTED_MEDIA_TYPE = 415, + HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416, + HTTP_EXPECTATION_FAILED = 417, HTTP_I_AM_A_TEAPOT = 418, HTTP_AUTHENTICATION_TIMEOUT = 419, HTTP_MISDIRECTED_REQUEST = 421, - HTTP_UNPROCESSABLE_ENTITY = 422, + HTTP_UNPROCESSABLE_ENTITY = 422, HTTP_LOCKED = 423, HTTP_FAILED_DEPENDENCY = 424, HTTP_UNORDERED_COLLECTION = 425, HTTP_UPGRADE_REQUIRED = 426, HTTP_PRECONDITION_REQUIRED = 428, - HTTP_TOO_MANY_REQUESTS = 429, + HTTP_TOO_MANY_REQUESTS = 429, HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431, HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451, - HTTP_INTERNAL_SERVER_ERROR = 500, - HTTP_NOT_IMPLEMENTED = 501, - HTTP_BAD_GATEWAY = 502, - HTTP_SERVICE_UNAVAILABLE = 503, - HTTP_GATEWAY_TIME_OUT = 504, - HTTP_HTTP_VERSION_NOT_SUPPORTED = 505, + HTTP_INTERNAL_SERVER_ERROR = 500, + HTTP_NOT_IMPLEMENTED = 501, + HTTP_BAD_GATEWAY = 502, + HTTP_SERVICE_UNAVAILABLE = 503, + HTTP_GATEWAY_TIME_OUT = 504, + HTTP_HTTP_VERSION_NOT_SUPPORTED = 505, HTTP_VARIANT_ALSO_NEGOTIATES = 506, HTTP_INSUFFICIENT_STORAGE = 507, HTTP_LOOP_DETECTED = 508, diff --git a/library/cpp/http/misc/httpdate.cpp b/library/cpp/http/misc/httpdate.cpp index 98876293c6..4a3031bbf4 100644 --- a/library/cpp/http/misc/httpdate.cpp +++ b/library/cpp/http/misc/httpdate.cpp @@ -43,7 +43,7 @@ static const char *wkdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - + static const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" @@ -64,15 +64,15 @@ int format_http_date(char buf[], size_t size, time_t when) { #endif } -char* format_http_date(time_t when, char* buf, size_t buflen) { - const int len = format_http_date(buf, buflen, when); - - if (len == 0) { +char* format_http_date(time_t when, char* buf, size_t buflen) { + const int len = format_http_date(buf, buflen, when); + + if (len == 0) { return nullptr; - } - + } + Y_ASSERT(len > 0 && size_t(len) < buflen); - + return buf; } diff --git a/library/cpp/http/misc/httpdate.h b/library/cpp/http/misc/httpdate.h index 0640624661..04876f38fe 100644 --- a/library/cpp/http/misc/httpdate.h +++ b/library/cpp/http/misc/httpdate.h @@ -1,11 +1,11 @@ #pragma once -#include <util/datetime/base.h> +#include <util/datetime/base.h> #include <util/generic/string.h> - + #include <ctime> -#define BAD_DATE ((time_t)-1) +#define BAD_DATE ((time_t)-1) inline time_t parse_http_date(const TStringBuf& datestring) { try { @@ -16,6 +16,6 @@ inline time_t parse_http_date(const TStringBuf& datestring) { } int format_http_date(char buf[], size_t size, time_t when); -char* format_http_date(time_t when, char* buf, size_t len); +char* format_http_date(time_t when, char* buf, size_t len); TString FormatHttpDate(time_t when); diff --git a/library/cpp/http/misc/httpdate_ut.cpp b/library/cpp/http/misc/httpdate_ut.cpp index ac4e54063e..c1a0103501 100644 --- a/library/cpp/http/misc/httpdate_ut.cpp +++ b/library/cpp/http/misc/httpdate_ut.cpp @@ -1,15 +1,15 @@ #include <library/cpp/testing/unittest/registar.h> - -#include "httpdate.h" - + +#include "httpdate.h" + Y_UNIT_TEST_SUITE(TestHttpDate) { Y_UNIT_TEST(Test1) { - char buf1[100]; - char buf2[100]; - - UNIT_ASSERT((int)strlen(format_http_date(0, buf1, sizeof(buf1))) == format_http_date(buf2, sizeof(buf2), 0)); - } + char buf1[100]; + char buf2[100]; + + UNIT_ASSERT((int)strlen(format_http_date(0, buf1, sizeof(buf1))) == format_http_date(buf2, sizeof(buf2), 0)); + } Y_UNIT_TEST(Test2) { UNIT_ASSERT_STRINGS_EQUAL(FormatHttpDate(1234567890), "Fri, 13 Feb 2009 23:31:30 GMT"); } -} +} 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(); -} +} diff --git a/library/cpp/http/misc/httpreqdata.h b/library/cpp/http/misc/httpreqdata.h index 946a0e5f14..16e59c4d78 100644 --- a/library/cpp/http/misc/httpreqdata.h +++ b/library/cpp/http/misc/httpreqdata.h @@ -1,14 +1,14 @@ #pragma once #include <library/cpp/digest/lower_case/hash_ops.h> - -#include <util/str_stl.h> - + +#include <util/str_stl.h> + #include <util/system/defaults.h> -#include <util/string/cast.h> +#include <util/string/cast.h> #include <library/cpp/cgiparam/cgiparam.h> #include <util/network/address.h> -#include <util/network/socket.h> +#include <util/network/socket.h> #include <util/generic/hash.h> #include <util/system/yassert.h> #include <util/generic/string.h> @@ -21,16 +21,16 @@ class TBaseServerRequestData { public: 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); } - + const TString& ServerName() const { return Host; } - + NAddr::IRemoteAddrPtr ServerAddress() const { return NAddr::GetSockAddr(Socket); } @@ -38,57 +38,57 @@ public: const TString& ServerPort() const { return Port; } - + const char* ScriptName() const { return Path; } - + const char* QueryString() const { return Search; } - + TStringBuf QueryStringBuf() const { return TStringBuf(Search, SearchLength); } - + TStringBuf OrigQueryStringBuf() const { return OrigSearch; } - void AppendQueryString(const char* str, size_t length); - const char* RemoteAddr() const; + void AppendQueryString(const char* str, size_t length); + const char* RemoteAddr() const; void SetRemoteAddr(TStringBuf addr); const char* HeaderIn(TStringBuf key) const; - + const THttpHeadersContainer& HeadersIn() const { return HeadersIn_; } inline size_t HeadersCount() const noexcept { return HeadersIn_.size(); - } - + } + TString HeaderByIndex(size_t n) const noexcept; - const char* Environment(const char* key) const; - - void Clear(); - + const char* Environment(const char* key) const; + + void Clear(); + void SetSocket(SOCKET s) noexcept { - Socket = s; - } - + Socket = s; + } + ui64 RequestBeginTime() const noexcept { - return BeginTime; - } - + return BeginTime; + } + void SetPath(const TString& path); const char* GetCurPage() const; bool Parse(const char* req); void AddHeader(const TString& name, const TString& value); - + private: TBuffer PathStorage; - mutable char* Addr; + mutable char* Addr; TString Host; TString Port; char* Path; @@ -98,13 +98,13 @@ private: THttpHeadersContainer HeadersIn_; mutable char AddrData[INET6_ADDRSTRLEN]; SOCKET Socket; - ui64 BeginTime; + ui64 BeginTime; mutable TString CurPage; TBuffer ParseBuf; TBuffer ModifiedQueryString; }; - -class TServerRequestData: public TBaseServerRequestData { + +class TServerRequestData: public TBaseServerRequestData { public: TServerRequestData(SOCKET s = INVALID_SOCKET) : TBaseServerRequestData(s) @@ -119,7 +119,7 @@ public: void Scan() { CgiParam.Scan(QueryStringBuf()); } - + public: - TCgiParameters CgiParam; + TCgiParameters CgiParam; }; diff --git a/library/cpp/http/misc/httpreqdata_ut.cpp b/library/cpp/http/misc/httpreqdata_ut.cpp index 24225e78ac..e7f16ef27c 100644 --- a/library/cpp/http/misc/httpreqdata_ut.cpp +++ b/library/cpp/http/misc/httpreqdata_ut.cpp @@ -4,19 +4,19 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { Y_UNIT_TEST(Headers) { - TServerRequestData sd; - + TServerRequestData sd; + sd.AddHeader("x-xx", "y-yy"); sd.AddHeader("x-Xx", "y-yy"); - - UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 1); - + + UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 1); + sd.AddHeader("x-XxX", "y-yyy"); - UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 2); + UNIT_ASSERT_VALUES_EQUAL(sd.HeadersCount(), 2); UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("X-XX")), TStringBuf("y-yy")); UNIT_ASSERT_VALUES_EQUAL(TStringBuf(sd.HeaderIn("X-XXX")), TStringBuf("y-yyy")); - } - + } + Y_UNIT_TEST(ComplexHeaders) { TServerRequestData sd; sd.SetHost("zzz", 1); @@ -43,24 +43,24 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { } Y_UNIT_TEST(ParseScan) { - TServerRequestData rd; + TServerRequestData rd; - // Parse parses url without host - UNIT_ASSERT(!rd.Parse(" http://yandex.ru/yandsearch?>a=fake&haha=da HTTP 1.1 OK")); + // Parse parses url without host + UNIT_ASSERT(!rd.Parse(" http://yandex.ru/yandsearch?>a=fake&haha=da HTTP 1.1 OK")); - // This should work - UNIT_ASSERT(rd.Parse(" /yandsearch?>a=fake&haha=da HTTP 1.1 OK")); + // This should work + UNIT_ASSERT(rd.Parse(" /yandsearch?>a=fake&haha=da HTTP 1.1 OK")); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ">a=fake&haha=da"); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf()); - rd.Scan(); - UNIT_ASSERT(rd.CgiParam.Has("gta", "fake")); - UNIT_ASSERT(rd.CgiParam.Has("haha", "da")); - UNIT_ASSERT(!rd.CgiParam.Has("no-param")); + rd.Scan(); + UNIT_ASSERT(rd.CgiParam.Has("gta", "fake")); + UNIT_ASSERT(rd.CgiParam.Has("haha", "da")); + UNIT_ASSERT(!rd.CgiParam.Has("no-param")); - rd.Clear(); - } + rd.Clear(); + } Y_UNIT_TEST(Ctor) { const TString qs("gta=fake&haha=da"); @@ -69,55 +69,55 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); - UNIT_ASSERT(rd.CgiParam.Has("gta")); - UNIT_ASSERT(rd.CgiParam.Has("haha")); - UNIT_ASSERT(!rd.CgiParam.Has("no-param")); - } + UNIT_ASSERT(rd.CgiParam.Has("gta")); + UNIT_ASSERT(rd.CgiParam.Has("haha")); + UNIT_ASSERT(!rd.CgiParam.Has("no-param")); + } Y_UNIT_TEST(HashCut) { const TString qs(">a=fake&haha=da"); const TString header = " /yandsearch?" + qs + "#&uberParam=yes&q=? HTTP 1.1 OK"; - TServerRequestData rd; + TServerRequestData rd; rd.Parse(header.c_str()); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); - rd.Scan(); - UNIT_ASSERT(rd.CgiParam.Has("gta")); - UNIT_ASSERT(rd.CgiParam.Has("haha")); - UNIT_ASSERT(!rd.CgiParam.Has("uberParam")); - } + rd.Scan(); + UNIT_ASSERT(rd.CgiParam.Has("gta")); + UNIT_ASSERT(rd.CgiParam.Has("haha")); + UNIT_ASSERT(!rd.CgiParam.Has("uberParam")); + } Y_UNIT_TEST(MisplacedHashCut) { - TServerRequestData rd; - rd.Parse(" /y#ndsearch?>a=fake&haha=da&uberParam=yes&q=? HTTP 1.1 OK"); + TServerRequestData rd; + rd.Parse(" /y#ndsearch?>a=fake&haha=da&uberParam=yes&q=? HTTP 1.1 OK"); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ""); UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), ""); - rd.Scan(); - UNIT_ASSERT(rd.CgiParam.empty()); - } + rd.Scan(); + UNIT_ASSERT(rd.CgiParam.empty()); + } Y_UNIT_TEST(CornerCase) { - TServerRequestData rd; - rd.Parse(" /yandsearch?#"); + TServerRequestData rd; + rd.Parse(" /yandsearch?#"); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), ""); UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), ""); - rd.Scan(); - UNIT_ASSERT(rd.CgiParam.empty()); - } + rd.Scan(); + UNIT_ASSERT(rd.CgiParam.empty()); + } Y_UNIT_TEST(AppendQueryString) { const TString qs("gta=fake&haha=da"); TServerRequestData rd(qs.c_str()); - UNIT_ASSERT(rd.CgiParam.Has("gta", "fake")); - UNIT_ASSERT(rd.CgiParam.Has("haha", "da")); + UNIT_ASSERT(rd.CgiParam.Has("gta", "fake")); + UNIT_ASSERT(rd.CgiParam.Has("haha", "da")); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs); UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), rd.OrigQueryStringBuf()); @@ -128,11 +128,11 @@ Y_UNIT_TEST_SUITE(TRequestServerDataTest) { UNIT_ASSERT_STRINGS_EQUAL(rd.QueryStringBuf(), qs + '&' + appendix); UNIT_ASSERT_STRINGS_EQUAL(rd.OrigQueryStringBuf(), qs); - rd.Scan(); + rd.Scan(); - UNIT_ASSERT(rd.CgiParam.Has("gta", "true")); - UNIT_ASSERT(rd.CgiParam.Has("gta", "new")); - } + UNIT_ASSERT(rd.CgiParam.Has("gta", "true")); + UNIT_ASSERT(rd.CgiParam.Has("gta", "new")); + } Y_UNIT_TEST(SetRemoteAddrSimple) { static const TString TEST = "abacaba.search.yandex.net"; diff --git a/library/cpp/http/misc/parsed_request.cpp b/library/cpp/http/misc/parsed_request.cpp index 8568293e2c..e332a24e91 100644 --- a/library/cpp/http/misc/parsed_request.cpp +++ b/library/cpp/http/misc/parsed_request.cpp @@ -1,32 +1,32 @@ -#include "parsed_request.h" - +#include "parsed_request.h" + #include <util/string/strip.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/string/cast.h> - + static inline TStringBuf StripLeft(const TStringBuf& s) noexcept { - const char* b = s.begin(); - const char* e = s.end(); - - StripRangeBegin(b, e); - - return TStringBuf(b, e); -} - -TParsedHttpRequest::TParsedHttpRequest(const TStringBuf& str) { - TStringBuf tmp; - + const char* b = s.begin(); + const char* e = s.end(); + + StripRangeBegin(b, e); + + return TStringBuf(b, e); +} + +TParsedHttpRequest::TParsedHttpRequest(const TStringBuf& str) { + TStringBuf tmp; + if (!StripLeft(str).TrySplit(' ', Method, tmp)) { - ythrow yexception() << "bad request(" << ToString(str).Quote() << ")"; - } - + ythrow yexception() << "bad request(" << ToString(str).Quote() << ")"; + } + if (!StripLeft(tmp).TrySplit(' ', Request, Proto)) { - ythrow yexception() << "bad request(" << ToString(str).Quote() << ")"; - } - - Proto = StripLeft(Proto); -} - -TParsedHttpLocation::TParsedHttpLocation(const TStringBuf& req) { - req.Split('?', Path, Cgi); -} + ythrow yexception() << "bad request(" << ToString(str).Quote() << ")"; + } + + Proto = StripLeft(Proto); +} + +TParsedHttpLocation::TParsedHttpLocation(const TStringBuf& req) { + req.Split('?', Path, Cgi); +} diff --git a/library/cpp/http/misc/parsed_request.h b/library/cpp/http/misc/parsed_request.h index 00ba7767e7..d4df705495 100644 --- a/library/cpp/http/misc/parsed_request.h +++ b/library/cpp/http/misc/parsed_request.h @@ -1,26 +1,26 @@ #pragma once - -#include <util/generic/strbuf.h> - -struct TParsedHttpRequest { - TParsedHttpRequest(const TStringBuf& str); - - TStringBuf Method; - TStringBuf Request; - TStringBuf Proto; -}; - -struct TParsedHttpLocation { - TParsedHttpLocation(const TStringBuf& req); - - TStringBuf Path; - TStringBuf Cgi; -}; - -struct TParsedHttpFull: public TParsedHttpRequest, public TParsedHttpLocation { - inline TParsedHttpFull(const TStringBuf& line) - : TParsedHttpRequest(line) - , TParsedHttpLocation(Request) - { - } -}; + +#include <util/generic/strbuf.h> + +struct TParsedHttpRequest { + TParsedHttpRequest(const TStringBuf& str); + + TStringBuf Method; + TStringBuf Request; + TStringBuf Proto; +}; + +struct TParsedHttpLocation { + TParsedHttpLocation(const TStringBuf& req); + + TStringBuf Path; + TStringBuf Cgi; +}; + +struct TParsedHttpFull: public TParsedHttpRequest, public TParsedHttpLocation { + inline TParsedHttpFull(const TStringBuf& line) + : TParsedHttpRequest(line) + , TParsedHttpLocation(Request) + { + } +}; diff --git a/library/cpp/http/misc/parsed_request_ut.cpp b/library/cpp/http/misc/parsed_request_ut.cpp index d578f69f24..da6d95c6ab 100644 --- a/library/cpp/http/misc/parsed_request_ut.cpp +++ b/library/cpp/http/misc/parsed_request_ut.cpp @@ -1,28 +1,28 @@ -#include "parsed_request.h" - +#include "parsed_request.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(THttpParse) { Y_UNIT_TEST(TestParse) { - TParsedHttpFull h("GET /yandsearch?text=nokia HTTP/1.1"); - - UNIT_ASSERT_EQUAL(h.Method, "GET"); - UNIT_ASSERT_EQUAL(h.Request, "/yandsearch?text=nokia"); - UNIT_ASSERT_EQUAL(h.Proto, "HTTP/1.1"); - - UNIT_ASSERT_EQUAL(h.Path, "/yandsearch"); - UNIT_ASSERT_EQUAL(h.Cgi, "text=nokia"); - } - + TParsedHttpFull h("GET /yandsearch?text=nokia HTTP/1.1"); + + UNIT_ASSERT_EQUAL(h.Method, "GET"); + UNIT_ASSERT_EQUAL(h.Request, "/yandsearch?text=nokia"); + UNIT_ASSERT_EQUAL(h.Proto, "HTTP/1.1"); + + UNIT_ASSERT_EQUAL(h.Path, "/yandsearch"); + UNIT_ASSERT_EQUAL(h.Cgi, "text=nokia"); + } + Y_UNIT_TEST(TestError) { - bool wasError = false; - - try { - TParsedHttpFull("GET /yandsearch?text=nokiaHTTP/1.1"); - } catch (...) { - wasError = true; - } - - UNIT_ASSERT(wasError); - } -} + bool wasError = false; + + try { + TParsedHttpFull("GET /yandsearch?text=nokiaHTTP/1.1"); + } catch (...) { + wasError = true; + } + + UNIT_ASSERT(wasError); + } +} diff --git a/library/cpp/http/misc/ut/ya.make b/library/cpp/http/misc/ut/ya.make index 49ca2b9aaf..f4bdd35662 100644 --- a/library/cpp/http/misc/ut/ya.make +++ b/library/cpp/http/misc/ut/ya.make @@ -1,11 +1,11 @@ UNITTEST_FOR(library/cpp/http/misc) - + OWNER(g:util) - -SRCS( - httpdate_ut.cpp - httpreqdata_ut.cpp - parsed_request_ut.cpp -) - -END() + +SRCS( + httpdate_ut.cpp + httpreqdata_ut.cpp + parsed_request_ut.cpp +) + +END() diff --git a/library/cpp/http/misc/ya.make b/library/cpp/http/misc/ya.make index 7a0f2a6e9f..fceb3cf79c 100644 --- a/library/cpp/http/misc/ya.make +++ b/library/cpp/http/misc/ya.make @@ -1,24 +1,24 @@ -LIBRARY() - +LIBRARY() + OWNER( g:util mvel ) - + GENERATE_ENUM_SERIALIZATION(httpcodes.h) -SRCS( - httpcodes.cpp - httpdate.cpp - httpreqdata.cpp - parsed_request.cpp -) - +SRCS( + httpcodes.cpp + httpdate.cpp + httpreqdata.cpp + parsed_request.cpp +) + PEERDIR( library/cpp/cgiparam library/cpp/digest/lower_case ) -END() +END() RECURSE_FOR_TESTS(ut) |