Innovenergy_trunk/csharp/lib/WebServer/HttpExtensions.cs

101 lines
2.7 KiB
C#

using System.Net;
using InnovEnergy.Lib.Utils;
namespace InnovEnergy.Lib.WebServer;
public static class HttpExtensions
{
public static String AsString(this ContentType t) => Types[(Int32) t];
public static IEnumerable<HttpHeader> ParseHeaders(this HttpListenerRequest req)
{
var headers = req.Headers;
return headers
.AllKeys
.EmptyIfNull()
.Where(k => k != null)
.Select(k => new HttpHeader(k!, headers[k]!));
}
public static HttpMethod ParseMethod(this HttpListenerRequest request)
{
return request.HttpMethod.ToUpper() switch
{
"GET" => HttpMethod.Get,
"POST" => HttpMethod.Post,
"PUT" => HttpMethod.Put,
"PATCH" => HttpMethod.Patch,
"CONNECT" => HttpMethod.Connect,
"HEAD" => HttpMethod.Head,
"DELETE" => HttpMethod.Delete,
"OPTIONS" => HttpMethod.Options,
"TRACE" => HttpMethod.Trace,
_ => HttpMethod.Unknown
};
}
public static readonly IReadOnlyList<String> Types = new[]
{
"application/atom+xml",
"application/atomcat+xml",
"application/ecmascript",
"application/java-archive",
"application/javascript",
"application/json",
"application/mp4",
"application/octet-stream",
"application/pdf",
"application/pkcs10",
"application/pkcs7-mime",
"application/pkcs7-signature",
"application/pkcs8",
"application/postscript",
"application/rdf+xml",
"application/rss+xml",
"application/rtf",
"application/smil+xml",
"application/x-font-otf",
"application/x-font-ttf",
"application/x-font-woff",
"application/x-pkcs12",
"application/x-shockwave-flash",
"application/x-silverlight-app",
"application/xhtml+xml",
"application/xml",
"application/xml-dtd",
"application/xslt+xml",
"application/zip",
"audio/midi",
"audio/mp4",
"audio/mpeg",
"audio/ogg",
"audio/webm",
"audio/x-aac",
"audio/x-aiff",
"audio/x-mpegurl",
"audio/x-ms-wma",
"audio/x-wav",
"image/bmp",
"image/gif",
"image/jpeg",
"image/png",
"image/svg+xml",
"image/tiff",
"image/webp",
"text/css",
"text/csv",
"text/html",
"text/plain",
"text/richtext",
"text/sgml",
"text/yaml",
"video/3gpp",
"video/h264",
"video/mp4",
"video/mpeg",
"video/ogg",
"video/quicktime",
"video/webm",
};
}