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"
|
2016-01-18 03:10:04 +03:00
|
|
|
"crypto/tls"
|
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"
|
2016-04-01 15:10:18 +03:00
|
|
|
"time"
|
2016-01-18 03:10:04 +03:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
maddr "github.com/micro/go-micro/v2/util/addr"
|
|
|
|
"github.com/micro/go-micro/v2/util/buf"
|
|
|
|
mnet "github.com/micro/go-micro/v2/util/net"
|
|
|
|
mls "github.com/micro/go-micro/v2/util/tls"
|
2018-07-29 12:55:46 +03:00
|
|
|
"golang.org/x/net/http2"
|
2019-01-31 20:14:36 +03:00
|
|
|
"golang.org/x/net/http2/h2c"
|
2015-05-21 00:57:19 +03:00
|
|
|
)
|
|
|
|
|
2016-01-18 03:10:04 +03:00
|
|
|
type httpTransport struct {
|
|
|
|
opts Options
|
|
|
|
}
|
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
|
|
|
|
2019-01-30 21:42:11 +03:00
|
|
|
sync.RWMutex
|
2019-08-24 22:12:04 +03:00
|
|
|
|
|
|
|
// request must be stored for response processing
|
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
|
2018-11-14 22:41:13 +03:00
|
|
|
|
|
|
|
// local/remote ip
|
|
|
|
local string
|
|
|
|
remote string
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportSocket struct {
|
2018-07-29 12:55:46 +03:00
|
|
|
ht *httpTransport
|
|
|
|
w http.ResponseWriter
|
|
|
|
r *http.Request
|
|
|
|
rw *bufio.ReadWriter
|
2015-12-18 23:28:50 +03:00
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
mtx sync.RWMutex
|
|
|
|
|
|
|
|
// the hijacked when using http 1
|
2018-07-29 12:55:46 +03:00
|
|
|
conn net.Conn
|
|
|
|
// for the first request
|
|
|
|
ch chan *http.Request
|
2018-11-14 22:41:13 +03:00
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
// h2 things
|
|
|
|
buf *bufio.Reader
|
|
|
|
// indicate if socket is closed
|
|
|
|
closed chan bool
|
|
|
|
|
2018-11-14 22:41:13 +03:00
|
|
|
// local/remote ip
|
|
|
|
local string
|
|
|
|
remote string
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
type httpTransportListener struct {
|
2016-07-28 21:12:56 +03:00
|
|
|
ht *httpTransport
|
2015-05-21 00:57:19 +03:00
|
|
|
listener net.Listener
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:41:13 +03:00
|
|
|
func (h *httpTransportClient) Local() string {
|
|
|
|
return h.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpTransportClient) Remote() string {
|
|
|
|
return h.remote
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:33:24 +03:00
|
|
|
b := buf.New(bytes.NewBuffer(m.Body))
|
|
|
|
defer b.Close()
|
2015-05-21 00:57:19 +03:00
|
|
|
|
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,
|
2019-07-28 21:33:24 +03:00
|
|
|
Body: b,
|
|
|
|
ContentLength: int64(b.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
|
|
|
|
2016-08-01 18:31:27 +03: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 21:12:56 +03:00
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
return req.Write(h.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpTransportClient) Recv(m *Message) error {
|
2016-10-29 23:33:04 +03:00
|
|
|
if m == nil {
|
|
|
|
return errors.New("message passed in is nil")
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-01 18:31:27 +03: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 21:12:56 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-06 17:44:32 +03:00
|
|
|
if rsp.StatusCode != 200 {
|
|
|
|
return errors.New(rsp.Status + ": " + string(b))
|
|
|
|
}
|
|
|
|
|
2016-10-29 23:33:04 +03:00
|
|
|
m.Body = b
|
|
|
|
|
|
|
|
if m.Header == nil {
|
2020-02-24 17:15:20 +03:00
|
|
|
m.Header = make(map[string]string, len(rsp.Header))
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range rsp.Header {
|
|
|
|
if len(v) > 0 {
|
2016-10-29 23:33:04 +03:00
|
|
|
m.Header[k] = v[0]
|
2015-05-21 00:57:19 +03:00
|
|
|
} else {
|
2016-10-29 23:33:04 +03:00
|
|
|
m.Header[k] = ""
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
return nil
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportClient) Close() error {
|
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.Unlock()
|
2015-10-22 17:14:56 +03:00
|
|
|
close(h.r)
|
|
|
|
})
|
2019-08-24 22:12:04 +03:00
|
|
|
return h.conn.Close()
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2018-11-14 22:41:13 +03:00
|
|
|
func (h *httpTransportSocket) Local() string {
|
|
|
|
return h.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpTransportSocket) Remote() string {
|
|
|
|
return h.remote
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
2020-03-02 19:17:26 +03:00
|
|
|
if m.Header == nil {
|
|
|
|
m.Header = make(map[string]string, len(h.r.Header))
|
|
|
|
}
|
2015-05-21 23:08:19 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// process http 1
|
|
|
|
if h.r.ProtoMajor == 1 {
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
var r *http.Request
|
|
|
|
|
|
|
|
select {
|
|
|
|
// get first request
|
|
|
|
case r = <-h.ch:
|
|
|
|
// read next request
|
|
|
|
default:
|
|
|
|
rr, err := http.ReadRequest(h.rw.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r = rr
|
|
|
|
}
|
|
|
|
|
|
|
|
// read body
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set body
|
|
|
|
r.Body.Close()
|
|
|
|
m.Body = b
|
|
|
|
|
|
|
|
// set headers
|
2018-11-22 13:39:36 +03:00
|
|
|
for k, v := range r.Header {
|
2018-07-29 12:55:46 +03:00
|
|
|
if len(v) > 0 {
|
|
|
|
m.Header[k] = v[0]
|
|
|
|
} else {
|
|
|
|
m.Header[k] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return early early
|
|
|
|
return nil
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
2015-12-18 23:28:50 +03:00
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
// only process if the socket is open
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return io.EOF
|
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// processing http2 request
|
|
|
|
// read streaming body
|
|
|
|
|
|
|
|
// set max buffer size
|
2019-08-24 22:12:04 +03:00
|
|
|
// TODO: adjustable buffer size
|
|
|
|
buf := make([]byte, 4*1024*1024)
|
2018-07-29 12:55:46 +03:00
|
|
|
|
|
|
|
// read the request body
|
2019-08-24 22:12:04 +03:00
|
|
|
n, err := h.buf.Read(buf)
|
2018-07-29 12:55:46 +03:00
|
|
|
// not an eof error
|
2015-12-18 23:28:50 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// check if we have data
|
|
|
|
if n > 0 {
|
|
|
|
m.Body = buf[:n]
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// set headers
|
|
|
|
for k, v := range h.r.Header {
|
2015-05-21 00:57:19 +03:00
|
|
|
if len(v) > 0 {
|
2016-10-29 23:33:04 +03:00
|
|
|
m.Header[k] = v[0]
|
2015-05-21 00:57:19 +03:00
|
|
|
} else {
|
2016-10-29 23:33:04 +03:00
|
|
|
m.Header[k] = ""
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 15:55:06 +03:00
|
|
|
// set path
|
|
|
|
m.Header[":path"] = h.r.URL.Path
|
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
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 {
|
2018-07-29 12:55:46 +03:00
|
|
|
if h.r.ProtoMajor == 1 {
|
2019-11-27 20:12:07 +03:00
|
|
|
// make copy of header
|
|
|
|
hdr := make(http.Header)
|
|
|
|
for k, v := range h.r.Header {
|
|
|
|
hdr[k] = v
|
|
|
|
}
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
rsp := &http.Response{
|
2019-11-27 20:12:07 +03:00
|
|
|
Header: hdr,
|
2018-07-29 12:55:46 +03:00
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(m.Body)),
|
|
|
|
Status: "200 OK",
|
|
|
|
StatusCode: 200,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
ContentLength: int64(len(m.Body)),
|
|
|
|
}
|
2015-06-01 20:55:27 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
for k, v := range m.Header {
|
|
|
|
rsp.Header.Set(k, v)
|
|
|
|
}
|
2015-05-21 00:57:19 +03:00
|
|
|
|
2018-07-29 12:55:46 +03: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))
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsp.Write(h.conn)
|
2015-12-18 23:28:50 +03:00
|
|
|
}
|
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
// only process if the socket is open
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return io.EOF
|
|
|
|
default:
|
|
|
|
// no op
|
|
|
|
}
|
2018-07-29 12:55:46 +03:00
|
|
|
|
2019-09-11 04:26:12 +03:00
|
|
|
// we need to lock to protect the write
|
|
|
|
h.mtx.RLock()
|
|
|
|
defer h.mtx.RUnlock()
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// set headers
|
|
|
|
for k, v := range m.Header {
|
|
|
|
h.w.Header().Set(k, v)
|
2016-07-28 21:12:56 +03:00
|
|
|
}
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// write request
|
|
|
|
_, err := h.w.Write(m.Body)
|
2019-08-24 22:12:04 +03:00
|
|
|
|
2019-08-25 21:30:22 +03:00
|
|
|
// flush the trailers
|
|
|
|
h.w.(http.Flusher).Flush()
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
return err
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-11-24 03:52:59 +03:00
|
|
|
func (h *httpTransportSocket) error(m *Message) error {
|
2018-07-29 12:55:46 +03:00
|
|
|
if h.r.ProtoMajor == 1 {
|
|
|
|
rsp := &http.Response{
|
|
|
|
Header: make(http.Header),
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader(m.Body)),
|
|
|
|
Status: "500 Internal Server Error",
|
|
|
|
StatusCode: 500,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 1,
|
|
|
|
ContentLength: int64(len(m.Body)),
|
|
|
|
}
|
2015-11-24 03:52:59 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
for k, v := range m.Header {
|
|
|
|
rsp.Header.Set(k, v)
|
|
|
|
}
|
2015-11-24 03:52:59 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
return rsp.Write(h.conn)
|
|
|
|
}
|
2019-08-24 22:12:04 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
return nil
|
2015-11-24 03:52:59 +03:00
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func (h *httpTransportSocket) Close() error {
|
2019-08-24 22:12:04 +03:00
|
|
|
h.mtx.Lock()
|
|
|
|
defer h.mtx.Unlock()
|
|
|
|
select {
|
|
|
|
case <-h.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
// close the channel
|
|
|
|
close(h.closed)
|
|
|
|
|
2019-08-26 14:33:59 +03:00
|
|
|
// close the buffer
|
|
|
|
h.r.Body.Close()
|
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
// close the connection
|
|
|
|
if h.r.ProtoMajor == 1 {
|
|
|
|
return h.conn.Close()
|
|
|
|
}
|
2018-07-29 12:55:46 +03:00
|
|
|
}
|
2019-08-24 22:12:04 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
return nil
|
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 {
|
2018-07-29 12:55:46 +03:00
|
|
|
// create handler mux
|
|
|
|
mux := http.NewServeMux()
|
2018-11-18 19:32:53 +03:00
|
|
|
|
|
|
|
// register our transport handler
|
2018-07-29 12:55:46 +03:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var buf *bufio.ReadWriter
|
|
|
|
var con net.Conn
|
|
|
|
|
|
|
|
// read a regular request
|
|
|
|
if r.ProtoMajor == 1 {
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.Body = ioutil.NopCloser(bytes.NewReader(b))
|
|
|
|
// hijack the conn
|
|
|
|
hj, ok := w.(http.Hijacker)
|
|
|
|
if !ok {
|
|
|
|
// we're screwed
|
|
|
|
http.Error(w, "cannot serve conn", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2016-04-06 20:03:27 +03:00
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
conn, bufrw, err := hj.Hijack()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2016-04-01 15:10:18 +03:00
|
|
|
}
|
2018-07-29 12:55:46 +03:00
|
|
|
defer conn.Close()
|
|
|
|
buf = bufrw
|
|
|
|
con = conn
|
2015-12-18 23:28:50 +03:00
|
|
|
}
|
2015-11-24 03:52:59 +03:00
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
// buffered reader
|
|
|
|
bufr := bufio.NewReader(r.Body)
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// save the request
|
|
|
|
ch := make(chan *http.Request, 1)
|
|
|
|
ch <- r
|
|
|
|
|
2019-08-24 22:12:04 +03:00
|
|
|
// create a new transport socket
|
|
|
|
sock := &httpTransportSocket{
|
2018-11-14 22:41:13 +03:00
|
|
|
ht: h.ht,
|
|
|
|
w: w,
|
|
|
|
r: r,
|
|
|
|
rw: buf,
|
2019-08-24 22:12:04 +03:00
|
|
|
buf: bufr,
|
2018-11-14 22:41:13 +03:00
|
|
|
ch: ch,
|
|
|
|
conn: con,
|
|
|
|
local: h.Addr(),
|
|
|
|
remote: r.RemoteAddr,
|
2019-08-24 22:12:04 +03:00
|
|
|
closed: make(chan bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the socket
|
|
|
|
fn(sock)
|
2018-07-29 12:55:46 +03:00
|
|
|
})
|
2015-12-18 23:28:50 +03:00
|
|
|
|
2018-11-18 19:32:53 +03:00
|
|
|
// get optional handlers
|
|
|
|
if h.ht.opts.Context != nil {
|
|
|
|
handlers, ok := h.ht.opts.Context.Value("http_handlers").(map[string]http.Handler)
|
|
|
|
if ok {
|
|
|
|
for pattern, handler := range handlers {
|
|
|
|
mux.Handle(pattern, handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 12:55:46 +03:00
|
|
|
// default http2 server
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: mux,
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
2018-07-29 12:55:46 +03:00
|
|
|
|
|
|
|
// insecure connection use h2c
|
|
|
|
if !(h.ht.opts.Secure || h.ht.opts.TLSConfig != nil) {
|
2019-01-31 20:14:36 +03:00
|
|
|
srv.Handler = h2c.NewHandler(mux, &http2.Server{})
|
2018-07-29 12:55:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// begin serving
|
|
|
|
return srv.Serve(h.listener)
|
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) {
|
2016-01-04 00:25:03 +03:00
|
|
|
dopts := DialOptions{
|
|
|
|
Timeout: DefaultDialTimeout,
|
2015-06-01 20:55:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&dopts)
|
|
|
|
}
|
|
|
|
|
2016-01-18 03:10:04 +03: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,
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 19:05:31 +03:00
|
|
|
config.NextProtos = []string{"http/1.1"}
|
2019-01-02 18:24:17 +03:00
|
|
|
conn, err = newConn(func(addr string) (net.Conn, error) {
|
|
|
|
return tls.DialWithDialer(&net.Dialer{Timeout: dopts.Timeout}, "tcp", addr, config)
|
|
|
|
})(addr)
|
2016-01-18 03:10:04 +03:00
|
|
|
} else {
|
2019-01-02 18:24:17 +03:00
|
|
|
conn, err = newConn(func(addr string) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("tcp", addr, dopts.Timeout)
|
|
|
|
})(addr)
|
2016-01-18 03:10:04 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 00:25:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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),
|
2018-11-14 22:41:13 +03:00
|
|
|
local: conn.LocalAddr().String(),
|
|
|
|
remote: conn.RemoteAddr().String(),
|
2015-05-21 00:57:19 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-18 03:10:04 +03: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 17:20:28 +03:00
|
|
|
|
|
|
|
fn := func(addr string) (net.Listener, error) {
|
|
|
|
if config == nil {
|
2016-06-05 18:13:29 +03:00
|
|
|
hosts := []string{addr}
|
2016-06-06 16:05:02 +03:00
|
|
|
|
|
|
|
// check if its a valid host:port
|
|
|
|
if host, _, err := net.SplitHostPort(addr); err == nil {
|
|
|
|
if len(host) == 0 {
|
2017-01-12 16:52:49 +03:00
|
|
|
hosts = maddr.IPs()
|
2016-06-05 18:13:29 +03:00
|
|
|
} else {
|
2016-06-06 16:05:02 +03:00
|
|
|
hosts = []string{host}
|
2016-06-05 18:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:05:02 +03:00
|
|
|
// generate a certificate
|
2016-06-05 18:13:29 +03:00
|
|
|
cert, err := mls.Certificate(hosts...)
|
2016-03-02 17:20:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config = &tls.Config{Certificates: []tls.Certificate{cert}}
|
2016-01-18 03:10:04 +03:00
|
|
|
}
|
2016-03-02 17:20:28 +03:00
|
|
|
return tls.Listen("tcp", addr, config)
|
2016-01-18 03:10:04 +03:00
|
|
|
}
|
2016-03-02 17:20:28 +03:00
|
|
|
|
2017-01-12 17:11:25 +03:00
|
|
|
l, err = mnet.Listen(addr, fn)
|
2016-01-18 03:10:04 +03:00
|
|
|
} else {
|
2016-03-02 17:20:28 +03:00
|
|
|
fn := func(addr string) (net.Listener, error) {
|
|
|
|
return net.Listen("tcp", addr)
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:11:25 +03:00
|
|
|
l, err = mnet.Listen(addr, fn)
|
2016-01-18 03:10:04 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 00:57:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
return &httpTransportListener{
|
2016-07-28 21:12:56 +03:00
|
|
|
ht: h,
|
2015-05-21 00:57:19 +03:00
|
|
|
listener: l,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-08-08 20:57:29 +03:00
|
|
|
func (h *httpTransport) Init(opts ...Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&h.opts)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpTransport) Options() Options {
|
|
|
|
return h.opts
|
|
|
|
}
|
|
|
|
|
2015-12-20 00:56:14 +03:00
|
|
|
func (h *httpTransport) String() string {
|
|
|
|
return "http"
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:05:02 +03:00
|
|
|
func newHTTPTransport(opts ...Option) *httpTransport {
|
|
|
|
var options Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return &httpTransport{opts: options}
|
|
|
|
}
|