You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1020 B

6 years ago
package rtsp
import (
"fmt"
"strconv"
)
type Response struct {
Version string
StatusCode int
Status string
Header map[string]interface{}
6 years ago
Body string
}
func NewResponse(statusCode int, status, cSeq, sid, body string) *Response {
res := &Response{
Version: RTSP_VERSION,
StatusCode: statusCode,
Status: status,
Header: map[string]interface{}{"CSeq": cSeq, "Session": sid},
6 years ago
Body: body,
}
len := len(body)
if len > 0 {
res.Header["Content-Length"] = strconv.Itoa(len)
} else {
delete(res.Header, "Content-Length")
}
return res
}
func (r *Response) String() string {
str := fmt.Sprintf("%s %d %s\r\n", r.Version, r.StatusCode, r.Status)
for key, value := range r.Header {
str += fmt.Sprintf("%s: %s\r\n", key, value)
}
str += "\r\n"
str += r.Body
return str
}
func (r *Response) SetBody(body string) {
len := len(body)
r.Body = body
if len > 0 {
r.Header["Content-Length"] = strconv.Itoa(len)
} else {
delete(r.Header, "Content-Length")
}
}