Skip to content

Commit 75e9ace

Browse files
committed
Moves types to individual code files
1 parent a007698 commit 75e9ace

File tree

9 files changed

+554
-515
lines changed

9 files changed

+554
-515
lines changed

src/https/Command.cs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
namespace Https
5+
{
6+
struct Command
7+
{
8+
public HttpMethod Method { get; }
9+
public Uri Uri { get; }
10+
11+
Command(Uri uri)
12+
{
13+
Method = default;
14+
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
15+
}
16+
Command(HttpMethod method, Uri uri)
17+
{
18+
Method = method ?? throw new ArgumentNullException(nameof(method));
19+
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
20+
}
21+
22+
static bool StartsWithHttp(string s) =>
23+
s.Length > 6 && s[0] == 'h' && s[1] == 't' && s[2] == 't' && s[3] == 'p' && (s[4] == ':' || (s[4] == 's' && s[5] == ':'));
24+
25+
static bool TryParseUri(string s, out Uri uri)
26+
{
27+
if (!StartsWithHttp(s))
28+
{
29+
s = "https://" + s;
30+
}
31+
32+
return Uri.TryCreate(s, UriKind.Absolute, out uri);
33+
}
34+
35+
static bool TryParseMethod(string s, out HttpMethod method)
36+
{
37+
if (s.Equals(nameof(HttpMethod.Delete), StringComparison.OrdinalIgnoreCase))
38+
{
39+
method = HttpMethod.Delete;
40+
return true;
41+
}
42+
else if (s.Equals(nameof(HttpMethod.Get), StringComparison.OrdinalIgnoreCase))
43+
{
44+
method = HttpMethod.Get;
45+
return true;
46+
}
47+
else if (s.Equals(nameof(HttpMethod.Head), StringComparison.OrdinalIgnoreCase))
48+
{
49+
method = HttpMethod.Head;
50+
return true;
51+
}
52+
else if (s.Equals(nameof(HttpMethod.Options), StringComparison.OrdinalIgnoreCase))
53+
{
54+
method = HttpMethod.Options;
55+
return true;
56+
}
57+
else if (s.Equals(nameof(HttpMethod.Patch), StringComparison.OrdinalIgnoreCase))
58+
{
59+
method = HttpMethod.Patch;
60+
return true;
61+
}
62+
else if (s.Equals(nameof(HttpMethod.Post), StringComparison.OrdinalIgnoreCase))
63+
{
64+
method = HttpMethod.Post;
65+
return true;
66+
}
67+
else if (s.Equals(nameof(HttpMethod.Put), StringComparison.OrdinalIgnoreCase))
68+
{
69+
method = HttpMethod.Put;
70+
return true;
71+
}
72+
else if (s.Equals(nameof(HttpMethod.Trace), StringComparison.OrdinalIgnoreCase))
73+
{
74+
method = HttpMethod.Trace;
75+
return true;
76+
}
77+
78+
method = default;
79+
return false;
80+
}
81+
82+
public static bool TryParse(string methodText, string uriText, out Command command)
83+
{
84+
if (TryParseMethod(methodText, out var method) && TryParseUri(uriText, out var uri))
85+
{
86+
command = new Command(method, uri);
87+
return true;
88+
}
89+
else
90+
{
91+
command = default;
92+
return false;
93+
}
94+
}
95+
96+
public static bool TryParse(string s, out Command command)
97+
{
98+
s = s.Trim();
99+
if (s.StartsWith('-') || s == "help")
100+
{
101+
command = default;
102+
return false;
103+
}
104+
105+
var index = s.IndexOf(' ');
106+
if (index == -1)
107+
{
108+
if (TryParseUri(s, out var uri))
109+
{
110+
command = new Command(uri);
111+
return true;
112+
}
113+
else
114+
{
115+
command = default;
116+
return false;
117+
}
118+
}
119+
else
120+
{
121+
var methodText = s.Substring(0, index);
122+
var uriText = s.Substring(index + 1);
123+
124+
return TryParse(methodText, uriText, out command);
125+
}
126+
}
127+
}
128+
}

src/https/Content.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace Https
2+
{
3+
class Content
4+
{
5+
public ContentLocation ContentLocation { get; }
6+
public string Property { get; }
7+
public string Value { get; }
8+
Content(ContentLocation contentLocation, string property, string value)
9+
{
10+
ContentLocation = contentLocation;
11+
Property = property;
12+
Value = value;
13+
}
14+
15+
public static bool TryParse(string s, out Content content)
16+
{
17+
var equalsIndex = s.IndexOf('=');
18+
var colonIndex = s.IndexOf(':');
19+
if (equalsIndex == -1 && colonIndex == -1)
20+
{
21+
content = default;
22+
return false;
23+
}
24+
25+
var contentType = default(ContentLocation);
26+
var index = default(int);
27+
if (equalsIndex > -1 && colonIndex > -1)
28+
{
29+
if (equalsIndex < colonIndex)
30+
{
31+
contentType = ContentLocation.Body;
32+
index = equalsIndex;
33+
}
34+
else
35+
{
36+
contentType = ContentLocation.Header;
37+
index = colonIndex;
38+
}
39+
}
40+
else if (equalsIndex > -1)
41+
{
42+
contentType = ContentLocation.Body;
43+
index = equalsIndex;
44+
}
45+
else
46+
{
47+
contentType = ContentLocation.Header;
48+
index = colonIndex;
49+
}
50+
51+
var property = s.Substring(0, index);
52+
if (property.Length == 0)
53+
{
54+
content = default;
55+
return false;
56+
}
57+
58+
var value = s.Substring(index + 1);
59+
content = new Content(contentType, property, value);
60+
return true;
61+
}
62+
}
63+
}

