-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
71 lines (60 loc) · 1.59 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
package main
import (
"flag"
"math/rand"
"net/http"
"strconv"
"time"
)
var (
cacheControl = flag.String("cache-control", "private,no-cache,max-age=0", "The Cache-Control header to return for all responses.")
contentType = flag.String("content-type", "text/plain", "The Content-Type to return for all responses.")
)
func handler(w http.ResponseWriter, r *http.Request) {
// first: Count of bytes to send before any sleep
first := formInt(r, "first")
// sleep: Sleep in ms converted to time.Duration
sleep := time.Duration(formInt(r, "sleep")) * time.Millisecond
// size: Count of bytes to send after sleep
size := formInt(r, "size")
// count: Count of sleep+send cycles
count := formInt(r, "count")
if count <= 0 && (sleep > 0 || size > 0) {
count = 1
}
w.Header().Set("Content-Type", *contentType)
w.Header().Set("Cache-Control", *cacheControl)
w.WriteHeader(http.StatusOK)
if writeRand(w, first) != nil {
return
}
for i := 0; i < count; i++ {
time.Sleep(sleep)
if writeRand(w, size) != nil {
return
}
}
}
// formInt returns the integer value of a http.request form parameter, or 0
// if the parmeter is missing or not a number.
func formInt(r *http.Request, param string) int {
v, _ := strconv.Atoi(r.FormValue(param))
return v
}
// writeRand writes n random bytes to w then flushes the result.
func writeRand(w http.ResponseWriter, n int) error {
if n <= 0 {
return nil
}
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return err
}
if _, err := w.Write(b); err != nil {
return err
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
return nil
}