add host field to all requests

This commit is contained in:
uwaa 2024-11-22 08:08:27 +00:00
parent f7119edb30
commit d9193a6473
4 changed files with 46 additions and 22 deletions

View file

@ -45,12 +45,27 @@ public sealed class HttpClient
HttpStream stream = new HttpStream(innerStream); HttpStream stream = new HttpStream(innerStream);
//Send request //Send request
await request.WriteTo(stream); await FixRequest(request, host, ConnectionType.Close).WriteTo(stream);
//Read response //Read response
return await stream.ReadResponse(); 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,
}
};
}
/// <summary> /// <summary>
/// The host to connect to. /// The host to connect to.
@ -122,7 +137,7 @@ public sealed class HttpClient
try try
{ {
//Send request //Send request
await request.WriteTo(stream); await FixRequest(request, Host, ConnectionType.KeepAlive).WriteTo(stream);
//Read response //Read response
return await stream.ReadResponse(); return await stream.ReadResponse();

View file

@ -3,29 +3,31 @@
/// <summary> /// <summary>
/// General purpose implementation of <see cref="IHttpFields"/>. /// General purpose implementation of <see cref="IHttpFields"/>.
/// </summary> /// </summary>
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; }
/// <summary> /// <summary>
/// Extra fields to include. /// Extra fields to include.
@ -52,6 +54,10 @@ public class HttpFields
return; return;
} }
case "host":
Host = value;
return;
case "upgrade": case "upgrade":
Upgrade = value; Upgrade = value;
return; return;
@ -137,6 +143,9 @@ public class HttpFields
_ => "close", _ => "close",
}); });
if (Host != null)
callback("Host", Host);
if (UserAgent != null) if (UserAgent != null)
callback("User-Agent", UserAgent); callback("User-Agent", UserAgent);

View file

@ -9,32 +9,32 @@ namespace Uwaa.HTTP;
/// <summary> /// <summary>
/// Contains the method, path, query, headers, and content of a HTTP request. /// Contains the method, path, query, headers, and content of a HTTP request.
/// </summary> /// </summary>
public class HttpRequest public record HttpRequest
{ {
/// <summary> /// <summary>
/// The HTTP method of the request. /// The HTTP method of the request.
/// </summary> /// </summary>
public HttpMethod Method = HttpMethod.Any; public HttpMethod Method { get; set; } = HttpMethod.Any;
/// <summary> /// <summary>
/// The HTTP path being requested. /// The HTTP path being requested.
/// </summary> /// </summary>
public string Path = string.Empty; public string Path { get; set; } = string.Empty;
/// <summary> /// <summary>
/// Additional paramters in the path of the request. /// Additional paramters in the path of the request.
/// </summary> /// </summary>
public NameValueCollection Query; public NameValueCollection Query { get; set; }
/// <summary> /// <summary>
/// HTTP header fields included in the request. /// HTTP header fields included in the request.
/// </summary> /// </summary>
public HttpFields Fields; public HttpFields Fields { get; set; }
/// <summary> /// <summary>
/// The body of the HTTP request, if any. /// The body of the HTTP request, if any.
/// </summary> /// </summary>
public HttpContent? Content; public HttpContent? Content { get; set; }
/// <summary> /// <summary>
/// If true, the connection is requesting to be upgrading to a websocket. /// If true, the connection is requesting to be upgrading to a websocket.

View file

@ -15,7 +15,7 @@ public class CORS : RouterBase
} }
} }
class PreflightResponseFields : HttpFields record PreflightResponseFields : HttpFields
{ {
public static readonly PreflightResponseFields Singleton = new PreflightResponseFields(); public static readonly PreflightResponseFields Singleton = new PreflightResponseFields();