Files
UnrealEngineUWP/Engine/Source/Runtime/Online/HTTP/Private/GenericPlatform/HttpRequestCommon.cpp
lorry li 48e220c626 Refactor to prepare for retry migration from retry system to http request, should have no behavior change:
- Move timeout related functions into FHttpRequestCommon, make sure those functions are not exposed to FHttpRequestAdapterBase;
- Rename PreCheck to PreProcess, also clear internal states in PreProcess to make sure the request is clean for retry.

[REVIEW] [at]rafa.lecina [at]michael.kirzinger [at]michael.atchison
#jira UE-197485

#rb Rafa.Lecina

[CL 30186509 by lorry li in ue5-main branch]
2023-12-07 13:38:15 -05:00

164 lines
3.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "GenericPlatform/HttpRequestCommon.h"
#include "GenericPlatform/HttpResponseCommon.h"
#include "Http.h"
#include "HttpManager.h"
FString FHttpRequestCommon::GetURLParameter(const FString& ParameterName) const
{
FString ReturnValue;
if (TOptional<FString> OptionalParameterValue = FGenericPlatformHttp::GetUrlParameter(GetURL(), ParameterName))
{
ReturnValue = MoveTemp(OptionalParameterValue.GetValue());
}
return ReturnValue;
}
EHttpRequestStatus::Type FHttpRequestCommon::GetStatus() const
{
return CompletionStatus;
}
EHttpFailureReason FHttpRequestCommon::GetFailureReason() const
{
return FailureReason;
}
bool FHttpRequestCommon::PreCheck() const
{
// Prevent overlapped requests using the same instance
if (CompletionStatus == EHttpRequestStatus::Processing)
{
UE_LOG(LogHttp, Warning, TEXT("ProcessRequest failed. Still processing last request."));
return false;
}
// Nothing to do without a valid URL
if (GetURL().IsEmpty())
{
UE_LOG(LogHttp, Warning, TEXT("ProcessRequest failed. No URL was specified."));
return false;
}
if (GetVerb().IsEmpty())
{
UE_LOG(LogHttp, Warning, TEXT("ProcessRequest failed. No Verb was specified."));
return false;
}
if (!FHttpModule::Get().GetHttpManager().IsDomainAllowed(GetURL()))
{
UE_LOG(LogHttp, Warning, TEXT("ProcessRequest failed. URL '%s' is not using an allowed domain."), *GetURL());
return false;
}
return true;
}
bool FHttpRequestCommon::PreProcess()
{
ClearInCaseOfRetry();
if (!PreCheck() || !SetupRequest())
{
FinishRequestNotInHttpManager();
return false;
}
return true;
}
void FHttpRequestCommon::ClearInCaseOfRetry()
{
// TODO: clear response shared ptr here as well after moving it from child class to this class
FailureReason = EHttpFailureReason::None;
}
void FHttpRequestCommon::FinishRequestNotInHttpManager()
{
if (IsInGameThread())
{
if (DelegateThreadPolicy == EHttpRequestDelegateThreadPolicy::CompleteOnGameThread)
{
FinishRequest();
}
else
{
FHttpModule::Get().GetHttpManager().AddHttpThreadTask([StrongThis = StaticCastSharedRef<FHttpRequestCommon>(AsShared())]()
{
StrongThis->FinishRequest();
});
}
}
else
{
if (DelegateThreadPolicy == EHttpRequestDelegateThreadPolicy::CompleteOnHttpThread)
{
FinishRequest();
}
else
{
FHttpModule::Get().GetHttpManager().AddGameThreadTask([StrongThis = StaticCastSharedRef<FHttpRequestCommon>(AsShared())]()
{
StrongThis->FinishRequest();
});
}
}
}
void FHttpRequestCommon::SetDelegateThreadPolicy(EHttpRequestDelegateThreadPolicy InDelegateThreadPolicy)
{
DelegateThreadPolicy = InDelegateThreadPolicy;
}
EHttpRequestDelegateThreadPolicy FHttpRequestCommon::GetDelegateThreadPolicy() const
{
return DelegateThreadPolicy;
}
void FHttpRequestCommon::SetStatus(EHttpRequestStatus::Type InCompletionStatus)
{
CompletionStatus = InCompletionStatus;
if (FHttpResponsePtr Response = GetResponse())
{
TSharedPtr<FHttpResponseCommon> ResponseCommon = StaticCastSharedPtr<FHttpResponseCommon>(Response);
ResponseCommon->SetRequestStatus(InCompletionStatus);
}
}
void FHttpRequestCommon::SetFailureReason(EHttpFailureReason InFailureReason)
{
check(FailureReason == EHttpFailureReason::None);
FailureReason = InFailureReason;
if (FHttpResponsePtr Response = GetResponse())
{
TSharedPtr<FHttpResponseCommon> ResponseCommon = StaticCastSharedPtr<FHttpResponseCommon>(Response);
ResponseCommon->SetRequestFailureReason(InFailureReason);
}
}
void FHttpRequestCommon::SetTimeout(float InTimeoutSecs)
{
TimeoutSecs = InTimeoutSecs;
}
void FHttpRequestCommon::ClearTimeout()
{
TimeoutSecs.Reset();
}
TOptional<float> FHttpRequestCommon::GetTimeout() const
{
return TimeoutSecs;
}
float FHttpRequestCommon::GetTimeoutOrDefault() const
{
return GetTimeout().Get(FHttpModule::Get().GetHttpTimeout());
}