Files
UnrealEngineUWP/Engine/Source/Runtime/Online/HTTPServer/Private/HttpServerResponse.cpp
lorry li 67cf5fe8e4 Fix untagged http allocations for http server.
#jira UE-155145
#review-24037950 @jake.niman @michael.kirzinger @rob.cannaday
#preflight 63e268181466610c64972e28

[CL 24048638 by lorry li in ue5-main branch]
2023-02-07 10:13:06 -05:00

61 lines
2.5 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "HttpServerResponse.h"
#include "HttpServerConstants.h"
#include "HttpServerConstantsPrivate.h"
TUniquePtr<FHttpServerResponse> FHttpServerResponse::Create(const FString& Text, FString ContentType)
{
TUniquePtr<FHttpServerResponse> Response = MakeUnique<FHttpServerResponse>();
Response->Code = EHttpServerResponseCodes::Ok;
FTCHARToUTF8 ConvertToUtf8(*Text);
const uint8* ConvertToUtf8Bytes = (reinterpret_cast<const uint8*>(ConvertToUtf8.Get()));
Response->Body.Append(ConvertToUtf8Bytes, ConvertToUtf8.Length());
FString Utf8CharsetContentType = FString::Printf(TEXT("%s;charset=utf-8"), *ContentType);
TArray<FString> ContentTypeValue = { MoveTemp(Utf8CharsetContentType) };
Response->Headers.Add(UE_HTTP_SERVER_HEADER_KEYS_CONTENT_TYPE, MoveTemp(ContentTypeValue));
return Response;
}
TUniquePtr<FHttpServerResponse> FHttpServerResponse::Create(TArray<uint8>&& RawBytes, FString ContentType)
{
TUniquePtr<FHttpServerResponse> Response = MakeUnique<FHttpServerResponse>(MoveTemp(RawBytes));
Response->Code = EHttpServerResponseCodes::Ok;
TArray<FString> ContentTypeValue = { MoveTemp(ContentType) };
Response->Headers.Add(UE_HTTP_SERVER_HEADER_KEYS_CONTENT_TYPE, MoveTemp(ContentTypeValue));
return Response;
}
TUniquePtr<FHttpServerResponse> FHttpServerResponse::Create(const TArrayView<uint8>& RawBytes, FString ContentType)
{
TUniquePtr<FHttpServerResponse> Response = MakeUnique<FHttpServerResponse>();
Response->Code = EHttpServerResponseCodes::Ok;
Response->Body.Append(RawBytes.GetData(), RawBytes.Num());
TArray<FString> ContentTypeValue = { MoveTemp(ContentType) };
Response->Headers.Add(UE_HTTP_SERVER_HEADER_KEYS_CONTENT_TYPE, MoveTemp(ContentTypeValue));
return Response;
}
TUniquePtr<FHttpServerResponse> FHttpServerResponse::Ok()
{
TUniquePtr<FHttpServerResponse> Response = MakeUnique<FHttpServerResponse>();
Response->Code = EHttpServerResponseCodes::NoContent;
return Response;
}
TUniquePtr<FHttpServerResponse> FHttpServerResponse::Error(EHttpServerResponseCodes ResponseCode,
const FString& ErrorCode /*=TEXT("")*/, const FString& ErrorMessage /*=TEXT("")*/)
{
const FString ErrorCodeEscaped = ErrorCode.ReplaceCharWithEscapedChar();
const FString ErrorMessageEscaped = ErrorMessage.ReplaceCharWithEscapedChar();
const FString ResponseBody = FString::Printf(TEXT("{\"errorCode\": \"%s\", \"errorMessage\": \"%s\"}"), *ErrorCodeEscaped, *ErrorMessageEscaped);
auto Response = Create(ResponseBody, TEXT("application/json"));
Response->Code = ResponseCode;
return Response;
}