-
Notifications
You must be signed in to change notification settings - Fork 0
/
stuffer.go
40 lines (32 loc) · 874 Bytes
/
stuffer.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
package main
import (
"math/rand"
"net/http"
"strconv"
)
const chunkSize = 8 * 1024 // Same as Async::IO::Stream::BLOCK_SIZE
func ServeRandomBlob(w http.ResponseWriter, r *http.Request) {
bytesParam := r.URL.Query().Get("bytes")
bytesToSend, err := strconv.Atoi(bytesParam)
if err != nil {
http.Error(w, "?bytes= parameter is required", http.StatusInternalServerError)
return
}
w.Header().Set("Connection", "close")
w.Header().Set("Content-Length", bytesParam)
w.Header().Set("Server", "stuffer/go")
buf := make([]byte, chunkSize)
rand.Read(buf)
wholeChunks := bytesToSend / chunkSize
for i := 0; i < wholeChunks; i++ {
w.Write(buf)
}
remainingChunk := bytesToSend % chunkSize
if remainingChunk > 0 {
w.Write(buf[0:remainingChunk])
}
}
func main() {
http.Handle("/", http.HandlerFunc(ServeRandomBlob))
http.ListenAndServe(":9395", nil)
}