Debian Codegraph @

cmd/panicweb/internal/internal.go

raw
1 // Copyright 2020 Marc-Antoine Ruel. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 // Package internal implements the handlers for panicweb so they are in a
6 // separate package than "main".
7 package internal
8
9 import (
10 "io"
11 "log"
12 "net/http"
13 )
14
15 // Unblock unblocks one http server handler.
16 var Unblock = make(chan struct{})
17
18 // GetAsync does an HTTP GET to the URL but leaves the actual fetching to a
19 // goroutine.
20 func GetAsync(url string) {
21 /* #nosec G107 */
22 resp, err := http.Get(url)
23 if err != nil {
24 log.Fatalf("get %s: %v", url, err)
25 }
26 go func() {
27 _, err := io.ReadAll(resp.Body)
28 if err != nil {
29 log.Fatalf("failed to read: %v", err)
30 }
31 _ = resp.Body.Close()
32 log.Fatal("the goal is to not complete this request")
33 }()
34 }
35
36 // URL1Handler is a http.HandlerFunc that hangs.
37 func URL1Handler(w http.ResponseWriter, req *http.Request) {
38 // Respond the HTTP header to unblock the http.Get() function.
39 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
40 w.Header().Set("Content-Length", "100000")
41 w.WriteHeader(200)
42 b := [4096]byte{}
43 _, _ = w.Write(b[:])
44 <-Unblock
45 }
46
47 // URL2Handler is a http.HandlerFunc that hangs.
48 func URL2Handler(w http.ResponseWriter, req *http.Request) {
49 // Respond the HTTP header to unblock the http.Get() function.
50 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
51 w.Header().Set("Content-Length", "100000")
52 w.WriteHeader(200)
53 b := [4096]byte{}
54 _, _ = w.Write(b[:])
55 <-Unblock
56 }