docserver.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. package docserver
  17. import (
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "path/filepath"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. )
  25. type DocServer struct {
  26. *http.Transport
  27. DocRoot string
  28. schemes []string
  29. }
  30. func New(docRoot string) (self *DocServer) {
  31. self = &DocServer{
  32. Transport: &http.Transport{},
  33. DocRoot: docRoot,
  34. schemes: []string{"file"},
  35. }
  36. self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot)))
  37. return
  38. }
  39. // Clients should be reused instead of created as needed. Clients are safe for concurrent use by multiple goroutines.
  40. // A Client is higher-level than a RoundTripper (such as Transport) and additionally handles HTTP details such as cookies and redirects.
  41. func (self *DocServer) Client() *http.Client {
  42. return &http.Client{
  43. Transport: self,
  44. }
  45. }
  46. func (self *DocServer) RegisterScheme(scheme string, rt http.RoundTripper) {
  47. self.schemes = append(self.schemes, scheme)
  48. self.RegisterProtocol(scheme, rt)
  49. }
  50. func (self *DocServer) HasScheme(scheme string) bool {
  51. for _, s := range self.schemes {
  52. if s == scheme {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. func (self *DocServer) GetAuthContent(uri string, hash common.Hash) (content []byte, err error) {
  59. // retrieve content
  60. content, err = self.Get(uri, "")
  61. if err != nil {
  62. return
  63. }
  64. // check hash to authenticate content
  65. chash := crypto.Sha3Hash(content)
  66. if chash != hash {
  67. content = nil
  68. err = fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
  69. }
  70. return
  71. }
  72. // Get(uri, path) downloads the document at uri, if path is non-empty it
  73. // is interpreted as a filepath to which the contents are saved
  74. func (self *DocServer) Get(uri, path string) (content []byte, err error) {
  75. // retrieve content
  76. resp, err := self.Client().Get(uri)
  77. defer func() {
  78. if resp != nil {
  79. resp.Body.Close()
  80. }
  81. }()
  82. if err != nil {
  83. return
  84. }
  85. content, err = ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. return
  88. }
  89. if path != "" {
  90. var abspath string
  91. abspath, err = filepath.Abs(path)
  92. ioutil.WriteFile(abspath, content, 0700)
  93. }
  94. return
  95. }