1
1
package dev
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"io"
@@ -14,7 +15,6 @@ import (
14
15
"time"
15
16
16
17
"github.com/bmizerany/pat"
17
- "github.com/puma/puma-dev/httpu"
18
18
)
19
19
20
20
type HTTPServer struct {
@@ -26,29 +26,60 @@ type HTTPServer struct {
26
26
IgnoredStaticPaths []string
27
27
Domains []string
28
28
29
- mux * pat.PatternServeMux
30
- transport * httpu.Transport
31
- proxy * httputil.ReverseProxy
29
+ mux * pat.PatternServeMux
30
+ unixTransport * http.Transport
31
+ unixProxy * httputil.ReverseProxy
32
+ tcpTransport * http.Transport
33
+ tcpProxy * httputil.ReverseProxy
32
34
}
33
35
36
+ const dialerTimeout = 5 * time .Second
37
+ const keepAlive = 10 * time .Second
38
+ const tlsHandshakeTimeout = 10 * time .Second
39
+ const expectContinueTimeout = 1 * time .Second
40
+ const proxyFlushInternal = 1 * time .Second
41
+
34
42
func (h * HTTPServer ) Setup () {
35
- h .transport = & httpu.Transport {
36
- Dial : (& net.Dialer {
37
- Timeout : 5 * time .Second ,
38
- KeepAlive : 10 * time .Second ,
39
- }).Dial ,
40
- TLSHandshakeTimeout : 10 * time .Second ,
41
- ExpectContinueTimeout : 1 * time .Second ,
43
+ h .unixTransport = & http.Transport {
44
+ DialContext : func (ctx context.Context , _ , addr string ) (net.Conn , error ) {
45
+ socketPath , _ , err := net .SplitHostPort (addr )
46
+ if err != nil {
47
+ return nil , err
48
+ }
49
+
50
+ dialer := net.Dialer {
51
+ Timeout : dialerTimeout ,
52
+ KeepAlive : keepAlive ,
53
+ }
54
+ return dialer .DialContext (ctx , "unix" , socketPath )
55
+ },
56
+ TLSHandshakeTimeout : tlsHandshakeTimeout ,
57
+ ExpectContinueTimeout : expectContinueTimeout ,
42
58
}
43
59
44
- h .Pool .AppClosed = h .AppClosed
60
+ h .unixProxy = & httputil.ReverseProxy {
61
+ Director : func (_ * http.Request ) {},
62
+ Transport : h .unixTransport ,
63
+ FlushInterval : proxyFlushInternal ,
64
+ }
45
65
46
- h .proxy = & httputil.ReverseProxy {
66
+ h .tcpTransport = & http.Transport {
67
+ DialContext : (& net.Dialer {
68
+ Timeout : dialerTimeout ,
69
+ KeepAlive : keepAlive ,
70
+ }).DialContext ,
71
+ TLSHandshakeTimeout : tlsHandshakeTimeout ,
72
+ ExpectContinueTimeout : expectContinueTimeout ,
73
+ }
74
+
75
+ h .tcpProxy = & httputil.ReverseProxy {
47
76
Director : func (_ * http.Request ) {},
48
- Transport : h .transport ,
49
- FlushInterval : 1 * time . Second ,
77
+ Transport : h .tcpTransport ,
78
+ FlushInterval : proxyFlushInternal ,
50
79
}
51
80
81
+ h .Pool .AppClosed = h .AppClosed
82
+
52
83
h .mux = pat .New ()
53
84
54
85
h .mux .Get ("/status" , http .HandlerFunc (h .status ))
@@ -59,7 +90,8 @@ func (h *HTTPServer) AppClosed(app *App) {
59
90
// Whenever an app is closed, wipe out all idle conns. This
60
91
// obviously closes down more than just this one apps connections
61
92
// but that's ok.
62
- h .transport .CloseIdleConnections ()
93
+ h .unixTransport .CloseIdleConnections ()
94
+ h .tcpTransport .CloseIdleConnections ()
63
95
}
64
96
65
97
func (h * HTTPServer ) removeTLD (host string ) string {
@@ -151,7 +183,13 @@ func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
151
183
}
152
184
153
185
req .URL .Scheme , req .URL .Host = app .Scheme , app .Address ()
154
- h .proxy .ServeHTTP (w , req )
186
+ if app .Scheme == "httpu" {
187
+ req .URL .Scheme , req .URL .Host = "http" , app .Address ()
188
+ h .unixProxy .ServeHTTP (w , req )
189
+ } else {
190
+ req .URL .Scheme , req .URL .Host = app .Scheme , app .Address ()
191
+ h .tcpProxy .ServeHTTP (w , req )
192
+ }
155
193
}
156
194
157
195
func (h * HTTPServer ) shouldServePublicPathForApp (a * App , req * http.Request ) bool {
0 commit comments