Repository navigation
Expand file tree
/
Copy pathHttpFileRequestParser.cs
More file actions
144 lines (124 loc) · 5.02 KB
/
Copy pathHttpFileRequestParser.cs
File metadata and controls
144 lines (124 loc) · 5.02 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace NpgsqlRestClient.Testing;
/// <summary>
/// Parses a single HTTP request out of a block-comment body, using a single-request subset of the
/// Microsoft .http syntax plus the test directives <c># @claim</c> and <c># @response</c>.
/// Returns null when the block is not an HTTP request (ordinary SQL comment).
/// </summary>
public static class HttpFileRequestParser
{
private static readonly string[] Methods = ["GET", "PUT", "POST", "DELETE"];
/// <summary>
/// Try to parse <paramref name="commentBody"/> (the text between /* and */) as an HTTP request.
/// Returns null if the block's first non-blank/non-comment line is not a request line.
/// </summary>
public static HttpStep? TryParse(string commentBody, int lineNumber)
{
var lines = commentBody.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
// Find the request line: first line that is not blank and not a #/// comment line.
int idx = 0;
string? requestLine = null;
for (; idx < lines.Length; idx++)
{
var t = lines[idx].Trim();
if (t.Length == 0) continue;
if (t.StartsWith('#') || t.StartsWith("//")) continue;
requestLine = t;
break;
}
if (requestLine is null) return null;
if (!TryParseRequestLine(requestLine, out var method, out var path)) return null;
idx++; // consume the request line
var headers = new List<(string, string)>();
var claims = new List<(string, string)>();
string? responseTable = null;
int bodyStart = -1;
for (; idx < lines.Length; idx++)
{
var t = lines[idx].Trim();
if (t.Length == 0)
{
bodyStart = idx + 1; // body begins after the blank line
break;
}
string? directive = null;
if (t.StartsWith('#')) directive = t[1..].TrimStart();
else if (t.StartsWith("//")) directive = t[2..].TrimStart();
if (directive is not null)
{
if (directive.StartsWith('@'))
{
ParseDirective(directive[1..].Trim(), claims, ref responseTable);
}
// plain comment line → ignore
continue;
}
// Header: Name: Value
int colon = t.IndexOf(':');
if (colon > 0)
{
headers.Add((t[..colon].Trim(), t[(colon + 1)..].Trim()));
}
// otherwise: stray line in the header region → ignore
}
string? body = null;
if (bodyStart >= 0 && bodyStart < lines.Length)
{
var bodyText = string.Join('\n', lines[bodyStart..]).Trim();
if (bodyText.Length > 0) body = bodyText;
}
return new HttpStep(method, path, headers, claims, body, responseTable, lineNumber);
}
// [HTTP] METHOD /path [HTTP/x]. Method required (GET|PUT|POST|DELETE); path must start with '/';
// an optional trailing HTTP-version token is allowed but ignored; anything else → not a request.
private static bool TryParseRequestLine(string line, out string method, out string path)
{
method = "";
path = "";
var tokens = line.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 0) return false;
int ti = 0;
if (string.Equals(tokens[ti], "HTTP", StringComparison.OrdinalIgnoreCase))
{
ti++; // optional leading HTTP keyword
}
if (ti >= tokens.Length) return false;
var m = tokens[ti].ToUpperInvariant();
if (Array.IndexOf(Methods, m) < 0) return false;
ti++;
if (ti >= tokens.Length) return false; // method but no path
var p = tokens[ti];
if (!p.StartsWith('/')) return false; // reject prose like "GET the data"
ti++;
// At most one trailing token, which must be an HTTP-version; otherwise it's prose, not a request.
if (ti < tokens.Length)
{
if (!tokens[ti].StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase)) return false;
ti++;
if (ti < tokens.Length) return false;
}
method = m;
path = p;
return true;
}
private static void ParseDirective(
string directive,
List<(string, string)> claims,
ref string? responseTable)
{
int sp = directive.IndexOfAny([' ', '\t']);
var keyword = sp < 0 ? directive : directive[..sp];
var rest = sp < 0 ? "" : directive[(sp + 1)..].Trim();
if (string.Equals(keyword, "claim", StringComparison.OrdinalIgnoreCase))
{
int eq = rest.IndexOf('=');
if (eq > 0)
{
claims.Add((rest[..eq].Trim(), rest[(eq + 1)..].Trim()));
}
}
else if (string.Equals(keyword, "response", StringComparison.OrdinalIgnoreCase))
{
if (rest.Length > 0) responseTable = rest;
}
}
}