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

Commit ee06690

Browse files
committed
Reimplement HttpUtils to use HttpClient in net6.0
1 parent 73fedf1 commit ee06690

13 files changed

+1216
-194
lines changed

src/ServiceStack.Text/Env.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ static Env()
107107
+ VersionString + " "
108108
+ PclExport.Instance.PlatformName
109109
+ (IsLinux ? "/Linux" : IsOSX ? "/OSX" : IsUnix ? "/Unix" : IsWindows ? "/Windows" : "/UnknownOS") + (IsIOS ? "/iOS" : IsAndroid ? "/Android" : IsUWP ? "/UWP" : "")
110-
+ PlatformModifier
110+
+ (IsNet6 ? "/net6" : IsNetStandard20 ? "/std2.0" : IsNetFramework ? "/netfx" : "") + (IsMono ? "/Mono" : "")
111111
+ $"/{LicenseUtils.Info}";
112112
__releaseDate = new DateTime(2001,01,01);
113113
}
114114

115-
public static string PlatformModifier => (IsNet6 ? "/net6" : IsNetStandard20 ? "/std2.0" : IsNetFramework ? "/netfx" : "") + (IsMono ? "/Mono" : "");
116-
117115
public static string VersionString { get; set; }
118116

119117
public static decimal ServiceStackVersion = 6.01m;

src/ServiceStack.Text/HttpUtils.HttpClient.cs

Lines changed: 957 additions & 0 deletions
Large diffs are not rendered by default.

src/ServiceStack.Text/HttpUtils.Legacy.cs renamed to src/ServiceStack.Text/HttpUtils.WebRequest.cs

Lines changed: 24 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#if !NET6_0_OR_GREATER
12
using System;
23
using System.Collections.Generic;
34
using System.IO;
@@ -773,54 +774,6 @@ public static async Task<Stream> SendStreamToUrlAsync(this string url, string me
773774
return stream;
774775
}
775776

776-
public static bool IsAny300(this Exception ex)
777-
{
778-
var status = ex.GetStatus();
779-
return status >= HttpStatusCode.MultipleChoices && status < HttpStatusCode.BadRequest;
780-
}
781-
782-
public static bool IsAny400(this Exception ex)
783-
{
784-
var status = ex.GetStatus();
785-
return status >= HttpStatusCode.BadRequest && status < HttpStatusCode.InternalServerError;
786-
}
787-
788-
public static bool IsAny500(this Exception ex)
789-
{
790-
var status = ex.GetStatus();
791-
return status >= HttpStatusCode.InternalServerError && (int)status < 600;
792-
}
793-
794-
public static bool IsNotModified(this Exception ex)
795-
{
796-
return GetStatus(ex) == HttpStatusCode.NotModified;
797-
}
798-
799-
public static bool IsBadRequest(this Exception ex)
800-
{
801-
return GetStatus(ex) == HttpStatusCode.BadRequest;
802-
}
803-
804-
public static bool IsNotFound(this Exception ex)
805-
{
806-
return GetStatus(ex) == HttpStatusCode.NotFound;
807-
}
808-
809-
public static bool IsUnauthorized(this Exception ex)
810-
{
811-
return GetStatus(ex) == HttpStatusCode.Unauthorized;
812-
}
813-
814-
public static bool IsForbidden(this Exception ex)
815-
{
816-
return GetStatus(ex) == HttpStatusCode.Forbidden;
817-
}
818-
819-
public static bool IsInternalServerError(this Exception ex)
820-
{
821-
return GetStatus(ex) == HttpStatusCode.InternalServerError;
822-
}
823-
824777
public static HttpStatusCode? GetResponseStatus(this string url)
825778
{
826779
try
@@ -838,74 +791,6 @@ public static bool IsInternalServerError(this Exception ex)
838791
}
839792
}
840793

