44 lines
1 KiB
C#
44 lines
1 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Text;
|
|
|
|
namespace Uwaa.HTTP;
|
|
|
|
/// <summary>
|
|
/// Content served as the body of a HTTP request or response.
|
|
/// </summary>
|
|
public struct HttpContent
|
|
{
|
|
public static implicit operator HttpContent?([NotNullWhen(false)] string? value)
|
|
{
|
|
return value == null ? null : new HttpContent("text/plain", value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The MIME type of the HTTP content.
|
|
/// </summary>
|
|
public MIMEType Type;
|
|
|
|
/// <summary>
|
|
/// The raw HTTP content body, as bytes.
|
|
/// </summary>
|
|
public byte[] Content;
|
|
|
|
/// <summary>
|
|
/// Converts the contents to a UTF8 string.
|
|
/// </summary>
|
|
public string AsText => Encoding.UTF8.GetString(Content);
|
|
|
|
public HttpContent(MIMEType type, string body)
|
|
{
|
|
Type = type;
|
|
Content = Encoding.UTF8.GetBytes(body);
|
|
}
|
|
|
|
public HttpContent(MIMEType type, byte[] body)
|
|
{
|
|
Type = type;
|
|
Content = body;
|
|
}
|
|
|
|
public override string ToString() => Type.ToString();
|
|
}
|