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);
//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,
}
};
}
/// <summary>
/// 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();

View file

@ -3,29 +3,31 @@
/// <summary>
/// General purpose implementation of <see cref="IHttpFields"/>.
/// </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>
/// 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);

View file

@ -9,32 +9,32 @@ namespace Uwaa.HTTP;
/// <summary>
/// Contains the method, path, query, headers, and content of a HTTP request.
/// </summary>
public class HttpRequest
public record HttpRequest
{
/// <summary>
/// The HTTP method of the request.
/// </summary>
public HttpMethod Method = HttpMethod.Any;
public HttpMethod Method { get; set; } = HttpMethod.Any;
/// <summary>
/// The HTTP path being requested.
/// </summary>
public string Path = string.Empty;
public string Path { get; set; } = string.Empty;
/// <summary>
/// Additional paramters in the path of the request.
/// </summary>
public NameValueCollection Query;
public NameValueCollection Query { get; set; }
/// <summary>
/// HTTP header fields included in the request.
/// </summary>
public HttpFields Fields;
public HttpFields Fields { get; set; }
/// <summary>
/// The body of the HTTP request, if any.
/// </summary>
public HttpContent? Content;
public HttpContent? Content { get; set; }
/// <summary>
/// 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();