normalize urls during registering and requesting so that they all start with a single slash

This commit is contained in:
GHXX 2024-01-14 00:55:43 +01:00
parent f0c9754fb2
commit 020075ad54

View File

@ -118,25 +118,27 @@ public sealed class HttpServer {
} }
foreach (var location in attrib.Locations) { foreach (var location in attrib.Locations) {
int idx = location.IndexOf('{'); var normLocation = NormalizeUrlPath(location);
int idx = normLocation.IndexOf('{');
if (idx >= 0) { if (idx >= 0) {
// this path contains path parameters // this path contains path parameters
throw new NotImplementedException("Path parameters are not yet implemented!"); throw new NotImplementedException("Path parameters are not yet implemented!");
} }
var reqMethod = Enum.GetName(attrib.RequestMethod) ?? throw new ArgumentException("Request method was undefined"); var reqMethod = Enum.GetName(attrib.RequestMethod) ?? throw new ArgumentException("Request method was undefined");
simpleEndpointMethodInfos.Add((location, reqMethod), new EndpointInvocationInfo(mi, qparams)); simpleEndpointMethodInfos.Add((normLocation, reqMethod), new EndpointInvocationInfo(mi, qparams));
} }
} }
} }
private readonly Dictionary<Type, IParameterConverter> stringToTypeParameterConverters = new(); private readonly Dictionary<Type, IParameterConverter> stringToTypeParameterConverters = new();
private string NormalizeUrlPath(string url) => '/' + url.TrimStart('/');
private async Task ProcessRequestAsync(HttpListenerContext ctx) { private async Task ProcessRequestAsync(HttpListenerContext ctx) {
try { try {
var decUri = WebUtility.UrlDecode(ctx.Request.RawUrl)!; // TODO add path escape countermeasures+unittests var decUri = WebUtility.UrlDecode(ctx.Request.RawUrl)!; // TODO add path escape countermeasures+unittests
var splitted = decUri.Split('?', 2, StringSplitOptions.None); var splitted = NormalizeUrlPath(decUri).Split('?', 2, StringSplitOptions.None);
var path = WebUtility.UrlDecode(splitted.First()); var path = WebUtility.UrlDecode(splitted.First());