protocol/rtsp: using sscanf to parse response

This commit is contained in:
Saxon 2019-04-28 02:35:13 +09:30
parent 26d05d1bd4
commit 39d0b5d6da
1 changed files with 7 additions and 27 deletions

View File

@ -31,6 +31,7 @@ package rtsp
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
@ -135,38 +136,17 @@ func ReadResponse(r io.Reader) (*Response, error) {
}
// Check that it was terminated by CRLF.
if []byte(s)[len(s)-2] != '\r' {
if s[len(s)-2] != '\r' {
return nil, errors.New("line not terminated by CRLF")
}
// Split first line.
s1 := strings.SplitN(s, " ", 3)
var proto string
fmt.Sscanf(s, "%s %d %s", &proto, &resp.StatusCode, &resp.Status)
// Get protocol.
s2 := strings.SplitN(s1[0], "/", 2)
resp.Proto = s2[0]
s1 := strings.Split(proto, "/")
resp.Proto = s1[0]
// Get major version.
s3 := strings.SplitN(s2[1], ".", 2)
resp.ProtoMajor, err = strconv.Atoi(s3[0])
if err != nil {
return nil, err
}
// Get minor version.
resp.ProtoMinor, err = strconv.Atoi(s3[1])
if err != nil {
return nil, err
}
// Get status code.
resp.StatusCode, err = strconv.Atoi(s1[1])
if err != nil {
return nil, err
}
// Get status
resp.Status = strings.TrimSpace(s1[2])
fmt.Sscanf(s1[1], "%d.%d", &resp.ProtoMajor, &resp.ProtoMinor)
// Read headers.
for {