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 ( import (
"bufio" "bufio"
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -135,38 +136,17 @@ func ReadResponse(r io.Reader) (*Response, error) {
} }
// Check that it was terminated by CRLF. // 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") return nil, errors.New("line not terminated by CRLF")
} }
// Split first line. var proto string
s1 := strings.SplitN(s, " ", 3) fmt.Sscanf(s, "%s %d %s", &proto, &resp.StatusCode, &resp.Status)
// Get protocol. s1 := strings.Split(proto, "/")
s2 := strings.SplitN(s1[0], "/", 2) resp.Proto = s1[0]
resp.Proto = s2[0]
// Get major version. fmt.Sscanf(s1[1], "%d.%d", &resp.ProtoMajor, &resp.ProtoMinor)
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])
// Read headers. // Read headers.
for { for {