diff --git a/HTTP/HttpClient.cs b/HTTP/HttpClient.cs index 19316d0..c1dce8e 100644 --- a/HTTP/HttpClient.cs +++ b/HTTP/HttpClient.cs @@ -45,12 +45,27 @@ public sealed class HttpClient HttpStream stream = new HttpStream(innerStream); //Send request - await request.WriteTo(stream); + await FixRequest(request, host, ConnectionType.Close).WriteTo(stream); //Read response return await stream.ReadResponse(); } + static HttpRequest FixRequest(HttpRequest req, string host, ConnectionType conType) + { + if (req.Fields.Host == host && req.Fields.Connection == conType) + return req; + + return req with + { + Fields = req.Fields with + { + Host = host, + Connection = ConnectionType.Close, + } + }; + } + /// /// The host to connect to. @@ -122,7 +137,7 @@ public sealed class HttpClient try { //Send request - await request.WriteTo(stream); + await FixRequest(request, Host, ConnectionType.KeepAlive).WriteTo(stream); //Read response return await stream.ReadResponse(); diff --git a/HTTP/HttpFields.cs b/HTTP/HttpFields.cs index abd2902..7b33030 100644 --- a/HTTP/HttpFields.cs +++ b/HTTP/HttpFields.cs @@ -3,29 +3,31 @@ /// /// General purpose implementation of . /// -public class HttpFields +public record HttpFields { - public MIMEType[]? Accept; + public MIMEType[]? Accept { get; set; } - public ConnectionType? Connection; + public ConnectionType? Connection { get; set; } - public string? Upgrade; + public string? Host { get; set; } - public string? UserAgent; + public string? Upgrade { get; set; } - public string? Authorization; + public string? UserAgent { get; set; } - public string? WebSocketKey; + public string? Authorization { get; set; } - public string? WebSocketAccept; + public string? WebSocketKey { get; set; } - public string? WebSocketProtocol; + public string? WebSocketAccept { get; set; } - public string? Location; + public string? WebSocketProtocol { get; set; } - public int? ContentLength; + public string? Location { get; set; } - public MIMEType? ContentType; + public int? ContentLength { get; set; } + + public MIMEType? ContentType { get; set; } /// /// Extra fields to include. @@ -52,6 +54,10 @@ public class HttpFields return; } + case "host": + Host = value; + return; + case "upgrade": Upgrade = value; return; @@ -137,6 +143,9 @@ public class HttpFields _ => "close", }); + if (Host != null) + callback("Host", Host); + if (UserAgent != null) callback("User-Agent", UserAgent); diff --git a/HTTP/HttpRequest.cs b/HTTP/HttpRequest.cs index 8146b1d..39c0ff7 100644 --- a/HTTP/HttpRequest.cs +++ b/HTTP/HttpRequest.cs @@ -9,32 +9,32 @@ namespace Uwaa.HTTP; /// /// Contains the method, path, query, headers, and content of a HTTP request. /// -public class HttpRequest +public record HttpRequest { /// /// The HTTP method of the request. /// - public HttpMethod Method = HttpMethod.Any; + public HttpMethod Method { get; set; } = HttpMethod.Any; /// /// The HTTP path being requested. /// - public string Path = string.Empty; + public string Path { get; set; } = string.Empty; /// /// Additional paramters in the path of the request. /// - public NameValueCollection Query; - + public NameValueCollection Query { get; set; } + /// /// HTTP header fields included in the request. /// - public HttpFields Fields; + public HttpFields Fields { get; set; } /// /// The body of the HTTP request, if any. /// - public HttpContent? Content; + public HttpContent? Content { get; set; } /// /// If true, the connection is requesting to be upgrading to a websocket. diff --git a/HTTP/Routing/CORS.cs b/HTTP/Routing/CORS.cs index a1290e5..a2466a5 100644 --- a/HTTP/Routing/CORS.cs +++ b/HTTP/Routing/CORS.cs @@ -15,7 +15,7 @@ public class CORS : RouterBase } } -class PreflightResponseFields : HttpFields +record PreflightResponseFields : HttpFields { public static readonly PreflightResponseFields Singleton = new PreflightResponseFields();