-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
147 lines (128 loc) · 4.27 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package graceful
import (
"fmt"
"io"
"net"
"os"
)
// ErrNotFiler returned when object given to Send* functios does provides
// ability for getting its underlying *os.File.
var ErrNotFiler = fmt.Errorf("given value does not provide *os.File getter")
// Handler describes an object that can send descriptors to the connection with
// given ResponseWriter.
type Handler interface {
Handle(net.Conn, ResponseWriter)
}
// HandlerFunc is an adapter to allow the use of ordinary functions as
// Handlers.
type HandlerFunc func(net.Conn, ResponseWriter)
// Handle calls h(conn, resp).
func (h HandlerFunc) Handle(conn net.Conn, resp ResponseWriter) {
h(conn, resp)
}
// HandlerFunc is an adapter to allow the use of ordinary functions with empty
// arguments as Handlers.
type CallbackHandler func()
// Handle calls cb().
func (cb CallbackHandler) Handle(net.Conn, ResponseWriter) {
cb()
}
// filer describes an object that can return os.File instance.
// Note that `net.Conn` interface implements it.
type filer interface {
File() (*os.File, error)
}
// SendListener sends a listener ln with given meta to the ResponseWriter.
func SendListener(resp ResponseWriter, ln net.Listener, meta io.WriterTo) error {
f, err := fileFrom(ln)
if err != nil {
return err
}
return SendFile(resp, f, meta)
}
// SendConn sends a connection conn with given meta to the ResponseWriter.
func SendConn(resp ResponseWriter, conn net.Conn, meta io.WriterTo) error {
f, err := fileFrom(conn)
if err != nil {
return err
}
return SendFile(resp, f, meta)
}
// SendPacketConn sends a connection conn with given meta to the ResponseWriter.
func SendPacketConn(resp ResponseWriter, conn net.PacketConn, meta io.WriterTo) error {
f, err := fileFrom(conn)
if err != nil {
return err
}
return SendFile(resp, f, meta)
}
// SendFile sends a file f with given meta to the ResponseWriter.
func SendFile(resp ResponseWriter, file *os.File, meta io.WriterTo) error {
return resp.Write(int(file.Fd()), meta)
}
// ListenerHandler returns a Handler that sends listener ln with given meta to
// the received connection. If some error occures, it logs it by calling
// resp.Errorf().
func ListenerHandler(ln net.Listener, meta io.WriterTo) Handler {
return HandlerFunc(func(_ net.Conn, resp ResponseWriter) {
if err := SendListener(resp, ln, meta); err != nil {
resp.Errorf("send listener error: %v", err)
}
})
}
// ConnHandler returns a Handler that sends conn with given meta to the
// received connection. If some error occures, it logs it by calling
// resp.Errorf().
func ConnHandler(conn net.Conn, meta io.WriterTo) Handler {
return HandlerFunc(func(_ net.Conn, resp ResponseWriter) {
if err := SendConn(resp, conn, meta); err != nil {
resp.Errorf("send conn error: %v", err)
}
})
}
// PacketConnHandler returns a Handler that sends conn with given meta to the
// received connection. If some error occures, it logs it by calling
// resp.Errorf().
func PacketConnHandler(conn net.PacketConn, meta io.WriterTo) Handler {
return HandlerFunc(func(_ net.Conn, resp ResponseWriter) {
if err := SendPacketConn(resp, conn, meta); err != nil {
resp.Errorf("send conn error: %v", err)
}
})
}
// FileHandler returns a Handler that sends file with given meta to the
// received connection. If some error occures, it logs it by calling
// resp.Errorf().
func FileHandler(file *os.File, meta io.WriterTo) Handler {
return HandlerFunc(func(_ net.Conn, resp ResponseWriter) {
if err := SendFile(resp, file, meta); err != nil {
resp.Errorf("send file error: %v", err)
}
})
}
// FdHandler returns a Handler that sends file descriptor with given meta to
// the received connection. If some error occures, it logs it by calling
// resp.Errorf().
func FdHandler(fd int, meta io.WriterTo) Handler {
return HandlerFunc(func(_ net.Conn, resp ResponseWriter) {
if err := resp.Write(fd, meta); err != nil {
resp.Errorf("send fd error: %v", err)
}
})
}
// SequenceHandler returns a Handler that calls Handle() method on each passed
// handlers in sequence.
func SequenceHandler(hs ...Handler) Handler {
return HandlerFunc(func(conn net.Conn, resp ResponseWriter) {
for _, h := range hs {
h.Handle(conn, resp)
}
})
}
func fileFrom(v interface{}) (*os.File, error) {
f, ok := v.(filer)
if !ok {
return nil, ErrNotFiler
}
return f.File()
}