-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_rt_unix.go
39 lines (33 loc) · 944 Bytes
/
http_rt_unix.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
package main
import (
"context"
"errors"
"net"
"net/http"
"strings"
"time"
)
type UnixRoundTripper struct {
}
func (u UnixRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
request.URL.Scheme = "http"
sepIdx := strings.Index(request.URL.Path, ":")
if sepIdx < 0 {
logf(request, logLevelError, "Unix endpoint %#v does not contain ':'", request.URL.Path)
return nil, errors.New("server configuration error")
}
unixPath := request.URL.Path[:sepIdx]
request.URL.Path = request.URL.Path[sepIdx+1:]
if request.URL.Host == "" {
request.URL.Host = "localhost"
}
dialer := &net.Dialer{Timeout: 30 * time.Second}
return (&http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, "unix", unixPath)
},
}).RoundTrip(request)
}
func init() {
customHttpSchemas["unix"] = func() http.RoundTripper { return &UnixRoundTripper{} }
}