Repository navigation
Expand file tree
/
Copy pathResponseParser.cs
More file actions
108 lines (87 loc) · 3.01 KB
/
Copy pathResponseParser.cs
File metadata and controls
108 lines (87 loc) · 3.01 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Text;
namespace Http11Probe.Response;
public static class ResponseParser
{
public static HttpResponse? TryParse(ReadOnlySpan<byte> data, int length)
{
if (length == 0)
return null;
var text = Encoding.ASCII.GetString(data[..length]);
// Find end of status line
var lineEnd = text.IndexOf('\n');
if (lineEnd < 0)
return null;
var statusLine = text[..lineEnd].TrimEnd('\r');
// Parse "HTTP/x.x SSS Reason"
var firstSpace = statusLine.IndexOf(' ');
if (firstSpace < 0)
return null;
var httpVersion = statusLine[..firstSpace];
var rest = statusLine[(firstSpace + 1)..];
var secondSpace = rest.IndexOf(' ');
string statusCodeStr;
string reasonPhrase;
if (secondSpace >= 0)
{
statusCodeStr = rest[..secondSpace];
reasonPhrase = rest[(secondSpace + 1)..];
}
else
{
statusCodeStr = rest;
reasonPhrase = string.Empty;
}
if (!int.TryParse(statusCodeStr, out var statusCode))
return null;
// Parse headers
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var pos = lineEnd + 1;
while (pos < text.Length)
{
var nextLineEnd = text.IndexOf('\n', pos);
if (nextLineEnd < 0)
nextLineEnd = text.Length;
var line = text[pos..nextLineEnd].TrimEnd('\r');
// Empty line = end of headers
if (line.Length == 0)
break;
var colonIndex = line.IndexOf(':');
if (colonIndex > 0)
{
var name = line[..colonIndex].Trim();
var value = line[(colonIndex + 1)..].Trim();
// If duplicate header, append with comma (RFC 9110 §5.3)
if (headers.TryGetValue(name, out var existing))
headers[name] = existing + ", " + value;
else
headers[name] = value;
}
pos = nextLineEnd + 1;
}
// Extract body after \r\n\r\n
string? body = null;
var headerEnd = text.IndexOf("\r\n\r\n", StringComparison.Ordinal);
if (headerEnd >= 0)
{
var bodyStart = headerEnd + 4;
if (bodyStart < text.Length)
{
var bodyText = text[bodyStart..];
body = bodyText.Length > 4096 ? bodyText[..4096] : bodyText;
}
}
var rawResponse = text.Length > 8192
? text[..8192] + $"\n\n[Truncated — showing 8,192 of {text.Length:N0} bytes]"
: text;
return new HttpResponse
{
StatusCode = statusCode,
ReasonPhrase = reasonPhrase,
HttpVersion = httpVersion,
Headers = headers,
IsEmpty = false,
RawResponse = rawResponse,
Body = body
};
}
}