types.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package spdy implements the SPDY protocol (currently SPDY/3), described in
  5. // http://www.chromium.org/spdy/spdy-protocol/spdy-protocol-draft3.
  6. package spdy // import "golang.org/x/net/spdy"
  7. import (
  8. "bytes"
  9. "compress/zlib"
  10. "io"
  11. "net/http"
  12. )
  13. // Version is the protocol version number that this package implements.
  14. const Version = 3
  15. // ControlFrameType stores the type field in a control frame header.
  16. type ControlFrameType uint16
  17. const (
  18. TypeSynStream ControlFrameType = 0x0001
  19. TypeSynReply = 0x0002
  20. TypeRstStream = 0x0003
  21. TypeSettings = 0x0004
  22. TypePing = 0x0006
  23. TypeGoAway = 0x0007
  24. TypeHeaders = 0x0008
  25. TypeWindowUpdate = 0x0009
  26. )
  27. // ControlFlags are the flags that can be set on a control frame.
  28. type ControlFlags uint8
  29. const (
  30. ControlFlagFin ControlFlags = 0x01
  31. ControlFlagUnidirectional = 0x02
  32. ControlFlagSettingsClearSettings = 0x01
  33. )
  34. // DataFlags are the flags that can be set on a data frame.
  35. type DataFlags uint8
  36. const (
  37. DataFlagFin DataFlags = 0x01
  38. )
  39. // MaxDataLength is the maximum number of bytes that can be stored in one frame.
  40. const MaxDataLength = 1<<24 - 1
  41. // headerValueSepator separates multiple header values.
  42. const headerValueSeparator = "\x00"
  43. // Frame is a single SPDY frame in its unpacked in-memory representation. Use
  44. // Framer to read and write it.
  45. type Frame interface {
  46. write(f *Framer) error
  47. }
  48. // ControlFrameHeader contains all the fields in a control frame header,
  49. // in its unpacked in-memory representation.
  50. type ControlFrameHeader struct {
  51. // Note, high bit is the "Control" bit.
  52. version uint16 // spdy version number
  53. frameType ControlFrameType
  54. Flags ControlFlags
  55. length uint32 // length of data field
  56. }
  57. type controlFrame interface {
  58. Frame
  59. read(h ControlFrameHeader, f *Framer) error
  60. }
  61. // StreamId represents a 31-bit value identifying the stream.
  62. type StreamId uint32
  63. // SynStreamFrame is the unpacked, in-memory representation of a SYN_STREAM
  64. // frame.
  65. type SynStreamFrame struct {
  66. CFHeader ControlFrameHeader
  67. StreamId StreamId
  68. AssociatedToStreamId StreamId // stream id for a stream which this stream is associated to
  69. Priority uint8 // priority of this frame (3-bit)
  70. Slot uint8 // index in the server's credential vector of the client certificate
  71. Headers http.Header
  72. }
  73. // SynReplyFrame is the unpacked, in-memory representation of a SYN_REPLY frame.
  74. type SynReplyFrame struct {
  75. CFHeader ControlFrameHeader
  76. StreamId StreamId
  77. Headers http.Header
  78. }
  79. // RstStreamStatus represents the status that led to a RST_STREAM.
  80. type RstStreamStatus uint32
  81. const (
  82. ProtocolError RstStreamStatus = iota + 1
  83. InvalidStream
  84. RefusedStream
  85. UnsupportedVersion
  86. Cancel
  87. InternalError
  88. FlowControlError
  89. StreamInUse
  90. StreamAlreadyClosed
  91. InvalidCredentials
  92. FrameTooLarge
  93. )
  94. // RstStreamFrame is the unpacked, in-memory representation of a RST_STREAM
  95. // frame.
  96. type RstStreamFrame struct {
  97. CFHeader ControlFrameHeader
  98. StreamId StreamId
  99. Status RstStreamStatus
  100. }
  101. // SettingsFlag represents a flag in a SETTINGS frame.
  102. type SettingsFlag uint8
  103. const (
  104. FlagSettingsPersistValue SettingsFlag = 0x1
  105. FlagSettingsPersisted = 0x2
  106. )
  107. // SettingsFlag represents the id of an id/value pair in a SETTINGS frame.
  108. type SettingsId uint32
  109. const (
  110. SettingsUploadBandwidth SettingsId = iota + 1
  111. SettingsDownloadBandwidth
  112. SettingsRoundTripTime
  113. SettingsMaxConcurrentStreams
  114. SettingsCurrentCwnd
  115. SettingsDownloadRetransRate
  116. SettingsInitialWindowSize
  117. SettingsClientCretificateVectorSize
  118. )
  119. // SettingsFlagIdValue is the unpacked, in-memory representation of the
  120. // combined flag/id/value for a setting in a SETTINGS frame.
  121. type SettingsFlagIdValue struct {
  122. Flag SettingsFlag
  123. Id SettingsId
  124. Value uint32
  125. }
  126. // SettingsFrame is the unpacked, in-memory representation of a SPDY
  127. // SETTINGS frame.
  128. type SettingsFrame struct {
  129. CFHeader ControlFrameHeader
  130. FlagIdValues []SettingsFlagIdValue
  131. }
  132. // PingFrame is the unpacked, in-memory representation of a PING frame.
  133. type PingFrame struct {
  134. CFHeader ControlFrameHeader
  135. Id uint32 // unique id for this ping, from server is even, from client is odd.
  136. }
  137. // GoAwayStatus represents the status in a GoAwayFrame.
  138. type GoAwayStatus uint32
  139. const (
  140. GoAwayOK GoAwayStatus = iota
  141. GoAwayProtocolError
  142. GoAwayInternalError
  143. )
  144. // GoAwayFrame is the unpacked, in-memory representation of a GOAWAY frame.
  145. type GoAwayFrame struct {
  146. CFHeader ControlFrameHeader
  147. LastGoodStreamId StreamId // last stream id which was accepted by sender
  148. Status GoAwayStatus
  149. }
  150. // HeadersFrame is the unpacked, in-memory representation of a HEADERS frame.
  151. type HeadersFrame struct {
  152. CFHeader ControlFrameHeader
  153. StreamId StreamId
  154. Headers http.Header
  155. }
  156. // WindowUpdateFrame is the unpacked, in-memory representation of a
  157. // WINDOW_UPDATE frame.
  158. type WindowUpdateFrame struct {
  159. CFHeader ControlFrameHeader
  160. StreamId StreamId
  161. DeltaWindowSize uint32 // additional number of bytes to existing window size
  162. }
  163. // TODO: Implement credential frame and related methods.
  164. // DataFrame is the unpacked, in-memory representation of a DATA frame.
  165. type DataFrame struct {
  166. // Note, high bit is the "Control" bit. Should be 0 for data frames.
  167. StreamId StreamId
  168. Flags DataFlags
  169. Data []byte // payload data of this frame
  170. }
  171. // A SPDY specific error.
  172. type ErrorCode string
  173. const (
  174. UnlowercasedHeaderName ErrorCode = "header was not lowercased"
  175. DuplicateHeaders = "multiple headers with same name"
  176. WrongCompressedPayloadSize = "compressed payload size was incorrect"
  177. UnknownFrameType = "unknown frame type"
  178. InvalidControlFrame = "invalid control frame"
  179. InvalidDataFrame = "invalid data frame"
  180. InvalidHeaderPresent = "frame contained invalid header"
  181. ZeroStreamId = "stream id zero is disallowed"
  182. )
  183. // Error contains both the type of error and additional values. StreamId is 0
  184. // if Error is not associated with a stream.
  185. type Error struct {
  186. Err ErrorCode
  187. StreamId StreamId
  188. }
  189. func (e *Error) Error() string {
  190. return string(e.Err)
  191. }
  192. var invalidReqHeaders = map[string]bool{
  193. "Connection": true,
  194. "Host": true,
  195. "Keep-Alive": true,
  196. "Proxy-Connection": true,
  197. "Transfer-Encoding": true,
  198. }
  199. var invalidRespHeaders = map[string]bool{
  200. "Connection": true,
  201. "Keep-Alive": true,
  202. "Proxy-Connection": true,
  203. "Transfer-Encoding": true,
  204. }
  205. // Framer handles serializing/deserializing SPDY frames, including compressing/
  206. // decompressing payloads.
  207. type Framer struct {
  208. headerCompressionDisabled bool
  209. w io.Writer
  210. headerBuf *bytes.Buffer
  211. headerCompressor *zlib.Writer
  212. r io.Reader
  213. headerReader io.LimitedReader
  214. headerDecompressor io.ReadCloser
  215. }
  216. // NewFramer allocates a new Framer for a given SPDY connection, represented by
  217. // a io.Writer and io.Reader. Note that Framer will read and write individual fields
  218. // from/to the Reader and Writer, so the caller should pass in an appropriately
  219. // buffered implementation to optimize performance.
  220. func NewFramer(w io.Writer, r io.Reader) (*Framer, error) {
  221. compressBuf := new(bytes.Buffer)
  222. compressor, err := zlib.NewWriterLevelDict(compressBuf, zlib.BestCompression, []byte(headerDictionary))
  223. if err != nil {
  224. return nil, err
  225. }
  226. framer := &Framer{
  227. w: w,
  228. headerBuf: compressBuf,
  229. headerCompressor: compressor,
  230. r: r,
  231. }
  232. return framer, nil
  233. }