51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
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
|
|
}
|