{"record":{"id":"bab06997594cc20a","repo":"geektutu/7days-golang","slug":"httppool-serving-unexpected-path-r-url-path-bab069","errorCode":null,"errorMessage":"HTTPPool serving unexpected path: \" + r.URL.Path","messagePattern":"HTTPPool serving unexpected path: \" \\+ r\\.URL\\.Path","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-cache/day7-proto-buf/geecache/http.go","lineNumber":48,"sourceCode":"}\n\n// NewHTTPPool initializes an HTTP pool of peers.\nfunc NewHTTPPool(self string) *HTTPPool {\n\treturn &HTTPPool{\n\t\tself:     self,\n\t\tbasePath: defaultBasePath,\n\t}\n}\n\n// Log info with server name\nfunc (p *HTTPPool) Log(format string, v ...interface{}) {\n\tlog.Printf(\"[Server %s] %s\", p.self, fmt.Sprintf(format, v...))\n}\n\n// ServeHTTP handle all http requests\nfunc (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {\n\tif !strings.HasPrefix(r.URL.Path, p.basePath) {\n\t\tpanic(\"HTTPPool serving unexpected path: \" + r.URL.Path)\n\t}\n\tp.Log(\"%s %s\", r.Method, r.URL.Path)\n\t// /<basepath>/<groupname>/<key> required\n\tparts := strings.SplitN(r.URL.Path[len(p.basePath):], \"/\", 2)\n\tif len(parts) != 2 {\n\t\thttp.Error(w, \"bad request\", http.StatusBadRequest)\n\t\treturn\n\t}\n\n\tgroupName := parts[0]\n\tkey := parts[1]\n\n\tgroup := GetGroup(groupName)\n\tif group == nil {\n\t\thttp.Error(w, \"no such group: \"+groupName, http.StatusNotFound)\n\t\treturn\n\t}\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-cache/day7-proto-buf/geecache/http.go#L30-L66","documentation":"HTTPPool.ServeHTTP panics when the request path does not start with the pool's basePath (default '/_geecache/'). The handler only serves the cache's internal HTTP API; any other path reaching it means the HTTP routing was misconfigured. This fail-fast panic indicates the server was mounted at the wrong location or a client/proxy is sending wrong paths.","triggerScenarios":"Registering the HTTPPool handler at '/' (or a prefix different from p.basePath) and receiving requests outside basePath; a load balancer forwarding non-cache paths to the cache server; a client constructed with a mismatched base URL prefix.","commonSituations":"Mounting httpPool on http.HandleFunc(\"/\") while basePath is '/_geecache/'; version upgrades where the default basePath changed; external health checks hitting '/' on the cache port.","solutions":["Register the HTTPPool handler with http.Handle(pool.BasePath, pool) so only matching paths reach it","If you must mount at a broader path, set p.basePath to the prefix the pool actually serves (Set/construct with matching basePath)","Check upstream proxies/load balancers to ensure they only route cache-prefixed paths to this handler","Wrap ServeHTTP in a recover middleware only for production hardening; fix routing rather than swallowing the panic"],"exampleFix":"// before\nhttp.Handle(\"/\", httpPool) // any path reaching ServeHTTP can panic\n// after\nhttp.Handle(httpPool.BasePath, httpPool)","handlingStrategy":"validation","validationCode":"mux.HandleFunc(\"/_geecache/\", pool.ServeHTTP) // only route basePath-prefixed paths\n// or before forwarding:\nif !strings.HasPrefix(req.URL.Path, pool.BasePath) {\n    http.NotFound(w, r)\n    return\n}","typeGuard":null,"tryCatchPattern":"func safeServe(pool *geecache.HTTPPool) http.HandlerFunc {\n    return func(w http.ResponseWriter, r *http.Request) {\n        defer func() { if recover() != nil { http.Error(w, \"bad path\", http.StatusNotFound) } }()\n        pool.ServeHTTP(w, r)\n    }\n}","preventionTips":["Mount HTTPPool with http.Handle(pool.BasePath, pool)","Dedicate a separate port/path for cache traffic","Verify proxy routing rules forward only cache-prefixed paths"],"tags":["go","panic","http","routing"],"backgroundTag":"unexpected-http-path","analyzedSha":"cf3644382101dc13e7fd92e8f5c66cabc51bcd3b","analyzedAt":"2026-09-03T18:31:24.087Z","contentChangedAt":"2026-09-03T18:31:24.087Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}