forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'frontier/natspec' of https://github.com/ethersphere/go-…
…ethereum into ethersphere-frontier/natspec
- Loading branch information
Showing
18 changed files
with
5,095 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package docserver | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
// http://golang.org/pkg/net/http/#RoundTripper | ||
var ( | ||
schemes = map[string]func(*DocServer) http.RoundTripper{ | ||
// Simple File server from local disk file:///etc/passwd :) | ||
"file": fileServerOnDocRoot, | ||
} | ||
) | ||
|
||
func fileServerOnDocRoot(ds *DocServer) http.RoundTripper { | ||
return http.NewFileTransport(http.Dir(ds.DocRoot)) | ||
} | ||
|
||
type DocServer struct { | ||
*http.Transport | ||
DocRoot string | ||
} | ||
|
||
func New(docRoot string) (self *DocServer, err error) { | ||
self = &DocServer{ | ||
Transport: &http.Transport{}, | ||
DocRoot: docRoot, | ||
} | ||
err = self.RegisterProtocols(schemes) | ||
return | ||
} | ||
|
||
// Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines. | ||
|
||
// A Client is higher-level than a RoundTripper (such as Transport) and additionally handles HTTP details such as cookies and redirects. | ||
|
||
func (self *DocServer) Client() *http.Client { | ||
return &http.Client{ | ||
Transport: self, | ||
} | ||
} | ||
|
||
func (self *DocServer) RegisterProtocols(schemes map[string]func(*DocServer) http.RoundTripper) (err error) { | ||
for scheme, rtf := range schemes { | ||
self.RegisterProtocol(scheme, rtf(self)) | ||
} | ||
return | ||
} | ||
|
||
func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) { | ||
// retrieve content | ||
resp, err := self.Client().Get(uri) | ||
defer func() { | ||
if resp != nil { | ||
resp.Body.Close() | ||
} | ||
}() | ||
if err != nil { | ||
return | ||
} | ||
content, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// check hash to authenticate content | ||
hashbytes := crypto.Sha3(content) | ||
var chash common.Hash | ||
copy(chash[:], hashbytes) | ||
if chash != hash { | ||
content = nil | ||
err = fmt.Errorf("content hash mismatch") | ||
} | ||
|
||
return | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package docserver | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
func TestGetAuthContent(t *testing.T) { | ||
text := "test" | ||
hash := common.Hash{} | ||
copy(hash[:], crypto.Sha3([]byte(text))) | ||
ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm) | ||
|
||
ds, err := New("/tmp/") | ||
content, err := ds.GetAuthContent("file:///test.content", hash) | ||
if err != nil { | ||
t.Errorf("no error expected, got %v", err) | ||
} | ||
if string(content) != text { | ||
t.Errorf("incorrect content. expected %v, got %v", text, string(content)) | ||
} | ||
|
||
hash = common.Hash{} | ||
content, err = ds.GetAuthContent("file:///test.content", hash) | ||
expected := "content hash mismatch" | ||
if err == nil { | ||
t.Errorf("expected error, got nothing") | ||
} else { | ||
if err.Error() != expected { | ||
t.Errorf("expected error '%s' got '%v'", expected, err) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.