This commit is contained in:
58
internal/cas/http.go
Normal file
58
internal/cas/http.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package cas
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Fetcher interface {
|
||||
FetchTo(hash string, w http.ResponseWriter) bool
|
||||
}
|
||||
|
||||
type HTTP struct {
|
||||
S *Store
|
||||
Fetcher Fetcher // optional, for federation
|
||||
}
|
||||
|
||||
// Public: GET /c/<hash>
|
||||
func (h *HTTP) Serve(w http.ResponseWriter, r *http.Request) {
|
||||
hash := strings.TrimPrefix(r.URL.Path, "/c/")
|
||||
if hash == "" {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
if h.S.Has(hash) {
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
||||
_ = h.S.Get(hash, w)
|
||||
return
|
||||
}
|
||||
if h.Fetcher != nil && h.Fetcher.FetchTo(hash, w) {
|
||||
return
|
||||
}
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
|
||||
// Mesh: GET /_mesh/cas/<hash> → raw bytes
|
||||
func (h *HTTP) MeshGet(w http.ResponseWriter, r *http.Request) {
|
||||
hash := strings.TrimPrefix(r.URL.Path, "/_mesh/cas/")
|
||||
if hash == "" {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
if !h.S.Has(hash) {
|
||||
w.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
_ = h.S.Get(hash, w)
|
||||
}
|
||||
|
||||
// Mesh: POST /_mesh/cas/put → returns hash as text
|
||||
func (h *HTTP) MeshPut(w http.ResponseWriter, r *http.Request) {
|
||||
hash, err := h.S.PutStream(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, hash)
|
||||
}
|
||||
Reference in New Issue
Block a user