Compare commits
2 commits
f7119edb30
...
13192a1972
Author | SHA1 | Date | |
---|---|---|---|
|
13192a1972 | ||
|
d9193a6473 |
5 changed files with 53 additions and 23 deletions
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -3,6 +3,8 @@ using System.Text.Json.Serialization;
|
|||
|
||||
namespace Uwaa.HTTP;
|
||||
|
||||
//TODO: Add support for parameters
|
||||
|
||||
/// <summary>
|
||||
/// Represents a Multipurpose Internet Mail Extensions type, which indicates the nature and format of a document/file/chunk of data.
|
||||
/// </summary>
|
||||
|
@ -44,6 +46,10 @@ public readonly record struct MIMEType
|
|||
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] == '*')
|
||||
Type = null;
|
||||
else
|
||||
|
@ -52,7 +58,7 @@ public readonly record struct MIMEType
|
|||
if (spl == text.Length - 2 && text[^1] == '*')
|
||||
Subtype = null;
|
||||
else
|
||||
Subtype = new string(text[(spl + 1)..]);
|
||||
Subtype = new string(text[(spl + 1)..end]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -15,7 +15,7 @@ public class CORS : RouterBase
|
|||
}
|
||||
}
|
||||
|
||||
class PreflightResponseFields : HttpFields
|
||||
record PreflightResponseFields : HttpFields
|
||||
{
|
||||
public static readonly PreflightResponseFields Singleton = new PreflightResponseFields();
|
||||
|
||||
|
|
Loading…
Reference in a new issue