2018-05-27 07:59:25 +00:00
|
|
|
// Package mhttp extends the standard package with extra functionality which is
|
|
|
|
// commonly useful
|
|
|
|
package mhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-09 19:20:16 +00:00
|
|
|
"net"
|
2018-05-27 07:59:25 +00:00
|
|
|
"net/http"
|
2018-08-09 20:02:11 +00:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2018-08-09 19:20:16 +00:00
|
|
|
"strings"
|
2018-05-27 07:59:25 +00:00
|
|
|
|
2019-01-25 00:23:04 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
2018-05-27 07:59:25 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
2018-08-09 19:20:16 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mnet"
|
2019-01-25 00:23:04 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
2018-05-27 07:59:25 +00:00
|
|
|
)
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// Server is returned by WithListeningServer and simply wraps an *http.Server.
|
|
|
|
type Server struct {
|
2019-02-05 20:18:17 +00:00
|
|
|
*http.Server
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
// WithListeningServer returns a *Server which will be initialized and have
|
2019-01-25 00:23:04 +00:00
|
|
|
// ListenAndServe called on it (asynchronously) when the start event is
|
2019-02-05 20:18:17 +00:00
|
|
|
// triggered on the returned Context (see mrun.Start). The Server will have
|
|
|
|
// Shutdown called on it when the stop event is triggered on the returned
|
|
|
|
// Context (see mrun.Stop).
|
2019-01-25 00:23:04 +00:00
|
|
|
//
|
|
|
|
// This function automatically handles setting up configuration parameters via
|
|
|
|
// mcfg. The default listen address is ":0".
|
2019-02-09 19:08:30 +00:00
|
|
|
func WithListeningServer(ctx context.Context, h http.Handler) (context.Context, *Server) {
|
|
|
|
srv := &Server{
|
2019-02-05 20:18:17 +00:00
|
|
|
Server: &http.Server{Handler: h},
|
|
|
|
ctx: mctx.NewChild(ctx, "http"),
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
var listener *mnet.Listener
|
2019-03-01 19:06:25 +00:00
|
|
|
srv.ctx, listener = mnet.WithListener(srv.ctx)
|
2019-01-25 00:23:04 +00:00
|
|
|
listener.NoCloseOnStop = true // http.Server.Shutdown will do this
|
2019-01-30 21:06:24 +00:00
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
srv.ctx = mrun.WithStartHook(srv.ctx, func(context.Context) error {
|
2019-01-25 00:23:04 +00:00
|
|
|
srv.Addr = listener.Addr().String()
|
2019-02-24 22:38:05 +00:00
|
|
|
srv.ctx = mrun.WithThreads(srv.ctx, 1, func() error {
|
2019-02-09 19:08:30 +00:00
|
|
|
mlog.Info("serving requests", srv.ctx)
|
|
|
|
if err := srv.Serve(listener); !merr.Equal(err, http.ErrServerClosed) {
|
|
|
|
mlog.Error("error serving listener", srv.ctx, merr.Context(err))
|
2019-01-25 00:23:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-05-27 07:59:25 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
srv.ctx = mrun.WithStopHook(srv.ctx, func(innerCtx context.Context) error {
|
|
|
|
mlog.Info("shutting down server", srv.ctx)
|
2019-02-05 20:18:17 +00:00
|
|
|
if err := srv.Shutdown(innerCtx); err != nil {
|
2019-01-25 00:23:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-02-05 20:18:17 +00:00
|
|
|
return mrun.Wait(srv.ctx, innerCtx.Done())
|
2019-01-25 00:23:04 +00:00
|
|
|
})
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
return mctx.WithChild(ctx, srv.ctx), srv
|
2018-05-27 07:59:25 +00:00
|
|
|
}
|
2018-08-09 19:20:16 +00:00
|
|
|
|
|
|
|
// AddXForwardedFor populates the X-Forwarded-For header on the Request to
|
|
|
|
// convey that the request is being proxied for IP.
|
|
|
|
//
|
|
|
|
// If the IP is invalid, loopback, or otherwise part of a reserved range, this
|
|
|
|
// does nothing.
|
|
|
|
func AddXForwardedFor(r *http.Request, ipStr string) {
|
|
|
|
const xff = "X-Forwarded-For"
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
if ip == nil || mnet.IsReservedIP(ip) { // IsReservedIP includes loopback
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prev, _ := r.Header[xff]
|
|
|
|
r.Header.Set(xff, strings.Join(append(prev, ip.String()), ", "))
|
|
|
|
}
|
2018-08-09 20:02:11 +00:00
|
|
|
|
|
|
|
// ReverseProxy returns an httputil.ReverseProxy which will send requests to the
|
|
|
|
// given URL and copy their responses back without modification.
|
|
|
|
//
|
|
|
|
// Only the Scheme and Host of the given URL are used.
|
|
|
|
//
|
|
|
|
// Any http.ResponseWriters passed into the ServeHTTP call of the returned
|
|
|
|
// instance should not be modified afterwards.
|
|
|
|
func ReverseProxy(u *url.URL) *httputil.ReverseProxy {
|
|
|
|
rp := new(httputil.ReverseProxy)
|
|
|
|
rp.Director = func(req *http.Request) {
|
|
|
|
if ipStr, _, err := net.SplitHostPort(req.RemoteAddr); err != nil {
|
|
|
|
AddXForwardedFor(req, ipStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.URL.Scheme = u.Scheme
|
|
|
|
req.URL.Host = u.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO when this package has a function for creating a Client use that for
|
|
|
|
// the default here
|
|
|
|
|
|
|
|
return rp
|
|
|
|
}
|