美文网首页
记录Okhttp & golang websocket serv

记录Okhttp & golang websocket serv

作者: DJ沸羊羊 | 来源:发表于2022-03-07 16:41 被阅读0次

    Okhttp 报错堆栈

    java.net.ProtocolException: Expected HTTP 101 response but was '403 Forbidden'
    at okhttp3.internal.ws.RealWebSocket.checkUpgradeSuccess$okhttp(RealWebSocket.kt:224)
    at okhttp3.internal.ws.RealWebSocket$connect$1.onResponse(RealWebSocket.kt:170)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919) 
    

    报错代码

            OkHttpClient mClient = new OkHttpClient.Builder()
                    .readTimeout(3, TimeUnit.SECONDS)
                    .writeTimeout(3, TimeUnit.SECONDS)
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .build();
            Request.Builder builder = new Request.Builder();
            Request request =builder.get().url("ws://xxxxx/ws").build();
            mClient.newWebSocket(request,new WebSocketConnect(getContext()));
    

    问题分析 golang 层面

    // Copyright 2009 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    package websocket
    
    import (
        "bufio"
        "fmt"
        "io"
        "net/http"
    )
    
    func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request, config *Config, handshake func(*Config, *http.Request) error) (conn *Conn, err error) {
        var hs serverHandshaker = &hybiServerHandshaker{Config: config}
        code, err := hs.ReadHandshake(buf.Reader, req)
        if err == ErrBadWebSocketVersion {
            fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
            fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
            buf.WriteString("\r\n")
            buf.WriteString(err.Error())
            buf.Flush()
            return
        }
        if err != nil {
            fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
            buf.WriteString("\r\n")
            buf.WriteString(err.Error())
            buf.Flush()
            return
        }
        if handshake != nil {
            err = handshake(config, req)
            if err != nil {
                code = http.StatusForbidden
                fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
                buf.WriteString("\r\n")
                buf.Flush()
                return
            }
        }
        err = hs.AcceptHandshake(buf.Writer)
        if err != nil {
            code = http.StatusBadRequest
            fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
            buf.WriteString("\r\n")
            buf.Flush()
            return
        }
        conn = hs.NewServerConn(buf, rwc, req)
        return
    }
    
    // Server represents a server of a WebSocket.
    type Server struct {
        // Config is a WebSocket configuration for new WebSocket connection.
        Config
    
        // Handshake is an optional function in WebSocket handshake.
        // For example, you can check, or don't check Origin header.
        // Another example, you can select config.Protocol.
        Handshake func(*Config, *http.Request) error
    
        // Handler handles a WebSocket connection.
        Handler
    }
    
    // ServeHTTP implements the http.Handler interface for a WebSocket
    func (s Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        s.serveWebSocket(w, req)
    }
    
    func (s Server) serveWebSocket(w http.ResponseWriter, req *http.Request) {
        rwc, buf, err := w.(http.Hijacker).Hijack()
        if err != nil {
            panic("Hijack failed: " + err.Error())
        }
        // The server should abort the WebSocket connection if it finds
        // the client did not send a handshake that matches with protocol
        // specification.
        defer rwc.Close()
        conn, err := newServerConn(rwc, buf, req, &s.Config, s.Handshake)
        if err != nil {
            return
        }
        if conn == nil {
            panic("unexpected nil conn")
        }
        s.Handler(conn)
    }
    
    // Handler is a simple interface to a WebSocket browser client.
    // It checks if Origin header is valid URL by default.
    // You might want to verify websocket.Conn.Config().Origin in the func.
    // If you use Server instead of Handler, you could call websocket.Origin and
    // check the origin in your Handshake func. So, if you want to accept
    // non-browser clients, which do not send an Origin header, set a
    // Server.Handshake that does not check the origin.
    type Handler func(*Conn)
    
    func checkOrigin(config *Config, req *http.Request) (err error) {
        config.Origin, err = Origin(config, req)
        if err == nil && config.Origin == nil {
            return fmt.Errorf("null origin")
        }
        return err
    }
    
    // ServeHTTP implements the http.Handler interface for a WebSocket
    func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        s := Server{Handler: h, Handshake: checkOrigin}
        s.serveWebSocket(w, req)
    }
    
    

    经过Debug 得知 golang websocket server 对客户端的 请求头参数有强校验,必须要求请求头中包含 Origin 字段.

    func checkOrigin(config *Config, req *http.Request) (err error) {
        config.Origin, err = Origin(config, req)
        if err == nil && config.Origin == nil {
            return fmt.Errorf("null origin")
        }
        return err
    }
    
    

    解决办法 Android 层面

          OkHttpClient mClient = new OkHttpClient.Builder()
                  .readTimeout(3, TimeUnit.SECONDS)//设置读取超时时间
                  .writeTimeout(3, TimeUnit.SECONDS)//设置写的超时时间
                  .connectTimeout(3, TimeUnit.SECONDS)//设置连接超时时间
                  .build();
          Request.Builder builder = new Request.Builder();
          // 构建请求时 请求头增加Origin 参数 
          builder.addHeader("Origin","https://xxxx");
          Request request =builder.get().url("ws://xxxxx/ws").build();
          mClient.newWebSocket(request,new WebSocketConnect(getContext()));
    

    相关文章

      网友评论

          本文标题:记录Okhttp & golang websocket serv

          本文链接:https://www.haomeiwen.com/subject/cnjnrrtx.html