Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously sends a standard HTML response for the specified status code.
        /// </summary>
        /// <param name="this">The <see cref="IHttpResponse"/> interface on which this method is called.</param>
        /// <param name="statusCode">The HTTP status code of the response.</param>
        /// <param name="appendAdditionalHtml">A callback function that may append additional HTML code
        /// to the response. If not <see langword="null"/>, the callback is called immediately before
        /// closing the HTML <c>body</c> tag.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param>
        /// <returns>A <see cref="Task"/> representing the ongoing operation.</returns>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">There is no standard status description for <paramref name="statusCode"/>.</exception>
        /// <seealso cref="StandardHtmlResponseAsync(IHttpResponse,int,CancellationToken)"/>
        public static Task StandardHtmlResponseAsync(
            this IHttpResponse @this,
            int statusCode,
            Func <StringBuilder, StringBuilder> appendAdditionalHtml,
            CancellationToken cancellationToken)
        {
            if (!HttpStatusDescription.TryGet(statusCode, out var statusDescription))
            {
                throw new ArgumentException("Status code has no standard description.", nameof(statusCode));
            }

            @this.StatusCode        = statusCode;
            @this.StatusDescription = statusDescription;
            @this.ContentType       = MimeTypes.HtmlType;
            var sb = new StringBuilder()
                     .Append("<html><head><meta charset=\"UTF-8\"><title>")
                     .Append(statusCode)
                     .Append(" - ")
                     .Append(statusDescription)
                     .Append("</title></head><body><h1>")
                     .Append(statusCode)
                     .Append(" - ")
                     .Append(statusDescription)
                     .Append("</h1>");

            appendAdditionalHtml?.Invoke(sb);
            sb.Append("</body></html>");
            var buffer = Encoding.UTF8.GetBytes(sb.ToString());

            sb = null; // Free some memory if next GC is near
            @this.ContentLength64 = buffer.Length;
            return(@this.OutputStream.WriteAsync(buffer, 0, buffer.Length, cancellationToken));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prepares a standard response without a body for the specified status code.
        /// </summary>
        /// <param name="this">The <see cref="IHttpResponse"/> interface on which this method is called.</param>
        /// <param name="statusCode">The HTTP status code of the response.</param>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">There is no standard status description for <paramref name="statusCode"/>.</exception>
        public static void StandardResponseWithoutBody(this IHttpResponse @this, int statusCode)
        {
            if (!HttpStatusDescription.TryGet(statusCode, out var statusDescription))
            {
                throw new ArgumentException("Status code has no standard description.", nameof(statusCode));
            }

            @this.StatusCode        = statusCode;
            @this.StatusDescription = statusDescription;
            @this.ContentType       = string.Empty;
            @this.ContentLength64   = 0;
        }