docserver.go 2.8 KB

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