use new nghttp2 library

gnutls_nghttp2
fangdingjun 6 years ago
parent 75c3cb0fcc
commit a139774bdb

@ -54,14 +54,14 @@ func (tc *timeoutConn) Write(b []byte) (n int, err error) {
} }
type handler struct { type handler struct {
h2conn *nghttp2.ClientConn h2conn *nghttp2.Conn
addr string addr string
hostname string hostname string
insecure bool insecure bool
lock *sync.Mutex lock *sync.Mutex
} }
func (h *handler) createConnection() (*nghttp2.ClientConn, error) { func (h *handler) createConnection() (*nghttp2.Conn, error) {
log.Println("create connection to ", h.addr) log.Println("create connection to ", h.addr)
c, err := net.DialTimeout("tcp", h.addr, 5*time.Second) c, err := net.DialTimeout("tcp", h.addr, 5*time.Second)
if err != nil { if err != nil {
@ -87,7 +87,7 @@ func (h *handler) createConnection() (*nghttp2.ClientConn, error) {
return client, nil return client, nil
} }
func (h *handler) getConn() (*nghttp2.ClientConn, error) { func (h *handler) getConn() (*nghttp2.Conn, error) {
h.lock.Lock() h.lock.Lock()
defer h.lock.Unlock() defer h.lock.Unlock()
@ -134,11 +134,11 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) { func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
var err error var err error
var h2conn *nghttp2.ClientConn var h2conn *nghttp2.Conn
var code int var code int
//var resp *http.Response //var resp *http.Response
var cs *nghttp2.ClientStream var cs net.Conn
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
h2conn, err = h.getConn() h2conn, err = h.getConn()
@ -163,6 +163,7 @@ func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
defer cs.Close() defer cs.Close()
if code != http.StatusOK { if code != http.StatusOK {
log.Println("code", code)
w.WriteHeader(code) w.WriteHeader(code)
return return
} }
@ -196,7 +197,7 @@ func (h *handler) handleConnect(w http.ResponseWriter, r *http.Request) {
func (h *handler) handleHTTP(w http.ResponseWriter, r *http.Request) { func (h *handler) handleHTTP(w http.ResponseWriter, r *http.Request) {
var err error var err error
var resp *http.Response var resp *http.Response
var h2conn *nghttp2.ClientConn var h2conn *nghttp2.Conn
if r.RequestURI[0] == '/' { if r.RequestURI[0] == '/' {
http.DefaultServeMux.ServeHTTP(w, r) http.DefaultServeMux.ServeHTTP(w, r)
@ -210,7 +211,7 @@ func (h *handler) handleHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway) w.WriteHeader(http.StatusBadGateway)
return return
} }
resp, err = h2conn.CreateRequest(r) resp, err = h2conn.RoundTrip(r)
if resp != nil { if resp != nil {
break break
} }

Loading…
Cancel
Save