From d91995a1cff9e7ea325303df5ece9be7e626013c Mon Sep 17 00:00:00 2001 From: Saxon Date: Sun, 28 Apr 2019 14:04:45 +0930 Subject: [PATCH] protocol/rtsp: using bufio.Scanner --- protocol/rtsp/rtsp.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/protocol/rtsp/rtsp.go b/protocol/rtsp/rtsp.go index b7cd5995..808abf2d 100644 --- a/protocol/rtsp/rtsp.go +++ b/protocol/rtsp/rtsp.go @@ -123,13 +123,15 @@ func (r Response) String() string { func ReadResponse(r io.Reader) (*Response, error) { resp := &Response{Header: make(map[string][]string)} - b := bufio.NewReader(r) + scanner := bufio.NewScanner(r) // Read the first line. - s, err := b.ReadString('\n') + scanner.Scan() + err := scanner.Err() if err != nil { return nil, err } + s := scanner.Text() if len(s) < minResponse { return nil, errSmallResponse @@ -146,17 +148,13 @@ func ReadResponse(r io.Reader) (*Response, error) { } // Read headers. - for { - s, err := b.ReadString('\n') + for scanner.Scan() { + err = scanner.Err() if err != nil { return nil, err } - if strings.TrimRight(s, "\r\n") == "" { - break - } - - parts := strings.SplitN(s, ":", 2) + parts := strings.SplitN(scanner.Text(), ":", 2) resp.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])) } @@ -166,13 +164,12 @@ func ReadResponse(r io.Reader) (*Response, error) { return nil, err } - resp.Body = closer{b, r} + resp.Body = closer{r} return resp, nil } type closer struct { - *bufio.Reader - r io.Reader + io.Reader } func (c closer) Close() error { @@ -181,9 +178,8 @@ func (c closer) Close() error { } defer func() { c.Reader = nil - c.r = nil }() - if r, ok := c.r.(io.ReadCloser); ok { + if r, ok := c.Reader.(io.ReadCloser); ok { return r.Close() } return nil