Adding support for net/rpc over HTTPS

211 views
Skip to first unread message

Adam Thomason

unread,
Apr 28, 2014, 9:48:32 PM4/28/14
to golan...@googlegroups.com
While net/rpc supports HTTPS trivially in the server, there's no built-in way for a client to connect to one since rpc.DialHTTPPath calls net.Dial itself. With some duplicated code from client.go it's possible to create a working DialHTTPSPath in user code, though this is suboptimal since DialHTTPPath references an unexported package variable which must be duplicated.

Would a patch to add DialHTTPSPath (and possibly DialHTTPS) be welcome?


// 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,
}
}

Adam Thomason

unread,
May 5, 2014, 8:00:07 PM5/5/14
to golan...@googlegroups.com
Since there were no objections, I've submitted this for review at https://codereview.appspot.com/100140043/.
Reply all
Reply to author
Forward
0 new messages