Skip to content

Commit

Permalink
proxy/router: fix panic & gofmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jsign authored and oiooj committed Aug 30, 2019
1 parent a7bbe9e commit 5aa10ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func main() {
log.Printf("ExcludeHost %s\n", excludeHost)
}
handle = &logger{proxy.NewRouter(proxy.NewServer(new(ops)), &proxy.RouterOptions{
Pattern: excludeHost,
Proxy: proxyHost,
Pattern: excludeHost,
Proxy: proxyHost,
DownloadRoot: downloadRoot,
})}
} else {
Expand Down
33 changes: 19 additions & 14 deletions proxy/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ const ListExpire = 5 * time.Minute

// A RouterOps provides the proxy host and the external pattern
type RouterOptions struct {
Pattern string
Proxy string
Pattern string
Proxy string
DownloadRoot string
}

// A Router is the proxy HTTP server,
// which implements Route Filter to
// routing private module or public module .
type Router struct {
srv *Server
proxy *httputil.ReverseProxy
pattern string
srv *Server
proxy *httputil.ReverseProxy
pattern string
downloadRoot string
}

Expand Down Expand Up @@ -107,6 +107,11 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var ctype string
defer f.Close()
i := strings.Index(r.URL.Path, "/@v/")
if i < 0 {
http.Error(w, "no such path", http.StatusNotFound)
return
}

what := r.URL.Path[i+len("/@v/"):]
if what == "list" {
if time.Since(info.ModTime()) >= ListExpire {
Expand All @@ -119,15 +124,15 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
ext := path.Ext(what)
switch ext {
case ".info":
ctype = "application/json"
case ".mod":
ctype = "text/plain; charset=UTF-8"
case ".zip":
ctype = "application/octet-stream"
default:
http.Error(w, "request not recognized", http.StatusNotFound)
return
case ".info":
ctype = "application/json"
case ".mod":
ctype = "text/plain; charset=UTF-8"
case ".zip":
ctype = "application/octet-stream"
default:
http.Error(w, "request not recognized", http.StatusNotFound)
return
}
}
w.Header().Set("Content-Type", ctype)
Expand Down

0 comments on commit 5aa10ed

Please sign in to comment.