This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
142 lines (117 loc) · 3.22 KB
/
handler.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
package main
import (
"context"
"fmt"
"golang.org/x/net/webdav"
"io"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
)
var (
prefix = "/"
datetime = "2006-01-02 15:04:05"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("%s\t%s\n", r.Method, r.URL.Path)
if !authHandler(w, r) {
return
}
handler := webdav.Handler{
Prefix: prefix,
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(data),
}
if r.Method == "GET" {
getHandler(handler, w, r)
return
}
fileDir := filepath.Dir(data + r.URL.Path)
if !pathExists(fileDir) {
_ = os.MkdirAll(fileDir, 0755)
}
checkFileNameLength(r)
go indexHookHandle(cloneHttpRequest(r))
handler.ServeHTTP(w, r)
}
func authHandler(w http.ResponseWriter, r *http.Request) bool {
_username, _password, ok := r.BasicAuth()
if !ok {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
w.WriteHeader(http.StatusUnauthorized)
return false
}
if _username != username || _password != "admin" {
http.Error(w, "WebDAV: need authorized!", http.StatusUnauthorized)
return false
}
return true
}
func getHandler(handler webdav.Handler, w http.ResponseWriter, r *http.Request) {
f, err := handler.FileSystem.OpenFile(context.TODO(), r.URL.Path, os.O_RDONLY, 0664)
if err != nil {
log.Printf("%s\t%s not found\n", r.Method, r.URL.Path)
return
}
defer f.Close()
if n := fileHandler(f, w); n > 0 {
return
}
listHandler(f, w, r)
}
func listHandler(file webdav.File, w http.ResponseWriter, r *http.Request) {
files, err := file.Readdir(-1)
if err != nil {
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = fmt.Fprintf(w, "<html>\n")
_, _ = fmt.Fprintf(w, "<head><title>Index of %s</title></head>\n", r.URL.Path)
_, _ = fmt.Fprintf(w, "<body>\n")
_, _ = fmt.Fprintf(w, "<h1>Index of %s</h1>\n", r.URL.Path)
_, _ = fmt.Fprintf(w, "<hr>\n")
_, _ = fmt.Fprintf(w, "<pre>\n")
_, _ = fmt.Fprintf(w, "<table border=\"0\" width=\"%s\">\n", "90%")
_, _ = fmt.Fprintf(w, "<tr>\n")
_, _ = fmt.Fprintf(w, "<td><a href=\"../\">../</a></td>\n")
_, _ = fmt.Fprintf(w, "<td></td>\n")
_, _ = fmt.Fprintf(w, "<td></td>\n")
_, _ = fmt.Fprintf(w, "</tr>\n")
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().After(files[j].ModTime())
})
for _, d := range files {
name := d.Name()
size := byteCountIEC(d.Size())
if d.IsDir() {
name += "/"
size = "-"
}
_, _ = fmt.Fprintf(w, "<tr>\n")
_, _ = fmt.Fprintf(w, "<td><a href=\"%s\" title=\"%s\">%s</a></td>\n", name, name, name)
_, _ = fmt.Fprintf(w, "<td>%s</td>\n", d.ModTime().Format(datetime))
_, _ = fmt.Fprintf(w, "<td>%s</td>\n", size)
_, _ = fmt.Fprintf(w, "</tr>\n")
}
_, _ = fmt.Fprintf(w, "</table>\n")
_, _ = fmt.Fprintf(w, "</pre>\n")
_, _ = fmt.Fprintf(w, "<hr>\n")
_, _ = fmt.Fprintf(w, "</body>\n")
_, _ = fmt.Fprintf(w, "</html>\n")
}
func fileHandler(file webdav.File, w http.ResponseWriter) int64 {
stat, _ := file.Stat()
if stat.IsDir() {
return 0
}
w.Header().Set("Content-Length", strconv.FormatUint(uint64(stat.Size()), 10))
w.Header().Set("Content-Type", fileMime(stat.Name()))
n, err := io.Copy(w, file)
if err != nil {
log.Fatalf("file %s handler err %v\n", stat.Name(), err)
}
return n
}