handler_server.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright 2016, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. // This file is the implementation of a gRPC server using HTTP/2 which
  33. // uses the standard Go http2 Server implementation (via the
  34. // http.Handler interface), rather than speaking low-level HTTP/2
  35. // frames itself. It is the implementation of *grpc.Server.ServeHTTP.
  36. package transport
  37. import (
  38. "errors"
  39. "fmt"
  40. "io"
  41. "net"
  42. "net/http"
  43. "strings"
  44. "sync"
  45. "time"
  46. "golang.org/x/net/context"
  47. "golang.org/x/net/http2"
  48. "google.golang.org/grpc/codes"
  49. "google.golang.org/grpc/credentials"
  50. "google.golang.org/grpc/metadata"
  51. "google.golang.org/grpc/peer"
  52. )
  53. // NewServerHandlerTransport returns a ServerTransport handling gRPC
  54. // from inside an http.Handler. It requires that the http Server
  55. // supports HTTP/2.
  56. func NewServerHandlerTransport(w http.ResponseWriter, r *http.Request) (ServerTransport, error) {
  57. if r.ProtoMajor != 2 {
  58. return nil, errors.New("gRPC requires HTTP/2")
  59. }
  60. if r.Method != "POST" {
  61. return nil, errors.New("invalid gRPC request method")
  62. }
  63. if !validContentType(r.Header.Get("Content-Type")) {
  64. return nil, errors.New("invalid gRPC request content-type")
  65. }
  66. if _, ok := w.(http.Flusher); !ok {
  67. return nil, errors.New("gRPC requires a ResponseWriter supporting http.Flusher")
  68. }
  69. if _, ok := w.(http.CloseNotifier); !ok {
  70. return nil, errors.New("gRPC requires a ResponseWriter supporting http.CloseNotifier")
  71. }
  72. st := &serverHandlerTransport{
  73. rw: w,
  74. req: r,
  75. closedCh: make(chan struct{}),
  76. writes: make(chan func()),
  77. }
  78. if v := r.Header.Get("grpc-timeout"); v != "" {
  79. to, err := decodeTimeout(v)
  80. if err != nil {
  81. return nil, streamErrorf(codes.Internal, "malformed time-out: %v", err)
  82. }
  83. st.timeoutSet = true
  84. st.timeout = to
  85. }
  86. var metakv []string
  87. if r.Host != "" {
  88. metakv = append(metakv, ":authority", r.Host)
  89. }
  90. for k, vv := range r.Header {
  91. k = strings.ToLower(k)
  92. if isReservedHeader(k) && !isWhitelistedPseudoHeader(k) {
  93. continue
  94. }
  95. for _, v := range vv {
  96. if k == "user-agent" {
  97. // user-agent is special. Copying logic of http_util.go.
  98. if i := strings.LastIndex(v, " "); i == -1 {
  99. // There is no application user agent string being set
  100. continue
  101. } else {
  102. v = v[:i]
  103. }
  104. }
  105. metakv = append(metakv, k, v)
  106. }
  107. }
  108. st.headerMD = metadata.Pairs(metakv...)
  109. return st, nil
  110. }
  111. // serverHandlerTransport is an implementation of ServerTransport
  112. // which replies to exactly one gRPC request (exactly one HTTP request),
  113. // using the net/http.Handler interface. This http.Handler is guaranteed
  114. // at this point to be speaking over HTTP/2, so it's able to speak valid
  115. // gRPC.
  116. type serverHandlerTransport struct {
  117. rw http.ResponseWriter
  118. req *http.Request
  119. timeoutSet bool
  120. timeout time.Duration
  121. didCommonHeaders bool
  122. headerMD metadata.MD
  123. closeOnce sync.Once
  124. closedCh chan struct{} // closed on Close
  125. // writes is a channel of code to run serialized in the
  126. // ServeHTTP (HandleStreams) goroutine. The channel is closed
  127. // when WriteStatus is called.
  128. writes chan func()
  129. }
  130. func (ht *serverHandlerTransport) Close() error {
  131. ht.closeOnce.Do(ht.closeCloseChanOnce)
  132. return nil
  133. }
  134. func (ht *serverHandlerTransport) closeCloseChanOnce() { close(ht.closedCh) }
  135. func (ht *serverHandlerTransport) RemoteAddr() net.Addr { return strAddr(ht.req.RemoteAddr) }
  136. // strAddr is a net.Addr backed by either a TCP "ip:port" string, or
  137. // the empty string if unknown.
  138. type strAddr string
  139. func (a strAddr) Network() string {
  140. if a != "" {
  141. // Per the documentation on net/http.Request.RemoteAddr, if this is
  142. // set, it's set to the IP:port of the peer (hence, TCP):
  143. // https://golang.org/pkg/net/http/#Request
  144. //
  145. // If we want to support Unix sockets later, we can
  146. // add our own grpc-specific convention within the
  147. // grpc codebase to set RemoteAddr to a different
  148. // format, or probably better: we can attach it to the
  149. // context and use that from serverHandlerTransport.RemoteAddr.
  150. return "tcp"
  151. }
  152. return ""
  153. }
  154. func (a strAddr) String() string { return string(a) }
  155. // do runs fn in the ServeHTTP goroutine.
  156. func (ht *serverHandlerTransport) do(fn func()) error {
  157. select {
  158. case ht.writes <- fn:
  159. return nil
  160. case <-ht.closedCh:
  161. return ErrConnClosing
  162. }
  163. }
  164. func (ht *serverHandlerTransport) WriteStatus(s *Stream, statusCode codes.Code, statusDesc string) error {
  165. err := ht.do(func() {
  166. ht.writeCommonHeaders(s)
  167. // And flush, in case no header or body has been sent yet.
  168. // This forces a separation of headers and trailers if this is the
  169. // first call (for example, in end2end tests's TestNoService).
  170. ht.rw.(http.Flusher).Flush()
  171. h := ht.rw.Header()
  172. h.Set("Grpc-Status", fmt.Sprintf("%d", statusCode))
  173. if statusDesc != "" {
  174. h.Set("Grpc-Message", encodeGrpcMessage(statusDesc))
  175. }
  176. if md := s.Trailer(); len(md) > 0 {
  177. for k, vv := range md {
  178. // Clients don't tolerate reading restricted headers after some non restricted ones were sent.
  179. if isReservedHeader(k) {
  180. continue
  181. }
  182. for _, v := range vv {
  183. // http2 ResponseWriter mechanism to
  184. // send undeclared Trailers after the
  185. // headers have possibly been written.
  186. h.Add(http2.TrailerPrefix+k, v)
  187. }
  188. }
  189. }
  190. })
  191. close(ht.writes)
  192. return err
  193. }
  194. // writeCommonHeaders sets common headers on the first write
  195. // call (Write, WriteHeader, or WriteStatus).
  196. func (ht *serverHandlerTransport) writeCommonHeaders(s *Stream) {
  197. if ht.didCommonHeaders {
  198. return
  199. }
  200. ht.didCommonHeaders = true
  201. h := ht.rw.Header()
  202. h["Date"] = nil // suppress Date to make tests happy; TODO: restore
  203. h.Set("Content-Type", "application/grpc")
  204. // Predeclare trailers we'll set later in WriteStatus (after the body).
  205. // This is a SHOULD in the HTTP RFC, and the way you add (known)
  206. // Trailers per the net/http.ResponseWriter contract.
  207. // See https://golang.org/pkg/net/http/#ResponseWriter
  208. // and https://golang.org/pkg/net/http/#example_ResponseWriter_trailers
  209. h.Add("Trailer", "Grpc-Status")
  210. h.Add("Trailer", "Grpc-Message")
  211. if s.sendCompress != "" {
  212. h.Set("Grpc-Encoding", s.sendCompress)
  213. }
  214. }
  215. func (ht *serverHandlerTransport) Write(s *Stream, data []byte, opts *Options) error {
  216. return ht.do(func() {
  217. ht.writeCommonHeaders(s)
  218. ht.rw.Write(data)
  219. if !opts.Delay {
  220. ht.rw.(http.Flusher).Flush()
  221. }
  222. })
  223. }
  224. func (ht *serverHandlerTransport) WriteHeader(s *Stream, md metadata.MD) error {
  225. return ht.do(func() {
  226. ht.writeCommonHeaders(s)
  227. h := ht.rw.Header()
  228. for k, vv := range md {
  229. // Clients don't tolerate reading restricted headers after some non restricted ones were sent.
  230. if isReservedHeader(k) {
  231. continue
  232. }
  233. for _, v := range vv {
  234. h.Add(k, v)
  235. }
  236. }
  237. ht.rw.WriteHeader(200)
  238. ht.rw.(http.Flusher).Flush()
  239. })
  240. }
  241. func (ht *serverHandlerTransport) HandleStreams(startStream func(*Stream)) {
  242. // With this transport type there will be exactly 1 stream: this HTTP request.
  243. var ctx context.Context
  244. var cancel context.CancelFunc
  245. if ht.timeoutSet {
  246. ctx, cancel = context.WithTimeout(context.Background(), ht.timeout)
  247. } else {
  248. ctx, cancel = context.WithCancel(context.Background())
  249. }
  250. // requestOver is closed when either the request's context is done
  251. // or the status has been written via WriteStatus.
  252. requestOver := make(chan struct{})
  253. // clientGone receives a single value if peer is gone, either
  254. // because the underlying connection is dead or because the
  255. // peer sends an http2 RST_STREAM.
  256. clientGone := ht.rw.(http.CloseNotifier).CloseNotify()
  257. go func() {
  258. select {
  259. case <-requestOver:
  260. return
  261. case <-ht.closedCh:
  262. case <-clientGone:
  263. }
  264. cancel()
  265. }()
  266. req := ht.req
  267. s := &Stream{
  268. id: 0, // irrelevant
  269. windowHandler: func(int) {}, // nothing
  270. cancel: cancel,
  271. buf: newRecvBuffer(),
  272. st: ht,
  273. method: req.URL.Path,
  274. recvCompress: req.Header.Get("grpc-encoding"),
  275. }
  276. pr := &peer.Peer{
  277. Addr: ht.RemoteAddr(),
  278. }
  279. if req.TLS != nil {
  280. pr.AuthInfo = credentials.TLSInfo{State: *req.TLS}
  281. }
  282. ctx = metadata.NewContext(ctx, ht.headerMD)
  283. ctx = peer.NewContext(ctx, pr)
  284. s.ctx = newContextWithStream(ctx, s)
  285. s.dec = &recvBufferReader{ctx: s.ctx, recv: s.buf}
  286. // readerDone is closed when the Body.Read-ing goroutine exits.
  287. readerDone := make(chan struct{})
  288. go func() {
  289. defer close(readerDone)
  290. // TODO: minimize garbage, optimize recvBuffer code/ownership
  291. const readSize = 8196
  292. for buf := make([]byte, readSize); ; {
  293. n, err := req.Body.Read(buf)
  294. if n > 0 {
  295. s.buf.put(&recvMsg{data: buf[:n:n]})
  296. buf = buf[n:]
  297. }
  298. if err != nil {
  299. s.buf.put(&recvMsg{err: mapRecvMsgError(err)})
  300. return
  301. }
  302. if len(buf) == 0 {
  303. buf = make([]byte, readSize)
  304. }
  305. }
  306. }()
  307. // startStream is provided by the *grpc.Server's serveStreams.
  308. // It starts a goroutine serving s and exits immediately.
  309. // The goroutine that is started is the one that then calls
  310. // into ht, calling WriteHeader, Write, WriteStatus, Close, etc.
  311. startStream(s)
  312. ht.runStream()
  313. close(requestOver)
  314. // Wait for reading goroutine to finish.
  315. req.Body.Close()
  316. <-readerDone
  317. }
  318. func (ht *serverHandlerTransport) runStream() {
  319. for {
  320. select {
  321. case fn, ok := <-ht.writes:
  322. if !ok {
  323. return
  324. }
  325. fn()
  326. case <-ht.closedCh:
  327. return
  328. }
  329. }
  330. }
  331. func (ht *serverHandlerTransport) Drain() {
  332. panic("Drain() is not implemented")
  333. }
  334. // mapRecvMsgError returns the non-nil err into the appropriate
  335. // error value as expected by callers of *grpc.parser.recvMsg.
  336. // In particular, in can only be:
  337. // * io.EOF
  338. // * io.ErrUnexpectedEOF
  339. // * of type transport.ConnectionError
  340. // * of type transport.StreamError
  341. func mapRecvMsgError(err error) error {
  342. if err == io.EOF || err == io.ErrUnexpectedEOF {
  343. return err
  344. }
  345. if se, ok := err.(http2.StreamError); ok {
  346. if code, ok := http2ErrConvTab[se.Code]; ok {
  347. return StreamError{
  348. Code: code,
  349. Desc: se.Error(),
  350. }
  351. }
  352. }
  353. return connectionErrorf(true, err, err.Error())
  354. }