micro/transport/http_transport.go

215 lines
3.5 KiB
Go
Raw Normal View History

2015-05-20 22:57:19 +01:00
package transport
import (
"bufio"
2015-05-20 22:57:19 +01:00
"bytes"
2015-05-21 21:08:19 +01:00
"errors"
2015-05-20 22:57:19 +01:00
"io/ioutil"
"net"
"net/http"
"net/url"
)
type httpTransport struct{}
2015-05-20 22:57:19 +01:00
type httpTransportClient struct {
ht *httpTransport
addr string
conn net.Conn
buff *bufio.Reader
dialOpts dialOptions
r chan *http.Request
2015-05-20 22:57:19 +01:00
}
type httpTransportSocket struct {
r *http.Request
conn net.Conn
2015-05-20 22:57:19 +01:00
}
type httpTransportListener struct {
2015-05-20 22:57:19 +01:00
listener net.Listener
}
func (h *httpTransportClient) Send(m *Message) error {
2015-05-20 22:57:19 +01: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,
}
req := &http.Request{
2015-05-20 22:57:19 +01:00
Method: "POST",
URL: &url.URL{
Scheme: "http",
2015-05-21 19:24:57 +01:00
Host: h.addr,
2015-05-20 22:57:19 +01:00
},
Header: header,
Body: buf,
ContentLength: int64(reqB.Len()),
2015-05-21 19:24:57 +01:00
Host: h.addr,
2015-05-20 22:57:19 +01:00
}
h.r <- req
return req.Write(h.conn)
}
func (h *httpTransportClient) Recv(m *Message) error {
var r *http.Request
if !h.dialOpts.stream {
r = <-h.r
}
rsp, err := http.ReadResponse(h.buff, r)
2015-05-20 22:57:19 +01:00
if err != nil {
return err
2015-05-20 22:57:19 +01:00
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return err
2015-05-20 22:57:19 +01: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] = ""
}
}
*m = *mr
return nil
2015-05-20 22:57:19 +01:00
}
func (h *httpTransportClient) Close() error {
return h.conn.Close()
2015-05-20 22:57:19 +01:00
}
func (h *httpTransportSocket) Recv(m *Message) error {
2015-05-21 21:08:19 +01:00
if m == nil {
return errors.New("message passed in is nil")
}
2015-05-20 22:57:19 +01:00
b, err := ioutil.ReadAll(h.r.Body)
if err != nil {
2015-05-21 21:08:19 +01:00
return err
2015-05-20 22:57:19 +01:00
}
h.r.Body.Close()
2015-05-21 21:08:19 +01:00
mr := &Message{
2015-05-20 22:57:19 +01:00
Header: make(map[string]string),
Body: b,
}
for k, v := range h.r.Header {
if len(v) > 0 {
2015-05-21 21:08:19 +01:00
mr.Header[k] = v[0]
2015-05-20 22:57:19 +01:00
} else {
2015-05-21 21:08:19 +01:00
mr.Header[k] = ""
2015-05-20 22:57:19 +01:00
}
}
2015-05-21 21:08:19 +01:00
*m = *mr
return nil
2015-05-20 22:57:19 +01:00
}
func (h *httpTransportSocket) Send(m *Message) error {
b := bytes.NewBuffer(m.Body)
defer b.Reset()
rsp := &http.Response{
Header: h.r.Header,
Body: &buffer{b},
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: int64(len(m.Body)),
}
2015-05-21 21:08:19 +01:00
for k, v := range m.Header {
rsp.Header.Set(k, v)
2015-05-21 21:08:19 +01:00
}
2015-05-20 22:57:19 +01:00
return rsp.Write(h.conn)
2015-05-20 22:57:19 +01:00
}
func (h *httpTransportSocket) Close() error {
return h.conn.Close()
2015-05-21 21:08:19 +01:00
}
func (h *httpTransportListener) Addr() string {
2015-05-20 22:57:19 +01:00
return h.listener.Addr().String()
}
func (h *httpTransportListener) Close() error {
2015-05-20 22:57:19 +01:00
return h.listener.Close()
}
func (h *httpTransportListener) Accept(fn func(Socket)) error {
2015-05-20 22:57:19 +01:00
srv := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
return
}
fn(&httpTransportSocket{
conn: conn,
r: r,
2015-05-20 22:57:19 +01:00
})
}),
}
return srv.Serve(h.listener)
}
func (h *httpTransport) Dial(addr string, opts ...DialOption) (Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
var dopts dialOptions
for _, opt := range opts {
opt(&dopts)
}
return &httpTransportClient{
ht: h,
addr: addr,
conn: conn,
buff: bufio.NewReader(conn),
dialOpts: dopts,
r: make(chan *http.Request, 1),
2015-05-20 22:57:19 +01:00
}, nil
}
func (h *httpTransport) Listen(addr string) (Listener, error) {
2015-05-20 22:57:19 +01:00
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
return &httpTransportListener{
2015-05-20 22:57:19 +01:00
listener: l,
}, nil
}
func newHttpTransport(addrs []string, opt ...Option) *httpTransport {
return &httpTransport{}
2015-05-20 22:57:19 +01:00
}