using System; using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; using EmbedIO.Utilities; namespace EmbedIO { partial class HttpContextExtensions { private const string StandardHtmlHeaderFormat = "{0} - {1}

{0} - {1}

"; private const string StandardHtmlFooter = ""; /// /// Asynchronously sends a string as response. /// /// The interface on which this method is called. /// The response content. /// The MIME type of the content. If , the content type will not be set. /// The to use. /// A representing the ongoing operation. /// is . /// /// is . /// - or - /// is . /// public static async Task SendStringAsync( this IHttpContext @this, string content, string contentType, Encoding encoding) { content = Validate.NotNull(nameof(content), content); encoding = Validate.NotNull(nameof(encoding), encoding); if (contentType != null) { @this.Response.ContentType = contentType; @this.Response.ContentEncoding = encoding; } using var text = @this.OpenResponseText(encoding); await text.WriteAsync(content).ConfigureAwait(false); } /// /// Asynchronously sends a standard HTML response for the specified status code. /// /// The interface on which this method is called. /// The HTTP status code of the response. /// A representing the ongoing operation. /// is . /// There is no standard status description for . /// public static Task SendStandardHtmlAsync(this IHttpContext @this, int statusCode) => SendStandardHtmlAsync(@this, statusCode, null); /// /// Asynchronously sends a standard HTML response for the specified status code. /// /// The interface on which this method is called. /// The HTTP status code of the response. /// A callback function that may write additional HTML code /// to a representing the response output. /// If not , the callback is called immediately before closing the HTML body tag. /// A representing the ongoing operation. /// is . /// There is no standard status description for . /// public static Task SendStandardHtmlAsync( this IHttpContext @this, int statusCode, Action? writeAdditionalHtml) { if (!HttpStatusDescription.TryGet(statusCode, out var statusDescription)) throw new ArgumentException("Status code has no standard description.", nameof(statusCode)); @this.Response.StatusCode = statusCode; @this.Response.StatusDescription = statusDescription; @this.Response.ContentType = MimeType.Html; @this.Response.ContentEncoding = WebServer.DefaultEncoding; using (var text = @this.OpenResponseText(WebServer.DefaultEncoding)) { text.Write(StandardHtmlHeaderFormat, statusCode, statusDescription, WebServer.DefaultEncoding.WebName); writeAdditionalHtml?.Invoke(text); text.Write(StandardHtmlFooter); } return Task.CompletedTask; } /// /// Asynchronously sends serialized data as a response, using the default response serializer. /// As of EmbedIO version 3.0, the default response serializer has the same behavior of JSON /// response methods of version 2. /// /// The interface on which this method is called. /// The data to serialize. /// A representing the ongoing operation. /// is . /// /// public static Task SendDataAsync(this IHttpContext @this, object data) => ResponseSerializer.Default(@this, data); /// /// Asynchronously sends serialized data as a response, using the specified response serializer. /// As of EmbedIO version 3.0, the default response serializer has the same behavior of JSON /// response methods of version 2. /// /// The interface on which this method is called. /// A used to prepare the response. /// The data to serialize. /// A representing the ongoing operation. /// is . /// is . /// /// public static Task SendDataAsync(this IHttpContext @this, ResponseSerializerCallback serializer, object data) => Validate.NotNull(nameof(serializer), serializer)(@this, data); } }