Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 405 fix handler #52

Closed
wants to merge 2 commits into from
Closed

add 405 fix handler #52

wants to merge 2 commits into from

Conversation

jchannon
Copy link
Member

@jchannon jchannon commented Nov 2, 2017

Fixes #51


private static void Add405Handler(IApplicationBuilder builder, IEnumerable<BotwinModule> srvs)
{
var systemRoutes = new List<(string verb, string route)>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dictionary<path, IEnumerable<verb> would probably read nicer in this method, and might be quicker than all of the linq too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to load the dictionary you'd have to do systemRoutes.Add(route.path, module.Routes.Where(x=>x.path == route.path).Select(x=>x.verb));

i think this whole piece of code could be improved though

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you're not wrong. There's gotta be a nicer way of getting that code in there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't something like this be more efficient in the long run?

var systemRoutes = srvs.SelectMany(m => m.Routes)
    .GroupBy(r => "/" + r.path)
    .ToDictionary(
        g => g.Key, 
        g => new HashSet<string>(
            g.Select(r => r.verb).Distinct()
        )
    ); // IDictionary<string, HashSet<string>>

We than could just do:

if (systemRoutes.ContainsKey(strippedPath) &&
    !systemRoutes[strippedPath].Contains(context.Request.Method)) // almost O(1) access time
{
    context.Response.StatusCode = 405;
    return;
}

(the LINQ part may be simplified into a foreach if it appears too cryptical)

@jchannon
Copy link
Member Author

jchannon commented Nov 8, 2017 via email

@jchannon
Copy link
Member Author

jchannon commented Nov 9, 2017

Pushed a change which should make the checks more precise but still not happy about the stripping per path on each request approach

@jchannon
Copy link
Member Author

@davidfowl any better perf suggestions?

@jchannon
Copy link
Member Author

Moved to #65

@jchannon jchannon closed this Nov 16, 2017
@jchannon jchannon deleted the 405fix branch November 16, 2017 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants