Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for GET requests and code update for UE 5.4 #6

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions Source/PsWebServer/Private/PsWebServerHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,44 @@ FPsWebServerHandlerImpl::FPsWebServerHandlerImpl()
CachedResponseHeaders = PrintHeadersToString(ResponseHeaders);
}

bool FPsWebServerHandlerImpl::handleGet(CivetServer* Server, mg_connection* RequestConnection)
{
const FGuid RequestId = FGuid::NewGuid();

const auto Handler = OwnerHandler.Load();
if (!Handler || Handler == nullptr)
{
const FString StatusCode = TEXT("503 Service Unavailable");
const FString ResponseData = TEXT("Request handler is not valid");
return SendResponse(RequestConnection, RequestId, StatusCode, ResponseData);
}

// Create cancellation source
auto CancellationSource = FPsWebCancellationSource{};
const auto CancellationToken = CancellationSource.GetToken();

FEvent* const RequestReadyEvent = CreateContext(RequestConnection, RequestId, MoveTemp(CancellationSource));
FString PostData = PsWebServerUtils::GetPostData(RequestConnection);

FString UeBuf(mg_get_request_info(RequestConnection)->query_string);

// Set request processing on the game thread
DECLARE_DELEGATE(FTaskDelegate);
auto TaskDelegate = FTaskDelegate::CreateWeakLambda(Handler, [Handler, RequestId, PostData = MoveTemp(UeBuf), CancellationToken]() mutable {
Handler->SetRequestOnNextTick(RequestId, MoveTemp(PostData), CancellationToken);
});
AsyncTask(ENamedThreads::GameThread, [TaskDelegate = MoveTemp(TaskDelegate)] { TaskDelegate.Execute(); });

return WaitForResponse(RequestConnection, RequestReadyEvent, RequestId);
}

bool FPsWebServerHandlerImpl::handlePost(CivetServer* Server, mg_connection* RequestConnection)
{
// Unique request id
const FGuid RequestId = FGuid::NewGuid();

const auto Handler = OwnerHandler.Load();
if (!Handler || Handler->IsPendingKill())
if (!Handler || Handler == nullptr)
Copy link

@IlinAleksey IlinAleksey Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use !IsValid(Handler) as a replacement for !Handler || Handler->IsPendingKill(). These are basically the same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it hadn't occurred to me for some reason

{
const FString StatusCode = TEXT("503 Service Unavailable");
const FString ResponseData = TEXT("Request handler is not valid");
Expand Down Expand Up @@ -166,7 +197,7 @@ FString FPsWebServerHandlerImpl::GetResponseData(const FGuid& RequestId, bool bT
Context.CancellationSource.Cancel();

const auto Handler = OwnerHandler.Load();
if (Handler && !Handler->IsPendingKill())
if (Handler && Handler == nullptr)
Copy link

@IlinAleksey IlinAleksey Aug 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use IsValid(Handler) as a replacement for Handler && !Handler->IsPendingKill().

{
return Handler->GetTimeoutResponse();
}
Expand Down