// DialHTTPPath connects to an HTTP RPC server
// at the specified network address and path.
func DialHTTPPath(network, address, path string) (*Client, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, err
}
return dialPath(network, address, path, conn)
}
// DialHTTPSPath connects to an HTTPS RPC server
// at the specified network address and path.
func DialHTTPSPath(network, address, path string, config *tls.Config) (*Client, error) {
conn, err := tls.Dial(network, address, config)
if err != nil {
return nil, err
}
return dialPath(network, address, path, conn)
}
func dialPath(network, address, path string, conn net.Conn) (*rpc.Client, error) {
io.WriteString(conn, "CONNECT "+path+" HTTP/1.0\n\n")
// Require successful HTTP response
// before switching to RPC protocol.
resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
if err == nil && resp.Status == connected {
return NewClient(conn), nil
}
if err == nil {
err = errors.New("unexpected HTTP response: " + resp.Status)
}
conn.Close()
return nil, &net.OpError{
Op: "dial-http",
Net: network + " " + address,
Addr: nil,
Err: err,
}
}