2015-05-21 00:57:19 +03:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
2015-06-01 20:55:27 +03:00
|
|
|
"bufio"
|
2015-05-21 00:57:19 +03:00
|
|
|
"bytes"
|
2015-05-21 23:08:19 +03:00
|
|
|
"errors"
|
2015-10-22 17:14:56 +03:00
|
|
|
"io"
|
2015-05-21 00:57:19 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-10-22 17:14:56 +03:00
|
|
|
"sync"
|
2015-05-21 00:57:19 +03:00
|
|
|
)
|
|
|
|
|
2015-11-25 03:17:15 +03:00
|
|
|
type buffer struct {
|
|
|
|
io.ReadWriter
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
type httpTransport struct{}
|
2015-05-21 00:57:19 +03:00
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportClient struct {
|
2015-06-01 20:55:27 +03:00
|
|
|
ht *httpTransport
|
|
|
|
addr string
|
|
|
|
conn net.Conn
|
2015-12-31 21:11:46 +03:00
|
|
|
dialOpts DialOptions
|
2015-10-22 17:14:56 +03:00
|
|
|
once sync.Once
|
2015-11-28 19:34:27 +03:00
|
|
|
|
|
|
|
sync.Mutex
|
2015-12-18 23:28:50 +03:00
|
|
|
r chan *http.Request
|
|
|
|
bl []*http.Request
|
2015-11-28 19:34:27 +03:00
|
|
|
buff *bufio.Reader
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportSocket struct {
|
2015-12-18 23:28:50 +03:00
|
|
|
r chan *http.Request
|
2015-06-01 20:55:27 +03:00
|
|
|
conn net.Conn
|
2015-12-18 23:28:50 +03:00
|
|
|
once sync.Once
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
buff *bufio.Reader
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportListener struct {
|
2015-05-21 00:57:19 +03:00
|
|
|
listener net.Listener
|
|
|
|
}
|
|
|
|
|
2015-11-25 03:17:15 +03:00
|
|
|
func (b *buffer) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func (h *httpTransportClient) Send(m *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,
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
req := &http.Request{
|
2015-05-21 00:57:19 +03:00
|
|
|
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-12-18 23:28:50 +03:00
|
|
|
h.Lock()
|
|
|
|
h.bl = append(h.bl, req)
|
|
|
|
select {
|
|
|
|
case h.r <- h.bl[0]:
|
|
|
|
h.bl = h.bl[1:]
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
h.Unlock()
|
2015-06-01 20:55:27 +03:00
|
|
|
|
|
|
|
return req.Write(h.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpTransportClient) Recv(m *Message) error {
|
|
|
|
var r *http.Request
|
2015-12-31 21:14:40 +03:00
|
|
|
if !h.dialOpts.Stream {
|
2015-10-22 17:14:56 +03:00
|
|
|
rc, ok := <-h.r
|
|
|
|
if !ok {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
r = rc
|
2015-06-01 20:55:27 +03:00
|
|
|
}
|
|
|
|
|
2015-11-28 19:34:27 +03:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
if h.buff == nil {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
rsp, err := http.ReadResponse(h.buff, r)
|
2015-05-21 00:57:19 +03:00
|
|
|
if err != nil {
|
2015-06-01 20:55:27 +03:00
|
|
|
return err
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
2015-06-01 20:55:27 +03:00
|
|
|
return err
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
*m = *mr
|
|
|
|
return nil
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportClient) Close() error {
|
2015-11-28 19:34:27 +03:00
|
|
|
err := h.conn.Close()
|
2015-10-22 17:14:56 +03:00
|
|
|
h.once.Do(func() {
|
2015-11-28 19:34:27 +03:00
|
|
|
h.Lock()
|
|
|
|
h.buff.Reset(nil)
|
|
|
|
h.buff = nil
|
|
|
|
h.Unlock()
|
2015-10-22 17:14:56 +03:00
|
|
|
close(h.r)
|
|
|
|
})
|
2015-11-28 19:34:27 +03:00
|
|
|
return err
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
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-12-18 23:28:50 +03:00
|
|
|
r, err := http.ReadRequest(h.buff)
|
2015-05-21 00:57:19 +03:00
|
|
|
if err != nil {
|
2015-05-21 23:08:19 +03:00
|
|
|
return err
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
2015-12-18 23:28:50 +03:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.Body.Close()
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2015-12-18 23:28:50 +03:00
|
|
|
for k, v := range r.Header {
|
2015-05-21 00:57:19 +03:00
|
|
|
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-12-18 23:28:50 +03:00
|
|
|
select {
|
|
|
|
case h.r <- r:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
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-06-01 20:55:27 +03:00
|
|
|
b := bytes.NewBuffer(m.Body)
|
|
|
|
defer b.Reset()
|
2015-12-18 23:28:50 +03:00
|
|
|
|
|
|
|
r := <-h.r
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
rsp := &http.Response{
|
2015-12-18 23:28:50 +03:00
|
|
|
Header: r.Header,
|
2015-06-01 20:55:27 +03:00
|
|
|
Body: &buffer{b},
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
ContentLength: int64(len(m.Body)),
|
|
|
|
}
|
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
for k, v := range m.Header {
|
2015-06-01 20:55:27 +03:00
|
|
|
rsp.Header.Set(k, v)
|
2015-05-21 23:08:19 +03:00
|
|
|
}
|
2015-05-21 00:57:19 +03:00
|
|
|
|
2015-12-18 23:28:50 +03:00
|
|
|
select {
|
|
|
|
case h.r <- r:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
return rsp.Write(h.conn)
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-24 03:52:59 +03:00
|
|
|
func (h *httpTransportSocket) error(m *Message) error {
|
|
|
|
b := bytes.NewBuffer(m.Body)
|
|
|
|
defer b.Reset()
|
|
|
|
rsp := &http.Response{
|
|
|
|
Header: make(http.Header),
|
|
|
|
Body: &buffer{b},
|
|
|
|
Status: "500 Internal Server Error",
|
|
|
|
StatusCode: 500,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
ContentLength: int64(len(m.Body)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range m.Header {
|
|
|
|
rsp.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Write(h.conn)
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportSocket) Close() error {
|
2015-12-18 23:28:50 +03:00
|
|
|
err := h.conn.Close()
|
|
|
|
h.once.Do(func() {
|
|
|
|
h.Lock()
|
|
|
|
h.buff.Reset(nil)
|
|
|
|
h.buff = nil
|
|
|
|
h.Unlock()
|
|
|
|
})
|
|
|
|
return err
|
2015-05-21 23:08:19 +03:00
|
|
|
}
|
|
|
|
|
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-12-18 23:28:50 +03:00
|
|
|
for {
|
|
|
|
c, err := h.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-24 03:52:59 +03:00
|
|
|
|
2015-12-18 23:28:50 +03:00
|
|
|
sock := &httpTransportSocket{
|
|
|
|
conn: c,
|
|
|
|
buff: bufio.NewReader(c),
|
|
|
|
r: make(chan *http.Request, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2015-11-24 03:52:59 +03:00
|
|
|
// TODO: think of a better error response strategy
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
sock.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fn(sock)
|
2015-12-18 23:28:50 +03:00
|
|
|
}()
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
2015-12-18 23:28:50 +03:00
|
|
|
return nil
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func (h *httpTransport) Dial(addr string, opts ...DialOption) (Client, error) {
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-31 21:11:46 +03:00
|
|
|
var dopts DialOptions
|
2015-06-01 20:55:27 +03:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&dopts)
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
return &httpTransportClient{
|
2015-06-01 20:55:27 +03:00
|
|
|
ht: h,
|
|
|
|
addr: addr,
|
|
|
|
conn: conn,
|
|
|
|
buff: bufio.NewReader(conn),
|
|
|
|
dialOpts: dopts,
|
|
|
|
r: make(chan *http.Request, 1),
|
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-12-20 00:56:14 +03:00
|
|
|
func (h *httpTransport) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func newHttpTransport(addrs []string, opt ...Option) *httpTransport {
|
2015-06-01 20:55:27 +03:00
|
|
|
return &httpTransport{}
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|