protocol/rtsp: using strings.Builder to write String formats for Request and Response

This commit is contained in:
Saxon 2019-04-28 01:29:47 +09:30
parent eb6b5a04b0
commit 94660e730b
1 changed files with 11 additions and 10 deletions

View File

@ -31,7 +31,6 @@ package rtsp
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
@ -79,18 +78,19 @@ func (r *Request) Write(w io.Writer) error {
// String returns a formatted string of the Request.
func (r Request) String() string {
s := fmt.Sprintf("%s %s %s/%d.%d\r\n", r.Method, r.URL, r.Proto, r.ProtoMajor, r.ProtoMinor)
var b strings.Builder
b.WriteString(r.Method + " " + r.URL.String() + " " + r.Proto + "/" + strconv.Itoa(r.ProtoMajor) + "." + strconv.Itoa(r.ProtoMinor) + "\r\n")
for k, v := range r.Header {
for _, v := range v {
s += fmt.Sprintf("%s: %s\r\n", k, v)
b.WriteString(k + ": " + v + "\r\n")
}
}
s += "\r\n"
b.WriteString("\r\n")
if r.Body != nil {
str, _ := ioutil.ReadAll(r.Body)
s += string(str)
s, _ := ioutil.ReadAll(r.Body)
b.WriteString(string(s))
}
return s
return b.String()
}
// Response describes an RTSP response.
@ -107,13 +107,14 @@ type Response struct {
// String returns a formatted string of the Response.
func (r Response) String() string {
s := fmt.Sprintf("%s/%d.%d %d %s\n", r.Proto, r.ProtoMajor, r.ProtoMinor, r.StatusCode, r.Status)
var b strings.Builder
b.WriteString(r.Proto + "/" + strconv.Itoa(r.ProtoMajor) + "." + strconv.Itoa(r.ProtoMinor) + " " + strconv.Itoa(r.StatusCode) + " " + r.Status + "\n")
for k, v := range r.Header {
for _, v := range v {
s += fmt.Sprintf("%s: %s\n", k, v)
b.WriteString(k + ": " + v + "\n")
}
}
return s
return b.String()
}
// ReadResponse will read the response of the RTSP request from the connection,