github.com/Cloud-Foundations/Dominator@v0.3.4/lib/net/rpc/rpc.go (about)

     1  package rpc
     2  
     3  import (
     4  	"bufio"
     5  	"errors"
     6  	"io"
     7  	gonet "net"
     8  	"net/http"
     9  	"net/rpc"
    10  
    11  	"github.com/Cloud-Foundations/Dominator/lib/net"
    12  )
    13  
    14  func dial(dialer net.Dialer, network, address string) (*rpc.Client, error) {
    15  	conn, err := dialer.Dial(network, address)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	return rpc.NewClient(conn), nil
    20  }
    21  
    22  func dialHTTPPath(dialer net.Dialer, network, address, path string) (
    23  	*rpc.Client, error) {
    24  	var err error
    25  	conn, err := dialer.Dial(network, address)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	io.WriteString(conn, "CONNECT "+path+" HTTP/1.0\n\n")
    30  	// Require successful HTTP response before switching to RPC protocol
    31  	resp, err := http.ReadResponse(
    32  		bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
    33  	// The status value is undocumented and subject to change!
    34  	if err == nil && resp.Status == "200 Connected to Go RPC" {
    35  		return rpc.NewClient(conn), nil
    36  	}
    37  	if err == nil {
    38  		err = errors.New("unexpected HTTP response: " + resp.Status)
    39  	}
    40  	conn.Close()
    41  	return nil, &gonet.OpError{
    42  		Op:   "dial-http",
    43  		Net:  network + " " + address,
    44  		Addr: nil,
    45  		Err:  err,
    46  	}
    47  }