1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-26 23:53:19 +08:00

use dagreader in servehttp

This commit is contained in:
verokarhu
2014-09-28 12:26:59 +03:00
parent 7b22bdc68b
commit 8382c797fc
3 changed files with 30 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package http
import (
"fmt"
"io"
"net/http"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
@ -30,11 +31,19 @@ func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
nd, err := i.ResolvePath(path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err)
return
}
// TODO: return json object containing the tree data if it's a folder
w.Write(nd.Data)
dr, err := i.NewDagReader(nd)
if err != nil {
// TODO: return json object containing the tree data if it's a directory (err == ErrIsDir)
w.WriteHeader(http.StatusInternalServerError)
fmt.Println(err)
return
}
io.Copy(w, dr)
}
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -1,6 +1,7 @@
package http
import (
"bytes"
"errors"
"io"
"io/ioutil"
@ -25,6 +26,7 @@ func TestServeHTTP(t *testing.T) {
tests := []test{
{"/", http.StatusInternalServerError, "", ""},
{"/hash", http.StatusOK, "", "some fine data"},
{"/hash2", http.StatusInternalServerError, "", ""},
}
for _, test := range tests {
@ -74,6 +76,10 @@ func (i *testIpfsHandler) ResolvePath(path string) (*dag.Node, error) {
return &dag.Node{Data: []byte("some fine data")}, nil
}
if path == "/hash2" {
return &dag.Node{Data: []byte("data that breaks dagreader")}, nil
}
return nil, errors.New("")
}
@ -92,3 +98,11 @@ func (i *testIpfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
return "", errors.New("")
}
func (i *testIpfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
if string(nd.Data) != "data that breaks dagreader" {
return bytes.NewReader(nd.Data), nil
}
return nil, errors.New("")
}

View File

@ -13,6 +13,7 @@ type ipfs interface {
ResolvePath(string) (*dag.Node, error)
NewDagFromReader(io.Reader) (*dag.Node, error)
AddNodeToDAG(nd *dag.Node) (u.Key, error)
NewDagReader(nd *dag.Node) (io.Reader, error)
}
type ipfsHandler struct {
@ -30,3 +31,7 @@ func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
return i.node.DAG.Add(nd)
}
func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
return dag.NewDagReader(nd, i.node.DAG)
}