forked from msf/go-minipypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.go
128 lines (113 loc) · 3.28 KB
/
frontend.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
package main
import (
"fmt"
"html"
"html/template"
"io"
"log"
"net/http"
"strings"
"time"
)
// WebServerConfigs holds configs for this server
type WebServerConfigs struct {
Host string
Port int
BasePath string
// webServer will use this FileFetcher to serve requests.
fetcher FileFetcher
}
// RunWebServer starts a proxy pypi server that handles HTTP requests by leveraging any implementation of FileFetcher to do the grunt work.
func RunWebServer(cfg WebServerConfigs, fileFetcher FileFetcher) {
cfg.fetcher = fileFetcher
http.HandleFunc(cfg.BasePath, cfg.handleRequest)
hostPortStr := fmt.Sprintf("%v:%v", cfg.Host, cfg.Port)
log.Printf("serving on HTTP://%v/%v\n", hostPortStr, cfg.BasePath)
log.Fatal(http.ListenAndServe(hostPortStr, nil))
}
func (ctx WebServerConfigs) handleRequest(w http.ResponseWriter, req *http.Request) {
path := html.EscapeString(req.URL.Path)
if strings.HasSuffix(path, "/") {
ctx.handleListDir(w, path)
} else {
// must be file, lets find it.
ctx.handleServeS3File(w, path)
}
}
func (ctx WebServerConfigs) handleServeS3File(w http.ResponseWriter, path string) {
start := time.Now()
fileName := handlePypiFileNames(path)
file, err := ctx.fetcher.GetFile(fileName)
if err != nil {
desc := fmt.Sprintf("\"GET %v\" 404 - time: %.3fs, error: %v",
fileName, time.Since(start).Seconds(), err)
log.Println(desc)
http.Error(w, desc, http.StatusNotFound)
return
}
timeFetching := time.Since(start)
defer file.Payload.Close()
nbytes, err := io.Copy(w, file.Payload)
if err != nil {
desc := fmt.Sprintf("\"GET %v\" 404 - time: %.3fs, network write error: %v",
fileName, time.Since(start).Seconds(), err)
log.Println(desc)
http.Error(w, desc, http.StatusNotFound)
return
}
elapsed := time.Since(start)
log.Printf("\"GET %v\" 200 - size: %v, fetch: %.3fs, total: %.3fs",
path, nbytes, timeFetching.Seconds(), elapsed.Seconds())
return
}
func (ctx WebServerConfigs) handleListDir(w http.ResponseWriter, path string) {
start := time.Now()
fileList, err := handlePypiListDir(ctx.fetcher, path)
if err != nil {
desc := fmt.Sprintf("\"LISTDIR %v\" 400 - err: %v", path, err)
log.Println(desc)
http.Error(w, desc, http.StatusBadRequest)
return
}
type templateData struct {
Results []ListDirEntry
Elapsed time.Duration
BaseDir string
}
if err := pypiResultsTemplate.Execute(w, templateData{
Results: fileList,
Elapsed: time.Since(start),
BaseDir: ctx.BasePath,
}); err != nil {
desc := fmt.Sprintf("\"LISTDIR %v\" 404 - time: %.3fs, template write error: %v",
path, time.Since(start).Seconds(), err)
log.Print(desc)
http.Error(w, desc, http.StatusInternalServerError)
}
log.Printf("\"LISTDIR %v\" 200 - items:%v, time: %.3fs",
path, len(fileList), time.Since(start).Seconds())
}
// results just like a pypi server would return
var pypiResultsTemplate = template.Must(template.New("results").Parse(`
<html>
<head/>
<body>
{{range .Results}}
<a href="{{.Name}}">{{.Name}}</a>
{{end}}
</body>
</html>
`))
var resultsTemplate = template.Must(template.New("results").Parse(`
<html>
<head/>
<body>
<ol>
{{range .Results}}
<li>{{.LastModified}} - {{.SizeKb}} Kb - <a href="http://localhost:8080/{{.Name}}">{{.Name}}</a></li>
{{end}}
</ol>
<p>{{len .Results}} results in {{.Elapsed}}</p>
</body>
</html>
`))