-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
108 lines (96 loc) · 2.73 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
// MIT License
//
// Copyright (c) 2023 TTBT Enterprises LLC
// Copyright (c) 2023 Robin Thellend <rthellend@rthellend.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package proxy
import (
"context"
"log"
"net"
"net/http"
"sync"
"time"
)
type ctxKey int
var connCtxKey ctxKey = 1
func startInternalHTTPServer(handler http.Handler, conns <-chan net.Conn) *http.Server {
l := &proxyListener{
ch: conns,
closedCh: make(chan struct{}),
}
s := &http.Server{
Handler: handler,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 30 * time.Second,
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, connCtxKey, c)
},
}
go serveHTTP(s, l)
return s
}
func serveHTTP(s *http.Server, l net.Listener) {
if err := s.Serve(l); err != net.ErrClosed && err != http.ErrServerClosed {
log.Printf("ERR http server exited: %v", err)
}
}
type proxyListener struct {
ch <-chan net.Conn
addr net.Addr
mu sync.Mutex
closed bool
closedCh chan struct{}
}
func (l *proxyListener) Accept() (net.Conn, error) {
select {
case <-l.closedCh:
return nil, net.ErrClosed
case c, ok := <-l.ch:
if !ok {
l.Close()
return nil, net.ErrClosed
}
return c, nil
}
}
func (l *proxyListener) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if !l.closed {
l.closed = true
close(l.closedCh)
go func() {
for range l.ch {
}
}()
}
return nil
}
func (l *proxyListener) Addr() net.Addr {
return l.addr
}
func logHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Printf("REQ %s ➔ %s %s", formatReqDesc(req), req.Method, req.URL)
next.ServeHTTP(w, req)
})
}