-
Notifications
You must be signed in to change notification settings - Fork 29
/
uri.go
158 lines (133 loc) · 3.77 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
package toolbox
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
)
//ExtractURIParameters parses URIs to extract {<param>} defined in templateURI from requestURI, it returns extracted parameters and flag if requestURI matched templateURI
func ExtractURIParameters(templateURI, requestURI string) (map[string]string, bool) {
var expectingValue, expectingName bool
var name, value string
var uriParameters = make(map[string]string)
maxLength := len(templateURI) + len(requestURI)
var requestURIIndex, templateURIIndex int
questionMarkPosition := strings.Index(requestURI, "?")
if questionMarkPosition != -1 {
requestURI = string(requestURI[:questionMarkPosition])
}
for k := 0; k < maxLength; k++ {
var requestChar, routingChar string
if requestURIIndex < len(requestURI) {
requestChar = requestURI[requestURIIndex : requestURIIndex+1]
}
if templateURIIndex < len(templateURI) {
routingChar = templateURI[templateURIIndex : templateURIIndex+1]
}
if (!expectingValue && !expectingName) && requestChar == routingChar && routingChar != "" {
requestURIIndex++
templateURIIndex++
continue
}
if routingChar == "}" {
expectingName = false
templateURIIndex++
}
if expectingValue && requestChar == "/" {
expectingValue = false
}
if expectingName && templateURIIndex < len(templateURI) {
name += routingChar
templateURIIndex++
}
if routingChar == "{" {
expectingValue = true
expectingName = true
templateURIIndex++
}
if expectingValue && requestURIIndex < len(requestURI) {
value += requestChar
requestURIIndex++
}
if !expectingValue && !expectingName && len(name) > 0 {
uriParameters[name] = value
name = ""
value = ""
}
}
if len(name) > 0 && len(value) > 0 {
uriParameters[name] = value
}
matched := requestURIIndex == len(requestURI) && templateURIIndex == len(templateURI)
return uriParameters, matched
}
//URLStripPath removes path from URL
func URLStripPath(URL string) string {
protoIndex := strings.Index(URL, "://")
if protoIndex != -1 {
pathIndex := strings.Index(string(URL[protoIndex+3:]), "/")
if pathIndex != -1 {
return string(URL[:protoIndex+3+pathIndex])
}
}
return URL
}
//URLPathJoin joins URL paths
func URLPathJoin(baseURL, path string) string {
if path == "" {
return baseURL
}
if strings.HasPrefix(path, "/") {
return URLStripPath(baseURL) + path
}
if !strings.HasSuffix(baseURL, "/") {
baseURL += "/"
}
return baseURL + path
}
//URLBase returns base URL
func URLBase(URL string) string {
parsedURL, err := url.Parse(URL)
if err != nil || parsedURL.Path == "" {
return URL
}
pathPosition := strings.Index(URL, parsedURL.Path)
if pathPosition == -1 {
return URL
}
return string(URL[:pathPosition])
}
//URLSplit returns URL with parent path and resource name
func URLSplit(URL string) (string, string) {
parsedURL, err := url.Parse(URL)
if err != nil || parsedURL.Path == "" {
return URL, ""
}
splitPosition := strings.LastIndex(parsedURL.Path, "/")
if splitPosition == -1 {
return URL, ""
}
return fmt.Sprintf("%v%v", URLBase(URL), string(parsedURL.Path[:splitPosition])), string(parsedURL.Path[splitPosition+1:])
}
//Filename reformat file name
func Filename(filename string) string {
if strings.Contains(filename, ":/") {
if parsed, err := url.Parse(filename); err == nil {
filename = parsed.Path
}
}
var root = make([]string, 0)
if strings.HasPrefix(filename, "/") {
root = append(root, "/")
}
elements := append(root, strings.Split(filename, "/")...)
filename = filepath.Join(elements...)
return filename
}
//OpenFile open file converting path to elements and rebuling path safety with path.Join
func OpenFile(filename string) (*os.File, error) {
var file = Filename(filename)
var result, err = os.Open(file)
return result, err
}