1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-12 08:23:58 +02:00
go-micro/transport/http_transport.go

528 lines
9.0 KiB
Go
Raw Normal View History

2015-05-20 23:57:19 +02:00
package transport
import (
"bufio"
2015-05-20 23:57:19 +02:00
"bytes"
2016-01-18 02:10:04 +02:00
"crypto/tls"
2015-05-21 22:08:19 +02:00
"errors"
2016-03-02 16:20:28 +02:00
"fmt"
2015-10-22 16:14:56 +02:00
"io"
2015-05-20 23:57:19 +02:00
"io/ioutil"
"log"
2015-05-20 23:57:19 +02:00
"net"
"net/http"
"net/url"
2016-03-02 16:20:28 +02:00
"strconv"
"strings"
2015-10-22 16:14:56 +02:00
"sync"
"time"
2016-01-18 02:10:04 +02:00
mls "github.com/micro/misc/lib/tls"
2015-05-20 23:57:19 +02:00
)
2015-11-25 02:17:15 +02:00
type buffer struct {
io.ReadWriter
}
2016-01-18 02:10:04 +02:00
type httpTransport struct {
opts Options
}
2015-05-20 23:57:19 +02:00
type httpTransportClient struct {
ht *httpTransport
addr string
conn net.Conn
dialOpts DialOptions
2015-10-22 16:14:56 +02:00
once sync.Once
2015-11-28 18:34:27 +02:00
sync.Mutex
r chan *http.Request
bl []*http.Request
2015-11-28 18:34:27 +02:00
buff *bufio.Reader
2015-05-20 23:57:19 +02:00
}
type httpTransportSocket struct {
2016-07-28 20:12:56 +02:00
ht *httpTransport
r chan *http.Request
conn net.Conn
once sync.Once
sync.Mutex
buff *bufio.Reader
2015-05-20 23:57:19 +02:00
}
type httpTransportListener struct {
2016-07-28 20:12:56 +02:00
ht *httpTransport
2015-05-20 23:57:19 +02:00
listener net.Listener
}
2016-10-29 22:34:15 +02:00
func getIPAddrs() []string {
ifaces, err := net.Interfaces()
if err != nil {
return nil
}
var ipAddrs []string
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil {
continue
}
ip = ip.To4()
if ip == nil {
continue
}
ipAddrs = append(ipAddrs, ip.String())
}
}
return ipAddrs
}
2016-03-02 16:20:28 +02:00
func listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error) {
// host:port || host:min-max
parts := strings.Split(addr, ":")
//
if len(parts) < 2 {
return fn(addr)
}
// try to extract port range
ports := strings.Split(parts[len(parts)-1], "-")
// single port
if len(ports) < 2 {
return fn(addr)
}
// we have a port range
// extract min port
min, err := strconv.Atoi(ports[0])
if err != nil {
return nil, errors.New("unable to extract port range")
}
// extract max port
max, err := strconv.Atoi(ports[1])
if err != nil {
return nil, errors.New("unable to extract port range")
}
// set host
host := parts[:len(parts)-1]
// range the ports
for port := min; port <= max; port++ {
// try bind to host:port
ln, err := fn(fmt.Sprintf("%s:%d", host, port))
if err == nil {
return ln, nil
}
// hit max port
if port == max {
return nil, err
}
}
// why are we here?
return nil, fmt.Errorf("unable to bind to %s", addr)
}
2015-11-25 02:17:15 +02:00
func (b *buffer) Close() error {
return nil
}
func (h *httpTransportClient) Send(m *Message) error {
2015-05-20 23:57:19 +02: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 23:57:19 +02:00
Method: "POST",
URL: &url.URL{
Scheme: "http",
2015-05-21 20:24:57 +02:00
Host: h.addr,
2015-05-20 23:57:19 +02:00
},
Header: header,
Body: buf,
ContentLength: int64(reqB.Len()),
2015-05-21 20:24:57 +02:00
Host: h.addr,
2015-05-20 23:57:19 +02:00
}
h.Lock()
h.bl = append(h.bl, req)
select {
case h.r <- h.bl[0]:
h.bl = h.bl[1:]
default:
}
h.Unlock()
2016-08-01 17:31:27 +02:00
// set timeout if its greater than 0
if h.ht.opts.Timeout > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Timeout))
2016-07-28 20:12:56 +02:00
}
return req.Write(h.conn)
}
func (h *httpTransportClient) Recv(m *Message) error {
2016-10-29 22:33:04 +02:00
if m == nil {
return errors.New("message passed in is nil")
}
var r *http.Request
if !h.dialOpts.Stream {
2015-10-22 16:14:56 +02:00
rc, ok := <-h.r
if !ok {
return io.EOF
}
r = rc
}
2015-11-28 18:34:27 +02:00
h.Lock()
defer h.Unlock()
if h.buff == nil {
return io.EOF
}
2016-08-01 17:31:27 +02:00
// set timeout if its greater than 0
if h.ht.opts.Timeout > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Timeout))
2016-07-28 20:12:56 +02:00
}
rsp, err := http.ReadResponse(h.buff, r)
2015-05-20 23:57:19 +02:00
if err != nil {
return err
2015-05-20 23:57:19 +02:00
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return err
2015-05-20 23:57:19 +02:00
}
if rsp.StatusCode != 200 {
return errors.New(rsp.Status + ": " + string(b))
}
2016-10-29 22:33:04 +02:00
m.Body = b
if m.Header == nil {
m.Header = make(map[string]string)
2015-05-20 23:57:19 +02:00
}
for k, v := range rsp.Header {
if len(v) > 0 {
2016-10-29 22:33:04 +02:00
m.Header[k] = v[0]
2015-05-20 23:57:19 +02:00
} else {
2016-10-29 22:33:04 +02:00
m.Header[k] = ""
2015-05-20 23:57:19 +02:00
}
}
return nil
2015-05-20 23:57:19 +02:00
}
func (h *httpTransportClient) Close() error {
2015-11-28 18:34:27 +02:00
err := h.conn.Close()
2015-10-22 16:14:56 +02:00
h.once.Do(func() {
2015-11-28 18:34:27 +02:00
h.Lock()
h.buff.Reset(nil)
h.buff = nil
h.Unlock()
2015-10-22 16:14:56 +02:00
close(h.r)
})
2015-11-28 18:34:27 +02:00
return err
2015-05-20 23:57:19 +02:00
}
func (h *httpTransportSocket) Recv(m *Message) error {
2015-05-21 22:08:19 +02:00
if m == nil {
return errors.New("message passed in is nil")
}
2016-08-01 17:31:27 +02:00
// set timeout if its greater than 0
if h.ht.opts.Timeout > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Timeout))
2016-07-28 20:12:56 +02:00
}
r, err := http.ReadRequest(h.buff)
2015-05-20 23:57:19 +02:00
if err != nil {
2015-05-21 22:08:19 +02:00
return err
2015-05-20 23:57:19 +02:00
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
r.Body.Close()
2016-10-29 22:33:04 +02:00
m.Body = b
2016-10-29 22:33:04 +02:00
if m.Header == nil {
m.Header = make(map[string]string)
2015-05-20 23:57:19 +02:00
}
for k, v := range r.Header {
2015-05-20 23:57:19 +02:00
if len(v) > 0 {
2016-10-29 22:33:04 +02:00
m.Header[k] = v[0]
2015-05-20 23:57:19 +02:00
} else {
2016-10-29 22:33:04 +02:00
m.Header[k] = ""
2015-05-20 23:57:19 +02:00
}
}
select {
case h.r <- r:
default:
}
2015-05-21 22:08:19 +02:00
return nil
2015-05-20 23:57:19 +02:00
}
func (h *httpTransportSocket) Send(m *Message) error {
b := bytes.NewBuffer(m.Body)
defer b.Reset()
r := <-h.r
rsp := &http.Response{
Header: 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 22:08:19 +02:00
for k, v := range m.Header {
rsp.Header.Set(k, v)
2015-05-21 22:08:19 +02:00
}
2015-05-20 23:57:19 +02:00
select {
case h.r <- r:
default:
}
2016-08-01 17:31:27 +02:00
// set timeout if its greater than 0
if h.ht.opts.Timeout > time.Duration(0) {
h.conn.SetDeadline(time.Now().Add(h.ht.opts.Timeout))
2016-07-28 20:12:56 +02:00
}
return rsp.Write(h.conn)
2015-05-20 23:57:19 +02: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)
}
func (h *httpTransportSocket) Close() error {
err := h.conn.Close()
h.once.Do(func() {
h.Lock()
h.buff.Reset(nil)
h.buff = nil
h.Unlock()
})
return err
2015-05-21 22:08:19 +02:00
}
func (h *httpTransportListener) Addr() string {
2015-05-20 23:57:19 +02:00
return h.listener.Addr().String()
}
func (h *httpTransportListener) Close() error {
2015-05-20 23:57:19 +02:00
return h.listener.Close()
}
func (h *httpTransportListener) Accept(fn func(Socket)) error {
var tempDelay time.Duration
2016-04-06 19:03:27 +02:00
for {
c, err := h.listener.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
log.Printf("http: Accept error: %v; retrying in %v\n", err, tempDelay)
time.Sleep(tempDelay)
continue
}
return err
}
sock := &httpTransportSocket{
2016-07-28 20:12:56 +02:00
ht: h.ht,
conn: c,
buff: bufio.NewReader(c),
r: make(chan *http.Request, 1),
}
go func() {
// TODO: think of a better error response strategy
defer func() {
if r := recover(); r != nil {
sock.Close()
}
}()
fn(sock)
}()
2015-05-20 23:57:19 +02:00
}
}
func (h *httpTransport) Dial(addr string, opts ...DialOption) (Client, error) {
2016-01-03 23:25:03 +02:00
dopts := DialOptions{
Timeout: DefaultDialTimeout,
}
for _, opt := range opts {
opt(&dopts)
}
2016-01-18 02:10:04 +02:00
var conn net.Conn
var err error
// TODO: support dial option here rather than using internal config
if h.opts.Secure || h.opts.TLSConfig != nil {
config := h.opts.TLSConfig
if config == nil {
config = &tls.Config{
InsecureSkipVerify: true,
}
}
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: dopts.Timeout}, "tcp", addr, config)
} else {
conn, err = net.DialTimeout("tcp", addr, dopts.Timeout)
}
2016-01-03 23:25:03 +02:00
if err != nil {
return nil, err
}
return &httpTransportClient{
ht: h,
addr: addr,
conn: conn,
buff: bufio.NewReader(conn),
dialOpts: dopts,
r: make(chan *http.Request, 1),
2015-05-20 23:57:19 +02:00
}, nil
}
2016-01-18 02:10:04 +02:00
func (h *httpTransport) Listen(addr string, opts ...ListenOption) (Listener, error) {
var options ListenOptions
for _, o := range opts {
o(&options)
}
var l net.Listener
var err error
// TODO: support use of listen options
if h.opts.Secure || h.opts.TLSConfig != nil {
config := h.opts.TLSConfig
2016-03-02 16:20:28 +02:00
fn := func(addr string) (net.Listener, error) {
if config == nil {
2016-06-05 17:13:29 +02:00
hosts := []string{addr}
2016-06-06 15:05:02 +02:00
// check if its a valid host:port
if host, _, err := net.SplitHostPort(addr); err == nil {
if len(host) == 0 {
hosts = getIPAddrs()
2016-06-05 17:13:29 +02:00
} else {
2016-06-06 15:05:02 +02:00
hosts = []string{host}
2016-06-05 17:13:29 +02:00
}
}
2016-06-06 15:05:02 +02:00
// generate a certificate
2016-06-05 17:13:29 +02:00
cert, err := mls.Certificate(hosts...)
2016-03-02 16:20:28 +02:00
if err != nil {
return nil, err
}
config = &tls.Config{Certificates: []tls.Certificate{cert}}
2016-01-18 02:10:04 +02:00
}
2016-03-02 16:20:28 +02:00
return tls.Listen("tcp", addr, config)
2016-01-18 02:10:04 +02:00
}
2016-03-02 16:20:28 +02:00
l, err = listen(addr, fn)
2016-01-18 02:10:04 +02:00
} else {
2016-03-02 16:20:28 +02:00
fn := func(addr string) (net.Listener, error) {
return net.Listen("tcp", addr)
}
l, err = listen(addr, fn)
2016-01-18 02:10:04 +02:00
}
2015-05-20 23:57:19 +02:00
if err != nil {
return nil, err
}
return &httpTransportListener{
2016-07-28 20:12:56 +02:00
ht: h,
2015-05-20 23:57:19 +02:00
listener: l,
}, nil
}
2015-12-19 23:56:14 +02:00
func (h *httpTransport) String() string {
return "http"
}
2016-06-06 15:05:02 +02:00
func newHTTPTransport(opts ...Option) *httpTransport {
var options Options
for _, o := range opts {
o(&options)
}
return &httpTransport{opts: options}
}