-
Notifications
You must be signed in to change notification settings - Fork 7
/
http.go
122 lines (100 loc) · 3.17 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ozone
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strconv"
"time"
"github.com/One-com/gone/daemon"
nshttp "github.com/One-com/gone/http"
"github.com/One-com/gone/log"
"github.com/One-com/gone/log/syslog"
"github.com/One-com/gone/netutil"
stdlog "log"
"github.com/One-com/ozone/config"
"github.com/One-com/gone/netutil/reaper"
)
func reaperConnStateCallback(to time.Duration) func(net.Conn, http.ConnState) {
return func (conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
// Put a timebomb on the connection requiring it to
// go into active state fast enough, to timeout on TLS handshake.
if to != time.Duration(0) {
f := func(w net.Conn) {
w.Close()
}
reaper.StartTimer(conn, to, f)
}
case http.StateActive:
if to != time.Duration(0) {
reaper.StopTimer(conn)
}
reaper.IOActivityTimeout(conn, true)
case http.StateIdle, http.StateClosed, http.StateHijacked:
reaper.IOActivityTimeout(conn, false)
}
}
}
// newHTTPServer creates a deamon.Server complying HTTP server from config with the given handler
func newHTTPServer(name string, cfg config.HTTPServerConfig, snis *tlsPluginRegistry, handler http.Handler) (srv *nshttp.Server, err error) {
var listeners daemon.ListenerGroup
for _, lcfg := range cfg.Listeners { // ignore name
addr := lcfg.Address + ":" + strconv.Itoa(lcfg.Port)
var tlsCfg *tls.Config
if lcfg.TLS != nil {
tlsCfg, err = getTLSServerConfigWithPlugin(lcfg.TLS, snis)
if err != nil {
return nil, err
}
if tlsCfg == nil {
log.ERROR("TLS requested, but not configured")
} else {
log.DEBUG("TLS Config", "ciphers", log.Lazy(func() interface{} { return fmt.Sprint(tlsCfg.CipherSuites) }))
if tlsCfg.GetCertificate == nil && len(tlsCfg.Certificates) == 0 {
log.WARN("No Server certificates and no SNI callback enabled")
}
}
}
listener := daemon.ListenerSpec{
Addr: addr,
TLSConfig: tlsCfg,
ListenerFdName: lcfg.SocketFdName,
InheritOnly: lcfg.SocketInheritOnly,
}
to := lcfg.IOActivityTimeout.Duration
reaperInterval := to / time.Duration(2)
listener.PrepareListener = func(lin net.Listener) (lout net.Listener) {
lout = reaper.NewIOActivityTimeoutListener(lin, to, reaperInterval)
return
}
listeners = append(listeners, listener)
}
var configuredListeners netutil.StreamListener
if len(listeners) != 0 {
configuredListeners = listeners
}
// Set up a log adapter for stdlib HTTP server.
errorGonelog := log.NewStdlibAdapter(log.GetLogger(name), syslog.LOG_CRIT)
errorlog := stdlog.New(errorGonelog, "", 0)
// Assemble the HTTP server object.
httpserver := &http.Server{
Handler: handler,
ErrorLog: errorlog,
ReadHeaderTimeout: cfg.ReadHeaderTimeout.Duration,
IdleTimeout: cfg.IdleTimeout.Duration,
ReadTimeout: cfg.ReadTimeout.Duration,
WriteTimeout: cfg.WriteTimeout.Duration,
}
httpserver.ConnState = reaperConnStateCallback(cfg.NewActiveTimeout.Duration)
if cfg.DisableKeepAlives {
httpserver.SetKeepAlivesEnabled(false)
}
srv = &nshttp.Server{
Name: name,
Server: httpserver,
Listeners: configuredListeners,
}
return
}