-
Notifications
You must be signed in to change notification settings - Fork 458
/
url.go
278 lines (237 loc) · 7.69 KB
/
url.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2020, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package base
import (
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
)
// 见单元测试
// TODO chef: 考虑部分内容移入naza中
const (
DefaultRtmpPort = 1935
DefaultHttpPort = 80
DefaultHttpsPort = 443
DefaultRtspPort = 554
DefaultRtmpsPort = 443
DefaultRtspsPort = 322
)
type UrlPathContext struct {
PathWithRawQuery string
Path string
PathWithoutLastItem string // 注意,没有前面的'/',也没有后面的'/'
LastItemOfPath string // 注意,没有前面的'/'
RawQuery string
}
type UrlContext struct {
Url string
Scheme string
Username string
Password string
StdHost string // host or host:port
HostWithPort string // 当原始url中不包含port时,填充scheme对应的默认port
Host string // 不包含port
Port int // 当原始url中不包含port时,填充scheme对应的默认port
//UrlPathContext
PathWithRawQuery string // 注意,有前面的'/'
Path string // 注意,有前面的'/'
PathWithoutLastItem string // 注意,没有前面的'/',也没有后面的'/'
LastItemOfPath string // 注意,没有前面的'/'
RawQuery string // 参数,注意,没有前面的'?'
RawUrlWithoutUserInfo string
filenameWithoutType string
fileType string
}
func (u *UrlContext) GetFilenameWithoutType() string {
u.calcFilenameAndTypeIfNeeded()
return u.filenameWithoutType
}
func (u *UrlContext) GetFileType() string {
u.calcFilenameAndTypeIfNeeded()
return u.fileType
}
func (u *UrlContext) calcFilenameAndTypeIfNeeded() {
if len(u.filenameWithoutType) == 0 || len(u.fileType) == 0 {
if index := strings.LastIndex(u.LastItemOfPath, "."); index != -1 {
u.filenameWithoutType = u.LastItemOfPath[:index]
u.fileType = u.LastItemOfPath[index+1:]
}
}
}
// ---------------------------------------------------------------------------------------------------------------------
// ParseUrl
//
// @param defaultPort:
// 注意,如果rawUrl中显示指定了端口,则该参数不生效。
// 注意,如果设置为-1,内部依然会对常见协议(http, https, rtmp, rtsp)设置官方默认端口。
func ParseUrl(rawUrl string, defaultPort int) (ctx UrlContext, err error) {
ctx.Url = rawUrl
stdUrl, err := url.Parse(rawUrl)
if err != nil {
return ctx, err
}
if stdUrl.Scheme == "" {
return ctx, fmt.Errorf("%w. url=%s", ErrInvalidUrl, rawUrl)
}
// 如果不存在,则设置默认的
if defaultPort == -1 {
// TODO(chef): 测试大小写的情况
switch stdUrl.Scheme {
case "http":
defaultPort = DefaultHttpPort
case "https":
defaultPort = DefaultHttpsPort
case "rtmp":
defaultPort = DefaultRtmpPort
case "rtsp":
defaultPort = DefaultRtspPort
case "rtmps":
defaultPort = DefaultRtmpsPort
case "rtsps":
defaultPort = DefaultRtspsPort
}
}
ctx.Scheme = stdUrl.Scheme
ctx.StdHost = stdUrl.Host
ctx.Username = stdUrl.User.Username()
ctx.Password, _ = stdUrl.User.Password()
h, p, err := net.SplitHostPort(stdUrl.Host)
if err != nil {
// url中端口不存在
ctx.Host = stdUrl.Host
if defaultPort == -1 {
ctx.HostWithPort = stdUrl.Host
} else {
ctx.HostWithPort = net.JoinHostPort(stdUrl.Host, fmt.Sprintf("%d", defaultPort))
ctx.Port = defaultPort
}
} else {
// 端口存在
ctx.Port, err = strconv.Atoi(p)
if err != nil {
return ctx, err
}
ctx.Host = h
ctx.HostWithPort = stdUrl.Host
}
pathCtx, err := parseUrlPath(stdUrl)
if err != nil {
return ctx, err
}
ctx.PathWithRawQuery = pathCtx.PathWithRawQuery
ctx.Path = pathCtx.Path
ctx.PathWithoutLastItem = pathCtx.PathWithoutLastItem
ctx.LastItemOfPath = pathCtx.LastItemOfPath
ctx.RawQuery = pathCtx.RawQuery
ctx.RawUrlWithoutUserInfo = fmt.Sprintf("%s://%s%s", ctx.Scheme, ctx.StdHost, ctx.PathWithRawQuery)
return ctx, nil
}
// ---------------------------------------------------------------------------------------------------------------------
func ParseRtmpUrl(rawUrl string) (ctx UrlContext, err error) {
ctx, err = ParseUrl(rawUrl, -1)
if err != nil {
return
}
if ctx.Scheme != "rtmp" && ctx.Scheme != "rtmps" || ctx.Host == "" || ctx.Path == "" {
return ctx, fmt.Errorf("%w. url=%s", ErrInvalidUrl, rawUrl)
}
// 处理特殊case,具体见 testParseRtmpUrlCase1
// 注意,使用ffmpeg推流时,会把`rtmp://127.0.0.1/test110`中的test110作为appName(streamName则为空)
// 这种其实已不算十分合法的rtmp url了
// 我们这里也处理一下,和ffmpeg保持一致
if ctx.PathWithoutLastItem == "" && ctx.LastItemOfPath != "" {
tmp := ctx.PathWithoutLastItem
ctx.PathWithoutLastItem = ctx.LastItemOfPath
ctx.LastItemOfPath = tmp
}
// 处理特殊case, 具体见 testParseRtmpUrlCase2
//
// PathWithRawQuery:/vyun?vhost=thirdVhost?token=88F4/lss_7
//
// Path:/vyun-----------------------------------------------> /vyun?vhost=thirdVhost?token=88F4/lss_7
// PathWithoutLastItem:vyun---------------------------------> vyun?vhost=thirdVhost?token=88F4
// LastItemOfPath:------------------------------------------> lss_7
// RawQuery:vhost=thirdVhost?token=88F4/lss_7---------------> 空
//
if strings.Count(ctx.PathWithRawQuery, "?") > 1 {
index := strings.LastIndexByte(ctx.PathWithRawQuery, '/')
ctx.Path = ctx.PathWithRawQuery
ctx.PathWithoutLastItem = ctx.PathWithRawQuery[1:index]
ctx.LastItemOfPath = ctx.PathWithRawQuery[index+1:]
ctx.RawQuery = ""
}
return
}
func ParseRtspUrl(rawUrl string) (ctx UrlContext, err error) {
ctx, err = ParseUrl(rawUrl, -1)
if err != nil {
return
}
// 注意,存在一种情况,使用rtsp pull session,直接拉取没有url path的流,所以不检查ctx.Path
if (ctx.Scheme != "rtsp" && ctx.Scheme != "rtsps") || ctx.Host == "" {
return ctx, fmt.Errorf("%w. url=%s", ErrInvalidUrl, rawUrl)
}
return
}
func ParseHttpflvUrl(rawUrl string) (ctx UrlContext, err error) {
return parseHttpUrl(rawUrl, ".flv")
}
// ---------------------------------------------------------------------------------------------------------------------
// ParseHttpRequest
//
// @return 完整url
func ParseHttpRequest(req *http.Request) string {
// TODO(chef): [refactor] scheme是否能从从req.URL.Scheme获取
var scheme string
if req.TLS == nil {
scheme = "http"
} else {
scheme = "https"
}
return fmt.Sprintf("%s://%s%s", scheme, req.Host, req.RequestURI)
}
// ----- private -------------------------------------------------------------------------------------------------------
func parseUrlPath(stdUrl *url.URL) (ctx UrlPathContext, err error) {
ctx.Path = stdUrl.Path
index := strings.LastIndexByte(ctx.Path, '/')
if index == -1 {
ctx.PathWithoutLastItem = ""
ctx.LastItemOfPath = ""
} else if index == 0 {
if ctx.Path == "/" {
ctx.PathWithoutLastItem = ""
ctx.LastItemOfPath = ""
} else {
ctx.PathWithoutLastItem = ""
ctx.LastItemOfPath = ctx.Path[1:]
}
} else {
ctx.PathWithoutLastItem = ctx.Path[1:index]
ctx.LastItemOfPath = ctx.Path[index+1:]
}
ctx.RawQuery = stdUrl.RawQuery
if ctx.RawQuery == "" {
ctx.PathWithRawQuery = ctx.Path
} else {
ctx.PathWithRawQuery = fmt.Sprintf("%s?%s", ctx.Path, ctx.RawQuery)
}
return ctx, nil
}
func parseHttpUrl(rawUrl string, filetype string) (ctx UrlContext, err error) {
ctx, err = ParseUrl(rawUrl, -1)
if err != nil {
return
}
if (ctx.Scheme != "http" && ctx.Scheme != "https") || ctx.Host == "" || ctx.Path == "" || !strings.HasSuffix(ctx.LastItemOfPath, filetype) {
return ctx, fmt.Errorf("%w. url=%s", ErrInvalidUrl, rawUrl)
}
return
}