provide additional path and query string parsing out-of-the-box

This commit is contained in:
Michael Becker 2024-11-01 23:07:05 -04:00
parent f0542c03f9
commit 05c3fd5274

View File

@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Data.SqlTypes;
using System.Net;
namespace MBS.Web;
@ -8,6 +9,10 @@ public class WebRequest
public string Version { get; }
public string Method { get; }
public string Path { get; }
public string[] PathParts { get; }
public string PathAndQuery { get; }
public string QueryString { get; }
public Dictionary<string, List<string>> Query { get; }
public Uri? Uri { get; } = null;
@ -20,7 +25,40 @@ public class WebRequest
{
Version = version;
Method = method;
Path = path;
QueryString = String.Empty;
Query = new Dictionary<string, List<string>>();
int queryStringIndex = path.IndexOf('?');
if (queryStringIndex > -1)
{
Path = path.Substring(0, queryStringIndex);
QueryString = path.Substring(queryStringIndex + 1);
}
else
{
Path = path;
}
PathParts = Path.Split(new char[] { '/' });
string[] queryStringParts = QueryString.Split(new char[] { '&' });
foreach (string queryStringPart in queryStringParts)
{
string[] nameValue = queryStringPart.Split(new char[] { '=' });
if (nameValue.Length > 1)
{
if (!Query.ContainsKey(nameValue[0]))
{
Query[nameValue[0]] = new List<string>();
}
Query[nameValue[0]].Add(nameValue[1]);
}
else
{
Query[nameValue[0]] = new List<string>();
}
}
PathAndQuery = path;
if (Uri.TryCreate(path, UriKind.Relative, out Uri? uri))
{
Uri = uri;