add handle for http/1.1 request

gnutls_nghttp2
fangdingjun 6 years ago
parent d83d751789
commit 1f56de791a

@ -1,6 +1,8 @@
package main package main
import ( import (
"bufio"
"bytes"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -148,7 +150,7 @@ func initRouters(cfg conf) {
log.Println(err) log.Println(err)
break break
} }
go handleHttpClient(conn, handler) go handleHTTPClient(conn, handler)
} }
}() }()
@ -269,7 +271,7 @@ func (m myURLMatch) match(r *http.Request, route *mux.RouteMatch) bool {
return ret return ret
} }
func handleHttpClient(c net.Conn, handler http.Handler) { func handleHTTPClient(c net.Conn, handler http.Handler) {
tlsconn := c.(*gnutls.Conn) tlsconn := c.(*gnutls.Conn)
if err := tlsconn.Handshake(); err != nil { if err := tlsconn.Handshake(); err != nil {
log.Println(err) log.Println(err)
@ -277,19 +279,94 @@ func handleHttpClient(c net.Conn, handler http.Handler) {
} }
state := tlsconn.ConnectionState() state := tlsconn.ConnectionState()
if state.NegotiatedProtocol == "h2" { if state.NegotiatedProtocol == "h2" {
h2conn, err := nghttp2.NewServerConn(tlsconn, handler) h2conn, err := nghttp2.Server(tlsconn, handler)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
h2conn.Run() h2conn.Run()
return return
} }
defer c.Close() defer c.Close()
resp := &http.Response{ r := bufio.NewReader(tlsconn)
StatusCode: http.StatusHTTPVersionNotSupported, buf := new(bytes.Buffer)
Status: http.StatusText(http.StatusHTTPVersionNotSupported), for {
ProtoMajor: 1, req, err := http.ReadRequest(r)
ProtoMinor: 1, if err != nil {
return
}
addr := tlsconn.RemoteAddr().String()
req.RemoteAddr = addr
rh := &responseHandler{
c: tlsconn,
header: http.Header{},
buf: buf,
}
handler.ServeHTTP(rh, req)
rh.Write(nil)
rh.buf.WriteTo(rh.c)
}
}
type responseHandler struct {
c net.Conn
statusCode int
header http.Header
responseSend bool
w io.Writer
buf *bytes.Buffer
}
func (r *responseHandler) WriteHeader(statusCode int) {
if r.responseSend {
return
}
r.buf.Reset()
r.statusCode = statusCode
cl := r.header.Get("content-length")
te := r.header.Get("transfer-encoding")
if cl == "" || te != "" {
if te == "" {
r.header.Set("transfer-encoding", "chunked")
}
r.w = &chunkWriter{r.buf}
} else {
r.w = r.buf
}
fmt.Fprintf(r.buf, "HTTP/1.1 %d %s\r\n", statusCode,
http.StatusText(statusCode))
for k, v := range r.header {
fmt.Fprintf(r.buf, "%s: %s\r\n", strings.Title(k), strings.Join(v, ","))
}
fmt.Fprintf(r.buf, "\r\n")
r.responseSend = true
}
func (r *responseHandler) Header() http.Header {
return r.header
}
func (r *responseHandler) Write(buf []byte) (int, error) {
if !r.responseSend {
r.WriteHeader(http.StatusOK)
}
n, err := r.w.Write(buf)
if r.buf.Len() > 2048 {
r.buf.WriteTo(r.c)
}
return n, err
}
var _ http.ResponseWriter = &responseHandler{}
type chunkWriter struct {
w io.Writer
}
func (cw *chunkWriter) Write(buf []byte) (int, error) {
n := len(buf)
if n == 0 {
return fmt.Fprintf(cw.w, "0\r\n\r\n")
} }
resp.Write(tlsconn) return fmt.Fprintf(cw.w, "%x\r\n%s\r\n", n, string(buf))
} }

Loading…
Cancel
Save