Esempio n. 1
0
        /// <summary>
        /// This examines the request to see if it matches a configured directory, and if there are any files with the
        /// configured default names in that directory.  If so this will append the corresponding file name to the request
        /// path for a later middleware to handle.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Invoke(ProtoContext context)
        {
            if (context.GetEndpoint() == null &&
                Helpers.IsGetOrHeadMethod(context.Request.Method) &&
                Helpers.TryMatchPath(context, _matchUrl, forDirectory: true, subpath: out var subpath))
            {
                var dirContents = _fileProvider.GetDirectoryContents(subpath.Value);
                if (dirContents.Exists)
                {
                    // Check if any of our default files exist.
                    for (var matchIndex = 0; matchIndex < _options.DefaultFileNames.Count; matchIndex++)
                    {
                        var defaultFile = _options.DefaultFileNames[matchIndex];
                        var file        = _fileProvider.GetFileInfo(subpath.Value + defaultFile);
                        // TryMatchPath will make sure subpath always ends with a "/" by adding it if needed.
                        if (file.Exists)
                        {
                            // 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);
                            }

                            // Match found, re-write the url. A later middleware will actually serve the file.
                            context.Request.Path = new PathString(context.Request.Path.Value + defaultFile);
                            break;
                        }
                    }
                }
            }
            return(_next(context));
        }
 /// <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
 public bool ValidateNoEndpoint() => _context.GetEndpoint() == null; // Return true because we only want to run if there is no endpoint.