Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2d7eacc

Browse files
committed
refactor HttpRequestConfig to use typed records
1 parent 8c6929d commit 2d7eacc

File tree

4 files changed

+81
-26
lines changed

4 files changed

+81
-26
lines changed

src/ServiceStack.Text/HttpRequestConfig.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,54 @@ public class HttpRequestConfig
1111
public string? Accept { get; set; }
1212
public string? UserAgent { get; set; }
1313
public string? ContentType { get; set; }
14-
public KeyValuePair<string,string>? Authorization { get; set; }
15-
public Dictionary<string, string>? Headers { get; set; }
14+
public HttpHeader? Authorization { get; set; }
15+
public RangeHeader? Range { get; set; }
16+
public List<HttpHeader> Headers { get; set; } = new();
1617

1718
public string AuthBearer
1819
{
1920
set => Authorization = new("Bearer", value);
2021
}
2122

22-
public KeyValuePair<string, string> AuthBasic
23+
public HttpHeader AuthBasic
2324
{
2425
set => Authorization = new("Basic",
25-
Convert.ToBase64String(Encoding.UTF8.GetBytes(value.Key + ":" + value.Value)));
26+
Convert.ToBase64String(Encoding.UTF8.GetBytes(value.Name + ":" + value.Value)));
27+
}
28+
}
29+
30+
public record HttpHeader
31+
{
32+
public HttpHeader(string name, string value)
33+
{
34+
this.Name = name;
35+
this.Value = value;
36+
}
37+
38+
public string Name { get; }
39+
public string Value { get; }
40+
41+
public void Deconstruct(out string name, out string value)
42+
{
43+
name = this.Name;
44+
value = this.Value;
45+
}
46+
}
47+
48+
public record RangeHeader
49+
{
50+
public RangeHeader(long from, long? to = null)
51+
{
52+
this.From = from;
53+
this.To = to;
54+
}
55+
56+
public long From { get; }
57+
public long? To { get; }
58+
59+
public void Deconstruct(out long from, out long? to)
60+
{
61+
from = this.From;
62+
to = this.To;
2663
}
2764
}

src/ServiceStack.Text/HttpUtils.HttpClient.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,35 +1089,36 @@ public static HttpRequestMessage With(this HttpRequestMessage httpReq, Action<Ht
10891089
var config = new HttpRequestConfig();
10901090
configure(config);
10911091

1092-
config.Headers ??= new Dictionary<string, string>();
10931092
var headers = config.Headers;
10941093

10951094
if (config.Accept != null)
1096-
headers[HttpHeaders.Accept] = config.Accept;
1095+
headers.Add(new(HttpHeaders.Accept, config.Accept));
10971096
if (config.UserAgent != null)
1098-
headers[HttpHeaders.UserAgent] = config.UserAgent;
1097+
headers.Add(new(HttpHeaders.UserAgent, config.UserAgent));
10991098
if (config.ContentType != null)
1100-
headers[HttpHeaders.ContentType] = config.ContentType;
1099+
headers.Add(new(HttpHeaders.ContentType, config.ContentType));
11011100
if (config.Authorization != null)
11021101
httpReq.Headers.Authorization =
1103-
new AuthenticationHeaderValue(config.Authorization.Value.Key, config.Authorization.Value.Value);
1102+
new AuthenticationHeaderValue(config.Authorization.Name, config.Authorization.Value);
1103+
if (config.Range != null)
1104+
httpReq.Headers.Range = new RangeHeaderValue(config.Range.From, config.Range.To);
11041105

11051106
foreach (var entry in headers)
11061107
{
1107-
httpReq.WithHeader(entry.Key, entry.Value);
1108+
httpReq.WithHeader(entry.Name, entry.Value);
11081109
}
11091110

11101111
return httpReq;
11111112
}
11121113

11131114
public static void DownloadFileTo(this string downloadUrl, string fileName,
1114-
Dictionary<string,string>? headers = null)
1115+
List<HttpHeader>? headers = null)
11151116
{
11161117
var client = Create();
11171118
var httpReq = new HttpRequestMessage(HttpMethod.Get, downloadUrl)
11181119
.With(c => {
11191120
c.Accept = "*/*";
1120-
c.Headers = headers;
1121+
if (headers != null) c.Headers = headers;
11211122
});
11221123

11231124
var httpRes = client.Send(httpReq);

src/ServiceStack.Text/HttpUtils.WebRequest.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,18 +1074,40 @@ private static byte[] GetHeaderBytes(string fileName, string mimeType, string fi
10741074
}
10751075

10761076
public static void DownloadFileTo(this string downloadUrl, string fileName,
1077-
Dictionary<string,string> headers = null)
1077+
List<HttpHeader> headers = null)
10781078
{
10791079
var webClient = new WebClient();
10801080
if (headers != null)
10811081
{
1082-
foreach (var entry in headers)
1082+
foreach (var header in headers)
10831083
{
1084-
webClient.Headers[entry.Key] = entry.Value;
1084+
webClient.Headers[header.Name] = header.Value;
10851085
}
10861086
}
10871087
webClient.DownloadFile(downloadUrl, fileName);
10881088
}
1089+
1090+
public static void SetRange(this HttpWebRequest request, long from, long? to)
1091+
{
1092+
var rangeSpecifier = "bytes";
1093+
var curRange = request.Headers[HttpRequestHeader.Range];
1094+
1095+
if (string.IsNullOrEmpty(curRange))
1096+
{
1097+
curRange = rangeSpecifier + "=";
1098+
}
1099+
else
1100+
{
1101+
if (string.Compare(curRange.Substring(0, curRange.IndexOf('=')), rangeSpecifier, StringComparison.OrdinalIgnoreCase) != 0)
1102+
throw new NotSupportedException("Invalid Range: " + curRange);
1103+
curRange = string.Empty;
1104+
}
1105+
curRange += from.ToString();
1106+
if (to != null) {
1107+
curRange += "-" + to;
1108+
}
1109+
request.Headers[HttpRequestHeader.Range] = curRange;
1110+
}
10891111

10901112
public static void AddHeader(this HttpWebRequest res, string name, string value) =>
10911113
res.Headers[name] = value;
@@ -1120,14 +1142,14 @@ public static HttpWebRequest With(this HttpWebRequest httpReq, Action<HttpReques
11201142

11211143
if (config.Authorization != null)
11221144
httpReq.Headers[HttpHeaders.Authorization] =
1123-
config.Authorization.Value.Key + " " + config.Authorization.Value.Value;
1145+
config.Authorization.Name + " " + config.Authorization.Value;
1146+
1147+
if (config.Range != null)
1148+
httpReq.SetRange(config.Range.From, config.Range.To);
11241149

1125-
if (config.Headers != null)
1150+
foreach (var entry in config.Headers)
11261151
{
1127-
foreach (var entry in config.Headers)
1128-
{
1129-
httpReq.Headers[entry.Key] = entry.Value;
1130-
}
1152+
httpReq.Headers[entry.Name] = entry.Value;
11311153
}
11321154

11331155
return httpReq;

src/ServiceStack.Text/PclExport.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,6 @@ public virtual void WriteLine(string line, params object[] args)
142142
{
143143
}
144144

145-
public virtual HttpWebRequest CreateWebRequest(string requestUri, bool? emulateHttpViaPost = null)
146-
{
147-
return (HttpWebRequest)WebRequest.Create(requestUri);
148-
}
149-
150145
public virtual void Config(HttpWebRequest req,
151146
bool? allowAutoRedirect = null,
152147
TimeSpan? timeout = null,

0 commit comments

Comments
 (0)