From 10c9e2bbabea2ccdd6eae47d1d660fbc74954279 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Thu, 12 Jul 2018 14:33:59 +0800 Subject: [PATCH] change connect function signature --- conn.go | 24 +++++++++++++----------- stream.go | 5 ----- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/conn.go b/conn.go index 5fa8e12..90a60c3 100644 --- a/conn.go +++ b/conn.go @@ -199,22 +199,25 @@ loop: } } -// Connect submit a CONNECT request -func (c *ClientConn) Connect(req *http.Request) (*ClientStream, error) { +// Connect submit a CONNECT request, return a ClientStream and http status code from server +// +// equals to "CONNECT host:port" in http/1.1 +func (c *ClientConn) Connect(addr string) (cs *ClientStream, statusCode int, err error) { if c.err != nil { - return nil, c.err + return nil, http.StatusServiceUnavailable, c.err } nvIndex := 0 nvMax := 5 nva := C.new_nv_array(C.size_t(nvMax)) //log.Printf("%s %s", req.Method, req.RequestURI) - setNvArray(nva, nvIndex, ":method", req.Method, 0) + setNvArray(nva, nvIndex, ":method", "CONNECT", 0) nvIndex++ //setNvArray(nva, nvIndex, ":scheme", "https", 0) //nvIndex++ //log.Printf("header authority: %s", req.RequestURI) - setNvArray(nva, nvIndex, ":authority", req.RequestURI, 0) + setNvArray(nva, nvIndex, ":authority", addr, 0) nvIndex++ + var dp *dataProvider var cdp *C.nghttp2_data_provider dp, cdp = newDataProvider(c.lock) @@ -225,7 +228,7 @@ func (c *ClientConn) Connect(req *http.Request) (*ClientStream, error) { C.delete_nv_array(nva) if int(streamID) < 0 { - return nil, fmt.Errorf("submit request error: %s", + return nil, http.StatusServiceUnavailable, fmt.Errorf("submit request error: %s", C.GoString(C.nghttp2_strerror(streamID))) } if dp != nil { @@ -250,16 +253,15 @@ func (c *ClientConn) Connect(req *http.Request) (*ClientStream, error) { select { case err := <-s.errch: //log.Println("wait response, got ", err) - return nil, err + return nil, http.StatusServiceUnavailable, err case res := <-s.resch: if res != nil { - res.Request = req - return s, nil + return s, res.StatusCode, nil } //log.Println("wait response, empty response") - return nil, io.EOF + return nil, http.StatusServiceUnavailable, io.EOF case <-c.errch: - return nil, fmt.Errorf("connection error") + return nil, http.StatusServiceUnavailable, fmt.Errorf("connection error") } } diff --git a/stream.go b/stream.go index 2bb25e2..dda3603 100644 --- a/stream.go +++ b/stream.go @@ -30,11 +30,6 @@ type ClientStream struct { lock *sync.Mutex } -// Response return response of the current stream -func (s *ClientStream) Response() *http.Response { - return s.res -} - // Read read stream data func (s *ClientStream) Read(buf []byte) (n int, err error) { if s.closed || s.res == nil || s.res.Body == nil {