summaryrefslogtreecommitdiff
path: root/go/internal/gitlabnet/socketclient.go
blob: fd9753545fd5cdaf2ae668a1905cb23face34ebe (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gitlabnet

import (
	"bytes"
	"context"
	"encoding/json"
	"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)
}

func (c *GitlabSocketClient) Post(path string, data interface{}) (*http.Response, error) {
	path = normalizePath(path)

	jsonData, err := json.Marshal(data)
	if err != nil {
		return nil, err
	}

	request, err := http.NewRequest("POST", socketBaseUrl+path, bytes.NewReader(jsonData))
	request.Header.Add("Content-Type", "application/json")

	if err != nil {
		return nil, err
	}

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