github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/common/docserver/docserver.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package docserver 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net/http" 23 "path/filepath" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/crypto" 27 ) 28 29 type DocServer struct { 30 *http.Transport 31 DocRoot string 32 schemes []string 33 } 34 35 func New(docRoot string) (self *DocServer) { 36 self = &DocServer{ 37 Transport: &http.Transport{}, 38 DocRoot: docRoot, 39 schemes: []string{"file"}, 40 } 41 self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot))) 42 return 43 } 44 45 // Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines. 46 47 // A Client is higher-level than a RoundTripper (such as Transport) and additionally handles HTTP details such as cookies and redirects. 48 49 func (self *DocServer) Client() *http.Client { 50 return &http.Client{ 51 Transport: self, 52 } 53 } 54 55 func (self *DocServer) RegisterScheme(scheme string, rt http.RoundTripper) { 56 self.schemes = append(self.schemes, scheme) 57 self.RegisterProtocol(scheme, rt) 58 } 59 60 func (self *DocServer) HasScheme(scheme string) bool { 61 for _, s := range self.schemes { 62 if s == scheme { 63 return true 64 } 65 } 66 return false 67 } 68 69 func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) { 70 // retrieve content 71 content, err = self.Get(uri, "") 72 if err != nil { 73 return 74 } 75 76 // check hash to authenticate content 77 chash := crypto.Sha3Hash(content) 78 if chash != hash { 79 content = nil 80 err = fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:]) 81 } 82 83 return 84 85 } 86 87 // Get(uri, path) downloads the document at uri, if path is non-empty it 88 // is interpreted as a filepath to which the contents are saved 89 func (self *DocServer) Get(uri, path string) (content []byte, err error) { 90 // retrieve content 91 resp, err := self.Client().Get(uri) 92 93 defer func() { 94 if resp != nil { 95 resp.Body.Close() 96 } 97 }() 98 if err != nil { 99 return 100 } 101 content, err = ioutil.ReadAll(resp.Body) 102 if err != nil { 103 return 104 } 105 106 if path != "" { 107 var abspath string 108 abspath, err = filepath.Abs(path) 109 ioutil.WriteFile(abspath, content, 0700) 110 } 111 112 return 113 114 }