mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.1 KiB
125 lines
3.1 KiB
9 years ago
|
// Copyright 2015 The go-ethereum Authors
|
||
9 years ago
|
// This file is part of the go-ethereum library.
|
||
9 years ago
|
//
|
||
9 years ago
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||
9 years ago
|
// it under the terms of the GNU Lesser General Public License as published by
|
||
|
// the Free Software Foundation, either version 3 of the License, or
|
||
|
// (at your option) any later version.
|
||
|
//
|
||
9 years ago
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||
9 years ago
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
9 years ago
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
9 years ago
|
// GNU Lesser General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Lesser General Public License
|
||
9 years ago
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||
9 years ago
|
|
||
9 years ago
|
package httpclient
|
||
10 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
10 years ago
|
"path/filepath"
|
||
10 years ago
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/crypto"
|
||
|
)
|
||
|
|
||
9 years ago
|
type HTTPClient struct {
|
||
10 years ago
|
*http.Transport
|
||
|
DocRoot string
|
||
10 years ago
|
schemes []string
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func New(docRoot string) (self *HTTPClient) {
|
||
|
self = &HTTPClient{
|
||
10 years ago
|
Transport: &http.Transport{},
|
||
|
DocRoot: docRoot,
|
||
10 years ago
|
schemes: []string{"file"},
|
||
10 years ago
|
}
|
||
10 years ago
|
self.RegisterProtocol("file", http.NewFileTransport(http.Dir(self.DocRoot)))
|
||
10 years ago
|
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.
|
||
|
|
||
9 years ago
|
func (self *HTTPClient) Client() *http.Client {
|
||
10 years ago
|
return &http.Client{
|
||
|
Transport: self,
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func (self *HTTPClient) RegisterScheme(scheme string, rt http.RoundTripper) {
|
||
10 years ago
|
self.schemes = append(self.schemes, scheme)
|
||
|
self.RegisterProtocol(scheme, rt)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (self *HTTPClient) HasScheme(scheme string) bool {
|
||
10 years ago
|
for _, s := range self.schemes {
|
||
|
if s == scheme {
|
||
|
return true
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
return false
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
func (self *HTTPClient) GetAuthContent(uri string, hash common.Hash) ([]byte, error) {
|
||
10 years ago
|
// retrieve content
|
||
9 years ago
|
content, err := self.Get(uri, "")
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
10 years ago
|
}
|
||
|
|
||
|
// check hash to authenticate content
|
||
9 years ago
|
chash := crypto.Sha3Hash(content)
|
||
10 years ago
|
if chash != hash {
|
||
9 years ago
|
return nil, fmt.Errorf("content hash mismatch %x != %x (exp)", hash[:], chash[:])
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
return content, nil
|
||
10 years ago
|
|
||
|
}
|
||
|
|
||
|
// Get(uri, path) downloads the document at uri, if path is non-empty it
|
||
|
// is interpreted as a filepath to which the contents are saved
|
||
9 years ago
|
func (self *HTTPClient) Get(uri, path string) ([]byte, error) {
|
||
10 years ago
|
// retrieve content
|
||
|
resp, err := self.Client().Get(uri)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
10 years ago
|
defer func() {
|
||
|
if resp != nil {
|
||
|
resp.Body.Close()
|
||
|
}
|
||
|
}()
|
||
9 years ago
|
|
||
9 years ago
|
var content []byte
|
||
10 years ago
|
content, err = ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
9 years ago
|
return nil, err
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
if resp.StatusCode/100 != 2 {
|
||
|
return content, fmt.Errorf("HTTP error: %s", resp.Status)
|
||
|
}
|
||
|
|
||
10 years ago
|
if path != "" {
|
||
|
var abspath string
|
||
|
abspath, err = filepath.Abs(path)
|
||
9 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
err = ioutil.WriteFile(abspath, content, 0600)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
return content, nil
|
||
10 years ago
|
|
||
|
}
|