Exemplo n.º 1
0
        private async Task <bool> HandleGet(IHttpContext context, CancellationToken ct, bool sendBuffer = true)
        {
            Stream buffer = null;

            try
            {
                var localPath     = FixPath(context.RequestPathCaseSensitive());
                var partialHeader = context.RequestHeader(HttpHeaders.Range);

                $"Resource System: {localPath}".Debug(nameof(ResourceFilesModule));

                buffer = _sourceAssembly.GetManifestResourceStream($"{_resourcePathRoot}.{localPath}");

                // If buffer is null something is really wrong
                if (buffer == null)
                {
                    return(false);
                }

                // check to see if the file was modified or e-tag is the same
                var utcFileDateString = DateTime.Now.ToUniversalTime()
                                        .ToString(Strings.BrowserTimeFormat, Strings.StandardCultureInfo);

                context.Response.ContentLength64 = buffer.Length;

                SetGeneralHeaders(context.Response, utcFileDateString, localPath.Contains(".") ? $".{localPath.Split('.').Last()}" : ".html");

                if (sendBuffer)
                {
                    await WriteFileAsync(
                        partialHeader,
                        context.Response,
                        buffer,
                        context.AcceptGzip(buffer.Length),
                        ct)
                    .ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                // Connection error, nothing else to do
                var isListenerException =
#if !NETSTANDARD1_3
                    ex is System.Net.HttpListenerException ||
#endif
                    ex is Net.HttpListenerException;

                if (!isListenerException)
                {
                    throw;
                }
            }
            finally
            {
                buffer?.Dispose();
            }

            return(true);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Writes a binary response asynchronous.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="buffer">The buffer.</param>
 /// <param name="useGzip">if set to <c>true</c> [use gzip].</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// A task for writing the output stream.
 /// </returns>
 public static Task <bool> BinaryResponseAsync(
     this IHttpContext context,
     Stream buffer,
     bool useGzip = true,
     CancellationToken cancellationToken = default)
 => BinaryResponseAsync(context.Response, buffer, useGzip && context.AcceptGzip(buffer.Length), cancellationToken);
Exemplo n.º 3
0
 /// <summary>
 /// Outputs async a string response given a string.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="content">The content.</param>
 /// <param name="contentType">Type of the content.</param>
 /// <param name="encoding">The encoding.</param>
 /// <param name="useGzip">if set to <c>true</c> [use gzip].</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// A task for writing the output stream.
 /// </returns>
 public static Task <bool> StringResponseAsync(
     this IHttpContext context,
     string content,
     string contentType = "application/json",
     Encoding encoding  = null,
     bool useGzip       = true,
     CancellationToken cancellationToken = default) =>
 context.Response.StringResponseAsync(content, contentType, encoding, useGzip && context.AcceptGzip(content.Length), cancellationToken);