-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOHelper.cs
51 lines (41 loc) · 1.2 KB
/
IOHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Text;
namespace Httpd;
internal static class IOHelper
{
public static Task<string> ReadHttpLineAsync(this Stream s)
{
var buf = new StringBuilder();
char last = default;
char curr;
int val;
while (true)
{
val = s.ReadByte();
if (val == -1) break; // EOF
else
{
curr = (char)val;
if (!char.IsAscii(curr))
throw new InvalidDataException($"Char '0x{val:X2}' is not US-ASCII well formed");
if (curr == '\n')
{
if (last != '\r')
throw new InvalidOperationException("Unexcepted UNIX line ending");
break;
}
else
{
last = curr;
if (curr != '\r')
buf.Append(curr);
}
}
}
return Task.FromResult(buf.ToString());
}
public static async ValueTask WriteHttpLineAsync(this Stream s, string str = "")
{
var buf = Encoding.ASCII.GetBytes(str + "\r\n");
await s.WriteAsync(buf);
}
}