-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathhttp.go
108 lines (87 loc) · 3 KB
/
http.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
package netemx
import (
"io"
"net"
"net/http"
"sync"
"github.com/ooni/netem"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// HTTPHandlerFactory constructs an [http.Handler].
type HTTPHandlerFactory interface {
NewHandler(env NetStackServerFactoryEnv, stack *netem.UNetStack) http.Handler
}
// HTTPHandlerFactoryFunc allows a func to become an [HTTPHandlerFactory].
type HTTPHandlerFactoryFunc func(env NetStackServerFactoryEnv, stack *netem.UNetStack) http.Handler
var _ HTTPHandlerFactory = HTTPHandlerFactoryFunc(nil)
// NewHandler implements HTTPHandlerFactory.
func (fx HTTPHandlerFactoryFunc) NewHandler(env NetStackServerFactoryEnv, stack *netem.UNetStack) http.Handler {
return fx(env, stack)
}
// HTTPCleartextServerFactory implements [NetStackServerFactory] for cleartext HTTP.
//
// Use this factory along with [QAEnvOptionNetStack] to create cleartext HTTP servers.
type HTTPCleartextServerFactory struct {
// Factory is the MANDATORY factory for creating the [http.Handler].
Factory HTTPHandlerFactory
// Ports is the MANDATORY list of ports where to listen.
Ports []int
}
var _ NetStackServerFactory = &HTTPCleartextServerFactory{}
// MustNewServer implements NetStackServerFactory.
func (f *HTTPCleartextServerFactory) MustNewServer(env NetStackServerFactoryEnv, stack *netem.UNetStack) NetStackServer {
return &httpCleartextServer{
closers: []io.Closer{},
env: env,
factory: f.Factory,
mu: sync.Mutex{},
ports: f.Ports,
unet: stack,
}
}
type httpCleartextServer struct {
closers []io.Closer
env NetStackServerFactoryEnv
factory HTTPHandlerFactory
mu sync.Mutex
ports []int
unet *netem.UNetStack
}
// Close implements NetStackServer.
func (srv *httpCleartextServer) Close() error {
// make the method locked as requested by the documentation
defer srv.mu.Unlock()
srv.mu.Lock()
// close each of the closers
for _, closer := range srv.closers {
_ = closer.Close()
}
// be idempotent
srv.closers = []io.Closer{}
return nil
}
// MustStart implements NetStackServer.
func (srv *httpCleartextServer) MustStart() {
// make the method locked as requested by the documentation
defer srv.mu.Unlock()
srv.mu.Lock()
// create the handler
handler := srv.factory.NewHandler(srv.env, srv.unet)
// create the listening address
ipAddr := net.ParseIP(srv.unet.IPAddress())
runtimex.Assert(ipAddr != nil, "expected valid IP address")
for _, port := range srv.ports {
srv.mustListenPortLocked(handler, ipAddr, port)
}
}
func (srv *httpCleartextServer) mustListenPortLocked(handler http.Handler, ipAddr net.IP, port int) {
// create the listening socket
addr := &net.TCPAddr{IP: ipAddr, Port: port}
listener := runtimex.Try1(srv.unet.ListenTCP("tcp", addr))
// serve requests in a background goroutine
srvr := &http.Server{Handler: handler} // #nosec G112 - just a testing server
go srvr.Serve(listener)
// make sure we track the server (the .Serve method will close the
// listener once we close the server itself)
srv.closers = append(srv.closers, srvr)
}