Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
5b0ea104
Commit
5b0ea104
authored
Mar 31, 2015
by
zelig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add docserver using net/http Transport/Roundtrip
parent
3136bae4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
0 deletions
+131
-0
docserver.go
common/docserver/docserver.go
+93
-0
docserver_test.go
common/docserver/docserver_test.go
+38
-0
No files found.
common/docserver/docserver.go
0 → 100644
View file @
5b0ea104
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
,
// Swarm RoundTripper for bzz scheme bzz://mydomain/contact/twitter.json
// "bzz": &bzz.FileServer{},
// swarm remote file system call bzzfs:///etc/passwd
// "bzzfs": &bzzfs.FileServer{},
// restful peer protocol RoundTripper for enode scheme:
// POST suggestPeer DELETE removePeer PUT switch protocols
// RPC - standard protocol provides arc API for self enode (very safe remote server only connects to registered enode)
// POST enode://ae234dfgc56b..@128.56.68.5:3456/eth/getBlock/356eac67890fe
// and remote peer protocol
// POST enode://ae234dfgc56b..@128.56.68.5:3456/eth/getBlock/356eac67890fe
// proxy protoocol
}
)
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
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
}
common/docserver/docserver_test.go
0 → 100644
View file @
5b0ea104
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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment