change connect function signature

merge_conn
fangdingjun 6 years ago
parent 58e1343da1
commit 10c9e2bbab

@ -199,22 +199,25 @@ loop:
} }
} }
// Connect submit a CONNECT request // Connect submit a CONNECT request, return a ClientStream and http status code from server
func (c *ClientConn) Connect(req *http.Request) (*ClientStream, error) { //
// 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 { if c.err != nil {
return nil, c.err return nil, http.StatusServiceUnavailable, c.err
} }
nvIndex := 0 nvIndex := 0
nvMax := 5 nvMax := 5
nva := C.new_nv_array(C.size_t(nvMax)) nva := C.new_nv_array(C.size_t(nvMax))
//log.Printf("%s %s", req.Method, req.RequestURI) //log.Printf("%s %s", req.Method, req.RequestURI)
setNvArray(nva, nvIndex, ":method", req.Method, 0) setNvArray(nva, nvIndex, ":method", "CONNECT", 0)
nvIndex++ nvIndex++
//setNvArray(nva, nvIndex, ":scheme", "https", 0) //setNvArray(nva, nvIndex, ":scheme", "https", 0)
//nvIndex++ //nvIndex++
//log.Printf("header authority: %s", req.RequestURI) //log.Printf("header authority: %s", req.RequestURI)
setNvArray(nva, nvIndex, ":authority", req.RequestURI, 0) setNvArray(nva, nvIndex, ":authority", addr, 0)
nvIndex++ nvIndex++
var dp *dataProvider var dp *dataProvider
var cdp *C.nghttp2_data_provider var cdp *C.nghttp2_data_provider
dp, cdp = newDataProvider(c.lock) dp, cdp = newDataProvider(c.lock)
@ -225,7 +228,7 @@ func (c *ClientConn) Connect(req *http.Request) (*ClientStream, error) {
C.delete_nv_array(nva) C.delete_nv_array(nva)
if int(streamID) < 0 { 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))) C.GoString(C.nghttp2_strerror(streamID)))
} }
if dp != nil { if dp != nil {
@ -250,16 +253,15 @@ func (c *ClientConn) Connect(req *http.Request) (*ClientStream, error) {
select { select {
case err := <-s.errch: case err := <-s.errch:
//log.Println("wait response, got ", err) //log.Println("wait response, got ", err)
return nil, err return nil, http.StatusServiceUnavailable, err
case res := <-s.resch: case res := <-s.resch:
if res != nil { if res != nil {
res.Request = req return s, res.StatusCode, nil
return s, nil
} }
//log.Println("wait response, empty response") //log.Println("wait response, empty response")
return nil, io.EOF return nil, http.StatusServiceUnavailable, io.EOF
case <-c.errch: case <-c.errch:
return nil, fmt.Errorf("connection error") return nil, http.StatusServiceUnavailable, fmt.Errorf("connection error")
} }
} }

@ -30,11 +30,6 @@ type ClientStream struct {
lock *sync.Mutex lock *sync.Mutex
} }
// Response return response of the current stream
func (s *ClientStream) Response() *http.Response {
return s.res
}
// Read read stream data // Read read stream data
func (s *ClientStream) Read(buf []byte) (n int, err error) { func (s *ClientStream) Read(buf []byte) (n int, err error) {
if s.closed || s.res == nil || s.res.Body == nil { if s.closed || s.res == nil || s.res.Body == nil {

Loading…
Cancel
Save