github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/api/http/roundtripper.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:43</date>
    10  //</624450111920934912>
    11  
    12  
    13  package http
    14  
    15  import (
    16  	"fmt"
    17  	"net/http"
    18  
    19  	"github.com/ethereum/go-ethereum/swarm/log"
    20  )
    21  
    22  /*
    23  注册BZZ URL方案的HTTP往返器
    24  请参阅https://github.com/ethereum/go-ethereum/issues/2040
    25  用途:
    26  
    27  进口(
    28   “github.com/ethereum/go-ethereum/common/httpclient”
    29   “github.com/ethereum/go-ethereum/swarm/api/http”
    30  )
    31  客户端:=httpclient.new()
    32  //对于本地运行的(私有)Swarm代理
    33  client.registerscheme(“bzz”,&http.roundtripper port:port)
    34  client.registerscheme(“bzz不可变”,&http.roundtripper port:port)
    35  client.registerscheme(“bzz raw”,&http.roundtripper port:port)
    36  
    37  您给往返者的端口是Swarm代理正在监听的端口。
    38  如果主机为空,则假定为localhost。
    39  
    40  使用公共网关,上面的几条线为您提供了最精简的
    41  BZZ方案感知只读HTTP客户端。你真的只需要这个
    42  如果你需要本地群访问BZZ地址。
    43  **/
    44  
    45  
    46  type RoundTripper struct {
    47  	Host string
    48  	Port string
    49  }
    50  
    51  func (self *RoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error) {
    52  	host := self.Host
    53  	if len(host) == 0 {
    54  		host = "localhost"
    55  	}
    56  url := fmt.Sprintf("http://%s:%s/%s/%s/%s”,主机,自身端口,请求协议,请求URL.host,请求URL.path)
    57  	log.Info(fmt.Sprintf("roundtripper: proxying request '%s' to '%s'", req.RequestURI, url))
    58  	reqProxy, err := http.NewRequest(req.Method, url, req.Body)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return http.DefaultClient.Do(reqProxy)
    63  }
    64