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

Commit 6f46ce0

Browse files
committed
Tidy + add some missing async overloads
1 parent e0ea39e commit 6f46ce0

File tree

3 files changed

+75
-82
lines changed

3 files changed

+75
-82
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -549,23 +549,19 @@ public static string SendStringToUrl(this string url, string method = null,
549549

550550
if (requestBody != null)
551551
{
552-
using (var reqStream = PclExport.Instance.GetRequestStream(webReq))
553-
using (var writer = new StreamWriter(reqStream, UseEncoding))
554-
{
555-
writer.Write(requestBody);
556-
}
552+
using var reqStream = PclExport.Instance.GetRequestStream(webReq);
553+
using var writer = new StreamWriter(reqStream, UseEncoding);
554+
writer.Write(requestBody);
557555
}
558556
else if (method != null && HasRequestBody(method))
559557
{
560558
webReq.ContentLength = 0;
561559
}
562560

563-
using (var webRes = PclExport.Instance.GetResponse(webReq))
564-
using (var stream = webRes.GetResponseStream())
565-
{
566-
responseFilter?.Invoke((HttpWebResponse)webRes);
567-
return stream.ReadToEnd(UseEncoding);
568-
}
561+
using var webRes = webReq.GetResponse();
562+
using var stream = webRes.GetResponseStream();
563+
responseFilter?.Invoke((HttpWebResponse)webRes);
564+
return stream.ReadToEnd(UseEncoding);
569565
}
570566

