mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +08:00
use dagreader in servehttp
This commit is contained in:
@ -2,6 +2,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
|
"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)
|
nd, err := i.ResolvePath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: return json object containing the tree data if it's a folder
|
dr, err := i.NewDagReader(nd)
|
||||||
w.Write(nd.Data)
|
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) {
|
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@ -25,6 +26,7 @@ func TestServeHTTP(t *testing.T) {
|
|||||||
tests := []test{
|
tests := []test{
|
||||||
{"/", http.StatusInternalServerError, "", ""},
|
{"/", http.StatusInternalServerError, "", ""},
|
||||||
{"/hash", http.StatusOK, "", "some fine data"},
|
{"/hash", http.StatusOK, "", "some fine data"},
|
||||||
|
{"/hash2", http.StatusInternalServerError, "", ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
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
|
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("")
|
return nil, errors.New("")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,3 +98,11 @@ func (i *testIpfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
|
|||||||
|
|
||||||
return "", errors.New("")
|
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("")
|
||||||
|
}
|
||||||
|
@ -13,6 +13,7 @@ type ipfs interface {
|
|||||||
ResolvePath(string) (*dag.Node, error)
|
ResolvePath(string) (*dag.Node, error)
|
||||||
NewDagFromReader(io.Reader) (*dag.Node, error)
|
NewDagFromReader(io.Reader) (*dag.Node, error)
|
||||||
AddNodeToDAG(nd *dag.Node) (u.Key, error)
|
AddNodeToDAG(nd *dag.Node) (u.Key, error)
|
||||||
|
NewDagReader(nd *dag.Node) (io.Reader, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ipfsHandler struct {
|
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) {
|
func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
|
||||||
return i.node.DAG.Add(nd)
|
return i.node.DAG.Add(nd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
|
||||||
|
return dag.NewDagReader(nd, i.node.DAG)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user