Skip to content

Commit

Permalink
Fix MVC router
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdante committed Apr 7, 2021
1 parent 77419ec commit 95775a5
Showing 1 changed file with 31 additions and 54 deletions.
85 changes: 31 additions & 54 deletions Modules/HtcSharp.HttpModule/HttpMvc.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -34,10 +35,7 @@ public void Setup(Assembly assembly, ILogger logger) {

public void ReloadControllers() {
_routes.Clear();
MethodInfo[] methods = _assembly.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(HttpMethodAttribute), false).Length > 0)
.ToArray();
MethodInfo[] methods = _assembly.GetTypes().SelectMany(t => t.GetMethods()).Where(m => m.GetCustomAttributes(typeof(HttpMethodAttribute), false).Length > 0).ToArray();
foreach (var method in methods) {
if (method.ReturnType != typeof(Task) || !method.IsStatic) continue;
ParameterInfo[] parameters = method.GetParameters();
Expand All @@ -46,14 +44,9 @@ public void ReloadControllers() {
if (parameters.Length < 1 || parameters[0].ParameterType != typeof(HttpContext)) continue;
if (parameters.Length == 2 && !parameters[1].ParameterType.GetInterfaces().Contains(typeof(IHttpJsonObject))) continue;
_logger?.LogInfo(parameters.Length == 2 ? $"Registering route: [{attribute.Method}, JsonObject] {attribute.Path}" : $"Registering route: [{attribute.Method}] {attribute.Path}");
if (_routes.ContainsKey($"{attribute.Method.ToUpper()}{attribute.Path}")) throw new DuplicateNameException($"Duplicate route [{attribute.Method.ToUpper()}] {attribute.Path}");
_routes.Add($"{attribute.Method.ToUpper()}{attribute.Path}", (attribute, parameters.Length == 2, parameters.Length == 2 ? parameters[1].ParameterType : null, method));
if (UrlMapper.RegisteredPages.TryGetValue(attribute.Path, out var httpEvents)) {
if (httpEvents != this) {
throw new Exception($"The '{attribute.Path}' page has already been registered by another plugin.");
}
return;
}
UrlMapper.RegisterPluginPage(attribute.Path, this);
if (!UrlMapper.RegisteredPages.ContainsKey(attribute.Path)) UrlMapper.RegisterPluginPage(attribute.Path, this);
}
}

Expand Down Expand Up @@ -106,55 +99,39 @@ public async Task OnHttpPageRequest(HttpContext httpContext, string filename) {
try {
if (_routes.TryGetValue($"{httpContext.Request.Method.ToUpper()}{filename}", out var value)) {
await LoadSession(httpContext);
if (value.Item1.RequireSession && httpContext.Session != null && !httpContext.Session.IsAvailable) {
await ThrowInvalidSession(httpContext);
if (value.Item1.RequireSession && httpContext.Session != null && !httpContext.Session.IsAvailable) {
await ThrowInvalidSession(httpContext);
return;
}
if (value.Item1.RequireContentType != null) {
if (httpContext.Request.ContentType == null || httpContext.Request.ContentType.Split(";")[0] != value.Item1.RequireContentType) {
await ThrowInvalidContentType(httpContext);
return;
}

if (value.Item1.RequireContentType != null) {
if (httpContext.Request.ContentType == null || httpContext.Request.ContentType.Split(";")[0] != value.Item1.RequireContentType) {
await ThrowInvalidContentType(httpContext);
}
try {
object[] data;
if (value.Item2) {
var obj = await JsonSerializer.DeserializeAsync(httpContext.Request.Body, value.Item3);
if (obj != null && obj is IHttpJsonObject httpJsonObject && await httpJsonObject.ValidateData(httpContext))
data = new[] { httpContext, obj };
else {
await ThrowFailedDecodeData(httpContext, null);
return;
}
} else {
data = new object[] { httpContext };
}

try {
object[] data;
if (value.Item2) {
var obj = await JsonSerializer.DeserializeAsync(httpContext.Request.Body, value.Item3);
if (obj != null && obj is IHttpJsonObject httpJsonObject && await httpJsonObject.ValidateData(httpContext))
data = new[] {httpContext, obj};
else {
await ThrowFailedDecodeData(httpContext, null);
return;
}
/*using var streamReader = new StreamReader(httpContext.Request.Body, Encoding.UTF8);
if (JsonConvertExt.TryDeserializeObject(await streamReader.ReadToEndAsync(), value.Item3, out var obj)) {
if (obj is IHttpJsonObject httpJsonObject && await httpJsonObject.ValidateData(httpContext))
data = new object[] { httpContext, obj };
else {
if (!httpContext.Response.HasStarted) await httpContext.Response.SendDecodeErrorAsync();
return;
}
} else {
await httpContext.Response.SendDecodeErrorAsync();
}*/

//streamReader.Close();
} else {
data = new object[] {httpContext};
}

// ReSharper disable once PossibleNullReferenceException
await (Task) value.Item4.Invoke(null, data);
} catch (HttpException ex) {
await ThrowHttpException(httpContext, ex);
} catch (JsonException ex) {
await ThrowFailedDecodeData(httpContext, ex);
} catch (Exception ex) {
await ThrowException(httpContext, ex);
}
// ReSharper disable once PossibleNullReferenceException
await (Task)value.Item4.Invoke(null, data);
} catch (HttpException ex) {
await ThrowHttpException(httpContext, ex);
} catch (JsonException ex) {
await ThrowFailedDecodeData(httpContext, ex);
} catch (Exception ex) {
await ThrowException(httpContext, ex);
}
}
} catch (Exception ex) {
await ThrowPreProcessingException(httpContext, ex);
}
Expand Down

0 comments on commit 95775a5

Please sign in to comment.