571567
public static async Task<string> SendStringToUrlAsync(this string url, string method = null, string requestBody = null,
@@ -591,21 +587,15 @@ public static async Task<string> SendStringToUrlAsync(this string url, string me
591587

592588
if (requestBody != null)
593589
{
594-
using (var reqStream = PclExport.Instance.GetRequestStream(webReq))
595-
using (var writer = new StreamWriter(reqStream, UseEncoding))
596-
{
597-
writer.Write(requestBody);
598-
}
590+
using var reqStream = PclExport.Instance.GetRequestStream(webReq);
591+
using var writer = new StreamWriter(reqStream, UseEncoding);
592+
await writer.WriteAsync(requestBody);
599593
}
600594

601-
using (var webRes = await webReq.GetResponseAsync().ConfigAwait())
602-
{
603-
responseFilter?.Invoke((HttpWebResponse)webRes);
604-
using (var stream = webRes.GetResponseStream())
605-
{
606-
return await stream.ReadToEndAsync().ConfigAwait();
607-
}
608-
}
595+
using var webRes = await webReq.GetResponseAsync().ConfigAwait();
596+
responseFilter?.Invoke((HttpWebResponse)webRes);
597+
using var stream = webRes.GetResponseStream();
598+
return await stream.ReadToEndAsync().ConfigAwait();
609599
}
610600

611601
public static byte[] GetBytesFromUrl(this string url, string accept = "*/*",
@@ -950,22 +940,24 @@ public static string GetResponseBody(this Exception ex)
950940

951941
public static string ReadToEnd(this WebResponse webRes)
952942
{
953-
using (var stream = webRes.GetResponseStream())
954-
{
955-
return stream.ReadToEnd(UseEncoding);
956-
}
943+
using var stream = webRes.GetResponseStream();
944+
return stream.ReadToEnd(UseEncoding);
945+
}
946+
947+
public static Task<string> ReadToEndAsync(this WebResponse webRes)
948+
{
949+
using var stream = webRes.GetResponseStream();
950+
return stream.ReadToEndAsync(UseEncoding);
957951
}
958952

959953
public static IEnumerable<string> ReadLines(this WebResponse webRes)
960954
{
961-
using (var stream = webRes.GetResponseStream())
962-
using (var reader = new StreamReader(stream, UseEncoding, true, 1024, leaveOpen:true))
955+
using var stream = webRes.GetResponseStream();
956+
using var reader = new StreamReader(stream, UseEncoding, true, 1024, leaveOpen:true);
957+
string line;
958+
while ((line = reader.ReadLine()) != null)
963959
{
964-
string line;
965-
while ((line = reader.ReadLine()) != null)
966-
{
967-
yield return line;
968-
}
960+
yield return line;
969961
}
970962
}
971963

@@ -974,11 +966,24 @@ public static HttpWebResponse GetErrorResponse(this string url)
974966
try
975967
{
976968
var webReq = WebRequest.Create(url);
977-
using (var webRes = PclExport.Instance.GetResponse(webReq))
978-
{
979-
webRes.ReadToEnd();
980-
return null;
981-
}
969+
using var webRes = PclExport.Instance.GetResponse(webReq);
970+
webRes.ReadToEnd();
971+
return null;
972+
}
973+
catch (WebException webEx)
974+
{
975+
return (HttpWebResponse)webEx.Response;
976+
}
977+
}
978+
979+
public static async Task<HttpWebResponse> GetErrorResponseAsync(this string url)
980+
{
981+
try
982+
{
983+
var webReq = WebRequest.Create(url);
984+
using var webRes = await webReq.GetResponseAsync();
985+
await webRes.ReadToEndAsync();
986+
return null;
982987
}
983988
catch (WebException webEx)
984989
{
@@ -1091,16 +1096,11 @@ public static void UploadFile(this WebRequest webRequest, Stream fileStream, str
10911096
return;
10921097
}
10931098

1094-
using (var outputStream = PclExport.Instance.GetRequestStream(httpReq))
1095-
{
1096-
outputStream.Write(headerBytes, 0, headerBytes.Length);
1097-
1098-
fileStream.CopyTo(outputStream, 4096);
1099-
1100-
outputStream.Write(boundaryBytes, 0, boundaryBytes.Length);
1101-
1102-
PclExport.Instance.CloseStream(outputStream);
1103-
}
1099+
using var outputStream = PclExport.Instance.GetRequestStream(httpReq);
1100+
outputStream.Write(headerBytes, 0, headerBytes.Length);
1101+
fileStream.CopyTo(outputStream, 4096);
1102+
outputStream.Write(boundaryBytes, 0, boundaryBytes.Length);
1103+
PclExport.Instance.CloseStream(outputStream);
11041104
}
11051105

11061106
public static void UploadFile(this WebRequest webRequest, Stream fileStream, string fileName)
@@ -1269,7 +1269,7 @@ public static string GetExtension(string mimeType)
12691269
throw new NotSupportedException("Unknown mimeType: " + mimeType);
12701270
}
12711271

1272-
//lowercases and trims left part of content-type prior ';'
1272+
//Lower cases and trims left part of content-type prior ';'
12731273
public static string GetRealContentType(string contentType)
12741274
{
12751275
if (contentType == null)

src/ServiceStack.Text/PclExport.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public virtual WebResponse GetResponse(WebRequest webRequest)
181181
}
182182
}
183183

184+
public virtual Task<WebResponse> GetResponseAsync(WebRequest webRequest)
185+
{
186+
return webRequest.GetResponseAsync();
187+
}
188+
184189
public virtual bool IsDebugBuild(Assembly assembly)
185190
{
186191
return assembly.AllAttributes()

src/ServiceStack.Text/StreamExtensions.cs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ public static IEnumerable<string> ReadLines(this Stream stream)
4343
if (stream == null)
4444
throw new ArgumentNullException(nameof(stream));
4545

46-
using (var reader = new StreamReader(stream))
46+
using var reader = new StreamReader(stream);
47+
string line;
48+
while ((line = reader.ReadLine()) != null)
4749
{
48-
string line;
49-
while ((line = reader.ReadLine()) != null)
50-
{
51-
yield return line;
52-
}
50+
yield return line;
5351
}
5452
}
5553

