Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2022-03-22 01:03:13 +03:00
parent 43478edf95
commit 8827a2547b

34
http.go
View File

@ -2,7 +2,7 @@ package http
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"net/http" "net/http"
"storj.io/drpc" "storj.io/drpc"
@ -16,6 +16,7 @@ type httpTransport struct {
endpoint string endpoint string
ct string ct string
closed bool closed bool
body io.ReadCloser
} }
func NewTransport(c *http.Client, endpoint, method, ct string) *httpTransport { func NewTransport(c *http.Client, endpoint, method, ct string) *httpTransport {
@ -26,33 +27,30 @@ func NewTransport(c *http.Client, endpoint, method, ct string) *httpTransport {
} }
func (t *httpTransport) Read(buf []byte) (int, error) { func (t *httpTransport) Read(buf []byte) (int, error) {
req, err := http.NewRequest(t.method, t.endpoint, nil) return t.body.Read(buf)
if err != nil {
return 0, err
}
req.Header.Add("Content-Type", t.ct)
rsp, err := t.c.Do(req)
if err != nil {
return 0, err
}
buf, err = ioutil.ReadAll(rsp.Body)
return len(buf), err
} }
func (t *httpTransport) Write(buf []byte) (int, error) { func (t *httpTransport) Write(buf []byte) (n int, err error) {
req, err := http.NewRequest(t.method, t.endpoint, bytes.NewReader(buf)) var req *http.Request
var rsp *http.Response
req, err = http.NewRequest(t.method, t.endpoint, bytes.NewReader(buf))
if err != nil { if err != nil {
return 0, err return n, err
} }
req.Header.Add("Content-Type", t.ct) req.Header.Add("Content-Type", t.ct)
_, err = t.c.Do(req) rsp, err = t.c.Do(req)
if err != nil { if err != nil {
return 0, err return n, err
} }
t.body = rsp.Body
return len(buf), err return len(buf), err
} }
func (t *httpTransport) Close() error { func (t *httpTransport) Close() error {
t.closed = true if t.body == nil {
return nil return nil
}
return t.body.Close()
} }