pax_global_header00006660000000000000000000000064123526575670014534gustar00rootroot0000000000000052 comment=b8a35001b773c267eb260a691f4e5499a3531600 golang-github-bmizerany-pat-0.0~git20140625/000077500000000000000000000000001235265756700204305ustar00rootroot00000000000000golang-github-bmizerany-pat-0.0~git20140625/.gitignore000066400000000000000000000000351235265756700224160ustar00rootroot00000000000000*.prof *.out example/example golang-github-bmizerany-pat-0.0~git20140625/README.md000066400000000000000000000037761235265756700217240ustar00rootroot00000000000000# pat (formerly pat.go) - A Sinatra style pattern muxer for Go's net/http library ## INSTALL $ go get github.com/bmizerany/pat ## USE package main import ( "io" "net/http" "github.com/bmizerany/pat" "log" ) // hello world, the web server func HelloServer(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n") } func main() { m := pat.New() m.Get("/hello/:name", http.HandlerFunc(HelloServer)) // Register this pat with the default serve mux so that other packages // may also be exported. (i.e. /debug/pprof/*) http.Handle("/", m) err := http.ListenAndServe(":12345", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } It's that simple. For more information, see: http://godoc.org/github.com/bmizerany/pat ## CONTRIBUTORS * Keith Rarick (@krarick) - github.com/kr * Blake Mizerany (@bmizerany) - github.com/bmizerany * Evan Shaw * George Rogers ## LICENSE Copyright (C) 2012 by Keith Rarick, Blake Mizerany Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-bmizerany-pat-0.0~git20140625/bench_test.go000066400000000000000000000005701235265756700230770ustar00rootroot00000000000000package pat import ( "net/http" "testing" ) func BenchmarkPatternMatching(b *testing.B) { p := New() p.Get("/hello/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) for n := 0; n < b.N; n++ { b.StopTimer() r, err := http.NewRequest("GET", "/hello/blake", nil) if err != nil { panic(err) } b.StartTimer() p.ServeHTTP(nil, r) } } golang-github-bmizerany-pat-0.0~git20140625/example/000077500000000000000000000000001235265756700220635ustar00rootroot00000000000000golang-github-bmizerany-pat-0.0~git20140625/example/hello.go000066400000000000000000000010701235265756700235130ustar00rootroot00000000000000package main import ( "io" "log" "net/http" "github.com/bmizerany/pat" ) // hello world, the web server func HelloServer(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n") } func main() { m := pat.New() m.Get("/hello/:name", http.HandlerFunc(HelloServer)) // Register this pat with the default serve mux so that other packages // may also be exported. (i.e. /debug/pprof/*) http.Handle("/", m) err := http.ListenAndServe(":12345", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } golang-github-bmizerany-pat-0.0~git20140625/example/patexample/000077500000000000000000000000001235265756700242235ustar00rootroot00000000000000golang-github-bmizerany-pat-0.0~git20140625/example/patexample/hello_appengine.go000066400000000000000000000014461235265756700277100ustar00rootroot00000000000000// hello.go ported for appengine // // this differs from the standard hello.go example in two ways: appengine // already provides an http server for you, obviating the need for the // ListenAndServe call (with associated logging), and the package must not be // called main (appengine reserves package 'main' for the underlying program). package patexample import ( "io" "net/http" "github.com/bmizerany/pat" ) // hello world, the web server func HelloServer(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n") } func init() { m := pat.New() m.Get("/hello/:name", http.HandlerFunc(HelloServer)) // Register this pat with the default serve mux so that other packages // may also be exported. (i.e. /debug/pprof/*) http.Handle("/", m) } golang-github-bmizerany-pat-0.0~git20140625/mux.go000066400000000000000000000153761235265756700216040ustar00rootroot00000000000000// Package pat implements a simple URL pattern muxer package pat import ( "net/http" "net/url" "strings" ) // PatternServeMux is an HTTP request multiplexer. It matches the URL of each // incoming request against a list of registered patterns with their associated // methods and calls the handler for the pattern that most closely matches the // URL. // // Pattern matching attempts each pattern in the order in which they were // registered. // // Patterns may contain literals or captures. Capture names start with a colon // and consist of letters A-Z, a-z, _, and 0-9. The rest of the pattern // matches literally. The portion of the URL matching each name ends with an // occurrence of the character in the pattern immediately following the name, // or a /, whichever comes first. It is possible for a name to match the empty // string. // // Example pattern with one capture: // /hello/:name // Will match: // /hello/blake // /hello/keith // Will not match: // /hello/blake/ // /hello/blake/foo // /foo // /foo/bar // // Example 2: // /hello/:name/ // Will match: // /hello/blake/ // /hello/keith/foo // /hello/blake // /hello/keith // Will not match: // /foo // /foo/bar // // A pattern ending with a slash will get an implicit redirect to it's // non-slash version. For example: Get("/foo/", handler) will implicitly // register Get("/foo", handler). You may override it by registering // Get("/foo", anotherhandler) before the slash version. // // Retrieve the capture from the r.URL.Query().Get(":name") in a handler (note // the colon). If a capture name appears more than once, the additional values // are appended to the previous values (see // http://golang.org/pkg/net/url/#Values) // // A trivial example server is: // // package main // // import ( // "io" // "net/http" // "github.com/bmizerany/pat" // "log" // ) // // // hello world, the web server // func HelloServer(w http.ResponseWriter, req *http.Request) { // io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n") // } // // func main() { // m := pat.New() // m.Get("/hello/:name", http.HandlerFunc(HelloServer)) // // // Register this pat with the default serve mux so that other packages // // may also be exported. (i.e. /debug/pprof/*) // http.Handle("/", m) // err := http.ListenAndServe(":12345", nil) // if err != nil { // log.Fatal("ListenAndServe: ", err) // } // } // // When "Method Not Allowed": // // Pat knows what methods are allowed given a pattern and a URI. For // convenience, PatternServeMux will add the Allow header for requests that // match a pattern for a method other than the method requested and set the // Status to "405 Method Not Allowed". type PatternServeMux struct { handlers map[string][]*patHandler } // New returns a new PatternServeMux. func New() *PatternServeMux { return &PatternServeMux{make(map[string][]*patHandler)} } // ServeHTTP matches r.URL.Path against its routing table using the rules // described above. func (p *PatternServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, ph := range p.handlers[r.Method] { if params, ok := ph.try(r.URL.Path); ok { if len(params) > 0 { r.URL.RawQuery = url.Values(params).Encode() + "&" + r.URL.RawQuery } ph.ServeHTTP(w, r) return } } allowed := make([]string, 0, len(p.handlers)) for meth, handlers := range p.handlers { if meth == r.Method { continue } for _, ph := range handlers { if _, ok := ph.try(r.URL.Path); ok { allowed = append(allowed, meth) } } } if len(allowed) == 0 { http.NotFound(w, r) return } w.Header().Add("Allow", strings.Join(allowed, ", ")) http.Error(w, "Method Not Allowed", 405) } // Head will register a pattern with a handler for HEAD requests. func (p *PatternServeMux) Head(pat string, h http.Handler) { p.Add("HEAD", pat, h) } // Get will register a pattern with a handler for GET requests. // It also registers pat for HEAD requests. If this needs to be overridden, use // Head before Get with pat. func (p *PatternServeMux) Get(pat string, h http.Handler) { p.Add("HEAD", pat, h) p.Add("GET", pat, h) } // Post will register a pattern with a handler for POST requests. func (p *PatternServeMux) Post(pat string, h http.Handler) { p.Add("POST", pat, h) } // Put will register a pattern with a handler for PUT requests. func (p *PatternServeMux) Put(pat string, h http.Handler) { p.Add("PUT", pat, h) } // Del will register a pattern with a handler for DELETE requests. func (p *PatternServeMux) Del(pat string, h http.Handler) { p.Add("DELETE", pat, h) } // Options will register a pattern with a handler for OPTIONS requests. func (p *PatternServeMux) Options(pat string, h http.Handler) { p.Add("OPTIONS", pat, h) } // Add will register a pattern with a handler for meth requests. func (p *PatternServeMux) Add(meth, pat string, h http.Handler) { p.handlers[meth] = append(p.handlers[meth], &patHandler{pat, h}) n := len(pat) if n > 0 && pat[n-1] == '/' { p.Add(meth, pat[:n-1], http.RedirectHandler(pat, http.StatusMovedPermanently)) } } // Tail returns the trailing string in path after the final slash for a pat ending with a slash. // // Examples: // // Tail("/hello/:title/", "/hello/mr/mizerany") == "mizerany" // Tail("/:a/", "/x/y/z") == "y/z" // func Tail(pat, path string) string { var i, j int for i < len(path) { switch { case j >= len(pat): if pat[len(pat)-1] == '/' { return path[i:] } return "" case pat[j] == ':': var nextc byte _, nextc, j = match(pat, isAlnum, j+1) _, _, i = match(path, matchPart(nextc), i) case path[i] == pat[j]: i++ j++ default: return "" } } return "" } type patHandler struct { pat string http.Handler } func (ph *patHandler) try(path string) (url.Values, bool) { p := make(url.Values) var i, j int for i < len(path) { switch { case j >= len(ph.pat): if ph.pat != "/" && len(ph.pat) > 0 && ph.pat[len(ph.pat)-1] == '/' { return p, true } return nil, false case ph.pat[j] == ':': var name, val string var nextc byte name, nextc, j = match(ph.pat, isAlnum, j+1) val, _, i = match(path, matchPart(nextc), i) p.Add(":"+name, val) case path[i] == ph.pat[j]: i++ j++ default: return nil, false } } if j != len(ph.pat) { return nil, false } return p, true } func matchPart(b byte) func(byte) bool { return func(c byte) bool { return c != b && c != '/' } } func match(s string, f func(byte) bool, i int) (matched string, next byte, j int) { j = i for j < len(s) && f(s[j]) { j++ } if j < len(s) { next = s[j] } return s[i:j], next, j } func isAlpha(ch byte) bool { return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' } func isDigit(ch byte) bool { return '0' <= ch && ch <= '9' } func isAlnum(ch byte) bool { return isAlpha(ch) || isDigit(ch) } golang-github-bmizerany-pat-0.0~git20140625/mux_test.go000066400000000000000000000143261235265756700226350ustar00rootroot00000000000000package pat import ( "net/http" "net/http/httptest" "net/url" "sort" "strings" "testing" "github.com/bmizerany/assert" ) func TestPatMatch(t *testing.T) { params, ok := (&patHandler{"/", nil}).try("/") assert.Equal(t, true, ok) params, ok = (&patHandler{"/", nil}).try("/wrong_url") assert.Equal(t, false, ok) params, ok = (&patHandler{"/foo/:name", nil}).try("/foo/bar") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"bar"}}, params) params, ok = (&patHandler{"/foo/:name/baz", nil}).try("/foo/bar") assert.Equal(t, false, ok) params, ok = (&patHandler{"/foo/:name/bar/", nil}).try("/foo/keith/bar/baz") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"keith"}}, params) params, ok = (&patHandler{"/foo/:name/bar/", nil}).try("/foo/keith/bar/") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"keith"}}, params) params, ok = (&patHandler{"/foo/:name/bar/", nil}).try("/foo/keith/bar") assert.Equal(t, false, ok) params, ok = (&patHandler{"/foo/:name/baz", nil}).try("/foo/bar/baz") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"bar"}}, params) params, ok = (&patHandler{"/foo/:name/baz/:id", nil}).try("/foo/bar/baz") assert.Equal(t, false, ok) params, ok = (&patHandler{"/foo/:name/baz/:id", nil}).try("/foo/bar/baz/123") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"bar"}, ":id": {"123"}}, params) params, ok = (&patHandler{"/foo/:name/baz/:name", nil}).try("/foo/bar/baz/123") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"bar", "123"}}, params) params, ok = (&patHandler{"/foo/:name.txt", nil}).try("/foo/bar.txt") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"bar"}}, params) params, ok = (&patHandler{"/foo/:name", nil}).try("/foo/:bar") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {":bar"}}, params) params, ok = (&patHandler{"/foo/:a:b", nil}).try("/foo/val1:val2") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":a": {"val1"}, ":b": {":val2"}}, params) params, ok = (&patHandler{"/foo/:a.", nil}).try("/foo/.") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":a": {""}}, params) params, ok = (&patHandler{"/foo/:a:b", nil}).try("/foo/:bar") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":a": {""}, ":b": {":bar"}}, params) params, ok = (&patHandler{"/foo/:a:b:c", nil}).try("/foo/:bar") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":a": {""}, ":b": {""}, ":c": {":bar"}}, params) params, ok = (&patHandler{"/foo/::name", nil}).try("/foo/val1:val2") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":": {"val1"}, ":name": {":val2"}}, params) params, ok = (&patHandler{"/foo/:name.txt", nil}).try("/foo/bar/baz.txt") assert.Equal(t, false, ok) params, ok = (&patHandler{"/foo/x:name", nil}).try("/foo/bar") assert.Equal(t, false, ok) params, ok = (&patHandler{"/foo/x:name", nil}).try("/foo/xbar") assert.Equal(t, true, ok) assert.Equal(t, url.Values{":name": {"bar"}}, params) } func TestPatRoutingHit(t *testing.T) { p := New() var ok bool p.Get("/foo/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ok = true t.Logf("%#v", r.URL.Query()) assert.Equal(t, "keith", r.URL.Query().Get(":name")) })) r, err := http.NewRequest("GET", "/foo/keith?a=b", nil) if err != nil { t.Fatal(err) } p.ServeHTTP(nil, r) assert.T(t, ok) } func TestPatRoutingMethodNotAllowed(t *testing.T) { p := New() var ok bool p.Post("/foo/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ok = true })) p.Put("/foo/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ok = true })) r, err := http.NewRequest("GET", "/foo/keith", nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() p.ServeHTTP(rr, r) assert.T(t, !ok) assert.Equal(t, http.StatusMethodNotAllowed, rr.Code) allowed := strings.Split(rr.Header().Get("Allow"), ", ") sort.Strings(allowed) assert.Equal(t, allowed, []string{"POST", "PUT"}) } // Check to make sure we don't pollute the Raw Query when we have no parameters func TestPatNoParams(t *testing.T) { p := New() var ok bool p.Get("/foo/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ok = true t.Logf("%#v", r.URL.RawQuery) assert.Equal(t, "", r.URL.RawQuery) })) r, err := http.NewRequest("GET", "/foo/", nil) if err != nil { t.Fatal(err) } p.ServeHTTP(nil, r) assert.T(t, ok) } // Check to make sure we don't pollute the Raw Query when there are parameters but no pattern variables func TestPatOnlyUserParams(t *testing.T) { p := New() var ok bool p.Get("/foo/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ok = true t.Logf("%#v", r.URL.RawQuery) assert.Equal(t, "a=b", r.URL.RawQuery) })) r, err := http.NewRequest("GET", "/foo/?a=b", nil) if err != nil { t.Fatal(err) } p.ServeHTTP(nil, r) assert.T(t, ok) } func TestPatImplicitRedirect(t *testing.T) { p := New() p.Get("/foo/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) r, err := http.NewRequest("GET", "/foo", nil) if err != nil { t.Fatal(err) } res := httptest.NewRecorder() p.ServeHTTP(res, r) if res.Code != 301 { t.Errorf("expected Code 301, was %d", res.Code) } if loc := res.Header().Get("Location"); loc != "/foo/" { t.Errorf("expected %q, got %q", "/foo/", loc) } p = New() p.Get("/foo", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) p.Get("/foo/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) r, err = http.NewRequest("GET", "/foo", nil) if err != nil { t.Fatal(err) } res = httptest.NewRecorder() res.Code = 200 p.ServeHTTP(res, r) if res.Code != 200 { t.Errorf("expected Code 200, was %d", res.Code) } } func TestTail(t *testing.T) { for i, test := range []struct { pat string path string expect string }{ {"/:a/", "/x/y/z", "y/z"}, {"/:a/", "/x", ""}, {"/:a/", "/x/", ""}, {"/:a", "/x/y/z", ""}, {"/b/:a", "/x/y/z", ""}, {"/hello/:title/", "/hello/mr/mizerany", "mizerany"}, {"/:a/", "/x/y/z", "y/z"}, } { tail := Tail(test.pat, test.path) if tail != test.expect { t.Errorf("failed test %d: Tail(%q, %q) == %q (!= %q)", i, test.pat, test.path, tail, test.expect) } } }