OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / spdy / types.go
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
5 package spdy
6
7 import (
8         "bytes"
9         "compress/zlib"
10         "http"
11         "io"
12         "os"
13 )
14
15 //  Data Frame Format
16 //  +----------------------------------+
17 //  |0|       Stream-ID (31bits)       |
18 //  +----------------------------------+
19 //  | flags (8)  |  Length (24 bits)   |
20 //  +----------------------------------+
21 //  |               Data               |
22 //  +----------------------------------+
23 //
24 //  Control Frame Format
25 //  +----------------------------------+
26 //  |1| Version(15bits) | Type(16bits) |
27 //  +----------------------------------+
28 //  | flags (8)  |  Length (24 bits)   |
29 //  +----------------------------------+
30 //  |               Data               |
31 //  +----------------------------------+
32 //
33 //  Control Frame: SYN_STREAM
34 //  +----------------------------------+
35 //  |1|000000000000001|0000000000000001|
36 //  +----------------------------------+
37 //  | flags (8)  |  Length (24 bits)   |  >= 12
38 //  +----------------------------------+
39 //  |X|       Stream-ID(31bits)        |
40 //  +----------------------------------+
41 //  |X|Associated-To-Stream-ID (31bits)|
42 //  +----------------------------------+
43 //  |Pri| unused      | Length (16bits)|
44 //  +----------------------------------+
45 //
46 //  Control Frame: SYN_REPLY
47 //  +----------------------------------+
48 //  |1|000000000000001|0000000000000010|
49 //  +----------------------------------+
50 //  | flags (8)  |  Length (24 bits)   |  >= 8
51 //  +----------------------------------+
52 //  |X|       Stream-ID(31bits)        |
53 //  +----------------------------------+
54 //  | unused (16 bits)| Length (16bits)|
55 //  +----------------------------------+
56 //
57 //  Control Frame: RST_STREAM
58 //  +----------------------------------+
59 //  |1|000000000000001|0000000000000011|
60 //  +----------------------------------+
61 //  | flags (8)  |  Length (24 bits)   |  >= 4
62 //  +----------------------------------+
63 //  |X|       Stream-ID(31bits)        |
64 //  +----------------------------------+
65 //  |        Status code (32 bits)     |
66 //  +----------------------------------+
67 //
68 //  Control Frame: SETTINGS
69 //  +----------------------------------+
70 //  |1|000000000000001|0000000000000100|
71 //  +----------------------------------+
72 //  | flags (8)  |  Length (24 bits)   |
73 //  +----------------------------------+
74 //  |        # of entries (32)         |
75 //  +----------------------------------+
76 //
77 //  Control Frame: NOOP
78 //  +----------------------------------+
79 //  |1|000000000000001|0000000000000101|
80 //  +----------------------------------+
81 //  | flags (8)  |  Length (24 bits)   | = 0
82 //  +----------------------------------+
83 //
84 //  Control Frame: PING
85 //  +----------------------------------+
86 //  |1|000000000000001|0000000000000110|
87 //  +----------------------------------+
88 //  | flags (8)  |  Length (24 bits)   | = 4
89 //  +----------------------------------+
90 //  |        Unique id (32 bits)       |
91 //  +----------------------------------+
92 //
93 //  Control Frame: GOAWAY
94 //  +----------------------------------+
95 //  |1|000000000000001|0000000000000111|
96 //  +----------------------------------+
97 //  | flags (8)  |  Length (24 bits)   | = 4
98 //  +----------------------------------+
99 //  |X|  Last-accepted-stream-id       |
100 //  +----------------------------------+
101 //
102 //  Control Frame: HEADERS
103 //  +----------------------------------+
104 //  |1|000000000000001|0000000000001000|
105 //  +----------------------------------+
106 //  | flags (8)  |  Length (24 bits)   | >= 8
107 //  +----------------------------------+
108 //  |X|      Stream-ID (31 bits)       |
109 //  +----------------------------------+
110 //  | unused (16 bits)| Length (16bits)|
111 //  +----------------------------------+
112 //
113 //  Control Frame: WINDOW_UPDATE
114 //  +----------------------------------+
115 //  |1|000000000000001|0000000000001001|
116 //  +----------------------------------+
117 //  | flags (8)  |  Length (24 bits)   | = 8
118 //  +----------------------------------+
119 //  |X|      Stream-ID (31 bits)       |
120 //  +----------------------------------+
121 //  |   Delta-Window-Size (32 bits)    |
122 //  +----------------------------------+
123
124 // Version is the protocol version number that this package implements.
125 const Version = 2
126
127 // ControlFrameType stores the type field in a control frame header.
128 type ControlFrameType uint16
129
130 // Control frame type constants
131 const (
132         TypeSynStream    ControlFrameType = 0x0001
133         TypeSynReply                      = 0x0002
134         TypeRstStream                     = 0x0003
135         TypeSettings                      = 0x0004
136         TypeNoop                          = 0x0005
137         TypePing                          = 0x0006
138         TypeGoAway                        = 0x0007
139         TypeHeaders                       = 0x0008
140         TypeWindowUpdate                  = 0x0009
141 )
142
143 // ControlFlags are the flags that can be set on a control frame.
144 type ControlFlags uint8
145
146 const (
147         ControlFlagFin ControlFlags = 0x01
148 )
149
150 // DataFlags are the flags that can be set on a data frame.
151 type DataFlags uint8
152
153 const (
154         DataFlagFin        DataFlags = 0x01
155         DataFlagCompressed           = 0x02
156 )
157
158 // MaxDataLength is the maximum number of bytes that can be stored in one frame.
159 const MaxDataLength = 1<<24 - 1
160
161 // Frame is a single SPDY frame in its unpacked in-memory representation. Use
162 // Framer to read and write it.
163 type Frame interface {
164         write(f *Framer) os.Error
165 }
166
167 // ControlFrameHeader contains all the fields in a control frame header,
168 // in its unpacked in-memory representation.
169 type ControlFrameHeader struct {
170         // Note, high bit is the "Control" bit.
171         version   uint16
172         frameType ControlFrameType
173         Flags     ControlFlags
174         length    uint32
175 }
176
177 type controlFrame interface {
178         Frame
179         read(h ControlFrameHeader, f *Framer) os.Error
180 }
181
182 // SynStreamFrame is the unpacked, in-memory representation of a SYN_STREAM
183 // frame.
184 type SynStreamFrame struct {
185         CFHeader             ControlFrameHeader
186         StreamId             uint32
187         AssociatedToStreamId uint32
188         // Note, only 2 highest bits currently used
189         // Rest of Priority is unused.
190         Priority uint16
191         Headers  http.Header
192 }
193
194 // SynReplyFrame is the unpacked, in-memory representation of a SYN_REPLY frame.
195 type SynReplyFrame struct {
196         CFHeader ControlFrameHeader
197         StreamId uint32
198         Headers  http.Header
199 }
200
201 // StatusCode represents the status that led to a RST_STREAM
202 type StatusCode uint32
203
204 const (
205         ProtocolError      StatusCode = 1
206         InvalidStream                 = 2
207         RefusedStream                 = 3
208         UnsupportedVersion            = 4
209         Cancel                        = 5
210         InternalError                 = 6
211         FlowControlError              = 7
212 )
213
214 // RstStreamFrame is the unpacked, in-memory representation of a RST_STREAM
215 // frame.
216 type RstStreamFrame struct {
217         CFHeader ControlFrameHeader
218         StreamId uint32
219         Status   StatusCode
220 }
221
222 // SettingsFlag represents a flag in a SETTINGS frame.
223 type SettingsFlag uint8
224
225 const (
226         FlagSettingsPersistValue SettingsFlag = 0x1
227         FlagSettingsPersisted                 = 0x2
228 )
229
230 // SettingsFlag represents the id of an id/value pair in a SETTINGS frame.
231 type SettingsId uint32
232
233 const (
234         SettingsUploadBandwidth      SettingsId = 1
235         SettingsDownloadBandwidth               = 2
236         SettingsRoundTripTime                   = 3
237         SettingsMaxConcurrentStreams            = 4
238         SettingsCurrentCwnd                     = 5
239 )
240
241 // SettingsFlagIdValue is the unpacked, in-memory representation of the
242 // combined flag/id/value for a setting in a SETTINGS frame.
243 type SettingsFlagIdValue struct {
244         Flag  SettingsFlag
245         Id    SettingsId
246         Value uint32
247 }
248
249 // SettingsFrame is the unpacked, in-memory representation of a SPDY
250 // SETTINGS frame.
251 type SettingsFrame struct {
252         CFHeader     ControlFrameHeader
253         FlagIdValues []SettingsFlagIdValue
254 }
255
256 // NoopFrame is the unpacked, in-memory representation of a NOOP frame.
257 type NoopFrame struct {
258         CFHeader ControlFrameHeader
259 }
260
261 // PingFrame is the unpacked, in-memory representation of a PING frame.
262 type PingFrame struct {
263         CFHeader ControlFrameHeader
264         Id       uint32
265 }
266
267 // GoAwayFrame is the unpacked, in-memory representation of a GOAWAY frame.
268 type GoAwayFrame struct {
269         CFHeader         ControlFrameHeader
270         LastGoodStreamId uint32
271 }
272
273 // HeadersFrame is the unpacked, in-memory representation of a HEADERS frame.
274 type HeadersFrame struct {
275         CFHeader ControlFrameHeader
276         StreamId uint32
277         Headers  http.Header
278 }
279
280 // DataFrame is the unpacked, in-memory representation of a DATA frame.
281 type DataFrame struct {
282         // Note, high bit is the "Control" bit. Should be 0 for data frames.
283         StreamId uint32
284         Flags    DataFlags
285         Data     []byte
286 }
287
288 // HeaderDictionary is the dictionary sent to the zlib compressor/decompressor.
289 // Even though the specification states there is no null byte at the end, Chrome sends it.
290 const HeaderDictionary = "optionsgetheadpostputdeletetrace" +
291         "acceptaccept-charsetaccept-encodingaccept-languageauthorizationexpectfromhost" +
292         "if-modified-sinceif-matchif-none-matchif-rangeif-unmodifiedsince" +
293         "max-forwardsproxy-authorizationrangerefererteuser-agent" +
294         "100101200201202203204205206300301302303304305306307400401402403404405406407408409410411412413414415416417500501502503504505" +
295         "accept-rangesageetaglocationproxy-authenticatepublicretry-after" +
296         "servervarywarningwww-authenticateallowcontent-basecontent-encodingcache-control" +
297         "connectiondatetrailertransfer-encodingupgradeviawarning" +
298         "content-languagecontent-lengthcontent-locationcontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookie" +
299         "MondayTuesdayWednesdayThursdayFridaySaturdaySunday" +
300         "JanFebMarAprMayJunJulAugSepOctNovDec" +
301         "chunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplication/xhtmltext/plainpublicmax-age" +
302         "charset=iso-8859-1utf-8gzipdeflateHTTP/1.1statusversionurl\x00"
303
304 // A SPDY specific error.
305 type ErrorCode string
306
307 const (
308         UnlowercasedHeaderName     ErrorCode = "header was not lowercased"
309         DuplicateHeaders           ErrorCode = "multiple headers with same name"
310         WrongCompressedPayloadSize ErrorCode = "compressed payload size was incorrect"
311         UnknownFrameType           ErrorCode = "unknown frame type"
312         InvalidControlFrame        ErrorCode = "invalid control frame"
313         InvalidDataFrame           ErrorCode = "invalid data frame"
314         InvalidHeaderPresent       ErrorCode = "frame contained invalid header"
315 )
316
317 // Error contains both the type of error and additional values. StreamId is 0
318 // if Error is not associated with a stream.
319 type Error struct {
320         Err      ErrorCode
321         StreamId uint32
322 }
323
324 func (e *Error) String() string {
325         return string(e.Err)
326 }
327
328 var invalidReqHeaders = map[string]bool{
329         "Connection":        true,
330         "Keep-Alive":        true,
331         "Proxy-Connection":  true,
332         "Transfer-Encoding": true,
333 }
334
335 var invalidRespHeaders = map[string]bool{
336         "Connection":        true,
337         "Keep-Alive":        true,
338         "Transfer-Encoding": true,
339 }
340
341 // Framer handles serializing/deserializing SPDY frames, including compressing/
342 // decompressing payloads.
343 type Framer struct {
344         headerCompressionDisabled bool
345         w                         io.Writer
346         headerBuf                 *bytes.Buffer
347         headerCompressor          *zlib.Writer
348         r                         io.Reader
349         headerReader              io.LimitedReader
350         headerDecompressor        io.ReadCloser
351 }
352
353 // NewFramer allocates a new Framer for a given SPDY connection, repesented by
354 // a io.Writer and io.Reader. Note that Framer will read and write individual fields 
355 // from/to the Reader and Writer, so the caller should pass in an appropriately 
356 // buffered implementation to optimize performance.
357 func NewFramer(w io.Writer, r io.Reader) (*Framer, os.Error) {
358         compressBuf := new(bytes.Buffer)
359         compressor, err := zlib.NewWriterDict(compressBuf, zlib.BestCompression, []byte(HeaderDictionary))
360         if err != nil {
361                 return nil, err
362         }
363         framer := &Framer{
364                 w:                w,
365                 headerBuf:        compressBuf,
366                 headerCompressor: compressor,
367                 r:                r,
368         }
369         return framer, nil
370 }