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