Skip to content

Commit

Permalink
don't require explicit empty blob uploads for HTTP either
Browse files Browse the repository at this point in the history
The gRPC API implementation has a hack to handle clients (like bazel?)
which in some cases don't upload the empty blob, but do reference it.
It is kind of OK not to upload the empty blob, since it can be recognised
by hash and trivially regenerated.

Let's imlpement the hack for the HTTP API also.

Implements buchgr#233.
  • Loading branch information
mostynb committed Mar 23, 2020
1 parent 6715476 commit cbdf639
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ func (h *httpCache) CacheHandler(w http.ResponseWriter, r *http.Request) {
return
}

if kind == cache.CAS && hash == emptySha256 {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", "0")
w.Write([]byte{}) // TODO: is this actually necessary?
return
}

rdr, sizeBytes, err := h.cache.Get(kind, hash)
if err != nil {
if e, ok := err.(*cache.Error); ok {
Expand All @@ -217,14 +224,27 @@ func (h *httpCache) CacheHandler(w http.ResponseWriter, r *http.Request) {

h.logResponse(http.StatusOK, r)
case http.MethodPut:
if r.ContentLength == -1 {
contentLength := r.ContentLength

if contentLength == -1 {
// We need the content-length header to make sure we have enough disk space.
msg := fmt.Sprintf("PUT without Content-Length (key = %s)", path(kind, hash))
http.Error(w, msg, http.StatusBadRequest)
h.errorLogger.Printf("PUT %s: %s", path(kind, hash), msg)
return
}
contentLength := r.ContentLength

if contentLength == 0 && kind == cache.CAS {
if hash == emptySha256 {
w.WriteHeader(http.StatusOK)
return
}

msg := fmt.Sprintf("Invalid empty blob hash: \"%s\"", hash)
http.Error(w, msg, http.StatusBadRequest)
h.errorLogger.Printf("PUT %s: %s", path(kind, hash), msg)
return
}

rc := r.Body
if h.validateAC && kind == cache.AC {
Expand Down Expand Up @@ -280,6 +300,13 @@ func (h *httpCache) CacheHandler(w http.ResponseWriter, r *http.Request) {
return
}

if kind == cache.CAS && hash == emptySha256 {
w.Header().Set("Content-Length", "0")
w.WriteHeader(http.StatusOK)
h.logResponse(http.StatusOK, r)
return
}

// Unvalidated path:

ok, size := h.cache.Contains(kind, hash)
Expand Down

0 comments on commit cbdf639

Please sign in to comment.