httpclient.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 httpclient
  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 HTTPClient struct {
  26. *http.Transport
  27. DocRoot string
  28. schemes []string
  29. }
  30. func New(docRoot string) (self *HTTPClient) {
  31. self = &HTTPClient{
  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 *HTTPClient) Client() *http.Client {
  42. return &http.Client{
  43. Transport: self,
  44. }
  45. }
  46. func (self *HTTPClient) RegisterScheme(scheme string, rt http.RoundTripper) {
  47. self.schemes = append(self.schemes, scheme)
  48. self.RegisterProtocol(scheme, rt)
  49. }
  50. func (self *HTTPClient) 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 *HTTPClient) GetAuthContent(uri string, hash common.Hash) ([]byte, error) {
  59. // retrieve content
  60. content, err := self.Get(uri, "")
  61. if err != nil {
  62. return nil, err
  63. }
  64. // check hash to authenticate content
  65. chash := crypto.Keccak256Hash(content)
  66. if chash != hash {
  67. return nil, fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
  68. }
  69. return content, nil
  70. }
  71. // Get(uri, path) downloads the document at uri, if path is non-empty it
  72. // is interpreted as a filepath to which the contents are saved
  73. func (self *HTTPClient) Get(uri, path string) ([]byte, error) {
  74. // retrieve content
  75. resp, err := self.Client().Get(uri)
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer func() {
  80. if resp != nil {
  81. resp.Body.Close()
  82. }
  83. }()
  84. var content []byte
  85. content, err = ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. return nil, err
  88. }
  89. if resp.StatusCode/100 != 2 {
  90. return content, fmt.Errorf("HTTP error: %s", resp.Status)
  91. }
  92. if path != "" {
  93. var abspath string
  94. abspath, err = filepath.Abs(path)
  95. if err != nil {
  96. return nil, err
  97. }
  98. err = ioutil.WriteFile(abspath, content, 0600)
  99. if err != nil {
  100. return nil, err
  101. }
  102. }
  103. return content, nil
  104. }