From 39d0b5d6da53b794034f6f6a4b3bedcd2c999ad2 Mon Sep 17 00:00:00 2001 From: Saxon Date: Sun, 28 Apr 2019 02:35:13 +0930 Subject: [PATCH] protocol/rtsp: using sscanf to parse response --- protocol/rtsp/rtsp.go | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/protocol/rtsp/rtsp.go b/protocol/rtsp/rtsp.go index fda744d8..605057ff 100644 --- a/protocol/rtsp/rtsp.go +++ b/protocol/rtsp/rtsp.go @@ -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 {