protocol/rtsp: using bufio.Scanner

This commit is contained in:
Saxon 2019-04-28 14:04:45 +09:30
parent d7c2421146
commit d91995a1cf
1 changed files with 10 additions and 14 deletions

View File

@ -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