Compare commits
4 commits
4903b6320a
...
b12616420b
Author | SHA1 | Date | |
---|---|---|---|
|
b12616420b | ||
|
5447f37cea | ||
|
e0ae00c523 | ||
|
ba79856131 |
4 changed files with 24 additions and 5 deletions
|
@ -76,6 +76,11 @@ public sealed class HttpRequest
|
|||
/// </summary>
|
||||
public bool IsWebsocket => Headers.TryGetValue("Upgrade", out string? connection) && connection.Equals("websocket", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>
|
||||
/// The total declared size of the request body, in bytes.
|
||||
/// </summary>
|
||||
public int ContentLength => Headers.TryGetValue("Content-Length", out string? contentLengthStr) && int.TryParse(contentLengthStr, out int contentLength) ? contentLength : 0;
|
||||
|
||||
internal HttpRequest(HttpServer server, TcpClient client, Stream stream, IPEndPoint endpoint)
|
||||
{
|
||||
Server = server;
|
||||
|
@ -374,6 +379,16 @@ public sealed class HttpRequest
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the entire request body. Only call this once.
|
||||
/// </summary>
|
||||
public async Task<string> ReadBody()
|
||||
{
|
||||
byte[] data = new byte[ContentLength];
|
||||
int count = await ReadBytes(data);
|
||||
return Encoding.UTF8.GetString(data, 0, count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -89,7 +89,11 @@ public partial class FileEndpoint : RouterBase
|
|||
{
|
||||
mime = GuessMIMEType(fileInfo.Extension);
|
||||
}
|
||||
return !File.Exists(assetPath) ? null : (HttpResponse)new HttpContent(mime, await File.ReadAllBytesAsync(assetPath));
|
||||
|
||||
if (!File.Exists(assetPath))
|
||||
return null;
|
||||
|
||||
return new HttpContent(mime, await File.ReadAllBytesAsync(assetPath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -15,7 +15,7 @@ public class FuncEndpoint : RouterBase
|
|||
Function = function;
|
||||
}
|
||||
|
||||
public override HttpMethod Method => HttpMethod.GET;
|
||||
public override HttpMethod Method => HttpMethod.Any;
|
||||
|
||||
public override int Arguments => 0;
|
||||
|
||||
|
|
|
@ -13,16 +13,16 @@ public class Router : RouterBase
|
|||
|
||||
public override HttpMethod Method => HttpMethod.Any;
|
||||
|
||||
public override int Arguments => 1;
|
||||
public override int Arguments => 0;
|
||||
|
||||
protected override async Task<HttpResponse?> GetResponseInner(HttpRequest req, ArraySegment<string> path)
|
||||
{
|
||||
string next = path[0];
|
||||
string next = path.Count == 0 ? string.Empty : path[0];
|
||||
foreach (Route route in Routes)
|
||||
{
|
||||
if (route.Key == next || route.Key == null)
|
||||
{
|
||||
HttpResponse? resp = await route.Router.GetResponse(req, route.Key == null ? path : path[1..]);
|
||||
HttpResponse? resp = await route.Router.GetResponse(req, route.Key == null || path.Count == 0 ? path : path[1..]);
|
||||
if (resp != null)
|
||||
return resp;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue