drp935159 2018-07-25 14:14
浏览 163
已采纳

如何完全禁用HTTP / 1.x支持

I only want to support HTTP/2 for a new project, the client is not a browser so it's not a problem if we don't support HTTP/1.x at all.

from what I see in golang.org/x/net/http2. I can use tls.Listen and pass the net.Conn to http2.Server.ServeConn.

But I'm bit confused about how to use http2.Transport here, can anyone give me an example?

Thanks

UPDATE:

This is the server part, pretty simple, it's an echo server

package main

import (
    "fmt"
    "io"
    "net"
    "net/http"

    "golang.org/x/net/http2"
)

func main() {
    l, err := net.Listen("tcp4", ":1234")
    panicIfNotNil(err)

    s := &http2.Server{}
    sopt := &http2.ServeConnOpts{
        BaseConfig: &http.Server{},
        Handler:    http.HandlerFunc(handler),
    }
    for {
        c, err := l.Accept()
        panicIfNotNil(err)
        go serve(s, sopt, c)
    }
}

func serve(s *http2.Server, sopt *http2.ServeConnOpts, c net.Conn) {
    defer c.Close()
    s.ServeConn(c, sopt)
}

func handler(w http.ResponseWriter, r *http.Request) {
    if r.ProtoMajor != 2 {
        w.WriteHeader(500)
        fmt.Fprintln(w, "Not HTTP/2")
        return
    }
    f, ok := w.(http.Flusher)
    if !ok {
        w.WriteHeader(500)
        fmt.Fprintln(w, "Not Flusher")
        return
    }
    w.Header().Set("Content-Type", "application/octet-stream")
    fmt.Fprintln(w, "Hello World, Echo Server")

    buf := [1024]byte{}
    for {
        n, err := r.Body.Read(buf[:])
        if err == io.EOF {
            break
        }
        panicIfNotNil(err)
        _, err = w.Write(buf[:n])
        f.Flush()
        panicIfNotNil(err)
    }
}

func panicIfNotNil(err error) {
    if err != nil {
        panic(err)
    }
}

tested with curl --http2-prior-knowledge http://127.0.0.1:1234 -d a=b -d c=d -d e=f

for the client part, I'm still trying, I will update this post again when I got something.

UPDATE:

for the sake of simplicity, I don't use TLS here

UPDATE:

This is the client part

package main

import (
    "crypto/tls"
    "fmt"
    "io"
    "net"
    "net/http"
    "net/url"
    "time"

    "golang.org/x/net/http2"
)

func main() {
    t := &http2.Transport{
        DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
            return net.Dial(network, addr)
        },
        AllowHTTP: true,
    }
    c := &http.Client{
        Transport: t,
    }

    pr, pw := io.Pipe()
    req := &http.Request{
        Method: "POST",
        URL:    mustUrl("http://127.0.0.1:1234/"),
        Body:   pr,
    }
    resp, err := c.Do(req)
    panicIfNotNil(err)
    defer resp.Body.Close()
    if resp.StatusCode != 200 {
        panic(fmt.Errorf("Server return non 200, %d", resp.StatusCode))
    }

    wchan := make(chan struct{})
    go func() {
        buf := [1024]byte{}
        for {
            n, err := resp.Body.Read(buf[:])
            if err == io.EOF {
                break
            }
            panicIfNotNil(err)
            fmt.Printf("GOT DATA %s
", string(buf[:n]))
        }
        close(wchan)
    }()

    time.Sleep(1 * time.Second)
    pw.Write([]byte("hai AAA"))
    time.Sleep(1 * time.Second)
    pw.Write([]byte("hai BBB"))
    time.Sleep(1 * time.Second)
    pw.Write([]byte("hai CCC"))
    time.Sleep(1 * time.Second)
    pw.Write([]byte("hai CCC"))
    time.Sleep(1 * time.Second)
    pw.Close()

    <-wchan
}

func mustUrl(s string) *url.URL {
    r, err := url.Parse(s)
    panicIfNotNil(err)
    return r
}

func panicIfNotNil(err error) {
    if err != nil {
        panic(err)
    }
}

but somehow it doesn't work You can see network traffic in https://imgur.com/EJV0uGI

  • 写回答

1条回答 默认 最新

  • dongtingrun4973 2018-07-25 15:37
    关注

    After looking into Wireshark more closely I found the problem, it happens because the server didn't send any header frame, so the client cannot continue with more data. Just printing into http.ResponseWriter doesn't ensure its written into the network, it gets buffered instead, so we need to explicitly flush it.

    This fixes the problem:

    --- main.go 2018-07-25 22:31:44.092823590 +0700
    +++ main2.go    2018-07-25 22:32:50.586179879 +0700
    @@ -43,6 +43,9 @@
            return
        }
        w.Header().Set("Content-Type", "application/octet-stream")
    +   w.WriteHeader(200)
    +   f.Flush()
    +
        fmt.Fprintln(w, "Hello World, Echo Server")
    
        buf := [1024]byte{}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义