// ServeHTTP implements http.Handler's ServeHTTP func (h *rpcHandler) ServeHTTP(c http.ResponseWriter, req *http.Request) { if req.Method != "CONNECT" { c.Header().Set("Content-Type", "text/plain; charset=utf-8") c.WriteHeader(http.StatusMethodNotAllowed) io.WriteString(c, "405 must CONNECT\n") return } conn, _, err := c.(http.Hijacker).Hijack() if err != nil { log.Errorf("rpc hijacking %s: %v", req.RemoteAddr, err) return } io.WriteString(conn, "HTTP/1.0 "+connected+"\n\n") codec := h.cFactory(NewBufferedConnection(conn)) ctx := proto.NewContext(req.RemoteAddr) if h.useAuth { if authenticated, err := auth.Authenticate(ctx, codec); !authenticated { if err != nil { log.Errorf("authentication erred at %s: %v", req.RemoteAddr, err) } codec.Close() return } } h.server.ServeCodecWithContext(ctx, codec) }
// ServeHTTP implements http.Handler's ServeHTTP func (h *httpRPCHandler) ServeHTTP(c http.ResponseWriter, req *http.Request) { codec := h.cFactory(&httpReadWriteCloser{rw: c, req: req}) var ctx context.Context if h.contextCreator != nil { ctx = h.contextCreator(req) } else { ctx = proto.NewContext(req.RemoteAddr) } h.server.ServeRequestWithContext( ctx, codec, ) codec.Close() }