841-
public static HttpStatusCode? GetStatus(this Exception ex)
842-
{
843-
if (ex == null)
844-
return null;
845-
846-
if (ex is WebException webEx)
847-
return GetStatus(webEx);
848-
849-
if (ex is IHasStatusCode hasStatus)
850-
return (HttpStatusCode)hasStatus.StatusCode;
851-
852-
return null;
853-
}
854-
855-
public static HttpStatusCode? GetStatus(this WebException webEx)
856-
{
857-
var httpRes = webEx?.Response as HttpWebResponse;
858-
return httpRes?.StatusCode;
859-
}
860-
861-
public static bool HasStatus(this Exception ex, HttpStatusCode statusCode)
862-
{
863-
return GetStatus(ex) == statusCode;
864-
}
865-
866-
public static string GetResponseBody(this Exception ex)
867-
{
868-
if (!(ex is WebException webEx) || webEx.Response == null || webEx.Status != WebExceptionStatus.ProtocolError)
869-
return null;
870-
871-
var errorResponse = (HttpWebResponse)webEx.Response;
872-
using var responseStream = errorResponse.GetResponseStream();
873-
return responseStream.ReadToEnd(UseEncoding);
874-
}
875-
876-
public static async Task<string> GetResponseBodyAsync(this Exception ex, CancellationToken token = default)
877-
{
878-
if (!(ex is WebException webEx) || webEx.Response == null || webEx.Status != WebExceptionStatus.ProtocolError)
879-
return null;
880-
881-
var errorResponse = (HttpWebResponse)webEx.Response;
882-
using var responseStream = errorResponse.GetResponseStream();
883-
return await responseStream.ReadToEndAsync(UseEncoding).ConfigAwait();
884-
}
885-
886-
public static string ReadToEnd(this WebResponse webRes)
887-
{
888-
using var stream = webRes.GetResponseStream();
889-
return stream.ReadToEnd(UseEncoding);
890-
}
891-
892-
public static Task<string> ReadToEndAsync(this WebResponse webRes)
893-
{
894-
using var stream = webRes.GetResponseStream();
895-
return stream.ReadToEndAsync(UseEncoding);
896-
}
897-
898-
public static IEnumerable<string> ReadLines(this WebResponse webRes)
899-
{
900-
using var stream = webRes.GetResponseStream();
901-
using var reader = new StreamReader(stream, UseEncoding, true, 1024, leaveOpen: true);
902-
string line;
903-
while ((line = reader.ReadLine()) != null)
904-
{
905-
yield return line;
906-
}
907-
}
908-
909794
public static HttpWebResponse GetErrorResponse(this string url)
910795
{
911796
try
@@ -1187,8 +1072,29 @@ private static byte[] GetHeaderBytes(string fileName, string mimeType, string fi
11871072
var headerBytes = header.ToAsciiBytes();
11881073
return headerBytes;
11891074
}
1190-
}
11911075

1076+
public static HttpWebRequest With(this HttpWebRequest httpReq,
1077+
string accept = null,
1078+
string userAgent = null,
1079+
Dictionary<string,string> headers = null)
1080+
{
1081+
if (accept != null)
1082+
httpReq.Headers.Add(HttpHeaders.Accept, accept);
1083+
1084+
if (userAgent != null)
1085+
httpReq.UserAgent = userAgent;
1086+
1087+
if (headers != null)
1088+
{
1089+
foreach (var entry in headers)
1090+
{
1091+
httpReq.Headers[entry.Key] = entry.Value;
1092+
}
1093+
}
1094+
1095+
return httpReq;
1096+
}
1097+
}
11921098

11931099
public interface IHttpResultsFilter : IDisposable
11941100
{
@@ -1241,3 +1147,4 @@ public void UploadStream(HttpWebRequest webRequest, Stream fileStream, string fi
12411147
UploadFileFn?.Invoke(webRequest, fileStream, fileName);
12421148
}
12431149
}
1150+
#endif

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,31 @@
22
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Net;
78
using System.Text;
9+
using System.Threading;
810
using System.Threading.Tasks;
11+
using ServiceStack.Text;
912

1013
namespace ServiceStack;
1114

