コード例 #1
0
        public async Task <bool> TryServeFile(HttpContext httpContext, bool serveContent, CancellationToken cancellationToken)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var request    = httpContext.Request;
            var fileSource = _fileSourceResolver.Resolve(request.Host);
            var filename   = request.Path.ToString().TrimStart('/');

            using (var fileInfo = await fileSource.FetchFile(filename, cancellationToken))
            {
                if (fileInfo == null)
                {
                    return(false);
                }

                return(await ServeFile(httpContext.Response, fileInfo, serveContent, cancellationToken));
            }
        }
コード例 #2
0
    public async Task <bool> TryServeFile(HttpContext httpContext, CancellationToken cancellationToken)
    {
        if (!CheckMethod(httpContext.Request.Method, out var serveContent))
        {
            httpContext.Response.StatusCode = StatusCodes.Status501NotImplemented;
            return(true);
        }

        using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, httpContext.RequestAborted);
        var request    = httpContext.Request;
        var fileSource = _fileSourceResolver.Resolve(request.Host);
        var filename   = request.Path.ToString().TrimStart('/');

        await using var fileInfo = await fileSource.FetchFile(filename, cts.Token);

        if (fileInfo is null)
        {
            return(false);
        }

        return(await ServeFile(httpContext.Response, fileInfo, serveContent, cts.Token));
    }