mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 20:26:41 +00:00
initial commit for archive/bzzpush
This commit is contained in:
parent
74e8f1c3ea
commit
8abb38e5d7
1 changed files with 20 additions and 16 deletions
|
|
@ -15,8 +15,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
port = ":8500"
|
port = ":8500"
|
||||||
manifest_type = "application/bzz-manifest+json"
|
manifestType = "application/bzz-manifest+json"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -32,11 +32,15 @@ type sequentialReader struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type manifest struct {
|
||||||
|
Entries []manifestEntry
|
||||||
|
}
|
||||||
|
|
||||||
type manifestEntry struct {
|
type manifestEntry struct {
|
||||||
Path string
|
Path string
|
||||||
Hash string
|
Hash string
|
||||||
Content_type string
|
ContentType string
|
||||||
Status int16
|
Status int16
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *sequentialReader) ReadAt(target []byte, off int64) (n int, err error) {
|
func (self *sequentialReader) ReadAt(target []byte, off int64) (n int, err error) {
|
||||||
|
|
@ -132,8 +136,8 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
|
||||||
for {
|
for {
|
||||||
manifestReader := dpa.Retrieve(key)
|
manifestReader := dpa.Retrieve(key)
|
||||||
// TODO check size for oversized manifests
|
// TODO check size for oversized manifests
|
||||||
manifest := make([]byte, manifestReader.Size())
|
manifestData := make([]byte, manifestReader.Size())
|
||||||
size, err := manifestReader.Read(manifest)
|
size, err := manifestReader.Read(manifestData)
|
||||||
if int64(size) < manifestReader.Size() {
|
if int64(size) < manifestReader.Size() {
|
||||||
dpaLogger.Debugf("Swarm: Manifest %s not found.", name)
|
dpaLogger.Debugf("Swarm: Manifest %s not found.", name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -145,28 +149,28 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dpaLogger.Debugf("Swarm: Manifest %s retrieved.", name)
|
dpaLogger.Debugf("Swarm: Manifest %s retrieved.", name)
|
||||||
manifestEntries := make([]manifestEntry, 0)
|
man := manifest{}
|
||||||
err = json.Unmarshal(manifest, &manifestEntries)
|
err = json.Unmarshal(manifestData, &man)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dpaLogger.Debugf("Swarm: Manifest %s is malformed.", name)
|
dpaLogger.Debugf("Swarm: Manifest %s is malformed.", name)
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
dpaLogger.Debugf("Swarm: Manifest %s has %d entries.", name, len(manifestEntries))
|
dpaLogger.Debugf("Swarm: Manifest %s has %d entries.", name, len(man.Entries))
|
||||||
}
|
}
|
||||||
var mimeType string
|
var mimeType string
|
||||||
key = nil
|
key = nil
|
||||||
prefix := 0
|
prefix := 0
|
||||||
status := int16(404)
|
status := int16(404)
|
||||||
MANIFEST_ENTRIES:
|
MANIFEST_ENTRIES:
|
||||||
for _, entry := range manifestEntries {
|
for _, entry := range man.Entries {
|
||||||
if !hashMatcher.MatchString(entry.Hash) {
|
if !hashMatcher.MatchString(entry.Hash) {
|
||||||
// hash is mandatory
|
// hash is mandatory
|
||||||
continue MANIFEST_ENTRIES
|
continue MANIFEST_ENTRIES
|
||||||
}
|
}
|
||||||
if entry.Content_type == "" {
|
if entry.ContentType == "" {
|
||||||
// content type defaults to manifest
|
// content type defaults to manifest
|
||||||
entry.Content_type = manifest_type
|
entry.ContentType = manifestType
|
||||||
}
|
}
|
||||||
if entry.Status == 0 {
|
if entry.Status == 0 {
|
||||||
// status defaults to 200
|
// status defaults to 200
|
||||||
|
|
@ -178,14 +182,14 @@ func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
|
||||||
prefix = pathLen
|
prefix = pathLen
|
||||||
key = ethutil.Hex2Bytes(entry.Hash)
|
key = ethutil.Hex2Bytes(entry.Hash)
|
||||||
dpaLogger.Debugf("Swarm: Payload hash %064x", key)
|
dpaLogger.Debugf("Swarm: Payload hash %064x", key)
|
||||||
mimeType = entry.Content_type
|
mimeType = entry.ContentType
|
||||||
status = entry.Status
|
status = entry.Status
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if key == nil {
|
if key == nil {
|
||||||
http.Error(w, "Object "+uri+" not found.", http.StatusNotFound)
|
http.Error(w, "Object "+uri+" not found.", http.StatusNotFound)
|
||||||
break MANIFEST_RESOLUTION
|
break MANIFEST_RESOLUTION
|
||||||
} else if mimeType != manifest_type {
|
} else if mimeType != manifestType {
|
||||||
w.Header().Set("Content-Type", mimeType)
|
w.Header().Set("Content-Type", mimeType)
|
||||||
dpaLogger.Debugf("Swarm: HTTP Status %d", status)
|
dpaLogger.Debugf("Swarm: HTTP Status %d", status)
|
||||||
w.WriteHeader(int(status))
|
w.WriteHeader(int(status))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue