Add support events for HomeKit client

This commit is contained in:
Alexey Khit
2023-09-12 21:03:59 +03:00
parent 9cf75565b5
commit 861632f92b
4 changed files with 61 additions and 69 deletions
+28
View File
@@ -1,6 +1,7 @@
package hap
import (
"bufio"
"errors"
"io"
"net/http"
@@ -22,6 +23,9 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) {
if err := req.Write(c.Conn); err != nil {
return nil, err
}
if c.res != nil {
return <-c.res, c.err
}
return http.ReadResponse(c.reader, req)
}
@@ -54,3 +58,27 @@ func (c *Client) Post(path, contentType string, body io.Reader) (*http.Response,
func (c *Client) Put(path, contentType string, body io.Reader) (*http.Response, error) {
return c.Request("PUT", path, contentType, body)
}
const ProtoEvent = "EVENT/1.0"
func ReadResponse(r *bufio.Reader, req *http.Request) (*http.Response, error) {
b, err := r.Peek(9)
if err != nil {
return nil, err
}
if string(b) != ProtoEvent {
return http.ReadResponse(r, req)
}
copy(b, "HTTP/1.1 ")
res, err := http.ReadResponse(r, req)
if err != nil {
return nil, err
}
res.Proto = ProtoEvent
return res, nil
}