github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/igm/sockjs-go.v2/sockjs/mapping.go (about)

     1  package sockjs
     2  
     3  import (
     4  	"net/http"
     5  	"regexp"
     6  )
     7  
     8  type mapping struct {
     9  	method string
    10  	path   *regexp.Regexp
    11  	chain  []http.HandlerFunc
    12  }
    13  
    14  func newMapping(method string, re string, handlers ...http.HandlerFunc) *mapping {
    15  	return &mapping{method, regexp.MustCompile(re), handlers}
    16  }
    17  
    18  type matchType uint32
    19  
    20  const (
    21  	fullMatch matchType = iota
    22  	pathMatch
    23  	noMatch
    24  )
    25  
    26  // matches checks if given req.URL is a match with a mapping. Match can be either full, partial (http method mismatch) or no match.
    27  func (m *mapping) matches(req *http.Request) (match matchType, method string) {
    28  	if !m.path.MatchString(req.URL.Path) {
    29  		match, method = noMatch, ""
    30  	} else if m.method != req.Method {
    31  		match, method = pathMatch, m.method
    32  	} else {
    33  		match, method = fullMatch, m.method
    34  	}
    35  	return
    36  }