Debian Codegraph @

lfshttp/endpoint.go

raw symbols SCIP
1 package lfshttp
2
3 import (
4 "fmt"
5 "net/url"
6 "regexp"
7 "strings"
8
9 "github.com/git-lfs/git-lfs/v3/git"
10 "github.com/git-lfs/git-lfs/v3/ssh"
11 )
12
13 const UrlUnknown = "<unknown>"
14
15 // An Endpoint describes how to access a Git LFS server.
16 type Endpoint struct {
17 Url string
18 SSHMetadata ssh.SSHMetadata
19 Operation string
20 OriginalUrl string
21 }
22
23 func endpointOperation(e Endpoint, method string) string {
24 if len(e.Operation) > 0 {
25 return e.Operation
26 }
27
28 switch method {
29 case "GET", "HEAD":
30 return "download"
31 default:
32 return "upload"
33 }
34 }
35
36 // EndpointFromSshUrl constructs a new endpoint from an ssh:// URL
37 func EndpointFromSshUrl(u *url.URL) Endpoint {
38 var endpoint Endpoint
39 // Pull out port now, we need it separately for SSH
40 regex := regexp.MustCompile(`^([^\:]+)(?:\:(\d+))?$`)
41 match := regex.FindStringSubmatch(u.Host)
42 if match == nil || len(match) < 2 {
43 endpoint.Url = UrlUnknown
44 return endpoint
45 }
46
47 endpoint.OriginalUrl = u.String()
48
49 host := match[1]
50 if u.User != nil && u.User.Username() != "" {
51 endpoint.SSHMetadata.UserAndHost = fmt.Sprintf("%s@%s", u.User.Username(), host)
52 } else {
53 endpoint.SSHMetadata.UserAndHost = host
54 }
55
56 if len(match) > 2 {
57 endpoint.SSHMetadata.Port = match[2]
58 }
59
60 endpoint.SSHMetadata.Path = u.Path
61
62 // Fallback URL for using HTTPS while still using SSH for git
63 // u.Host includes host & port so can't use SSH port
64 endpoint.Url = fmt.Sprintf("https://%s%s", host, u.Path)
65
66 return endpoint
67 }
68
69 // EndpointFromBareSshUrl constructs a new endpoint from a bare SSH URL:
70 //
71 // [email protected]:path/to/repo.git or
72 // [[email protected]:port]:path/to/repo.git
73 func EndpointFromBareSshUrl(rawurl string) Endpoint {
74 parts := strings.Split(rawurl, ":")
75 partsLen := len(parts)
76 if partsLen < 2 {
77 return Endpoint{Url: rawurl}
78 }
79
80 // Treat presence of ':' as a bare URL
81 var newPath string
82 if len(parts) > 2 { // port included; really should only ever be 3 parts
83 // Correctly handle [host:port]:path URLs
84 parts[0] = strings.TrimPrefix(parts[0], "[")
85 parts[1] = strings.TrimSuffix(parts[1], "]")
86 newPath = fmt.Sprintf("%v:%v", parts[0], strings.Join(parts[1:], "/"))
87 } else {
88 newPath = strings.Join(parts, "/")
89 }
90 newrawurl := fmt.Sprintf("ssh://%v", newPath)
91 newu, err := url.Parse(newrawurl)
92 if err != nil {
93 return Endpoint{Url: UrlUnknown}
94 }
95
96 endpoint := EndpointFromSshUrl(newu)
97 if strings.HasPrefix(endpoint.SSHMetadata.Path, "/") {
98 endpoint.SSHMetadata.Path = endpoint.SSHMetadata.Path[1:]
99 }
100 return endpoint
101 }
102
103 // Construct a new endpoint from a HTTP URL
104 func EndpointFromHttpUrl(u *url.URL) Endpoint {
105 // just pass this straight through
106 return Endpoint{Url: u.String(), OriginalUrl: u.String()}
107 }
108
109 func EndpointFromLocalPath(path string) Endpoint {
110 url := git.RewriteLocalPathAsURL(path)
111 return Endpoint{Url: url, OriginalUrl: url}
112 }
113
114 // Construct a new endpoint from a file URL
115 func EndpointFromFileUrl(u *url.URL) Endpoint {
116 // just pass this straight through
117 return Endpoint{Url: u.String(), OriginalUrl: u.String()}
118 }