base: return error from Write() methods

This commit is contained in:
aler9
2022-05-11 13:30:09 +02:00
parent 6fe7a316ba
commit ee6d7a87a3
5 changed files with 38 additions and 17 deletions

View File

@@ -35,10 +35,11 @@ func (b *body) read(header Header, rb *bufio.Reader) error {
return nil return nil
} }
func (b body) write(w io.Writer) { func (b body) write(w io.Writer) error {
if len(b) == 0 { if len(b) == 0 {
return return nil
} }
w.Write(b) _, err := w.Write(b)
return err
} }

View File

@@ -98,7 +98,7 @@ func (h *Header) read(rb *bufio.Reader) error {
return nil return nil
} }
func (h Header) write(w io.Writer) { func (h Header) write(w io.Writer) error {
// sort headers by key // sort headers by key
// in order to obtain deterministic results // in order to obtain deterministic results
keys := make([]string, len(h)) keys := make([]string, len(h))
@@ -109,9 +109,13 @@ func (h Header) write(w io.Writer) {
for _, key := range keys { for _, key := range keys {
for _, val := range h[key] { for _, val := range h[key] {
w.Write([]byte(key + ": " + val + "\r\n")) _, err := w.Write([]byte(key + ": " + val + "\r\n"))
if err != nil {
return err
}
} }
} }
w.Write([]byte("\r\n")) _, err := w.Write([]byte("\r\n"))
return err
} }

View File

@@ -106,11 +106,15 @@ func (f *InterleavedFrame) Read(maxPayloadSize int, br *bufio.Reader) error {
} }
// Write writes an InterleavedFrame into a buffered writer. // Write writes an InterleavedFrame into a buffered writer.
func (f InterleavedFrame) Write(w io.Writer) { func (f InterleavedFrame) Write(w io.Writer) error {
buf := []byte{0x24, byte(f.Channel), 0x00, 0x00} buf := []byte{0x24, byte(f.Channel), 0x00, 0x00}
binary.BigEndian.PutUint16(buf[2:], uint16(len(f.Payload))) binary.BigEndian.PutUint16(buf[2:], uint16(len(f.Payload)))
w.Write(buf) _, err := w.Write(buf)
if err != nil {
return err
}
w.Write(f.Payload) _, err = w.Write(f.Payload)
return err
} }

View File

@@ -118,17 +118,23 @@ func (req *Request) ReadIgnoreFrames(maxPayloadSize int, rb *bufio.Reader) error
} }
// Write writes a request. // Write writes a request.
func (req Request) Write(w io.Writer) { func (req Request) Write(w io.Writer) error {
urStr := req.URL.CloneWithoutCredentials().String() urStr := req.URL.CloneWithoutCredentials().String()
w.Write([]byte(string(req.Method) + " " + urStr + " " + rtspProtocol10 + "\r\n")) _, err := w.Write([]byte(string(req.Method) + " " + urStr + " " + rtspProtocol10 + "\r\n"))
if err != nil {
return err
}
if len(req.Body) != 0 { if len(req.Body) != 0 {
req.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(req.Body)), 10)} req.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(req.Body)), 10)}
} }
req.Header.write(w) err = req.Header.write(w)
if err != nil {
return err
}
body(req.Body).write(w) return body(req.Body).write(w)
} }
// String implements fmt.Stringer. // String implements fmt.Stringer.

View File

@@ -204,24 +204,30 @@ func (res *Response) ReadIgnoreFrames(maxPayloadSize int, rb *bufio.Reader) erro
} }
// Write writes a Response. // Write writes a Response.
func (res Response) Write(w io.Writer) { func (res Response) Write(w io.Writer) error {
if res.StatusMessage == "" { if res.StatusMessage == "" {
if status, ok := statusMessages[res.StatusCode]; ok { if status, ok := statusMessages[res.StatusCode]; ok {
res.StatusMessage = status res.StatusMessage = status
} }
} }
w.Write([]byte(rtspProtocol10 + " " + _, err := w.Write([]byte(rtspProtocol10 + " " +
strconv.FormatInt(int64(res.StatusCode), 10) + " " + strconv.FormatInt(int64(res.StatusCode), 10) + " " +
res.StatusMessage + "\r\n")) res.StatusMessage + "\r\n"))
if err != nil {
return err
}
if len(res.Body) != 0 { if len(res.Body) != 0 {
res.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(res.Body)), 10)} res.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(res.Body)), 10)}
} }
res.Header.write(w) err = res.Header.write(w)
if err != nil {
return err
}
body(res.Body).write(w) return body(res.Body).write(w)
} }
// String implements fmt.Stringer. // String implements fmt.Stringer.