Skip to content

Commit

Permalink
Merge pull request #99 from seionmoya/rework-httpcontext
Browse files Browse the repository at this point in the history
Rework httpcontext
  • Loading branch information
seionmoya authored Nov 19, 2024
2 parents a586cdf + 08f0bdf commit b4d9191
Show file tree
Hide file tree
Showing 79 changed files with 836 additions and 377 deletions.
6 changes: 3 additions & 3 deletions Fuyu.Backend.Core/Controllers/AccountGamesController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System.Threading.Tasks;
using Fuyu.Backend.Core.Models.Responses;
using Fuyu.Backend.Core.Networking;
using Fuyu.Backend.Core.Services;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.Core.Controllers
{
public class AccountGamesController : HttpController
public class AccountGamesController : CoreHttpController
{
public AccountGamesController() : base("/account/games")
{
}

public override Task RunAsync(HttpContext context)
public override Task RunAsync(CoreHttpContext context)
{
var sessionId = context.GetSessionId();
var result = AccountService.GetGames(sessionId);
Expand Down
6 changes: 3 additions & 3 deletions Fuyu.Backend.Core/Controllers/AccountLoginController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System.Threading.Tasks;
using Fuyu.Backend.Core.Models.Requests;
using Fuyu.Backend.Core.Networking;
using Fuyu.Backend.Core.Services;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.Core.Controllers
{
public class AccountLoginController : HttpController<AccountLoginRequest>
public class AccountLoginController : CoreHttpController<AccountLoginRequest>
{
public AccountLoginController() : base("/account/login")
{
}

public override Task RunAsync(HttpContext context, AccountLoginRequest body)
public override Task RunAsync(CoreHttpContext context, AccountLoginRequest body)
{
var response = AccountService.LoginAccount(body.Username, body.Password);

Expand Down
6 changes: 3 additions & 3 deletions Fuyu.Backend.Core/Controllers/AccountLogoutController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System.Threading.Tasks;
using Fuyu.Common.Networking;
using Fuyu.Backend.Core.Networking;

namespace Fuyu.Backend.Core.Controllers
{
public class AccountLogoutController : HttpController
public class AccountLogoutController : CoreHttpController
{
public AccountLogoutController() : base("/account/logout")
{
}

public override Task RunAsync(HttpContext context)
public override Task RunAsync(CoreHttpContext context)
{
var sessionId = context.GetSessionId();
CoreOrm.RemoveSession(sessionId);
Expand Down
6 changes: 3 additions & 3 deletions Fuyu.Backend.Core/Controllers/AccountRegisterController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System.Threading.Tasks;
using Fuyu.Backend.Core.Models.Requests;
using Fuyu.Backend.Core.Models.Responses;
using Fuyu.Backend.Core.Networking;
using Fuyu.Backend.Core.Services;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.Core.Controllers
{
public class AccountRegisterController : HttpController<AccountRegisterRequest>
public class AccountRegisterController : CoreHttpController<AccountRegisterRequest>
{
public AccountRegisterController() : base("/account/register")
{
}

public override Task RunAsync(HttpContext context, AccountRegisterRequest request)
public override Task RunAsync(CoreHttpContext context, AccountRegisterRequest request)
{
var result = AccountService.RegisterAccount(request.Username, request.Password);
var response = new AccountRegisterResponse()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System.Threading.Tasks;
using Fuyu.Backend.Core.Models.Requests;
using Fuyu.Backend.Core.Networking;
using Fuyu.Backend.Core.Services;
using Fuyu.Common.Networking;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.Core.Controllers
{
public class AccountRegisterGameController : HttpController<AccountRegisterGameRequest>
public class AccountRegisterGameController : CoreHttpController<AccountRegisterGameRequest>
{
public AccountRegisterGameController() : base("/account/register/game")
{
}

public override Task RunAsync(HttpContext context, AccountRegisterGameRequest request)
public override Task RunAsync(CoreHttpContext context, AccountRegisterGameRequest request)
{
var sessionId = context.GetSessionId();
var result = AccountService.RegisterGame(sessionId, request.Game, request.Edition);
Expand Down
93 changes: 93 additions & 0 deletions Fuyu.Backend.Core/Networking/CoreHttpContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Fuyu.Common.Networking;

namespace Fuyu.Backend.Core.Networking
{
public class CoreHttpContext : HttpContext
{
public CoreHttpContext(HttpListenerRequest request, HttpListenerResponse response) : base(request, response)
{
}

public override byte[] GetBinary()
{
using (var ms = new MemoryStream())
{
Request.InputStream.CopyTo(ms);

var body = ms.ToArray();
var encryption = GetEncryption();

if (!string.IsNullOrWhiteSpace(encryption))
{
switch (encryption)
{
case "aes":
// TODO: handle AES-192 encryption
// body = CryptographyService.DecryptAes(body);
break;

default:
throw new InvalidDataException(encryption);
}
}

return body;
}
}

protected Task SendAsync(byte[] data, string mime, HttpStatusCode status, bool encrypted)
{
var hasData = !(data == null);

// Used for postman debugging by Nexus4880
// -- seionmoya, 2024-11-18
#if DEBUG
if (Request.Headers["X-Require-Plaintext"] != null)
{
encrypted = false;
}
#endif

if (hasData && encrypted)
{
// TODO: handle X-Encryption: aes
/*
Response.Headers.Add("X-Encryption", "aes");
data = CryptographyService.EncryptAes(data);
*/
encrypted = false;
}

return SendAsync(data, mime, status);
}

public Task SendBinaryAsync(byte[] data, string mime, bool encrypted)
{
return SendAsync(data, mime, HttpStatusCode.OK, encrypted);
}

public Task SendJsonAsync(string text, bool encrypted)
{
var encoded = Encoding.UTF8.GetBytes(text);
var mime = encrypted
? "application/octet-stream"
: "application/json; charset=utf-8";

return SendAsync(encoded, mime, HttpStatusCode.OK, encrypted);
}

public string GetEncryption()
{
return Request.Headers["X-Encryption"];
}

public string GetSessionId()
{
return Request.Cookies["Session"].Value;
}
}
}
66 changes: 66 additions & 0 deletions Fuyu.Backend.Core/Networking/CoreHttpController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Fuyu.Common.Networking;

namespace Fuyu.Backend.Core.Networking
{
public abstract class CoreHttpController : HttpController
{
protected CoreHttpController(Regex pattern) : base(pattern)
{
// match dynamic paths
}

protected CoreHttpController(string path) : base(path)
{
// match static paths
}

public override Task RunAsync(HttpContext context)
{
var downcast = new CoreHttpContext(context.Request, context.Response);
return RunAsync(downcast);
}

public abstract Task RunAsync(CoreHttpContext context);
}

public abstract class CoreHttpController<TRequest> : CoreHttpController where TRequest : class
{
protected CoreHttpController(Regex pattern) : base(pattern)
{
// match dynamic paths
}

protected CoreHttpController(string path) : base(path)
{
// match static paths
}

public override Task RunAsync(CoreHttpContext context)
{
// TODO:
// - Use better exception type
// -- seionmoya, 2024-10-13
if (!context.HasBody())
{
throw new Exception("Request does not contain body.");
}

var body = context.GetJson<TRequest>();

// TODO:
// - Use better exception type
// -- seionmoya, 2024-10-13
if (body == null)
{
throw new Exception("Body could not be parsed as TRequest.");
}

return RunAsync(context, body);
}

public abstract Task RunAsync(CoreHttpContext context, TRequest body);
}
}
10 changes: 2 additions & 8 deletions Fuyu.Backend.Core/Services/RequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ static RequestService()
// TODO:
// * get address from config
// -- seionmoya, 2024/09/08
_httpClients.Set("fuyu", new EftHttpClient("http://localhost:8000", string.Empty));
_httpClients.Set("eft", new EftHttpClient("http://localhost:8010", string.Empty));
_httpClients.Set("arena", new EftHttpClient("http://localhost:8020", string.Empty));
_httpClients.Set("eft", new HttpClient("http://localhost:8010"));
_httpClients.Set("arena", new HttpClient("http://localhost:8020"));
}

private static T2 HttpPost<T1, T2>(string id, string path, T1 request)
Expand All @@ -41,11 +40,6 @@ private static T2 HttpPost<T1, T2>(string id, string path, T1 request)
return responseValue;
}

public static void CreateSession(string id, string address, string sessionId)
{
_httpClients.Set(id, new EftHttpClient(address, sessionId));
}

public static int RegisterGame(string game, string username, string edition)
{
var request = new FuyuGameRegisterRequest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System.Threading.Tasks;
using Fuyu.Common.Networking;
using Fuyu.Backend.EFT.Networking;

namespace Fuyu.Backend.EFT.Controllers.Http
{
public class AccountCustomizationController : HttpController
public class AccountCustomizationController : EftHttpController
{
public AccountCustomizationController() : base("/client/account/customization")
{
}

public override Task RunAsync(HttpContext context)
public override Task RunAsync(EftHttpContext context)
{
return context.SendJsonAsync(EftOrm.GetAccountCustomization());
// TODO: generate this
// --seionmoya, 2024-11-18
var text = EftOrm.GetAccountCustomization();
return context.SendJsonAsync(text, true, true);
}
}
}
11 changes: 7 additions & 4 deletions Fuyu.Backend.EFT/Controllers/Http/AchievementListController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System.Threading.Tasks;
using Fuyu.Common.Networking;
using Fuyu.Backend.EFT.Networking;

namespace Fuyu.Backend.EFT.Controllers.Http
{
public class AchievementListController : HttpController
public class AchievementListController : EftHttpController
{
public AchievementListController() : base("/client/achievement/list")
{
}

public override Task RunAsync(HttpContext context)
public override Task RunAsync(EftHttpContext context)
{
return context.SendJsonAsync(EftOrm.GetAchievementList());
// TODO: generate this
// --seionmoya, 2024-11-18
var text = EftOrm.GetAchievementList();
return context.SendJsonAsync(text, true, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System.Threading.Tasks;
using Fuyu.Backend.BSG.Models.Responses;
using Fuyu.Common.Networking;
using Fuyu.Backend.EFT.Networking;
using Fuyu.Common.Serialization;

namespace Fuyu.Backend.EFT.Controllers.Http
{
public class AchievementStatisticController : HttpController
public class AchievementStatisticController : EftHttpController
{
public AchievementStatisticController() : base("/client/achievement/statistic")
{
}

public override Task RunAsync(HttpContext context)
public override Task RunAsync(EftHttpContext context)
{
// TODO: generate this
// --seionmoya, 2024-11-18
var json = EftOrm.GetAchievementStatistic();
var response = Json.Parse<ResponseBody<AchievementStatisticResponse>>(json);
return context.SendJsonAsync(Json.Stringify(response));

var text = Json.Stringify(response);
return context.SendJsonAsync(text, true, true);
}
}
}
Loading

0 comments on commit b4d9191

Please sign in to comment.