This commit is contained in:
uwaa 2024-10-25 06:18:50 +01:00
parent 8b29044671
commit 04e91b4d7b
3 changed files with 27 additions and 22 deletions

View file

@ -48,7 +48,7 @@ public sealed class HttpRequest
/// </summary>
public readonly Dictionary<string, string> Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
internal readonly BufferedStream Buffer;
readonly BufferedStream Buffer;
readonly Decoder Decoder;
@ -68,10 +68,9 @@ public sealed class HttpRequest
Decoder = Encoding.UTF8.GetDecoder();
Writer = new StreamWriter(Buffer, Encoding.ASCII);
Writer.AutoFlush = true;
}
public async Task ReadAll()
internal async Task ReadAllHeaders()
{
//Read initial header
string? header = await ReadLine();
@ -127,7 +126,7 @@ public sealed class HttpRequest
}
}
public async Task<string> ReadLine()
async Task<string> ReadLine()
{
byte[] dataBuffer = new byte[1];
char[] charBuffer = new char[4096];
@ -152,7 +151,13 @@ public sealed class HttpRequest
return new string(charBuffer, 0, charBufferIndex);
}
public async Task WriteStatus(int code, string message)
public ValueTask<int> ReadBytes(Memory<byte> buffer)
{
return Buffer.ReadAsync(buffer);
}
async Task WriteStatus(int code, string message)
{
await Writer.WriteAsync("HTTP/1.1 ");
await Writer.WriteAsync(code.ToString());
@ -161,7 +166,7 @@ public sealed class HttpRequest
await WriteLine();
}
public async Task WriteHeader(string name, string value)
async Task WriteHeader(string name, string value)
{
await Writer.WriteAsync(name);
await Writer.WriteAsync(": ");
@ -169,19 +174,19 @@ public sealed class HttpRequest
await WriteLine();
}
public async Task WriteLine()
async Task WriteLine()
{
await Writer.WriteAsync("\r\n");
}
public async Task WriteBytes(byte[] bytes)
public ValueTask WriteBytes(ReadOnlyMemory<byte> bytes)
{
await Buffer.WriteAsync(bytes);
return Buffer.WriteAsync(bytes);
}
public async Task Flush()
public Task Flush()
{
await Buffer.FlushAsync();
return Buffer.FlushAsync();
}
/// <summary>

View file

@ -114,7 +114,7 @@ public sealed class HttpServer
HttpRequest req = new HttpRequest(this, client, stream);
try
{
await req.ReadAll();
await req.ReadAllHeaders();
//Parse path
ArraySegment<string> pathSpl = req.Path.Split('/');

View file

@ -42,7 +42,7 @@ public sealed class Websocket
while (true)
{
//First byte
if (await Request.Buffer.ReadAsync(recvBuffer.AsMemory(0, 2)) < 2)
if (await Request.ReadBytes(recvBuffer.AsMemory(0, 2)) < 2)
throw new EndOfStreamException();
byte firstByte = recvBuffer[0];
@ -70,14 +70,14 @@ public sealed class Websocket
{
if (secondByte == 126)
{
if (await Request.Buffer.ReadAsync(recvBuffer.AsMemory(0, 2)) < 2)
if (await Request.ReadBytes(recvBuffer.AsMemory(0, 2)) < 2)
throw new EndOfStreamException();
payloadLength = BinaryPrimitives.ReadUInt16BigEndian(recvBuffer);
}
else if (secondByte == 127)
{
if (await Request.Buffer.ReadAsync(recvBuffer.AsMemory(0, 8)) < 8)
if (await Request.ReadBytes(recvBuffer.AsMemory(0, 8)) < 8)
throw new EndOfStreamException();
payloadLength = BinaryPrimitives.ReadUInt32BigEndian(recvBuffer);
@ -95,7 +95,7 @@ public sealed class Websocket
byte maskKey1, maskKey2, maskKey3, maskKey4;
if (maskEnabled)
{
if (await Request.Buffer.ReadAsync(recvBuffer.AsMemory(0, 4)) < 4)
if (await Request.ReadBytes(recvBuffer.AsMemory(0, 4)) < 4)
throw new EndOfStreamException();
maskKey1 = recvBuffer[0];
@ -116,7 +116,7 @@ public sealed class Websocket
try
{
ArraySegment<byte> payload = new ArraySegment<byte>(payloadBuffer, 0, (int)payloadLength);
if (await Request.Buffer.ReadAsync(payload) < payloadLength)
if (await Request.ReadBytes(payload) < payloadLength)
throw new EndOfStreamException();
//Unmask payload
@ -220,12 +220,12 @@ public sealed class Websocket
firstByte |= (byte)((int)frame.Opcode & 0b00001111);
writeBuf[0] = firstByte;
await Request.Buffer.WriteAsync(writeBuf.AsMemory(0, 1));
await Request.WriteBytes(writeBuf.AsMemory(0, 1));
if (frame.Payload.Count < 126)
{
writeBuf[0] = (byte)frame.Payload.Count;
await Request.Buffer.WriteAsync(writeBuf.AsMemory(0, 1));
await Request.WriteBytes(writeBuf.AsMemory(0, 1));
}
else
{
@ -233,17 +233,17 @@ public sealed class Websocket
{
writeBuf[0] = 126;
BinaryPrimitives.WriteUInt16BigEndian(writeBuf.AsSpan(1), (ushort)frame.Payload.Count);
await Request.Buffer.WriteAsync(writeBuf.AsMemory(0, 3));
await Request.WriteBytes(writeBuf.AsMemory(0, 3));
}
else
{
writeBuf[0] = 127;
BinaryPrimitives.WriteUInt64BigEndian(writeBuf.AsSpan(1), (ulong)frame.Payload.Count);
await Request.Buffer.WriteAsync(writeBuf.AsMemory(0, 9));
await Request.WriteBytes(writeBuf.AsMemory(0, 9));
}
}
await Request.Buffer.WriteAsync(frame.Payload);
await Request.WriteBytes(frame.Payload);
await Request.Flush();
}
finally