-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.cs
More file actions
128 lines (117 loc) · 3.84 KB
/
Command.cs
File metadata and controls
128 lines (117 loc) · 3.84 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
using System;
using System.Net.Http;
namespace Https
{
struct Command
{
public HttpMethod Method { get; }
public Uri Uri { get; }
Command(Uri uri)
{
Method = default;
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
Command(HttpMethod method, Uri uri)
{
Method = method ?? throw new ArgumentNullException(nameof(method));
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
static bool StartsWithHttp(string s) =>
s.Length > 6 && s[0] == 'h' && s[1] == 't' && s[2] == 't' && s[3] == 'p' && (s[4] == ':' || (s[4] == 's' && s[5] == ':'));
static bool TryParseUri(string s, out Uri uri)
{
if (!StartsWithHttp(s))
{
s = "https://" + s;
}
return Uri.TryCreate(s, UriKind.Absolute, out uri);
}
static bool TryParseMethod(string s, out HttpMethod method)
{
if (s.Equals(nameof(HttpMethod.Delete), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Delete;
return true;
}
else if (s.Equals(nameof(HttpMethod.Get), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Get;
return true;
}
else if (s.Equals(nameof(HttpMethod.Head), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Head;
return true;
}
else if (s.Equals(nameof(HttpMethod.Options), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Options;
return true;
}
else if (s.Equals(nameof(HttpMethod.Patch), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Patch;
return true;
}
else if (s.Equals(nameof(HttpMethod.Post), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Post;
return true;
}
else if (s.Equals(nameof(HttpMethod.Put), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Put;
return true;
}
else if (s.Equals(nameof(HttpMethod.Trace), StringComparison.OrdinalIgnoreCase))
{
method = HttpMethod.Trace;
return true;
}
method = default;
return false;
}
public static bool TryParse(string methodText, string uriText, out Command command)
{
if (TryParseMethod(methodText, out var method) && TryParseUri(uriText, out var uri))
{
command = new Command(method, uri);
return true;
}
else
{
command = default;
return false;
}
}
public static bool TryParse(string s, out Command command)
{
s = s.Trim();
if (s.StartsWith('-') || s == "help")
{
command = default;
return false;
}
var index = s.IndexOf(' ');
if (index == -1)
{
if (TryParseUri(s, out var uri))
{
command = new Command(uri);
return true;
}
else
{
command = default;
return false;
}
}
else
{
var methodText = s.Substring(0, index);
var uriText = s.Substring(index + 1);
return TryParse(methodText, uriText, out command);
}
}
}
}