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
|
package finger
import (
"context"
"fmt"
"io"
"net"
"strings"
"tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/internal"
"tildegit.org/tjp/gus/logging"
)
type fingerServer struct {
internal.Server
handler gus.Handler
}
func (fs fingerServer) Protocol() string { return "FINGER" }
// NewServer builds a finger server.
func NewServer(
ctx context.Context,
hostname string,
network string,
address string,
handler gus.Handler,
errLog logging.Logger,
) (gus.Server, error) {
fs := &fingerServer{handler: handler}
if strings.IndexByte(hostname, ':') < 0 {
hostname = net.JoinHostPort(hostname, "79")
}
var err error
fs.Server, err = internal.NewServer(ctx, hostname, network, address, errLog, fs.handleConn)
if err != nil {
return nil, err
}
return fs, nil
}
func (fs *fingerServer) handleConn(conn net.Conn) {
request, err := ParseRequest(conn)
if err != nil {
_, _ = fmt.Fprint(conn, err.Error()+"\r\n")
}
request.Server = fs
request.RemoteAddr = conn.RemoteAddr()
defer func() {
if r := recover(); r != nil {
_ = fs.LogError("msg", "panic in handler", "err", r)
_, _ = fmt.Fprint(conn, "Error handling request.\r\n")
}
}()
response := fs.handler.Handle(fs.Ctx, request)
if response == nil {
response = Error("No result found.")
}
defer response.Close()
_, _ = io.Copy(conn, response.Body)
}
|