From 8382c797fcc415802be875a72a929357de49f972 Mon Sep 17 00:00:00 2001
From: verokarhu <andreas.metsala@gmail.com>
Date: Sun, 28 Sep 2014 12:26:59 +0300
Subject: [PATCH] use dagreader in servehttp

---
 server/http/http.go      | 13 +++++++++++--
 server/http/http_test.go | 14 ++++++++++++++
 server/http/ipfs.go      |  5 +++++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/server/http/http.go b/server/http/http.go
index a6056bcaa..591d1660a 100644
--- a/server/http/http.go
+++ b/server/http/http.go
@@ -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
+	}
+
+	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
 	}
 
-	// TODO: return json object containing the tree data if it's a folder
-	w.Write(nd.Data)
+	io.Copy(w, dr)
 }
 
 func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
diff --git a/server/http/http_test.go b/server/http/http_test.go
index f62ba6c77..71512978b 100644
--- a/server/http/http_test.go
+++ b/server/http/http_test.go
@@ -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("")
+}
diff --git a/server/http/ipfs.go b/server/http/ipfs.go
index e9c243993..6a177023f 100644
--- a/server/http/ipfs.go
+++ b/server/http/ipfs.go
@@ -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)
+}
-- 
GitLab