diff options
author | Bas van Kervel <bas@ethdev.com> | 2016-02-24 18:19:00 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2016-03-23 18:27:08 +0800 |
commit | a7bae3b2a645653a149b9bcbb9bdc857e27027e2 (patch) | |
tree | 72a0014c4f4c3e6f64eabe6e7d541890b437246d /Godeps/_workspace/src/github.com/rs/xhandler/xhandler.go | |
parent | 6d3cd03a03167ccac851676a912ce31c76d5f75c (diff) | |
download | go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.tar go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.tar.gz go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.tar.bz2 go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.tar.lz go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.tar.xz go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.tar.zst go-tangerine-a7bae3b2a645653a149b9bcbb9bdc857e27027e2.zip |
rpc/http: improve request handling
Diffstat (limited to 'Godeps/_workspace/src/github.com/rs/xhandler/xhandler.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/rs/xhandler/xhandler.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/rs/xhandler/xhandler.go b/Godeps/_workspace/src/github.com/rs/xhandler/xhandler.go new file mode 100644 index 000000000..b71789804 --- /dev/null +++ b/Godeps/_workspace/src/github.com/rs/xhandler/xhandler.go @@ -0,0 +1,42 @@ +// Package xhandler provides a bridge between http.Handler and net/context. +// +// xhandler enforces net/context in your handlers without sacrificing +// compatibility with existing http.Handlers nor imposing a specific router. +// +// Thanks to net/context deadline management, xhandler is able to enforce +// a per request deadline and will cancel the context in when the client close +// the connection unexpectedly. +// +// You may create net/context aware middlewares pretty much the same way as +// you would do with http.Handler. +package xhandler + +import ( + "net/http" + + "github.com/ethereum/go-ethereum/Godeps/_workspace/src/golang.org/x/net/context" +) + +// HandlerC is a net/context aware http.Handler +type HandlerC interface { + ServeHTTPC(context.Context, http.ResponseWriter, *http.Request) +} + +// HandlerFuncC type is an adapter to allow the use of ordinary functions +// as a xhandler.Handler. If f is a function with the appropriate signature, +// xhandler.HandlerFuncC(f) is a xhandler.Handler object that calls f. +type HandlerFuncC func(context.Context, http.ResponseWriter, *http.Request) + +// ServeHTTPC calls f(ctx, w, r). +func (f HandlerFuncC) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) { + f(ctx, w, r) +} + +// New creates a conventional http.Handler injecting the provided root +// context to sub handlers. This handler is used as a bridge between conventional +// http.Handler and context aware handlers. +func New(ctx context.Context, h HandlerC) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTPC(ctx, w, r) + }) +} |