-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.go
213 lines (192 loc) · 4.83 KB
/
uri.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package urip
import (
"errors"
abnfp "github.com/um7a/abnf-parser"
)
type Uri struct {
Scheme []byte
DoubleSlash []byte // part of hier-part
UserInfo []byte // part of hier-part
AtSign []byte // part of hier-part
Host []byte // part of hier-part
Port []byte // part of hier-part
Path []byte // part of hier-part
Question []byte
Query []byte
Sharp []byte
Fragment []byte
}
func Parse(data []byte) (uri *Uri, err error) {
// RFC3986 - 3. Syntax Components
//
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
//
// RFC3986 - 3.2. Authority
//
// authority = [ userinfo "@" ] host [ ":" port ]
//
uri = new(Uri)
// RFC3986 - 3.1. Scheme
//
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
//
parsed, remaining := abnfp.Parse(data, NewSchemeFinder())
if len(parsed) == 0 {
return nil, errors.New("scheme not found.")
}
uri.Scheme = parsed
// ":"
found, end := abnfp.NewByteFinder(':').Find(remaining)
if !found {
return nil, errors.New("colon following scheme not found.")
}
remaining = remaining[end:]
// RFC3986 - 3. Syntax Components
//
// hier-part = "//" authority path-abempty
// / path-absolute
// / path-rootless
// / path-empty
//
var hierPartRemaining []byte
parsed, remaining = abnfp.Parse(remaining, NewHierPartFinder())
if len(parsed) >= 2 && parsed[0] == '/' && parsed[1] == '/' {
// hier-part = "//" authority path-abempty
// "//"
uri.DoubleSlash = parsed[:2]
hierPartRemaining = parsed[2:]
// RFC3986 - 3.2. Authority
//
// authority = [ userinfo "@" ] host [ ":" port ]
//
// [ userinfo "@" ]
parsed, hierPartRemaining = abnfp.Parse(
hierPartRemaining,
abnfp.NewOptionalSequenceFinder(
abnfp.NewConcatenationFinder([]abnfp.Finder{
NewUserInfoFinder(),
abnfp.NewByteFinder('@'),
}),
))
if len(parsed) > 0 {
uri.UserInfo = parsed[:len(parsed)-1]
uri.AtSign = []byte("@")
}
// RFC3986 - 3.2.2. Host
//
// host = IP-literal / IPv4address / reg-name
//
parsed, hierPartRemaining = abnfp.Parse(hierPartRemaining, NewHostFinder())
if len(parsed) > 0 {
uri.Host = parsed
}
// [ ":" port ]
parsed, hierPartRemaining = abnfp.Parse(
hierPartRemaining,
abnfp.NewOptionalSequenceFinder(
abnfp.NewConcatenationFinder([]abnfp.Finder{
abnfp.NewByteFinder(':'),
NewPortFinder(),
}),
))
if len(parsed) > 0 {
uri.Port = parsed[1:]
}
// RFC3986 - 3.3. Path
//
// path-abempty = *( "/" segment )
//
parsed, hierPartRemaining = abnfp.Parse(hierPartRemaining, NewPathAbemptyFinder())
if len(parsed) > 0 {
uri.Path = parsed
}
} else if len(parsed) >= 1 && parsed[0] == '/' {
// hier-part = path-absolute
// RFC3986 - 3.3. Path
//
// path-absolute = "/" [ segment-nz *( "/" segment ) ]
//
parsed, hierPartRemaining = abnfp.Parse(parsed, NewPathAbsoluteFinder())
if len(parsed) == 0 {
return nil, errors.New("path-absolute not found.")
}
uri.Path = parsed
} else if len(parsed) > 0 {
// hier-part = path-rootless
// RFC3986 - 3.3. Path
//
// path-rootless = segment-nz *( "/" segment )
//
parsed, hierPartRemaining = abnfp.Parse(parsed, NewPathRootlessFinder())
if len(parsed) == 0 {
return nil, errors.New("path-rootless not found.")
}
uri.Path = parsed
} else {
// hier-part = path-empty
// do nothing.
}
// [ "?" query ]
parsed, remaining = abnfp.Parse(remaining, abnfp.NewOptionalSequenceFinder(
abnfp.NewConcatenationFinder([]abnfp.Finder{
abnfp.NewByteFinder('?'),
NewQueryFinder(),
}),
))
if len(parsed) > 0 {
uri.Question = []byte("?")
uri.Query = parsed[1:]
}
// [ "#" fragment ]
parsed, remaining = abnfp.Parse(remaining, abnfp.NewOptionalSequenceFinder(
abnfp.NewConcatenationFinder([]abnfp.Finder{
abnfp.NewByteFinder('#'),
NewFragmentFinder(),
}),
))
if len(parsed) > 0 {
uri.Sharp = []byte("#")
uri.Fragment = parsed[1:]
}
return uri, nil
}
func (uri *Uri) String() string {
// RFC3986 - 3. Syntax Components
//
// URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
//
// RFC3986 - 3. Syntax Components
//
// hier-part = "//" authority path-abempty
// / path-absolute
// / path-rootless
// / path-empty
//
str := string(uri.Scheme)
str += ":"
if len(uri.DoubleSlash) > 0 {
str += string(uri.DoubleSlash)
str += uri.GetAuthority()
}
str += string(uri.Path)
str += string(uri.Question)
str += string(uri.Query)
str += string(uri.Sharp)
str += string(uri.Fragment)
return str
}
func (uri *Uri) GetAuthority() string {
// RFC3986 - 3.2. Authority
//
// authority = [ userinfo "@" ] host [ ":" port ]
//
var str string
str += string(uri.UserInfo)
str += string(uri.AtSign)
str += string(uri.Host)
if len(uri.Port) > 0 {
str += ":"
str += string(uri.Port)
}
return str
}