1215
public static partial class HttpUtils
1316
{
14-
public static string UserAgent = "ServiceStack.Text" + Text.Env.PlatformModifier;
17+
public static string UserAgent = "ServiceStack.Text" +
18+
#if NET6_0_OR_GREATER
19+
"/net6"
20+
#elif NETSTANDARD2_0
21+
"/std2.0"
22+
#elif NETFX
23+
"/net472"
24+
#else
25+
"/unknown"
26+
#endif
27+
;
1528

16-
public static Encoding UseEncoding { get; set; } = PclExport.Instance.GetUTF8Encoding(false);
29+
public static Encoding UseEncoding { get; set; } = new UTF8Encoding(false);
1730

1831
public static string AddQueryParam(this string url, string key, object val, bool encode = true)
1932
{
@@ -208,6 +221,128 @@ public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request
208221

209222
return tcs.Task;
210223
}
224+
225+
public static bool IsAny300(this Exception ex)
226+
{
227+
var status = ex.GetStatus();
228+
return status is >= HttpStatusCode.MultipleChoices and < HttpStatusCode.BadRequest;
229+
}
230+
231+
public static bool IsAny400(this Exception ex)
232+
{
233+
var status = ex.GetStatus();
234+
return status is >= HttpStatusCode.BadRequest and < HttpStatusCode.InternalServerError;
235+
}
236+
237+
public static bool IsAny500(this Exception ex)
238+
{
239+
var status = ex.GetStatus();
240+
return status >= HttpStatusCode.InternalServerError && (int)status < 600;
241+
}
242+
243+
public static bool IsNotModified(this Exception ex)
244+
{
245+
return GetStatus(ex) == HttpStatusCode.NotModified;
246+
}
247+
248+
public static bool IsBadRequest(this Exception ex)
249+
{
250+
return GetStatus(ex) == HttpStatusCode.BadRequest;
251+
}
252+
253+
public static bool IsNotFound(this Exception ex)
254+
{
255+
return GetStatus(ex) == HttpStatusCode.NotFound;
256+
}
257+
258+
public static bool IsUnauthorized(this Exception ex)
259+
{
260+
return GetStatus(ex) == HttpStatusCode.Unauthorized;
261+
}
262+
263+
public static bool IsForbidden(this Exception ex)
264+
{
265+
return GetStatus(ex) == HttpStatusCode.Forbidden;
266+
}
267+
268+
public static bool IsInternalServerError(this Exception ex)
269+
{
270+
return GetStatus(ex) == HttpStatusCode.InternalServerError;
271+
}
272+
273+
public static HttpStatusCode? GetStatus(this Exception ex)
274+
{
275+
#if NET6_0_OR_GREATER
276+
if (ex is System.Net.Http.HttpRequestException httpEx)
277+
return GetStatus(httpEx);
278+
#endif
279+
280+
if (ex is WebException webEx)
281+
return GetStatus(webEx);
282+
283+
if (ex is IHasStatusCode hasStatus)
284+
return (HttpStatusCode)hasStatus.StatusCode;
285+
286+
return null;
287+
}
288+
289+
#if NET6_0_OR_GREATER
290+
public static HttpStatusCode? GetStatus(this System.Net.Http.HttpRequestException ex) => ex.StatusCode;
291+
#endif
292+
293+
public static HttpStatusCode? GetStatus(this WebException webEx)
294+
{
295+
var httpRes = webEx?.Response as HttpWebResponse;
296+
return httpRes?.StatusCode;
297+
}
298+
299+
public static bool HasStatus(this Exception ex, HttpStatusCode statusCode)
300+
{
301+
return GetStatus(ex) == statusCode;
302+
}
303+
304+
public static string GetResponseBody(this Exception ex)
305+
{
306+
if (!(ex is WebException webEx) || webEx.Response == null || webEx.Status != WebExceptionStatus.ProtocolError)
307+
return null;
308+
309+
var errorResponse = (HttpWebResponse)webEx.Response;
310+
using var responseStream = errorResponse.GetResponseStream();
311+
return responseStream.ReadToEnd(UseEncoding);
312+
}
313+
314+
public static async Task<string> GetResponseBodyAsync(this Exception ex, CancellationToken token = default)
315+
{
316+
if (!(ex is WebException webEx) || webEx.Response == null || webEx.Status != WebExceptionStatus.ProtocolError)
317+
return null;
318+
319+
var errorResponse = (HttpWebResponse)webEx.Response;
320+
using var responseStream = errorResponse.GetResponseStream();
321+
return await responseStream.ReadToEndAsync(UseEncoding).ConfigAwait();
322+
}
323+
324+
public static string ReadToEnd(this WebResponse webRes)
325+
{
326+
using var stream = webRes.GetResponseStream();
327+
return stream.ReadToEnd(UseEncoding);
328+
}
329+
330+
public static Task<string> ReadToEndAsync(this WebResponse webRes)
331+
{
332+
using var stream = webRes.GetResponseStream();
333+
return stream.ReadToEndAsync(UseEncoding);
334+
}
335+
336+
public static IEnumerable<string> ReadLines(this WebResponse webRes)
337+
{
338+
using var stream = webRes.GetResponseStream();
339+
using var reader = new StreamReader(stream, UseEncoding, true, 1024, leaveOpen: true);
340+
string line;
341+
while ((line = reader.ReadLine()) != null)
342+
{
343+
yield return line;
344+
}
345+
}
211346
}
212347

213348
//Allow Exceptions to Customize HTTP StatusCode and StatusDescription returned

src/ServiceStack.Text/LicenseUtils.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,8 @@ public static bool VerifyLicenseKeyTextFallback(this string licenseKeyText, out
821821

822822
public static bool VerifySha1Data(this System.Security.Cryptography.RSACryptoServiceProvider RSAalg, byte[] unsignedData, byte[] encryptedData)
823823
{
824-
using (var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider())
825-
{
826-
return RSAalg.VerifyData(unsignedData, sha, encryptedData);
827-
}
824+
using var sha = System.Security.Cryptography.SHA1.Create();
825+
return RSAalg.VerifyData(unsignedData, sha, encryptedData);
828826
}
829827
}
830828
}

src/ServiceStack.Text/PclExport.NetCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if NETCORE && !NETSTANDARD2_0
1+
#if (NETCORE || NET6_0_OR_GREATER) && !NETSTANDARD2_0
22

33
using System;
44
using ServiceStack.Text;

src/ServiceStack.Text/PclExport.NetStandard.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ namespace ServiceStack
1919
{
2020
public class NetStandardPclExport : PclExport
2121
{
22-
public static NetStandardPclExport Provider = new NetStandardPclExport();
22+
public static NetStandardPclExport Provider = new();
2323

24-
static string[] allDateTimeFormats = new string[]
25-
{
24+
static string[] allDateTimeFormats = {
2625
"yyyy-MM-ddTHH:mm:ss.FFFFFFFzzzzzz",
2726
"yyyy-MM-ddTHH:mm:ss.FFFFFFF",
2827
"yyyy-MM-ddTHH:mm:ss.FFFFFFFZ",

0 commit comments

Comments
 (0)