1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00

Refine header based websocket earlydata fix

This commit is contained in:
Shelikhoo 2023-02-19 14:41:28 +00:00
parent dca2f2bd83
commit c055a08b2c
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316

View File

@ -38,7 +38,8 @@ var upgrader = &websocket.Upgrader{
}
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
var earlyDataStr string
responseHeader := http.Header{}
var earlyData io.Reader
if !h.earlyDataEnabled { // nolint: gocritic
if request.URL.Path != h.path {
@ -50,11 +51,14 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
writer.WriteHeader(http.StatusNotFound)
return
}
earlyDataStr = request.Header.Get(h.earlyDataHeaderName)
earlyDataStr := request.Header.Get(h.earlyDataHeaderName)
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
if strings.EqualFold("Sec-WebSocket-Protocol", h.earlyDataHeaderName) {
responseHeader.Set(h.earlyDataHeaderName, earlyDataStr)
}
} else {
if strings.HasPrefix(request.URL.RequestURI(), h.path) {
earlyDataStr = request.URL.RequestURI()[len(h.path):]
earlyDataStr := request.URL.RequestURI()[len(h.path):]
earlyData = base64.NewDecoder(base64.RawURLEncoding, bytes.NewReader([]byte(earlyDataStr)))
} else {
writer.WriteHeader(http.StatusNotFound)
@ -62,11 +66,6 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
}
responseHeader := http.Header{}
if h.earlyDataEnabled && h.earlyDataHeaderName != "" {
responseHeader.Set(h.earlyDataHeaderName, earlyDataStr)
}
conn, err := upgrader.Upgrade(writer, request, responseHeader)
if err != nil {
newError("failed to convert to WebSocket connection").Base(err).WriteToLog()