File: implementation.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (71 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download
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
67
68
69
70
71
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// Copyright twenty-panda <twenty-panda@posteo.com>
// SPDX-License-Identifier: MIT

package http

import (
	"crypto/tls"
	"fmt"
	"net/http"
	"net/url"
)

type Implementation struct {
	newMigrationHTTPClient NewMigrationHTTPClientFun
	skipTLSVerify          *bool
	proxy                  *string
}

func (o *Implementation) GetNewMigrationHTTPClient() NewMigrationHTTPClientFun {
	if o.newMigrationHTTPClient == nil {
		return func() *http.Client {
			transport := &http.Transport{}
			if o.GetSkipTLSVerify() {
				transport.TLSClientConfig = &tls.Config{
					InsecureSkipVerify: true,
				}
			}
			if o.GetProxy() != "" {
				proxyURL, err := url.Parse(o.GetProxy())
				if err != nil {
					panic(fmt.Errorf("url.Parse %w", err))
				}
				transport.Proxy = func(req *http.Request) (*url.URL, error) {
					return http.ProxyURL(proxyURL)(req)
				}
			}
			return &http.Client{
				Transport: transport,
			}
		}
	}
	return o.newMigrationHTTPClient
}

func (o *Implementation) SetNewMigrationHTTPClient(fun NewMigrationHTTPClientFun) {
	o.newMigrationHTTPClient = fun
}

func (o *Implementation) GetSkipTLSVerify() bool {
	if o.skipTLSVerify == nil {
		return false
	}
	return *o.skipTLSVerify
}

func (o *Implementation) SetSkipTLSVerify(skipTLSVerify bool) {
	o.skipTLSVerify = &skipTLSVerify
}

func (o *Implementation) GetProxy() string {
	if o.proxy == nil {
		return ""
	}
	return *o.proxy
}

func (o *Implementation) SetProxy(proxy string) {
	o.proxy = &proxy
}