/// <summary>
        /// Examines the request to see if it matches a configured directory.  If so, a view of the directory contents is returned.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            // Check if the URL matches any expected paths
            if (!Helpers.IsGetOrHeadMethod(context.Request.Method) ||
                !Helpers.TryMatchPath(context, _prefixUrl, forDirectory: true, subpath: out PathString libraryPath) ||
                !Helpers.TryMatchLibrary(libraryPath, _pathProvider.GetLibraries(), forDirectory: true,
                                         subpath: out PathString subpath, library: out ILibrary library))
            {
                await _next(context);

                return;
            }

            if (!context.IsAuthenticated(_options))
            {
                context.Response.StatusCode = 401;
                return;
            }

            if (!context.IsAuthorized(_options, library, LibraryServerAuthorizationPolicy.Browser))
            {
                context.Response.StatusCode = 403;
                return;
            }

            // If the path matches a directory but does not end in a slash, redirect to add the slash.
            // This prevents relative links from breaking.
            if (!Helpers.PathEndsInSlash(context.Request.Path))
            {
                context.Response.StatusCode = 301;
                context.Response.Headers[HeaderNames.Location] =
                    context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
                return;
            }

            if (TryGetDirectoryInfo(library, subpath, out IDirectoryContents contents))
            {
                await _formatter.GenerateContentAsync(context, contents);

                return;
            }

            context.Response.StatusCode = 500;
        }
 /// <summary>
 /// Examines the request to see if it matches a configured directory.  If so, a view of the directory contents is returned.
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public Task Invoke(ProtoContext context)
 {
     // Check if the URL matches any expected paths, skip if an endpoint was selected
     if (context.GetEndpoint() == null &&
         Helpers.IsGetOrHeadMethod(context.Request.Method) &&
         Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out var subpath) &&
         TryGetDirectoryInfo(subpath, out var contents))
     {
         // If the path matches a directory but does not end in a slash, redirect to add the slash.
         // This prevents relative links from breaking.
         if (!Helpers.PathEndsInSlash(context.Request.Path))
         {
             context.Response.StatusCode = 301;
             context.Response.Headers[HeaderNames.Location] = context.Request.PathBase + context.Request.Path + "/" + context.Request.QueryString;
             return(Task.CompletedTask);
         }
         return(_formatter.GenerateContentAsync(context, contents));
     }
     return(_next(context));
 }
Esempio n. 3
0
    /// <summary>
    /// Examines the request to see if it matches a configured directory.  If so, a view of the directory contents is returned.
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public Task Invoke(HttpContext context)
    {
        // Check if the URL matches any expected paths, skip if an endpoint with a request delegate was selected
        if (context.GetEndpoint()?.RequestDelegate is null &&
            Helpers.IsGetOrHeadMethod(context.Request.Method) &&
            Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out var subpath) &&
            TryGetDirectoryInfo(subpath, out var contents))
        {
            // If the path matches a directory but does not end in a slash, redirect to add the slash.
            // This prevents relative links from breaking.
            if (_options.RedirectToAppendTrailingSlash && !Helpers.PathEndsInSlash(context.Request.Path))
            {
                Helpers.RedirectToPathWithSlash(context);
                return(Task.CompletedTask);
            }

            return(_formatter.GenerateContentAsync(context, contents));
        }

        return(_next(context));
    }