-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_helpers.go
More file actions
203 lines (176 loc) · 4.27 KB
/
Copy pathhttp_helpers.go
File metadata and controls
203 lines (176 loc) · 4.27 KB
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
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"archive/zip"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"unicode"
"mime"
"net/url"
"path/filepath"
"bufio"
)
func contentDisposition(filename string) string {
base := filepath.Base(filename)
return mime.FormatMediaType("attachment", map[string]string{
"filename": base, // quoted-string
"filename*": "UTF-8''" + url.PathEscape(base), // RFC 5987
})
}
func setDownloadHeaders(w http.ResponseWriter, filename string) {
w.Header().Set("Content-Disposition", contentDisposition(filename))
if ext := filepath.Ext(filename); ext != "" {
if mt := mime.TypeByExtension(ext); mt != "" {
w.Header().Set("Content-Type", mt)
}
}
}
// loggingMiddleware logs basic request information for every HTTP request.
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
log.Printf("%s %s%s\t| done in %s, served to %s", r.Method, r.URL.Path, r.URL.RawQuery, time.Since(start), r.RemoteAddr)
})
}
func splitURLs(s string) []string {
scanner := bufio.NewScanner(strings.NewReader(s))
seen := map[string]struct{}{}
var out []string
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
for _, rawURL := range strings.Fields(line) {
u, err := url.ParseRequestURI(rawURL)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
log.Printf("skipping invalid URL: %q (err=%v)", rawURL, err)
continue
}
f := u.String()
if _, ok := seen[f]; ok {
continue
}
seen[f] = struct{}{}
out = append(out, f)
}
}
return out
}
// toRelPath trims the downloads root prefix and returns a leading slash path.
func toRelPath(root, abs string) string {
rel, err := filepath.Rel(root, abs)
if err != nil {
return ""
}
// If the path is outside the downloads root, don't leak it.
if rel == "." || strings.HasPrefix(rel, "..") {
return ""
}
if !strings.HasPrefix(rel, string(os.PathSeparator)) {
rel = string(os.PathSeparator) + rel
}
return rel
}
// parameterize creates a URL-safe version of the string, similar to Rails parameterize.
// e.g. "This is my Happy String" -> "this-is-my-happy-string"
func parameterize(s string, fallback string) string {
f := func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
}
parts := strings.FieldsFunc(strings.ToLower(s), f)
res := strings.Join(parts, "-")
if res == "" {
return fallback
}
return res
}
// zip helpers
type zipWriter struct {
zw *zip.Writer
rootPath string
}
func newZipWriter(w http.ResponseWriter, root string) *zipWriter {
return &zipWriter{zw: zip.NewWriter(w), rootPath: root}
}
func (z *zipWriter) AddFile(path string) error {
rel, err := filepath.Rel(z.rootPath, path)
if err != nil {
return err
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = rel
header.Method = zip.Deflate
w, err := z.zw.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(w, f)
return err
}
func (z *zipWriter) Close() error {
return z.zw.Close()
}
func isPublicURL(rawURL string) bool {
u, err := url.Parse(rawURL)
if err != nil {
return false
}
host := u.Hostname()
ips, err := net.LookupIP(host)
if err != nil {
log.Printf("isPublicURL: lookup failed for %s: %v", host, err)
return false
}
if len(ips) == 0 {
return false
}
for _, ip := range ips {
if !isPublicIP(ip) {
return false
}
}
return true
}
func isPublicIP(ip net.IP) bool {
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsUnspecified() {
return false
}
// IPv4 private ranges
if ip4 := ip.To4(); ip4 != nil {
switch {
case ip4[0] == 10:
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return false
case ip4[0] == 192 && ip4[1] == 168:
return false
case ip4[0] == 100 && ip4[1] >= 64 && ip4[1] <= 127: // CGNAT
return false
}
} else if ip6 := ip.To16(); ip6 != nil {
// IPv6 Unique Local Address (ULA) - fc00::/7
if ip6[0]&0xfe == 0xfc {
return false
}
}
return true
}