-
Notifications
You must be signed in to change notification settings - Fork 2
/
urltable.go
169 lines (138 loc) · 3.42 KB
/
urltable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package urltable
import (
"net/http"
"strings"
"github.com/bluekaki/pkg/errors"
)
const (
empty = ""
fuzzy = "*"
omitted = "**"
delimiter = "/"
methodView = "VIEW"
)
// parse and validate pattern
func parse(pattern string) ([]string, error) {
const format = "[get, post, put, patch, delete, view]/{a-Z}+/{*}+/{**}"
if pattern = strings.TrimLeft(strings.TrimSpace(pattern), delimiter); pattern == "" {
return nil, errors.Errorf("pattern illegal, should in format of %s", format)
}
paths := strings.Split(pattern, delimiter)
if len(paths) < 2 {
return nil, errors.Errorf("pattern illegal, should in format of %s", format)
}
for i := range paths {
paths[i] = strings.TrimSpace(paths[i])
}
// likes get/ get/* get/**
if len(paths) == 2 && (paths[1] == empty || paths[1] == fuzzy || paths[1] == omitted) {
return nil, errors.New("illegal wildcard")
}
switch paths[0] = strings.ToUpper(paths[0]); paths[0] {
case http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
methodView:
default:
return nil, errors.Errorf("only supports [%s %s %s %s %s %s]",
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, methodView)
}
for k := 1; k < len(paths); k++ {
if paths[k] == empty && k+1 != len(paths) {
return nil, errors.New("pattern contains illegal empty path")
}
if paths[k] == omitted && k+1 != len(paths) {
return nil, errors.New("pattern contains illegal omitted path")
}
}
return paths, nil
}
// Format pattern
func Format(pattern string) (string, error) {
paths, err := parse(pattern)
if err != nil {
return "", err
}
return strings.Join(paths, delimiter), nil
}
type section struct {
leaf bool
mapping map[string]*section
}
func newSection() *section {
return §ion{mapping: make(map[string]*section)}
}
// Table a (thread unsafe) table to store urls
type Table struct {
size int
root *section
}
// NewTable create a table
func NewTable() *Table {
return &Table{root: newSection()}
}
// Size contains how many urls
func (t *Table) Size() int {
return t.size
}
// Append pattern
func (t *Table) Append(pattern string) error {
paths, err := parse(pattern)
if err != nil {
return err
}
insert := false
root := t.root
for i, path := range paths {
if (path == fuzzy && root.mapping[omitted] != nil) ||
(path == omitted && root.mapping[fuzzy] != nil) ||
(path != omitted && root.mapping[omitted] != nil) {
return errors.Errorf("conflict at %s", strings.Join(paths[:i], delimiter))
}
next := root.mapping[path]
if next == nil {
next = newSection()
root.mapping[path] = next
insert = true
}
root = next
}
if insert {
t.size++
}
root.leaf = true
return nil
}
// Mapping url to pattern
func (t *Table) Mapping(url string) (string, error) {
paths, err := parse(url)
if err != nil {
return "", err
}
pattern := make([]string, 0, len(paths))
root := t.root
for _, path := range paths {
next := root.mapping[path]
if next == nil {
nextFuzzy := root.mapping[fuzzy]
nextOmitted := root.mapping[omitted]
if nextFuzzy == nil && nextOmitted == nil {
return "", nil
}
if nextOmitted != nil {
pattern = append(pattern, omitted)
return strings.Join(pattern, delimiter), nil
}
next = nextFuzzy
path = fuzzy
}
root = next
pattern = append(pattern, path)
}
if root.leaf {
return strings.Join(pattern, delimiter), nil
}
return "", nil
}