-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
49 lines (40 loc) · 1.21 KB
/
common.go
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
// Copyright 2019 Nirenjan Krishnan. All rights reserved.
package vanity
import (
"log"
"net/http"
"strings"
)
// repoBase returns the first segment of the requested URL. It does so
// by splitting on `/`, and returning the first result.
func repoBase(url string) string {
return strings.Split(strings.TrimPrefix(url, "/"), "/")[0]
}
// checkUpstream verifies that the package is available on the remote server
func (s *Server) checkUpstream(module string) (bool, int) {
if !s.queryRemote {
return true, http.StatusOK
}
base := repoBase(module)
upstream := s.repo.root + base
// Head will follow up to 10 redirects, so no need to worry about
// it here.
resp, err := s.client.Head(upstream)
if err != nil {
log.Print(err)
return false, http.StatusServiceUnavailable
}
// Close the body, avoid leaking resources
resp.Body.Close()
return resp.StatusCode == http.StatusOK, resp.StatusCode
}
// getRedirect gets the URL to redirect to
// If s.Redirect and s.Repo are the same, we cannot use the full request
// and must use the base only.
func (s *Server) getRedirect(module string) string {
base := repoBase(module)
if s.redirect == s.repo.root {
return s.repo.root + base
}
return s.redirect + module
}