diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-11-01 11:24:59 -0400 |
---|---|---|
committer | Matthew Heon <matthew.heon@gmail.com> | 2017-11-01 11:24:59 -0400 |
commit | a031b83a09a8628435317a03f199cdc18b78262f (patch) | |
tree | bc017a96769ce6de33745b8b0b1304ccf38e9df0 /vendor/github.com/go-zoo/bone/bone.go | |
parent | 2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff) | |
download | podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.gz podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.bz2 podman-a031b83a09a8628435317a03f199cdc18b78262f.zip |
Initial checkin from CRI-O repo
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Diffstat (limited to 'vendor/github.com/go-zoo/bone/bone.go')
-rw-r--r-- | vendor/github.com/go-zoo/bone/bone.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/github.com/go-zoo/bone/bone.go b/vendor/github.com/go-zoo/bone/bone.go new file mode 100644 index 000000000..d00a0b083 --- /dev/null +++ b/vendor/github.com/go-zoo/bone/bone.go @@ -0,0 +1,74 @@ +/******************************** +*** Multiplexer for Go *** +*** Bone is under MIT license *** +*** Code by CodingFerret *** +*** github.com/go-zoo *** +*********************************/ + +package bone + +import ( + "net/http" + "strings" +) + +// Mux have routes and a notFound handler +// Route: all the registred route +// notFound: 404 handler, default http.NotFound if not provided +type Mux struct { + Routes map[string][]*Route + prefix string + notFound http.Handler + Serve func(rw http.ResponseWriter, req *http.Request) + CaseSensitive bool +} + +var ( + static = "static" + method = []string{"GET", "POST", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS"} +) + +type adapter func(*Mux) *Mux + +// New create a pointer to a Mux instance +func New(adapters ...adapter) *Mux { + m := &Mux{Routes: make(map[string][]*Route), Serve: nil, CaseSensitive: true} + for _, adap := range adapters { + adap(m) + } + if m.Serve == nil { + m.Serve = m.DefaultServe + } + return m +} + +// Prefix set a default prefix for all routes registred on the router +func (m *Mux) Prefix(p string) *Mux { + m.prefix = strings.TrimSuffix(p, "/") + return m +} + +// DefaultServe is the default http request handler +func (m *Mux) DefaultServe(rw http.ResponseWriter, req *http.Request) { + // Check if a route match + if !m.parse(rw, req) { + // Check if it's a static ressource + if !m.staticRoute(rw, req) { + // Check if the request path doesn't end with / + if !m.validate(rw, req) { + // Check if same route exists for another HTTP method + if !m.otherMethods(rw, req) { + m.HandleNotFound(rw, req) + } + } + } + } +} + +// ServeHTTP pass the request to the serve method of Mux +func (m *Mux) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + if !m.CaseSensitive { + req.URL.Path = strings.ToLower(req.URL.Path) + } + m.Serve(rw, req) +} |