geektutu/7days-golang · warning

bad request

Error message

bad request

What it means

Same as the day3 'bad request': HTTPPool.ServeHTTP (day4-consistent-hash version) requires /<basePath>/<groupName>/<key>. When SplitN yields fewer than 2 parts after basePath, it replies 400 'bad request'.

Source

Thrown at gee-cache/day4-consistent-hash/geecache/http.go:41

		basePath: defaultBasePath,
	}
}

// Log info with server name
func (p *HTTPPool) Log(format string, v ...interface{}) {
	log.Printf("[Server %s] %s", p.self, fmt.Sprintf(format, v...))
}

// ServeHTTP handle all http requests
func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if !strings.HasPrefix(r.URL.Path, p.basePath) {
		panic("HTTPPool serving unexpected path: " + r.URL.Path)
	}
	p.Log("%s %s", r.Method, r.URL.Path)
	// /<basepath>/<groupname>/<key> required
	parts := strings.SplitN(r.URL.Path[len(p.basePath):], "/", 2)
	if len(parts) != 2 {
		http.Error(w, "bad request", http.StatusBadRequest)
		return
	}

	groupName := parts[0]
	key := parts[1]

	group := GetGroup(groupName)
	if group == nil {
		http.Error(w, "no such group: "+groupName, http.StatusNotFound)
		return
	}

	view, err := group.Get(key)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

View on GitHub (pinned to cf36443821)

Solutions

  1. Use the full path /<basePath>/<group>/<key> in requests.
  2. Point health checks at a separate route/handler, not the geecache HTTPPool.
  3. Check ingress/proxy rewrite rules for stripped path segments.
  4. If keys may contain '/', percent-encode them so the split yields exactly group and key.

Example fix

// before
GET /_geecache/groups
// after
GET /_geecache/groups/mykey
Defensive patterns

Strategy: validation

Validate before calling

// validate path shape before requesting (day4 node)
func validCacheURL(base, group, key string) bool {
    return group != "" && key != "" && !strings.ContainsAny(key, "/?#")
}

Prevention

When it happens

Trigger: Request path such as /_geecache/ or /_geecache/mygroup — after basePath there aren't two '/'-separated segments.

Common situations: Load-balancer health probes hitting the cache endpoint; client omitting the key; proxy path rewriting dropping a segment; manual curl tests with an incomplete URL.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/56f79ed82fd11cf1. Report an issue: GitHub.