2015-05-21 00:57:19 +03:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-05-21 23:08:19 +03:00
|
|
|
"errors"
|
2015-05-21 00:57:19 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type headerRoundTripper struct {
|
|
|
|
r http.RoundTripper
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransport struct {
|
2015-05-21 00:57:19 +03:00
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportClient struct {
|
|
|
|
ht *httpTransport
|
2015-05-21 21:24:57 +03:00
|
|
|
addr string
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportSocket struct {
|
2015-05-21 00:57:19 +03:00
|
|
|
r *http.Request
|
|
|
|
w http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportListener struct {
|
2015-05-21 00:57:19 +03:00
|
|
|
listener net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
r.Header.Set("X-Client-Version", "1.0")
|
|
|
|
return t.r.RoundTrip(r)
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportClient) Send(m *Message) (*Message, error) {
|
2015-05-21 00:57:19 +03:00
|
|
|
header := make(http.Header)
|
|
|
|
|
|
|
|
for k, v := range m.Header {
|
|
|
|
header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqB := bytes.NewBuffer(m.Body)
|
|
|
|
defer reqB.Reset()
|
|
|
|
buf := &buffer{
|
|
|
|
reqB,
|
|
|
|
}
|
|
|
|
|
|
|
|
hreq := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "http",
|
2015-05-21 21:24:57 +03:00
|
|
|
Host: h.addr,
|
2015-05-21 00:57:19 +03:00
|
|
|
},
|
|
|
|
Header: header,
|
|
|
|
Body: buf,
|
|
|
|
ContentLength: int64(reqB.Len()),
|
2015-05-21 21:24:57 +03:00
|
|
|
Host: h.addr,
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:24:57 +03:00
|
|
|
rsp, err := h.ht.client.Do(hreq)
|
2015-05-21 00:57:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mr := &Message{
|
|
|
|
Header: make(map[string]string),
|
|
|
|
Body: b,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range rsp.Header {
|
|
|
|
if len(v) > 0 {
|
|
|
|
mr.Header[k] = v[0]
|
|
|
|
} else {
|
|
|
|
mr.Header[k] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mr, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportClient) Close() error {
|
2015-05-21 00:57:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportSocket) Recv(m *Message) error {
|
2015-05-21 23:08:19 +03:00
|
|
|
if m == nil {
|
|
|
|
return errors.New("message passed in is nil")
|
|
|
|
}
|
|
|
|
|
2015-05-21 00:57:19 +03:00
|
|
|
b, err := ioutil.ReadAll(h.r.Body)
|
|
|
|
if err != nil {
|
2015-05-21 23:08:19 +03:00
|
|
|
return err
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
mr := &Message{
|
2015-05-21 00:57:19 +03:00
|
|
|
Header: make(map[string]string),
|
|
|
|
Body: b,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range h.r.Header {
|
|
|
|
if len(v) > 0 {
|
2015-05-21 23:08:19 +03:00
|
|
|
mr.Header[k] = v[0]
|
2015-05-21 00:57:19 +03:00
|
|
|
} else {
|
2015-05-21 23:08:19 +03:00
|
|
|
mr.Header[k] = ""
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
*m = *mr
|
|
|
|
return nil
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportSocket) Send(m *Message) error {
|
2015-05-21 23:08:19 +03:00
|
|
|
for k, v := range m.Header {
|
|
|
|
h.w.Header().Set(k, v)
|
|
|
|
}
|
2015-05-21 00:57:19 +03:00
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
_, err := h.w.Write(m.Body)
|
2015-05-21 00:57:19 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportSocket) Close() error {
|
2015-05-21 23:08:19 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportListener) Addr() string {
|
2015-05-21 00:57:19 +03:00
|
|
|
return h.listener.Addr().String()
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportListener) Close() error {
|
2015-05-21 00:57:19 +03:00
|
|
|
return h.listener.Close()
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportListener) Accept(fn func(Socket)) error {
|
2015-05-21 00:57:19 +03:00
|
|
|
srv := &http.Server{
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-23 22:04:16 +03:00
|
|
|
fn(&httpTransportSocket{
|
2015-05-21 00:57:19 +03:00
|
|
|
r: r,
|
|
|
|
w: w,
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv.Serve(h.listener)
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransport) Dial(addr string) (Client, error) {
|
|
|
|
return &httpTransportClient{
|
2015-05-21 21:24:57 +03:00
|
|
|
ht: h,
|
|
|
|
addr: addr,
|
2015-05-21 00:57:19 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransport) Listen(addr string) (Listener, error) {
|
2015-05-21 00:57:19 +03:00
|
|
|
l, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
return &httpTransportListener{
|
2015-05-21 00:57:19 +03:00
|
|
|
listener: l,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func newHttpTransport(addrs []string, opt ...Option) *httpTransport {
|
2015-05-21 00:57:19 +03:00
|
|
|
client := &http.Client{}
|
|
|
|
client.Transport = &headerRoundTripper{http.DefaultTransport}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
return &httpTransport{client: client}
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|