Revert "Fix stream sniffer"

This commit is contained in:
世界 2024-09-20 20:40:02 +08:00
parent e2077009c4
commit 39d7127651
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -40,23 +40,29 @@ func PeekStream(ctx context.Context, conn net.Conn, buffer *buf.Buffer, timeout
} }
deadline := time.Now().Add(timeout) deadline := time.Now().Add(timeout)
var errors []error var errors []error
for i := 0; ; i++ {
err := conn.SetReadDeadline(deadline) err := conn.SetReadDeadline(deadline)
if err != nil { if err != nil {
return nil, E.Cause(err, "set read deadline") return nil, E.Cause(err, "set read deadline")
} }
defer conn.SetReadDeadline(time.Time{}) _, err = buffer.ReadOnceFrom(conn)
_ = conn.SetReadDeadline(time.Time{})
if err != nil {
if i > 0 {
break
}
return nil, E.Cause(err, "read payload")
}
errors = nil
var metadata *adapter.InboundContext var metadata *adapter.InboundContext
for _, sniffer := range sniffers { for _, sniffer := range sniffers {
if buffer.IsEmpty() { metadata, err = sniffer(ctx, bytes.NewReader(buffer.Bytes()))
metadata, err = sniffer(ctx, io.TeeReader(conn, buffer))
} else {
metadata, err = sniffer(ctx, io.MultiReader(bytes.NewReader(buffer.Bytes()), io.TeeReader(conn, buffer)))
}
if metadata != nil { if metadata != nil {
return metadata, nil return metadata, nil
} }
errors = append(errors, err) errors = append(errors, err)
} }
}
return nil, E.Errors(errors...) return nil, E.Errors(errors...)
} }