You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
466 lines
11 KiB
Go
466 lines
11 KiB
Go
6 years ago
|
package nghttp2
|
||
|
|
||
|
/*
|
||
|
#cgo pkg-config: libnghttp2
|
||
|
#include "_nghttp2.h"
|
||
|
*/
|
||
|
import "C"
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
6 years ago
|
"log"
|
||
6 years ago
|
"net"
|
||
|
"net/http"
|
||
6 years ago
|
"strconv"
|
||
6 years ago
|
"strings"
|
||
6 years ago
|
"sync"
|
||
|
"time"
|
||
6 years ago
|
"unsafe"
|
||
|
)
|
||
|
|
||
6 years ago
|
// ClientConn http2 connection
|
||
|
type ClientConn struct {
|
||
6 years ago
|
session *C.nghttp2_session
|
||
|
conn net.Conn
|
||
6 years ago
|
streams map[int]*ClientStream
|
||
6 years ago
|
lock *sync.Mutex
|
||
|
errch chan struct{}
|
||
6 years ago
|
exitch chan struct{}
|
||
6 years ago
|
err error
|
||
|
isServer bool
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// ClientStream http2 stream
|
||
|
type ClientStream struct {
|
||
6 years ago
|
streamID int
|
||
|
cdp *C.nghttp2_data_provider
|
||
|
dp *dataProvider
|
||
|
// application read data from stream
|
||
6 years ago
|
r *io.PipeReader
|
||
6 years ago
|
// recv stream data from session
|
||
6 years ago
|
w *io.PipeWriter
|
||
|
res *http.Response
|
||
|
resch chan *http.Response
|
||
|
errch chan error
|
||
|
closed bool
|
||
|
}
|
||
|
|
||
6 years ago
|
type dataProvider struct {
|
||
|
// drain the data
|
||
|
r io.Reader
|
||
|
// provider the data
|
||
|
w io.Writer
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewClientConn create http2 client
|
||
6 years ago
|
func NewClientConn(c net.Conn) (*ClientConn, error) {
|
||
|
conn := &ClientConn{
|
||
|
conn: c, streams: make(map[int]*ClientStream),
|
||
|
lock: new(sync.Mutex),
|
||
|
errch: make(chan struct{}),
|
||
|
exitch: make(chan struct{}),
|
||
6 years ago
|
}
|
||
6 years ago
|
conn.session = C.init_client_session(
|
||
|
C.size_t(int(uintptr(unsafe.Pointer(conn)))))
|
||
6 years ago
|
if conn.session == nil {
|
||
|
return nil, fmt.Errorf("init session failed")
|
||
|
}
|
||
|
ret := C.send_client_connection_header(conn.session)
|
||
|
if int(ret) < 0 {
|
||
6 years ago
|
log.Printf("submit settings error: %s",
|
||
|
C.GoString(C.nghttp2_strerror(ret)))
|
||
6 years ago
|
}
|
||
|
go conn.run()
|
||
|
return conn, nil
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (c *ClientConn) onDataRecv(buf []byte, streamID int) {
|
||
6 years ago
|
stream := c.streams[streamID]
|
||
|
stream.onDataRecv(buf)
|
||
|
}
|
||
|
|
||
6 years ago
|
func (c *ClientConn) onBeginHeader(streamID int) {
|
||
6 years ago
|
stream := c.streams[streamID]
|
||
|
stream.onBeginHeader()
|
||
|
}
|
||
|
|
||
6 years ago
|
func (c *ClientConn) onHeader(streamID int, name, value string) {
|
||
6 years ago
|
stream := c.streams[streamID]
|
||
|
stream.onHeader(name, value)
|
||
|
|
||
|
}
|
||
|
|
||
6 years ago
|
func (c *ClientConn) onFrameRecv(streamID int) {
|
||
6 years ago
|
stream := c.streams[streamID]
|
||
|
stream.onFrameRecv()
|
||
|
}
|
||
|
|
||
6 years ago
|
func (c *ClientConn) onStreamClose(streamID int) {
|
||
6 years ago
|
stream, ok := c.streams[streamID]
|
||
|
if ok {
|
||
|
stream.Close()
|
||
|
c.lock.Lock()
|
||
|
delete(c.streams, streamID)
|
||
|
c.lock.Unlock()
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// Close close the http2 connection
|
||
6 years ago
|
func (c *ClientConn) Close() error {
|
||
6 years ago
|
for _, s := range c.streams {
|
||
|
s.Close()
|
||
|
}
|
||
6 years ago
|
C.nghttp2_session_terminate_session(c.session, 0)
|
||
6 years ago
|
C.nghttp2_session_del(c.session)
|
||
6 years ago
|
close(c.exitch)
|
||
6 years ago
|
c.conn.Close()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
6 years ago
|
func (c *ClientConn) run() {
|
||
6 years ago
|
var wantRead int
|
||
|
var wantWrite int
|
||
|
var delay = 50
|
||
|
var ret C.int
|
||
|
|
||
6 years ago
|
defer close(c.errch)
|
||
|
|
||
6 years ago
|
datach := make(chan []byte)
|
||
|
errch := make(chan error)
|
||
|
|
||
|
go func() {
|
||
6 years ago
|
buf := make([]byte, 16*1024)
|
||
6 years ago
|
readloop:
|
||
6 years ago
|
for {
|
||
6 years ago
|
select {
|
||
|
case <-c.exitch:
|
||
|
break readloop
|
||
|
default:
|
||
|
}
|
||
|
|
||
6 years ago
|
n, err := c.conn.Read(buf)
|
||
|
if err != nil {
|
||
|
errch <- err
|
||
|
break
|
||
|
}
|
||
|
datach <- buf[:n]
|
||
|
}
|
||
|
}()
|
||
|
|
||
6 years ago
|
loop:
|
||
|
for {
|
||
|
select {
|
||
|
case <-c.errch:
|
||
|
break loop
|
||
6 years ago
|
case err := <-errch:
|
||
|
c.err = err
|
||
|
break loop
|
||
6 years ago
|
case <-c.exitch:
|
||
|
break loop
|
||
6 years ago
|
default:
|
||
|
}
|
||
|
|
||
|
wantWrite = int(C.nghttp2_session_want_write(c.session))
|
||
|
if wantWrite != 0 {
|
||
|
ret = C.nghttp2_session_send(c.session)
|
||
|
if int(ret) < 0 {
|
||
6 years ago
|
c.err = fmt.Errorf("sesion send error: %s",
|
||
|
C.GoString(C.nghttp2_strerror(ret)))
|
||
6 years ago
|
log.Println(c.err)
|
||
|
break
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
|
wantRead = int(C.nghttp2_session_want_read(c.session))
|
||
|
select {
|
||
|
case d := <-datach:
|
||
|
d1 := C.CBytes(d)
|
||
|
ret1 := C.nghttp2_session_mem_recv(c.session,
|
||
|
(*C.uchar)(d1), C.size_t(int(len(d))))
|
||
6 years ago
|
C.free(d1)
|
||
6 years ago
|
if int(ret1) < 0 {
|
||
|
c.err = fmt.Errorf("sesion recv error: %s",
|
||
|
C.GoString(C.nghttp2_strerror(ret)))
|
||
6 years ago
|
log.Println(c.err)
|
||
6 years ago
|
break loop
|
||
6 years ago
|
}
|
||
6 years ago
|
default:
|
||
6 years ago
|
}
|
||
|
|
||
|
// make delay when no data read/write
|
||
|
if wantRead == 0 && wantWrite == 0 {
|
||
|
select {
|
||
|
case <-time.After(time.Duration(delay) * time.Millisecond):
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// CreateRequest submit a request and return a http.Response, client only
|
||
6 years ago
|
func (c *ClientConn) CreateRequest(req *http.Request) (*http.Response, error) {
|
||
6 years ago
|
if c.err != nil {
|
||
|
return nil, c.err
|
||
|
}
|
||
|
|
||
6 years ago
|
if c.isServer {
|
||
|
return nil, fmt.Errorf("only client can create new request")
|
||
|
}
|
||
|
|
||
6 years ago
|
nvIndex := 0
|
||
|
nvMax := 25
|
||
|
nva := C.new_nv_array(C.size_t(nvMax))
|
||
|
setNvArray(nva, nvIndex, ":method", req.Method, 0)
|
||
6 years ago
|
nvIndex++
|
||
6 years ago
|
setNvArray(nva, nvIndex, ":scheme", "https", 0)
|
||
6 years ago
|
nvIndex++
|
||
6 years ago
|
setNvArray(nva, nvIndex, ":authority", req.Host, 0)
|
||
6 years ago
|
nvIndex++
|
||
6 years ago
|
|
||
|
p := req.URL.Path
|
||
|
q := req.URL.Query().Encode()
|
||
|
if q != "" {
|
||
|
p = p + "?" + q
|
||
|
}
|
||
|
setNvArray(nva, nvIndex, ":path", p, 0)
|
||
6 years ago
|
nvIndex++
|
||
6 years ago
|
for k, v := range req.Header {
|
||
|
if strings.ToLower(k) == "host" {
|
||
|
continue
|
||
|
}
|
||
6 years ago
|
//log.Printf("header %s: %s", k, v)
|
||
6 years ago
|
setNvArray(nva, nvIndex, strings.Title(k), v[0], 0)
|
||
6 years ago
|
nvIndex++
|
||
6 years ago
|
}
|
||
|
var dp *dataProvider
|
||
|
var cdp *C.nghttp2_data_provider
|
||
|
if req.Body != nil {
|
||
|
dp, cdp = newDataProvider(req.Body, nil)
|
||
|
}
|
||
6 years ago
|
streamID := C.submit_request(c.session, nva.nv, C.size_t(nvIndex), cdp)
|
||
6 years ago
|
C.delete_nv_array(nva)
|
||
|
if int(streamID) < 0 {
|
||
6 years ago
|
return nil, fmt.Errorf("submit request error: %s",
|
||
|
C.GoString(C.nghttp2_strerror(streamID)))
|
||
6 years ago
|
}
|
||
6 years ago
|
//log.Println("stream id ", int(streamID))
|
||
6 years ago
|
r, w := io.Pipe()
|
||
6 years ago
|
s := &ClientStream{
|
||
6 years ago
|
streamID: int(streamID),
|
||
|
dp: dp,
|
||
|
cdp: cdp,
|
||
|
r: r,
|
||
|
w: w,
|
||
|
resch: make(chan *http.Response),
|
||
|
errch: make(chan error),
|
||
|
}
|
||
|
c.lock.Lock()
|
||
6 years ago
|
c.streams[int(streamID)] = s
|
||
6 years ago
|
c.lock.Unlock()
|
||
6 years ago
|
|
||
6 years ago
|
select {
|
||
|
case err := <-s.errch:
|
||
|
return nil, err
|
||
|
case res := <-s.resch:
|
||
|
return res, nil
|
||
6 years ago
|
case <-c.errch:
|
||
|
return nil, fmt.Errorf("connection error")
|
||
6 years ago
|
}
|
||
|
//return nil, fmt.Errorf("unknown error")
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func setNvArray(a *C.struct_nv_array, index int,
|
||
|
name, value string, flags int) {
|
||
6 years ago
|
cname := C.CString(name)
|
||
|
cvalue := C.CString(value)
|
||
|
cnamelen := C.size_t(len(name))
|
||
|
cvaluelen := C.size_t(len(value))
|
||
|
cflags := C.int(flags)
|
||
6 years ago
|
//defer C.free(unsafe.Pointer(cname))
|
||
|
//defer C.free(unsafe.Pointer(cvalue))
|
||
6 years ago
|
C.nv_array_set(a, C.int(index), cname,
|
||
|
cvalue, cnamelen, cvaluelen, cflags)
|
||
|
}
|
||
|
|
||
|
func (dp *dataProvider) Read(buf []byte) (n int, err error) {
|
||
|
return dp.r.Read(buf)
|
||
|
}
|
||
|
|
||
|
func (dp *dataProvider) Write(buf []byte) (n int, err error) {
|
||
|
if dp.w == nil {
|
||
|
return 0, fmt.Errorf("write not supported")
|
||
|
}
|
||
|
return dp.w.Write(buf)
|
||
|
}
|
||
|
|
||
6 years ago
|
func newDataProvider(r io.Reader, w io.Writer) (
|
||
|
*dataProvider, *C.nghttp2_data_provider) {
|
||
6 years ago
|
dp := &dataProvider{r, w}
|
||
6 years ago
|
cdp := C.new_data_provider(C.size_t(uintptr(unsafe.Pointer(dp))))
|
||
6 years ago
|
return dp, cdp
|
||
|
}
|
||
|
|
||
6 years ago
|
func (s *ClientStream) Read(buf []byte) (n int, err error) {
|
||
6 years ago
|
return s.r.Read(buf)
|
||
|
}
|
||
|
|
||
6 years ago
|
func (s *ClientStream) Write(buf []byte) (n int, err error) {
|
||
6 years ago
|
return s.dp.Write(buf)
|
||
|
}
|
||
|
|
||
6 years ago
|
func (s *ClientStream) onDataRecv(buf []byte) {
|
||
6 years ago
|
s.w.Write(buf)
|
||
|
}
|
||
|
|
||
6 years ago
|
func (s *ClientStream) onBeginHeader() {
|
||
6 years ago
|
s.res = &http.Response{
|
||
|
Header: make(http.Header),
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (s *ClientStream) onHeader(name, value string) {
|
||
6 years ago
|
if name == ":status" {
|
||
|
statusCode, _ := strconv.Atoi(value)
|
||
|
s.res.StatusCode = statusCode
|
||
|
s.res.Status = http.StatusText(statusCode)
|
||
|
s.res.Proto = "HTTP/2.0"
|
||
|
s.res.ProtoMajor = 2
|
||
|
s.res.ProtoMinor = 0
|
||
|
return
|
||
|
}
|
||
|
s.res.Header.Add(name, value)
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func (s *ClientStream) onFrameRecv() {
|
||
6 years ago
|
s.res.Body = s
|
||
|
s.resch <- s.res
|
||
6 years ago
|
//log.Println("stream frame recv")
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// Close close the stream
|
||
6 years ago
|
func (s *ClientStream) Close() error {
|
||
6 years ago
|
if s.closed {
|
||
|
return nil
|
||
|
}
|
||
|
err := fmt.Errorf("stream closed")
|
||
|
//log.Println("close stream")
|
||
6 years ago
|
select {
|
||
6 years ago
|
case s.errch <- err:
|
||
|
default:
|
||
6 years ago
|
}
|
||
6 years ago
|
//log.Println("close stream resch")
|
||
6 years ago
|
close(s.resch)
|
||
6 years ago
|
//log.Println("close stream errch")
|
||
6 years ago
|
close(s.errch)
|
||
6 years ago
|
//log.Println("close pipe w")
|
||
|
s.w.CloseWithError(err)
|
||
|
//log.Println("close stream done")
|
||
|
s.closed = true
|
||
6 years ago
|
return nil
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// DataSourceRead callback function for data read from data provider source
|
||
6 years ago
|
//export DataSourceRead
|
||
6 years ago
|
func DataSourceRead(ptr unsafe.Pointer,
|
||
|
buf unsafe.Pointer, length C.size_t) C.ssize_t {
|
||
|
//log.Println("data source read")
|
||
6 years ago
|
dp := (*dataProvider)(ptr)
|
||
|
gobuf := make([]byte, int(length))
|
||
|
n, err := dp.Read(gobuf)
|
||
|
if err != nil {
|
||
|
if err == io.EOF {
|
||
|
return 0
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
cbuf := C.CBytes(gobuf)
|
||
|
defer C.free(cbuf)
|
||
|
C.memcpy(buf, cbuf, C.size_t(n))
|
||
|
return C.ssize_t(n)
|
||
|
}
|
||
|
|
||
6 years ago
|
// OnClientDataRecv callback function for data frame received
|
||
|
//export OnClientDataRecv
|
||
|
func OnClientDataRecv(ptr unsafe.Pointer, streamID C.int,
|
||
6 years ago
|
buf unsafe.Pointer, length C.size_t) C.int {
|
||
|
//log.Println("on data recv")
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
gobuf := C.GoBytes(buf, C.int(length))
|
||
|
conn.onDataRecv(gobuf, int(streamID))
|
||
|
return 0
|
||
|
}
|
||
|
|
||
6 years ago
|
// ClientDataRecv callback function for session wants read data from peer
|
||
|
//export ClientDataRecv
|
||
|
func ClientDataRecv(ptr unsafe.Pointer, data unsafe.Pointer, size C.size_t) C.ssize_t {
|
||
6 years ago
|
//log.Println("data read req", int(size))
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
buf := make([]byte, int(size))
|
||
6 years ago
|
//log.Println(conn.conn.RemoteAddr())
|
||
6 years ago
|
n, err := conn.conn.Read(buf)
|
||
|
if err != nil {
|
||
6 years ago
|
//log.Println(err)
|
||
6 years ago
|
return -1
|
||
|
}
|
||
6 years ago
|
cbuf := C.CBytes(buf)
|
||
|
//log.Println("read from network ", n, buf[:n])
|
||
|
C.memcpy(data, cbuf, C.size_t(n))
|
||
6 years ago
|
return C.ssize_t(n)
|
||
|
}
|
||
|
|
||
6 years ago
|
// ClientDataSend callback function for session wants send data to peer
|
||
|
//export ClientDataSend
|
||
|
func ClientDataSend(ptr unsafe.Pointer, data unsafe.Pointer, size C.size_t) C.ssize_t {
|
||
6 years ago
|
//log.Println("data write req ", int(size))
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
buf := C.GoBytes(data, C.int(size))
|
||
6 years ago
|
//log.Println(conn.conn.RemoteAddr())
|
||
6 years ago
|
n, err := conn.conn.Write(buf)
|
||
|
if err != nil {
|
||
6 years ago
|
//log.Println(err)
|
||
6 years ago
|
return -1
|
||
|
}
|
||
6 years ago
|
//log.Println("write data to network ", n)
|
||
6 years ago
|
return C.ssize_t(n)
|
||
|
}
|
||
|
|
||
6 years ago
|
// OnClientBeginHeaderCallback callback function for response
|
||
|
//export OnClientBeginHeaderCallback
|
||
|
func OnClientBeginHeaderCallback(ptr unsafe.Pointer, streamID C.int) C.int {
|
||
6 years ago
|
//log.Println("begin header")
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
conn.onBeginHeader(int(streamID))
|
||
|
return 0
|
||
|
}
|
||
|
|
||
6 years ago
|
// OnClientHeaderCallback callback function for header
|
||
|
//export OnClientHeaderCallback
|
||
|
func OnClientHeaderCallback(ptr unsafe.Pointer, streamID C.int,
|
||
6 years ago
|
name unsafe.Pointer, namelen C.int,
|
||
|
value unsafe.Pointer, valuelen C.int) C.int {
|
||
6 years ago
|
//log.Println("header")
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
goname := C.GoBytes(name, namelen)
|
||
|
govalue := C.GoBytes(value, valuelen)
|
||
|
conn.onHeader(int(streamID), string(goname), string(govalue))
|
||
|
return 0
|
||
|
}
|
||
|
|
||
6 years ago
|
// OnClientFrameRecvCallback callback function for begion to recv data
|
||
|
//export OnClientFrameRecvCallback
|
||
|
func OnClientFrameRecvCallback(ptr unsafe.Pointer, streamID C.int) C.int {
|
||
6 years ago
|
//log.Println("frame recv")
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
conn.onFrameRecv(int(streamID))
|
||
|
return 0
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// OnClientStreamClose callback function for stream close
|
||
|
//export OnClientStreamClose
|
||
|
func OnClientStreamClose(ptr unsafe.Pointer, streamID C.int) C.int {
|
||
6 years ago
|
//log.Println("stream close")
|
||
6 years ago
|
conn := (*ClientConn)(ptr)
|
||
6 years ago
|
conn.onStreamClose(int(streamID))
|
||
|
return 0
|
||
|
}
|