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

Feature/method case insensitive #764

Closed

Conversation

cornejong
Copy link

@cornejong cornejong commented Jun 20, 2024

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Go Version Update
  • Dependency Update

Description

This PR adds the ability to define case insensitive method matching for routes. as suggested in issue #762.
To achieve this this pr adds the following:

// methodMatcher matches the request against HTTP methods without case sensitivity.
// Both the supplied methods as well as the request method will be transformed to uppercase.
type methodCaseInsensitiveMatcher []string

// methodCaseExactMatcher matches the request against HTTP methods exactly.
// No transformation of supplied methods or the request method is applied.
type methodCaseExactMatcher []string

// Explicitly apply the default (and current)  method matcher, overrides the router default
func (r *Route) MethodsDefault(methods ...string) *Route
// Explicitly apply the case insensitive method matcher, overrides the router default
func (r *Route) MethodsCaseInsensitive(methods ...string) *Route
// Explicitly apply the exact method matcher, overrides the router default
func (r *Route) MethodsCaseExact(methods ...string) *Route

// Set routeConf value for methodMatcher, for this route, to methodDefaultMatcher
func (r *Route) MatchMethodDefault() *Route
// Set routeConf value for methodMatcher, for this route, to methodCaseInsensitiveMatcher
func (r *Route) MatchMethodCaseInsensitive() *Route
// Set routeConf value for methodMatcher, for this route, to methodCaseExactMatcher
func (r *Route) MatchMethodCaseExact() *Route

// Set default method matcher for the router to methodDefaultMatcher
func (r *Router) MatchMethodDefault() *Router
// Set default method matcher for the router to methodCaseInsensitiveMatcher
func (r *Router) MatchMethodCaseInsensitive() *Router
// Set default method matcher for the router to methodCaseExactMatcher
func (r *Router) MatchMethodCaseExact() *Router


type routeConf struct {
    // ...
    // Holds the default method matcher
    methodMatcher matcher
}

// as well a the helper
func sliceToUpper(slice []string) []string 

Usage

Setting the default method matcher on the router:

r := mux.NewRouter()
r.MatchMethodDefault()          // For the default matcher
r.MatchMethodCaseInsensitive()  // For case insensitive matching
r.MatchMethodCaseExact()        // For exact matching

r.HandleFunc("/", func).Methods("GET") //  will use the default matcher set on the router

Applying a specific matcher to a single route and overriding the default method matcher of the router:

r.HandleFunc("/", func).MatchMethodDefault().Methods("GET")
r.HandleFunc("/", func).MatchMethodCaseInsensitive().Methods("GET")
r.HandleFunc("/", func).MatchMethodCaseExact().Methods("GET")
// Not the prefferd method, order of opperations is critcal here. 
// Since the Methods(...) func adds the matcher. 
// Invoking the MatchMethodCaseInsensitive() method after it will have no effect.

// OR, the more preferred method:

r.HandleFunc("/", func).MethodsDefault("GET")
// Explicitly match the methods for this route with the default matcher
r.HandleFunc("/", func).MethodsCaseInsensitive("GET")
// Explicitly match the methods for this route without case sensitivity
r.HandleFunc("/", func).MethodsCaseExact("GET")
// Explicitly match the methods for this route exactly

While using the standard Methods(...) method the default matcher for the router will be used.

Related Tickets & Documents

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

Run verifications and test

  • make verify is passing
  • make test is passing

@hulkingshtick
Copy link

HTTP method names are case sensitive. "GET" and "get" are different methods.

This PR adds a large amount of API surface area for something that should rarely be used.

@cornejong
Copy link
Author

HTTP method names are case sensitive. "GET" and "get" are different methods.

This PR adds a large amount of API surface area for something that should rarely be used.

@hulkingshtick, actually I totally agree with you and no longer think this is a good approach.

@jaitaiwan
Copy link
Member

I too agree, closing

@jaitaiwan jaitaiwan closed this Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Ignore case sensitivity when matching defined methods
3 participants