-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (91 loc) · 2.26 KB
/
main.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
package main
import (
"crypto/rand"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"time"
)
const oneMB = 1024 * 1024 // 1 MB
var (
FlagAddress = flag.String("address", ":8080", "Address to listen on")
FlagMaxSizeMB = flag.Int("maxSizeMB", 100000, "Maximum size of file to serve in MB")
)
func main() {
flag.Parse()
http.HandleFunc("/file", func(w http.ResponseWriter, r *http.Request) {
sizeMBString := r.URL.Query().Get("size_mb")
sizeMB, err := strconv.Atoi(sizeMBString)
if err != nil {
http.Error(w, "invalid size_mb", http.StatusBadRequest)
return
}
if sizeMB > *FlagMaxSizeMB {
http.Error(w, fmt.Sprintf("size_mb must be less than or equal to %d", *FlagMaxSizeMB), http.StatusBadRequest)
return
}
MBsString := r.URL.Query().Get("mbs")
MBs := 0
limitedSpeed := false
if MBsString != "" {
limitedSpeed = true
MBs, err = strconv.Atoi(MBsString)
if err != nil {
http.Error(w, "invalid mbs", http.StatusBadRequest)
return
}
}
filename := r.URL.Query().Get("filename")
if filename == "" {
filename = "file.bin"
}
log.Printf("serving %d MB file...\n", sizeMB)
w.Header().Set("Content-Length", fmt.Sprint(sizeMB*oneMB))
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
randomBytes := make([]byte, sizeMB*oneMB)
_, err = rand.Read(randomBytes)
if err != nil {
http.Error(w, "error generating random data", http.StatusInternalServerError)
return
}
if !limitedSpeed {
_, err := w.Write(randomBytes)
if err != nil {
log.Println(err)
}
return
}
tick := time.Tick(10 * time.Millisecond)
bytesWritten := 0
for {
<-tick
if bytesWritten >= sizeMB*oneMB {
log.Printf("finished serving %d MB file successfully.\n", sizeMB)
return
}
n := MBs * oneMB / 100
if n > sizeMB*oneMB-bytesWritten {
n = sizeMB*oneMB - bytesWritten
}
_, err := w.Write(randomBytes[bytesWritten : bytesWritten+n])
if err != nil {
log.Println(err)
return
}
bytesWritten += n
}
})
if FlagAddress == nil {
panic("flagAddress is nil")
}
if FlagMaxSizeMB == nil {
panic("flagMaxSizeMB is nil")
}
log.Printf("listening on %s...\n", *FlagAddress)
err := http.ListenAndServe(*FlagAddress, nil)
if err != nil {
panic(err)
}
}