-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCurlProcessArgumentsBuilder.cs
More file actions
205 lines (128 loc) · 5.76 KB
/
CurlProcessArgumentsBuilder.cs
File metadata and controls
205 lines (128 loc) · 5.76 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Gsemac.Core;
using Gsemac.Net.Extensions;
using Gsemac.Net.Http;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Threading;
namespace Gsemac.Net.Curl {
public class CurlProcessArgumentsBuilder :
ProcessArgumentsBuilderBase<CurlProcessArgumentsBuilder> {
// Public members
public CurlProcessArgumentsBuilder WithAutomaticDecompression(DecompressionMethods decompressionMethods) {
if (decompressionMethods != DecompressionMethods.None) {
WithArgument("--compressed");
string headerValue = HttpUtilities.GetAcceptEncodingString(decompressionMethods);
if (!string.IsNullOrEmpty(headerValue))
WithArgument("--header", $"Accept-Encoding: {headerValue}");
}
return this;
}
public CurlProcessArgumentsBuilder WithAutomaticRedirect(int maxRedirections) {
if (maxRedirections > 0) {
WithArgument("--location");
WithArgument("--max-redirs", maxRedirections.ToString(CultureInfo.InvariantCulture));
}
return this;
}
public CurlProcessArgumentsBuilder WithCertificateValidation(bool certificateValidationEnabled) {
if (!certificateValidationEnabled)
WithArgument("--insecure");
return this;
}
public CurlProcessArgumentsBuilder WithCookies(CookieContainer cookieContainer, Uri requestUri = null) {
if (requestUri is null)
requestUri = uri;
if (cookieContainer is object) {
List<string> cookieStrings = new List<string>();
foreach (Cookie cookie in cookieContainer.GetCookies(requestUri))
cookieStrings.Add(cookie.ToString());
if (cookieStrings.Count > 0)
WithArgument("--cookie", string.Join(";", cookieStrings));
}
return this;
}
public CurlProcessArgumentsBuilder WithConnectTimeout(int timeoutSeconds) {
if (timeoutSeconds != Timeout.Infinite)
WithArgument("--connect-timeout", (timeoutSeconds / 1000.0).ToString(CultureInfo.InvariantCulture));
return this;
}
public CurlProcessArgumentsBuilder WithConsoleOutput() => WithArgument("--output", "-"); // output binary data directly to standard output
public CurlProcessArgumentsBuilder WithCredentials(ICredentials credentials, Uri requestUri = null) {
// Argument should be of the following form:
// -u username:password
if (requestUri is null)
requestUri = uri;
if (!(credentials is null))
WithArgument("-u", credentials.ToCredentialsString(requestUri));
return this;
}
public CurlProcessArgumentsBuilder WithHeaderOutput() => WithArgument("--include"); // include response headers in output
public CurlProcessArgumentsBuilder WithHeaders(WebHeaderCollection headers) {
for (int i = 0; i < headers.Count; ++i) {
string header = headers.GetKey(i);
foreach (string value in headers.GetValues(i))
WithArgument("--header", $"{header}: {headers[header]}");
}
return this;
}
public CurlProcessArgumentsBuilder WithHttpVersion(System.Version version) {
if (version != null) {
if (version.Major > 1)
WithArgument(string.Format("--http{0}", version.Major));
else
WithArgument(string.Format("--http{0}.{1}", version.Major, version.Minor));
}
return this;
}
public CurlProcessArgumentsBuilder WithKeepAlive(bool keepAlive) {
if (!keepAlive)
WithArgument("--no-keepalive");
return this;
}
public CurlProcessArgumentsBuilder WithMethod(string method) {
if (string.IsNullOrWhiteSpace(method))
method = "GET";
switch (method.ToLowerInvariant()) {
case "get": // no action required
break;
case "head": // make HEAD request + redirect stderr to stdout so we can read it via the ProcessStream
WithArgument("--head");
WithArgument("--stderr", "-");
break;
case "post":
WithArgument("-X", "POST");
break;
case "put":
WithArgument("-X", "PUT");
break;
default:
WithArgument("-X", method);
break;
}
return this;
}
public CurlProcessArgumentsBuilder WithPostData(string postData) {
if (!string.IsNullOrWhiteSpace(postData))
WithArgument("--data", postData);
return this;
}
public CurlProcessArgumentsBuilder WithProxy(IWebProxy proxy, Uri requestUri = null) {
// Argument should be of the following form:
// -x, --proxy <[protocol://][user:password@]proxyhost[:port]>
if (requestUri is null)
requestUri = uri;
if (!(proxy is null) && !proxy.IsBypassed(requestUri))
WithArgument("--proxy", proxy.ToProxyString(requestUri));
return this;
}
public CurlProcessArgumentsBuilder WithUri(Uri uri) {
this.uri = uri;
WithArgument(uri.AbsoluteUri);
return this;
}
// Private members
private Uri uri = new Uri("http://example.com");
}
}