GET requests served from Swarm

This commit is contained in:
Daniel A. Nagy 2015-02-10 17:31:20 +01:00
parent dd1f9885af
commit 84c8a1452e
2 changed files with 47 additions and 1 deletions

43
bzz/httpaccess.go Normal file
View file

@ -0,0 +1,43 @@
/*
A simple http server interface to Swarm
*/
package bzz
import (
"github.com/ethereum/go-ethereum/ethutil"
"net/http"
"regexp"
"time"
)
const (
port = ":8500"
)
var (
uriMatcher = regexp.MustCompile("^/raw/[0-9A-Fa-f]{64}$")
)
func handler(w http.ResponseWriter, r *http.Request, dpa *DPA) {
uri := r.RequestURI
switch {
case r.Method == "PUT":
case r.Method == "GET":
if uriMatcher.MatchString(uri) {
name := uri[5:]
key := ethutil.Hex2Bytes(name)
http.ServeContent(w, r, name+".bin", time.Unix(0, 0), dpa.Retrieve(key))
} else {
http.Error(w, "Object "+uri+" not found.", http.StatusNotFound)
}
default:
http.Error(w, "Method "+r.Method+" is not supported.", http.StatusBadRequest)
}
}
func StartHttpServer(dpa *DPA) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler(w, r, dpa)
})
http.ListenAndServe(port, nil)
}

View file

@ -135,11 +135,14 @@ func New(config *Config) (*Ethereum, error) {
eth.blockPool = NewBlockPool(hasBlock, insertChain, ezp.Verify) eth.blockPool = NewBlockPool(hasBlock, insertChain, ezp.Verify)
ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool) ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool)
netStore := bzz.NewNetStore(config.DataDir + "/bzz")
protocols := []p2p.Protocol{ protocols := []p2p.Protocol{
ethProto, ethProto,
eth.whisper.Protocol(), eth.whisper.Protocol(),
bzz.BzzProtocol(bzz.NewNetStore(config.DataDir + "/bzz")), bzz.BzzProtocol(netStore),
} }
// dpa :=
bzz.StartHttpServer(nil)
nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway) nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway)
if err != nil { if err != nil {