mirror of https://bitbucket.org/ausocean/av.git
protocol/rtsp: using bufio.Scanner
This commit is contained in:
parent
d7c2421146
commit
d91995a1cf
|
@ -123,13 +123,15 @@ func (r Response) String() string {
|
||||||
func ReadResponse(r io.Reader) (*Response, error) {
|
func ReadResponse(r io.Reader) (*Response, error) {
|
||||||
resp := &Response{Header: make(map[string][]string)}
|
resp := &Response{Header: make(map[string][]string)}
|
||||||
|
|
||||||
b := bufio.NewReader(r)
|
scanner := bufio.NewScanner(r)
|
||||||
|
|
||||||
// Read the first line.
|
// Read the first line.
|
||||||
s, err := b.ReadString('\n')
|
scanner.Scan()
|
||||||
|
err := scanner.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
s := scanner.Text()
|
||||||
|
|
||||||
if len(s) < minResponse {
|
if len(s) < minResponse {
|
||||||
return nil, errSmallResponse
|
return nil, errSmallResponse
|
||||||
|
@ -146,17 +148,13 @@ func ReadResponse(r io.Reader) (*Response, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read headers.
|
// Read headers.
|
||||||
for {
|
for scanner.Scan() {
|
||||||
s, err := b.ReadString('\n')
|
err = scanner.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimRight(s, "\r\n") == "" {
|
parts := strings.SplitN(scanner.Text(), ":", 2)
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := strings.SplitN(s, ":", 2)
|
|
||||||
resp.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.Body = closer{b, r}
|
resp.Body = closer{r}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type closer struct {
|
type closer struct {
|
||||||
*bufio.Reader
|
io.Reader
|
||||||
r io.Reader
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c closer) Close() error {
|
func (c closer) Close() error {
|
||||||
|
@ -181,9 +178,8 @@ func (c closer) Close() error {
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
c.Reader = nil
|
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 r.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue