add code to read request body

This commit is contained in:
uwaa 2024-11-14 02:33:12 +00:00
parent e0ae00c523
commit 5447f37cea

View file

@ -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>