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)
|
||||
}
|
||||
50
internal/cas/mesh_fetcher.go
Normal file
50
internal/cas/mesh_fetcher.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package cas
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.send.nrw/sendnrw/decent-websrv/internal/mesh"
|
||||
"git.send.nrw/sendnrw/decent-websrv/internal/security"
|
||||
)
|
||||
|
||||
type MeshFetcher struct {
|
||||
Ring *mesh.Rendezvous
|
||||
HTTP *http.Client
|
||||
Verifier *security.MeshVerifier
|
||||
Self mesh.NodeInfo
|
||||
}
|
||||
|
||||
func (f *MeshFetcher) FetchTo(hash string, w http.ResponseWriter) bool {
|
||||
owners := f.Ring.Owners(hash, 3)
|
||||
if len(owners) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, id := range owners {
|
||||
if id == f.Self.NodeID {
|
||||
continue
|
||||
}
|
||||
// resolve id to node
|
||||
// In this minimal starter we assume Mesh URL can be derived externally; in a fuller impl, pass a Catalog here.
|
||||
// For simplicity, try owner as URL directly if it's already a URL; otherwise skip.
|
||||
u := id // If your ring holds IDs, you should map ID->Node (MeshURL); keep simple here.
|
||||
if !strings.HasPrefix(u, "http") {
|
||||
continue
|
||||
}
|
||||
path := "/_mesh/cas/" + hash
|
||||
req, _ := http.NewRequest(http.MethodGet, strings.TrimRight(u, "/")+path, nil)
|
||||
f.Verifier.SignOutgoing(req, "", f.Self.NodeID) // sign GET with empty body-hash
|
||||
resp, err := f.HTTP.Do(req)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode == 200 {
|
||||
defer resp.Body.Close()
|
||||
io.Copy(w, resp.Body)
|
||||
return true
|
||||
}
|
||||
resp.Body.Close()
|
||||
}
|
||||
return false
|
||||
}
|
||||
57
internal/cas/store.go
Normal file
57
internal/cas/store.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package cas
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Store struct{ dir string }
|
||||
|
||||
func New(dir string) *Store { os.MkdirAll(dir, 0o755); return &Store{dir: dir} }
|
||||
|
||||
func (s *Store) pathOf(hash string) string {
|
||||
if len(hash) < 2 {
|
||||
return filepath.Join(s.dir, hash)
|
||||
}
|
||||
return filepath.Join(s.dir, hash[:2], hash)
|
||||
}
|
||||
|
||||
func (s *Store) Has(hash string) bool { _, err := os.Stat(s.pathOf(hash)); return err == nil }
|
||||
|
||||
func (s *Store) PutStream(r io.Reader) (string, error) {
|
||||
h := sha256.New()
|
||||
tmp, err := os.CreateTemp(s.dir, "put-*.tmp")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.Remove(tmp.Name())
|
||||
mw := io.MultiWriter(h, tmp)
|
||||
if _, err := io.Copy(mw, r); err != nil {
|
||||
tmp.Close()
|
||||
return "", err
|
||||
}
|
||||
tmp.Close()
|
||||
sum := hex.EncodeToString(h.Sum(nil))
|
||||
dst := s.pathOf(sum)
|
||||
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.Rename(tmp.Name(), dst); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func (s *Store) Get(hash string, w io.Writer) error {
|
||||
f, err := os.Open(s.pathOf(hash))
|
||||
if err != nil {
|
||||
return errors.New("not found")
|
||||
}
|
||||
defer f.Close()
|
||||
_, err = io.Copy(w, f)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user