Compare commits

...

2 commits

Author SHA1 Message Date
uwaa
13192a1972 ignore parameters for now 2024-11-22 08:09:58 +00:00
uwaa
d9193a6473 add host field to all requests 2024-11-22 08:08:27 +00:00
5 changed files with 53 additions and 23 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

@ -3,6 +3,8 @@ using System.Text.Json.Serialization;
namespace Uwaa.HTTP; namespace Uwaa.HTTP;
//TODO: Add support for parameters
/// <summary> /// <summary>
/// Represents a Multipurpose Internet Mail Extensions type, which indicates the nature and format of a document/file/chunk of data. /// Represents a Multipurpose Internet Mail Extensions type, which indicates the nature and format of a document/file/chunk of data.
/// </summary> /// </summary>
@ -44,6 +46,10 @@ public readonly record struct MIMEType
throw new FormatException("The provided MIME type is missing a subtype."); throw new FormatException("The provided MIME type is missing a subtype.");
} }
int end = text.IndexOf(';');
if (end == -1)
end = text.Length;
if (spl == 1 && text[0] == '*') if (spl == 1 && text[0] == '*')
Type = null; Type = null;
else else
@ -52,7 +58,7 @@ public readonly record struct MIMEType
if (spl == text.Length - 2 && text[^1] == '*') if (spl == text.Length - 2 && text[^1] == '*')
Subtype = null; Subtype = null;
else else
Subtype = new string(text[(spl + 1)..]); Subtype = new string(text[(spl + 1)..end]);
} }
/// <summary> /// <summary>

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();