NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.15k stars 1.47k forks source link

Invalid conventions initialization with custom static content configuration #2973

Open lorond opened 5 years ago

lorond commented 5 years ago

Description

Static conventions are initialized BEFORE StaticContentConfiguration initialization. If custom StaticContentConfiguration changes root directory, path to files, matched by static conventions, are related to directory, that WAS root before StaticContentConfiguration was initialized.

Steps to Reproduce

Make a custom static conventions either way, via bootstrapper or implementing IConvention interface:

// this will be executed before CustomStaticConfiguration
conv.StaticContentsConventions.AddFile("/test.css", @"content/static/test.min.css");

Make a custom static configuration:

public class CustomStaticConfiguration : DefaultStaticContentConfigurationProvider
{
    private const string RootPath = @"../../../PublicOffice";

    public CustomStaticConfiguration()
        : base(new RootPathProvider())
    {
    }

    private class RootPathProvider : IRootPathProvider
    {
        // this will be executed after static conventions initialization
        public string GetRootPath() => Path.Combine(Directory.GetCurrentDirectory(),
                                                    RootPath);
    }
}

Make a module, returning same static content:

public sealed class StaticExampleModule : NancyModule
{
    public StaticExampleModule()
    {
        this.Get("/example", _ => new GenericFileResponse(@"content/static/test.min.css",
                                                          this.Context));
    }
}

When you query http://localhost/test.css (static conventions) and http://localhost/example (module), different files are accessed by nancy:

image

But they both must be same - /example correspond to valid path. /test.css lead to 404.

System Configuration