MCPcopy
hub / github.com/fagongzi/manba / onWebsocket

Method onWebsocket

pkg/proxy/proxy_websocket.go:71–116  ·  view source on GitHub ↗
(c *proxyContext, addr string)

Source from the content-addressed store, hash-verified

69}
70
71func (p *Proxy) onWebsocket(c *proxyContext, addr string) (*fasthttp.Response, error) {
72 resp := fasthttp.AcquireResponse()
73
74 var r http.Request
75 r.Method = "GET"
76 r.Proto = "HTTP/1.1"
77 r.ProtoMajor = 1
78 r.ProtoMinor = 1
79 r.RequestURI = string(c.forwardReq.RequestURI())
80 r.Host = string(c.forwardReq.Host())
81
82 hdr := make(http.Header)
83 c.forwardReq.Header.VisitAll(func(k, v []byte) {
84 sk := string(k)
85 sv := string(v)
86 switch sk {
87 case "Transfer-Encoding":
88 r.TransferEncoding = append(r.TransferEncoding, sv)
89 default:
90 hdr.Set(sk, sv)
91 }
92 })
93 r.Header = hdr
94 r.URL, _ = url.ParseRequestURI(r.RequestURI)
95
96 wp := &websocketproxy.WebsocketProxy{
97 Upgrader: &websocket.Upgrader{
98 ReadBufferSize: c.result.httpOption().ReadBufferSize,
99 WriteBufferSize: c.result.httpOption().WriteBufferSize,
100 CheckOrigin: func(r *http.Request) bool {
101 return true
102 },
103 },
104 Director: func(incoming *http.Request, out http.Header) {
105 out.Set("Origin", fmt.Sprintf("http://%s", addr))
106 out.Set("Host", addr)
107 },
108 Backend: func(r *http.Request) *url.URL {
109 u, _ := url.Parse(fmt.Sprintf("ws://%s%s", addr, r.RequestURI))
110 return u
111 },
112 }
113
114 wp.ServeHTTP(c.GetAttr(websocketRspKey).(http.ResponseWriter), &r)
115 return resp, nil
116}

Callers 1

doProxyMethod · 0.95

Calls 4

SetMethod · 0.80
httpOptionMethod · 0.80
ServeHTTPMethod · 0.80
GetAttrMethod · 0.65

Tested by

no test coverage detected