From 84c8a1452e2dc1abbc80f80d4134654a28cc5fe5 Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Tue, 10 Feb 2015 17:31:20 +0100 Subject: [PATCH] GET requests served from Swarm --- bzz/httpaccess.go | 43 +++++++++++++++++++++++++++++++++++++++++++ eth/backend.go | 5 ++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 bzz/httpaccess.go diff --git a/bzz/httpaccess.go b/bzz/httpaccess.go new file mode 100644 index 0000000000..072fa9fc23 --- /dev/null +++ b/bzz/httpaccess.go @@ -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) +} diff --git a/eth/backend.go b/eth/backend.go index 8029c25ca2..a6d117eee4 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -135,11 +135,14 @@ func New(config *Config) (*Ethereum, error) { eth.blockPool = NewBlockPool(hasBlock, insertChain, ezp.Verify) ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool) + netStore := bzz.NewNetStore(config.DataDir + "/bzz") protocols := []p2p.Protocol{ ethProto, 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) if err != nil {