-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from seionmoya/rework-httpcontext
Rework httpcontext
- Loading branch information
Showing
79 changed files
with
836 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
Fuyu.Backend.Core/Controllers/AccountRegisterGameController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 7 additions & 4 deletions
11
Fuyu.Backend.EFT/Controllers/Http/AccountCustomizationController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
Fuyu.Backend.EFT/Controllers/Http/AchievementListController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
12 changes: 8 additions & 4 deletions
12
Fuyu.Backend.EFT/Controllers/Http/AchievementStatisticController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.