-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunixtransport.go
More file actions
34 lines (29 loc) · 874 Bytes
/
unixtransport.go
File metadata and controls
34 lines (29 loc) · 874 Bytes
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
package main
import (
"net"
"net/http"
"net/http/httputil"
)
func NewUnixTransport(socketPath string) *http.Transport {
unixTransport := &http.Transport{}
unixTransport.RegisterProtocol("unix", NewUnixRoundTripper(socketPath))
return unixTransport
}
func NewUnixRoundTripper(path string) *UnixRoundTripper {
return &UnixRoundTripper{path: path}
}
type UnixRoundTripper struct {
path string
conn httputil.ClientConn
}
// The RoundTripper (http://golang.org/pkg/net/http/#RoundTripper) for
// the socket transport dials the socket each time a request is made.
func (roundTripper UnixRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
conn, err := net.Dial("unix", roundTripper.path)
if err != nil {
return nil, err
}
socketClientConn := httputil.NewClientConn(conn, nil)
defer socketClientConn.Close()
return socketClientConn.Do(req)
}