http_util.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. *
  3. * Copyright 2014 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package transport
  19. import (
  20. "bufio"
  21. "bytes"
  22. "encoding/base64"
  23. "fmt"
  24. "io"
  25. "net"
  26. "net/http"
  27. "strconv"
  28. "strings"
  29. "time"
  30. "unicode/utf8"
  31. "github.com/golang/protobuf/proto"
  32. "golang.org/x/net/http2"
  33. "golang.org/x/net/http2/hpack"
  34. spb "google.golang.org/genproto/googleapis/rpc/status"
  35. "google.golang.org/grpc/codes"
  36. "google.golang.org/grpc/status"
  37. )
  38. const (
  39. // http2MaxFrameLen specifies the max length of a HTTP2 frame.
  40. http2MaxFrameLen = 16384 // 16KB frame
  41. // http://http2.github.io/http2-spec/#SettingValues
  42. http2InitHeaderTableSize = 4096
  43. // baseContentType is the base content-type for gRPC. This is a valid
  44. // content-type on it's own, but can also include a content-subtype such as
  45. // "proto" as a suffix after "+" or ";". See
  46. // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests
  47. // for more details.
  48. baseContentType = "application/grpc"
  49. )
  50. var (
  51. clientPreface = []byte(http2.ClientPreface)
  52. http2ErrConvTab = map[http2.ErrCode]codes.Code{
  53. http2.ErrCodeNo: codes.Internal,
  54. http2.ErrCodeProtocol: codes.Internal,
  55. http2.ErrCodeInternal: codes.Internal,
  56. http2.ErrCodeFlowControl: codes.ResourceExhausted,
  57. http2.ErrCodeSettingsTimeout: codes.Internal,
  58. http2.ErrCodeStreamClosed: codes.Internal,
  59. http2.ErrCodeFrameSize: codes.Internal,
  60. http2.ErrCodeRefusedStream: codes.Unavailable,
  61. http2.ErrCodeCancel: codes.Canceled,
  62. http2.ErrCodeCompression: codes.Internal,
  63. http2.ErrCodeConnect: codes.Internal,
  64. http2.ErrCodeEnhanceYourCalm: codes.ResourceExhausted,
  65. http2.ErrCodeInadequateSecurity: codes.PermissionDenied,
  66. http2.ErrCodeHTTP11Required: codes.Internal,
  67. }
  68. statusCodeConvTab = map[codes.Code]http2.ErrCode{
  69. codes.Internal: http2.ErrCodeInternal,
  70. codes.Canceled: http2.ErrCodeCancel,
  71. codes.Unavailable: http2.ErrCodeRefusedStream,
  72. codes.ResourceExhausted: http2.ErrCodeEnhanceYourCalm,
  73. codes.PermissionDenied: http2.ErrCodeInadequateSecurity,
  74. }
  75. httpStatusConvTab = map[int]codes.Code{
  76. // 400 Bad Request - INTERNAL.
  77. http.StatusBadRequest: codes.Internal,
  78. // 401 Unauthorized - UNAUTHENTICATED.
  79. http.StatusUnauthorized: codes.Unauthenticated,
  80. // 403 Forbidden - PERMISSION_DENIED.
  81. http.StatusForbidden: codes.PermissionDenied,
  82. // 404 Not Found - UNIMPLEMENTED.
  83. http.StatusNotFound: codes.Unimplemented,
  84. // 429 Too Many Requests - UNAVAILABLE.
  85. http.StatusTooManyRequests: codes.Unavailable,
  86. // 502 Bad Gateway - UNAVAILABLE.
  87. http.StatusBadGateway: codes.Unavailable,
  88. // 503 Service Unavailable - UNAVAILABLE.
  89. http.StatusServiceUnavailable: codes.Unavailable,
  90. // 504 Gateway timeout - UNAVAILABLE.
  91. http.StatusGatewayTimeout: codes.Unavailable,
  92. }
  93. )
  94. // Records the states during HPACK decoding. Must be reset once the
  95. // decoding of the entire headers are finished.
  96. type decodeState struct {
  97. encoding string
  98. // statusGen caches the stream status received from the trailer the server
  99. // sent. Client side only. Do not access directly. After all trailers are
  100. // parsed, use the status method to retrieve the status.
  101. statusGen *status.Status
  102. // rawStatusCode and rawStatusMsg are set from the raw trailer fields and are not
  103. // intended for direct access outside of parsing.
  104. rawStatusCode *int
  105. rawStatusMsg string
  106. httpStatus *int
  107. // Server side only fields.
  108. timeoutSet bool
  109. timeout time.Duration
  110. method string
  111. // key-value metadata map from the peer.
  112. mdata map[string][]string
  113. statsTags []byte
  114. statsTrace []byte
  115. contentSubtype string
  116. // whether decoding on server side or not
  117. serverSide bool
  118. }
  119. // isReservedHeader checks whether hdr belongs to HTTP2 headers
  120. // reserved by gRPC protocol. Any other headers are classified as the
  121. // user-specified metadata.
  122. func isReservedHeader(hdr string) bool {
  123. if hdr != "" && hdr[0] == ':' {
  124. return true
  125. }
  126. switch hdr {
  127. case "content-type",
  128. "user-agent",
  129. "grpc-message-type",
  130. "grpc-encoding",
  131. "grpc-message",
  132. "grpc-status",
  133. "grpc-timeout",
  134. "grpc-status-details-bin",
  135. // Intentionally exclude grpc-previous-rpc-attempts and
  136. // grpc-retry-pushback-ms, which are "reserved", but their API
  137. // intentionally works via metadata.
  138. "te":
  139. return true
  140. default:
  141. return false
  142. }
  143. }
  144. // isWhitelistedHeader checks whether hdr should be propagated into metadata
  145. // visible to users, even though it is classified as "reserved", above.
  146. func isWhitelistedHeader(hdr string) bool {
  147. switch hdr {
  148. case ":authority", "user-agent":
  149. return true
  150. default:
  151. return false
  152. }
  153. }
  154. // contentSubtype returns the content-subtype for the given content-type. The
  155. // given content-type must be a valid content-type that starts with
  156. // "application/grpc". A content-subtype will follow "application/grpc" after a
  157. // "+" or ";". See
  158. // https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests for
  159. // more details.
  160. //
  161. // If contentType is not a valid content-type for gRPC, the boolean
  162. // will be false, otherwise true. If content-type == "application/grpc",
  163. // "application/grpc+", or "application/grpc;", the boolean will be true,
  164. // but no content-subtype will be returned.
  165. //
  166. // contentType is assumed to be lowercase already.
  167. func contentSubtype(contentType string) (string, bool) {
  168. if contentType == baseContentType {
  169. return "", true
  170. }
  171. if !strings.HasPrefix(contentType, baseContentType) {
  172. return "", false
  173. }
  174. // guaranteed since != baseContentType and has baseContentType prefix
  175. switch contentType[len(baseContentType)] {
  176. case '+', ';':
  177. // this will return true for "application/grpc+" or "application/grpc;"
  178. // which the previous validContentType function tested to be valid, so we
  179. // just say that no content-subtype is specified in this case
  180. return contentType[len(baseContentType)+1:], true
  181. default:
  182. return "", false
  183. }
  184. }
  185. // contentSubtype is assumed to be lowercase
  186. func contentType(contentSubtype string) string {
  187. if contentSubtype == "" {
  188. return baseContentType
  189. }
  190. return baseContentType + "+" + contentSubtype
  191. }
  192. func (d *decodeState) status() *status.Status {
  193. if d.statusGen == nil {
  194. // No status-details were provided; generate status using code/msg.
  195. d.statusGen = status.New(codes.Code(int32(*(d.rawStatusCode))), d.rawStatusMsg)
  196. }
  197. return d.statusGen
  198. }
  199. const binHdrSuffix = "-bin"
  200. func encodeBinHeader(v []byte) string {
  201. return base64.RawStdEncoding.EncodeToString(v)
  202. }
  203. func decodeBinHeader(v string) ([]byte, error) {
  204. if len(v)%4 == 0 {
  205. // Input was padded, or padding was not necessary.
  206. return base64.StdEncoding.DecodeString(v)
  207. }
  208. return base64.RawStdEncoding.DecodeString(v)
  209. }
  210. func encodeMetadataHeader(k, v string) string {
  211. if strings.HasSuffix(k, binHdrSuffix) {
  212. return encodeBinHeader(([]byte)(v))
  213. }
  214. return v
  215. }
  216. func decodeMetadataHeader(k, v string) (string, error) {
  217. if strings.HasSuffix(k, binHdrSuffix) {
  218. b, err := decodeBinHeader(v)
  219. return string(b), err
  220. }
  221. return v, nil
  222. }
  223. func (d *decodeState) decodeHeader(frame *http2.MetaHeadersFrame) error {
  224. // frame.Truncated is set to true when framer detects that the current header
  225. // list size hits MaxHeaderListSize limit.
  226. if frame.Truncated {
  227. return streamErrorf(codes.Internal, "peer header list size exceeded limit")
  228. }
  229. for _, hf := range frame.Fields {
  230. if err := d.processHeaderField(hf); err != nil {
  231. return err
  232. }
  233. }
  234. if d.serverSide {
  235. return nil
  236. }
  237. // If grpc status exists, no need to check further.
  238. if d.rawStatusCode != nil || d.statusGen != nil {
  239. return nil
  240. }
  241. // If grpc status doesn't exist and http status doesn't exist,
  242. // then it's a malformed header.
  243. if d.httpStatus == nil {
  244. return streamErrorf(codes.Internal, "malformed header: doesn't contain status(gRPC or HTTP)")
  245. }
  246. if *(d.httpStatus) != http.StatusOK {
  247. code, ok := httpStatusConvTab[*(d.httpStatus)]
  248. if !ok {
  249. code = codes.Unknown
  250. }
  251. return streamErrorf(code, http.StatusText(*(d.httpStatus)))
  252. }
  253. // gRPC status doesn't exist and http status is OK.
  254. // Set rawStatusCode to be unknown and return nil error.
  255. // So that, if the stream has ended this Unknown status
  256. // will be propagated to the user.
  257. // Otherwise, it will be ignored. In which case, status from
  258. // a later trailer, that has StreamEnded flag set, is propagated.
  259. code := int(codes.Unknown)
  260. d.rawStatusCode = &code
  261. return nil
  262. }
  263. func (d *decodeState) addMetadata(k, v string) {
  264. if d.mdata == nil {
  265. d.mdata = make(map[string][]string)
  266. }
  267. d.mdata[k] = append(d.mdata[k], v)
  268. }
  269. func (d *decodeState) processHeaderField(f hpack.HeaderField) error {
  270. switch f.Name {
  271. case "content-type":
  272. contentSubtype, validContentType := contentSubtype(f.Value)
  273. if !validContentType {
  274. return streamErrorf(codes.Internal, "transport: received the unexpected content-type %q", f.Value)
  275. }
  276. d.contentSubtype = contentSubtype
  277. // TODO: do we want to propagate the whole content-type in the metadata,
  278. // or come up with a way to just propagate the content-subtype if it was set?
  279. // ie {"content-type": "application/grpc+proto"} or {"content-subtype": "proto"}
  280. // in the metadata?
  281. d.addMetadata(f.Name, f.Value)
  282. case "grpc-encoding":
  283. d.encoding = f.Value
  284. case "grpc-status":
  285. code, err := strconv.Atoi(f.Value)
  286. if err != nil {
  287. return streamErrorf(codes.Internal, "transport: malformed grpc-status: %v", err)
  288. }
  289. d.rawStatusCode = &code
  290. case "grpc-message":
  291. d.rawStatusMsg = decodeGrpcMessage(f.Value)
  292. case "grpc-status-details-bin":
  293. v, err := decodeBinHeader(f.Value)
  294. if err != nil {
  295. return streamErrorf(codes.Internal, "transport: malformed grpc-status-details-bin: %v", err)
  296. }
  297. s := &spb.Status{}
  298. if err := proto.Unmarshal(v, s); err != nil {
  299. return streamErrorf(codes.Internal, "transport: malformed grpc-status-details-bin: %v", err)
  300. }
  301. d.statusGen = status.FromProto(s)
  302. case "grpc-timeout":
  303. d.timeoutSet = true
  304. var err error
  305. if d.timeout, err = decodeTimeout(f.Value); err != nil {
  306. return streamErrorf(codes.Internal, "transport: malformed time-out: %v", err)
  307. }
  308. case ":path":
  309. d.method = f.Value
  310. case ":status":
  311. code, err := strconv.Atoi(f.Value)
  312. if err != nil {
  313. return streamErrorf(codes.Internal, "transport: malformed http-status: %v", err)
  314. }
  315. d.httpStatus = &code
  316. case "grpc-tags-bin":
  317. v, err := decodeBinHeader(f.Value)
  318. if err != nil {
  319. return streamErrorf(codes.Internal, "transport: malformed grpc-tags-bin: %v", err)
  320. }
  321. d.statsTags = v
  322. d.addMetadata(f.Name, string(v))
  323. case "grpc-trace-bin":
  324. v, err := decodeBinHeader(f.Value)
  325. if err != nil {
  326. return streamErrorf(codes.Internal, "transport: malformed grpc-trace-bin: %v", err)
  327. }
  328. d.statsTrace = v
  329. d.addMetadata(f.Name, string(v))
  330. default:
  331. if isReservedHeader(f.Name) && !isWhitelistedHeader(f.Name) {
  332. break
  333. }
  334. v, err := decodeMetadataHeader(f.Name, f.Value)
  335. if err != nil {
  336. errorf("Failed to decode metadata header (%q, %q): %v", f.Name, f.Value, err)
  337. return nil
  338. }
  339. d.addMetadata(f.Name, v)
  340. }
  341. return nil
  342. }
  343. type timeoutUnit uint8
  344. const (
  345. hour timeoutUnit = 'H'
  346. minute timeoutUnit = 'M'
  347. second timeoutUnit = 'S'
  348. millisecond timeoutUnit = 'm'
  349. microsecond timeoutUnit = 'u'
  350. nanosecond timeoutUnit = 'n'
  351. )
  352. func timeoutUnitToDuration(u timeoutUnit) (d time.Duration, ok bool) {
  353. switch u {
  354. case hour:
  355. return time.Hour, true
  356. case minute:
  357. return time.Minute, true
  358. case second:
  359. return time.Second, true
  360. case millisecond:
  361. return time.Millisecond, true
  362. case microsecond:
  363. return time.Microsecond, true
  364. case nanosecond:
  365. return time.Nanosecond, true
  366. default:
  367. }
  368. return
  369. }
  370. const maxTimeoutValue int64 = 100000000 - 1
  371. // div does integer division and round-up the result. Note that this is
  372. // equivalent to (d+r-1)/r but has less chance to overflow.
  373. func div(d, r time.Duration) int64 {
  374. if m := d % r; m > 0 {
  375. return int64(d/r + 1)
  376. }
  377. return int64(d / r)
  378. }
  379. // TODO(zhaoq): It is the simplistic and not bandwidth efficient. Improve it.
  380. func encodeTimeout(t time.Duration) string {
  381. if t <= 0 {
  382. return "0n"
  383. }
  384. if d := div(t, time.Nanosecond); d <= maxTimeoutValue {
  385. return strconv.FormatInt(d, 10) + "n"
  386. }
  387. if d := div(t, time.Microsecond); d <= maxTimeoutValue {
  388. return strconv.FormatInt(d, 10) + "u"
  389. }
  390. if d := div(t, time.Millisecond); d <= maxTimeoutValue {
  391. return strconv.FormatInt(d, 10) + "m"
  392. }
  393. if d := div(t, time.Second); d <= maxTimeoutValue {
  394. return strconv.FormatInt(d, 10) + "S"
  395. }
  396. if d := div(t, time.Minute); d <= maxTimeoutValue {
  397. return strconv.FormatInt(d, 10) + "M"
  398. }
  399. // Note that maxTimeoutValue * time.Hour > MaxInt64.
  400. return strconv.FormatInt(div(t, time.Hour), 10) + "H"
  401. }
  402. func decodeTimeout(s string) (time.Duration, error) {
  403. size := len(s)
  404. if size < 2 {
  405. return 0, fmt.Errorf("transport: timeout string is too short: %q", s)
  406. }
  407. unit := timeoutUnit(s[size-1])
  408. d, ok := timeoutUnitToDuration(unit)
  409. if !ok {
  410. return 0, fmt.Errorf("transport: timeout unit is not recognized: %q", s)
  411. }
  412. t, err := strconv.ParseInt(s[:size-1], 10, 64)
  413. if err != nil {
  414. return 0, err
  415. }
  416. return d * time.Duration(t), nil
  417. }
  418. const (
  419. spaceByte = ' '
  420. tildeByte = '~'
  421. percentByte = '%'
  422. )
  423. // encodeGrpcMessage is used to encode status code in header field
  424. // "grpc-message". It does percent encoding and also replaces invalid utf-8
  425. // characters with Unicode replacement character.
  426. //
  427. // It checks to see if each individual byte in msg is an allowable byte, and
  428. // then either percent encoding or passing it through. When percent encoding,
  429. // the byte is converted into hexadecimal notation with a '%' prepended.
  430. func encodeGrpcMessage(msg string) string {
  431. if msg == "" {
  432. return ""
  433. }
  434. lenMsg := len(msg)
  435. for i := 0; i < lenMsg; i++ {
  436. c := msg[i]
  437. if !(c >= spaceByte && c <= tildeByte && c != percentByte) {
  438. return encodeGrpcMessageUnchecked(msg)
  439. }
  440. }
  441. return msg
  442. }
  443. func encodeGrpcMessageUnchecked(msg string) string {
  444. var buf bytes.Buffer
  445. for len(msg) > 0 {
  446. r, size := utf8.DecodeRuneInString(msg)
  447. for _, b := range []byte(string(r)) {
  448. if size > 1 {
  449. // If size > 1, r is not ascii. Always do percent encoding.
  450. buf.WriteString(fmt.Sprintf("%%%02X", b))
  451. continue
  452. }
  453. // The for loop is necessary even if size == 1. r could be
  454. // utf8.RuneError.
  455. //
  456. // fmt.Sprintf("%%%02X", utf8.RuneError) gives "%FFFD".
  457. if b >= spaceByte && b <= tildeByte && b != percentByte {
  458. buf.WriteByte(b)
  459. } else {
  460. buf.WriteString(fmt.Sprintf("%%%02X", b))
  461. }
  462. }
  463. msg = msg[size:]
  464. }
  465. return buf.String()
  466. }
  467. // decodeGrpcMessage decodes the msg encoded by encodeGrpcMessage.
  468. func decodeGrpcMessage(msg string) string {
  469. if msg == "" {
  470. return ""
  471. }
  472. lenMsg := len(msg)
  473. for i := 0; i < lenMsg; i++ {
  474. if msg[i] == percentByte && i+2 < lenMsg {
  475. return decodeGrpcMessageUnchecked(msg)
  476. }
  477. }
  478. return msg
  479. }
  480. func decodeGrpcMessageUnchecked(msg string) string {
  481. var buf bytes.Buffer
  482. lenMsg := len(msg)
  483. for i := 0; i < lenMsg; i++ {
  484. c := msg[i]
  485. if c == percentByte && i+2 < lenMsg {
  486. parsed, err := strconv.ParseUint(msg[i+1:i+3], 16, 8)
  487. if err != nil {
  488. buf.WriteByte(c)
  489. } else {
  490. buf.WriteByte(byte(parsed))
  491. i += 2
  492. }
  493. } else {
  494. buf.WriteByte(c)
  495. }
  496. }
  497. return buf.String()
  498. }
  499. type bufWriter struct {
  500. buf []byte
  501. offset int
  502. batchSize int
  503. conn net.Conn
  504. err error
  505. onFlush func()
  506. }
  507. func newBufWriter(conn net.Conn, batchSize int) *bufWriter {
  508. return &bufWriter{
  509. buf: make([]byte, batchSize*2),
  510. batchSize: batchSize,
  511. conn: conn,
  512. }
  513. }
  514. func (w *bufWriter) Write(b []byte) (n int, err error) {
  515. if w.err != nil {
  516. return 0, w.err
  517. }
  518. if w.batchSize == 0 { // Buffer has been disabled.
  519. return w.conn.Write(b)
  520. }
  521. for len(b) > 0 {
  522. nn := copy(w.buf[w.offset:], b)
  523. b = b[nn:]
  524. w.offset += nn
  525. n += nn
  526. if w.offset >= w.batchSize {
  527. err = w.Flush()
  528. }
  529. }
  530. return n, err
  531. }
  532. func (w *bufWriter) Flush() error {
  533. if w.err != nil {
  534. return w.err
  535. }
  536. if w.offset == 0 {
  537. return nil
  538. }
  539. if w.onFlush != nil {
  540. w.onFlush()
  541. }
  542. _, w.err = w.conn.Write(w.buf[:w.offset])
  543. w.offset = 0
  544. return w.err
  545. }
  546. type framer struct {
  547. writer *bufWriter
  548. fr *http2.Framer
  549. }
  550. func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, maxHeaderListSize uint32) *framer {
  551. if writeBufferSize < 0 {
  552. writeBufferSize = 0
  553. }
  554. var r io.Reader = conn
  555. if readBufferSize > 0 {
  556. r = bufio.NewReaderSize(r, readBufferSize)
  557. }
  558. w := newBufWriter(conn, writeBufferSize)
  559. f := &framer{
  560. writer: w,
  561. fr: http2.NewFramer(w, r),
  562. }
  563. // Opt-in to Frame reuse API on framer to reduce garbage.
  564. // Frames aren't safe to read from after a subsequent call to ReadFrame.
  565. f.fr.SetReuseFrames()
  566. f.fr.MaxHeaderListSize = maxHeaderListSize
  567. f.fr.ReadMetaHeaders = hpack.NewDecoder(http2InitHeaderTableSize, nil)
  568. return f
  569. }