summaryrefslogtreecommitdiff
path: root/go/internal/gitlabnet/socketclient.go
blob: 3bd7c70f3fa3df630185a177bfb9e6eea6f1149c (plain)
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
package gitlabnet

import (
	"context"
	"net"
	"net/http"
	"strings"

	"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
)

const (
	// We need to set the base URL to something starting with HTTP, the host
	// itself is ignored as we're talking over a socket.
	socketBaseUrl      = "http://unix"
	UnixSocketProtocol = "http+unix://"
)

type GitlabSocketClient struct {
	httpClient *http.Client
	config     *config.Config
}

func buildSocketClient(config *config.Config) *GitlabSocketClient {
	path := strings.TrimPrefix(config.GitlabUrl, UnixSocketProtocol)
	httpClient := &http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", path)
			},
		},
	}

	return &GitlabSocketClient{httpClient: httpClient, config: config}
}

func (c *GitlabSocketClient) Get(path string) (*http.Response, error) {
	path = normalizePath(path)

	request, err := http.NewRequest("GET", socketBaseUrl+path, nil)
	if err != nil {
		return nil, err
	}

	return doRequest(c.httpClient, c.config, request)
}