diff options
author | Alexey Efimov <xeno@prnwatch.com> | 2022-05-20 19:18:30 +0300 |
---|---|---|
committer | Alexey Efimov <xeno@prnwatch.com> | 2022-05-20 19:18:30 +0300 |
commit | a4c79297abc662140028caf7fb04e49dcbccb975 (patch) | |
tree | 5ad9cf6459ecf1d61beb7a95893076671dca3f00 | |
parent | 1c96c3345be1820c6be4f4f307e12e6c20b2b384 (diff) | |
download | ydb-a4c79297abc662140028caf7fb04e49dcbccb975.tar.gz |
fixes for redirections KIKIMR-14742
ref:1c576bf8c00f29291ab91c1039b8ad64798ba93d
-rw-r--r-- | library/cpp/actors/http/http.cpp | 9 | ||||
-rw-r--r-- | ydb/core/mon/async_http_mon.cpp | 70 | ||||
-rw-r--r-- | ydb/core/mon/sync_http_mon.cpp | 7 | ||||
-rw-r--r-- | ydb/core/testlib/actors/test_runtime.cpp | 2 | ||||
-rw-r--r-- | ydb/core/testlib/test_client.cpp | 4 |
5 files changed, 61 insertions, 31 deletions
diff --git a/library/cpp/actors/http/http.cpp b/library/cpp/actors/http/http.cpp index 20dea47573..4f8df434ca 100644 --- a/library/cpp/actors/http/http.cpp +++ b/library/cpp/actors/http/http.cpp @@ -374,7 +374,7 @@ THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponseString(TStringBuf d if (parser.HaveBody()) { if (parser.ContentType && !Endpoint->CompressContentTypes.empty()) { TStringBuf contentType = parser.ContentType.Before(';'); - Trim(ContentType, ' '); + Trim(contentType, ' '); if (Count(Endpoint->CompressContentTypes, contentType) != 0) { if (response->EnableCompression()) { headers.Erase("Content-Length"); // we will need new length after compression @@ -432,6 +432,13 @@ THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, } if (!contentType.empty() && !body.empty()) { response->Set<&THttpResponse::ContentType>(contentType); + if (!Endpoint->CompressContentTypes.empty()) { + contentType = contentType.Before(';'); + Trim(contentType, ' '); + if (Count(Endpoint->CompressContentTypes, contentType) != 0) { + response->EnableCompression(); + } + } } if (lastModified) { response->Set<&THttpResponse::LastModified>(lastModified.FormatGmTime("%a, %d %b %Y %H:%M:%S GMT")); diff --git a/ydb/core/mon/async_http_mon.cpp b/ydb/core/mon/async_http_mon.cpp index 2e6c6d87a4..3cd0fba615 100644 --- a/ydb/core/mon/async_http_mon.cpp +++ b/ydb/core/mon/async_http_mon.cpp @@ -146,7 +146,10 @@ public: } virtual TString GetRemoteAddr() const override { - return Request->Address->ToString(); + if (Request->Address) { + return Request->Address->ToString(); + } + return {}; } virtual TString GetServiceTitle() const override { @@ -278,7 +281,7 @@ public: if (ActorMonPage->Authorizer) { TString user = authorizeResult ? authorizeResult->Token->GetUserSID() : "anonymous"; LOG_NOTICE_S(*TlsActivationContext, NActorsServices::HTTP, - request->Address->ToString() + (request->Address ? request->Address->ToString() : "") << " " << user << " " << request->Method << " " << request->URL); @@ -512,25 +515,27 @@ public: {} static void ToProto(NKikimrMonProto::TSockAddr& proto, const NHttp::THttpConfig::SocketAddressType& address) { - switch (address->SockAddr()->sa_family) { - case AF_INET: { - proto.SetFamily(AF_INET); - sockaddr_in* addr = (sockaddr_in*)address->SockAddr(); - char ip[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, (void*)&addr->sin_addr, ip, INET_ADDRSTRLEN); - proto.SetAddress(ip); - proto.SetPort(htons(addr->sin_port)); - } - break; - case AF_INET6: { - proto.SetFamily(AF_INET6); - sockaddr_in6* addr = (sockaddr_in6*)address->SockAddr(); - char ip6[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, (void*)&addr->sin6_addr, ip6, INET6_ADDRSTRLEN); - proto.SetAddress(ip6); - proto.SetPort(htons(addr->sin6_port)); - } - break; + if (address) { + switch (address->SockAddr()->sa_family) { + case AF_INET: { + proto.SetFamily(AF_INET); + sockaddr_in* addr = (sockaddr_in*)address->SockAddr(); + char ip[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, (void*)&addr->sin_addr, ip, INET_ADDRSTRLEN); + proto.SetAddress(ip); + proto.SetPort(htons(addr->sin_port)); + } + break; + case AF_INET6: { + proto.SetFamily(AF_INET6); + sockaddr_in6* addr = (sockaddr_in6*)address->SockAddr(); + char ip6[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, (void*)&addr->sin6_addr, ip6, INET6_ADDRSTRLEN); + proto.SetAddress(ip6); + proto.SetPort(htons(addr->sin6_port)); + } + break; + } } } @@ -538,7 +543,7 @@ public: TActorId monServiceNodeProxy = MakeNodeProxyId(NodeId); auto request = std::make_unique<TEvMon::TEvMonitoringRequest>(); request->Record.SetHttpRequest(Event->Get()->Request->AsString()); - // TODO address + ToProto(*request->Record.MutableAddress(), Event->Get()->Request->Address); Send(monServiceNodeProxy, request.release(), IEventHandle::FlagTrackDelivery); Become(&THttpMonServiceMonRequest::StateWork); } @@ -561,7 +566,26 @@ public: } void Handle(TEvMon::TEvMonitoringResponse::TPtr& ev) { - Send(Event->Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(Event->Get()->Request->CreateResponseString(ev->Get()->Record.GetHttpResponse())), 0, Event->Cookie); + TString responseTxt = ev->Get()->Record.GetHttpResponse(); + NHttp::THttpOutgoingResponsePtr responseObj = Event->Get()->Request->CreateResponseString(responseTxt); + if (responseObj->Status == "301" || responseObj->Status == "302") { + NHttp::THttpParser<NHttp::THttpResponse, NHttp::TSocketBuffer> parser(responseTxt); + NHttp::THeadersBuilder headers(parser.Headers); + if (headers["Location"].starts_with('/')) { + NHttp::THttpOutgoingResponsePtr response = new NHttp::THttpOutgoingResponse(Event->Get()->Request); + response->InitResponse(parser.Protocol, parser.Version, parser.Status, parser.Message); + + headers.Set("Location", TStringBuilder() << "/node/" << NodeId << headers["Location"]); + + response->Set(headers); + if (parser.HaveBody()) { + response->SetBody(parser.Body); + } + responseObj = response; + } + } + + Send(Event->Sender, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(responseObj.Release()), 0, Event->Cookie); PassAway(); } diff --git a/ydb/core/mon/sync_http_mon.cpp b/ydb/core/mon/sync_http_mon.cpp index 74ea7dadec..6c863c55c1 100644 --- a/ydb/core/mon/sync_http_mon.cpp +++ b/ydb/core/mon/sync_http_mon.cpp @@ -47,8 +47,11 @@ namespace NActors { } void TSyncHttpMon::Stop() { - IndexMonPage->ClearPages(); // it's required to avoid loop-reference - TBase::Stop(); + if (IndexMonPage) { + IndexMonPage->ClearPages(); // it's required to avoid loop-reference + TBase::Stop(); + IndexMonPage.Drop(); + } } void TSyncHttpMon::Register(NMonitoring::IMonPage* page) { diff --git a/ydb/core/testlib/actors/test_runtime.cpp b/ydb/core/testlib/actors/test_runtime.cpp index dec58164fb..6318952f34 100644 --- a/ydb/core/testlib/actors/test_runtime.cpp +++ b/ydb/core/testlib/actors/test_runtime.cpp @@ -17,10 +17,10 @@ namespace NActors { void TTestActorRuntime::TNodeData::Stop() { + TNodeDataBase::Stop(); if (Mon) { Mon->Stop(); } - TNodeDataBase::Stop(); } TTestActorRuntime::TNodeData::~TNodeData() { diff --git a/ydb/core/testlib/test_client.cpp b/ydb/core/testlib/test_client.cpp index e54505f33f..6909485339 100644 --- a/ydb/core/testlib/test_client.cpp +++ b/ydb/core/testlib/test_client.cpp @@ -884,10 +884,6 @@ namespace Tests { } TServer::~TServer() { - if (Runtime->GetAppData().Mon) { - Runtime->GetAppData().Mon->Stop(); - } - if (GRpcServer) { GRpcServer->Stop(); } |