@@ -103,17 +101,15 @@ public static byte[] ReadFully(this Stream input, byte[] buffer)
103101

104102
// We could do all our own work here, but using MemoryStream is easier
105103
// and likely to be just as efficient.
106-
using (var tempStream = MemoryStreamFactory.GetStream())
104+
using var tempStream = MemoryStreamFactory.GetStream();
105+
CopyTo(input, tempStream, buffer);
106+
// No need to copy the buffer if it's the right size
107+
if (tempStream.Length == tempStream.GetBuffer().Length)
107108
{
108-
CopyTo(input, tempStream, buffer);
109-
// No need to copy the buffer if it's the right size
110-
if (tempStream.Length == tempStream.GetBuffer().Length)
111-
{
112-
return tempStream.GetBuffer();
113-
}
114-
// Okay, make a copy that's the right size
115-
return tempStream.ToArray();
109+
return tempStream.GetBuffer();
116110
}
111+
// Okay, make a copy that's the right size
112+
return tempStream.ToArray();
117113
}
118114

119115
/// <summary>
@@ -379,10 +375,8 @@ public static string ReadToEnd(this MemoryStream ms, Encoding encoding)
379375

380376
Tracer.Instance.WriteWarning("MemoryStream wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl");
381377

382-
using (var reader = new StreamReader(ms, encoding, true, DefaultBufferSize, leaveOpen: true))
383-
{
384-
return reader.ReadToEnd();
385-
}
378+
using var reader = new StreamReader(ms, encoding, true, DefaultBufferSize, leaveOpen: true);
379+
return reader.ReadToEnd();
386380
}
387381

388382
public static ReadOnlyMemory<byte> GetBufferAsMemory(this MemoryStream ms)
@@ -470,10 +464,8 @@ public static Task<string> ReadToEndAsync(this MemoryStream ms, Encoding encodin
470464

471465
Tracer.Instance.WriteWarning("MemoryStream in ReadToEndAsync() wasn't created with a publiclyVisible:true byte[] buffer, falling back to slow impl");
472466

473-
using (var reader = new StreamReader(ms, encoding, true, DefaultBufferSize, leaveOpen: true))
474-
{
475-
return reader.ReadToEndAsync();
476-
}
467+
using var reader = new StreamReader(ms, encoding, true, DefaultBufferSize, leaveOpen: true);
468+
return reader.ReadToEndAsync();
477469
}
478470

479471
public static string ReadToEnd(this Stream stream) => ReadToEnd(stream, JsConfig.UTF8Encoding);
@@ -487,10 +479,8 @@ public static string ReadToEnd(this Stream stream, Encoding encoding)
487479
stream.Position = 0;
488480
}
489481

490-
using (var reader = new StreamReader(stream, encoding, true, DefaultBufferSize, leaveOpen:true))
491-
{
492-
return reader.ReadToEnd();
493-
}
482+
using var reader = new StreamReader(stream, encoding, true, DefaultBufferSize, leaveOpen:true);
483+
return reader.ReadToEnd();
494484
}
495485

496486
public static Task<string> ReadToEndAsync(this Stream stream) => ReadToEndAsync(stream, JsConfig.UTF8Encoding);
@@ -504,10 +494,8 @@ public static Task<string> ReadToEndAsync(this Stream stream, Encoding encoding)
504494
stream.Position = 0;
505495
}
506496

507-
using (var reader = new StreamReader(stream, encoding, true, DefaultBufferSize, leaveOpen:true))
508-
{
509-
return reader.ReadToEndAsync();
510-
}
497+
using var reader = new StreamReader(stream, encoding, true, DefaultBufferSize, leaveOpen:true);
498+
return reader.ReadToEndAsync();
511499
}
512500

513501
public static Task WriteToAsync(this MemoryStream stream, Stream output, CancellationToken token=default(CancellationToken)) =>

0 commit comments

Comments
 (0)