| 1 | package goji |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "strings" |
| 7 | |
| 8 | "goji.io/internal" |
| 9 | ) |
| 10 | |
| 11 | type boolPattern bool |
| 12 | |
| 13 | func (b boolPattern) Match(r *http.Request) *http.Request { |
| 14 | if b { |
| 15 | return r |
| 16 | } |
| 17 | return nil |
| 18 | } |
| 19 | |
| 20 | type testPattern struct { |
| 21 | index int |
| 22 | mark *int |
| 23 | methods []string |
| 24 | prefix string |
| 25 | } |
| 26 | |
| 27 | func (t testPattern) Match(r *http.Request) *http.Request { |
| 28 | ctx := r.Context() |
| 29 | if t.index < *t.mark { |
| 30 | return nil |
| 31 | } |
| 32 | path := ctx.Value(internal.Path).(string) |
| 33 | if !strings.HasPrefix(path, t.prefix) { |
| 34 | return nil |
| 35 | } |
| 36 | if t.methods != nil { |
| 37 | if _, ok := t.HTTPMethods()[r.Method]; !ok { |
| 38 | return nil |
| 39 | } |
| 40 | } |
| 41 | return r |
| 42 | } |
| 43 | |
| 44 | func (t testPattern) PathPrefix() string { |
| 45 | return t.prefix |
| 46 | } |
| 47 | |
| 48 | func (t testPattern) HTTPMethods() map[string]struct{} { |
| 49 | if t.methods == nil { |
| 50 | return nil |
| 51 | } |
| 52 | m := make(map[string]struct{}) |
| 53 | for _, method := range t.methods { |
| 54 | m[method] = struct{}{} |
| 55 | } |
| 56 | return m |
| 57 | } |
| 58 | |
| 59 | type intHandler int |
| 60 | |
| 61 | func (i intHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 62 | } |
| 63 | |
| 64 | func wr() (*httptest.ResponseRecorder, *http.Request) { |
| 65 | w := httptest.NewRecorder() |
| 66 | r, err := http.NewRequest("GET", "/", nil) |
| 67 | if err != nil { |
| 68 | panic(err) |
| 69 | } |
| 70 | return w, r |
| 71 | } |