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

View file

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

View file

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