Remove asynchronoucity from startlines
MODIFY
WalletWasabi/Extensions/HttpRequestMessageExtensions.cs
WalletWasabi/Extensions/HttpRequestMessageExtensions.cs
@@ -32,7 +32,7 @@ namespace System.Net.Http
string startLine = await HttpMessageHelper.ReadStartLineAsync(requestStream, ctsToken);
- var requestLine = await RequestLine.CreateNewAsync(startLine);
+ var requestLine = RequestLine.FromString(startLine);
var request = new HttpRequestMessage(requestLine.Method, requestLine.URI);
string headers = await HttpMessageHelper.ReadHeadersAsync(requestStream, ctsToken);

MODIFY
WalletWasabi/Extensions/HttpResponseMessageExtensions.cs
WalletWasabi/Extensions/HttpResponseMessageExtensions.cs
@@ -32,7 +32,7 @@ namespace System.Net.Http
string startLine = await HttpMessageHelper.ReadStartLineAsync(responseStream);
- var statusLine = await StatusLine.CreateNewAsync(startLine);
+ var statusLine = StatusLine.FromString(startLine);
var response = new HttpResponseMessage(statusLine.StatusCode);
string headers = await HttpMessageHelper.ReadHeadersAsync(responseStream);

MODIFY
WalletWasabi/Http/Models/RequestLine.cs
WalletWasabi/Http/Models/RequestLine.cs
@@ -10,10 +10,10 @@ namespace WalletWasabi.Http.Models
// request-line = method SP request-target SP HTTP-version CRLF
public class RequestLine : StartLine
{
- public HttpMethod Method { get; private set; }
- public Uri URI { get; private set; }
+ public HttpMethod Method { get; }
+ public Uri URI { get; }
- public RequestLine(HttpMethod method, Uri uri, HttpProtocol protocol)
+ public RequestLine(HttpMethod method, Uri uri, HttpProtocol protocol) : base(protocol)
{
Method = method;
// https://tools.ietf.org/html/rfc7230#section-2.7.1
@@ -24,16 +24,18 @@ namespace WalletWasabi.Http.Models
}
URI = uri;
- Protocol = protocol;
+ }
- StartLineString = Method.Method + SP + URI.AbsolutePath + URI.Query + SP + Protocol + CRLF;
+ public override string ToString()
+ {
+ return $"{Method.Method}{SP}{URI.AbsolutePath}{URI.Query}{SP}{Protocol}{CRLF}";
}
- public static async Task<RequestLine> CreateNewAsync(string requestLineString)
+ public static RequestLine FromString(string requestLineString)
{
try
{
- var parts = (await GetPartsAsync(requestLineString).ConfigureAwait(false)).ToArray();
+ var parts = GetParts(requestLineString);
var methodString = parts[0];
var uri = new Uri(parts[1]);
var protocolString = parts[2];

MODIFY
WalletWasabi/Http/Models/StartLine.cs
WalletWasabi/Http/Models/StartLine.cs
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Threading.Tasks;
+using WalletWasabi.Helpers;
using static WalletWasabi.Http.Constants;
namespace WalletWasabi.Http.Models
{
public abstract class StartLine
{
- public HttpProtocol Protocol { get; protected set; }
- public string StartLineString { get; protected set; }
+ public HttpProtocol Protocol { get; }
- public override string ToString()
+ protected StartLine(HttpProtocol protocol)
{
- return StartLineString;
+ Protocol = protocol;
}
- public static async Task<IEnumerable<string>> GetPartsAsync(string startLineString)
+ public static string[] GetParts(string startLineString)
{
- var trimmed = "";
- // check if an unexpected crlf in the startlinestring
- using (var reader = new StringReader(startLineString))
- {
- // read to CRLF, if it does not end with that it reads to end, if it does, it removes it
- trimmed = reader.ReadLine(strictCRLF: true);
- // startLineString must end here
- if (reader.Read() != -1)
- {
- throw new Exception($"Wrong {startLineString} provided.");
- }
- }
-
- var parts = new List<string>();
- using (var reader = new StringReader(trimmed))
- {
- while (true)
- {
- var part = reader.ReadPart(SP.ToCharArray()[0]);
-
- if (part is null || part == "")
- {
- break;
- }
-
- if (parts.Count == 2)
- {
- var rest = await reader.ReadToEndAsync().ConfigureAwait(false);
-
- // startLineString must end here, the ReadToEnd returns "" if nothing to read instead of null
- if (rest != "")
- {
- part += SP + rest;
- }
- }
- parts.Add(part);
- }
- }
- return parts;
+ var trimmed = Guard.NotNullOrEmptyOrWhitespace(nameof(startLineString), startLineString, trim: true);
+ return trimmed.Split(SP, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
}
}
}

MODIFY
WalletWasabi/Http/Models/StatusLine.cs
WalletWasabi/Http/Models/StatusLine.cs
@@ -9,21 +9,23 @@ namespace WalletWasabi.Http.Models
{
public class StatusLine : StartLine
{
- public HttpStatusCode StatusCode { get; private set; }
+ public HttpStatusCode StatusCode { get; }
- public StatusLine(HttpProtocol protocol, HttpStatusCode status)
+ public StatusLine(HttpProtocol protocol, HttpStatusCode status) : base(protocol)
{
- Protocol = protocol;
StatusCode = status;
+ }
- StartLineString = Protocol + SP + (int)StatusCode + SP + StatusCode.ToReasonString() + CRLF;
+ public override string ToString()
+ {
+ return $"{Protocol}{SP}{(int)StatusCode}{SP}{StatusCode.ToReasonString()}{CRLF}";
}
- public static async Task<StatusLine> CreateNewAsync(string statusLineString)
+ public static StatusLine FromString(string statusLineString)
{
try
{
- var parts = (await GetPartsAsync(statusLineString).ConfigureAwait(false)).ToArray();
+ var parts = GetParts(statusLineString);
var protocolString = parts[0];
var codeString = parts[1];
var reason = parts[2];

Remove asynchronoucity from startlines