Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
32ae3a3d0d | |||
32e7468eef | |||
3e6d03db1a | |||
c237f8c566 | |||
a23264b00f | |||
b4163d2162 | |||
5d613b34ee | |||
cc2cd72df8 |
65
httpr.go
65
httpr.go
@ -9,9 +9,14 @@ import (
|
||||
|
||||
type path []string
|
||||
|
||||
// newPath splits a path and ensures that it starts with a slash (/) and doesn't
|
||||
// have more than 1 catch-all parameter.
|
||||
// newPath ensures that a path provided is correct and splits it.
|
||||
func newPath(path string) (path, error) {
|
||||
pathLen := len(path)
|
||||
|
||||
if pathLen == 0 {
|
||||
return nil, errors.New("empty path is not allowed")
|
||||
}
|
||||
|
||||
if path[0] != '/' {
|
||||
return nil, errors.New("path should start with a slash symbol \"/\"")
|
||||
}
|
||||
@ -20,9 +25,31 @@ func newPath(path string) (path, error) {
|
||||
return nil, errors.New("path can have only one catch-all parameter \"*\"")
|
||||
}
|
||||
|
||||
if path[pathLen-1] == '/' {
|
||||
path = path[:pathLen-1]
|
||||
}
|
||||
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
parts[0] = "/"
|
||||
|
||||
return parts, nil
|
||||
}
|
||||
|
||||
// newServePath is a reduced version of newPath for ServeHTTP.
|
||||
func newServePath(path string) (path, error) {
|
||||
if path[0] != '/' {
|
||||
return nil, errors.New("path should start with a slash symbol \"/\"")
|
||||
}
|
||||
|
||||
path = strings.ReplaceAll(path, "//", "/")
|
||||
|
||||
parts := strings.Split(strings.TrimSuffix(path, "/"), "/")
|
||||
pathLen := len(path)
|
||||
if path[pathLen-1] == '/' {
|
||||
path = path[:pathLen-1]
|
||||
}
|
||||
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
parts[0] = "/"
|
||||
|
||||
@ -62,7 +89,9 @@ outer:
|
||||
path[i] = curNode.endpoint + ":" + path[i]
|
||||
}
|
||||
|
||||
if pathLen == i+1 {
|
||||
pathNextIdx := i + 1
|
||||
|
||||
if pathLen == pathNextIdx {
|
||||
var params Params = make(Params)
|
||||
|
||||
for _, part := range path {
|
||||
@ -75,14 +104,15 @@ outer:
|
||||
return curNode.handler, params
|
||||
}
|
||||
|
||||
if pathLen > i+1 {
|
||||
var paramNode *node
|
||||
if pathLen > pathNextIdx {
|
||||
if len(curNode.children) == 0 {
|
||||
break outer
|
||||
}
|
||||
|
||||
var paramNode *node
|
||||
|
||||
for _, next := range curNode.children {
|
||||
if next.endpoint == path[i+1] {
|
||||
if next.endpoint == path[pathNextIdx] {
|
||||
curNode = next
|
||||
continue outer
|
||||
}
|
||||
@ -105,12 +135,12 @@ outer:
|
||||
}
|
||||
|
||||
func (n *node) add(path path, handler http.HandlerFunc) error {
|
||||
pathLen := len(path)
|
||||
pathLastIdx := len(path) - 1
|
||||
curNode := n
|
||||
|
||||
outer:
|
||||
for i := range path {
|
||||
if pathLen == i+1 {
|
||||
if pathLastIdx == i {
|
||||
if curNode.handler != nil {
|
||||
return errors.New("attempt to redefine a handler for already existing path")
|
||||
}
|
||||
@ -119,27 +149,29 @@ outer:
|
||||
return nil
|
||||
}
|
||||
|
||||
pathNextIdx := i + 1
|
||||
|
||||
for _, child := range curNode.children {
|
||||
firstChar := path[i+1][0]
|
||||
firstChar := path[pathNextIdx][0]
|
||||
if (firstChar == ':' || firstChar == '*') && firstChar == child.endpoint[0] {
|
||||
// Do not allow different param names, because only the first one
|
||||
// is saved, so a param won't be available by a new name.
|
||||
// Therefore, it is good to return an error because in this case
|
||||
// you're doing something wrong.
|
||||
if path[i+1] != child.endpoint {
|
||||
return errors.New("param names " + path[i+1] + " and " + child.endpoint + " are differ")
|
||||
if path[pathNextIdx] != child.endpoint {
|
||||
return errors.New("param names " + path[pathNextIdx] + " and " + child.endpoint + " are differ")
|
||||
}
|
||||
curNode = child
|
||||
continue outer
|
||||
}
|
||||
|
||||
if child.endpoint == path[i+1] {
|
||||
if child.endpoint == path[pathNextIdx] {
|
||||
curNode = child
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
|
||||
newChild := &node{endpoint: path[i+1]}
|
||||
newChild := &node{endpoint: path[pathNextIdx]}
|
||||
curNode.children = append(curNode.children, newChild)
|
||||
curNode = newChild
|
||||
}
|
||||
@ -159,11 +191,11 @@ func New() *Router {
|
||||
func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
tree, ok := rr.tree[r.Method]
|
||||
if !ok {
|
||||
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
||||
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
path, err := newPath(r.URL.Path)
|
||||
path, err := newServePath(r.URL.Path)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotAcceptable)
|
||||
return
|
||||
@ -184,6 +216,7 @@ func (rr *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Handler registers a handler for provided pattern for a given HTTP method.
|
||||
// Pattern must start with a slash (/) symbol.
|
||||
func (rr *Router) Handler(method, pattern string, handler http.HandlerFunc) error {
|
||||
path, err := newPath(pattern)
|
||||
if err != nil {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package httpr_test
|
||||
package httpr
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@ -6,12 +6,10 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.arav.su/Arav/httpr"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) {
|
||||
r := httpr.New()
|
||||
r := New()
|
||||
|
||||
err := r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {})
|
||||
if err != nil {
|
||||
@ -53,7 +51,7 @@ func Test(t *testing.T) {
|
||||
func TestPaths(t *testing.T) {
|
||||
found := false
|
||||
|
||||
r := httpr.New()
|
||||
r := New()
|
||||
|
||||
err := r.Handler(http.MethodGet, "/:lel", func(w http.ResponseWriter, r *http.Request) { found = true })
|
||||
if err != nil {
|
||||
@ -88,7 +86,7 @@ func TestPaths(t *testing.T) {
|
||||
func TestSubPaths(t *testing.T) {
|
||||
found := true
|
||||
|
||||
r := httpr.New()
|
||||
r := New()
|
||||
|
||||
s := r.Sub("/api/v1")
|
||||
|
||||
@ -126,3 +124,33 @@ func TestSubPaths(t *testing.T) {
|
||||
t.Error(found, "Path", p, "should return 404")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathParsing(t *testing.T) {
|
||||
p, err := newPath("/api/v1/../.")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log(p)
|
||||
}
|
||||
|
||||
const testStr = "/api/v1/foo/bar/baz/abc/def/fucc/b0y/of/a/local/dungeon/master/got/his/ass/fisted/for/free/and/he/was/absolutely/happy/with/that/"
|
||||
|
||||
func BenchmarkPatternPathParsing(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
p, err := newPath(testStr)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.Log(len(p))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkServePathParsing(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
p, err := newServePath(testStr)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.Log(len(p))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user