Permalink
Browse files

Add a query option to return only HTTPS or HTTP mirrors

Appending the query argument https=1 on any request will return only
HTTPS mirrors and conversely https=0 will return only HTTP mirrors.

This option is experimental and should be used carefully.
  • Loading branch information...
1 parent 36eda24 commit 185d2db394fbf008a399aab83662ad5921ab117e @etix committed Jul 25, 2017
Showing with 36 additions and 0 deletions.
  1. +23 −0 http/context.go
  2. +8 −0 http/selection.go
  3. +5 −0 mirrors/mirrors.go
View
@@ -11,12 +11,19 @@ import (
// RequestType defines the type of the request
type RequestType int
+// SecureOption is the type that defines TLS requirements
+type SecureOption int
+
const (
STANDARD RequestType = iota
MIRRORLIST
FILESTATS
MIRRORSTATS
CHECKSUM
+
+ UNDEFINED SecureOption = iota
+ WITHTLS
+ WITHOUTTLS
)
// Context represents the context of a request
@@ -31,6 +38,7 @@ type Context struct {
isFileStats bool
isChecksum bool
isPretty bool
+ secureOption SecureOption
}
// NewContext returns a new instance of Context
@@ -57,6 +65,16 @@ func NewContext(w http.ResponseWriter, r *http.Request, t Templates) *Context {
c.isPretty = true
}
+ // Check for HTTPS requirements
+ v, ok := c.v["https"]
+ if ok {
+ if v[0] == "1" {
+ c.secureOption = WITHTLS
+ } else if v[0] == "0" {
+ c.secureOption = WITHOUTTLS
+ }
+ }
+
return c
}
@@ -110,6 +128,11 @@ func (c *Context) QueryParam(key string) string {
return c.v.Get(key)
}
+// SecureOption returns the selected secure option
+func (c *Context) SecureOption() SecureOption {
+ return c.secureOption
+}
+
func (c *Context) paramBool(key string) bool {
_, ok := c.v[key]
return ok
View
@@ -62,6 +62,14 @@ func (h DefaultEngine) Selection(ctx *Context, cache *mirrors.Cache, fileInfo *f
}
goto delete
}
+ if ctx.SecureOption() == WITHTLS && !m.IsHTTPS() {
+ m.ExcludeReason = "Not HTTPS"
+ goto delete
+ }
+ if ctx.SecureOption() == WITHOUTTLS && m.IsHTTPS() {
+ m.ExcludeReason = "Not HTTP"
+ goto delete
+ }
// Is it the same size as source?
if m.FileInfo != nil {
if m.FileInfo.Size != fileInfo.Size {
View
@@ -66,6 +66,11 @@ func (m *Mirror) Prepare() {
m.ExcludedCountryFields = strings.Fields(m.ExcludedCountryCodes)
}
+// IsHTTPS returns true if the mirror has an HTTPS address
+func (m *Mirror) IsHTTPS() bool {
+ return strings.HasPrefix(m.HttpURL, "https://")
+}
+
// Mirrors represents a slice of Mirror
type Mirrors []Mirror

0 comments on commit 185d2db

Please sign in to comment.