src/https/ContentLocation.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Https
2+
{
3+
enum ContentLocation
4+
{
5+
Body = 1,
6+
Header = 2
7+
}
8+
}

src/https/ContentType.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Https
2+
{
3+
enum ContentType
4+
{
5+
Json = 1,
6+
FormUrlEncoded = 2,
7+
Xml = 3
8+
}
9+
}

src/https/Options.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Https
5+
{
6+
class Options
7+
{
8+
public ContentType RequestContentType { get; }
9+
public string XmlRootName { get; }
10+
public bool IgnoreCertificate { get; }
11+
public TimeSpan? Timeout { get; }
12+
public bool Version { get; }
13+
public bool Help { get; }
14+
public bool StopAutoRedirects { get; }
15+
16+
public bool RequiresHandler => IgnoreCertificate || StopAutoRedirects;
17+
18+
public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate, TimeSpan? timeout, bool version, bool help, bool stopAutoRedirects)
19+
{
20+
RequestContentType = requestContentType;
21+
XmlRootName = xmlRootName;
22+
IgnoreCertificate = ignoreCertificate;
23+
Timeout = timeout;
24+
Version = version;
25+
Help = help;
26+
StopAutoRedirects = stopAutoRedirects;
27+
}
28+
29+
public static IEnumerable<string> GetOptionHelp()
30+
{
31+
yield return "--form Renders the content arguments as application/x-www-form-urlencoded";
32+
yield return "--help Show command line help.";
33+
yield return "--ignore-certificate Prevents server certificate validation.";
34+
yield return "--json Renders the content arguments as application/json.";
35+
yield return "--timeout=<VALUE> Sets the timeout of the request using System.TimeSpan.TryParse (https://docs.microsoft.com/en-us/dotnet/api/system.timespan.parse)";
36+
yield return "--version Displays the application verison.";
37+
yield return "--xml=<ROOT_NAME> Renders the content arguments as application/xml using the optional xml root name.";
38+
yield return "--stop-auto-redirects Prevents redirects from automatically being processed.";
39+
}
40+
41+
static int GetArgValueIndex(string arg)
42+
{
43+
var equalsIndex = arg.IndexOf('=');
44+
var spaceIndex = arg.IndexOf(' ');
45+
var index = equalsIndex > -1 && spaceIndex > -1
46+
? Math.Min(equalsIndex, spaceIndex)
47+
: Math.Max(equalsIndex, spaceIndex);
48+
49+
return index == -1 ? index : index + 1;
50+
}
51+
52+
public static Options Parse(IEnumerable<string> args)
53+
{
54+
var requestContentType = ContentType.Json;
55+
var xmlRootName = default(string);
56+
var ignoreCertificate = false;
57+
var timeout = default(TimeSpan?);
58+
var help = false;
59+
var version = false;
60+
var stopAutoRedirects = false;
61+
foreach (var arg in args)
62+
{
63+
if (arg.StartsWith("--json"))
64+
{
65+
requestContentType = ContentType.Json;
66+
}
67+
else if (arg.StartsWith("--xml"))
68+
{
69+
var index = GetArgValueIndex(arg);
70+
if (index == -1)
71+
{
72+
xmlRootName = "xml";
73+
}
74+
else
75+
{
76+
xmlRootName = arg.Substring(index).Trim();
77+
if (string.IsNullOrEmpty(xmlRootName))
78+
{
79+
xmlRootName = "xml";
80+
}
81+
}
82+
requestContentType = ContentType.Xml;
83+
}
84+
else if (arg.StartsWith("--form"))
85+
{
86+
requestContentType = ContentType.FormUrlEncoded;
87+
}
88+
else if (arg.StartsWith("--ignore-certificate"))
89+
{
90+
ignoreCertificate = true;
91+
}
92+
else if (arg.StartsWith("--timeout"))
93+
{
94+
var index = GetArgValueIndex(arg);
95+
if (index > -1)
96+
{
97+
var s = arg.Substring(index).Trim();
98+
if (TimeSpan.TryParse(s, out var to) && to > TimeSpan.Zero)
99+
{
100+
timeout = to;
101+
}
102+
}
103+
}
104+
else if (arg.StartsWith("--version"))
105+
{
106+
version = true;
107+
}
108+
else if (arg.StartsWith("--help") || arg.StartsWith("-?") || arg.StartsWith("help"))
109+
{
110+
help = true;
111+
}
112+
else if (arg.StartsWith("--stop-auto-redirects"))
113+
{
114+
stopAutoRedirects = true;
115+
}
116+
}
117+
return new Options(requestContentType, xmlRootName, ignoreCertificate, timeout, version, help, stopAutoRedirects);
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)