gddo-server: do not 404 when URL contains unknown query parameters
Currently, the server sends a 404 when the URL contains query parameters
unknown to the application. Instead, ignore the parameters. Fixes #500
Change-Id: I4478118311ee57c2f4adcb28fcdfff98357d0c39
GitHub-Last-Rev: f2754463e02fbae9a07ff625669ab1cdc75aaa2a
GitHub-Pull-Request: golang/gddo#548
Reviewed-on: https://go-review.googlesource.com/103400
Reviewed-by: Dmitri Shuralyov <dmitri@shuralyov.com>
Reviewed-by: Tuo Shan <shantuo@google.com>
diff --git a/gddo-server/main.go b/gddo-server/main.go
index 67accfa..2f080fe 100644
--- a/gddo-server/main.go
+++ b/gddo-server/main.go
@@ -273,53 +273,9 @@
}
switch {
- case len(req.Form) == 0:
- importerCount := 0
- if pdoc.Name != "" {
- importerCount, err = s.db.ImporterCount(importPath)
- if err != nil {
- return err
- }
- }
-
- etag := s.httpEtag(pdoc, pkgs, importerCount, flashMessages)
- status := http.StatusOK
- if req.Header.Get("If-None-Match") == etag {
- status = http.StatusNotModified
- }
-
- if requestType == humanRequest &&
- pdoc.Name != "" && // not a directory
- pdoc.ProjectRoot != "" && // not a standard package
- !pdoc.IsCmd &&
- len(pdoc.Errors) == 0 &&
- !popularLinkReferral(req) {
- if err := s.db.IncrementPopularScore(pdoc.ImportPath); err != nil {
- log.Printf("ERROR db.IncrementPopularScore(%s): %v", pdoc.ImportPath, err)
- }
- }
- if s.gceLogger != nil {
- s.gceLogger.LogEvent(resp, req, nil)
- }
-
- template := "dir"
- switch {
- case pdoc.IsCmd:
- template = "cmd"
- case pdoc.Name != "":
- template = "pkg"
- }
- template += templateExt(req)
-
- return s.templates.execute(resp, template, status, http.Header{"Etag": {etag}}, map[string]interface{}{
- "flashMessages": flashMessages,
- "pkgs": removeInternal(pdoc, pkgs),
- "pdoc": newTDoc(s.v, pdoc),
- "importerCount": importerCount,
- })
case isView(req, "imports"):
if pdoc.Name == "" {
- break
+ return &httpError{status: http.StatusNotFound}
}
pkgs, err = s.db.Packages(pdoc.Imports)
if err != nil {
@@ -342,7 +298,7 @@
})
case isView(req, "importers"):
if pdoc.Name == "" {
- break
+ return &httpError{status: http.StatusNotFound}
}
pkgs, err = s.db.Importers(importPath)
if err != nil {
@@ -363,7 +319,7 @@
return &httpError{status: http.StatusForbidden}
}
if pdoc.Name == "" {
- break
+ return &httpError{status: http.StatusNotFound}
}
hide := database.ShowAllDeps
switch req.Form.Get("hide") {
@@ -412,8 +368,52 @@
http.Redirect(resp, req, u.String(), http.StatusMovedPermanently)
return nil
}
+ return &httpError{status: http.StatusNotFound}
+ default:
+ importerCount := 0
+ if pdoc.Name != "" {
+ importerCount, err = s.db.ImporterCount(importPath)
+ if err != nil {
+ return err
+ }
+ }
+
+ etag := s.httpEtag(pdoc, pkgs, importerCount, flashMessages)
+ status := http.StatusOK
+ if req.Header.Get("If-None-Match") == etag {
+ status = http.StatusNotModified
+ }
+
+ if requestType == humanRequest &&
+ pdoc.Name != "" && // not a directory
+ pdoc.ProjectRoot != "" && // not a standard package
+ !pdoc.IsCmd &&
+ len(pdoc.Errors) == 0 &&
+ !popularLinkReferral(req) {
+ if err := s.db.IncrementPopularScore(pdoc.ImportPath); err != nil {
+ log.Printf("ERROR db.IncrementPopularScore(%s): %v", pdoc.ImportPath, err)
+ }
+ }
+ if s.gceLogger != nil {
+ s.gceLogger.LogEvent(resp, req, nil)
+ }
+
+ template := "dir"
+ switch {
+ case pdoc.IsCmd:
+ template = "cmd"
+ case pdoc.Name != "":
+ template = "pkg"
+ }
+ template += templateExt(req)
+
+ return s.templates.execute(resp, template, status, http.Header{"Etag": {etag}}, map[string]interface{}{
+ "flashMessages": flashMessages,
+ "pkgs": removeInternal(pdoc, pkgs),
+ "pdoc": newTDoc(s.v, pdoc),
+ "importerCount": importerCount,
+ })
}
- return &httpError{status: http.StatusNotFound}
}
func (s *server) serveRefresh(resp http.ResponseWriter, req *http.Request) error {