Browse Source

Removed unnecessary code from vendor directory

Wirecog 7 years ago
parent
commit
1487767455
100 changed files with 0 additions and 40959 deletions
  1. 0
    41
      vendor/golang.org/x/net/bpf/asm.go
  2. 0
    218
      vendor/golang.org/x/net/bpf/constants.go
  3. 0
    82
      vendor/golang.org/x/net/bpf/doc.go
  4. 0
    704
      vendor/golang.org/x/net/bpf/instructions.go
  5. 0
    525
      vendor/golang.org/x/net/bpf/instructions_test.go
  6. 0
    10
      vendor/golang.org/x/net/bpf/setter.go
  7. 0
    1
      vendor/golang.org/x/net/bpf/testdata/all_instructions.bpf
  8. 0
    79
      vendor/golang.org/x/net/bpf/testdata/all_instructions.txt
  9. 0
    140
      vendor/golang.org/x/net/bpf/vm.go
  10. 0
    512
      vendor/golang.org/x/net/bpf/vm_aluop_test.go
  11. 0
    192
      vendor/golang.org/x/net/bpf/vm_bpf_test.go
  12. 0
    49
      vendor/golang.org/x/net/bpf/vm_extension_test.go
  13. 0
    174
      vendor/golang.org/x/net/bpf/vm_instructions.go
  14. 0
    380
      vendor/golang.org/x/net/bpf/vm_jump_test.go
  15. 0
    246
      vendor/golang.org/x/net/bpf/vm_load_test.go
  16. 0
    115
      vendor/golang.org/x/net/bpf/vm_ret_test.go
  17. 0
    247
      vendor/golang.org/x/net/bpf/vm_scratch_test.go
  18. 0
    144
      vendor/golang.org/x/net/bpf/vm_test.go
  19. 0
    54
      vendor/golang.org/x/net/context/context.go
  20. 0
    583
      vendor/golang.org/x/net/context/context_test.go
  21. 0
    74
      vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go
  22. 0
    29
      vendor/golang.org/x/net/context/ctxhttp/ctxhttp_17_test.go
  23. 0
    147
      vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17.go
  24. 0
    79
      vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17_test.go
  25. 0
    105
      vendor/golang.org/x/net/context/ctxhttp/ctxhttp_test.go
  26. 0
    72
      vendor/golang.org/x/net/context/go17.go
  27. 0
    20
      vendor/golang.org/x/net/context/go19.go
  28. 0
    300
      vendor/golang.org/x/net/context/pre_go17.go
  29. 0
    109
      vendor/golang.org/x/net/context/pre_go19.go
  30. 0
    31
      vendor/golang.org/x/net/context/withtimeout_test.go
  31. 0
    210
      vendor/golang.org/x/net/dict/dict.go
  32. 0
    132
      vendor/golang.org/x/net/dns/dnsmessage/example_test.go
  33. 0
    1997
      vendor/golang.org/x/net/dns/dnsmessage/message.go
  34. 0
    1116
      vendor/golang.org/x/net/dns/dnsmessage/message_test.go
  35. 0
    2
      vendor/golang.org/x/net/http2/.gitignore
  36. 0
    51
      vendor/golang.org/x/net/http2/Dockerfile
  37. 0
    3
      vendor/golang.org/x/net/http2/Makefile
  38. 0
    20
      vendor/golang.org/x/net/http2/README
  39. 0
    641
      vendor/golang.org/x/net/http2/ciphers.go
  40. 0
    309
      vendor/golang.org/x/net/http2/ciphers_test.go
  41. 0
    256
      vendor/golang.org/x/net/http2/client_conn_pool.go
  42. 0
    80
      vendor/golang.org/x/net/http2/configure_transport.go
  43. 0
    146
      vendor/golang.org/x/net/http2/databuffer.go
  44. 0
    157
      vendor/golang.org/x/net/http2/databuffer_test.go
  45. 0
    133
      vendor/golang.org/x/net/http2/errors.go
  46. 0
    24
      vendor/golang.org/x/net/http2/errors_test.go
  47. 0
    50
      vendor/golang.org/x/net/http2/flow.go
  48. 0
    53
      vendor/golang.org/x/net/http2/flow_test.go
  49. 0
    1579
      vendor/golang.org/x/net/http2/frame.go
  50. 0
    1191
      vendor/golang.org/x/net/http2/frame_test.go
  51. 0
    16
      vendor/golang.org/x/net/http2/go16.go
  52. 0
    106
      vendor/golang.org/x/net/http2/go17.go
  53. 0
    36
      vendor/golang.org/x/net/http2/go17_not18.go
  54. 0
    56
      vendor/golang.org/x/net/http2/go18.go
  55. 0
    79
      vendor/golang.org/x/net/http2/go18_test.go
  56. 0
    16
      vendor/golang.org/x/net/http2/go19.go
  57. 0
    60
      vendor/golang.org/x/net/http2/go19_test.go
  58. 0
    170
      vendor/golang.org/x/net/http2/gotrack.go
  59. 0
    33
      vendor/golang.org/x/net/http2/gotrack_test.go
  60. 0
    5
      vendor/golang.org/x/net/http2/h2demo/.gitignore
  61. 0
    8
      vendor/golang.org/x/net/http2/h2demo/Makefile
  62. 0
    16
      vendor/golang.org/x/net/http2/h2demo/README
  63. 0
    538
      vendor/golang.org/x/net/http2/h2demo/h2demo.go
  64. 0
    302
      vendor/golang.org/x/net/http2/h2demo/launch.go
  65. 0
    27
      vendor/golang.org/x/net/http2/h2demo/rootCA.key
  66. 0
    26
      vendor/golang.org/x/net/http2/h2demo/rootCA.pem
  67. 0
    1
      vendor/golang.org/x/net/http2/h2demo/rootCA.srl
  68. 0
    20
      vendor/golang.org/x/net/http2/h2demo/server.crt
  69. 0
    27
      vendor/golang.org/x/net/http2/h2demo/server.key
  70. 0
    1991
      vendor/golang.org/x/net/http2/h2demo/tmpl.go
  71. 0
    97
      vendor/golang.org/x/net/http2/h2i/README.md
  72. 0
    522
      vendor/golang.org/x/net/http2/h2i/h2i.go
  73. 0
    78
      vendor/golang.org/x/net/http2/headermap.go
  74. 0
    240
      vendor/golang.org/x/net/http2/hpack/encode.go
  75. 0
    386
      vendor/golang.org/x/net/http2/hpack/encode_test.go
  76. 0
    490
      vendor/golang.org/x/net/http2/hpack/hpack.go
  77. 0
    722
      vendor/golang.org/x/net/http2/hpack/hpack_test.go
  78. 0
    212
      vendor/golang.org/x/net/http2/hpack/huffman.go
  79. 0
    479
      vendor/golang.org/x/net/http2/hpack/tables.go
  80. 0
    214
      vendor/golang.org/x/net/http2/hpack/tables_test.go
  81. 0
    391
      vendor/golang.org/x/net/http2/http2.go
  82. 0
    199
      vendor/golang.org/x/net/http2/http2_test.go
  83. 0
    21
      vendor/golang.org/x/net/http2/not_go16.go
  84. 0
    87
      vendor/golang.org/x/net/http2/not_go17.go
  85. 0
    29
      vendor/golang.org/x/net/http2/not_go18.go
  86. 0
    16
      vendor/golang.org/x/net/http2/not_go19.go
  87. 0
    163
      vendor/golang.org/x/net/http2/pipe.go
  88. 0
    130
      vendor/golang.org/x/net/http2/pipe_test.go
  89. 0
    2857
      vendor/golang.org/x/net/http2/server.go
  90. 0
    521
      vendor/golang.org/x/net/http2/server_push_test.go
  91. 0
    3721
      vendor/golang.org/x/net/http2/server_test.go
  92. 0
    5021
      vendor/golang.org/x/net/http2/testdata/draft-ietf-httpbis-http2.xml
  93. 0
    2221
      vendor/golang.org/x/net/http2/transport.go
  94. 0
    3320
      vendor/golang.org/x/net/http2/transport_test.go
  95. 0
    370
      vendor/golang.org/x/net/http2/write.go
  96. 0
    242
      vendor/golang.org/x/net/http2/writesched.go
  97. 0
    452
      vendor/golang.org/x/net/http2/writesched_priority.go
  98. 0
    541
      vendor/golang.org/x/net/http2/writesched_priority_test.go
  99. 0
    72
      vendor/golang.org/x/net/http2/writesched_random.go
  100. 0
    0
      vendor/golang.org/x/net/http2/writesched_random_test.go

+ 0
- 41
vendor/golang.org/x/net/bpf/asm.go View File

1
-// Copyright 2016 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 bpf
6
-
7
-import "fmt"
8
-
9
-// Assemble converts insts into raw instructions suitable for loading
10
-// into a BPF virtual machine.
11
-//
12
-// Currently, no optimization is attempted, the assembled program flow
13
-// is exactly as provided.
14
-func Assemble(insts []Instruction) ([]RawInstruction, error) {
15
-	ret := make([]RawInstruction, len(insts))
16
-	var err error
17
-	for i, inst := range insts {
18
-		ret[i], err = inst.Assemble()
19
-		if err != nil {
20
-			return nil, fmt.Errorf("assembling instruction %d: %s", i+1, err)
21
-		}
22
-	}
23
-	return ret, nil
24
-}
25
-
26
-// Disassemble attempts to parse raw back into
27
-// Instructions. Unrecognized RawInstructions are assumed to be an
28
-// extension not implemented by this package, and are passed through
29
-// unchanged to the output. The allDecoded value reports whether insts
30
-// contains no RawInstructions.
31
-func Disassemble(raw []RawInstruction) (insts []Instruction, allDecoded bool) {
32
-	insts = make([]Instruction, len(raw))
33
-	allDecoded = true
34
-	for i, r := range raw {
35
-		insts[i] = r.Disassemble()
36
-		if _, ok := insts[i].(RawInstruction); ok {
37
-			allDecoded = false
38
-		}
39
-	}
40
-	return insts, allDecoded
41
-}

+ 0
- 218
vendor/golang.org/x/net/bpf/constants.go View File

1
-// Copyright 2016 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 bpf
6
-
7
-// A Register is a register of the BPF virtual machine.
8
-type Register uint16
9
-
10
-const (
11
-	// RegA is the accumulator register. RegA is always the
12
-	// destination register of ALU operations.
13
-	RegA Register = iota
14
-	// RegX is the indirection register, used by LoadIndirect
15
-	// operations.
16
-	RegX
17
-)
18
-
19
-// An ALUOp is an arithmetic or logic operation.
20
-type ALUOp uint16
21
-
22
-// ALU binary operation types.
23
-const (
24
-	ALUOpAdd ALUOp = iota << 4
25
-	ALUOpSub
26
-	ALUOpMul
27
-	ALUOpDiv
28
-	ALUOpOr
29
-	ALUOpAnd
30
-	ALUOpShiftLeft
31
-	ALUOpShiftRight
32
-	aluOpNeg // Not exported because it's the only unary ALU operation, and gets its own instruction type.
33
-	ALUOpMod
34
-	ALUOpXor
35
-)
36
-
37
-// A JumpTest is a comparison operator used in conditional jumps.
38
-type JumpTest uint16
39
-
40
-// Supported operators for conditional jumps.
41
-const (
42
-	// K == A
43
-	JumpEqual JumpTest = iota
44
-	// K != A
45
-	JumpNotEqual
46
-	// K > A
47
-	JumpGreaterThan
48
-	// K < A
49
-	JumpLessThan
50
-	// K >= A
51
-	JumpGreaterOrEqual
52
-	// K <= A
53
-	JumpLessOrEqual
54
-	// K & A != 0
55
-	JumpBitsSet
56
-	// K & A == 0
57
-	JumpBitsNotSet
58
-)
59
-
60
-// An Extension is a function call provided by the kernel that
61
-// performs advanced operations that are expensive or impossible
62
-// within the BPF virtual machine.
63
-//
64
-// Extensions are only implemented by the Linux kernel.
65
-//
66
-// TODO: should we prune this list? Some of these extensions seem
67
-// either broken or near-impossible to use correctly, whereas other
68
-// (len, random, ifindex) are quite useful.
69
-type Extension int
70
-
71
-// Extension functions available in the Linux kernel.
72
-const (
73
-	// extOffset is the negative maximum number of instructions used
74
-	// to load instructions by overloading the K argument.
75
-	extOffset = -0x1000
76
-	// ExtLen returns the length of the packet.
77
-	ExtLen Extension = 1
78
-	// ExtProto returns the packet's L3 protocol type.
79
-	ExtProto Extension = 0
80
-	// ExtType returns the packet's type (skb->pkt_type in the kernel)
81
-	//
82
-	// TODO: better documentation. How nice an API do we want to
83
-	// provide for these esoteric extensions?
84
-	ExtType Extension = 4
85
-	// ExtPayloadOffset returns the offset of the packet payload, or
86
-	// the first protocol header that the kernel does not know how to
87
-	// parse.
88
-	ExtPayloadOffset Extension = 52
89
-	// ExtInterfaceIndex returns the index of the interface on which
90
-	// the packet was received.
91
-	ExtInterfaceIndex Extension = 8
92
-	// ExtNetlinkAttr returns the netlink attribute of type X at
93
-	// offset A.
94
-	ExtNetlinkAttr Extension = 12
95
-	// ExtNetlinkAttrNested returns the nested netlink attribute of
96
-	// type X at offset A.
97
-	ExtNetlinkAttrNested Extension = 16
98
-	// ExtMark returns the packet's mark value.
99
-	ExtMark Extension = 20
100
-	// ExtQueue returns the packet's assigned hardware queue.
101
-	ExtQueue Extension = 24
102
-	// ExtLinkLayerType returns the packet's hardware address type
103
-	// (e.g. Ethernet, Infiniband).
104
-	ExtLinkLayerType Extension = 28
105
-	// ExtRXHash returns the packets receive hash.
106
-	//
107
-	// TODO: figure out what this rxhash actually is.
108
-	ExtRXHash Extension = 32
109
-	// ExtCPUID returns the ID of the CPU processing the current
110
-	// packet.
111
-	ExtCPUID Extension = 36
112
-	// ExtVLANTag returns the packet's VLAN tag.
113
-	ExtVLANTag Extension = 44
114
-	// ExtVLANTagPresent returns non-zero if the packet has a VLAN
115
-	// tag.
116
-	//
117
-	// TODO: I think this might be a lie: it reads bit 0x1000 of the
118
-	// VLAN header, which changed meaning in recent revisions of the
119
-	// spec - this extension may now return meaningless information.
120
-	ExtVLANTagPresent Extension = 48
121
-	// ExtVLANProto returns 0x8100 if the frame has a VLAN header,
122
-	// 0x88a8 if the frame has a "Q-in-Q" double VLAN header, or some
123
-	// other value if no VLAN information is present.
124
-	ExtVLANProto Extension = 60
125
-	// ExtRand returns a uniformly random uint32.
126
-	ExtRand Extension = 56
127
-)
128
-
129
-// The following gives names to various bit patterns used in opcode construction.
130
-
131
-const (
132
-	opMaskCls uint16 = 0x7
133
-	// opClsLoad masks
134
-	opMaskLoadDest  = 0x01
135
-	opMaskLoadWidth = 0x18
136
-	opMaskLoadMode  = 0xe0
137
-	// opClsALU
138
-	opMaskOperandSrc = 0x08
139
-	opMaskOperator   = 0xf0
140
-	// opClsJump
141
-	opMaskJumpConst = 0x0f
142
-	opMaskJumpCond  = 0xf0
143
-)
144
-
145
-const (
146
-	// +---------------+-----------------+---+---+---+
147
-	// | AddrMode (3b) | LoadWidth (2b)  | 0 | 0 | 0 |
148
-	// +---------------+-----------------+---+---+---+
149
-	opClsLoadA uint16 = iota
150
-	// +---------------+-----------------+---+---+---+
151
-	// | AddrMode (3b) | LoadWidth (2b)  | 0 | 0 | 1 |
152
-	// +---------------+-----------------+---+---+---+
153
-	opClsLoadX
154
-	// +---+---+---+---+---+---+---+---+
155
-	// | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
156
-	// +---+---+---+---+---+---+---+---+
157
-	opClsStoreA
158
-	// +---+---+---+---+---+---+---+---+
159
-	// | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
160
-	// +---+---+---+---+---+---+---+---+
161
-	opClsStoreX
162
-	// +---------------+-----------------+---+---+---+
163
-	// | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 |
164
-	// +---------------+-----------------+---+---+---+
165
-	opClsALU
166
-	// +-----------------------------+---+---+---+---+
167
-	// |      TestOperator (4b)      | 0 | 1 | 0 | 1 |
168
-	// +-----------------------------+---+---+---+---+
169
-	opClsJump
170
-	// +---+-------------------------+---+---+---+---+
171
-	// | 0 | 0 | 0 |   RetSrc (1b)   | 0 | 1 | 1 | 0 |
172
-	// +---+-------------------------+---+---+---+---+
173
-	opClsReturn
174
-	// +---+-------------------------+---+---+---+---+
175
-	// | 0 | 0 | 0 |  TXAorTAX (1b)  | 0 | 1 | 1 | 1 |
176
-	// +---+-------------------------+---+---+---+---+
177
-	opClsMisc
178
-)
179
-
180
-const (
181
-	opAddrModeImmediate uint16 = iota << 5
182
-	opAddrModeAbsolute
183
-	opAddrModeIndirect
184
-	opAddrModeScratch
185
-	opAddrModePacketLen // actually an extension, not an addressing mode.
186
-	opAddrModeMemShift
187
-)
188
-
189
-const (
190
-	opLoadWidth4 uint16 = iota << 3
191
-	opLoadWidth2
192
-	opLoadWidth1
193
-)
194
-
195
-// Operator defined by ALUOp*
196
-
197
-const (
198
-	opALUSrcConstant uint16 = iota << 3
199
-	opALUSrcX
200
-)
201
-
202
-const (
203
-	opJumpAlways = iota << 4
204
-	opJumpEqual
205
-	opJumpGT
206
-	opJumpGE
207
-	opJumpSet
208
-)
209
-
210
-const (
211
-	opRetSrcConstant uint16 = iota << 4
212
-	opRetSrcA
213
-)
214
-
215
-const (
216
-	opMiscTAX = 0x00
217
-	opMiscTXA = 0x80
218
-)

+ 0
- 82
vendor/golang.org/x/net/bpf/doc.go View File

1
-// Copyright 2016 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
-/*
6
-
7
-Package bpf implements marshaling and unmarshaling of programs for the
8
-Berkeley Packet Filter virtual machine, and provides a Go implementation
9
-of the virtual machine.
10
-
11
-BPF's main use is to specify a packet filter for network taps, so that
12
-the kernel doesn't have to expensively copy every packet it sees to
13
-userspace. However, it's been repurposed to other areas where running
14
-user code in-kernel is needed. For example, Linux's seccomp uses BPF
15
-to apply security policies to system calls. For simplicity, this
16
-documentation refers only to packets, but other uses of BPF have their
17
-own data payloads.
18
-
19
-BPF programs run in a restricted virtual machine. It has almost no
20
-access to kernel functions, and while conditional branches are
21
-allowed, they can only jump forwards, to guarantee that there are no
22
-infinite loops.
23
-
24
-The virtual machine
25
-
26
-The BPF VM is an accumulator machine. Its main register, called
27
-register A, is an implicit source and destination in all arithmetic
28
-and logic operations. The machine also has 16 scratch registers for
29
-temporary storage, and an indirection register (register X) for
30
-indirect memory access. All registers are 32 bits wide.
31
-
32
-Each run of a BPF program is given one packet, which is placed in the
33
-VM's read-only "main memory". LoadAbsolute and LoadIndirect
34
-instructions can fetch up to 32 bits at a time into register A for
35
-examination.
36
-
37
-The goal of a BPF program is to produce and return a verdict (uint32),
38
-which tells the kernel what to do with the packet. In the context of
39
-packet filtering, the returned value is the number of bytes of the
40
-packet to forward to userspace, or 0 to ignore the packet. Other
41
-contexts like seccomp define their own return values.
42
-
43
-In order to simplify programs, attempts to read past the end of the
44
-packet terminate the program execution with a verdict of 0 (ignore
45
-packet). This means that the vast majority of BPF programs don't need
46
-to do any explicit bounds checking.
47
-
48
-In addition to the bytes of the packet, some BPF programs have access
49
-to extensions, which are essentially calls to kernel utility
50
-functions. Currently, the only extensions supported by this package
51
-are the Linux packet filter extensions.
52
-
53
-Examples
54
-
55
-This packet filter selects all ARP packets.
56
-
57
-	bpf.Assemble([]bpf.Instruction{
58
-		// Load "EtherType" field from the ethernet header.
59
-		bpf.LoadAbsolute{Off: 12, Size: 2},
60
-		// Skip over the next instruction if EtherType is not ARP.
61
-		bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x0806, SkipTrue: 1},
62
-		// Verdict is "send up to 4k of the packet to userspace."
63
-		bpf.RetConstant{Val: 4096},
64
-		// Verdict is "ignore packet."
65
-		bpf.RetConstant{Val: 0},
66
-	})
67
-
68
-This packet filter captures a random 1% sample of traffic.
69
-
70
-	bpf.Assemble([]bpf.Instruction{
71
-		// Get a 32-bit random number from the Linux kernel.
72
-		bpf.LoadExtension{Num: bpf.ExtRand},
73
-		// 1% dice roll?
74
-		bpf.JumpIf{Cond: bpf.JumpLessThan, Val: 2^32/100, SkipFalse: 1},
75
-		// Capture.
76
-		bpf.RetConstant{Val: 4096},
77
-		// Ignore.
78
-		bpf.RetConstant{Val: 0},
79
-	})
80
-
81
-*/
82
-package bpf // import "golang.org/x/net/bpf"

+ 0
- 704
vendor/golang.org/x/net/bpf/instructions.go View File

1
-// Copyright 2016 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 bpf
6
-
7
-import "fmt"
8
-
9
-// An Instruction is one instruction executed by the BPF virtual
10
-// machine.
11
-type Instruction interface {
12
-	// Assemble assembles the Instruction into a RawInstruction.
13
-	Assemble() (RawInstruction, error)
14
-}
15
-
16
-// A RawInstruction is a raw BPF virtual machine instruction.
17
-type RawInstruction struct {
18
-	// Operation to execute.
19
-	Op uint16
20
-	// For conditional jump instructions, the number of instructions
21
-	// to skip if the condition is true/false.
22
-	Jt uint8
23
-	Jf uint8
24
-	// Constant parameter. The meaning depends on the Op.
25
-	K uint32
26
-}
27
-
28
-// Assemble implements the Instruction Assemble method.
29
-func (ri RawInstruction) Assemble() (RawInstruction, error) { return ri, nil }
30
-
31
-// Disassemble parses ri into an Instruction and returns it. If ri is
32
-// not recognized by this package, ri itself is returned.
33
-func (ri RawInstruction) Disassemble() Instruction {
34
-	switch ri.Op & opMaskCls {
35
-	case opClsLoadA, opClsLoadX:
36
-		reg := Register(ri.Op & opMaskLoadDest)
37
-		sz := 0
38
-		switch ri.Op & opMaskLoadWidth {
39
-		case opLoadWidth4:
40
-			sz = 4
41
-		case opLoadWidth2:
42
-			sz = 2
43
-		case opLoadWidth1:
44
-			sz = 1
45
-		default:
46
-			return ri
47
-		}
48
-		switch ri.Op & opMaskLoadMode {
49
-		case opAddrModeImmediate:
50
-			if sz != 4 {
51
-				return ri
52
-			}
53
-			return LoadConstant{Dst: reg, Val: ri.K}
54
-		case opAddrModeScratch:
55
-			if sz != 4 || ri.K > 15 {
56
-				return ri
57
-			}
58
-			return LoadScratch{Dst: reg, N: int(ri.K)}
59
-		case opAddrModeAbsolute:
60
-			if ri.K > extOffset+0xffffffff {
61
-				return LoadExtension{Num: Extension(-extOffset + ri.K)}
62
-			}
63
-			return LoadAbsolute{Size: sz, Off: ri.K}
64
-		case opAddrModeIndirect:
65
-			return LoadIndirect{Size: sz, Off: ri.K}
66
-		case opAddrModePacketLen:
67
-			if sz != 4 {
68
-				return ri
69
-			}
70
-			return LoadExtension{Num: ExtLen}
71
-		case opAddrModeMemShift:
72
-			return LoadMemShift{Off: ri.K}
73
-		default:
74
-			return ri
75
-		}
76
-
77
-	case opClsStoreA:
78
-		if ri.Op != opClsStoreA || ri.K > 15 {
79
-			return ri
80
-		}
81
-		return StoreScratch{Src: RegA, N: int(ri.K)}
82
-
83
-	case opClsStoreX:
84
-		if ri.Op != opClsStoreX || ri.K > 15 {
85
-			return ri
86
-		}
87
-		return StoreScratch{Src: RegX, N: int(ri.K)}
88
-
89
-	case opClsALU:
90
-		switch op := ALUOp(ri.Op & opMaskOperator); op {
91
-		case ALUOpAdd, ALUOpSub, ALUOpMul, ALUOpDiv, ALUOpOr, ALUOpAnd, ALUOpShiftLeft, ALUOpShiftRight, ALUOpMod, ALUOpXor:
92
-			if ri.Op&opMaskOperandSrc != 0 {
93
-				return ALUOpX{Op: op}
94
-			}
95
-			return ALUOpConstant{Op: op, Val: ri.K}
96
-		case aluOpNeg:
97
-			return NegateA{}
98
-		default:
99
-			return ri
100
-		}
101
-
102
-	case opClsJump:
103
-		if ri.Op&opMaskJumpConst != opClsJump {
104
-			return ri
105
-		}
106
-		switch ri.Op & opMaskJumpCond {
107
-		case opJumpAlways:
108
-			return Jump{Skip: ri.K}
109
-		case opJumpEqual:
110
-			if ri.Jt == 0 {
111
-				return JumpIf{
112
-					Cond:      JumpNotEqual,
113
-					Val:       ri.K,
114
-					SkipTrue:  ri.Jf,
115
-					SkipFalse: 0,
116
-				}
117
-			}
118
-			return JumpIf{
119
-				Cond:      JumpEqual,
120
-				Val:       ri.K,
121
-				SkipTrue:  ri.Jt,
122
-				SkipFalse: ri.Jf,
123
-			}
124
-		case opJumpGT:
125
-			if ri.Jt == 0 {
126
-				return JumpIf{
127
-					Cond:      JumpLessOrEqual,
128
-					Val:       ri.K,
129
-					SkipTrue:  ri.Jf,
130
-					SkipFalse: 0,
131
-				}
132
-			}
133
-			return JumpIf{
134
-				Cond:      JumpGreaterThan,
135
-				Val:       ri.K,
136
-				SkipTrue:  ri.Jt,
137
-				SkipFalse: ri.Jf,
138
-			}
139
-		case opJumpGE:
140
-			if ri.Jt == 0 {
141
-				return JumpIf{
142
-					Cond:      JumpLessThan,
143
-					Val:       ri.K,
144
-					SkipTrue:  ri.Jf,
145
-					SkipFalse: 0,
146
-				}
147
-			}
148
-			return JumpIf{
149
-				Cond:      JumpGreaterOrEqual,
150
-				Val:       ri.K,
151
-				SkipTrue:  ri.Jt,
152
-				SkipFalse: ri.Jf,
153
-			}
154
-		case opJumpSet:
155
-			return JumpIf{
156
-				Cond:      JumpBitsSet,
157
-				Val:       ri.K,
158
-				SkipTrue:  ri.Jt,
159
-				SkipFalse: ri.Jf,
160
-			}
161
-		default:
162
-			return ri
163
-		}
164
-
165
-	case opClsReturn:
166
-		switch ri.Op {
167
-		case opClsReturn | opRetSrcA:
168
-			return RetA{}
169
-		case opClsReturn | opRetSrcConstant:
170
-			return RetConstant{Val: ri.K}
171
-		default:
172
-			return ri
173
-		}
174
-
175
-	case opClsMisc:
176
-		switch ri.Op {
177
-		case opClsMisc | opMiscTAX:
178
-			return TAX{}
179
-		case opClsMisc | opMiscTXA:
180
-			return TXA{}
181
-		default:
182
-			return ri
183
-		}
184
-
185
-	default:
186
-		panic("unreachable") // switch is exhaustive on the bit pattern
187
-	}
188
-}
189
-
190
-// LoadConstant loads Val into register Dst.
191
-type LoadConstant struct {
192
-	Dst Register
193
-	Val uint32
194
-}
195
-
196
-// Assemble implements the Instruction Assemble method.
197
-func (a LoadConstant) Assemble() (RawInstruction, error) {
198
-	return assembleLoad(a.Dst, 4, opAddrModeImmediate, a.Val)
199
-}
200
-
201
-// String returns the the instruction in assembler notation.
202
-func (a LoadConstant) String() string {
203
-	switch a.Dst {
204
-	case RegA:
205
-		return fmt.Sprintf("ld #%d", a.Val)
206
-	case RegX:
207
-		return fmt.Sprintf("ldx #%d", a.Val)
208
-	default:
209
-		return fmt.Sprintf("unknown instruction: %#v", a)
210
-	}
211
-}
212
-
213
-// LoadScratch loads scratch[N] into register Dst.
214
-type LoadScratch struct {
215
-	Dst Register
216
-	N   int // 0-15
217
-}
218
-
219
-// Assemble implements the Instruction Assemble method.
220
-func (a LoadScratch) Assemble() (RawInstruction, error) {
221
-	if a.N < 0 || a.N > 15 {
222
-		return RawInstruction{}, fmt.Errorf("invalid scratch slot %d", a.N)
223
-	}
224
-	return assembleLoad(a.Dst, 4, opAddrModeScratch, uint32(a.N))
225
-}
226
-
227
-// String returns the the instruction in assembler notation.
228
-func (a LoadScratch) String() string {
229
-	switch a.Dst {
230
-	case RegA:
231
-		return fmt.Sprintf("ld M[%d]", a.N)
232
-	case RegX:
233
-		return fmt.Sprintf("ldx M[%d]", a.N)
234
-	default:
235
-		return fmt.Sprintf("unknown instruction: %#v", a)
236
-	}
237
-}
238
-
239
-// LoadAbsolute loads packet[Off:Off+Size] as an integer value into
240
-// register A.
241
-type LoadAbsolute struct {
242
-	Off  uint32
243
-	Size int // 1, 2 or 4
244
-}
245
-
246
-// Assemble implements the Instruction Assemble method.
247
-func (a LoadAbsolute) Assemble() (RawInstruction, error) {
248
-	return assembleLoad(RegA, a.Size, opAddrModeAbsolute, a.Off)
249
-}
250
-
251
-// String returns the the instruction in assembler notation.
252
-func (a LoadAbsolute) String() string {
253
-	switch a.Size {
254
-	case 1: // byte
255
-		return fmt.Sprintf("ldb [%d]", a.Off)
256
-	case 2: // half word
257
-		return fmt.Sprintf("ldh [%d]", a.Off)
258
-	case 4: // word
259
-		if a.Off > extOffset+0xffffffff {
260
-			return LoadExtension{Num: Extension(a.Off + 0x1000)}.String()
261
-		}
262
-		return fmt.Sprintf("ld [%d]", a.Off)
263
-	default:
264
-		return fmt.Sprintf("unknown instruction: %#v", a)
265
-	}
266
-}
267
-
268
-// LoadIndirect loads packet[X+Off:X+Off+Size] as an integer value
269
-// into register A.
270
-type LoadIndirect struct {
271
-	Off  uint32
272
-	Size int // 1, 2 or 4
273
-}
274
-
275
-// Assemble implements the Instruction Assemble method.
276
-func (a LoadIndirect) Assemble() (RawInstruction, error) {
277
-	return assembleLoad(RegA, a.Size, opAddrModeIndirect, a.Off)
278
-}
279
-
280
-// String returns the the instruction in assembler notation.
281
-func (a LoadIndirect) String() string {
282
-	switch a.Size {
283
-	case 1: // byte
284
-		return fmt.Sprintf("ldb [x + %d]", a.Off)
285
-	case 2: // half word
286
-		return fmt.Sprintf("ldh [x + %d]", a.Off)
287
-	case 4: // word
288
-		return fmt.Sprintf("ld [x + %d]", a.Off)
289
-	default:
290
-		return fmt.Sprintf("unknown instruction: %#v", a)
291
-	}
292
-}
293
-
294
-// LoadMemShift multiplies the first 4 bits of the byte at packet[Off]
295
-// by 4 and stores the result in register X.
296
-//
297
-// This instruction is mainly useful to load into X the length of an
298
-// IPv4 packet header in a single instruction, rather than have to do
299
-// the arithmetic on the header's first byte by hand.
300
-type LoadMemShift struct {
301
-	Off uint32
302
-}
303
-
304
-// Assemble implements the Instruction Assemble method.
305
-func (a LoadMemShift) Assemble() (RawInstruction, error) {
306
-	return assembleLoad(RegX, 1, opAddrModeMemShift, a.Off)
307
-}
308
-
309
-// String returns the the instruction in assembler notation.
310
-func (a LoadMemShift) String() string {
311
-	return fmt.Sprintf("ldx 4*([%d]&0xf)", a.Off)
312
-}
313
-
314
-// LoadExtension invokes a linux-specific extension and stores the
315
-// result in register A.
316
-type LoadExtension struct {
317
-	Num Extension
318
-}
319
-
320
-// Assemble implements the Instruction Assemble method.
321
-func (a LoadExtension) Assemble() (RawInstruction, error) {
322
-	if a.Num == ExtLen {
323
-		return assembleLoad(RegA, 4, opAddrModePacketLen, 0)
324
-	}
325
-	return assembleLoad(RegA, 4, opAddrModeAbsolute, uint32(extOffset+a.Num))
326
-}
327
-
328
-// String returns the the instruction in assembler notation.
329
-func (a LoadExtension) String() string {
330
-	switch a.Num {
331
-	case ExtLen:
332
-		return "ld #len"
333
-	case ExtProto:
334
-		return "ld #proto"
335
-	case ExtType:
336
-		return "ld #type"
337
-	case ExtPayloadOffset:
338
-		return "ld #poff"
339
-	case ExtInterfaceIndex:
340
-		return "ld #ifidx"
341
-	case ExtNetlinkAttr:
342
-		return "ld #nla"
343
-	case ExtNetlinkAttrNested:
344
-		return "ld #nlan"
345
-	case ExtMark:
346
-		return "ld #mark"
347
-	case ExtQueue:
348
-		return "ld #queue"
349
-	case ExtLinkLayerType:
350
-		return "ld #hatype"
351
-	case ExtRXHash:
352
-		return "ld #rxhash"
353
-	case ExtCPUID:
354
-		return "ld #cpu"
355
-	case ExtVLANTag:
356
-		return "ld #vlan_tci"
357
-	case ExtVLANTagPresent:
358
-		return "ld #vlan_avail"
359
-	case ExtVLANProto:
360
-		return "ld #vlan_tpid"
361
-	case ExtRand:
362
-		return "ld #rand"
363
-	default:
364
-		return fmt.Sprintf("unknown instruction: %#v", a)
365
-	}
366
-}
367
-
368
-// StoreScratch stores register Src into scratch[N].
369
-type StoreScratch struct {
370
-	Src Register
371
-	N   int // 0-15
372
-}
373
-
374
-// Assemble implements the Instruction Assemble method.
375
-func (a StoreScratch) Assemble() (RawInstruction, error) {
376
-	if a.N < 0 || a.N > 15 {
377
-		return RawInstruction{}, fmt.Errorf("invalid scratch slot %d", a.N)
378
-	}
379
-	var op uint16
380
-	switch a.Src {
381
-	case RegA:
382
-		op = opClsStoreA
383
-	case RegX:
384
-		op = opClsStoreX
385
-	default:
386
-		return RawInstruction{}, fmt.Errorf("invalid source register %v", a.Src)
387
-	}
388
-
389
-	return RawInstruction{
390
-		Op: op,
391
-		K:  uint32(a.N),
392
-	}, nil
393
-}
394
-
395
-// String returns the the instruction in assembler notation.
396
-func (a StoreScratch) String() string {
397
-	switch a.Src {
398
-	case RegA:
399
-		return fmt.Sprintf("st M[%d]", a.N)
400
-	case RegX:
401
-		return fmt.Sprintf("stx M[%d]", a.N)
402
-	default:
403
-		return fmt.Sprintf("unknown instruction: %#v", a)
404
-	}
405
-}
406
-
407
-// ALUOpConstant executes A = A <Op> Val.
408
-type ALUOpConstant struct {
409
-	Op  ALUOp
410
-	Val uint32
411
-}
412
-
413
-// Assemble implements the Instruction Assemble method.
414
-func (a ALUOpConstant) Assemble() (RawInstruction, error) {
415
-	return RawInstruction{
416
-		Op: opClsALU | opALUSrcConstant | uint16(a.Op),
417
-		K:  a.Val,
418
-	}, nil
419
-}
420
-
421
-// String returns the the instruction in assembler notation.
422
-func (a ALUOpConstant) String() string {
423
-	switch a.Op {
424
-	case ALUOpAdd:
425
-		return fmt.Sprintf("add #%d", a.Val)
426
-	case ALUOpSub:
427
-		return fmt.Sprintf("sub #%d", a.Val)
428
-	case ALUOpMul:
429
-		return fmt.Sprintf("mul #%d", a.Val)
430
-	case ALUOpDiv:
431
-		return fmt.Sprintf("div #%d", a.Val)
432
-	case ALUOpMod:
433
-		return fmt.Sprintf("mod #%d", a.Val)
434
-	case ALUOpAnd:
435
-		return fmt.Sprintf("and #%d", a.Val)
436
-	case ALUOpOr:
437
-		return fmt.Sprintf("or #%d", a.Val)
438
-	case ALUOpXor:
439
-		return fmt.Sprintf("xor #%d", a.Val)
440
-	case ALUOpShiftLeft:
441
-		return fmt.Sprintf("lsh #%d", a.Val)
442
-	case ALUOpShiftRight:
443
-		return fmt.Sprintf("rsh #%d", a.Val)
444
-	default:
445
-		return fmt.Sprintf("unknown instruction: %#v", a)
446
-	}
447
-}
448
-
449
-// ALUOpX executes A = A <Op> X
450
-type ALUOpX struct {
451
-	Op ALUOp
452
-}
453
-
454
-// Assemble implements the Instruction Assemble method.
455
-func (a ALUOpX) Assemble() (RawInstruction, error) {
456
-	return RawInstruction{
457
-		Op: opClsALU | opALUSrcX | uint16(a.Op),
458
-	}, nil
459
-}
460
-
461
-// String returns the the instruction in assembler notation.
462
-func (a ALUOpX) String() string {
463
-	switch a.Op {
464
-	case ALUOpAdd:
465
-		return "add x"
466
-	case ALUOpSub:
467
-		return "sub x"
468
-	case ALUOpMul:
469
-		return "mul x"
470
-	case ALUOpDiv:
471
-		return "div x"
472
-	case ALUOpMod:
473
-		return "mod x"
474
-	case ALUOpAnd:
475
-		return "and x"
476
-	case ALUOpOr:
477
-		return "or x"
478
-	case ALUOpXor:
479
-		return "xor x"
480
-	case ALUOpShiftLeft:
481
-		return "lsh x"
482
-	case ALUOpShiftRight:
483
-		return "rsh x"
484
-	default:
485
-		return fmt.Sprintf("unknown instruction: %#v", a)
486
-	}
487
-}
488
-
489
-// NegateA executes A = -A.
490
-type NegateA struct{}
491
-
492
-// Assemble implements the Instruction Assemble method.
493
-func (a NegateA) Assemble() (RawInstruction, error) {
494
-	return RawInstruction{
495
-		Op: opClsALU | uint16(aluOpNeg),
496
-	}, nil
497
-}
498
-
499
-// String returns the the instruction in assembler notation.
500
-func (a NegateA) String() string {
501
-	return fmt.Sprintf("neg")
502
-}
503
-
504
-// Jump skips the following Skip instructions in the program.
505
-type Jump struct {
506
-	Skip uint32
507
-}
508
-
509
-// Assemble implements the Instruction Assemble method.
510
-func (a Jump) Assemble() (RawInstruction, error) {
511
-	return RawInstruction{
512
-		Op: opClsJump | opJumpAlways,
513
-		K:  a.Skip,
514
-	}, nil
515
-}
516
-
517
-// String returns the the instruction in assembler notation.
518
-func (a Jump) String() string {
519
-	return fmt.Sprintf("ja %d", a.Skip)
520
-}
521
-
522
-// JumpIf skips the following Skip instructions in the program if A
523
-// <Cond> Val is true.
524
-type JumpIf struct {
525
-	Cond      JumpTest
526
-	Val       uint32
527
-	SkipTrue  uint8
528
-	SkipFalse uint8
529
-}
530
-
531
-// Assemble implements the Instruction Assemble method.
532
-func (a JumpIf) Assemble() (RawInstruction, error) {
533
-	var (
534
-		cond uint16
535
-		flip bool
536
-	)
537
-	switch a.Cond {
538
-	case JumpEqual:
539
-		cond = opJumpEqual
540
-	case JumpNotEqual:
541
-		cond, flip = opJumpEqual, true
542
-	case JumpGreaterThan:
543
-		cond = opJumpGT
544
-	case JumpLessThan:
545
-		cond, flip = opJumpGE, true
546
-	case JumpGreaterOrEqual:
547
-		cond = opJumpGE
548
-	case JumpLessOrEqual:
549
-		cond, flip = opJumpGT, true
550
-	case JumpBitsSet:
551
-		cond = opJumpSet
552
-	case JumpBitsNotSet:
553
-		cond, flip = opJumpSet, true
554
-	default:
555
-		return RawInstruction{}, fmt.Errorf("unknown JumpTest %v", a.Cond)
556
-	}
557
-	jt, jf := a.SkipTrue, a.SkipFalse
558
-	if flip {
559
-		jt, jf = jf, jt
560
-	}
561
-	return RawInstruction{
562
-		Op: opClsJump | cond,
563
-		Jt: jt,
564
-		Jf: jf,
565
-		K:  a.Val,
566
-	}, nil
567
-}
568
-
569
-// String returns the the instruction in assembler notation.
570
-func (a JumpIf) String() string {
571
-	switch a.Cond {
572
-	// K == A
573
-	case JumpEqual:
574
-		return conditionalJump(a, "jeq", "jneq")
575
-	// K != A
576
-	case JumpNotEqual:
577
-		return fmt.Sprintf("jneq #%d,%d", a.Val, a.SkipTrue)
578
-	// K > A
579
-	case JumpGreaterThan:
580
-		return conditionalJump(a, "jgt", "jle")
581
-	// K < A
582
-	case JumpLessThan:
583
-		return fmt.Sprintf("jlt #%d,%d", a.Val, a.SkipTrue)
584
-	// K >= A
585
-	case JumpGreaterOrEqual:
586
-		return conditionalJump(a, "jge", "jlt")
587
-	// K <= A
588
-	case JumpLessOrEqual:
589
-		return fmt.Sprintf("jle #%d,%d", a.Val, a.SkipTrue)
590
-	// K & A != 0
591
-	case JumpBitsSet:
592
-		if a.SkipFalse > 0 {
593
-			return fmt.Sprintf("jset #%d,%d,%d", a.Val, a.SkipTrue, a.SkipFalse)
594
-		}
595
-		return fmt.Sprintf("jset #%d,%d", a.Val, a.SkipTrue)
596
-	// K & A == 0, there is no assembler instruction for JumpBitNotSet, use JumpBitSet and invert skips
597
-	case JumpBitsNotSet:
598
-		return JumpIf{Cond: JumpBitsSet, SkipTrue: a.SkipFalse, SkipFalse: a.SkipTrue, Val: a.Val}.String()
599
-	default:
600
-		return fmt.Sprintf("unknown instruction: %#v", a)
601
-	}
602
-}
603
-
604
-func conditionalJump(inst JumpIf, positiveJump, negativeJump string) string {
605
-	if inst.SkipTrue > 0 {
606
-		if inst.SkipFalse > 0 {
607
-			return fmt.Sprintf("%s #%d,%d,%d", positiveJump, inst.Val, inst.SkipTrue, inst.SkipFalse)
608
-		}
609
-		return fmt.Sprintf("%s #%d,%d", positiveJump, inst.Val, inst.SkipTrue)
610
-	}
611
-	return fmt.Sprintf("%s #%d,%d", negativeJump, inst.Val, inst.SkipFalse)
612
-}
613
-
614
-// RetA exits the BPF program, returning the value of register A.
615
-type RetA struct{}
616
-
617
-// Assemble implements the Instruction Assemble method.
618
-func (a RetA) Assemble() (RawInstruction, error) {
619
-	return RawInstruction{
620
-		Op: opClsReturn | opRetSrcA,
621
-	}, nil
622
-}
623
-
624
-// String returns the the instruction in assembler notation.
625
-func (a RetA) String() string {
626
-	return fmt.Sprintf("ret a")
627
-}
628
-
629
-// RetConstant exits the BPF program, returning a constant value.
630
-type RetConstant struct {
631
-	Val uint32
632
-}
633
-
634
-// Assemble implements the Instruction Assemble method.
635
-func (a RetConstant) Assemble() (RawInstruction, error) {
636
-	return RawInstruction{
637
-		Op: opClsReturn | opRetSrcConstant,
638
-		K:  a.Val,
639
-	}, nil
640
-}
641
-
642
-// String returns the the instruction in assembler notation.
643
-func (a RetConstant) String() string {
644
-	return fmt.Sprintf("ret #%d", a.Val)
645
-}
646
-
647
-// TXA copies the value of register X to register A.
648
-type TXA struct{}
649
-
650
-// Assemble implements the Instruction Assemble method.
651
-func (a TXA) Assemble() (RawInstruction, error) {
652
-	return RawInstruction{
653
-		Op: opClsMisc | opMiscTXA,
654
-	}, nil
655
-}
656
-
657
-// String returns the the instruction in assembler notation.
658
-func (a TXA) String() string {
659
-	return fmt.Sprintf("txa")
660
-}
661
-
662
-// TAX copies the value of register A to register X.
663
-type TAX struct{}
664
-
665
-// Assemble implements the Instruction Assemble method.
666
-func (a TAX) Assemble() (RawInstruction, error) {
667
-	return RawInstruction{
668
-		Op: opClsMisc | opMiscTAX,
669
-	}, nil
670
-}
671
-
672
-// String returns the the instruction in assembler notation.
673
-func (a TAX) String() string {
674
-	return fmt.Sprintf("tax")
675
-}
676
-
677
-func assembleLoad(dst Register, loadSize int, mode uint16, k uint32) (RawInstruction, error) {
678
-	var (
679
-		cls uint16
680
-		sz  uint16
681
-	)
682
-	switch dst {
683
-	case RegA:
684
-		cls = opClsLoadA
685
-	case RegX:
686
-		cls = opClsLoadX
687
-	default:
688
-		return RawInstruction{}, fmt.Errorf("invalid target register %v", dst)
689
-	}
690
-	switch loadSize {
691
-	case 1:
692
-		sz = opLoadWidth1
693
-	case 2:
694
-		sz = opLoadWidth2
695
-	case 4:
696
-		sz = opLoadWidth4
697
-	default:
698
-		return RawInstruction{}, fmt.Errorf("invalid load byte length %d", sz)
699
-	}
700
-	return RawInstruction{
701
-		Op: cls | sz | mode,
702
-		K:  k,
703
-	}, nil
704
-}

+ 0
- 525
vendor/golang.org/x/net/bpf/instructions_test.go View File

1
-// Copyright 2016 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 bpf
6
-
7
-import (
8
-	"fmt"
9
-	"io/ioutil"
10
-	"reflect"
11
-	"strconv"
12
-	"strings"
13
-	"testing"
14
-)
15
-
16
-// This is a direct translation of the program in
17
-// testdata/all_instructions.txt.
18
-var allInstructions = []Instruction{
19
-	LoadConstant{Dst: RegA, Val: 42},
20
-	LoadConstant{Dst: RegX, Val: 42},
21
-
22
-	LoadScratch{Dst: RegA, N: 3},
23
-	LoadScratch{Dst: RegX, N: 3},
24
-
25
-	LoadAbsolute{Off: 42, Size: 1},
26
-	LoadAbsolute{Off: 42, Size: 2},
27
-	LoadAbsolute{Off: 42, Size: 4},
28
-
29
-	LoadIndirect{Off: 42, Size: 1},
30
-	LoadIndirect{Off: 42, Size: 2},
31
-	LoadIndirect{Off: 42, Size: 4},
32
-
33
-	LoadMemShift{Off: 42},
34
-
35
-	LoadExtension{Num: ExtLen},
36
-	LoadExtension{Num: ExtProto},
37
-	LoadExtension{Num: ExtType},
38
-	LoadExtension{Num: ExtRand},
39
-
40
-	StoreScratch{Src: RegA, N: 3},
41
-	StoreScratch{Src: RegX, N: 3},
42
-
43
-	ALUOpConstant{Op: ALUOpAdd, Val: 42},
44
-	ALUOpConstant{Op: ALUOpSub, Val: 42},
45
-	ALUOpConstant{Op: ALUOpMul, Val: 42},
46
-	ALUOpConstant{Op: ALUOpDiv, Val: 42},
47
-	ALUOpConstant{Op: ALUOpOr, Val: 42},
48
-	ALUOpConstant{Op: ALUOpAnd, Val: 42},
49
-	ALUOpConstant{Op: ALUOpShiftLeft, Val: 42},
50
-	ALUOpConstant{Op: ALUOpShiftRight, Val: 42},
51
-	ALUOpConstant{Op: ALUOpMod, Val: 42},
52
-	ALUOpConstant{Op: ALUOpXor, Val: 42},
53
-
54
-	ALUOpX{Op: ALUOpAdd},
55
-	ALUOpX{Op: ALUOpSub},
56
-	ALUOpX{Op: ALUOpMul},
57
-	ALUOpX{Op: ALUOpDiv},
58
-	ALUOpX{Op: ALUOpOr},
59
-	ALUOpX{Op: ALUOpAnd},
60
-	ALUOpX{Op: ALUOpShiftLeft},
61
-	ALUOpX{Op: ALUOpShiftRight},
62
-	ALUOpX{Op: ALUOpMod},
63
-	ALUOpX{Op: ALUOpXor},
64
-
65
-	NegateA{},
66
-
67
-	Jump{Skip: 10},
68
-	JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8, SkipFalse: 9},
69
-	JumpIf{Cond: JumpNotEqual, Val: 42, SkipTrue: 8},
70
-	JumpIf{Cond: JumpLessThan, Val: 42, SkipTrue: 7},
71
-	JumpIf{Cond: JumpLessOrEqual, Val: 42, SkipTrue: 6},
72
-	JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4, SkipFalse: 5},
73
-	JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3, SkipFalse: 4},
74
-	JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
75
-
76
-	TAX{},
77
-	TXA{},
78
-
79
-	RetA{},
80
-	RetConstant{Val: 42},
81
-}
82
-var allInstructionsExpected = "testdata/all_instructions.bpf"
83
-
84
-// Check that we produce the same output as the canonical bpf_asm
85
-// linux kernel tool.
86
-func TestInterop(t *testing.T) {
87
-	out, err := Assemble(allInstructions)
88
-	if err != nil {
89
-		t.Fatalf("assembly of allInstructions program failed: %s", err)
90
-	}
91
-	t.Logf("Assembled program is %d instructions long", len(out))
92
-
93
-	bs, err := ioutil.ReadFile(allInstructionsExpected)
94
-	if err != nil {
95
-		t.Fatalf("reading %s: %s", allInstructionsExpected, err)
96
-	}
97
-	// First statement is the number of statements, last statement is
98
-	// empty. We just ignore both and rely on slice length.
99
-	stmts := strings.Split(string(bs), ",")
100
-	if len(stmts)-2 != len(out) {
101
-		t.Fatalf("test program lengths don't match: %s has %d, Go implementation has %d", allInstructionsExpected, len(stmts)-2, len(allInstructions))
102
-	}
103
-
104
-	for i, stmt := range stmts[1 : len(stmts)-2] {
105
-		nums := strings.Split(stmt, " ")
106
-		if len(nums) != 4 {
107
-			t.Fatalf("malformed instruction %d in %s: %s", i+1, allInstructionsExpected, stmt)
108
-		}
109
-
110
-		actual := out[i]
111
-
112
-		op, err := strconv.ParseUint(nums[0], 10, 16)
113
-		if err != nil {
114
-			t.Fatalf("malformed opcode %s in instruction %d of %s", nums[0], i+1, allInstructionsExpected)
115
-		}
116
-		if actual.Op != uint16(op) {
117
-			t.Errorf("opcode mismatch on instruction %d (%#v): got 0x%02x, want 0x%02x", i+1, allInstructions[i], actual.Op, op)
118
-		}
119
-
120
-		jt, err := strconv.ParseUint(nums[1], 10, 8)
121
-		if err != nil {
122
-			t.Fatalf("malformed jt offset %s in instruction %d of %s", nums[1], i+1, allInstructionsExpected)
123
-		}
124
-		if actual.Jt != uint8(jt) {
125
-			t.Errorf("jt mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.Jt, jt)
126
-		}
127
-
128
-		jf, err := strconv.ParseUint(nums[2], 10, 8)
129
-		if err != nil {
130
-			t.Fatalf("malformed jf offset %s in instruction %d of %s", nums[2], i+1, allInstructionsExpected)
131
-		}
132
-		if actual.Jf != uint8(jf) {
133
-			t.Errorf("jf mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.Jf, jf)
134
-		}
135
-
136
-		k, err := strconv.ParseUint(nums[3], 10, 32)
137
-		if err != nil {
138
-			t.Fatalf("malformed constant %s in instruction %d of %s", nums[3], i+1, allInstructionsExpected)
139
-		}
140
-		if actual.K != uint32(k) {
141
-			t.Errorf("constant mismatch on instruction %d (%#v): got %d, want %d", i+1, allInstructions[i], actual.K, k)
142
-		}
143
-	}
144
-}
145
-
146
-// Check that assembly and disassembly match each other.
147
-func TestAsmDisasm(t *testing.T) {
148
-	prog1, err := Assemble(allInstructions)
149
-	if err != nil {
150
-		t.Fatalf("assembly of allInstructions program failed: %s", err)
151
-	}
152
-	t.Logf("Assembled program is %d instructions long", len(prog1))
153
-
154
-	got, allDecoded := Disassemble(prog1)
155
-	if !allDecoded {
156
-		t.Errorf("Disassemble(Assemble(allInstructions)) produced unrecognized instructions:")
157
-		for i, inst := range got {
158
-			if r, ok := inst.(RawInstruction); ok {
159
-				t.Logf("  insn %d, %#v --> %#v", i+1, allInstructions[i], r)
160
-			}
161
-		}
162
-	}
163
-
164
-	if len(allInstructions) != len(got) {
165
-		t.Fatalf("disassembly changed program size: %d insns before, %d insns after", len(allInstructions), len(got))
166
-	}
167
-	if !reflect.DeepEqual(allInstructions, got) {
168
-		t.Errorf("program mutated by disassembly:")
169
-		for i := range got {
170
-			if !reflect.DeepEqual(allInstructions[i], got[i]) {
171
-				t.Logf("  insn %d, s: %#v, p1: %#v, got: %#v", i+1, allInstructions[i], prog1[i], got[i])
172
-			}
173
-		}
174
-	}
175
-}
176
-
177
-type InvalidInstruction struct{}
178
-
179
-func (a InvalidInstruction) Assemble() (RawInstruction, error) {
180
-	return RawInstruction{}, fmt.Errorf("Invalid Instruction")
181
-}
182
-
183
-func (a InvalidInstruction) String() string {
184
-	return fmt.Sprintf("unknown instruction: %#v", a)
185
-}
186
-
187
-func TestString(t *testing.T) {
188
-	testCases := []struct {
189
-		instruction Instruction
190
-		assembler   string
191
-	}{
192
-		{
193
-			instruction: LoadConstant{Dst: RegA, Val: 42},
194
-			assembler:   "ld #42",
195
-		},
196
-		{
197
-			instruction: LoadConstant{Dst: RegX, Val: 42},
198
-			assembler:   "ldx #42",
199
-		},
200
-		{
201
-			instruction: LoadConstant{Dst: 0xffff, Val: 42},
202
-			assembler:   "unknown instruction: bpf.LoadConstant{Dst:0xffff, Val:0x2a}",
203
-		},
204
-		{
205
-			instruction: LoadScratch{Dst: RegA, N: 3},
206
-			assembler:   "ld M[3]",
207
-		},
208
-		{
209
-			instruction: LoadScratch{Dst: RegX, N: 3},
210
-			assembler:   "ldx M[3]",
211
-		},
212
-		{
213
-			instruction: LoadScratch{Dst: 0xffff, N: 3},
214
-			assembler:   "unknown instruction: bpf.LoadScratch{Dst:0xffff, N:3}",
215
-		},
216
-		{
217
-			instruction: LoadAbsolute{Off: 42, Size: 1},
218
-			assembler:   "ldb [42]",
219
-		},
220
-		{
221
-			instruction: LoadAbsolute{Off: 42, Size: 2},
222
-			assembler:   "ldh [42]",
223
-		},
224
-		{
225
-			instruction: LoadAbsolute{Off: 42, Size: 4},
226
-			assembler:   "ld [42]",
227
-		},
228
-		{
229
-			instruction: LoadAbsolute{Off: 42, Size: -1},
230
-			assembler:   "unknown instruction: bpf.LoadAbsolute{Off:0x2a, Size:-1}",
231
-		},
232
-		{
233
-			instruction: LoadIndirect{Off: 42, Size: 1},
234
-			assembler:   "ldb [x + 42]",
235
-		},
236
-		{
237
-			instruction: LoadIndirect{Off: 42, Size: 2},
238
-			assembler:   "ldh [x + 42]",
239
-		},
240
-		{
241
-			instruction: LoadIndirect{Off: 42, Size: 4},
242
-			assembler:   "ld [x + 42]",
243
-		},
244
-		{
245
-			instruction: LoadIndirect{Off: 42, Size: -1},
246
-			assembler:   "unknown instruction: bpf.LoadIndirect{Off:0x2a, Size:-1}",
247
-		},
248
-		{
249
-			instruction: LoadMemShift{Off: 42},
250
-			assembler:   "ldx 4*([42]&0xf)",
251
-		},
252
-		{
253
-			instruction: LoadExtension{Num: ExtLen},
254
-			assembler:   "ld #len",
255
-		},
256
-		{
257
-			instruction: LoadExtension{Num: ExtProto},
258
-			assembler:   "ld #proto",
259
-		},
260
-		{
261
-			instruction: LoadExtension{Num: ExtType},
262
-			assembler:   "ld #type",
263
-		},
264
-		{
265
-			instruction: LoadExtension{Num: ExtPayloadOffset},
266
-			assembler:   "ld #poff",
267
-		},
268
-		{
269
-			instruction: LoadExtension{Num: ExtInterfaceIndex},
270
-			assembler:   "ld #ifidx",
271
-		},
272
-		{
273
-			instruction: LoadExtension{Num: ExtNetlinkAttr},
274
-			assembler:   "ld #nla",
275
-		},
276
-		{
277
-			instruction: LoadExtension{Num: ExtNetlinkAttrNested},
278
-			assembler:   "ld #nlan",
279
-		},
280
-		{
281
-			instruction: LoadExtension{Num: ExtMark},
282
-			assembler:   "ld #mark",
283
-		},
284
-		{
285
-			instruction: LoadExtension{Num: ExtQueue},
286
-			assembler:   "ld #queue",
287
-		},
288
-		{
289
-			instruction: LoadExtension{Num: ExtLinkLayerType},
290
-			assembler:   "ld #hatype",
291
-		},
292
-		{
293
-			instruction: LoadExtension{Num: ExtRXHash},
294
-			assembler:   "ld #rxhash",
295
-		},
296
-		{
297
-			instruction: LoadExtension{Num: ExtCPUID},
298
-			assembler:   "ld #cpu",
299
-		},
300
-		{
301
-			instruction: LoadExtension{Num: ExtVLANTag},
302
-			assembler:   "ld #vlan_tci",
303
-		},
304
-		{
305
-			instruction: LoadExtension{Num: ExtVLANTagPresent},
306
-			assembler:   "ld #vlan_avail",
307
-		},
308
-		{
309
-			instruction: LoadExtension{Num: ExtVLANProto},
310
-			assembler:   "ld #vlan_tpid",
311
-		},
312
-		{
313
-			instruction: LoadExtension{Num: ExtRand},
314
-			assembler:   "ld #rand",
315
-		},
316
-		{
317
-			instruction: LoadAbsolute{Off: 0xfffff038, Size: 4},
318
-			assembler:   "ld #rand",
319
-		},
320
-		{
321
-			instruction: LoadExtension{Num: 0xfff},
322
-			assembler:   "unknown instruction: bpf.LoadExtension{Num:4095}",
323
-		},
324
-		{
325
-			instruction: StoreScratch{Src: RegA, N: 3},
326
-			assembler:   "st M[3]",
327
-		},
328
-		{
329
-			instruction: StoreScratch{Src: RegX, N: 3},
330
-			assembler:   "stx M[3]",
331
-		},
332
-		{
333
-			instruction: StoreScratch{Src: 0xffff, N: 3},
334
-			assembler:   "unknown instruction: bpf.StoreScratch{Src:0xffff, N:3}",
335
-		},
336
-		{
337
-			instruction: ALUOpConstant{Op: ALUOpAdd, Val: 42},
338
-			assembler:   "add #42",
339
-		},
340
-		{
341
-			instruction: ALUOpConstant{Op: ALUOpSub, Val: 42},
342
-			assembler:   "sub #42",
343
-		},
344
-		{
345
-			instruction: ALUOpConstant{Op: ALUOpMul, Val: 42},
346
-			assembler:   "mul #42",
347
-		},
348
-		{
349
-			instruction: ALUOpConstant{Op: ALUOpDiv, Val: 42},
350
-			assembler:   "div #42",
351
-		},
352
-		{
353
-			instruction: ALUOpConstant{Op: ALUOpOr, Val: 42},
354
-			assembler:   "or #42",
355
-		},
356
-		{
357
-			instruction: ALUOpConstant{Op: ALUOpAnd, Val: 42},
358
-			assembler:   "and #42",
359
-		},
360
-		{
361
-			instruction: ALUOpConstant{Op: ALUOpShiftLeft, Val: 42},
362
-			assembler:   "lsh #42",
363
-		},
364
-		{
365
-			instruction: ALUOpConstant{Op: ALUOpShiftRight, Val: 42},
366
-			assembler:   "rsh #42",
367
-		},
368
-		{
369
-			instruction: ALUOpConstant{Op: ALUOpMod, Val: 42},
370
-			assembler:   "mod #42",
371
-		},
372
-		{
373
-			instruction: ALUOpConstant{Op: ALUOpXor, Val: 42},
374
-			assembler:   "xor #42",
375
-		},
376
-		{
377
-			instruction: ALUOpConstant{Op: 0xffff, Val: 42},
378
-			assembler:   "unknown instruction: bpf.ALUOpConstant{Op:0xffff, Val:0x2a}",
379
-		},
380
-		{
381
-			instruction: ALUOpX{Op: ALUOpAdd},
382
-			assembler:   "add x",
383
-		},
384
-		{
385
-			instruction: ALUOpX{Op: ALUOpSub},
386
-			assembler:   "sub x",
387
-		},
388
-		{
389
-			instruction: ALUOpX{Op: ALUOpMul},
390
-			assembler:   "mul x",
391
-		},
392
-		{
393
-			instruction: ALUOpX{Op: ALUOpDiv},
394
-			assembler:   "div x",
395
-		},
396
-		{
397
-			instruction: ALUOpX{Op: ALUOpOr},
398
-			assembler:   "or x",
399
-		},
400
-		{
401
-			instruction: ALUOpX{Op: ALUOpAnd},
402
-			assembler:   "and x",
403
-		},
404
-		{
405
-			instruction: ALUOpX{Op: ALUOpShiftLeft},
406
-			assembler:   "lsh x",
407
-		},
408
-		{
409
-			instruction: ALUOpX{Op: ALUOpShiftRight},
410
-			assembler:   "rsh x",
411
-		},
412
-		{
413
-			instruction: ALUOpX{Op: ALUOpMod},
414
-			assembler:   "mod x",
415
-		},
416
-		{
417
-			instruction: ALUOpX{Op: ALUOpXor},
418
-			assembler:   "xor x",
419
-		},
420
-		{
421
-			instruction: ALUOpX{Op: 0xffff},
422
-			assembler:   "unknown instruction: bpf.ALUOpX{Op:0xffff}",
423
-		},
424
-		{
425
-			instruction: NegateA{},
426
-			assembler:   "neg",
427
-		},
428
-		{
429
-			instruction: Jump{Skip: 10},
430
-			assembler:   "ja 10",
431
-		},
432
-		{
433
-			instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8, SkipFalse: 9},
434
-			assembler:   "jeq #42,8,9",
435
-		},
436
-		{
437
-			instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipTrue: 8},
438
-			assembler:   "jeq #42,8",
439
-		},
440
-		{
441
-			instruction: JumpIf{Cond: JumpEqual, Val: 42, SkipFalse: 8},
442
-			assembler:   "jneq #42,8",
443
-		},
444
-		{
445
-			instruction: JumpIf{Cond: JumpNotEqual, Val: 42, SkipTrue: 8},
446
-			assembler:   "jneq #42,8",
447
-		},
448
-		{
449
-			instruction: JumpIf{Cond: JumpLessThan, Val: 42, SkipTrue: 7},
450
-			assembler:   "jlt #42,7",
451
-		},
452
-		{
453
-			instruction: JumpIf{Cond: JumpLessOrEqual, Val: 42, SkipTrue: 6},
454
-			assembler:   "jle #42,6",
455
-		},
456
-		{
457
-			instruction: JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4, SkipFalse: 5},
458
-			assembler:   "jgt #42,4,5",
459
-		},
460
-		{
461
-			instruction: JumpIf{Cond: JumpGreaterThan, Val: 42, SkipTrue: 4},
462
-			assembler:   "jgt #42,4",
463
-		},
464
-		{
465
-			instruction: JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3, SkipFalse: 4},
466
-			assembler:   "jge #42,3,4",
467
-		},
468
-		{
469
-			instruction: JumpIf{Cond: JumpGreaterOrEqual, Val: 42, SkipTrue: 3},
470
-			assembler:   "jge #42,3",
471
-		},
472
-		{
473
-			instruction: JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
474
-			assembler:   "jset #42,2,3",
475
-		},
476
-		{
477
-			instruction: JumpIf{Cond: JumpBitsSet, Val: 42, SkipTrue: 2},
478
-			assembler:   "jset #42,2",
479
-		},
480
-		{
481
-			instruction: JumpIf{Cond: JumpBitsNotSet, Val: 42, SkipTrue: 2, SkipFalse: 3},
482
-			assembler:   "jset #42,3,2",
483
-		},
484
-		{
485
-			instruction: JumpIf{Cond: JumpBitsNotSet, Val: 42, SkipTrue: 2},
486
-			assembler:   "jset #42,0,2",
487
-		},
488
-		{
489
-			instruction: JumpIf{Cond: 0xffff, Val: 42, SkipTrue: 1, SkipFalse: 2},
490
-			assembler:   "unknown instruction: bpf.JumpIf{Cond:0xffff, Val:0x2a, SkipTrue:0x1, SkipFalse:0x2}",
491
-		},
492
-		{
493
-			instruction: TAX{},
494
-			assembler:   "tax",
495
-		},
496
-		{
497
-			instruction: TXA{},
498
-			assembler:   "txa",
499
-		},
500
-		{
501
-			instruction: RetA{},
502
-			assembler:   "ret a",
503
-		},
504
-		{
505
-			instruction: RetConstant{Val: 42},
506
-			assembler:   "ret #42",
507
-		},
508
-		// Invalid instruction
509
-		{
510
-			instruction: InvalidInstruction{},
511
-			assembler:   "unknown instruction: bpf.InvalidInstruction{}",
512
-		},
513
-	}
514
-
515
-	for _, testCase := range testCases {
516
-		if input, ok := testCase.instruction.(fmt.Stringer); ok {
517
-			got := input.String()
518
-			if got != testCase.assembler {
519
-				t.Errorf("String did not return expected assembler notation, expected: %s, got: %s", testCase.assembler, got)
520
-			}
521
-		} else {
522
-			t.Errorf("Instruction %#v is not a fmt.Stringer", testCase.instruction)
523
-		}
524
-	}
525
-}

+ 0
- 10
vendor/golang.org/x/net/bpf/setter.go View File

1
-// Copyright 2017 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 bpf
6
-
7
-// A Setter is a type which can attach a compiled BPF filter to itself.
8
-type Setter interface {
9
-	SetBPF(filter []RawInstruction) error
10
-}

+ 0
- 1
vendor/golang.org/x/net/bpf/testdata/all_instructions.bpf View File

1
-50,0 0 0 42,1 0 0 42,96 0 0 3,97 0 0 3,48 0 0 42,40 0 0 42,32 0 0 42,80 0 0 42,72 0 0 42,64 0 0 42,177 0 0 42,128 0 0 0,32 0 0 4294963200,32 0 0 4294963204,32 0 0 4294963256,2 0 0 3,3 0 0 3,4 0 0 42,20 0 0 42,36 0 0 42,52 0 0 42,68 0 0 42,84 0 0 42,100 0 0 42,116 0 0 42,148 0 0 42,164 0 0 42,12 0 0 0,28 0 0 0,44 0 0 0,60 0 0 0,76 0 0 0,92 0 0 0,108 0 0 0,124 0 0 0,156 0 0 0,172 0 0 0,132 0 0 0,5 0 0 10,21 8 9 42,21 0 8 42,53 0 7 42,37 0 6 42,37 4 5 42,53 3 4 42,69 2 3 42,7 0 0 0,135 0 0 0,22 0 0 0,6 0 0 0,

+ 0
- 79
vendor/golang.org/x/net/bpf/testdata/all_instructions.txt View File

1
-# This filter is compiled to all_instructions.bpf by the `bpf_asm`
2
-# tool, which can be found in the linux kernel source tree under
3
-# tools/net.
4
-
5
-# Load immediate
6
-ld #42
7
-ldx #42
8
-
9
-# Load scratch
10
-ld M[3]
11
-ldx M[3]
12
-
13
-# Load absolute
14
-ldb [42]
15
-ldh [42]
16
-ld [42]
17
-
18
-# Load indirect
19
-ldb [x + 42]
20
-ldh [x + 42]
21
-ld [x + 42]
22
-
23
-# Load IPv4 header length
24
-ldx 4*([42]&0xf)
25
-
26
-# Run extension function
27
-ld #len
28
-ld #proto
29
-ld #type
30
-ld #rand
31
-
32
-# Store scratch
33
-st M[3]
34
-stx M[3]
35
-
36
-# A <op> constant
37
-add #42
38
-sub #42
39
-mul #42
40
-div #42
41
-or #42
42
-and #42
43
-lsh #42
44
-rsh #42
45
-mod #42
46
-xor #42
47
-
48
-# A <op> X
49
-add x
50
-sub x
51
-mul x
52
-div x
53
-or x
54
-and x
55
-lsh x
56
-rsh x
57
-mod x
58
-xor x
59
-
60
-# !A
61
-neg
62
-
63
-# Jumps
64
-ja end
65
-jeq #42,prev,end
66
-jne #42,end
67
-jlt #42,end
68
-jle #42,end
69
-jgt #42,prev,end
70
-jge #42,prev,end
71
-jset #42,prev,end
72
-
73
-# Register transfers
74
-tax
75
-txa
76
-
77
-# Returns
78
-prev: ret a
79
-end: ret #42

+ 0
- 140
vendor/golang.org/x/net/bpf/vm.go View File

1
-// Copyright 2016 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 bpf
6
-
7
-import (
8
-	"errors"
9
-	"fmt"
10
-)
11
-
12
-// A VM is an emulated BPF virtual machine.
13
-type VM struct {
14
-	filter []Instruction
15
-}
16
-
17
-// NewVM returns a new VM using the input BPF program.
18
-func NewVM(filter []Instruction) (*VM, error) {
19
-	if len(filter) == 0 {
20
-		return nil, errors.New("one or more Instructions must be specified")
21
-	}
22
-
23
-	for i, ins := range filter {
24
-		check := len(filter) - (i + 1)
25
-		switch ins := ins.(type) {
26
-		// Check for out-of-bounds jumps in instructions
27
-		case Jump:
28
-			if check <= int(ins.Skip) {
29
-				return nil, fmt.Errorf("cannot jump %d instructions; jumping past program bounds", ins.Skip)
30
-			}
31
-		case JumpIf:
32
-			if check <= int(ins.SkipTrue) {
33
-				return nil, fmt.Errorf("cannot jump %d instructions in true case; jumping past program bounds", ins.SkipTrue)
34
-			}
35
-			if check <= int(ins.SkipFalse) {
36
-				return nil, fmt.Errorf("cannot jump %d instructions in false case; jumping past program bounds", ins.SkipFalse)
37
-			}
38
-		// Check for division or modulus by zero
39
-		case ALUOpConstant:
40
-			if ins.Val != 0 {
41
-				break
42
-			}
43
-
44
-			switch ins.Op {
45
-			case ALUOpDiv, ALUOpMod:
46
-				return nil, errors.New("cannot divide by zero using ALUOpConstant")
47
-			}
48
-		// Check for unknown extensions
49
-		case LoadExtension:
50
-			switch ins.Num {
51
-			case ExtLen:
52
-			default:
53
-				return nil, fmt.Errorf("extension %d not implemented", ins.Num)
54
-			}
55
-		}
56
-	}
57
-
58
-	// Make sure last instruction is a return instruction
59
-	switch filter[len(filter)-1].(type) {
60
-	case RetA, RetConstant:
61
-	default:
62
-		return nil, errors.New("BPF program must end with RetA or RetConstant")
63
-	}
64
-
65
-	// Though our VM works using disassembled instructions, we
66
-	// attempt to assemble the input filter anyway to ensure it is compatible
67
-	// with an operating system VM.
68
-	_, err := Assemble(filter)
69
-
70
-	return &VM{
71
-		filter: filter,
72
-	}, err
73
-}
74
-
75
-// Run runs the VM's BPF program against the input bytes.
76
-// Run returns the number of bytes accepted by the BPF program, and any errors
77
-// which occurred while processing the program.
78
-func (v *VM) Run(in []byte) (int, error) {
79
-	var (
80
-		// Registers of the virtual machine
81
-		regA       uint32
82
-		regX       uint32
83
-		regScratch [16]uint32
84
-
85
-		// OK is true if the program should continue processing the next
86
-		// instruction, or false if not, causing the loop to break
87
-		ok = true
88
-	)
89
-
90
-	// TODO(mdlayher): implement:
91
-	// - NegateA:
92
-	//   - would require a change from uint32 registers to int32
93
-	//     registers
94
-
95
-	// TODO(mdlayher): add interop tests that check signedness of ALU
96
-	// operations against kernel implementation, and make sure Go
97
-	// implementation matches behavior
98
-
99
-	for i := 0; i < len(v.filter) && ok; i++ {
100
-		ins := v.filter[i]
101
-
102
-		switch ins := ins.(type) {
103
-		case ALUOpConstant:
104
-			regA = aluOpConstant(ins, regA)
105
-		case ALUOpX:
106
-			regA, ok = aluOpX(ins, regA, regX)
107
-		case Jump:
108
-			i += int(ins.Skip)
109
-		case JumpIf:
110
-			jump := jumpIf(ins, regA)
111
-			i += jump
112
-		case LoadAbsolute:
113
-			regA, ok = loadAbsolute(ins, in)
114
-		case LoadConstant:
115
-			regA, regX = loadConstant(ins, regA, regX)
116
-		case LoadExtension:
117
-			regA = loadExtension(ins, in)
118
-		case LoadIndirect:
119
-			regA, ok = loadIndirect(ins, in, regX)
120
-		case LoadMemShift:
121
-			regX, ok = loadMemShift(ins, in)
122
-		case LoadScratch:
123
-			regA, regX = loadScratch(ins, regScratch, regA, regX)
124
-		case RetA:
125
-			return int(regA), nil
126
-		case RetConstant:
127
-			return int(ins.Val), nil
128
-		case StoreScratch:
129
-			regScratch = storeScratch(ins, regScratch, regA, regX)
130
-		case TAX:
131
-			regX = regA
132
-		case TXA:
133
-			regA = regX
134
-		default:
135
-			return 0, fmt.Errorf("unknown Instruction at index %d: %T", i, ins)
136
-		}
137
-	}
138
-
139
-	return 0, nil
140
-}

+ 0
- 512
vendor/golang.org/x/net/bpf/vm_aluop_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"testing"
9
-
10
-	"golang.org/x/net/bpf"
11
-)
12
-
13
-func TestVMALUOpAdd(t *testing.T) {
14
-	vm, done, err := testVM(t, []bpf.Instruction{
15
-		bpf.LoadAbsolute{
16
-			Off:  8,
17
-			Size: 1,
18
-		},
19
-		bpf.ALUOpConstant{
20
-			Op:  bpf.ALUOpAdd,
21
-			Val: 3,
22
-		},
23
-		bpf.RetA{},
24
-	})
25
-	if err != nil {
26
-		t.Fatalf("failed to load BPF program: %v", err)
27
-	}
28
-	defer done()
29
-
30
-	out, err := vm.Run([]byte{
31
-		0xff, 0xff, 0xff, 0xff,
32
-		0xff, 0xff, 0xff, 0xff,
33
-		8, 2, 3,
34
-	})
35
-	if err != nil {
36
-		t.Fatalf("unexpected error while running program: %v", err)
37
-	}
38
-	if want, got := 3, out; want != got {
39
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
40
-			want, got)
41
-	}
42
-}
43
-
44
-func TestVMALUOpSub(t *testing.T) {
45
-	vm, done, err := testVM(t, []bpf.Instruction{
46
-		bpf.LoadAbsolute{
47
-			Off:  8,
48
-			Size: 1,
49
-		},
50
-		bpf.TAX{},
51
-		bpf.ALUOpX{
52
-			Op: bpf.ALUOpSub,
53
-		},
54
-		bpf.RetA{},
55
-	})
56
-	if err != nil {
57
-		t.Fatalf("failed to load BPF program: %v", err)
58
-	}
59
-	defer done()
60
-
61
-	out, err := vm.Run([]byte{
62
-		0xff, 0xff, 0xff, 0xff,
63
-		0xff, 0xff, 0xff, 0xff,
64
-		1, 2, 3,
65
-	})
66
-	if err != nil {
67
-		t.Fatalf("unexpected error while running program: %v", err)
68
-	}
69
-	if want, got := 0, out; want != got {
70
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
71
-			want, got)
72
-	}
73
-}
74
-
75
-func TestVMALUOpMul(t *testing.T) {
76
-	vm, done, err := testVM(t, []bpf.Instruction{
77
-		bpf.LoadAbsolute{
78
-			Off:  8,
79
-			Size: 1,
80
-		},
81
-		bpf.ALUOpConstant{
82
-			Op:  bpf.ALUOpMul,
83
-			Val: 2,
84
-		},
85
-		bpf.RetA{},
86
-	})
87
-	if err != nil {
88
-		t.Fatalf("failed to load BPF program: %v", err)
89
-	}
90
-	defer done()
91
-
92
-	out, err := vm.Run([]byte{
93
-		0xff, 0xff, 0xff, 0xff,
94
-		0xff, 0xff, 0xff, 0xff,
95
-		6, 2, 3, 4,
96
-	})
97
-	if err != nil {
98
-		t.Fatalf("unexpected error while running program: %v", err)
99
-	}
100
-	if want, got := 4, out; want != got {
101
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
102
-			want, got)
103
-	}
104
-}
105
-
106
-func TestVMALUOpDiv(t *testing.T) {
107
-	vm, done, err := testVM(t, []bpf.Instruction{
108
-		bpf.LoadAbsolute{
109
-			Off:  8,
110
-			Size: 1,
111
-		},
112
-		bpf.ALUOpConstant{
113
-			Op:  bpf.ALUOpDiv,
114
-			Val: 2,
115
-		},
116
-		bpf.RetA{},
117
-	})
118
-	if err != nil {
119
-		t.Fatalf("failed to load BPF program: %v", err)
120
-	}
121
-	defer done()
122
-
123
-	out, err := vm.Run([]byte{
124
-		0xff, 0xff, 0xff, 0xff,
125
-		0xff, 0xff, 0xff, 0xff,
126
-		20, 2, 3, 4,
127
-	})
128
-	if err != nil {
129
-		t.Fatalf("unexpected error while running program: %v", err)
130
-	}
131
-	if want, got := 2, out; want != got {
132
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
133
-			want, got)
134
-	}
135
-}
136
-
137
-func TestVMALUOpDivByZeroALUOpConstant(t *testing.T) {
138
-	_, _, err := testVM(t, []bpf.Instruction{
139
-		bpf.ALUOpConstant{
140
-			Op:  bpf.ALUOpDiv,
141
-			Val: 0,
142
-		},
143
-		bpf.RetA{},
144
-	})
145
-	if errStr(err) != "cannot divide by zero using ALUOpConstant" {
146
-		t.Fatalf("unexpected error: %v", err)
147
-	}
148
-}
149
-
150
-func TestVMALUOpDivByZeroALUOpX(t *testing.T) {
151
-	vm, done, err := testVM(t, []bpf.Instruction{
152
-		// Load byte 0 into X
153
-		bpf.LoadAbsolute{
154
-			Off:  8,
155
-			Size: 1,
156
-		},
157
-		bpf.TAX{},
158
-		// Load byte 1 into A
159
-		bpf.LoadAbsolute{
160
-			Off:  9,
161
-			Size: 1,
162
-		},
163
-		// Attempt to perform 1/0
164
-		bpf.ALUOpX{
165
-			Op: bpf.ALUOpDiv,
166
-		},
167
-		// Return 4 bytes if program does not terminate
168
-		bpf.LoadConstant{
169
-			Val: 12,
170
-		},
171
-		bpf.RetA{},
172
-	})
173
-	if err != nil {
174
-		t.Fatalf("failed to load BPF program: %v", err)
175
-	}
176
-	defer done()
177
-
178
-	out, err := vm.Run([]byte{
179
-		0xff, 0xff, 0xff, 0xff,
180
-		0xff, 0xff, 0xff, 0xff,
181
-		0, 1, 3, 4,
182
-	})
183
-	if err != nil {
184
-		t.Fatalf("unexpected error while running program: %v", err)
185
-	}
186
-	if want, got := 0, out; want != got {
187
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
188
-			want, got)
189
-	}
190
-}
191
-
192
-func TestVMALUOpOr(t *testing.T) {
193
-	vm, done, err := testVM(t, []bpf.Instruction{
194
-		bpf.LoadAbsolute{
195
-			Off:  8,
196
-			Size: 2,
197
-		},
198
-		bpf.ALUOpConstant{
199
-			Op:  bpf.ALUOpOr,
200
-			Val: 0x01,
201
-		},
202
-		bpf.RetA{},
203
-	})
204
-	if err != nil {
205
-		t.Fatalf("failed to load BPF program: %v", err)
206
-	}
207
-	defer done()
208
-
209
-	out, err := vm.Run([]byte{
210
-		0xff, 0xff, 0xff, 0xff,
211
-		0xff, 0xff, 0xff, 0xff,
212
-		0x00, 0x10, 0x03, 0x04,
213
-		0x05, 0x06, 0x07, 0x08,
214
-		0x09, 0xff,
215
-	})
216
-	if err != nil {
217
-		t.Fatalf("unexpected error while running program: %v", err)
218
-	}
219
-	if want, got := 9, out; want != got {
220
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
221
-			want, got)
222
-	}
223
-}
224
-
225
-func TestVMALUOpAnd(t *testing.T) {
226
-	vm, done, err := testVM(t, []bpf.Instruction{
227
-		bpf.LoadAbsolute{
228
-			Off:  8,
229
-			Size: 2,
230
-		},
231
-		bpf.ALUOpConstant{
232
-			Op:  bpf.ALUOpAnd,
233
-			Val: 0x0019,
234
-		},
235
-		bpf.RetA{},
236
-	})
237
-	if err != nil {
238
-		t.Fatalf("failed to load BPF program: %v", err)
239
-	}
240
-	defer done()
241
-
242
-	out, err := vm.Run([]byte{
243
-		0xff, 0xff, 0xff, 0xff,
244
-		0xff, 0xff, 0xff, 0xff,
245
-		0xaa, 0x09,
246
-	})
247
-	if err != nil {
248
-		t.Fatalf("unexpected error while running program: %v", err)
249
-	}
250
-	if want, got := 1, out; want != got {
251
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
252
-			want, got)
253
-	}
254
-}
255
-
256
-func TestVMALUOpShiftLeft(t *testing.T) {
257
-	vm, done, err := testVM(t, []bpf.Instruction{
258
-		bpf.LoadAbsolute{
259
-			Off:  8,
260
-			Size: 1,
261
-		},
262
-		bpf.ALUOpConstant{
263
-			Op:  bpf.ALUOpShiftLeft,
264
-			Val: 0x01,
265
-		},
266
-		bpf.JumpIf{
267
-			Cond:     bpf.JumpEqual,
268
-			Val:      0x02,
269
-			SkipTrue: 1,
270
-		},
271
-		bpf.RetConstant{
272
-			Val: 0,
273
-		},
274
-		bpf.RetConstant{
275
-			Val: 9,
276
-		},
277
-	})
278
-	if err != nil {
279
-		t.Fatalf("failed to load BPF program: %v", err)
280
-	}
281
-	defer done()
282
-
283
-	out, err := vm.Run([]byte{
284
-		0xff, 0xff, 0xff, 0xff,
285
-		0xff, 0xff, 0xff, 0xff,
286
-		0x01, 0xaa,
287
-	})
288
-	if err != nil {
289
-		t.Fatalf("unexpected error while running program: %v", err)
290
-	}
291
-	if want, got := 1, out; want != got {
292
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
293
-			want, got)
294
-	}
295
-}
296
-
297
-func TestVMALUOpShiftRight(t *testing.T) {
298
-	vm, done, err := testVM(t, []bpf.Instruction{
299
-		bpf.LoadAbsolute{
300
-			Off:  8,
301
-			Size: 1,
302
-		},
303
-		bpf.ALUOpConstant{
304
-			Op:  bpf.ALUOpShiftRight,
305
-			Val: 0x01,
306
-		},
307
-		bpf.JumpIf{
308
-			Cond:     bpf.JumpEqual,
309
-			Val:      0x04,
310
-			SkipTrue: 1,
311
-		},
312
-		bpf.RetConstant{
313
-			Val: 0,
314
-		},
315
-		bpf.RetConstant{
316
-			Val: 9,
317
-		},
318
-	})
319
-	if err != nil {
320
-		t.Fatalf("failed to load BPF program: %v", err)
321
-	}
322
-	defer done()
323
-
324
-	out, err := vm.Run([]byte{
325
-		0xff, 0xff, 0xff, 0xff,
326
-		0xff, 0xff, 0xff, 0xff,
327
-		0x08, 0xff, 0xff,
328
-	})
329
-	if err != nil {
330
-		t.Fatalf("unexpected error while running program: %v", err)
331
-	}
332
-	if want, got := 1, out; want != got {
333
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
334
-			want, got)
335
-	}
336
-}
337
-
338
-func TestVMALUOpMod(t *testing.T) {
339
-	vm, done, err := testVM(t, []bpf.Instruction{
340
-		bpf.LoadAbsolute{
341
-			Off:  8,
342
-			Size: 1,
343
-		},
344
-		bpf.ALUOpConstant{
345
-			Op:  bpf.ALUOpMod,
346
-			Val: 20,
347
-		},
348
-		bpf.RetA{},
349
-	})
350
-	if err != nil {
351
-		t.Fatalf("failed to load BPF program: %v", err)
352
-	}
353
-	defer done()
354
-
355
-	out, err := vm.Run([]byte{
356
-		0xff, 0xff, 0xff, 0xff,
357
-		0xff, 0xff, 0xff, 0xff,
358
-		30, 0, 0,
359
-	})
360
-	if err != nil {
361
-		t.Fatalf("unexpected error while running program: %v", err)
362
-	}
363
-	if want, got := 2, out; want != got {
364
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
365
-			want, got)
366
-	}
367
-}
368
-
369
-func TestVMALUOpModByZeroALUOpConstant(t *testing.T) {
370
-	_, _, err := testVM(t, []bpf.Instruction{
371
-		bpf.LoadAbsolute{
372
-			Off:  8,
373
-			Size: 1,
374
-		},
375
-		bpf.ALUOpConstant{
376
-			Op:  bpf.ALUOpMod,
377
-			Val: 0,
378
-		},
379
-		bpf.RetA{},
380
-	})
381
-	if errStr(err) != "cannot divide by zero using ALUOpConstant" {
382
-		t.Fatalf("unexpected error: %v", err)
383
-	}
384
-}
385
-
386
-func TestVMALUOpModByZeroALUOpX(t *testing.T) {
387
-	vm, done, err := testVM(t, []bpf.Instruction{
388
-		// Load byte 0 into X
389
-		bpf.LoadAbsolute{
390
-			Off:  8,
391
-			Size: 1,
392
-		},
393
-		bpf.TAX{},
394
-		// Load byte 1 into A
395
-		bpf.LoadAbsolute{
396
-			Off:  9,
397
-			Size: 1,
398
-		},
399
-		// Attempt to perform 1%0
400
-		bpf.ALUOpX{
401
-			Op: bpf.ALUOpMod,
402
-		},
403
-		// Return 4 bytes if program does not terminate
404
-		bpf.LoadConstant{
405
-			Val: 12,
406
-		},
407
-		bpf.RetA{},
408
-	})
409
-	if err != nil {
410
-		t.Fatalf("failed to load BPF program: %v", err)
411
-	}
412
-	defer done()
413
-
414
-	out, err := vm.Run([]byte{
415
-		0xff, 0xff, 0xff, 0xff,
416
-		0xff, 0xff, 0xff, 0xff,
417
-		0, 1, 3, 4,
418
-	})
419
-	if err != nil {
420
-		t.Fatalf("unexpected error while running program: %v", err)
421
-	}
422
-	if want, got := 0, out; want != got {
423
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
424
-			want, got)
425
-	}
426
-}
427
-
428
-func TestVMALUOpXor(t *testing.T) {
429
-	vm, done, err := testVM(t, []bpf.Instruction{
430
-		bpf.LoadAbsolute{
431
-			Off:  8,
432
-			Size: 1,
433
-		},
434
-		bpf.ALUOpConstant{
435
-			Op:  bpf.ALUOpXor,
436
-			Val: 0x0a,
437
-		},
438
-		bpf.JumpIf{
439
-			Cond:     bpf.JumpEqual,
440
-			Val:      0x01,
441
-			SkipTrue: 1,
442
-		},
443
-		bpf.RetConstant{
444
-			Val: 0,
445
-		},
446
-		bpf.RetConstant{
447
-			Val: 9,
448
-		},
449
-	})
450
-	if err != nil {
451
-		t.Fatalf("failed to load BPF program: %v", err)
452
-	}
453
-	defer done()
454
-
455
-	out, err := vm.Run([]byte{
456
-		0xff, 0xff, 0xff, 0xff,
457
-		0xff, 0xff, 0xff, 0xff,
458
-		0x0b, 0x00, 0x00, 0x00,
459
-	})
460
-	if err != nil {
461
-		t.Fatalf("unexpected error while running program: %v", err)
462
-	}
463
-	if want, got := 1, out; want != got {
464
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
465
-			want, got)
466
-	}
467
-}
468
-
469
-func TestVMALUOpUnknown(t *testing.T) {
470
-	vm, done, err := testVM(t, []bpf.Instruction{
471
-		bpf.LoadAbsolute{
472
-			Off:  8,
473
-			Size: 1,
474
-		},
475
-		bpf.ALUOpConstant{
476
-			Op:  bpf.ALUOpAdd,
477
-			Val: 1,
478
-		},
479
-		// Verify that an unknown operation is a no-op
480
-		bpf.ALUOpConstant{
481
-			Op: 100,
482
-		},
483
-		bpf.JumpIf{
484
-			Cond:     bpf.JumpEqual,
485
-			Val:      0x02,
486
-			SkipTrue: 1,
487
-		},
488
-		bpf.RetConstant{
489
-			Val: 0,
490
-		},
491
-		bpf.RetConstant{
492
-			Val: 9,
493
-		},
494
-	})
495
-	if err != nil {
496
-		t.Fatalf("failed to load BPF program: %v", err)
497
-	}
498
-	defer done()
499
-
500
-	out, err := vm.Run([]byte{
501
-		0xff, 0xff, 0xff, 0xff,
502
-		0xff, 0xff, 0xff, 0xff,
503
-		1,
504
-	})
505
-	if err != nil {
506
-		t.Fatalf("unexpected error while running program: %v", err)
507
-	}
508
-	if want, got := 1, out; want != got {
509
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
510
-			want, got)
511
-	}
512
-}

+ 0
- 192
vendor/golang.org/x/net/bpf/vm_bpf_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"net"
9
-	"runtime"
10
-	"testing"
11
-	"time"
12
-
13
-	"golang.org/x/net/bpf"
14
-	"golang.org/x/net/ipv4"
15
-)
16
-
17
-// A virtualMachine is a BPF virtual machine which can process an
18
-// input packet against a BPF program and render a verdict.
19
-type virtualMachine interface {
20
-	Run(in []byte) (int, error)
21
-}
22
-
23
-// canUseOSVM indicates if the OS BPF VM is available on this platform.
24
-func canUseOSVM() bool {
25
-	// OS BPF VM can only be used on platforms where x/net/ipv4 supports
26
-	// attaching a BPF program to a socket.
27
-	switch runtime.GOOS {
28
-	case "linux":
29
-		return true
30
-	}
31
-
32
-	return false
33
-}
34
-
35
-// All BPF tests against both the Go VM and OS VM are assumed to
36
-// be used with a UDP socket. As a result, the entire contents
37
-// of a UDP datagram is sent through the BPF program, but only
38
-// the body after the UDP header will ever be returned in output.
39
-
40
-// testVM sets up a Go BPF VM, and if available, a native OS BPF VM
41
-// for integration testing.
42
-func testVM(t *testing.T, filter []bpf.Instruction) (virtualMachine, func(), error) {
43
-	goVM, err := bpf.NewVM(filter)
44
-	if err != nil {
45
-		// Some tests expect an error, so this error must be returned
46
-		// instead of fatally exiting the test
47
-		return nil, nil, err
48
-	}
49
-
50
-	mvm := &multiVirtualMachine{
51
-		goVM: goVM,
52
-
53
-		t: t,
54
-	}
55
-
56
-	// If available, add the OS VM for tests which verify that both the Go
57
-	// VM and OS VM have exactly the same output for the same input program
58
-	// and packet.
59
-	done := func() {}
60
-	if canUseOSVM() {
61
-		osVM, osVMDone := testOSVM(t, filter)
62
-		done = func() { osVMDone() }
63
-		mvm.osVM = osVM
64
-	}
65
-
66
-	return mvm, done, nil
67
-}
68
-
69
-// udpHeaderLen is the length of a UDP header.
70
-const udpHeaderLen = 8
71
-
72
-// A multiVirtualMachine is a virtualMachine which can call out to both the Go VM
73
-// and the native OS VM, if the OS VM is available.
74
-type multiVirtualMachine struct {
75
-	goVM virtualMachine
76
-	osVM virtualMachine
77
-
78
-	t *testing.T
79
-}
80
-
81
-func (mvm *multiVirtualMachine) Run(in []byte) (int, error) {
82
-	if len(in) < udpHeaderLen {
83
-		mvm.t.Fatalf("input must be at least length of UDP header (%d), got: %d",
84
-			udpHeaderLen, len(in))
85
-	}
86
-
87
-	// All tests have a UDP header as part of input, because the OS VM
88
-	// packets always will. For the Go VM, this output is trimmed before
89
-	// being sent back to tests.
90
-	goOut, goErr := mvm.goVM.Run(in)
91
-	if goOut >= udpHeaderLen {
92
-		goOut -= udpHeaderLen
93
-	}
94
-
95
-	// If Go output is larger than the size of the packet, packet filtering
96
-	// interop tests must trim the output bytes to the length of the packet.
97
-	// The BPF VM should not do this on its own, as other uses of it do
98
-	// not trim the output byte count.
99
-	trim := len(in) - udpHeaderLen
100
-	if goOut > trim {
101
-		goOut = trim
102
-	}
103
-
104
-	// When the OS VM is not available, process using the Go VM alone
105
-	if mvm.osVM == nil {
106
-		return goOut, goErr
107
-	}
108
-
109
-	// The OS VM will apply its own UDP header, so remove the pseudo header
110
-	// that the Go VM needs.
111
-	osOut, err := mvm.osVM.Run(in[udpHeaderLen:])
112
-	if err != nil {
113
-		mvm.t.Fatalf("error while running OS VM: %v", err)
114
-	}
115
-
116
-	// Verify both VMs return same number of bytes
117
-	var mismatch bool
118
-	if goOut != osOut {
119
-		mismatch = true
120
-		mvm.t.Logf("output byte count does not match:\n- go: %v\n- os: %v", goOut, osOut)
121
-	}
122
-
123
-	if mismatch {
124
-		mvm.t.Fatal("Go BPF and OS BPF packet outputs do not match")
125
-	}
126
-
127
-	return goOut, goErr
128
-}
129
-
130
-// An osVirtualMachine is a virtualMachine which uses the OS's BPF VM for
131
-// processing BPF programs.
132
-type osVirtualMachine struct {
133
-	l net.PacketConn
134
-	s net.Conn
135
-}
136
-
137
-// testOSVM creates a virtualMachine which uses the OS's BPF VM by injecting
138
-// packets into a UDP listener with a BPF program attached to it.
139
-func testOSVM(t *testing.T, filter []bpf.Instruction) (virtualMachine, func()) {
140
-	l, err := net.ListenPacket("udp4", "127.0.0.1:0")
141
-	if err != nil {
142
-		t.Fatalf("failed to open OS VM UDP listener: %v", err)
143
-	}
144
-
145
-	prog, err := bpf.Assemble(filter)
146
-	if err != nil {
147
-		t.Fatalf("failed to compile BPF program: %v", err)
148
-	}
149
-
150
-	p := ipv4.NewPacketConn(l)
151
-	if err = p.SetBPF(prog); err != nil {
152
-		t.Fatalf("failed to attach BPF program to listener: %v", err)
153
-	}
154
-
155
-	s, err := net.Dial("udp4", l.LocalAddr().String())
156
-	if err != nil {
157
-		t.Fatalf("failed to dial connection to listener: %v", err)
158
-	}
159
-
160
-	done := func() {
161
-		_ = s.Close()
162
-		_ = l.Close()
163
-	}
164
-
165
-	return &osVirtualMachine{
166
-		l: l,
167
-		s: s,
168
-	}, done
169
-}
170
-
171
-// Run sends the input bytes into the OS's BPF VM and returns its verdict.
172
-func (vm *osVirtualMachine) Run(in []byte) (int, error) {
173
-	go func() {
174
-		_, _ = vm.s.Write(in)
175
-	}()
176
-
177
-	vm.l.SetDeadline(time.Now().Add(50 * time.Millisecond))
178
-
179
-	var b [512]byte
180
-	n, _, err := vm.l.ReadFrom(b[:])
181
-	if err != nil {
182
-		// A timeout indicates that BPF filtered out the packet, and thus,
183
-		// no input should be returned.
184
-		if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
185
-			return n, nil
186
-		}
187
-
188
-		return n, err
189
-	}
190
-
191
-	return n, nil
192
-}

+ 0
- 49
vendor/golang.org/x/net/bpf/vm_extension_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"testing"
9
-
10
-	"golang.org/x/net/bpf"
11
-)
12
-
13
-func TestVMLoadExtensionNotImplemented(t *testing.T) {
14
-	_, _, err := testVM(t, []bpf.Instruction{
15
-		bpf.LoadExtension{
16
-			Num: 100,
17
-		},
18
-		bpf.RetA{},
19
-	})
20
-	if errStr(err) != "extension 100 not implemented" {
21
-		t.Fatalf("unexpected error: %v", err)
22
-	}
23
-}
24
-
25
-func TestVMLoadExtensionExtLen(t *testing.T) {
26
-	vm, done, err := testVM(t, []bpf.Instruction{
27
-		bpf.LoadExtension{
28
-			Num: bpf.ExtLen,
29
-		},
30
-		bpf.RetA{},
31
-	})
32
-	if err != nil {
33
-		t.Fatalf("failed to load BPF program: %v", err)
34
-	}
35
-	defer done()
36
-
37
-	out, err := vm.Run([]byte{
38
-		0xff, 0xff, 0xff, 0xff,
39
-		0xff, 0xff, 0xff, 0xff,
40
-		0, 1, 2, 3,
41
-	})
42
-	if err != nil {
43
-		t.Fatalf("unexpected error while running program: %v", err)
44
-	}
45
-	if want, got := 4, out; want != got {
46
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
47
-			want, got)
48
-	}
49
-}

+ 0
- 174
vendor/golang.org/x/net/bpf/vm_instructions.go View File

1
-// Copyright 2016 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 bpf
6
-
7
-import (
8
-	"encoding/binary"
9
-	"fmt"
10
-)
11
-
12
-func aluOpConstant(ins ALUOpConstant, regA uint32) uint32 {
13
-	return aluOpCommon(ins.Op, regA, ins.Val)
14
-}
15
-
16
-func aluOpX(ins ALUOpX, regA uint32, regX uint32) (uint32, bool) {
17
-	// Guard against division or modulus by zero by terminating
18
-	// the program, as the OS BPF VM does
19
-	if regX == 0 {
20
-		switch ins.Op {
21
-		case ALUOpDiv, ALUOpMod:
22
-			return 0, false
23
-		}
24
-	}
25
-
26
-	return aluOpCommon(ins.Op, regA, regX), true
27
-}
28
-
29
-func aluOpCommon(op ALUOp, regA uint32, value uint32) uint32 {
30
-	switch op {
31
-	case ALUOpAdd:
32
-		return regA + value
33
-	case ALUOpSub:
34
-		return regA - value
35
-	case ALUOpMul:
36
-		return regA * value
37
-	case ALUOpDiv:
38
-		// Division by zero not permitted by NewVM and aluOpX checks
39
-		return regA / value
40
-	case ALUOpOr:
41
-		return regA | value
42
-	case ALUOpAnd:
43
-		return regA & value
44
-	case ALUOpShiftLeft:
45
-		return regA << value
46
-	case ALUOpShiftRight:
47
-		return regA >> value
48
-	case ALUOpMod:
49
-		// Modulus by zero not permitted by NewVM and aluOpX checks
50
-		return regA % value
51
-	case ALUOpXor:
52
-		return regA ^ value
53
-	default:
54
-		return regA
55
-	}
56
-}
57
-
58
-func jumpIf(ins JumpIf, value uint32) int {
59
-	var ok bool
60
-	inV := uint32(ins.Val)
61
-
62
-	switch ins.Cond {
63
-	case JumpEqual:
64
-		ok = value == inV
65
-	case JumpNotEqual:
66
-		ok = value != inV
67
-	case JumpGreaterThan:
68
-		ok = value > inV
69
-	case JumpLessThan:
70
-		ok = value < inV
71
-	case JumpGreaterOrEqual:
72
-		ok = value >= inV
73
-	case JumpLessOrEqual:
74
-		ok = value <= inV
75
-	case JumpBitsSet:
76
-		ok = (value & inV) != 0
77
-	case JumpBitsNotSet:
78
-		ok = (value & inV) == 0
79
-	}
80
-
81
-	if ok {
82
-		return int(ins.SkipTrue)
83
-	}
84
-
85
-	return int(ins.SkipFalse)
86
-}
87
-
88
-func loadAbsolute(ins LoadAbsolute, in []byte) (uint32, bool) {
89
-	offset := int(ins.Off)
90
-	size := int(ins.Size)
91
-
92
-	return loadCommon(in, offset, size)
93
-}
94
-
95
-func loadConstant(ins LoadConstant, regA uint32, regX uint32) (uint32, uint32) {
96
-	switch ins.Dst {
97
-	case RegA:
98
-		regA = ins.Val
99
-	case RegX:
100
-		regX = ins.Val
101
-	}
102
-
103
-	return regA, regX
104
-}
105
-
106
-func loadExtension(ins LoadExtension, in []byte) uint32 {
107
-	switch ins.Num {
108
-	case ExtLen:
109
-		return uint32(len(in))
110
-	default:
111
-		panic(fmt.Sprintf("unimplemented extension: %d", ins.Num))
112
-	}
113
-}
114
-
115
-func loadIndirect(ins LoadIndirect, in []byte, regX uint32) (uint32, bool) {
116
-	offset := int(ins.Off) + int(regX)
117
-	size := int(ins.Size)
118
-
119
-	return loadCommon(in, offset, size)
120
-}
121
-
122
-func loadMemShift(ins LoadMemShift, in []byte) (uint32, bool) {
123
-	offset := int(ins.Off)
124
-
125
-	if !inBounds(len(in), offset, 0) {
126
-		return 0, false
127
-	}
128
-
129
-	// Mask off high 4 bits and multiply low 4 bits by 4
130
-	return uint32(in[offset]&0x0f) * 4, true
131
-}
132
-
133
-func inBounds(inLen int, offset int, size int) bool {
134
-	return offset+size <= inLen
135
-}
136
-
137
-func loadCommon(in []byte, offset int, size int) (uint32, bool) {
138
-	if !inBounds(len(in), offset, size) {
139
-		return 0, false
140
-	}
141
-
142
-	switch size {
143
-	case 1:
144
-		return uint32(in[offset]), true
145
-	case 2:
146
-		return uint32(binary.BigEndian.Uint16(in[offset : offset+size])), true
147
-	case 4:
148
-		return uint32(binary.BigEndian.Uint32(in[offset : offset+size])), true
149
-	default:
150
-		panic(fmt.Sprintf("invalid load size: %d", size))
151
-	}
152
-}
153
-
154
-func loadScratch(ins LoadScratch, regScratch [16]uint32, regA uint32, regX uint32) (uint32, uint32) {
155
-	switch ins.Dst {
156
-	case RegA:
157
-		regA = regScratch[ins.N]
158
-	case RegX:
159
-		regX = regScratch[ins.N]
160
-	}
161
-
162
-	return regA, regX
163
-}
164
-
165
-func storeScratch(ins StoreScratch, regScratch [16]uint32, regA uint32, regX uint32) [16]uint32 {
166
-	switch ins.Src {
167
-	case RegA:
168
-		regScratch[ins.N] = regA
169
-	case RegX:
170
-		regScratch[ins.N] = regX
171
-	}
172
-
173
-	return regScratch
174
-}

+ 0
- 380
vendor/golang.org/x/net/bpf/vm_jump_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"testing"
9
-
10
-	"golang.org/x/net/bpf"
11
-)
12
-
13
-func TestVMJumpOne(t *testing.T) {
14
-	vm, done, err := testVM(t, []bpf.Instruction{
15
-		bpf.LoadAbsolute{
16
-			Off:  8,
17
-			Size: 1,
18
-		},
19
-		bpf.Jump{
20
-			Skip: 1,
21
-		},
22
-		bpf.RetConstant{
23
-			Val: 0,
24
-		},
25
-		bpf.RetConstant{
26
-			Val: 9,
27
-		},
28
-	})
29
-	if err != nil {
30
-		t.Fatalf("failed to load BPF program: %v", err)
31
-	}
32
-	defer done()
33
-
34
-	out, err := vm.Run([]byte{
35
-		0xff, 0xff, 0xff, 0xff,
36
-		0xff, 0xff, 0xff, 0xff,
37
-		1,
38
-	})
39
-	if err != nil {
40
-		t.Fatalf("unexpected error while running program: %v", err)
41
-	}
42
-	if want, got := 1, out; want != got {
43
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
44
-			want, got)
45
-	}
46
-}
47
-
48
-func TestVMJumpOutOfProgram(t *testing.T) {
49
-	_, _, err := testVM(t, []bpf.Instruction{
50
-		bpf.Jump{
51
-			Skip: 1,
52
-		},
53
-		bpf.RetA{},
54
-	})
55
-	if errStr(err) != "cannot jump 1 instructions; jumping past program bounds" {
56
-		t.Fatalf("unexpected error: %v", err)
57
-	}
58
-}
59
-
60
-func TestVMJumpIfTrueOutOfProgram(t *testing.T) {
61
-	_, _, err := testVM(t, []bpf.Instruction{
62
-		bpf.JumpIf{
63
-			Cond:     bpf.JumpEqual,
64
-			SkipTrue: 2,
65
-		},
66
-		bpf.RetA{},
67
-	})
68
-	if errStr(err) != "cannot jump 2 instructions in true case; jumping past program bounds" {
69
-		t.Fatalf("unexpected error: %v", err)
70
-	}
71
-}
72
-
73
-func TestVMJumpIfFalseOutOfProgram(t *testing.T) {
74
-	_, _, err := testVM(t, []bpf.Instruction{
75
-		bpf.JumpIf{
76
-			Cond:      bpf.JumpEqual,
77
-			SkipFalse: 3,
78
-		},
79
-		bpf.RetA{},
80
-	})
81
-	if errStr(err) != "cannot jump 3 instructions in false case; jumping past program bounds" {
82
-		t.Fatalf("unexpected error: %v", err)
83
-	}
84
-}
85
-
86
-func TestVMJumpIfEqual(t *testing.T) {
87
-	vm, done, err := testVM(t, []bpf.Instruction{
88
-		bpf.LoadAbsolute{
89
-			Off:  8,
90
-			Size: 1,
91
-		},
92
-		bpf.JumpIf{
93
-			Cond:     bpf.JumpEqual,
94
-			Val:      1,
95
-			SkipTrue: 1,
96
-		},
97
-		bpf.RetConstant{
98
-			Val: 0,
99
-		},
100
-		bpf.RetConstant{
101
-			Val: 9,
102
-		},
103
-	})
104
-	if err != nil {
105
-		t.Fatalf("failed to load BPF program: %v", err)
106
-	}
107
-	defer done()
108
-
109
-	out, err := vm.Run([]byte{
110
-		0xff, 0xff, 0xff, 0xff,
111
-		0xff, 0xff, 0xff, 0xff,
112
-		1,
113
-	})
114
-	if err != nil {
115
-		t.Fatalf("unexpected error while running program: %v", err)
116
-	}
117
-	if want, got := 1, out; want != got {
118
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
119
-			want, got)
120
-	}
121
-}
122
-
123
-func TestVMJumpIfNotEqual(t *testing.T) {
124
-	vm, done, err := testVM(t, []bpf.Instruction{
125
-		bpf.LoadAbsolute{
126
-			Off:  8,
127
-			Size: 1,
128
-		},
129
-		bpf.JumpIf{
130
-			Cond:      bpf.JumpNotEqual,
131
-			Val:       1,
132
-			SkipFalse: 1,
133
-		},
134
-		bpf.RetConstant{
135
-			Val: 0,
136
-		},
137
-		bpf.RetConstant{
138
-			Val: 9,
139
-		},
140
-	})
141
-	if err != nil {
142
-		t.Fatalf("failed to load BPF program: %v", err)
143
-	}
144
-	defer done()
145
-
146
-	out, err := vm.Run([]byte{
147
-		0xff, 0xff, 0xff, 0xff,
148
-		0xff, 0xff, 0xff, 0xff,
149
-		1,
150
-	})
151
-	if err != nil {
152
-		t.Fatalf("unexpected error while running program: %v", err)
153
-	}
154
-	if want, got := 1, out; want != got {
155
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
156
-			want, got)
157
-	}
158
-}
159
-
160
-func TestVMJumpIfGreaterThan(t *testing.T) {
161
-	vm, done, err := testVM(t, []bpf.Instruction{
162
-		bpf.LoadAbsolute{
163
-			Off:  8,
164
-			Size: 4,
165
-		},
166
-		bpf.JumpIf{
167
-			Cond:     bpf.JumpGreaterThan,
168
-			Val:      0x00010202,
169
-			SkipTrue: 1,
170
-		},
171
-		bpf.RetConstant{
172
-			Val: 0,
173
-		},
174
-		bpf.RetConstant{
175
-			Val: 12,
176
-		},
177
-	})
178
-	if err != nil {
179
-		t.Fatalf("failed to load BPF program: %v", err)
180
-	}
181
-	defer done()
182
-
183
-	out, err := vm.Run([]byte{
184
-		0xff, 0xff, 0xff, 0xff,
185
-		0xff, 0xff, 0xff, 0xff,
186
-		0, 1, 2, 3,
187
-	})
188
-	if err != nil {
189
-		t.Fatalf("unexpected error while running program: %v", err)
190
-	}
191
-	if want, got := 4, out; want != got {
192
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
193
-			want, got)
194
-	}
195
-}
196
-
197
-func TestVMJumpIfLessThan(t *testing.T) {
198
-	vm, done, err := testVM(t, []bpf.Instruction{
199
-		bpf.LoadAbsolute{
200
-			Off:  8,
201
-			Size: 4,
202
-		},
203
-		bpf.JumpIf{
204
-			Cond:     bpf.JumpLessThan,
205
-			Val:      0xff010203,
206
-			SkipTrue: 1,
207
-		},
208
-		bpf.RetConstant{
209
-			Val: 0,
210
-		},
211
-		bpf.RetConstant{
212
-			Val: 12,
213
-		},
214
-	})
215
-	if err != nil {
216
-		t.Fatalf("failed to load BPF program: %v", err)
217
-	}
218
-	defer done()
219
-
220
-	out, err := vm.Run([]byte{
221
-		0xff, 0xff, 0xff, 0xff,
222
-		0xff, 0xff, 0xff, 0xff,
223
-		0, 1, 2, 3,
224
-	})
225
-	if err != nil {
226
-		t.Fatalf("unexpected error while running program: %v", err)
227
-	}
228
-	if want, got := 4, out; want != got {
229
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
230
-			want, got)
231
-	}
232
-}
233
-
234
-func TestVMJumpIfGreaterOrEqual(t *testing.T) {
235
-	vm, done, err := testVM(t, []bpf.Instruction{
236
-		bpf.LoadAbsolute{
237
-			Off:  8,
238
-			Size: 4,
239
-		},
240
-		bpf.JumpIf{
241
-			Cond:     bpf.JumpGreaterOrEqual,
242
-			Val:      0x00010203,
243
-			SkipTrue: 1,
244
-		},
245
-		bpf.RetConstant{
246
-			Val: 0,
247
-		},
248
-		bpf.RetConstant{
249
-			Val: 12,
250
-		},
251
-	})
252
-	if err != nil {
253
-		t.Fatalf("failed to load BPF program: %v", err)
254
-	}
255
-	defer done()
256
-
257
-	out, err := vm.Run([]byte{
258
-		0xff, 0xff, 0xff, 0xff,
259
-		0xff, 0xff, 0xff, 0xff,
260
-		0, 1, 2, 3,
261
-	})
262
-	if err != nil {
263
-		t.Fatalf("unexpected error while running program: %v", err)
264
-	}
265
-	if want, got := 4, out; want != got {
266
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
267
-			want, got)
268
-	}
269
-}
270
-
271
-func TestVMJumpIfLessOrEqual(t *testing.T) {
272
-	vm, done, err := testVM(t, []bpf.Instruction{
273
-		bpf.LoadAbsolute{
274
-			Off:  8,
275
-			Size: 4,
276
-		},
277
-		bpf.JumpIf{
278
-			Cond:     bpf.JumpLessOrEqual,
279
-			Val:      0xff010203,
280
-			SkipTrue: 1,
281
-		},
282
-		bpf.RetConstant{
283
-			Val: 0,
284
-		},
285
-		bpf.RetConstant{
286
-			Val: 12,
287
-		},
288
-	})
289
-	if err != nil {
290
-		t.Fatalf("failed to load BPF program: %v", err)
291
-	}
292
-	defer done()
293
-
294
-	out, err := vm.Run([]byte{
295
-		0xff, 0xff, 0xff, 0xff,
296
-		0xff, 0xff, 0xff, 0xff,
297
-		0, 1, 2, 3,
298
-	})
299
-	if err != nil {
300
-		t.Fatalf("unexpected error while running program: %v", err)
301
-	}
302
-	if want, got := 4, out; want != got {
303
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
304
-			want, got)
305
-	}
306
-}
307
-
308
-func TestVMJumpIfBitsSet(t *testing.T) {
309
-	vm, done, err := testVM(t, []bpf.Instruction{
310
-		bpf.LoadAbsolute{
311
-			Off:  8,
312
-			Size: 2,
313
-		},
314
-		bpf.JumpIf{
315
-			Cond:     bpf.JumpBitsSet,
316
-			Val:      0x1122,
317
-			SkipTrue: 1,
318
-		},
319
-		bpf.RetConstant{
320
-			Val: 0,
321
-		},
322
-		bpf.RetConstant{
323
-			Val: 10,
324
-		},
325
-	})
326
-	if err != nil {
327
-		t.Fatalf("failed to load BPF program: %v", err)
328
-	}
329
-	defer done()
330
-
331
-	out, err := vm.Run([]byte{
332
-		0xff, 0xff, 0xff, 0xff,
333
-		0xff, 0xff, 0xff, 0xff,
334
-		0x01, 0x02,
335
-	})
336
-	if err != nil {
337
-		t.Fatalf("unexpected error while running program: %v", err)
338
-	}
339
-	if want, got := 2, out; want != got {
340
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
341
-			want, got)
342
-	}
343
-}
344
-
345
-func TestVMJumpIfBitsNotSet(t *testing.T) {
346
-	vm, done, err := testVM(t, []bpf.Instruction{
347
-		bpf.LoadAbsolute{
348
-			Off:  8,
349
-			Size: 2,
350
-		},
351
-		bpf.JumpIf{
352
-			Cond:     bpf.JumpBitsNotSet,
353
-			Val:      0x1221,
354
-			SkipTrue: 1,
355
-		},
356
-		bpf.RetConstant{
357
-			Val: 0,
358
-		},
359
-		bpf.RetConstant{
360
-			Val: 10,
361
-		},
362
-	})
363
-	if err != nil {
364
-		t.Fatalf("failed to load BPF program: %v", err)
365
-	}
366
-	defer done()
367
-
368
-	out, err := vm.Run([]byte{
369
-		0xff, 0xff, 0xff, 0xff,
370
-		0xff, 0xff, 0xff, 0xff,
371
-		0x01, 0x02,
372
-	})
373
-	if err != nil {
374
-		t.Fatalf("unexpected error while running program: %v", err)
375
-	}
376
-	if want, got := 2, out; want != got {
377
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
378
-			want, got)
379
-	}
380
-}

+ 0
- 246
vendor/golang.org/x/net/bpf/vm_load_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"net"
9
-	"testing"
10
-
11
-	"golang.org/x/net/bpf"
12
-	"golang.org/x/net/ipv4"
13
-)
14
-
15
-func TestVMLoadAbsoluteOffsetOutOfBounds(t *testing.T) {
16
-	vm, done, err := testVM(t, []bpf.Instruction{
17
-		bpf.LoadAbsolute{
18
-			Off:  100,
19
-			Size: 2,
20
-		},
21
-		bpf.RetA{},
22
-	})
23
-	if err != nil {
24
-		t.Fatalf("failed to load BPF program: %v", err)
25
-	}
26
-	defer done()
27
-
28
-	out, err := vm.Run([]byte{
29
-		0xff, 0xff, 0xff, 0xff,
30
-		0xff, 0xff, 0xff, 0xff,
31
-		0, 1, 2, 3,
32
-	})
33
-	if err != nil {
34
-		t.Fatalf("unexpected error while running program: %v", err)
35
-	}
36
-	if want, got := 0, out; want != got {
37
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
38
-			want, got)
39
-	}
40
-}
41
-
42
-func TestVMLoadAbsoluteOffsetPlusSizeOutOfBounds(t *testing.T) {
43
-	vm, done, err := testVM(t, []bpf.Instruction{
44
-		bpf.LoadAbsolute{
45
-			Off:  8,
46
-			Size: 2,
47
-		},
48
-		bpf.RetA{},
49
-	})
50
-	if err != nil {
51
-		t.Fatalf("failed to load BPF program: %v", err)
52
-	}
53
-	defer done()
54
-
55
-	out, err := vm.Run([]byte{
56
-		0xff, 0xff, 0xff, 0xff,
57
-		0xff, 0xff, 0xff, 0xff,
58
-		0,
59
-	})
60
-	if err != nil {
61
-		t.Fatalf("unexpected error while running program: %v", err)
62
-	}
63
-	if want, got := 0, out; want != got {
64
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
65
-			want, got)
66
-	}
67
-}
68
-
69
-func TestVMLoadAbsoluteBadInstructionSize(t *testing.T) {
70
-	_, _, err := testVM(t, []bpf.Instruction{
71
-		bpf.LoadAbsolute{
72
-			Size: 5,
73
-		},
74
-		bpf.RetA{},
75
-	})
76
-	if errStr(err) != "assembling instruction 1: invalid load byte length 0" {
77
-		t.Fatalf("unexpected error: %v", err)
78
-	}
79
-}
80
-
81
-func TestVMLoadConstantOK(t *testing.T) {
82
-	vm, done, err := testVM(t, []bpf.Instruction{
83
-		bpf.LoadConstant{
84
-			Dst: bpf.RegX,
85
-			Val: 9,
86
-		},
87
-		bpf.TXA{},
88
-		bpf.RetA{},
89
-	})
90
-	if err != nil {
91
-		t.Fatalf("failed to load BPF program: %v", err)
92
-	}
93
-	defer done()
94
-
95
-	out, err := vm.Run([]byte{
96
-		0xff, 0xff, 0xff, 0xff,
97
-		0xff, 0xff, 0xff, 0xff,
98
-		0,
99
-	})
100
-	if err != nil {
101
-		t.Fatalf("unexpected error while running program: %v", err)
102
-	}
103
-	if want, got := 1, out; want != got {
104
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
105
-			want, got)
106
-	}
107
-}
108
-
109
-func TestVMLoadIndirectOutOfBounds(t *testing.T) {
110
-	vm, done, err := testVM(t, []bpf.Instruction{
111
-		bpf.LoadIndirect{
112
-			Off:  100,
113
-			Size: 1,
114
-		},
115
-		bpf.RetA{},
116
-	})
117
-	if err != nil {
118
-		t.Fatalf("failed to load BPF program: %v", err)
119
-	}
120
-	defer done()
121
-
122
-	out, err := vm.Run([]byte{
123
-		0xff, 0xff, 0xff, 0xff,
124
-		0xff, 0xff, 0xff, 0xff,
125
-		0,
126
-	})
127
-	if err != nil {
128
-		t.Fatalf("unexpected error while running program: %v", err)
129
-	}
130
-	if want, got := 0, out; want != got {
131
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
132
-			want, got)
133
-	}
134
-}
135
-
136
-func TestVMLoadMemShiftOutOfBounds(t *testing.T) {
137
-	vm, done, err := testVM(t, []bpf.Instruction{
138
-		bpf.LoadMemShift{
139
-			Off: 100,
140
-		},
141
-		bpf.RetA{},
142
-	})
143
-	if err != nil {
144
-		t.Fatalf("failed to load BPF program: %v", err)
145
-	}
146
-	defer done()
147
-
148
-	out, err := vm.Run([]byte{
149
-		0xff, 0xff, 0xff, 0xff,
150
-		0xff, 0xff, 0xff, 0xff,
151
-		0,
152
-	})
153
-	if err != nil {
154
-		t.Fatalf("unexpected error while running program: %v", err)
155
-	}
156
-	if want, got := 0, out; want != got {
157
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
158
-			want, got)
159
-	}
160
-}
161
-
162
-const (
163
-	dhcp4Port = 53
164
-)
165
-
166
-func TestVMLoadMemShiftLoadIndirectNoResult(t *testing.T) {
167
-	vm, in, done := testDHCPv4(t)
168
-	defer done()
169
-
170
-	// Append mostly empty UDP header with incorrect DHCPv4 port
171
-	in = append(in, []byte{
172
-		0, 0,
173
-		0, dhcp4Port + 1,
174
-		0, 0,
175
-		0, 0,
176
-	}...)
177
-
178
-	out, err := vm.Run(in)
179
-	if err != nil {
180
-		t.Fatalf("unexpected error while running program: %v", err)
181
-	}
182
-	if want, got := 0, out; want != got {
183
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
184
-			want, got)
185
-	}
186
-}
187
-
188
-func TestVMLoadMemShiftLoadIndirectOK(t *testing.T) {
189
-	vm, in, done := testDHCPv4(t)
190
-	defer done()
191
-
192
-	// Append mostly empty UDP header with correct DHCPv4 port
193
-	in = append(in, []byte{
194
-		0, 0,
195
-		0, dhcp4Port,
196
-		0, 0,
197
-		0, 0,
198
-	}...)
199
-
200
-	out, err := vm.Run(in)
201
-	if err != nil {
202
-		t.Fatalf("unexpected error while running program: %v", err)
203
-	}
204
-	if want, got := len(in)-8, out; want != got {
205
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
206
-			want, got)
207
-	}
208
-}
209
-
210
-func testDHCPv4(t *testing.T) (virtualMachine, []byte, func()) {
211
-	// DHCPv4 test data courtesy of David Anderson:
212
-	// https://github.com/google/netboot/blob/master/dhcp4/conn_linux.go#L59-L70
213
-	vm, done, err := testVM(t, []bpf.Instruction{
214
-		// Load IPv4 packet length
215
-		bpf.LoadMemShift{Off: 8},
216
-		// Get UDP dport
217
-		bpf.LoadIndirect{Off: 8 + 2, Size: 2},
218
-		// Correct dport?
219
-		bpf.JumpIf{Cond: bpf.JumpEqual, Val: dhcp4Port, SkipFalse: 1},
220
-		// Accept
221
-		bpf.RetConstant{Val: 1500},
222
-		// Ignore
223
-		bpf.RetConstant{Val: 0},
224
-	})
225
-	if err != nil {
226
-		t.Fatalf("failed to load BPF program: %v", err)
227
-	}
228
-
229
-	// Minimal requirements to make a valid IPv4 header
230
-	h := &ipv4.Header{
231
-		Len: ipv4.HeaderLen,
232
-		Src: net.IPv4(192, 168, 1, 1),
233
-		Dst: net.IPv4(192, 168, 1, 2),
234
-	}
235
-	hb, err := h.Marshal()
236
-	if err != nil {
237
-		t.Fatalf("failed to marshal IPv4 header: %v", err)
238
-	}
239
-
240
-	hb = append([]byte{
241
-		0xff, 0xff, 0xff, 0xff,
242
-		0xff, 0xff, 0xff, 0xff,
243
-	}, hb...)
244
-
245
-	return vm, hb, done
246
-}

+ 0
- 115
vendor/golang.org/x/net/bpf/vm_ret_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"testing"
9
-
10
-	"golang.org/x/net/bpf"
11
-)
12
-
13
-func TestVMRetA(t *testing.T) {
14
-	vm, done, err := testVM(t, []bpf.Instruction{
15
-		bpf.LoadAbsolute{
16
-			Off:  8,
17
-			Size: 1,
18
-		},
19
-		bpf.RetA{},
20
-	})
21
-	if err != nil {
22
-		t.Fatalf("failed to load BPF program: %v", err)
23
-	}
24
-	defer done()
25
-
26
-	out, err := vm.Run([]byte{
27
-		0xff, 0xff, 0xff, 0xff,
28
-		0xff, 0xff, 0xff, 0xff,
29
-		9,
30
-	})
31
-	if err != nil {
32
-		t.Fatalf("unexpected error while running program: %v", err)
33
-	}
34
-	if want, got := 1, out; want != got {
35
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
36
-			want, got)
37
-	}
38
-}
39
-
40
-func TestVMRetALargerThanInput(t *testing.T) {
41
-	vm, done, err := testVM(t, []bpf.Instruction{
42
-		bpf.LoadAbsolute{
43
-			Off:  8,
44
-			Size: 2,
45
-		},
46
-		bpf.RetA{},
47
-	})
48
-	if err != nil {
49
-		t.Fatalf("failed to load BPF program: %v", err)
50
-	}
51
-	defer done()
52
-
53
-	out, err := vm.Run([]byte{
54
-		0xff, 0xff, 0xff, 0xff,
55
-		0xff, 0xff, 0xff, 0xff,
56
-		0, 255,
57
-	})
58
-	if err != nil {
59
-		t.Fatalf("unexpected error while running program: %v", err)
60
-	}
61
-	if want, got := 2, out; want != got {
62
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
63
-			want, got)
64
-	}
65
-}
66
-
67
-func TestVMRetConstant(t *testing.T) {
68
-	vm, done, err := testVM(t, []bpf.Instruction{
69
-		bpf.RetConstant{
70
-			Val: 9,
71
-		},
72
-	})
73
-	if err != nil {
74
-		t.Fatalf("failed to load BPF program: %v", err)
75
-	}
76
-	defer done()
77
-
78
-	out, err := vm.Run([]byte{
79
-		0xff, 0xff, 0xff, 0xff,
80
-		0xff, 0xff, 0xff, 0xff,
81
-		0, 1,
82
-	})
83
-	if err != nil {
84
-		t.Fatalf("unexpected error while running program: %v", err)
85
-	}
86
-	if want, got := 1, out; want != got {
87
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
88
-			want, got)
89
-	}
90
-}
91
-
92
-func TestVMRetConstantLargerThanInput(t *testing.T) {
93
-	vm, done, err := testVM(t, []bpf.Instruction{
94
-		bpf.RetConstant{
95
-			Val: 16,
96
-		},
97
-	})
98
-	if err != nil {
99
-		t.Fatalf("failed to load BPF program: %v", err)
100
-	}
101
-	defer done()
102
-
103
-	out, err := vm.Run([]byte{
104
-		0xff, 0xff, 0xff, 0xff,
105
-		0xff, 0xff, 0xff, 0xff,
106
-		0, 1,
107
-	})
108
-	if err != nil {
109
-		t.Fatalf("unexpected error while running program: %v", err)
110
-	}
111
-	if want, got := 2, out; want != got {
112
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
113
-			want, got)
114
-	}
115
-}

+ 0
- 247
vendor/golang.org/x/net/bpf/vm_scratch_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"testing"
9
-
10
-	"golang.org/x/net/bpf"
11
-)
12
-
13
-func TestVMStoreScratchInvalidScratchRegisterTooSmall(t *testing.T) {
14
-	_, _, err := testVM(t, []bpf.Instruction{
15
-		bpf.StoreScratch{
16
-			Src: bpf.RegA,
17
-			N:   -1,
18
-		},
19
-		bpf.RetA{},
20
-	})
21
-	if errStr(err) != "assembling instruction 1: invalid scratch slot -1" {
22
-		t.Fatalf("unexpected error: %v", err)
23
-	}
24
-}
25
-
26
-func TestVMStoreScratchInvalidScratchRegisterTooLarge(t *testing.T) {
27
-	_, _, err := testVM(t, []bpf.Instruction{
28
-		bpf.StoreScratch{
29
-			Src: bpf.RegA,
30
-			N:   16,
31
-		},
32
-		bpf.RetA{},
33
-	})
34
-	if errStr(err) != "assembling instruction 1: invalid scratch slot 16" {
35
-		t.Fatalf("unexpected error: %v", err)
36
-	}
37
-}
38
-
39
-func TestVMStoreScratchUnknownSourceRegister(t *testing.T) {
40
-	_, _, err := testVM(t, []bpf.Instruction{
41
-		bpf.StoreScratch{
42
-			Src: 100,
43
-			N:   0,
44
-		},
45
-		bpf.RetA{},
46
-	})
47
-	if errStr(err) != "assembling instruction 1: invalid source register 100" {
48
-		t.Fatalf("unexpected error: %v", err)
49
-	}
50
-}
51
-
52
-func TestVMLoadScratchInvalidScratchRegisterTooSmall(t *testing.T) {
53
-	_, _, err := testVM(t, []bpf.Instruction{
54
-		bpf.LoadScratch{
55
-			Dst: bpf.RegX,
56
-			N:   -1,
57
-		},
58
-		bpf.RetA{},
59
-	})
60
-	if errStr(err) != "assembling instruction 1: invalid scratch slot -1" {
61
-		t.Fatalf("unexpected error: %v", err)
62
-	}
63
-}
64
-
65
-func TestVMLoadScratchInvalidScratchRegisterTooLarge(t *testing.T) {
66
-	_, _, err := testVM(t, []bpf.Instruction{
67
-		bpf.LoadScratch{
68
-			Dst: bpf.RegX,
69
-			N:   16,
70
-		},
71
-		bpf.RetA{},
72
-	})
73
-	if errStr(err) != "assembling instruction 1: invalid scratch slot 16" {
74
-		t.Fatalf("unexpected error: %v", err)
75
-	}
76
-}
77
-
78
-func TestVMLoadScratchUnknownDestinationRegister(t *testing.T) {
79
-	_, _, err := testVM(t, []bpf.Instruction{
80
-		bpf.LoadScratch{
81
-			Dst: 100,
82
-			N:   0,
83
-		},
84
-		bpf.RetA{},
85
-	})
86
-	if errStr(err) != "assembling instruction 1: invalid target register 100" {
87
-		t.Fatalf("unexpected error: %v", err)
88
-	}
89
-}
90
-
91
-func TestVMStoreScratchLoadScratchOneValue(t *testing.T) {
92
-	vm, done, err := testVM(t, []bpf.Instruction{
93
-		// Load byte 255
94
-		bpf.LoadAbsolute{
95
-			Off:  8,
96
-			Size: 1,
97
-		},
98
-		// Copy to X and store in scratch[0]
99
-		bpf.TAX{},
100
-		bpf.StoreScratch{
101
-			Src: bpf.RegX,
102
-			N:   0,
103
-		},
104
-		// Load byte 1
105
-		bpf.LoadAbsolute{
106
-			Off:  9,
107
-			Size: 1,
108
-		},
109
-		// Overwrite 1 with 255 from scratch[0]
110
-		bpf.LoadScratch{
111
-			Dst: bpf.RegA,
112
-			N:   0,
113
-		},
114
-		// Return 255
115
-		bpf.RetA{},
116
-	})
117
-	if err != nil {
118
-		t.Fatalf("failed to load BPF program: %v", err)
119
-	}
120
-	defer done()
121
-
122
-	out, err := vm.Run([]byte{
123
-		0xff, 0xff, 0xff, 0xff,
124
-		0xff, 0xff, 0xff, 0xff,
125
-		255, 1, 2,
126
-	})
127
-	if err != nil {
128
-		t.Fatalf("unexpected error while running program: %v", err)
129
-	}
130
-	if want, got := 3, out; want != got {
131
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
132
-			want, got)
133
-	}
134
-}
135
-
136
-func TestVMStoreScratchLoadScratchMultipleValues(t *testing.T) {
137
-	vm, done, err := testVM(t, []bpf.Instruction{
138
-		// Load byte 10
139
-		bpf.LoadAbsolute{
140
-			Off:  8,
141
-			Size: 1,
142
-		},
143
-		// Store in scratch[0]
144
-		bpf.StoreScratch{
145
-			Src: bpf.RegA,
146
-			N:   0,
147
-		},
148
-		// Load byte 20
149
-		bpf.LoadAbsolute{
150
-			Off:  9,
151
-			Size: 1,
152
-		},
153
-		// Store in scratch[1]
154
-		bpf.StoreScratch{
155
-			Src: bpf.RegA,
156
-			N:   1,
157
-		},
158
-		// Load byte 30
159
-		bpf.LoadAbsolute{
160
-			Off:  10,
161
-			Size: 1,
162
-		},
163
-		// Store in scratch[2]
164
-		bpf.StoreScratch{
165
-			Src: bpf.RegA,
166
-			N:   2,
167
-		},
168
-		// Load byte 1
169
-		bpf.LoadAbsolute{
170
-			Off:  11,
171
-			Size: 1,
172
-		},
173
-		// Store in scratch[3]
174
-		bpf.StoreScratch{
175
-			Src: bpf.RegA,
176
-			N:   3,
177
-		},
178
-		// Load in byte 10 to X
179
-		bpf.LoadScratch{
180
-			Dst: bpf.RegX,
181
-			N:   0,
182
-		},
183
-		// Copy X -> A
184
-		bpf.TXA{},
185
-		// Verify value is 10
186
-		bpf.JumpIf{
187
-			Cond:     bpf.JumpEqual,
188
-			Val:      10,
189
-			SkipTrue: 1,
190
-		},
191
-		// Fail test if incorrect
192
-		bpf.RetConstant{
193
-			Val: 0,
194
-		},
195
-		// Load in byte 20 to A
196
-		bpf.LoadScratch{
197
-			Dst: bpf.RegA,
198
-			N:   1,
199
-		},
200
-		// Verify value is 20
201
-		bpf.JumpIf{
202
-			Cond:     bpf.JumpEqual,
203
-			Val:      20,
204
-			SkipTrue: 1,
205
-		},
206
-		// Fail test if incorrect
207
-		bpf.RetConstant{
208
-			Val: 0,
209
-		},
210
-		// Load in byte 30 to A
211
-		bpf.LoadScratch{
212
-			Dst: bpf.RegA,
213
-			N:   2,
214
-		},
215
-		// Verify value is 30
216
-		bpf.JumpIf{
217
-			Cond:     bpf.JumpEqual,
218
-			Val:      30,
219
-			SkipTrue: 1,
220
-		},
221
-		// Fail test if incorrect
222
-		bpf.RetConstant{
223
-			Val: 0,
224
-		},
225
-		// Return first two bytes on success
226
-		bpf.RetConstant{
227
-			Val: 10,
228
-		},
229
-	})
230
-	if err != nil {
231
-		t.Fatalf("failed to load BPF program: %v", err)
232
-	}
233
-	defer done()
234
-
235
-	out, err := vm.Run([]byte{
236
-		0xff, 0xff, 0xff, 0xff,
237
-		0xff, 0xff, 0xff, 0xff,
238
-		10, 20, 30, 1,
239
-	})
240
-	if err != nil {
241
-		t.Fatalf("unexpected error while running program: %v", err)
242
-	}
243
-	if want, got := 2, out; want != got {
244
-		t.Fatalf("unexpected number of output bytes:\n- want: %d\n-  got: %d",
245
-			want, got)
246
-	}
247
-}

+ 0
- 144
vendor/golang.org/x/net/bpf/vm_test.go View File

1
-// Copyright 2016 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 bpf_test
6
-
7
-import (
8
-	"fmt"
9
-	"testing"
10
-
11
-	"golang.org/x/net/bpf"
12
-)
13
-
14
-var _ bpf.Instruction = unknown{}
15
-
16
-type unknown struct{}
17
-
18
-func (unknown) Assemble() (bpf.RawInstruction, error) {
19
-	return bpf.RawInstruction{}, nil
20
-}
21
-
22
-func TestVMUnknownInstruction(t *testing.T) {
23
-	vm, done, err := testVM(t, []bpf.Instruction{
24
-		bpf.LoadConstant{
25
-			Dst: bpf.RegA,
26
-			Val: 100,
27
-		},
28
-		// Should terminate the program with an error immediately
29
-		unknown{},
30
-		bpf.RetA{},
31
-	})
32
-	if err != nil {
33
-		t.Fatalf("unexpected error: %v", err)
34
-	}
35
-	defer done()
36
-
37
-	_, err = vm.Run([]byte{
38
-		0xff, 0xff, 0xff, 0xff,
39
-		0xff, 0xff, 0xff, 0xff,
40
-		0x00, 0x00,
41
-	})
42
-	if errStr(err) != "unknown Instruction at index 1: bpf_test.unknown" {
43
-		t.Fatalf("unexpected error while running program: %v", err)
44
-	}
45
-}
46
-
47
-func TestVMNoReturnInstruction(t *testing.T) {
48
-	_, _, err := testVM(t, []bpf.Instruction{
49
-		bpf.LoadConstant{
50
-			Dst: bpf.RegA,
51
-			Val: 1,
52
-		},
53
-	})
54
-	if errStr(err) != "BPF program must end with RetA or RetConstant" {
55
-		t.Fatalf("unexpected error: %v", err)
56
-	}
57
-}
58
-
59
-func TestVMNoInputInstructions(t *testing.T) {
60
-	_, _, err := testVM(t, []bpf.Instruction{})
61
-	if errStr(err) != "one or more Instructions must be specified" {
62
-		t.Fatalf("unexpected error: %v", err)
63
-	}
64
-}
65
-
66
-// ExampleNewVM demonstrates usage of a VM, using an Ethernet frame
67
-// as input and checking its EtherType to determine if it should be accepted.
68
-func ExampleNewVM() {
69
-	// Offset | Length | Comment
70
-	// -------------------------
71
-	//   00   |   06   | Ethernet destination MAC address
72
-	//   06   |   06   | Ethernet source MAC address
73
-	//   12   |   02   | Ethernet EtherType
74
-	const (
75
-		etOff = 12
76
-		etLen = 2
77
-
78
-		etARP = 0x0806
79
-	)
80
-
81
-	// Set up a VM to filter traffic based on if its EtherType
82
-	// matches the ARP EtherType.
83
-	vm, err := bpf.NewVM([]bpf.Instruction{
84
-		// Load EtherType value from Ethernet header
85
-		bpf.LoadAbsolute{
86
-			Off:  etOff,
87
-			Size: etLen,
88
-		},
89
-		// If EtherType is equal to the ARP EtherType, jump to allow
90
-		// packet to be accepted
91
-		bpf.JumpIf{
92
-			Cond:     bpf.JumpEqual,
93
-			Val:      etARP,
94
-			SkipTrue: 1,
95
-		},
96
-		// EtherType does not match the ARP EtherType
97
-		bpf.RetConstant{
98
-			Val: 0,
99
-		},
100
-		// EtherType matches the ARP EtherType, accept up to 1500
101
-		// bytes of packet
102
-		bpf.RetConstant{
103
-			Val: 1500,
104
-		},
105
-	})
106
-	if err != nil {
107
-		panic(fmt.Sprintf("failed to load BPF program: %v", err))
108
-	}
109
-
110
-	// Create an Ethernet frame with the ARP EtherType for testing
111
-	frame := []byte{
112
-		0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
113
-		0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
114
-		0x08, 0x06,
115
-		// Payload omitted for brevity
116
-	}
117
-
118
-	// Run our VM's BPF program using the Ethernet frame as input
119
-	out, err := vm.Run(frame)
120
-	if err != nil {
121
-		panic(fmt.Sprintf("failed to accept Ethernet frame: %v", err))
122
-	}
123
-
124
-	// BPF VM can return a byte count greater than the number of input
125
-	// bytes, so trim the output to match the input byte length
126
-	if out > len(frame) {
127
-		out = len(frame)
128
-	}
129
-
130
-	fmt.Printf("out: %d bytes", out)
131
-
132
-	// Output:
133
-	// out: 14 bytes
134
-}
135
-
136
-// errStr returns the string representation of an error, or
137
-// "<nil>" if it is nil.
138
-func errStr(err error) string {
139
-	if err == nil {
140
-		return "<nil>"
141
-	}
142
-
143
-	return err.Error()
144
-}

+ 0
- 54
vendor/golang.org/x/net/context/context.go View File

1
-// Copyright 2014 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 context defines the Context type, which carries deadlines,
6
-// cancelation signals, and other request-scoped values across API boundaries
7
-// and between processes.
8
-//
9
-// Incoming requests to a server should create a Context, and outgoing calls to
10
-// servers should accept a Context. The chain of function calls between must
11
-// propagate the Context, optionally replacing it with a modified copy created
12
-// using WithDeadline, WithTimeout, WithCancel, or WithValue.
13
-//
14
-// Programs that use Contexts should follow these rules to keep interfaces
15
-// consistent across packages and enable static analysis tools to check context
16
-// propagation:
17
-//
18
-// Do not store Contexts inside a struct type; instead, pass a Context
19
-// explicitly to each function that needs it. The Context should be the first
20
-// parameter, typically named ctx:
21
-//
22
-// 	func DoSomething(ctx context.Context, arg Arg) error {
23
-// 		// ... use ctx ...
24
-// 	}
25
-//
26
-// Do not pass a nil Context, even if a function permits it. Pass context.TODO
27
-// if you are unsure about which Context to use.
28
-//
29
-// Use context Values only for request-scoped data that transits processes and
30
-// APIs, not for passing optional parameters to functions.
31
-//
32
-// The same Context may be passed to functions running in different goroutines;
33
-// Contexts are safe for simultaneous use by multiple goroutines.
34
-//
35
-// See http://blog.golang.org/context for example code for a server that uses
36
-// Contexts.
37
-package context // import "golang.org/x/net/context"
38
-
39
-// Background returns a non-nil, empty Context. It is never canceled, has no
40
-// values, and has no deadline. It is typically used by the main function,
41
-// initialization, and tests, and as the top-level Context for incoming
42
-// requests.
43
-func Background() Context {
44
-	return background
45
-}
46
-
47
-// TODO returns a non-nil, empty Context. Code should use context.TODO when
48
-// it's unclear which Context to use or it is not yet available (because the
49
-// surrounding function has not yet been extended to accept a Context
50
-// parameter).  TODO is recognized by static analysis tools that determine
51
-// whether Contexts are propagated correctly in a program.
52
-func TODO() Context {
53
-	return todo
54
-}

+ 0
- 583
vendor/golang.org/x/net/context/context_test.go View File

1
-// Copyright 2014 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
-// +build !go1.7
6
-
7
-package context
8
-
9
-import (
10
-	"fmt"
11
-	"math/rand"
12
-	"runtime"
13
-	"strings"
14
-	"sync"
15
-	"testing"
16
-	"time"
17
-)
18
-
19
-// otherContext is a Context that's not one of the types defined in context.go.
20
-// This lets us test code paths that differ based on the underlying type of the
21
-// Context.
22
-type otherContext struct {
23
-	Context
24
-}
25
-
26
-func TestBackground(t *testing.T) {
27
-	c := Background()
28
-	if c == nil {
29
-		t.Fatalf("Background returned nil")
30
-	}
31
-	select {
32
-	case x := <-c.Done():
33
-		t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
34
-	default:
35
-	}
36
-	if got, want := fmt.Sprint(c), "context.Background"; got != want {
37
-		t.Errorf("Background().String() = %q want %q", got, want)
38
-	}
39
-}
40
-
41
-func TestTODO(t *testing.T) {
42
-	c := TODO()
43
-	if c == nil {
44
-		t.Fatalf("TODO returned nil")
45
-	}
46
-	select {
47
-	case x := <-c.Done():
48
-		t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
49
-	default:
50
-	}
51
-	if got, want := fmt.Sprint(c), "context.TODO"; got != want {
52
-		t.Errorf("TODO().String() = %q want %q", got, want)
53
-	}
54
-}
55
-
56
-func TestWithCancel(t *testing.T) {
57
-	c1, cancel := WithCancel(Background())
58
-
59
-	if got, want := fmt.Sprint(c1), "context.Background.WithCancel"; got != want {
60
-		t.Errorf("c1.String() = %q want %q", got, want)
61
-	}
62
-
63
-	o := otherContext{c1}
64
-	c2, _ := WithCancel(o)
65
-	contexts := []Context{c1, o, c2}
66
-
67
-	for i, c := range contexts {
68
-		if d := c.Done(); d == nil {
69
-			t.Errorf("c[%d].Done() == %v want non-nil", i, d)
70
-		}
71
-		if e := c.Err(); e != nil {
72
-			t.Errorf("c[%d].Err() == %v want nil", i, e)
73
-		}
74
-
75
-		select {
76
-		case x := <-c.Done():
77
-			t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
78
-		default:
79
-		}
80
-	}
81
-
82
-	cancel()
83
-	time.Sleep(100 * time.Millisecond) // let cancelation propagate
84
-
85
-	for i, c := range contexts {
86
-		select {
87
-		case <-c.Done():
88
-		default:
89
-			t.Errorf("<-c[%d].Done() blocked, but shouldn't have", i)
90
-		}
91
-		if e := c.Err(); e != Canceled {
92
-			t.Errorf("c[%d].Err() == %v want %v", i, e, Canceled)
93
-		}
94
-	}
95
-}
96
-
97
-func TestParentFinishesChild(t *testing.T) {
98
-	// Context tree:
99
-	// parent -> cancelChild
100
-	// parent -> valueChild -> timerChild
101
-	parent, cancel := WithCancel(Background())
102
-	cancelChild, stop := WithCancel(parent)
103
-	defer stop()
104
-	valueChild := WithValue(parent, "key", "value")
105
-	timerChild, stop := WithTimeout(valueChild, 10000*time.Hour)
106
-	defer stop()
107
-
108
-	select {
109
-	case x := <-parent.Done():
110
-		t.Errorf("<-parent.Done() == %v want nothing (it should block)", x)
111
-	case x := <-cancelChild.Done():
112
-		t.Errorf("<-cancelChild.Done() == %v want nothing (it should block)", x)
113
-	case x := <-timerChild.Done():
114
-		t.Errorf("<-timerChild.Done() == %v want nothing (it should block)", x)
115
-	case x := <-valueChild.Done():
116
-		t.Errorf("<-valueChild.Done() == %v want nothing (it should block)", x)
117
-	default:
118
-	}
119
-
120
-	// The parent's children should contain the two cancelable children.
121
-	pc := parent.(*cancelCtx)
122
-	cc := cancelChild.(*cancelCtx)
123
-	tc := timerChild.(*timerCtx)
124
-	pc.mu.Lock()
125
-	if len(pc.children) != 2 || !pc.children[cc] || !pc.children[tc] {
126
-		t.Errorf("bad linkage: pc.children = %v, want %v and %v",
127
-			pc.children, cc, tc)
128
-	}
129
-	pc.mu.Unlock()
130
-
131
-	if p, ok := parentCancelCtx(cc.Context); !ok || p != pc {
132
-		t.Errorf("bad linkage: parentCancelCtx(cancelChild.Context) = %v, %v want %v, true", p, ok, pc)
133
-	}
134
-	if p, ok := parentCancelCtx(tc.Context); !ok || p != pc {
135
-		t.Errorf("bad linkage: parentCancelCtx(timerChild.Context) = %v, %v want %v, true", p, ok, pc)
136
-	}
137
-
138
-	cancel()
139
-
140
-	pc.mu.Lock()
141
-	if len(pc.children) != 0 {
142
-		t.Errorf("pc.cancel didn't clear pc.children = %v", pc.children)
143
-	}
144
-	pc.mu.Unlock()
145
-
146
-	// parent and children should all be finished.
147
-	check := func(ctx Context, name string) {
148
-		select {
149
-		case <-ctx.Done():
150
-		default:
151
-			t.Errorf("<-%s.Done() blocked, but shouldn't have", name)
152
-		}
153
-		if e := ctx.Err(); e != Canceled {
154
-			t.Errorf("%s.Err() == %v want %v", name, e, Canceled)
155
-		}
156
-	}
157
-	check(parent, "parent")
158
-	check(cancelChild, "cancelChild")
159
-	check(valueChild, "valueChild")
160
-	check(timerChild, "timerChild")
161
-
162
-	// WithCancel should return a canceled context on a canceled parent.
163
-	precanceledChild := WithValue(parent, "key", "value")
164
-	select {
165
-	case <-precanceledChild.Done():
166
-	default:
167
-		t.Errorf("<-precanceledChild.Done() blocked, but shouldn't have")
168
-	}
169
-	if e := precanceledChild.Err(); e != Canceled {
170
-		t.Errorf("precanceledChild.Err() == %v want %v", e, Canceled)
171
-	}
172
-}
173
-
174
-func TestChildFinishesFirst(t *testing.T) {
175
-	cancelable, stop := WithCancel(Background())
176
-	defer stop()
177
-	for _, parent := range []Context{Background(), cancelable} {
178
-		child, cancel := WithCancel(parent)
179
-
180
-		select {
181
-		case x := <-parent.Done():
182
-			t.Errorf("<-parent.Done() == %v want nothing (it should block)", x)
183
-		case x := <-child.Done():
184
-			t.Errorf("<-child.Done() == %v want nothing (it should block)", x)
185
-		default:
186
-		}
187
-
188
-		cc := child.(*cancelCtx)
189
-		pc, pcok := parent.(*cancelCtx) // pcok == false when parent == Background()
190
-		if p, ok := parentCancelCtx(cc.Context); ok != pcok || (ok && pc != p) {
191
-			t.Errorf("bad linkage: parentCancelCtx(cc.Context) = %v, %v want %v, %v", p, ok, pc, pcok)
192
-		}
193
-
194
-		if pcok {
195
-			pc.mu.Lock()
196
-			if len(pc.children) != 1 || !pc.children[cc] {
197
-				t.Errorf("bad linkage: pc.children = %v, cc = %v", pc.children, cc)
198
-			}
199
-			pc.mu.Unlock()
200
-		}
201
-
202
-		cancel()
203
-
204
-		if pcok {
205
-			pc.mu.Lock()
206
-			if len(pc.children) != 0 {
207
-				t.Errorf("child's cancel didn't remove self from pc.children = %v", pc.children)
208
-			}
209
-			pc.mu.Unlock()
210
-		}
211
-
212
-		// child should be finished.
213
-		select {
214
-		case <-child.Done():
215
-		default:
216
-			t.Errorf("<-child.Done() blocked, but shouldn't have")
217
-		}
218
-		if e := child.Err(); e != Canceled {
219
-			t.Errorf("child.Err() == %v want %v", e, Canceled)
220
-		}
221
-
222
-		// parent should not be finished.
223
-		select {
224
-		case x := <-parent.Done():
225
-			t.Errorf("<-parent.Done() == %v want nothing (it should block)", x)
226
-		default:
227
-		}
228
-		if e := parent.Err(); e != nil {
229
-			t.Errorf("parent.Err() == %v want nil", e)
230
-		}
231
-	}
232
-}
233
-
234
-func testDeadline(c Context, wait time.Duration, t *testing.T) {
235
-	select {
236
-	case <-time.After(wait):
237
-		t.Fatalf("context should have timed out")
238
-	case <-c.Done():
239
-	}
240
-	if e := c.Err(); e != DeadlineExceeded {
241
-		t.Errorf("c.Err() == %v want %v", e, DeadlineExceeded)
242
-	}
243
-}
244
-
245
-func TestDeadline(t *testing.T) {
246
-	t.Parallel()
247
-	const timeUnit = 500 * time.Millisecond
248
-	c, _ := WithDeadline(Background(), time.Now().Add(1*timeUnit))
249
-	if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
250
-		t.Errorf("c.String() = %q want prefix %q", got, prefix)
251
-	}
252
-	testDeadline(c, 2*timeUnit, t)
253
-
254
-	c, _ = WithDeadline(Background(), time.Now().Add(1*timeUnit))
255
-	o := otherContext{c}
256
-	testDeadline(o, 2*timeUnit, t)
257
-
258
-	c, _ = WithDeadline(Background(), time.Now().Add(1*timeUnit))
259
-	o = otherContext{c}
260
-	c, _ = WithDeadline(o, time.Now().Add(3*timeUnit))
261
-	testDeadline(c, 2*timeUnit, t)
262
-}
263
-
264
-func TestTimeout(t *testing.T) {
265
-	t.Parallel()
266
-	const timeUnit = 500 * time.Millisecond
267
-	c, _ := WithTimeout(Background(), 1*timeUnit)
268
-	if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
269
-		t.Errorf("c.String() = %q want prefix %q", got, prefix)
270
-	}
271
-	testDeadline(c, 2*timeUnit, t)
272
-
273
-	c, _ = WithTimeout(Background(), 1*timeUnit)
274
-	o := otherContext{c}
275
-	testDeadline(o, 2*timeUnit, t)
276
-
277
-	c, _ = WithTimeout(Background(), 1*timeUnit)
278
-	o = otherContext{c}
279
-	c, _ = WithTimeout(o, 3*timeUnit)
280
-	testDeadline(c, 2*timeUnit, t)
281
-}
282
-
283
-func TestCanceledTimeout(t *testing.T) {
284
-	t.Parallel()
285
-	const timeUnit = 500 * time.Millisecond
286
-	c, _ := WithTimeout(Background(), 2*timeUnit)
287
-	o := otherContext{c}
288
-	c, cancel := WithTimeout(o, 4*timeUnit)
289
-	cancel()
290
-	time.Sleep(1 * timeUnit) // let cancelation propagate
291
-	select {
292
-	case <-c.Done():
293
-	default:
294
-		t.Errorf("<-c.Done() blocked, but shouldn't have")
295
-	}
296
-	if e := c.Err(); e != Canceled {
297
-		t.Errorf("c.Err() == %v want %v", e, Canceled)
298
-	}
299
-}
300
-
301
-type key1 int
302
-type key2 int
303
-
304
-var k1 = key1(1)
305
-var k2 = key2(1) // same int as k1, different type
306
-var k3 = key2(3) // same type as k2, different int
307
-
308
-func TestValues(t *testing.T) {
309
-	check := func(c Context, nm, v1, v2, v3 string) {
310
-		if v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 {
311
-			t.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0)
312
-		}
313
-		if v, ok := c.Value(k2).(string); ok == (len(v2) == 0) || v != v2 {
314
-			t.Errorf(`%s.Value(k2).(string) = %q, %t want %q, %t`, nm, v, ok, v2, len(v2) != 0)
315
-		}
316
-		if v, ok := c.Value(k3).(string); ok == (len(v3) == 0) || v != v3 {
317
-			t.Errorf(`%s.Value(k3).(string) = %q, %t want %q, %t`, nm, v, ok, v3, len(v3) != 0)
318
-		}
319
-	}
320
-
321
-	c0 := Background()
322
-	check(c0, "c0", "", "", "")
323
-
324
-	c1 := WithValue(Background(), k1, "c1k1")
325
-	check(c1, "c1", "c1k1", "", "")
326
-
327
-	if got, want := fmt.Sprint(c1), `context.Background.WithValue(1, "c1k1")`; got != want {
328
-		t.Errorf("c.String() = %q want %q", got, want)
329
-	}
330
-
331
-	c2 := WithValue(c1, k2, "c2k2")
332
-	check(c2, "c2", "c1k1", "c2k2", "")
333
-
334
-	c3 := WithValue(c2, k3, "c3k3")
335
-	check(c3, "c2", "c1k1", "c2k2", "c3k3")
336
-
337
-	c4 := WithValue(c3, k1, nil)
338
-	check(c4, "c4", "", "c2k2", "c3k3")
339
-
340
-	o0 := otherContext{Background()}
341
-	check(o0, "o0", "", "", "")
342
-
343
-	o1 := otherContext{WithValue(Background(), k1, "c1k1")}
344
-	check(o1, "o1", "c1k1", "", "")
345
-
346
-	o2 := WithValue(o1, k2, "o2k2")
347
-	check(o2, "o2", "c1k1", "o2k2", "")
348
-
349
-	o3 := otherContext{c4}
350
-	check(o3, "o3", "", "c2k2", "c3k3")
351
-
352
-	o4 := WithValue(o3, k3, nil)
353
-	check(o4, "o4", "", "c2k2", "")
354
-}
355
-
356
-func TestAllocs(t *testing.T) {
357
-	bg := Background()
358
-	for _, test := range []struct {
359
-		desc       string
360
-		f          func()
361
-		limit      float64
362
-		gccgoLimit float64
363
-	}{
364
-		{
365
-			desc:       "Background()",
366
-			f:          func() { Background() },
367
-			limit:      0,
368
-			gccgoLimit: 0,
369
-		},
370
-		{
371
-			desc: fmt.Sprintf("WithValue(bg, %v, nil)", k1),
372
-			f: func() {
373
-				c := WithValue(bg, k1, nil)
374
-				c.Value(k1)
375
-			},
376
-			limit:      3,
377
-			gccgoLimit: 3,
378
-		},
379
-		{
380
-			desc: "WithTimeout(bg, 15*time.Millisecond)",
381
-			f: func() {
382
-				c, _ := WithTimeout(bg, 15*time.Millisecond)
383
-				<-c.Done()
384
-			},
385
-			limit:      8,
386
-			gccgoLimit: 16,
387
-		},
388
-		{
389
-			desc: "WithCancel(bg)",
390
-			f: func() {
391
-				c, cancel := WithCancel(bg)
392
-				cancel()
393
-				<-c.Done()
394
-			},
395
-			limit:      5,
396
-			gccgoLimit: 8,
397
-		},
398
-		{
399
-			desc: "WithTimeout(bg, 100*time.Millisecond)",
400
-			f: func() {
401
-				c, cancel := WithTimeout(bg, 100*time.Millisecond)
402
-				cancel()
403
-				<-c.Done()
404
-			},
405
-			limit:      8,
406
-			gccgoLimit: 25,
407
-		},
408
-	} {
409
-		limit := test.limit
410
-		if runtime.Compiler == "gccgo" {
411
-			// gccgo does not yet do escape analysis.
412
-			// TODO(iant): Remove this when gccgo does do escape analysis.
413
-			limit = test.gccgoLimit
414
-		}
415
-		if n := testing.AllocsPerRun(100, test.f); n > limit {
416
-			t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit))
417
-		}
418
-	}
419
-}
420
-
421
-func TestSimultaneousCancels(t *testing.T) {
422
-	root, cancel := WithCancel(Background())
423
-	m := map[Context]CancelFunc{root: cancel}
424
-	q := []Context{root}
425
-	// Create a tree of contexts.
426
-	for len(q) != 0 && len(m) < 100 {
427
-		parent := q[0]
428
-		q = q[1:]
429
-		for i := 0; i < 4; i++ {
430
-			ctx, cancel := WithCancel(parent)
431
-			m[ctx] = cancel
432
-			q = append(q, ctx)
433
-		}
434
-	}
435
-	// Start all the cancels in a random order.
436
-	var wg sync.WaitGroup
437
-	wg.Add(len(m))
438
-	for _, cancel := range m {
439
-		go func(cancel CancelFunc) {
440
-			cancel()
441
-			wg.Done()
442
-		}(cancel)
443
-	}
444
-	// Wait on all the contexts in a random order.
445
-	for ctx := range m {
446
-		select {
447
-		case <-ctx.Done():
448
-		case <-time.After(1 * time.Second):
449
-			buf := make([]byte, 10<<10)
450
-			n := runtime.Stack(buf, true)
451
-			t.Fatalf("timed out waiting for <-ctx.Done(); stacks:\n%s", buf[:n])
452
-		}
453
-	}
454
-	// Wait for all the cancel functions to return.
455
-	done := make(chan struct{})
456
-	go func() {
457
-		wg.Wait()
458
-		close(done)
459
-	}()
460
-	select {
461
-	case <-done:
462
-	case <-time.After(1 * time.Second):
463
-		buf := make([]byte, 10<<10)
464
-		n := runtime.Stack(buf, true)
465
-		t.Fatalf("timed out waiting for cancel functions; stacks:\n%s", buf[:n])
466
-	}
467
-}
468
-
469
-func TestInterlockedCancels(t *testing.T) {
470
-	parent, cancelParent := WithCancel(Background())
471
-	child, cancelChild := WithCancel(parent)
472
-	go func() {
473
-		parent.Done()
474
-		cancelChild()
475
-	}()
476
-	cancelParent()
477
-	select {
478
-	case <-child.Done():
479
-	case <-time.After(1 * time.Second):
480
-		buf := make([]byte, 10<<10)
481
-		n := runtime.Stack(buf, true)
482
-		t.Fatalf("timed out waiting for child.Done(); stacks:\n%s", buf[:n])
483
-	}
484
-}
485
-
486
-func TestLayersCancel(t *testing.T) {
487
-	testLayers(t, time.Now().UnixNano(), false)
488
-}
489
-
490
-func TestLayersTimeout(t *testing.T) {
491
-	testLayers(t, time.Now().UnixNano(), true)
492
-}
493
-
494
-func testLayers(t *testing.T, seed int64, testTimeout bool) {
495
-	rand.Seed(seed)
496
-	errorf := func(format string, a ...interface{}) {
497
-		t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
498
-	}
499
-	const (
500
-		timeout   = 200 * time.Millisecond
501
-		minLayers = 30
502
-	)
503
-	type value int
504
-	var (
505
-		vals      []*value
506
-		cancels   []CancelFunc
507
-		numTimers int
508
-		ctx       = Background()
509
-	)
510
-	for i := 0; i < minLayers || numTimers == 0 || len(cancels) == 0 || len(vals) == 0; i++ {
511
-		switch rand.Intn(3) {
512
-		case 0:
513
-			v := new(value)
514
-			ctx = WithValue(ctx, v, v)
515
-			vals = append(vals, v)
516
-		case 1:
517
-			var cancel CancelFunc
518
-			ctx, cancel = WithCancel(ctx)
519
-			cancels = append(cancels, cancel)
520
-		case 2:
521
-			var cancel CancelFunc
522
-			ctx, cancel = WithTimeout(ctx, timeout)
523
-			cancels = append(cancels, cancel)
524
-			numTimers++
525
-		}
526
-	}
527
-	checkValues := func(when string) {
528
-		for _, key := range vals {
529
-			if val := ctx.Value(key).(*value); key != val {
530
-				errorf("%s: ctx.Value(%p) = %p want %p", when, key, val, key)
531
-			}
532
-		}
533
-	}
534
-	select {
535
-	case <-ctx.Done():
536
-		errorf("ctx should not be canceled yet")
537
-	default:
538
-	}
539
-	if s, prefix := fmt.Sprint(ctx), "context.Background."; !strings.HasPrefix(s, prefix) {
540
-		t.Errorf("ctx.String() = %q want prefix %q", s, prefix)
541
-	}
542
-	t.Log(ctx)
543
-	checkValues("before cancel")
544
-	if testTimeout {
545
-		select {
546
-		case <-ctx.Done():
547
-		case <-time.After(timeout + 100*time.Millisecond):
548
-			errorf("ctx should have timed out")
549
-		}
550
-		checkValues("after timeout")
551
-	} else {
552
-		cancel := cancels[rand.Intn(len(cancels))]
553
-		cancel()
554
-		select {
555
-		case <-ctx.Done():
556
-		default:
557
-			errorf("ctx should be canceled")
558
-		}
559
-		checkValues("after cancel")
560
-	}
561
-}
562
-
563
-func TestCancelRemoves(t *testing.T) {
564
-	checkChildren := func(when string, ctx Context, want int) {
565
-		if got := len(ctx.(*cancelCtx).children); got != want {
566
-			t.Errorf("%s: context has %d children, want %d", when, got, want)
567
-		}
568
-	}
569
-
570
-	ctx, _ := WithCancel(Background())
571
-	checkChildren("after creation", ctx, 0)
572
-	_, cancel := WithCancel(ctx)
573
-	checkChildren("with WithCancel child ", ctx, 1)
574
-	cancel()
575
-	checkChildren("after cancelling WithCancel child", ctx, 0)
576
-
577
-	ctx, _ = WithCancel(Background())
578
-	checkChildren("after creation", ctx, 0)
579
-	_, cancel = WithTimeout(ctx, 60*time.Minute)
580
-	checkChildren("with WithTimeout child ", ctx, 1)
581
-	cancel()
582
-	checkChildren("after cancelling WithTimeout child", ctx, 0)
583
-}

+ 0
- 74
vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go View File

1
-// Copyright 2016 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
-// +build go1.7
6
-
7
-// Package ctxhttp provides helper functions for performing context-aware HTTP requests.
8
-package ctxhttp // import "golang.org/x/net/context/ctxhttp"
9
-
10
-import (
11
-	"io"
12
-	"net/http"
13
-	"net/url"
14
-	"strings"
15
-
16
-	"golang.org/x/net/context"
17
-)
18
-
19
-// Do sends an HTTP request with the provided http.Client and returns
20
-// an HTTP response.
21
-//
22
-// If the client is nil, http.DefaultClient is used.
23
-//
24
-// The provided ctx must be non-nil. If it is canceled or times out,
25
-// ctx.Err() will be returned.
26
-func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
27
-	if client == nil {
28
-		client = http.DefaultClient
29
-	}
30
-	resp, err := client.Do(req.WithContext(ctx))
31
-	// If we got an error, and the context has been canceled,
32
-	// the context's error is probably more useful.
33
-	if err != nil {
34
-		select {
35
-		case <-ctx.Done():
36
-			err = ctx.Err()
37
-		default:
38
-		}
39
-	}
40
-	return resp, err
41
-}
42
-
43
-// Get issues a GET request via the Do function.
44
-func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
45
-	req, err := http.NewRequest("GET", url, nil)
46
-	if err != nil {
47
-		return nil, err
48
-	}
49
-	return Do(ctx, client, req)
50
-}
51
-
52
-// Head issues a HEAD request via the Do function.
53
-func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
54
-	req, err := http.NewRequest("HEAD", url, nil)
55
-	if err != nil {
56
-		return nil, err
57
-	}
58
-	return Do(ctx, client, req)
59
-}
60
-
61
-// Post issues a POST request via the Do function.
62
-func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
63
-	req, err := http.NewRequest("POST", url, body)
64
-	if err != nil {
65
-		return nil, err
66
-	}
67
-	req.Header.Set("Content-Type", bodyType)
68
-	return Do(ctx, client, req)
69
-}
70
-
71
-// PostForm issues a POST request via the Do function.
72
-func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
73
-	return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
74
-}

+ 0
- 29
vendor/golang.org/x/net/context/ctxhttp/ctxhttp_17_test.go View File

1
-// Copyright 2015 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
-// +build !plan9,go1.7
6
-
7
-package ctxhttp
8
-
9
-import (
10
-	"io"
11
-	"net/http"
12
-	"net/http/httptest"
13
-	"testing"
14
-
15
-	"context"
16
-)
17
-
18
-func TestGo17Context(t *testing.T) {
19
-	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20
-		io.WriteString(w, "ok")
21
-	}))
22
-	defer ts.Close()
23
-	ctx := context.Background()
24
-	resp, err := Get(ctx, http.DefaultClient, ts.URL)
25
-	if resp == nil || err != nil {
26
-		t.Fatalf("error received from client: %v %v", err, resp)
27
-	}
28
-	resp.Body.Close()
29
-}

+ 0
- 147
vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17.go View File

1
-// Copyright 2015 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
-// +build !go1.7
6
-
7
-package ctxhttp // import "golang.org/x/net/context/ctxhttp"
8
-
9
-import (
10
-	"io"
11
-	"net/http"
12
-	"net/url"
13
-	"strings"
14
-
15
-	"golang.org/x/net/context"
16
-)
17
-
18
-func nop() {}
19
-
20
-var (
21
-	testHookContextDoneBeforeHeaders = nop
22
-	testHookDoReturned               = nop
23
-	testHookDidBodyClose             = nop
24
-)
25
-
26
-// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
27
-// If the client is nil, http.DefaultClient is used.
28
-// If the context is canceled or times out, ctx.Err() will be returned.
29
-func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
30
-	if client == nil {
31
-		client = http.DefaultClient
32
-	}
33
-
34
-	// TODO(djd): Respect any existing value of req.Cancel.
35
-	cancel := make(chan struct{})
36
-	req.Cancel = cancel
37
-
38
-	type responseAndError struct {
39
-		resp *http.Response
40
-		err  error
41
-	}
42
-	result := make(chan responseAndError, 1)
43
-
44
-	// Make local copies of test hooks closed over by goroutines below.
45
-	// Prevents data races in tests.
46
-	testHookDoReturned := testHookDoReturned
47
-	testHookDidBodyClose := testHookDidBodyClose
48
-
49
-	go func() {
50
-		resp, err := client.Do(req)
51
-		testHookDoReturned()
52
-		result <- responseAndError{resp, err}
53
-	}()
54
-
55
-	var resp *http.Response
56
-
57
-	select {
58
-	case <-ctx.Done():
59
-		testHookContextDoneBeforeHeaders()
60
-		close(cancel)
61
-		// Clean up after the goroutine calling client.Do:
62
-		go func() {
63
-			if r := <-result; r.resp != nil {
64
-				testHookDidBodyClose()
65
-				r.resp.Body.Close()
66
-			}
67
-		}()
68
-		return nil, ctx.Err()
69
-	case r := <-result:
70
-		var err error
71
-		resp, err = r.resp, r.err
72
-		if err != nil {
73
-			return resp, err
74
-		}
75
-	}
76
-
77
-	c := make(chan struct{})
78
-	go func() {
79
-		select {
80
-		case <-ctx.Done():
81
-			close(cancel)
82
-		case <-c:
83
-			// The response's Body is closed.
84
-		}
85
-	}()
86
-	resp.Body = &notifyingReader{resp.Body, c}
87
-
88
-	return resp, nil
89
-}
90
-
91
-// Get issues a GET request via the Do function.
92
-func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
93
-	req, err := http.NewRequest("GET", url, nil)
94
-	if err != nil {
95
-		return nil, err
96
-	}
97
-	return Do(ctx, client, req)
98
-}
99
-
100
-// Head issues a HEAD request via the Do function.
101
-func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
102
-	req, err := http.NewRequest("HEAD", url, nil)
103
-	if err != nil {
104
-		return nil, err
105
-	}
106
-	return Do(ctx, client, req)
107
-}
108
-
109
-// Post issues a POST request via the Do function.
110
-func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
111
-	req, err := http.NewRequest("POST", url, body)
112
-	if err != nil {
113
-		return nil, err
114
-	}
115
-	req.Header.Set("Content-Type", bodyType)
116
-	return Do(ctx, client, req)
117
-}
118
-
119
-// PostForm issues a POST request via the Do function.
120
-func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
121
-	return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
122
-}
123
-
124
-// notifyingReader is an io.ReadCloser that closes the notify channel after
125
-// Close is called or a Read fails on the underlying ReadCloser.
126
-type notifyingReader struct {
127
-	io.ReadCloser
128
-	notify chan<- struct{}
129
-}
130
-
131
-func (r *notifyingReader) Read(p []byte) (int, error) {
132
-	n, err := r.ReadCloser.Read(p)
133
-	if err != nil && r.notify != nil {
134
-		close(r.notify)
135
-		r.notify = nil
136
-	}
137
-	return n, err
138
-}
139
-
140
-func (r *notifyingReader) Close() error {
141
-	err := r.ReadCloser.Close()
142
-	if r.notify != nil {
143
-		close(r.notify)
144
-		r.notify = nil
145
-	}
146
-	return err
147
-}

+ 0
- 79
vendor/golang.org/x/net/context/ctxhttp/ctxhttp_pre17_test.go View File

1
-// Copyright 2015 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
-// +build !plan9,!go1.7
6
-
7
-package ctxhttp
8
-
9
-import (
10
-	"net"
11
-	"net/http"
12
-	"net/http/httptest"
13
-	"sync"
14
-	"testing"
15
-	"time"
16
-
17
-	"golang.org/x/net/context"
18
-)
19
-
20
-// golang.org/issue/14065
21
-func TestClosesResponseBodyOnCancel(t *testing.T) {
22
-	defer func() { testHookContextDoneBeforeHeaders = nop }()
23
-	defer func() { testHookDoReturned = nop }()
24
-	defer func() { testHookDidBodyClose = nop }()
25
-
26
-	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
27
-	defer ts.Close()
28
-
29
-	ctx, cancel := context.WithCancel(context.Background())
30
-
31
-	// closed when Do enters select case <-ctx.Done()
32
-	enteredDonePath := make(chan struct{})
33
-
34
-	testHookContextDoneBeforeHeaders = func() {
35
-		close(enteredDonePath)
36
-	}
37
-
38
-	testHookDoReturned = func() {
39
-		// We now have the result (the Flush'd headers) at least,
40
-		// so we can cancel the request.
41
-		cancel()
42
-
43
-		// But block the client.Do goroutine from sending
44
-		// until Do enters into the <-ctx.Done() path, since
45
-		// otherwise if both channels are readable, select
46
-		// picks a random one.
47
-		<-enteredDonePath
48
-	}
49
-
50
-	sawBodyClose := make(chan struct{})
51
-	testHookDidBodyClose = func() { close(sawBodyClose) }
52
-
53
-	tr := &http.Transport{}
54
-	defer tr.CloseIdleConnections()
55
-	c := &http.Client{Transport: tr}
56
-	req, _ := http.NewRequest("GET", ts.URL, nil)
57
-	_, doErr := Do(ctx, c, req)
58
-
59
-	select {
60
-	case <-sawBodyClose:
61
-	case <-time.After(5 * time.Second):
62
-		t.Fatal("timeout waiting for body to close")
63
-	}
64
-
65
-	if doErr != ctx.Err() {
66
-		t.Errorf("Do error = %v; want %v", doErr, ctx.Err())
67
-	}
68
-}
69
-
70
-type noteCloseConn struct {
71
-	net.Conn
72
-	onceClose sync.Once
73
-	closefn   func()
74
-}
75
-
76
-func (c *noteCloseConn) Close() error {
77
-	c.onceClose.Do(c.closefn)
78
-	return c.Conn.Close()
79
-}

+ 0
- 105
vendor/golang.org/x/net/context/ctxhttp/ctxhttp_test.go View File

1
-// Copyright 2015 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
-// +build !plan9
6
-
7
-package ctxhttp
8
-
9
-import (
10
-	"io"
11
-	"io/ioutil"
12
-	"net/http"
13
-	"net/http/httptest"
14
-	"testing"
15
-	"time"
16
-
17
-	"golang.org/x/net/context"
18
-)
19
-
20
-const (
21
-	requestDuration = 100 * time.Millisecond
22
-	requestBody     = "ok"
23
-)
24
-
25
-func okHandler(w http.ResponseWriter, r *http.Request) {
26
-	time.Sleep(requestDuration)
27
-	io.WriteString(w, requestBody)
28
-}
29
-
30
-func TestNoTimeout(t *testing.T) {
31
-	ts := httptest.NewServer(http.HandlerFunc(okHandler))
32
-	defer ts.Close()
33
-
34
-	ctx := context.Background()
35
-	res, err := Get(ctx, nil, ts.URL)
36
-	if err != nil {
37
-		t.Fatal(err)
38
-	}
39
-	defer res.Body.Close()
40
-	slurp, err := ioutil.ReadAll(res.Body)
41
-	if err != nil {
42
-		t.Fatal(err)
43
-	}
44
-	if string(slurp) != requestBody {
45
-		t.Errorf("body = %q; want %q", slurp, requestBody)
46
-	}
47
-}
48
-
49
-func TestCancelBeforeHeaders(t *testing.T) {
50
-	ctx, cancel := context.WithCancel(context.Background())
51
-
52
-	blockServer := make(chan struct{})
53
-	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54
-		cancel()
55
-		<-blockServer
56
-		io.WriteString(w, requestBody)
57
-	}))
58
-	defer ts.Close()
59
-	defer close(blockServer)
60
-
61
-	res, err := Get(ctx, nil, ts.URL)
62
-	if err == nil {
63
-		res.Body.Close()
64
-		t.Fatal("Get returned unexpected nil error")
65
-	}
66
-	if err != context.Canceled {
67
-		t.Errorf("err = %v; want %v", err, context.Canceled)
68
-	}
69
-}
70
-
71
-func TestCancelAfterHangingRequest(t *testing.T) {
72
-	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73
-		w.WriteHeader(http.StatusOK)
74
-		w.(http.Flusher).Flush()
75
-		<-w.(http.CloseNotifier).CloseNotify()
76
-	}))
77
-	defer ts.Close()
78
-
79
-	ctx, cancel := context.WithCancel(context.Background())
80
-	resp, err := Get(ctx, nil, ts.URL)
81
-	if err != nil {
82
-		t.Fatalf("unexpected error in Get: %v", err)
83
-	}
84
-
85
-	// Cancel befer reading the body.
86
-	// Reading Request.Body should fail, since the request was
87
-	// canceled before anything was written.
88
-	cancel()
89
-
90
-	done := make(chan struct{})
91
-
92
-	go func() {
93
-		b, err := ioutil.ReadAll(resp.Body)
94
-		if len(b) != 0 || err == nil {
95
-			t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
96
-		}
97
-		close(done)
98
-	}()
99
-
100
-	select {
101
-	case <-time.After(1 * time.Second):
102
-		t.Errorf("Test timed out")
103
-	case <-done:
104
-	}
105
-}

+ 0
- 72
vendor/golang.org/x/net/context/go17.go View File

1
-// Copyright 2016 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
-// +build go1.7
6
-
7
-package context
8
-
9
-import (
10
-	"context" // standard library's context, as of Go 1.7
11
-	"time"
12
-)
13
-
14
-var (
15
-	todo       = context.TODO()
16
-	background = context.Background()
17
-)
18
-
19
-// Canceled is the error returned by Context.Err when the context is canceled.
20
-var Canceled = context.Canceled
21
-
22
-// DeadlineExceeded is the error returned by Context.Err when the context's
23
-// deadline passes.
24
-var DeadlineExceeded = context.DeadlineExceeded
25
-
26
-// WithCancel returns a copy of parent with a new Done channel. The returned
27
-// context's Done channel is closed when the returned cancel function is called
28
-// or when the parent context's Done channel is closed, whichever happens first.
29
-//
30
-// Canceling this context releases resources associated with it, so code should
31
-// call cancel as soon as the operations running in this Context complete.
32
-func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
33
-	ctx, f := context.WithCancel(parent)
34
-	return ctx, CancelFunc(f)
35
-}
36
-
37
-// WithDeadline returns a copy of the parent context with the deadline adjusted
38
-// to be no later than d. If the parent's deadline is already earlier than d,
39
-// WithDeadline(parent, d) is semantically equivalent to parent. The returned
40
-// context's Done channel is closed when the deadline expires, when the returned
41
-// cancel function is called, or when the parent context's Done channel is
42
-// closed, whichever happens first.
43
-//
44
-// Canceling this context releases resources associated with it, so code should
45
-// call cancel as soon as the operations running in this Context complete.
46
-func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
47
-	ctx, f := context.WithDeadline(parent, deadline)
48
-	return ctx, CancelFunc(f)
49
-}
50
-
51
-// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
52
-//
53
-// Canceling this context releases resources associated with it, so code should
54
-// call cancel as soon as the operations running in this Context complete:
55
-//
56
-// 	func slowOperationWithTimeout(ctx context.Context) (Result, error) {
57
-// 		ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
58
-// 		defer cancel()  // releases resources if slowOperation completes before timeout elapses
59
-// 		return slowOperation(ctx)
60
-// 	}
61
-func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
62
-	return WithDeadline(parent, time.Now().Add(timeout))
63
-}
64
-
65
-// WithValue returns a copy of parent in which the value associated with key is
66
-// val.
67
-//
68
-// Use context Values only for request-scoped data that transits processes and
69
-// APIs, not for passing optional parameters to functions.
70
-func WithValue(parent Context, key interface{}, val interface{}) Context {
71
-	return context.WithValue(parent, key, val)
72
-}

+ 0
- 20
vendor/golang.org/x/net/context/go19.go View File

1
-// Copyright 2017 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
-// +build go1.9
6
-
7
-package context
8
-
9
-import "context" // standard library's context, as of Go 1.7
10
-
11
-// A Context carries a deadline, a cancelation signal, and other values across
12
-// API boundaries.
13
-//
14
-// Context's methods may be called by multiple goroutines simultaneously.
15
-type Context = context.Context
16
-
17
-// A CancelFunc tells an operation to abandon its work.
18
-// A CancelFunc does not wait for the work to stop.
19
-// After the first call, subsequent calls to a CancelFunc do nothing.
20
-type CancelFunc = context.CancelFunc

+ 0
- 300
vendor/golang.org/x/net/context/pre_go17.go View File

1
-// Copyright 2014 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
-// +build !go1.7
6
-
7
-package context
8
-
9
-import (
10
-	"errors"
11
-	"fmt"
12
-	"sync"
13
-	"time"
14
-)
15
-
16
-// An emptyCtx is never canceled, has no values, and has no deadline. It is not
17
-// struct{}, since vars of this type must have distinct addresses.
18
-type emptyCtx int
19
-
20
-func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
21
-	return
22
-}
23
-
24
-func (*emptyCtx) Done() <-chan struct{} {
25
-	return nil
26
-}
27
-
28
-func (*emptyCtx) Err() error {
29
-	return nil
30
-}
31
-
32
-func (*emptyCtx) Value(key interface{}) interface{} {
33
-	return nil
34
-}
35
-
36
-func (e *emptyCtx) String() string {
37
-	switch e {
38
-	case background:
39
-		return "context.Background"
40
-	case todo:
41
-		return "context.TODO"
42
-	}
43
-	return "unknown empty Context"
44
-}
45
-
46
-var (
47
-	background = new(emptyCtx)
48
-	todo       = new(emptyCtx)
49
-)
50
-
51
-// Canceled is the error returned by Context.Err when the context is canceled.
52
-var Canceled = errors.New("context canceled")
53
-
54
-// DeadlineExceeded is the error returned by Context.Err when the context's
55
-// deadline passes.
56
-var DeadlineExceeded = errors.New("context deadline exceeded")
57
-
58
-// WithCancel returns a copy of parent with a new Done channel. The returned
59
-// context's Done channel is closed when the returned cancel function is called
60
-// or when the parent context's Done channel is closed, whichever happens first.
61
-//
62
-// Canceling this context releases resources associated with it, so code should
63
-// call cancel as soon as the operations running in this Context complete.
64
-func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
65
-	c := newCancelCtx(parent)
66
-	propagateCancel(parent, c)
67
-	return c, func() { c.cancel(true, Canceled) }
68
-}
69
-
70
-// newCancelCtx returns an initialized cancelCtx.
71
-func newCancelCtx(parent Context) *cancelCtx {
72
-	return &cancelCtx{
73
-		Context: parent,
74
-		done:    make(chan struct{}),
75
-	}
76
-}
77
-
78
-// propagateCancel arranges for child to be canceled when parent is.
79
-func propagateCancel(parent Context, child canceler) {
80
-	if parent.Done() == nil {
81
-		return // parent is never canceled
82
-	}
83
-	if p, ok := parentCancelCtx(parent); ok {
84
-		p.mu.Lock()
85
-		if p.err != nil {
86
-			// parent has already been canceled
87
-			child.cancel(false, p.err)
88
-		} else {
89
-			if p.children == nil {
90
-				p.children = make(map[canceler]bool)
91
-			}
92
-			p.children[child] = true
93
-		}
94
-		p.mu.Unlock()
95
-	} else {
96
-		go func() {
97
-			select {
98
-			case <-parent.Done():
99
-				child.cancel(false, parent.Err())
100
-			case <-child.Done():
101
-			}
102
-		}()
103
-	}
104
-}
105
-
106
-// parentCancelCtx follows a chain of parent references until it finds a
107
-// *cancelCtx. This function understands how each of the concrete types in this
108
-// package represents its parent.
109
-func parentCancelCtx(parent Context) (*cancelCtx, bool) {
110
-	for {
111
-		switch c := parent.(type) {
112
-		case *cancelCtx:
113
-			return c, true
114
-		case *timerCtx:
115
-			return c.cancelCtx, true
116
-		case *valueCtx:
117
-			parent = c.Context
118
-		default:
119
-			return nil, false
120
-		}
121
-	}
122
-}
123
-
124
-// removeChild removes a context from its parent.
125
-func removeChild(parent Context, child canceler) {
126
-	p, ok := parentCancelCtx(parent)
127
-	if !ok {
128
-		return
129
-	}
130
-	p.mu.Lock()
131
-	if p.children != nil {
132
-		delete(p.children, child)
133
-	}
134
-	p.mu.Unlock()
135
-}
136
-
137
-// A canceler is a context type that can be canceled directly. The
138
-// implementations are *cancelCtx and *timerCtx.
139
-type canceler interface {
140
-	cancel(removeFromParent bool, err error)
141
-	Done() <-chan struct{}
142
-}
143
-
144
-// A cancelCtx can be canceled. When canceled, it also cancels any children
145
-// that implement canceler.
146
-type cancelCtx struct {
147
-	Context
148
-
149
-	done chan struct{} // closed by the first cancel call.
150
-
151
-	mu       sync.Mutex
152
-	children map[canceler]bool // set to nil by the first cancel call
153
-	err      error             // set to non-nil by the first cancel call
154
-}
155
-
156
-func (c *cancelCtx) Done() <-chan struct{} {
157
-	return c.done
158
-}
159
-
160
-func (c *cancelCtx) Err() error {
161
-	c.mu.Lock()
162
-	defer c.mu.Unlock()
163
-	return c.err
164
-}
165
-
166
-func (c *cancelCtx) String() string {
167
-	return fmt.Sprintf("%v.WithCancel", c.Context)
168
-}
169
-
170
-// cancel closes c.done, cancels each of c's children, and, if
171
-// removeFromParent is true, removes c from its parent's children.
172
-func (c *cancelCtx) cancel(removeFromParent bool, err error) {
173
-	if err == nil {
174
-		panic("context: internal error: missing cancel error")
175
-	}
176
-	c.mu.Lock()
177
-	if c.err != nil {
178
-		c.mu.Unlock()
179
-		return // already canceled
180
-	}
181
-	c.err = err
182
-	close(c.done)
183
-	for child := range c.children {
184
-		// NOTE: acquiring the child's lock while holding parent's lock.
185
-		child.cancel(false, err)
186
-	}
187
-	c.children = nil
188
-	c.mu.Unlock()
189
-
190
-	if removeFromParent {
191
-		removeChild(c.Context, c)
192
-	}
193
-}
194
-
195
-// WithDeadline returns a copy of the parent context with the deadline adjusted
196
-// to be no later than d. If the parent's deadline is already earlier than d,
197
-// WithDeadline(parent, d) is semantically equivalent to parent. The returned
198
-// context's Done channel is closed when the deadline expires, when the returned
199
-// cancel function is called, or when the parent context's Done channel is
200
-// closed, whichever happens first.
201
-//
202
-// Canceling this context releases resources associated with it, so code should
203
-// call cancel as soon as the operations running in this Context complete.
204
-func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) {
205
-	if cur, ok := parent.Deadline(); ok && cur.Before(deadline) {
206
-		// The current deadline is already sooner than the new one.
207
-		return WithCancel(parent)
208
-	}
209
-	c := &timerCtx{
210
-		cancelCtx: newCancelCtx(parent),
211
-		deadline:  deadline,
212
-	}
213
-	propagateCancel(parent, c)
214
-	d := deadline.Sub(time.Now())
215
-	if d <= 0 {
216
-		c.cancel(true, DeadlineExceeded) // deadline has already passed
217
-		return c, func() { c.cancel(true, Canceled) }
218
-	}
219
-	c.mu.Lock()
220
-	defer c.mu.Unlock()
221
-	if c.err == nil {
222
-		c.timer = time.AfterFunc(d, func() {
223
-			c.cancel(true, DeadlineExceeded)
224
-		})
225
-	}
226
-	return c, func() { c.cancel(true, Canceled) }
227
-}
228
-
229
-// A timerCtx carries a timer and a deadline. It embeds a cancelCtx to
230
-// implement Done and Err. It implements cancel by stopping its timer then
231
-// delegating to cancelCtx.cancel.
232
-type timerCtx struct {
233
-	*cancelCtx
234
-	timer *time.Timer // Under cancelCtx.mu.
235
-
236
-	deadline time.Time
237
-}
238
-
239
-func (c *timerCtx) Deadline() (deadline time.Time, ok bool) {
240
-	return c.deadline, true
241
-}
242
-
243
-func (c *timerCtx) String() string {
244
-	return fmt.Sprintf("%v.WithDeadline(%s [%s])", c.cancelCtx.Context, c.deadline, c.deadline.Sub(time.Now()))
245
-}
246
-
247
-func (c *timerCtx) cancel(removeFromParent bool, err error) {
248
-	c.cancelCtx.cancel(false, err)
249
-	if removeFromParent {
250
-		// Remove this timerCtx from its parent cancelCtx's children.
251
-		removeChild(c.cancelCtx.Context, c)
252
-	}
253
-	c.mu.Lock()
254
-	if c.timer != nil {
255
-		c.timer.Stop()
256
-		c.timer = nil
257
-	}
258
-	c.mu.Unlock()
259
-}
260
-
261
-// WithTimeout returns WithDeadline(parent, time.Now().Add(timeout)).
262
-//
263
-// Canceling this context releases resources associated with it, so code should
264
-// call cancel as soon as the operations running in this Context complete:
265
-//
266
-// 	func slowOperationWithTimeout(ctx context.Context) (Result, error) {
267
-// 		ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
268
-// 		defer cancel()  // releases resources if slowOperation completes before timeout elapses
269
-// 		return slowOperation(ctx)
270
-// 	}
271
-func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) {
272
-	return WithDeadline(parent, time.Now().Add(timeout))
273
-}
274
-
275
-// WithValue returns a copy of parent in which the value associated with key is
276
-// val.
277
-//
278
-// Use context Values only for request-scoped data that transits processes and
279
-// APIs, not for passing optional parameters to functions.
280
-func WithValue(parent Context, key interface{}, val interface{}) Context {
281
-	return &valueCtx{parent, key, val}
282
-}
283
-
284
-// A valueCtx carries a key-value pair. It implements Value for that key and
285
-// delegates all other calls to the embedded Context.
286
-type valueCtx struct {
287
-	Context
288
-	key, val interface{}
289
-}
290
-
291
-func (c *valueCtx) String() string {
292
-	return fmt.Sprintf("%v.WithValue(%#v, %#v)", c.Context, c.key, c.val)
293
-}
294
-
295
-func (c *valueCtx) Value(key interface{}) interface{} {
296
-	if c.key == key {
297
-		return c.val
298
-	}
299
-	return c.Context.Value(key)
300
-}

+ 0
- 109
vendor/golang.org/x/net/context/pre_go19.go View File

1
-// Copyright 2014 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
-// +build !go1.9
6
-
7
-package context
8
-
9
-import "time"
10
-
11
-// A Context carries a deadline, a cancelation signal, and other values across
12
-// API boundaries.
13
-//
14
-// Context's methods may be called by multiple goroutines simultaneously.
15
-type Context interface {
16
-	// Deadline returns the time when work done on behalf of this context
17
-	// should be canceled. Deadline returns ok==false when no deadline is
18
-	// set. Successive calls to Deadline return the same results.
19
-	Deadline() (deadline time.Time, ok bool)
20
-
21
-	// Done returns a channel that's closed when work done on behalf of this
22
-	// context should be canceled. Done may return nil if this context can
23
-	// never be canceled. Successive calls to Done return the same value.
24
-	//
25
-	// WithCancel arranges for Done to be closed when cancel is called;
26
-	// WithDeadline arranges for Done to be closed when the deadline
27
-	// expires; WithTimeout arranges for Done to be closed when the timeout
28
-	// elapses.
29
-	//
30
-	// Done is provided for use in select statements:
31
-	//
32
-	//  // Stream generates values with DoSomething and sends them to out
33
-	//  // until DoSomething returns an error or ctx.Done is closed.
34
-	//  func Stream(ctx context.Context, out chan<- Value) error {
35
-	//  	for {
36
-	//  		v, err := DoSomething(ctx)
37
-	//  		if err != nil {
38
-	//  			return err
39
-	//  		}
40
-	//  		select {
41
-	//  		case <-ctx.Done():
42
-	//  			return ctx.Err()
43
-	//  		case out <- v:
44
-	//  		}
45
-	//  	}
46
-	//  }
47
-	//
48
-	// See http://blog.golang.org/pipelines for more examples of how to use
49
-	// a Done channel for cancelation.
50
-	Done() <-chan struct{}
51
-
52
-	// Err returns a non-nil error value after Done is closed. Err returns
53
-	// Canceled if the context was canceled or DeadlineExceeded if the
54
-	// context's deadline passed. No other values for Err are defined.
55
-	// After Done is closed, successive calls to Err return the same value.
56
-	Err() error
57
-
58
-	// Value returns the value associated with this context for key, or nil
59
-	// if no value is associated with key. Successive calls to Value with
60
-	// the same key returns the same result.
61
-	//
62
-	// Use context values only for request-scoped data that transits
63
-	// processes and API boundaries, not for passing optional parameters to
64
-	// functions.
65
-	//
66
-	// A key identifies a specific value in a Context. Functions that wish
67
-	// to store values in Context typically allocate a key in a global
68
-	// variable then use that key as the argument to context.WithValue and
69
-	// Context.Value. A key can be any type that supports equality;
70
-	// packages should define keys as an unexported type to avoid
71
-	// collisions.
72
-	//
73
-	// Packages that define a Context key should provide type-safe accessors
74
-	// for the values stores using that key:
75
-	//
76
-	// 	// Package user defines a User type that's stored in Contexts.
77
-	// 	package user
78
-	//
79
-	// 	import "golang.org/x/net/context"
80
-	//
81
-	// 	// User is the type of value stored in the Contexts.
82
-	// 	type User struct {...}
83
-	//
84
-	// 	// key is an unexported type for keys defined in this package.
85
-	// 	// This prevents collisions with keys defined in other packages.
86
-	// 	type key int
87
-	//
88
-	// 	// userKey is the key for user.User values in Contexts. It is
89
-	// 	// unexported; clients use user.NewContext and user.FromContext
90
-	// 	// instead of using this key directly.
91
-	// 	var userKey key = 0
92
-	//
93
-	// 	// NewContext returns a new Context that carries value u.
94
-	// 	func NewContext(ctx context.Context, u *User) context.Context {
95
-	// 		return context.WithValue(ctx, userKey, u)
96
-	// 	}
97
-	//
98
-	// 	// FromContext returns the User value stored in ctx, if any.
99
-	// 	func FromContext(ctx context.Context) (*User, bool) {
100
-	// 		u, ok := ctx.Value(userKey).(*User)
101
-	// 		return u, ok
102
-	// 	}
103
-	Value(key interface{}) interface{}
104
-}
105
-
106
-// A CancelFunc tells an operation to abandon its work.
107
-// A CancelFunc does not wait for the work to stop.
108
-// After the first call, subsequent calls to a CancelFunc do nothing.
109
-type CancelFunc func()

+ 0
- 31
vendor/golang.org/x/net/context/withtimeout_test.go View File

1
-// Copyright 2014 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 context_test
6
-
7
-import (
8
-	"fmt"
9
-	"time"
10
-
11
-	"golang.org/x/net/context"
12
-)
13
-
14
-// This example passes a context with a timeout to tell a blocking function that
15
-// it should abandon its work after the timeout elapses.
16
-func ExampleWithTimeout() {
17
-	// Pass a context with a timeout to tell a blocking function that it
18
-	// should abandon its work after the timeout elapses.
19
-	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
20
-	defer cancel()
21
-
22
-	select {
23
-	case <-time.After(1 * time.Second):
24
-		fmt.Println("overslept")
25
-	case <-ctx.Done():
26
-		fmt.Println(ctx.Err()) // prints "context deadline exceeded"
27
-	}
28
-
29
-	// Output:
30
-	// context deadline exceeded
31
-}

+ 0
- 210
vendor/golang.org/x/net/dict/dict.go View File

1
-// Copyright 2010 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 dict implements the Dictionary Server Protocol
6
-// as defined in RFC 2229.
7
-package dict // import "golang.org/x/net/dict"
8
-
9
-import (
10
-	"net/textproto"
11
-	"strconv"
12
-	"strings"
13
-)
14
-
15
-// A Client represents a client connection to a dictionary server.
16
-type Client struct {
17
-	text *textproto.Conn
18
-}
19
-
20
-// Dial returns a new client connected to a dictionary server at
21
-// addr on the given network.
22
-func Dial(network, addr string) (*Client, error) {
23
-	text, err := textproto.Dial(network, addr)
24
-	if err != nil {
25
-		return nil, err
26
-	}
27
-	_, _, err = text.ReadCodeLine(220)
28
-	if err != nil {
29
-		text.Close()
30
-		return nil, err
31
-	}
32
-	return &Client{text: text}, nil
33
-}
34
-
35
-// Close closes the connection to the dictionary server.
36
-func (c *Client) Close() error {
37
-	return c.text.Close()
38
-}
39
-
40
-// A Dict represents a dictionary available on the server.
41
-type Dict struct {
42
-	Name string // short name of dictionary
43
-	Desc string // long description
44
-}
45
-
46
-// Dicts returns a list of the dictionaries available on the server.
47
-func (c *Client) Dicts() ([]Dict, error) {
48
-	id, err := c.text.Cmd("SHOW DB")
49
-	if err != nil {
50
-		return nil, err
51
-	}
52
-
53
-	c.text.StartResponse(id)
54
-	defer c.text.EndResponse(id)
55
-
56
-	_, _, err = c.text.ReadCodeLine(110)
57
-	if err != nil {
58
-		return nil, err
59
-	}
60
-	lines, err := c.text.ReadDotLines()
61
-	if err != nil {
62
-		return nil, err
63
-	}
64
-	_, _, err = c.text.ReadCodeLine(250)
65
-
66
-	dicts := make([]Dict, len(lines))
67
-	for i := range dicts {
68
-		d := &dicts[i]
69
-		a, _ := fields(lines[i])
70
-		if len(a) < 2 {
71
-			return nil, textproto.ProtocolError("invalid dictionary: " + lines[i])
72
-		}
73
-		d.Name = a[0]
74
-		d.Desc = a[1]
75
-	}
76
-	return dicts, err
77
-}
78
-
79
-// A Defn represents a definition.
80
-type Defn struct {
81
-	Dict Dict   // Dict where definition was found
82
-	Word string // Word being defined
83
-	Text []byte // Definition text, typically multiple lines
84
-}
85
-
86
-// Define requests the definition of the given word.
87
-// The argument dict names the dictionary to use,
88
-// the Name field of a Dict returned by Dicts.
89
-//
90
-// The special dictionary name "*" means to look in all the
91
-// server's dictionaries.
92
-// The special dictionary name "!" means to look in all the
93
-// server's dictionaries in turn, stopping after finding the word
94
-// in one of them.
95
-func (c *Client) Define(dict, word string) ([]*Defn, error) {
96
-	id, err := c.text.Cmd("DEFINE %s %q", dict, word)
97
-	if err != nil {
98
-		return nil, err
99
-	}
100
-
101
-	c.text.StartResponse(id)
102
-	defer c.text.EndResponse(id)
103
-
104
-	_, line, err := c.text.ReadCodeLine(150)
105
-	if err != nil {
106
-		return nil, err
107
-	}
108
-	a, _ := fields(line)
109
-	if len(a) < 1 {
110
-		return nil, textproto.ProtocolError("malformed response: " + line)
111
-	}
112
-	n, err := strconv.Atoi(a[0])
113
-	if err != nil {
114
-		return nil, textproto.ProtocolError("invalid definition count: " + a[0])
115
-	}
116
-	def := make([]*Defn, n)
117
-	for i := 0; i < n; i++ {
118
-		_, line, err = c.text.ReadCodeLine(151)
119
-		if err != nil {
120
-			return nil, err
121
-		}
122
-		a, _ := fields(line)
123
-		if len(a) < 3 {
124
-			// skip it, to keep protocol in sync
125
-			i--
126
-			n--
127
-			def = def[0:n]
128
-			continue
129
-		}
130
-		d := &Defn{Word: a[0], Dict: Dict{a[1], a[2]}}
131
-		d.Text, err = c.text.ReadDotBytes()
132
-		if err != nil {
133
-			return nil, err
134
-		}
135
-		def[i] = d
136
-	}
137
-	_, _, err = c.text.ReadCodeLine(250)
138
-	return def, err
139
-}
140
-
141
-// Fields returns the fields in s.
142
-// Fields are space separated unquoted words
143
-// or quoted with single or double quote.
144
-func fields(s string) ([]string, error) {
145
-	var v []string
146
-	i := 0
147
-	for {
148
-		for i < len(s) && (s[i] == ' ' || s[i] == '\t') {
149
-			i++
150
-		}
151
-		if i >= len(s) {
152
-			break
153
-		}
154
-		if s[i] == '"' || s[i] == '\'' {
155
-			q := s[i]
156
-			// quoted string
157
-			var j int
158
-			for j = i + 1; ; j++ {
159
-				if j >= len(s) {
160
-					return nil, textproto.ProtocolError("malformed quoted string")
161
-				}
162
-				if s[j] == '\\' {
163
-					j++
164
-					continue
165
-				}
166
-				if s[j] == q {
167
-					j++
168
-					break
169
-				}
170
-			}
171
-			v = append(v, unquote(s[i+1:j-1]))
172
-			i = j
173
-		} else {
174
-			// atom
175
-			var j int
176
-			for j = i; j < len(s); j++ {
177
-				if s[j] == ' ' || s[j] == '\t' || s[j] == '\\' || s[j] == '"' || s[j] == '\'' {
178
-					break
179
-				}
180
-			}
181
-			v = append(v, s[i:j])
182
-			i = j
183
-		}
184
-		if i < len(s) {
185
-			c := s[i]
186
-			if c != ' ' && c != '\t' {
187
-				return nil, textproto.ProtocolError("quotes not on word boundaries")
188
-			}
189
-		}
190
-	}
191
-	return v, nil
192
-}
193
-
194
-func unquote(s string) string {
195
-	if strings.Index(s, "\\") < 0 {
196
-		return s
197
-	}
198
-	b := []byte(s)
199
-	w := 0
200
-	for r := 0; r < len(b); r++ {
201
-		c := b[r]
202
-		if c == '\\' {
203
-			r++
204
-			c = b[r]
205
-		}
206
-		b[w] = c
207
-		w++
208
-	}
209
-	return string(b[0:w])
210
-}

+ 0
- 132
vendor/golang.org/x/net/dns/dnsmessage/example_test.go View File

1
-// Copyright 2017 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 dnsmessage_test
6
-
7
-import (
8
-	"fmt"
9
-	"net"
10
-	"strings"
11
-
12
-	"golang.org/x/net/dns/dnsmessage"
13
-)
14
-
15
-func mustNewName(name string) dnsmessage.Name {
16
-	n, err := dnsmessage.NewName(name)
17
-	if err != nil {
18
-		panic(err)
19
-	}
20
-	return n
21
-}
22
-
23
-func ExampleParser() {
24
-	msg := dnsmessage.Message{
25
-		Header: dnsmessage.Header{Response: true, Authoritative: true},
26
-		Questions: []dnsmessage.Question{
27
-			{
28
-				Name:  mustNewName("foo.bar.example.com."),
29
-				Type:  dnsmessage.TypeA,
30
-				Class: dnsmessage.ClassINET,
31
-			},
32
-			{
33
-				Name:  mustNewName("bar.example.com."),
34
-				Type:  dnsmessage.TypeA,
35
-				Class: dnsmessage.ClassINET,
36
-			},
37
-		},
38
-		Answers: []dnsmessage.Resource{
39
-			{
40
-				dnsmessage.ResourceHeader{
41
-					Name:  mustNewName("foo.bar.example.com."),
42
-					Type:  dnsmessage.TypeA,
43
-					Class: dnsmessage.ClassINET,
44
-				},
45
-				&dnsmessage.AResource{[4]byte{127, 0, 0, 1}},
46
-			},
47
-			{
48
-				dnsmessage.ResourceHeader{
49
-					Name:  mustNewName("bar.example.com."),
50
-					Type:  dnsmessage.TypeA,
51
-					Class: dnsmessage.ClassINET,
52
-				},
53
-				&dnsmessage.AResource{[4]byte{127, 0, 0, 2}},
54
-			},
55
-		},
56
-	}
57
-
58
-	buf, err := msg.Pack()
59
-	if err != nil {
60
-		panic(err)
61
-	}
62
-
63
-	wantName := "bar.example.com."
64
-
65
-	var p dnsmessage.Parser
66
-	if _, err := p.Start(buf); err != nil {
67
-		panic(err)
68
-	}
69
-
70
-	for {
71
-		q, err := p.Question()
72
-		if err == dnsmessage.ErrSectionDone {
73
-			break
74
-		}
75
-		if err != nil {
76
-			panic(err)
77
-		}
78
-
79
-		if q.Name.String() != wantName {
80
-			continue
81
-		}
82
-
83
-		fmt.Println("Found question for name", wantName)
84
-		if err := p.SkipAllQuestions(); err != nil {
85
-			panic(err)
86
-		}
87
-		break
88
-	}
89
-
90
-	var gotIPs []net.IP
91
-	for {
92
-		h, err := p.AnswerHeader()
93
-		if err == dnsmessage.ErrSectionDone {
94
-			break
95
-		}
96
-		if err != nil {
97
-			panic(err)
98
-		}
99
-
100
-		if (h.Type != dnsmessage.TypeA && h.Type != dnsmessage.TypeAAAA) || h.Class != dnsmessage.ClassINET {
101
-			continue
102
-		}
103
-
104
-		if !strings.EqualFold(h.Name.String(), wantName) {
105
-			if err := p.SkipAnswer(); err != nil {
106
-				panic(err)
107
-			}
108
-			continue
109
-		}
110
-
111
-		switch h.Type {
112
-		case dnsmessage.TypeA:
113
-			r, err := p.AResource()
114
-			if err != nil {
115
-				panic(err)
116
-			}
117
-			gotIPs = append(gotIPs, r.A[:])
118
-		case dnsmessage.TypeAAAA:
119
-			r, err := p.AAAAResource()
120
-			if err != nil {
121
-				panic(err)
122
-			}
123
-			gotIPs = append(gotIPs, r.AAAA[:])
124
-		}
125
-	}
126
-
127
-	fmt.Printf("Found A/AAAA records for name %s: %v\n", wantName, gotIPs)
128
-
129
-	// Output:
130
-	// Found question for name bar.example.com.
131
-	// Found A/AAAA records for name bar.example.com.: [127.0.0.2]
132
-}

+ 0
- 1997
vendor/golang.org/x/net/dns/dnsmessage/message.go
File diff suppressed because it is too large
View File


+ 0
- 1116
vendor/golang.org/x/net/dns/dnsmessage/message_test.go
File diff suppressed because it is too large
View File


+ 0
- 2
vendor/golang.org/x/net/http2/.gitignore View File

1
-*~
2
-h2i/h2i

+ 0
- 51
vendor/golang.org/x/net/http2/Dockerfile View File

1
-#
2
-# This Dockerfile builds a recent curl with HTTP/2 client support, using
3
-# a recent nghttp2 build.
4
-#
5
-# See the Makefile for how to tag it. If Docker and that image is found, the
6
-# Go tests use this curl binary for integration tests.
7
-#
8
-
9
-FROM ubuntu:trusty
10
-
11
-RUN apt-get update && \
12
-    apt-get upgrade -y && \
13
-    apt-get install -y git-core build-essential wget
14
-
15
-RUN apt-get install -y --no-install-recommends \
16
-       autotools-dev libtool pkg-config zlib1g-dev \
17
-       libcunit1-dev libssl-dev libxml2-dev libevent-dev \
18
-       automake autoconf
19
-
20
-# The list of packages nghttp2 recommends for h2load:
21
-RUN apt-get install -y --no-install-recommends make binutils \
22
-        autoconf automake autotools-dev \
23
-        libtool pkg-config zlib1g-dev libcunit1-dev libssl-dev libxml2-dev \
24
-        libev-dev libevent-dev libjansson-dev libjemalloc-dev \
25
-        cython python3.4-dev python-setuptools
26
-
27
-# Note: setting NGHTTP2_VER before the git clone, so an old git clone isn't cached:
28
-ENV NGHTTP2_VER 895da9a
29
-RUN cd /root && git clone https://github.com/tatsuhiro-t/nghttp2.git
30
-
31
-WORKDIR /root/nghttp2
32
-RUN git reset --hard $NGHTTP2_VER
33
-RUN autoreconf -i
34
-RUN automake
35
-RUN autoconf
36
-RUN ./configure
37
-RUN make
38
-RUN make install
39
-
40
-WORKDIR /root
41
-RUN wget http://curl.haxx.se/download/curl-7.45.0.tar.gz
42
-RUN tar -zxvf curl-7.45.0.tar.gz
43
-WORKDIR /root/curl-7.45.0
44
-RUN ./configure --with-ssl --with-nghttp2=/usr/local
45
-RUN make
46
-RUN make install
47
-RUN ldconfig
48
-
49
-CMD ["-h"]
50
-ENTRYPOINT ["/usr/local/bin/curl"]
51
-

+ 0
- 3
vendor/golang.org/x/net/http2/Makefile View File

1
-curlimage:
2
-	docker build -t gohttp2/curl .
3
-

+ 0
- 20
vendor/golang.org/x/net/http2/README View File

1
-This is a work-in-progress HTTP/2 implementation for Go.
2
-
3
-It will eventually live in the Go standard library and won't require
4
-any changes to your code to use.  It will just be automatic.
5
-
6
-Status:
7
-
8
-* The server support is pretty good. A few things are missing
9
-  but are being worked on.
10
-* The client work has just started but shares a lot of code
11
-  is coming along much quicker.
12
-
13
-Docs are at https://godoc.org/golang.org/x/net/http2
14
-
15
-Demo test server at https://http2.golang.org/
16
-
17
-Help & bug reports welcome!
18
-
19
-Contributing: https://golang.org/doc/contribute.html
20
-Bugs:         https://golang.org/issue/new?title=x/net/http2:+

+ 0
- 641
vendor/golang.org/x/net/http2/ciphers.go View File

1
-// Copyright 2017 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 http2
6
-
7
-// A list of the possible cipher suite ids. Taken from
8
-// http://www.iana.org/assignments/tls-parameters/tls-parameters.txt
9
-
10
-const (
11
-	cipher_TLS_NULL_WITH_NULL_NULL               uint16 = 0x0000
12
-	cipher_TLS_RSA_WITH_NULL_MD5                 uint16 = 0x0001
13
-	cipher_TLS_RSA_WITH_NULL_SHA                 uint16 = 0x0002
14
-	cipher_TLS_RSA_EXPORT_WITH_RC4_40_MD5        uint16 = 0x0003
15
-	cipher_TLS_RSA_WITH_RC4_128_MD5              uint16 = 0x0004
16
-	cipher_TLS_RSA_WITH_RC4_128_SHA              uint16 = 0x0005
17
-	cipher_TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5    uint16 = 0x0006
18
-	cipher_TLS_RSA_WITH_IDEA_CBC_SHA             uint16 = 0x0007
19
-	cipher_TLS_RSA_EXPORT_WITH_DES40_CBC_SHA     uint16 = 0x0008
20
-	cipher_TLS_RSA_WITH_DES_CBC_SHA              uint16 = 0x0009
21
-	cipher_TLS_RSA_WITH_3DES_EDE_CBC_SHA         uint16 = 0x000A
22
-	cipher_TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA  uint16 = 0x000B
23
-	cipher_TLS_DH_DSS_WITH_DES_CBC_SHA           uint16 = 0x000C
24
-	cipher_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA      uint16 = 0x000D
25
-	cipher_TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA  uint16 = 0x000E
26
-	cipher_TLS_DH_RSA_WITH_DES_CBC_SHA           uint16 = 0x000F
27
-	cipher_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA      uint16 = 0x0010
28
-	cipher_TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA uint16 = 0x0011
29
-	cipher_TLS_DHE_DSS_WITH_DES_CBC_SHA          uint16 = 0x0012
30
-	cipher_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA     uint16 = 0x0013
31
-	cipher_TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA uint16 = 0x0014
32
-	cipher_TLS_DHE_RSA_WITH_DES_CBC_SHA          uint16 = 0x0015
33
-	cipher_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA     uint16 = 0x0016
34
-	cipher_TLS_DH_anon_EXPORT_WITH_RC4_40_MD5    uint16 = 0x0017
35
-	cipher_TLS_DH_anon_WITH_RC4_128_MD5          uint16 = 0x0018
36
-	cipher_TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA uint16 = 0x0019
37
-	cipher_TLS_DH_anon_WITH_DES_CBC_SHA          uint16 = 0x001A
38
-	cipher_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA     uint16 = 0x001B
39
-	// Reserved uint16 =  0x001C-1D
40
-	cipher_TLS_KRB5_WITH_DES_CBC_SHA             uint16 = 0x001E
41
-	cipher_TLS_KRB5_WITH_3DES_EDE_CBC_SHA        uint16 = 0x001F
42
-	cipher_TLS_KRB5_WITH_RC4_128_SHA             uint16 = 0x0020
43
-	cipher_TLS_KRB5_WITH_IDEA_CBC_SHA            uint16 = 0x0021
44
-	cipher_TLS_KRB5_WITH_DES_CBC_MD5             uint16 = 0x0022
45
-	cipher_TLS_KRB5_WITH_3DES_EDE_CBC_MD5        uint16 = 0x0023
46
-	cipher_TLS_KRB5_WITH_RC4_128_MD5             uint16 = 0x0024
47
-	cipher_TLS_KRB5_WITH_IDEA_CBC_MD5            uint16 = 0x0025
48
-	cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA   uint16 = 0x0026
49
-	cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA   uint16 = 0x0027
50
-	cipher_TLS_KRB5_EXPORT_WITH_RC4_40_SHA       uint16 = 0x0028
51
-	cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5   uint16 = 0x0029
52
-	cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5   uint16 = 0x002A
53
-	cipher_TLS_KRB5_EXPORT_WITH_RC4_40_MD5       uint16 = 0x002B
54
-	cipher_TLS_PSK_WITH_NULL_SHA                 uint16 = 0x002C
55
-	cipher_TLS_DHE_PSK_WITH_NULL_SHA             uint16 = 0x002D
56
-	cipher_TLS_RSA_PSK_WITH_NULL_SHA             uint16 = 0x002E
57
-	cipher_TLS_RSA_WITH_AES_128_CBC_SHA          uint16 = 0x002F
58
-	cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA       uint16 = 0x0030
59
-	cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA       uint16 = 0x0031
60
-	cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA      uint16 = 0x0032
61
-	cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA      uint16 = 0x0033
62
-	cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA      uint16 = 0x0034
63
-	cipher_TLS_RSA_WITH_AES_256_CBC_SHA          uint16 = 0x0035
64
-	cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA       uint16 = 0x0036
65
-	cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA       uint16 = 0x0037
66
-	cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA      uint16 = 0x0038
67
-	cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA      uint16 = 0x0039
68
-	cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA      uint16 = 0x003A
69
-	cipher_TLS_RSA_WITH_NULL_SHA256              uint16 = 0x003B
70
-	cipher_TLS_RSA_WITH_AES_128_CBC_SHA256       uint16 = 0x003C
71
-	cipher_TLS_RSA_WITH_AES_256_CBC_SHA256       uint16 = 0x003D
72
-	cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA256    uint16 = 0x003E
73
-	cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA256    uint16 = 0x003F
74
-	cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256   uint16 = 0x0040
75
-	cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA     uint16 = 0x0041
76
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA  uint16 = 0x0042
77
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA  uint16 = 0x0043
78
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA uint16 = 0x0044
79
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA uint16 = 0x0045
80
-	cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA uint16 = 0x0046
81
-	// Reserved uint16 =  0x0047-4F
82
-	// Reserved uint16 =  0x0050-58
83
-	// Reserved uint16 =  0x0059-5C
84
-	// Unassigned uint16 =  0x005D-5F
85
-	// Reserved uint16 =  0x0060-66
86
-	cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x0067
87
-	cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA256  uint16 = 0x0068
88
-	cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA256  uint16 = 0x0069
89
-	cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 uint16 = 0x006A
90
-	cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 uint16 = 0x006B
91
-	cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA256 uint16 = 0x006C
92
-	cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA256 uint16 = 0x006D
93
-	// Unassigned uint16 =  0x006E-83
94
-	cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA        uint16 = 0x0084
95
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA     uint16 = 0x0085
96
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA     uint16 = 0x0086
97
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA    uint16 = 0x0087
98
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA    uint16 = 0x0088
99
-	cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA    uint16 = 0x0089
100
-	cipher_TLS_PSK_WITH_RC4_128_SHA                 uint16 = 0x008A
101
-	cipher_TLS_PSK_WITH_3DES_EDE_CBC_SHA            uint16 = 0x008B
102
-	cipher_TLS_PSK_WITH_AES_128_CBC_SHA             uint16 = 0x008C
103
-	cipher_TLS_PSK_WITH_AES_256_CBC_SHA             uint16 = 0x008D
104
-	cipher_TLS_DHE_PSK_WITH_RC4_128_SHA             uint16 = 0x008E
105
-	cipher_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA        uint16 = 0x008F
106
-	cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA         uint16 = 0x0090
107
-	cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA         uint16 = 0x0091
108
-	cipher_TLS_RSA_PSK_WITH_RC4_128_SHA             uint16 = 0x0092
109
-	cipher_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA        uint16 = 0x0093
110
-	cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA         uint16 = 0x0094
111
-	cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA         uint16 = 0x0095
112
-	cipher_TLS_RSA_WITH_SEED_CBC_SHA                uint16 = 0x0096
113
-	cipher_TLS_DH_DSS_WITH_SEED_CBC_SHA             uint16 = 0x0097
114
-	cipher_TLS_DH_RSA_WITH_SEED_CBC_SHA             uint16 = 0x0098
115
-	cipher_TLS_DHE_DSS_WITH_SEED_CBC_SHA            uint16 = 0x0099
116
-	cipher_TLS_DHE_RSA_WITH_SEED_CBC_SHA            uint16 = 0x009A
117
-	cipher_TLS_DH_anon_WITH_SEED_CBC_SHA            uint16 = 0x009B
118
-	cipher_TLS_RSA_WITH_AES_128_GCM_SHA256          uint16 = 0x009C
119
-	cipher_TLS_RSA_WITH_AES_256_GCM_SHA384          uint16 = 0x009D
120
-	cipher_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256      uint16 = 0x009E
121
-	cipher_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384      uint16 = 0x009F
122
-	cipher_TLS_DH_RSA_WITH_AES_128_GCM_SHA256       uint16 = 0x00A0
123
-	cipher_TLS_DH_RSA_WITH_AES_256_GCM_SHA384       uint16 = 0x00A1
124
-	cipher_TLS_DHE_DSS_WITH_AES_128_GCM_SHA256      uint16 = 0x00A2
125
-	cipher_TLS_DHE_DSS_WITH_AES_256_GCM_SHA384      uint16 = 0x00A3
126
-	cipher_TLS_DH_DSS_WITH_AES_128_GCM_SHA256       uint16 = 0x00A4
127
-	cipher_TLS_DH_DSS_WITH_AES_256_GCM_SHA384       uint16 = 0x00A5
128
-	cipher_TLS_DH_anon_WITH_AES_128_GCM_SHA256      uint16 = 0x00A6
129
-	cipher_TLS_DH_anon_WITH_AES_256_GCM_SHA384      uint16 = 0x00A7
130
-	cipher_TLS_PSK_WITH_AES_128_GCM_SHA256          uint16 = 0x00A8
131
-	cipher_TLS_PSK_WITH_AES_256_GCM_SHA384          uint16 = 0x00A9
132
-	cipher_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256      uint16 = 0x00AA
133
-	cipher_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384      uint16 = 0x00AB
134
-	cipher_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256      uint16 = 0x00AC
135
-	cipher_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384      uint16 = 0x00AD
136
-	cipher_TLS_PSK_WITH_AES_128_CBC_SHA256          uint16 = 0x00AE
137
-	cipher_TLS_PSK_WITH_AES_256_CBC_SHA384          uint16 = 0x00AF
138
-	cipher_TLS_PSK_WITH_NULL_SHA256                 uint16 = 0x00B0
139
-	cipher_TLS_PSK_WITH_NULL_SHA384                 uint16 = 0x00B1
140
-	cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256      uint16 = 0x00B2
141
-	cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384      uint16 = 0x00B3
142
-	cipher_TLS_DHE_PSK_WITH_NULL_SHA256             uint16 = 0x00B4
143
-	cipher_TLS_DHE_PSK_WITH_NULL_SHA384             uint16 = 0x00B5
144
-	cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256      uint16 = 0x00B6
145
-	cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384      uint16 = 0x00B7
146
-	cipher_TLS_RSA_PSK_WITH_NULL_SHA256             uint16 = 0x00B8
147
-	cipher_TLS_RSA_PSK_WITH_NULL_SHA384             uint16 = 0x00B9
148
-	cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256     uint16 = 0x00BA
149
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256  uint16 = 0x00BB
150
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256  uint16 = 0x00BC
151
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0x00BD
152
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0x00BE
153
-	cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0x00BF
154
-	cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256     uint16 = 0x00C0
155
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256  uint16 = 0x00C1
156
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256  uint16 = 0x00C2
157
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256 uint16 = 0x00C3
158
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 uint16 = 0x00C4
159
-	cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256 uint16 = 0x00C5
160
-	// Unassigned uint16 =  0x00C6-FE
161
-	cipher_TLS_EMPTY_RENEGOTIATION_INFO_SCSV uint16 = 0x00FF
162
-	// Unassigned uint16 =  0x01-55,*
163
-	cipher_TLS_FALLBACK_SCSV uint16 = 0x5600
164
-	// Unassigned                                   uint16 = 0x5601 - 0xC000
165
-	cipher_TLS_ECDH_ECDSA_WITH_NULL_SHA                 uint16 = 0xC001
166
-	cipher_TLS_ECDH_ECDSA_WITH_RC4_128_SHA              uint16 = 0xC002
167
-	cipher_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA         uint16 = 0xC003
168
-	cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA          uint16 = 0xC004
169
-	cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA          uint16 = 0xC005
170
-	cipher_TLS_ECDHE_ECDSA_WITH_NULL_SHA                uint16 = 0xC006
171
-	cipher_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA             uint16 = 0xC007
172
-	cipher_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA        uint16 = 0xC008
173
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA         uint16 = 0xC009
174
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA         uint16 = 0xC00A
175
-	cipher_TLS_ECDH_RSA_WITH_NULL_SHA                   uint16 = 0xC00B
176
-	cipher_TLS_ECDH_RSA_WITH_RC4_128_SHA                uint16 = 0xC00C
177
-	cipher_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA           uint16 = 0xC00D
178
-	cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA            uint16 = 0xC00E
179
-	cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA            uint16 = 0xC00F
180
-	cipher_TLS_ECDHE_RSA_WITH_NULL_SHA                  uint16 = 0xC010
181
-	cipher_TLS_ECDHE_RSA_WITH_RC4_128_SHA               uint16 = 0xC011
182
-	cipher_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA          uint16 = 0xC012
183
-	cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA           uint16 = 0xC013
184
-	cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA           uint16 = 0xC014
185
-	cipher_TLS_ECDH_anon_WITH_NULL_SHA                  uint16 = 0xC015
186
-	cipher_TLS_ECDH_anon_WITH_RC4_128_SHA               uint16 = 0xC016
187
-	cipher_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA          uint16 = 0xC017
188
-	cipher_TLS_ECDH_anon_WITH_AES_128_CBC_SHA           uint16 = 0xC018
189
-	cipher_TLS_ECDH_anon_WITH_AES_256_CBC_SHA           uint16 = 0xC019
190
-	cipher_TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA            uint16 = 0xC01A
191
-	cipher_TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA        uint16 = 0xC01B
192
-	cipher_TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA        uint16 = 0xC01C
193
-	cipher_TLS_SRP_SHA_WITH_AES_128_CBC_SHA             uint16 = 0xC01D
194
-	cipher_TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA         uint16 = 0xC01E
195
-	cipher_TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA         uint16 = 0xC01F
196
-	cipher_TLS_SRP_SHA_WITH_AES_256_CBC_SHA             uint16 = 0xC020
197
-	cipher_TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA         uint16 = 0xC021
198
-	cipher_TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA         uint16 = 0xC022
199
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256      uint16 = 0xC023
200
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384      uint16 = 0xC024
201
-	cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256       uint16 = 0xC025
202
-	cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384       uint16 = 0xC026
203
-	cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256        uint16 = 0xC027
204
-	cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384        uint16 = 0xC028
205
-	cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256         uint16 = 0xC029
206
-	cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384         uint16 = 0xC02A
207
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256      uint16 = 0xC02B
208
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384      uint16 = 0xC02C
209
-	cipher_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256       uint16 = 0xC02D
210
-	cipher_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384       uint16 = 0xC02E
211
-	cipher_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256        uint16 = 0xC02F
212
-	cipher_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384        uint16 = 0xC030
213
-	cipher_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256         uint16 = 0xC031
214
-	cipher_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384         uint16 = 0xC032
215
-	cipher_TLS_ECDHE_PSK_WITH_RC4_128_SHA               uint16 = 0xC033
216
-	cipher_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA          uint16 = 0xC034
217
-	cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA           uint16 = 0xC035
218
-	cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA           uint16 = 0xC036
219
-	cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256        uint16 = 0xC037
220
-	cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384        uint16 = 0xC038
221
-	cipher_TLS_ECDHE_PSK_WITH_NULL_SHA                  uint16 = 0xC039
222
-	cipher_TLS_ECDHE_PSK_WITH_NULL_SHA256               uint16 = 0xC03A
223
-	cipher_TLS_ECDHE_PSK_WITH_NULL_SHA384               uint16 = 0xC03B
224
-	cipher_TLS_RSA_WITH_ARIA_128_CBC_SHA256             uint16 = 0xC03C
225
-	cipher_TLS_RSA_WITH_ARIA_256_CBC_SHA384             uint16 = 0xC03D
226
-	cipher_TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256          uint16 = 0xC03E
227
-	cipher_TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384          uint16 = 0xC03F
228
-	cipher_TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256          uint16 = 0xC040
229
-	cipher_TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384          uint16 = 0xC041
230
-	cipher_TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC042
231
-	cipher_TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC043
232
-	cipher_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC044
233
-	cipher_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC045
234
-	cipher_TLS_DH_anon_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC046
235
-	cipher_TLS_DH_anon_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC047
236
-	cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256     uint16 = 0xC048
237
-	cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384     uint16 = 0xC049
238
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256      uint16 = 0xC04A
239
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384      uint16 = 0xC04B
240
-	cipher_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256       uint16 = 0xC04C
241
-	cipher_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384       uint16 = 0xC04D
242
-	cipher_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256        uint16 = 0xC04E
243
-	cipher_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384        uint16 = 0xC04F
244
-	cipher_TLS_RSA_WITH_ARIA_128_GCM_SHA256             uint16 = 0xC050
245
-	cipher_TLS_RSA_WITH_ARIA_256_GCM_SHA384             uint16 = 0xC051
246
-	cipher_TLS_DHE_RSA_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC052
247
-	cipher_TLS_DHE_RSA_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC053
248
-	cipher_TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256          uint16 = 0xC054
249
-	cipher_TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384          uint16 = 0xC055
250
-	cipher_TLS_DHE_DSS_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC056
251
-	cipher_TLS_DHE_DSS_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC057
252
-	cipher_TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256          uint16 = 0xC058
253
-	cipher_TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384          uint16 = 0xC059
254
-	cipher_TLS_DH_anon_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC05A
255
-	cipher_TLS_DH_anon_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC05B
256
-	cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_GCM_SHA256     uint16 = 0xC05C
257
-	cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_GCM_SHA384     uint16 = 0xC05D
258
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256      uint16 = 0xC05E
259
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384      uint16 = 0xC05F
260
-	cipher_TLS_ECDHE_RSA_WITH_ARIA_128_GCM_SHA256       uint16 = 0xC060
261
-	cipher_TLS_ECDHE_RSA_WITH_ARIA_256_GCM_SHA384       uint16 = 0xC061
262
-	cipher_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256        uint16 = 0xC062
263
-	cipher_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384        uint16 = 0xC063
264
-	cipher_TLS_PSK_WITH_ARIA_128_CBC_SHA256             uint16 = 0xC064
265
-	cipher_TLS_PSK_WITH_ARIA_256_CBC_SHA384             uint16 = 0xC065
266
-	cipher_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC066
267
-	cipher_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC067
268
-	cipher_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256         uint16 = 0xC068
269
-	cipher_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384         uint16 = 0xC069
270
-	cipher_TLS_PSK_WITH_ARIA_128_GCM_SHA256             uint16 = 0xC06A
271
-	cipher_TLS_PSK_WITH_ARIA_256_GCM_SHA384             uint16 = 0xC06B
272
-	cipher_TLS_DHE_PSK_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC06C
273
-	cipher_TLS_DHE_PSK_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC06D
274
-	cipher_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256         uint16 = 0xC06E
275
-	cipher_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384         uint16 = 0xC06F
276
-	cipher_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256       uint16 = 0xC070
277
-	cipher_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384       uint16 = 0xC071
278
-	cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256 uint16 = 0xC072
279
-	cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384 uint16 = 0xC073
280
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256  uint16 = 0xC074
281
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384  uint16 = 0xC075
282
-	cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256   uint16 = 0xC076
283
-	cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384   uint16 = 0xC077
284
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256    uint16 = 0xC078
285
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384    uint16 = 0xC079
286
-	cipher_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256         uint16 = 0xC07A
287
-	cipher_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384         uint16 = 0xC07B
288
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC07C
289
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC07D
290
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256      uint16 = 0xC07E
291
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384      uint16 = 0xC07F
292
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC080
293
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC081
294
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256      uint16 = 0xC082
295
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384      uint16 = 0xC083
296
-	cipher_TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC084
297
-	cipher_TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC085
298
-	cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_GCM_SHA256 uint16 = 0xC086
299
-	cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_GCM_SHA384 uint16 = 0xC087
300
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256  uint16 = 0xC088
301
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384  uint16 = 0xC089
302
-	cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_GCM_SHA256   uint16 = 0xC08A
303
-	cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_GCM_SHA384   uint16 = 0xC08B
304
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256    uint16 = 0xC08C
305
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384    uint16 = 0xC08D
306
-	cipher_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256         uint16 = 0xC08E
307
-	cipher_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384         uint16 = 0xC08F
308
-	cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC090
309
-	cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC091
310
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256     uint16 = 0xC092
311
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384     uint16 = 0xC093
312
-	cipher_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256         uint16 = 0xC094
313
-	cipher_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384         uint16 = 0xC095
314
-	cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256     uint16 = 0xC096
315
-	cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384     uint16 = 0xC097
316
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256     uint16 = 0xC098
317
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384     uint16 = 0xC099
318
-	cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256   uint16 = 0xC09A
319
-	cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384   uint16 = 0xC09B
320
-	cipher_TLS_RSA_WITH_AES_128_CCM                     uint16 = 0xC09C
321
-	cipher_TLS_RSA_WITH_AES_256_CCM                     uint16 = 0xC09D
322
-	cipher_TLS_DHE_RSA_WITH_AES_128_CCM                 uint16 = 0xC09E
323
-	cipher_TLS_DHE_RSA_WITH_AES_256_CCM                 uint16 = 0xC09F
324
-	cipher_TLS_RSA_WITH_AES_128_CCM_8                   uint16 = 0xC0A0
325
-	cipher_TLS_RSA_WITH_AES_256_CCM_8                   uint16 = 0xC0A1
326
-	cipher_TLS_DHE_RSA_WITH_AES_128_CCM_8               uint16 = 0xC0A2
327
-	cipher_TLS_DHE_RSA_WITH_AES_256_CCM_8               uint16 = 0xC0A3
328
-	cipher_TLS_PSK_WITH_AES_128_CCM                     uint16 = 0xC0A4
329
-	cipher_TLS_PSK_WITH_AES_256_CCM                     uint16 = 0xC0A5
330
-	cipher_TLS_DHE_PSK_WITH_AES_128_CCM                 uint16 = 0xC0A6
331
-	cipher_TLS_DHE_PSK_WITH_AES_256_CCM                 uint16 = 0xC0A7
332
-	cipher_TLS_PSK_WITH_AES_128_CCM_8                   uint16 = 0xC0A8
333
-	cipher_TLS_PSK_WITH_AES_256_CCM_8                   uint16 = 0xC0A9
334
-	cipher_TLS_PSK_DHE_WITH_AES_128_CCM_8               uint16 = 0xC0AA
335
-	cipher_TLS_PSK_DHE_WITH_AES_256_CCM_8               uint16 = 0xC0AB
336
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CCM             uint16 = 0xC0AC
337
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CCM             uint16 = 0xC0AD
338
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8           uint16 = 0xC0AE
339
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8           uint16 = 0xC0AF
340
-	// Unassigned uint16 =  0xC0B0-FF
341
-	// Unassigned uint16 =  0xC1-CB,*
342
-	// Unassigned uint16 =  0xCC00-A7
343
-	cipher_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xCCA8
344
-	cipher_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xCCA9
345
-	cipher_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256     uint16 = 0xCCAA
346
-	cipher_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256         uint16 = 0xCCAB
347
-	cipher_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256   uint16 = 0xCCAC
348
-	cipher_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256     uint16 = 0xCCAD
349
-	cipher_TLS_RSA_PSK_WITH_CHACHA20_POLY1305_SHA256     uint16 = 0xCCAE
350
-)
351
-
352
-// isBadCipher reports whether the cipher is blacklisted by the HTTP/2 spec.
353
-// References:
354
-// https://tools.ietf.org/html/rfc7540#appendix-A
355
-// Reject cipher suites from Appendix A.
356
-// "This list includes those cipher suites that do not
357
-// offer an ephemeral key exchange and those that are
358
-// based on the TLS null, stream or block cipher type"
359
-func isBadCipher(cipher uint16) bool {
360
-	switch cipher {
361
-	case cipher_TLS_NULL_WITH_NULL_NULL,
362
-		cipher_TLS_RSA_WITH_NULL_MD5,
363
-		cipher_TLS_RSA_WITH_NULL_SHA,
364
-		cipher_TLS_RSA_EXPORT_WITH_RC4_40_MD5,
365
-		cipher_TLS_RSA_WITH_RC4_128_MD5,
366
-		cipher_TLS_RSA_WITH_RC4_128_SHA,
367
-		cipher_TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5,
368
-		cipher_TLS_RSA_WITH_IDEA_CBC_SHA,
369
-		cipher_TLS_RSA_EXPORT_WITH_DES40_CBC_SHA,
370
-		cipher_TLS_RSA_WITH_DES_CBC_SHA,
371
-		cipher_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
372
-		cipher_TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,
373
-		cipher_TLS_DH_DSS_WITH_DES_CBC_SHA,
374
-		cipher_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA,
375
-		cipher_TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA,
376
-		cipher_TLS_DH_RSA_WITH_DES_CBC_SHA,
377
-		cipher_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA,
378
-		cipher_TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA,
379
-		cipher_TLS_DHE_DSS_WITH_DES_CBC_SHA,
380
-		cipher_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA,
381
-		cipher_TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA,
382
-		cipher_TLS_DHE_RSA_WITH_DES_CBC_SHA,
383
-		cipher_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
384
-		cipher_TLS_DH_anon_EXPORT_WITH_RC4_40_MD5,
385
-		cipher_TLS_DH_anon_WITH_RC4_128_MD5,
386
-		cipher_TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA,
387
-		cipher_TLS_DH_anon_WITH_DES_CBC_SHA,
388
-		cipher_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA,
389
-		cipher_TLS_KRB5_WITH_DES_CBC_SHA,
390
-		cipher_TLS_KRB5_WITH_3DES_EDE_CBC_SHA,
391
-		cipher_TLS_KRB5_WITH_RC4_128_SHA,
392
-		cipher_TLS_KRB5_WITH_IDEA_CBC_SHA,
393
-		cipher_TLS_KRB5_WITH_DES_CBC_MD5,
394
-		cipher_TLS_KRB5_WITH_3DES_EDE_CBC_MD5,
395
-		cipher_TLS_KRB5_WITH_RC4_128_MD5,
396
-		cipher_TLS_KRB5_WITH_IDEA_CBC_MD5,
397
-		cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA,
398
-		cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA,
399
-		cipher_TLS_KRB5_EXPORT_WITH_RC4_40_SHA,
400
-		cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5,
401
-		cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5,
402
-		cipher_TLS_KRB5_EXPORT_WITH_RC4_40_MD5,
403
-		cipher_TLS_PSK_WITH_NULL_SHA,
404
-		cipher_TLS_DHE_PSK_WITH_NULL_SHA,
405
-		cipher_TLS_RSA_PSK_WITH_NULL_SHA,
406
-		cipher_TLS_RSA_WITH_AES_128_CBC_SHA,
407
-		cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA,
408
-		cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA,
409
-		cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
410
-		cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
411
-		cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA,
412
-		cipher_TLS_RSA_WITH_AES_256_CBC_SHA,
413
-		cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA,
414
-		cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA,
415
-		cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA,
416
-		cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
417
-		cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA,
418
-		cipher_TLS_RSA_WITH_NULL_SHA256,
419
-		cipher_TLS_RSA_WITH_AES_128_CBC_SHA256,
420
-		cipher_TLS_RSA_WITH_AES_256_CBC_SHA256,
421
-		cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA256,
422
-		cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA256,
423
-		cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,
424
-		cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
425
-		cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA,
426
-		cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA,
427
-		cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA,
428
-		cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
429
-		cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA,
430
-		cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
431
-		cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA256,
432
-		cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA256,
433
-		cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,
434
-		cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
435
-		cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA256,
436
-		cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA256,
437
-		cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
438
-		cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA,
439
-		cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA,
440
-		cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA,
441
-		cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
442
-		cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA,
443
-		cipher_TLS_PSK_WITH_RC4_128_SHA,
444
-		cipher_TLS_PSK_WITH_3DES_EDE_CBC_SHA,
445
-		cipher_TLS_PSK_WITH_AES_128_CBC_SHA,
446
-		cipher_TLS_PSK_WITH_AES_256_CBC_SHA,
447
-		cipher_TLS_DHE_PSK_WITH_RC4_128_SHA,
448
-		cipher_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA,
449
-		cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA,
450
-		cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA,
451
-		cipher_TLS_RSA_PSK_WITH_RC4_128_SHA,
452
-		cipher_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA,
453
-		cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA,
454
-		cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA,
455
-		cipher_TLS_RSA_WITH_SEED_CBC_SHA,
456
-		cipher_TLS_DH_DSS_WITH_SEED_CBC_SHA,
457
-		cipher_TLS_DH_RSA_WITH_SEED_CBC_SHA,
458
-		cipher_TLS_DHE_DSS_WITH_SEED_CBC_SHA,
459
-		cipher_TLS_DHE_RSA_WITH_SEED_CBC_SHA,
460
-		cipher_TLS_DH_anon_WITH_SEED_CBC_SHA,
461
-		cipher_TLS_RSA_WITH_AES_128_GCM_SHA256,
462
-		cipher_TLS_RSA_WITH_AES_256_GCM_SHA384,
463
-		cipher_TLS_DH_RSA_WITH_AES_128_GCM_SHA256,
464
-		cipher_TLS_DH_RSA_WITH_AES_256_GCM_SHA384,
465
-		cipher_TLS_DH_DSS_WITH_AES_128_GCM_SHA256,
466
-		cipher_TLS_DH_DSS_WITH_AES_256_GCM_SHA384,
467
-		cipher_TLS_DH_anon_WITH_AES_128_GCM_SHA256,
468
-		cipher_TLS_DH_anon_WITH_AES_256_GCM_SHA384,
469
-		cipher_TLS_PSK_WITH_AES_128_GCM_SHA256,
470
-		cipher_TLS_PSK_WITH_AES_256_GCM_SHA384,
471
-		cipher_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256,
472
-		cipher_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384,
473
-		cipher_TLS_PSK_WITH_AES_128_CBC_SHA256,
474
-		cipher_TLS_PSK_WITH_AES_256_CBC_SHA384,
475
-		cipher_TLS_PSK_WITH_NULL_SHA256,
476
-		cipher_TLS_PSK_WITH_NULL_SHA384,
477
-		cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
478
-		cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384,
479
-		cipher_TLS_DHE_PSK_WITH_NULL_SHA256,
480
-		cipher_TLS_DHE_PSK_WITH_NULL_SHA384,
481
-		cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256,
482
-		cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384,
483
-		cipher_TLS_RSA_PSK_WITH_NULL_SHA256,
484
-		cipher_TLS_RSA_PSK_WITH_NULL_SHA384,
485
-		cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
486
-		cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256,
487
-		cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
488
-		cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256,
489
-		cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
490
-		cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256,
491
-		cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
492
-		cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256,
493
-		cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256,
494
-		cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256,
495
-		cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
496
-		cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256,
497
-		cipher_TLS_EMPTY_RENEGOTIATION_INFO_SCSV,
498
-		cipher_TLS_ECDH_ECDSA_WITH_NULL_SHA,
499
-		cipher_TLS_ECDH_ECDSA_WITH_RC4_128_SHA,
500
-		cipher_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
501
-		cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
502
-		cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
503
-		cipher_TLS_ECDHE_ECDSA_WITH_NULL_SHA,
504
-		cipher_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
505
-		cipher_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
506
-		cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
507
-		cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
508
-		cipher_TLS_ECDH_RSA_WITH_NULL_SHA,
509
-		cipher_TLS_ECDH_RSA_WITH_RC4_128_SHA,
510
-		cipher_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
511
-		cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
512
-		cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
513
-		cipher_TLS_ECDHE_RSA_WITH_NULL_SHA,
514
-		cipher_TLS_ECDHE_RSA_WITH_RC4_128_SHA,
515
-		cipher_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
516
-		cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
517
-		cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
518
-		cipher_TLS_ECDH_anon_WITH_NULL_SHA,
519
-		cipher_TLS_ECDH_anon_WITH_RC4_128_SHA,
520
-		cipher_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA,
521
-		cipher_TLS_ECDH_anon_WITH_AES_128_CBC_SHA,
522
-		cipher_TLS_ECDH_anon_WITH_AES_256_CBC_SHA,
523
-		cipher_TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA,
524
-		cipher_TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA,
525
-		cipher_TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA,
526
-		cipher_TLS_SRP_SHA_WITH_AES_128_CBC_SHA,
527
-		cipher_TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA,
528
-		cipher_TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA,
529
-		cipher_TLS_SRP_SHA_WITH_AES_256_CBC_SHA,
530
-		cipher_TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
531
-		cipher_TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA,
532
-		cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
533
-		cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
534
-		cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
535
-		cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
536
-		cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
537
-		cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
538
-		cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
539
-		cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
540
-		cipher_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
541
-		cipher_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
542
-		cipher_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
543
-		cipher_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
544
-		cipher_TLS_ECDHE_PSK_WITH_RC4_128_SHA,
545
-		cipher_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA,
546
-		cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
547
-		cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA,
548
-		cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
549
-		cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384,
550
-		cipher_TLS_ECDHE_PSK_WITH_NULL_SHA,
551
-		cipher_TLS_ECDHE_PSK_WITH_NULL_SHA256,
552
-		cipher_TLS_ECDHE_PSK_WITH_NULL_SHA384,
553
-		cipher_TLS_RSA_WITH_ARIA_128_CBC_SHA256,
554
-		cipher_TLS_RSA_WITH_ARIA_256_CBC_SHA384,
555
-		cipher_TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256,
556
-		cipher_TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384,
557
-		cipher_TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256,
558
-		cipher_TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384,
559
-		cipher_TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256,
560
-		cipher_TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384,
561
-		cipher_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256,
562
-		cipher_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384,
563
-		cipher_TLS_DH_anon_WITH_ARIA_128_CBC_SHA256,
564
-		cipher_TLS_DH_anon_WITH_ARIA_256_CBC_SHA384,
565
-		cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256,
566
-		cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384,
567
-		cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256,
568
-		cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384,
569
-		cipher_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256,
570
-		cipher_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384,
571
-		cipher_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256,
572
-		cipher_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384,
573
-		cipher_TLS_RSA_WITH_ARIA_128_GCM_SHA256,
574
-		cipher_TLS_RSA_WITH_ARIA_256_GCM_SHA384,
575
-		cipher_TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256,
576
-		cipher_TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384,
577
-		cipher_TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256,
578
-		cipher_TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384,
579
-		cipher_TLS_DH_anon_WITH_ARIA_128_GCM_SHA256,
580
-		cipher_TLS_DH_anon_WITH_ARIA_256_GCM_SHA384,
581
-		cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256,
582
-		cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384,
583
-		cipher_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256,
584
-		cipher_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384,
585
-		cipher_TLS_PSK_WITH_ARIA_128_CBC_SHA256,
586
-		cipher_TLS_PSK_WITH_ARIA_256_CBC_SHA384,
587
-		cipher_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256,
588
-		cipher_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384,
589
-		cipher_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256,
590
-		cipher_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384,
591
-		cipher_TLS_PSK_WITH_ARIA_128_GCM_SHA256,
592
-		cipher_TLS_PSK_WITH_ARIA_256_GCM_SHA384,
593
-		cipher_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256,
594
-		cipher_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384,
595
-		cipher_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256,
596
-		cipher_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384,
597
-		cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
598
-		cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
599
-		cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
600
-		cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
601
-		cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
602
-		cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384,
603
-		cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
604
-		cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384,
605
-		cipher_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256,
606
-		cipher_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384,
607
-		cipher_TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
608
-		cipher_TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
609
-		cipher_TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256,
610
-		cipher_TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384,
611
-		cipher_TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256,
612
-		cipher_TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384,
613
-		cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256,
614
-		cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384,
615
-		cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
616
-		cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
617
-		cipher_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256,
618
-		cipher_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384,
619
-		cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256,
620
-		cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384,
621
-		cipher_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256,
622
-		cipher_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384,
623
-		cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
624
-		cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
625
-		cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256,
626
-		cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384,
627
-		cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
628
-		cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
629
-		cipher_TLS_RSA_WITH_AES_128_CCM,
630
-		cipher_TLS_RSA_WITH_AES_256_CCM,
631
-		cipher_TLS_RSA_WITH_AES_128_CCM_8,
632
-		cipher_TLS_RSA_WITH_AES_256_CCM_8,
633
-		cipher_TLS_PSK_WITH_AES_128_CCM,
634
-		cipher_TLS_PSK_WITH_AES_256_CCM,
635
-		cipher_TLS_PSK_WITH_AES_128_CCM_8,
636
-		cipher_TLS_PSK_WITH_AES_256_CCM_8:
637
-		return true
638
-	default:
639
-		return false
640
-	}
641
-}

+ 0
- 309
vendor/golang.org/x/net/http2/ciphers_test.go View File

1
-// Copyright 2017 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 http2
6
-
7
-import "testing"
8
-
9
-func TestIsBadCipherBad(t *testing.T) {
10
-	for _, c := range badCiphers {
11
-		if !isBadCipher(c) {
12
-			t.Errorf("Wrong result for isBadCipher(%d), want true", c)
13
-		}
14
-	}
15
-}
16
-
17
-// verify we don't give false positives on ciphers not on blacklist
18
-func TestIsBadCipherGood(t *testing.T) {
19
-	goodCiphers := map[uint16]string{
20
-		cipher_TLS_DHE_RSA_WITH_AES_256_CCM:                "cipher_TLS_DHE_RSA_WITH_AES_256_CCM",
21
-		cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CCM:            "cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CCM",
22
-		cipher_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256: "cipher_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",
23
-	}
24
-	for c, name := range goodCiphers {
25
-		if isBadCipher(c) {
26
-			t.Errorf("Wrong result for isBadCipher(%d) %s, want false", c, name)
27
-		}
28
-	}
29
-}
30
-
31
-// copied from https://http2.github.io/http2-spec/#BadCipherSuites,
32
-var badCiphers = []uint16{
33
-	cipher_TLS_NULL_WITH_NULL_NULL,
34
-	cipher_TLS_RSA_WITH_NULL_MD5,
35
-	cipher_TLS_RSA_WITH_NULL_SHA,
36
-	cipher_TLS_RSA_EXPORT_WITH_RC4_40_MD5,
37
-	cipher_TLS_RSA_WITH_RC4_128_MD5,
38
-	cipher_TLS_RSA_WITH_RC4_128_SHA,
39
-	cipher_TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5,
40
-	cipher_TLS_RSA_WITH_IDEA_CBC_SHA,
41
-	cipher_TLS_RSA_EXPORT_WITH_DES40_CBC_SHA,
42
-	cipher_TLS_RSA_WITH_DES_CBC_SHA,
43
-	cipher_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
44
-	cipher_TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,
45
-	cipher_TLS_DH_DSS_WITH_DES_CBC_SHA,
46
-	cipher_TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA,
47
-	cipher_TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA,
48
-	cipher_TLS_DH_RSA_WITH_DES_CBC_SHA,
49
-	cipher_TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA,
50
-	cipher_TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA,
51
-	cipher_TLS_DHE_DSS_WITH_DES_CBC_SHA,
52
-	cipher_TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA,
53
-	cipher_TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA,
54
-	cipher_TLS_DHE_RSA_WITH_DES_CBC_SHA,
55
-	cipher_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA,
56
-	cipher_TLS_DH_anon_EXPORT_WITH_RC4_40_MD5,
57
-	cipher_TLS_DH_anon_WITH_RC4_128_MD5,
58
-	cipher_TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA,
59
-	cipher_TLS_DH_anon_WITH_DES_CBC_SHA,
60
-	cipher_TLS_DH_anon_WITH_3DES_EDE_CBC_SHA,
61
-	cipher_TLS_KRB5_WITH_DES_CBC_SHA,
62
-	cipher_TLS_KRB5_WITH_3DES_EDE_CBC_SHA,
63
-	cipher_TLS_KRB5_WITH_RC4_128_SHA,
64
-	cipher_TLS_KRB5_WITH_IDEA_CBC_SHA,
65
-	cipher_TLS_KRB5_WITH_DES_CBC_MD5,
66
-	cipher_TLS_KRB5_WITH_3DES_EDE_CBC_MD5,
67
-	cipher_TLS_KRB5_WITH_RC4_128_MD5,
68
-	cipher_TLS_KRB5_WITH_IDEA_CBC_MD5,
69
-	cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA,
70
-	cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_SHA,
71
-	cipher_TLS_KRB5_EXPORT_WITH_RC4_40_SHA,
72
-	cipher_TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5,
73
-	cipher_TLS_KRB5_EXPORT_WITH_RC2_CBC_40_MD5,
74
-	cipher_TLS_KRB5_EXPORT_WITH_RC4_40_MD5,
75
-	cipher_TLS_PSK_WITH_NULL_SHA,
76
-	cipher_TLS_DHE_PSK_WITH_NULL_SHA,
77
-	cipher_TLS_RSA_PSK_WITH_NULL_SHA,
78
-	cipher_TLS_RSA_WITH_AES_128_CBC_SHA,
79
-	cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA,
80
-	cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA,
81
-	cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
82
-	cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
83
-	cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA,
84
-	cipher_TLS_RSA_WITH_AES_256_CBC_SHA,
85
-	cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA,
86
-	cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA,
87
-	cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA,
88
-	cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
89
-	cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA,
90
-	cipher_TLS_RSA_WITH_NULL_SHA256,
91
-	cipher_TLS_RSA_WITH_AES_128_CBC_SHA256,
92
-	cipher_TLS_RSA_WITH_AES_256_CBC_SHA256,
93
-	cipher_TLS_DH_DSS_WITH_AES_128_CBC_SHA256,
94
-	cipher_TLS_DH_RSA_WITH_AES_128_CBC_SHA256,
95
-	cipher_TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,
96
-	cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,
97
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA,
98
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA,
99
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA,
100
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,
101
-	cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA,
102
-	cipher_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
103
-	cipher_TLS_DH_DSS_WITH_AES_256_CBC_SHA256,
104
-	cipher_TLS_DH_RSA_WITH_AES_256_CBC_SHA256,
105
-	cipher_TLS_DHE_DSS_WITH_AES_256_CBC_SHA256,
106
-	cipher_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256,
107
-	cipher_TLS_DH_anon_WITH_AES_128_CBC_SHA256,
108
-	cipher_TLS_DH_anon_WITH_AES_256_CBC_SHA256,
109
-	cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,
110
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA,
111
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA,
112
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA,
113
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,
114
-	cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA,
115
-	cipher_TLS_PSK_WITH_RC4_128_SHA,
116
-	cipher_TLS_PSK_WITH_3DES_EDE_CBC_SHA,
117
-	cipher_TLS_PSK_WITH_AES_128_CBC_SHA,
118
-	cipher_TLS_PSK_WITH_AES_256_CBC_SHA,
119
-	cipher_TLS_DHE_PSK_WITH_RC4_128_SHA,
120
-	cipher_TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA,
121
-	cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA,
122
-	cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA,
123
-	cipher_TLS_RSA_PSK_WITH_RC4_128_SHA,
124
-	cipher_TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA,
125
-	cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA,
126
-	cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA,
127
-	cipher_TLS_RSA_WITH_SEED_CBC_SHA,
128
-	cipher_TLS_DH_DSS_WITH_SEED_CBC_SHA,
129
-	cipher_TLS_DH_RSA_WITH_SEED_CBC_SHA,
130
-	cipher_TLS_DHE_DSS_WITH_SEED_CBC_SHA,
131
-	cipher_TLS_DHE_RSA_WITH_SEED_CBC_SHA,
132
-	cipher_TLS_DH_anon_WITH_SEED_CBC_SHA,
133
-	cipher_TLS_RSA_WITH_AES_128_GCM_SHA256,
134
-	cipher_TLS_RSA_WITH_AES_256_GCM_SHA384,
135
-	cipher_TLS_DH_RSA_WITH_AES_128_GCM_SHA256,
136
-	cipher_TLS_DH_RSA_WITH_AES_256_GCM_SHA384,
137
-	cipher_TLS_DH_DSS_WITH_AES_128_GCM_SHA256,
138
-	cipher_TLS_DH_DSS_WITH_AES_256_GCM_SHA384,
139
-	cipher_TLS_DH_anon_WITH_AES_128_GCM_SHA256,
140
-	cipher_TLS_DH_anon_WITH_AES_256_GCM_SHA384,
141
-	cipher_TLS_PSK_WITH_AES_128_GCM_SHA256,
142
-	cipher_TLS_PSK_WITH_AES_256_GCM_SHA384,
143
-	cipher_TLS_RSA_PSK_WITH_AES_128_GCM_SHA256,
144
-	cipher_TLS_RSA_PSK_WITH_AES_256_GCM_SHA384,
145
-	cipher_TLS_PSK_WITH_AES_128_CBC_SHA256,
146
-	cipher_TLS_PSK_WITH_AES_256_CBC_SHA384,
147
-	cipher_TLS_PSK_WITH_NULL_SHA256,
148
-	cipher_TLS_PSK_WITH_NULL_SHA384,
149
-	cipher_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,
150
-	cipher_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384,
151
-	cipher_TLS_DHE_PSK_WITH_NULL_SHA256,
152
-	cipher_TLS_DHE_PSK_WITH_NULL_SHA384,
153
-	cipher_TLS_RSA_PSK_WITH_AES_128_CBC_SHA256,
154
-	cipher_TLS_RSA_PSK_WITH_AES_256_CBC_SHA384,
155
-	cipher_TLS_RSA_PSK_WITH_NULL_SHA256,
156
-	cipher_TLS_RSA_PSK_WITH_NULL_SHA384,
157
-	cipher_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,
158
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA256,
159
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
160
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA256,
161
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
162
-	cipher_TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256,
163
-	cipher_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,
164
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA256,
165
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA256,
166
-	cipher_TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA256,
167
-	cipher_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,
168
-	cipher_TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA256,
169
-	cipher_TLS_EMPTY_RENEGOTIATION_INFO_SCSV,
170
-	cipher_TLS_ECDH_ECDSA_WITH_NULL_SHA,
171
-	cipher_TLS_ECDH_ECDSA_WITH_RC4_128_SHA,
172
-	cipher_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
173
-	cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
174
-	cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
175
-	cipher_TLS_ECDHE_ECDSA_WITH_NULL_SHA,
176
-	cipher_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
177
-	cipher_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
178
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
179
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
180
-	cipher_TLS_ECDH_RSA_WITH_NULL_SHA,
181
-	cipher_TLS_ECDH_RSA_WITH_RC4_128_SHA,
182
-	cipher_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
183
-	cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
184
-	cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
185
-	cipher_TLS_ECDHE_RSA_WITH_NULL_SHA,
186
-	cipher_TLS_ECDHE_RSA_WITH_RC4_128_SHA,
187
-	cipher_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
188
-	cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
189
-	cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
190
-	cipher_TLS_ECDH_anon_WITH_NULL_SHA,
191
-	cipher_TLS_ECDH_anon_WITH_RC4_128_SHA,
192
-	cipher_TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA,
193
-	cipher_TLS_ECDH_anon_WITH_AES_128_CBC_SHA,
194
-	cipher_TLS_ECDH_anon_WITH_AES_256_CBC_SHA,
195
-	cipher_TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA,
196
-	cipher_TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA,
197
-	cipher_TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA,
198
-	cipher_TLS_SRP_SHA_WITH_AES_128_CBC_SHA,
199
-	cipher_TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA,
200
-	cipher_TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA,
201
-	cipher_TLS_SRP_SHA_WITH_AES_256_CBC_SHA,
202
-	cipher_TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA,
203
-	cipher_TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA,
204
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
205
-	cipher_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
206
-	cipher_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
207
-	cipher_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
208
-	cipher_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
209
-	cipher_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
210
-	cipher_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
211
-	cipher_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
212
-	cipher_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
213
-	cipher_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
214
-	cipher_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
215
-	cipher_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
216
-	cipher_TLS_ECDHE_PSK_WITH_RC4_128_SHA,
217
-	cipher_TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA,
218
-	cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA,
219
-	cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA,
220
-	cipher_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,
221
-	cipher_TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384,
222
-	cipher_TLS_ECDHE_PSK_WITH_NULL_SHA,
223
-	cipher_TLS_ECDHE_PSK_WITH_NULL_SHA256,
224
-	cipher_TLS_ECDHE_PSK_WITH_NULL_SHA384,
225
-	cipher_TLS_RSA_WITH_ARIA_128_CBC_SHA256,
226
-	cipher_TLS_RSA_WITH_ARIA_256_CBC_SHA384,
227
-	cipher_TLS_DH_DSS_WITH_ARIA_128_CBC_SHA256,
228
-	cipher_TLS_DH_DSS_WITH_ARIA_256_CBC_SHA384,
229
-	cipher_TLS_DH_RSA_WITH_ARIA_128_CBC_SHA256,
230
-	cipher_TLS_DH_RSA_WITH_ARIA_256_CBC_SHA384,
231
-	cipher_TLS_DHE_DSS_WITH_ARIA_128_CBC_SHA256,
232
-	cipher_TLS_DHE_DSS_WITH_ARIA_256_CBC_SHA384,
233
-	cipher_TLS_DHE_RSA_WITH_ARIA_128_CBC_SHA256,
234
-	cipher_TLS_DHE_RSA_WITH_ARIA_256_CBC_SHA384,
235
-	cipher_TLS_DH_anon_WITH_ARIA_128_CBC_SHA256,
236
-	cipher_TLS_DH_anon_WITH_ARIA_256_CBC_SHA384,
237
-	cipher_TLS_ECDHE_ECDSA_WITH_ARIA_128_CBC_SHA256,
238
-	cipher_TLS_ECDHE_ECDSA_WITH_ARIA_256_CBC_SHA384,
239
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_CBC_SHA256,
240
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_CBC_SHA384,
241
-	cipher_TLS_ECDHE_RSA_WITH_ARIA_128_CBC_SHA256,
242
-	cipher_TLS_ECDHE_RSA_WITH_ARIA_256_CBC_SHA384,
243
-	cipher_TLS_ECDH_RSA_WITH_ARIA_128_CBC_SHA256,
244
-	cipher_TLS_ECDH_RSA_WITH_ARIA_256_CBC_SHA384,
245
-	cipher_TLS_RSA_WITH_ARIA_128_GCM_SHA256,
246
-	cipher_TLS_RSA_WITH_ARIA_256_GCM_SHA384,
247
-	cipher_TLS_DH_RSA_WITH_ARIA_128_GCM_SHA256,
248
-	cipher_TLS_DH_RSA_WITH_ARIA_256_GCM_SHA384,
249
-	cipher_TLS_DH_DSS_WITH_ARIA_128_GCM_SHA256,
250
-	cipher_TLS_DH_DSS_WITH_ARIA_256_GCM_SHA384,
251
-	cipher_TLS_DH_anon_WITH_ARIA_128_GCM_SHA256,
252
-	cipher_TLS_DH_anon_WITH_ARIA_256_GCM_SHA384,
253
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_128_GCM_SHA256,
254
-	cipher_TLS_ECDH_ECDSA_WITH_ARIA_256_GCM_SHA384,
255
-	cipher_TLS_ECDH_RSA_WITH_ARIA_128_GCM_SHA256,
256
-	cipher_TLS_ECDH_RSA_WITH_ARIA_256_GCM_SHA384,
257
-	cipher_TLS_PSK_WITH_ARIA_128_CBC_SHA256,
258
-	cipher_TLS_PSK_WITH_ARIA_256_CBC_SHA384,
259
-	cipher_TLS_DHE_PSK_WITH_ARIA_128_CBC_SHA256,
260
-	cipher_TLS_DHE_PSK_WITH_ARIA_256_CBC_SHA384,
261
-	cipher_TLS_RSA_PSK_WITH_ARIA_128_CBC_SHA256,
262
-	cipher_TLS_RSA_PSK_WITH_ARIA_256_CBC_SHA384,
263
-	cipher_TLS_PSK_WITH_ARIA_128_GCM_SHA256,
264
-	cipher_TLS_PSK_WITH_ARIA_256_GCM_SHA384,
265
-	cipher_TLS_RSA_PSK_WITH_ARIA_128_GCM_SHA256,
266
-	cipher_TLS_RSA_PSK_WITH_ARIA_256_GCM_SHA384,
267
-	cipher_TLS_ECDHE_PSK_WITH_ARIA_128_CBC_SHA256,
268
-	cipher_TLS_ECDHE_PSK_WITH_ARIA_256_CBC_SHA384,
269
-	cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
270
-	cipher_TLS_ECDHE_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
271
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_CBC_SHA256,
272
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_CBC_SHA384,
273
-	cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,
274
-	cipher_TLS_ECDHE_RSA_WITH_CAMELLIA_256_CBC_SHA384,
275
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_CBC_SHA256,
276
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_CBC_SHA384,
277
-	cipher_TLS_RSA_WITH_CAMELLIA_128_GCM_SHA256,
278
-	cipher_TLS_RSA_WITH_CAMELLIA_256_GCM_SHA384,
279
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
280
-	cipher_TLS_DH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
281
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_128_GCM_SHA256,
282
-	cipher_TLS_DH_DSS_WITH_CAMELLIA_256_GCM_SHA384,
283
-	cipher_TLS_DH_anon_WITH_CAMELLIA_128_GCM_SHA256,
284
-	cipher_TLS_DH_anon_WITH_CAMELLIA_256_GCM_SHA384,
285
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_128_GCM_SHA256,
286
-	cipher_TLS_ECDH_ECDSA_WITH_CAMELLIA_256_GCM_SHA384,
287
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_128_GCM_SHA256,
288
-	cipher_TLS_ECDH_RSA_WITH_CAMELLIA_256_GCM_SHA384,
289
-	cipher_TLS_PSK_WITH_CAMELLIA_128_GCM_SHA256,
290
-	cipher_TLS_PSK_WITH_CAMELLIA_256_GCM_SHA384,
291
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_GCM_SHA256,
292
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_GCM_SHA384,
293
-	cipher_TLS_PSK_WITH_CAMELLIA_128_CBC_SHA256,
294
-	cipher_TLS_PSK_WITH_CAMELLIA_256_CBC_SHA384,
295
-	cipher_TLS_DHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
296
-	cipher_TLS_DHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
297
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_128_CBC_SHA256,
298
-	cipher_TLS_RSA_PSK_WITH_CAMELLIA_256_CBC_SHA384,
299
-	cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_128_CBC_SHA256,
300
-	cipher_TLS_ECDHE_PSK_WITH_CAMELLIA_256_CBC_SHA384,
301
-	cipher_TLS_RSA_WITH_AES_128_CCM,
302
-	cipher_TLS_RSA_WITH_AES_256_CCM,
303
-	cipher_TLS_RSA_WITH_AES_128_CCM_8,
304
-	cipher_TLS_RSA_WITH_AES_256_CCM_8,
305
-	cipher_TLS_PSK_WITH_AES_128_CCM,
306
-	cipher_TLS_PSK_WITH_AES_256_CCM,
307
-	cipher_TLS_PSK_WITH_AES_128_CCM_8,
308
-	cipher_TLS_PSK_WITH_AES_256_CCM_8,
309
-}

+ 0
- 256
vendor/golang.org/x/net/http2/client_conn_pool.go View File

1
-// Copyright 2015 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
-// Transport code's client connection pooling.
6
-
7
-package http2
8
-
9
-import (
10
-	"crypto/tls"
11
-	"net/http"
12
-	"sync"
13
-)
14
-
15
-// ClientConnPool manages a pool of HTTP/2 client connections.
16
-type ClientConnPool interface {
17
-	GetClientConn(req *http.Request, addr string) (*ClientConn, error)
18
-	MarkDead(*ClientConn)
19
-}
20
-
21
-// clientConnPoolIdleCloser is the interface implemented by ClientConnPool
22
-// implementations which can close their idle connections.
23
-type clientConnPoolIdleCloser interface {
24
-	ClientConnPool
25
-	closeIdleConnections()
26
-}
27
-
28
-var (
29
-	_ clientConnPoolIdleCloser = (*clientConnPool)(nil)
30
-	_ clientConnPoolIdleCloser = noDialClientConnPool{}
31
-)
32
-
33
-// TODO: use singleflight for dialing and addConnCalls?
34
-type clientConnPool struct {
35
-	t *Transport
36
-
37
-	mu sync.Mutex // TODO: maybe switch to RWMutex
38
-	// TODO: add support for sharing conns based on cert names
39
-	// (e.g. share conn for googleapis.com and appspot.com)
40
-	conns        map[string][]*ClientConn // key is host:port
41
-	dialing      map[string]*dialCall     // currently in-flight dials
42
-	keys         map[*ClientConn][]string
43
-	addConnCalls map[string]*addConnCall // in-flight addConnIfNeede calls
44
-}
45
-
46
-func (p *clientConnPool) GetClientConn(req *http.Request, addr string) (*ClientConn, error) {
47
-	return p.getClientConn(req, addr, dialOnMiss)
48
-}
49
-
50
-const (
51
-	dialOnMiss   = true
52
-	noDialOnMiss = false
53
-)
54
-
55
-func (p *clientConnPool) getClientConn(req *http.Request, addr string, dialOnMiss bool) (*ClientConn, error) {
56
-	if isConnectionCloseRequest(req) && dialOnMiss {
57
-		// It gets its own connection.
58
-		const singleUse = true
59
-		cc, err := p.t.dialClientConn(addr, singleUse)
60
-		if err != nil {
61
-			return nil, err
62
-		}
63
-		return cc, nil
64
-	}
65
-	p.mu.Lock()
66
-	for _, cc := range p.conns[addr] {
67
-		if cc.CanTakeNewRequest() {
68
-			p.mu.Unlock()
69
-			return cc, nil
70
-		}
71
-	}
72
-	if !dialOnMiss {
73
-		p.mu.Unlock()
74
-		return nil, ErrNoCachedConn
75
-	}
76
-	call := p.getStartDialLocked(addr)
77
-	p.mu.Unlock()
78
-	<-call.done
79
-	return call.res, call.err
80
-}
81
-
82
-// dialCall is an in-flight Transport dial call to a host.
83
-type dialCall struct {
84
-	p    *clientConnPool
85
-	done chan struct{} // closed when done
86
-	res  *ClientConn   // valid after done is closed
87
-	err  error         // valid after done is closed
88
-}
89
-
90
-// requires p.mu is held.
91
-func (p *clientConnPool) getStartDialLocked(addr string) *dialCall {
92
-	if call, ok := p.dialing[addr]; ok {
93
-		// A dial is already in-flight. Don't start another.
94
-		return call
95
-	}
96
-	call := &dialCall{p: p, done: make(chan struct{})}
97
-	if p.dialing == nil {
98
-		p.dialing = make(map[string]*dialCall)
99
-	}
100
-	p.dialing[addr] = call
101
-	go call.dial(addr)
102
-	return call
103
-}
104
-
105
-// run in its own goroutine.
106
-func (c *dialCall) dial(addr string) {
107
-	const singleUse = false // shared conn
108
-	c.res, c.err = c.p.t.dialClientConn(addr, singleUse)
109
-	close(c.done)
110
-
111
-	c.p.mu.Lock()
112
-	delete(c.p.dialing, addr)
113
-	if c.err == nil {
114
-		c.p.addConnLocked(addr, c.res)
115
-	}
116
-	c.p.mu.Unlock()
117
-}
118
-
119
-// addConnIfNeeded makes a NewClientConn out of c if a connection for key doesn't
120
-// already exist. It coalesces concurrent calls with the same key.
121
-// This is used by the http1 Transport code when it creates a new connection. Because
122
-// the http1 Transport doesn't de-dup TCP dials to outbound hosts (because it doesn't know
123
-// the protocol), it can get into a situation where it has multiple TLS connections.
124
-// This code decides which ones live or die.
125
-// The return value used is whether c was used.
126
-// c is never closed.
127
-func (p *clientConnPool) addConnIfNeeded(key string, t *Transport, c *tls.Conn) (used bool, err error) {
128
-	p.mu.Lock()
129
-	for _, cc := range p.conns[key] {
130
-		if cc.CanTakeNewRequest() {
131
-			p.mu.Unlock()
132
-			return false, nil
133
-		}
134
-	}
135
-	call, dup := p.addConnCalls[key]
136
-	if !dup {
137
-		if p.addConnCalls == nil {
138
-			p.addConnCalls = make(map[string]*addConnCall)
139
-		}
140
-		call = &addConnCall{
141
-			p:    p,
142
-			done: make(chan struct{}),
143
-		}
144
-		p.addConnCalls[key] = call
145
-		go call.run(t, key, c)
146
-	}
147
-	p.mu.Unlock()
148
-
149
-	<-call.done
150
-	if call.err != nil {
151
-		return false, call.err
152
-	}
153
-	return !dup, nil
154
-}
155
-
156
-type addConnCall struct {
157
-	p    *clientConnPool
158
-	done chan struct{} // closed when done
159
-	err  error
160
-}
161
-
162
-func (c *addConnCall) run(t *Transport, key string, tc *tls.Conn) {
163
-	cc, err := t.NewClientConn(tc)
164
-
165
-	p := c.p
166
-	p.mu.Lock()
167
-	if err != nil {
168
-		c.err = err
169
-	} else {
170
-		p.addConnLocked(key, cc)
171
-	}
172
-	delete(p.addConnCalls, key)
173
-	p.mu.Unlock()
174
-	close(c.done)
175
-}
176
-
177
-func (p *clientConnPool) addConn(key string, cc *ClientConn) {
178
-	p.mu.Lock()
179
-	p.addConnLocked(key, cc)
180
-	p.mu.Unlock()
181
-}
182
-
183
-// p.mu must be held
184
-func (p *clientConnPool) addConnLocked(key string, cc *ClientConn) {
185
-	for _, v := range p.conns[key] {
186
-		if v == cc {
187
-			return
188
-		}
189
-	}
190
-	if p.conns == nil {
191
-		p.conns = make(map[string][]*ClientConn)
192
-	}
193
-	if p.keys == nil {
194
-		p.keys = make(map[*ClientConn][]string)
195
-	}
196
-	p.conns[key] = append(p.conns[key], cc)
197
-	p.keys[cc] = append(p.keys[cc], key)
198
-}
199
-
200
-func (p *clientConnPool) MarkDead(cc *ClientConn) {
201
-	p.mu.Lock()
202
-	defer p.mu.Unlock()
203
-	for _, key := range p.keys[cc] {
204
-		vv, ok := p.conns[key]
205
-		if !ok {
206
-			continue
207
-		}
208
-		newList := filterOutClientConn(vv, cc)
209
-		if len(newList) > 0 {
210
-			p.conns[key] = newList
211
-		} else {
212
-			delete(p.conns, key)
213
-		}
214
-	}
215
-	delete(p.keys, cc)
216
-}
217
-
218
-func (p *clientConnPool) closeIdleConnections() {
219
-	p.mu.Lock()
220
-	defer p.mu.Unlock()
221
-	// TODO: don't close a cc if it was just added to the pool
222
-	// milliseconds ago and has never been used. There's currently
223
-	// a small race window with the HTTP/1 Transport's integration
224
-	// where it can add an idle conn just before using it, and
225
-	// somebody else can concurrently call CloseIdleConns and
226
-	// break some caller's RoundTrip.
227
-	for _, vv := range p.conns {
228
-		for _, cc := range vv {
229
-			cc.closeIfIdle()
230
-		}
231
-	}
232
-}
233
-
234
-func filterOutClientConn(in []*ClientConn, exclude *ClientConn) []*ClientConn {
235
-	out := in[:0]
236
-	for _, v := range in {
237
-		if v != exclude {
238
-			out = append(out, v)
239
-		}
240
-	}
241
-	// If we filtered it out, zero out the last item to prevent
242
-	// the GC from seeing it.
243
-	if len(in) != len(out) {
244
-		in[len(in)-1] = nil
245
-	}
246
-	return out
247
-}
248
-
249
-// noDialClientConnPool is an implementation of http2.ClientConnPool
250
-// which never dials. We let the HTTP/1.1 client dial and use its TLS
251
-// connection instead.
252
-type noDialClientConnPool struct{ *clientConnPool }
253
-
254
-func (p noDialClientConnPool) GetClientConn(req *http.Request, addr string) (*ClientConn, error) {
255
-	return p.getClientConn(req, addr, noDialOnMiss)
256
-}

+ 0
- 80
vendor/golang.org/x/net/http2/configure_transport.go View File

1
-// Copyright 2015 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
-// +build go1.6
6
-
7
-package http2
8
-
9
-import (
10
-	"crypto/tls"
11
-	"fmt"
12
-	"net/http"
13
-)
14
-
15
-func configureTransport(t1 *http.Transport) (*Transport, error) {
16
-	connPool := new(clientConnPool)
17
-	t2 := &Transport{
18
-		ConnPool: noDialClientConnPool{connPool},
19
-		t1:       t1,
20
-	}
21
-	connPool.t = t2
22
-	if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil {
23
-		return nil, err
24
-	}
25
-	if t1.TLSClientConfig == nil {
26
-		t1.TLSClientConfig = new(tls.Config)
27
-	}
28
-	if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
29
-		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
30
-	}
31
-	if !strSliceContains(t1.TLSClientConfig.NextProtos, "http/1.1") {
32
-		t1.TLSClientConfig.NextProtos = append(t1.TLSClientConfig.NextProtos, "http/1.1")
33
-	}
34
-	upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper {
35
-		addr := authorityAddr("https", authority)
36
-		if used, err := connPool.addConnIfNeeded(addr, t2, c); err != nil {
37
-			go c.Close()
38
-			return erringRoundTripper{err}
39
-		} else if !used {
40
-			// Turns out we don't need this c.
41
-			// For example, two goroutines made requests to the same host
42
-			// at the same time, both kicking off TCP dials. (since protocol
43
-			// was unknown)
44
-			go c.Close()
45
-		}
46
-		return t2
47
-	}
48
-	if m := t1.TLSNextProto; len(m) == 0 {
49
-		t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{
50
-			"h2": upgradeFn,
51
-		}
52
-	} else {
53
-		m["h2"] = upgradeFn
54
-	}
55
-	return t2, nil
56
-}
57
-
58
-// registerHTTPSProtocol calls Transport.RegisterProtocol but
59
-// converting panics into errors.
60
-func registerHTTPSProtocol(t *http.Transport, rt http.RoundTripper) (err error) {
61
-	defer func() {
62
-		if e := recover(); e != nil {
63
-			err = fmt.Errorf("%v", e)
64
-		}
65
-	}()
66
-	t.RegisterProtocol("https", rt)
67
-	return nil
68
-}
69
-
70
-// noDialH2RoundTripper is a RoundTripper which only tries to complete the request
71
-// if there's already has a cached connection to the host.
72
-type noDialH2RoundTripper struct{ t *Transport }
73
-
74
-func (rt noDialH2RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
75
-	res, err := rt.t.RoundTrip(req)
76
-	if err == ErrNoCachedConn {
77
-		return nil, http.ErrSkipAltProtocol
78
-	}
79
-	return res, err
80
-}

+ 0
- 146
vendor/golang.org/x/net/http2/databuffer.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"errors"
9
-	"fmt"
10
-	"sync"
11
-)
12
-
13
-// Buffer chunks are allocated from a pool to reduce pressure on GC.
14
-// The maximum wasted space per dataBuffer is 2x the largest size class,
15
-// which happens when the dataBuffer has multiple chunks and there is
16
-// one unread byte in both the first and last chunks. We use a few size
17
-// classes to minimize overheads for servers that typically receive very
18
-// small request bodies.
19
-//
20
-// TODO: Benchmark to determine if the pools are necessary. The GC may have
21
-// improved enough that we can instead allocate chunks like this:
22
-// make([]byte, max(16<<10, expectedBytesRemaining))
23
-var (
24
-	dataChunkSizeClasses = []int{
25
-		1 << 10,
26
-		2 << 10,
27
-		4 << 10,
28
-		8 << 10,
29
-		16 << 10,
30
-	}
31
-	dataChunkPools = [...]sync.Pool{
32
-		{New: func() interface{} { return make([]byte, 1<<10) }},
33
-		{New: func() interface{} { return make([]byte, 2<<10) }},
34
-		{New: func() interface{} { return make([]byte, 4<<10) }},
35
-		{New: func() interface{} { return make([]byte, 8<<10) }},
36
-		{New: func() interface{} { return make([]byte, 16<<10) }},
37
-	}
38
-)
39
-
40
-func getDataBufferChunk(size int64) []byte {
41
-	i := 0
42
-	for ; i < len(dataChunkSizeClasses)-1; i++ {
43
-		if size <= int64(dataChunkSizeClasses[i]) {
44
-			break
45
-		}
46
-	}
47
-	return dataChunkPools[i].Get().([]byte)
48
-}
49
-
50
-func putDataBufferChunk(p []byte) {
51
-	for i, n := range dataChunkSizeClasses {
52
-		if len(p) == n {
53
-			dataChunkPools[i].Put(p)
54
-			return
55
-		}
56
-	}
57
-	panic(fmt.Sprintf("unexpected buffer len=%v", len(p)))
58
-}
59
-
60
-// dataBuffer is an io.ReadWriter backed by a list of data chunks.
61
-// Each dataBuffer is used to read DATA frames on a single stream.
62
-// The buffer is divided into chunks so the server can limit the
63
-// total memory used by a single connection without limiting the
64
-// request body size on any single stream.
65
-type dataBuffer struct {
66
-	chunks   [][]byte
67
-	r        int   // next byte to read is chunks[0][r]
68
-	w        int   // next byte to write is chunks[len(chunks)-1][w]
69
-	size     int   // total buffered bytes
70
-	expected int64 // we expect at least this many bytes in future Write calls (ignored if <= 0)
71
-}
72
-
73
-var errReadEmpty = errors.New("read from empty dataBuffer")
74
-
75
-// Read copies bytes from the buffer into p.
76
-// It is an error to read when no data is available.
77
-func (b *dataBuffer) Read(p []byte) (int, error) {
78
-	if b.size == 0 {
79
-		return 0, errReadEmpty
80
-	}
81
-	var ntotal int
82
-	for len(p) > 0 && b.size > 0 {
83
-		readFrom := b.bytesFromFirstChunk()
84
-		n := copy(p, readFrom)
85
-		p = p[n:]
86
-		ntotal += n
87
-		b.r += n
88
-		b.size -= n
89
-		// If the first chunk has been consumed, advance to the next chunk.
90
-		if b.r == len(b.chunks[0]) {
91
-			putDataBufferChunk(b.chunks[0])
92
-			end := len(b.chunks) - 1
93
-			copy(b.chunks[:end], b.chunks[1:])
94
-			b.chunks[end] = nil
95
-			b.chunks = b.chunks[:end]
96
-			b.r = 0
97
-		}
98
-	}
99
-	return ntotal, nil
100
-}
101
-
102
-func (b *dataBuffer) bytesFromFirstChunk() []byte {
103
-	if len(b.chunks) == 1 {
104
-		return b.chunks[0][b.r:b.w]
105
-	}
106
-	return b.chunks[0][b.r:]
107
-}
108
-
109
-// Len returns the number of bytes of the unread portion of the buffer.
110
-func (b *dataBuffer) Len() int {
111
-	return b.size
112
-}
113
-
114
-// Write appends p to the buffer.
115
-func (b *dataBuffer) Write(p []byte) (int, error) {
116
-	ntotal := len(p)
117
-	for len(p) > 0 {
118
-		// If the last chunk is empty, allocate a new chunk. Try to allocate
119
-		// enough to fully copy p plus any additional bytes we expect to
120
-		// receive. However, this may allocate less than len(p).
121
-		want := int64(len(p))
122
-		if b.expected > want {
123
-			want = b.expected
124
-		}
125
-		chunk := b.lastChunkOrAlloc(want)
126
-		n := copy(chunk[b.w:], p)
127
-		p = p[n:]
128
-		b.w += n
129
-		b.size += n
130
-		b.expected -= int64(n)
131
-	}
132
-	return ntotal, nil
133
-}
134
-
135
-func (b *dataBuffer) lastChunkOrAlloc(want int64) []byte {
136
-	if len(b.chunks) != 0 {
137
-		last := b.chunks[len(b.chunks)-1]
138
-		if b.w < len(last) {
139
-			return last
140
-		}
141
-	}
142
-	chunk := getDataBufferChunk(want)
143
-	b.chunks = append(b.chunks, chunk)
144
-	b.w = 0
145
-	return chunk
146
-}

+ 0
- 157
vendor/golang.org/x/net/http2/databuffer_test.go View File

1
-// Copyright 2017 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
-// +build go1.7
6
-
7
-package http2
8
-
9
-import (
10
-	"bytes"
11
-	"fmt"
12
-	"reflect"
13
-	"testing"
14
-)
15
-
16
-func fmtDataChunk(chunk []byte) string {
17
-	out := ""
18
-	var last byte
19
-	var count int
20
-	for _, c := range chunk {
21
-		if c != last {
22
-			if count > 0 {
23
-				out += fmt.Sprintf(" x %d ", count)
24
-				count = 0
25
-			}
26
-			out += string([]byte{c})
27
-			last = c
28
-		}
29
-		count++
30
-	}
31
-	if count > 0 {
32
-		out += fmt.Sprintf(" x %d", count)
33
-	}
34
-	return out
35
-}
36
-
37
-func fmtDataChunks(chunks [][]byte) string {
38
-	var out string
39
-	for _, chunk := range chunks {
40
-		out += fmt.Sprintf("{%q}", fmtDataChunk(chunk))
41
-	}
42
-	return out
43
-}
44
-
45
-func testDataBuffer(t *testing.T, wantBytes []byte, setup func(t *testing.T) *dataBuffer) {
46
-	// Run setup, then read the remaining bytes from the dataBuffer and check
47
-	// that they match wantBytes. We use different read sizes to check corner
48
-	// cases in Read.
49
-	for _, readSize := range []int{1, 2, 1 * 1024, 32 * 1024} {
50
-		t.Run(fmt.Sprintf("ReadSize=%d", readSize), func(t *testing.T) {
51
-			b := setup(t)
52
-			buf := make([]byte, readSize)
53
-			var gotRead bytes.Buffer
54
-			for {
55
-				n, err := b.Read(buf)
56
-				gotRead.Write(buf[:n])
57
-				if err == errReadEmpty {
58
-					break
59
-				}
60
-				if err != nil {
61
-					t.Fatalf("error after %v bytes: %v", gotRead.Len(), err)
62
-				}
63
-			}
64
-			if got, want := gotRead.Bytes(), wantBytes; !bytes.Equal(got, want) {
65
-				t.Errorf("FinalRead=%q, want %q", fmtDataChunk(got), fmtDataChunk(want))
66
-			}
67
-		})
68
-	}
69
-}
70
-
71
-func TestDataBufferAllocation(t *testing.T) {
72
-	writes := [][]byte{
73
-		bytes.Repeat([]byte("a"), 1*1024-1),
74
-		[]byte("a"),
75
-		bytes.Repeat([]byte("b"), 4*1024-1),
76
-		[]byte("b"),
77
-		bytes.Repeat([]byte("c"), 8*1024-1),
78
-		[]byte("c"),
79
-		bytes.Repeat([]byte("d"), 16*1024-1),
80
-		[]byte("d"),
81
-		bytes.Repeat([]byte("e"), 32*1024),
82
-	}
83
-	var wantRead bytes.Buffer
84
-	for _, p := range writes {
85
-		wantRead.Write(p)
86
-	}
87
-
88
-	testDataBuffer(t, wantRead.Bytes(), func(t *testing.T) *dataBuffer {
89
-		b := &dataBuffer{}
90
-		for _, p := range writes {
91
-			if n, err := b.Write(p); n != len(p) || err != nil {
92
-				t.Fatalf("Write(%q x %d)=%v,%v want %v,nil", p[:1], len(p), n, err, len(p))
93
-			}
94
-		}
95
-		want := [][]byte{
96
-			bytes.Repeat([]byte("a"), 1*1024),
97
-			bytes.Repeat([]byte("b"), 4*1024),
98
-			bytes.Repeat([]byte("c"), 8*1024),
99
-			bytes.Repeat([]byte("d"), 16*1024),
100
-			bytes.Repeat([]byte("e"), 16*1024),
101
-			bytes.Repeat([]byte("e"), 16*1024),
102
-		}
103
-		if !reflect.DeepEqual(b.chunks, want) {
104
-			t.Errorf("dataBuffer.chunks\ngot:  %s\nwant: %s", fmtDataChunks(b.chunks), fmtDataChunks(want))
105
-		}
106
-		return b
107
-	})
108
-}
109
-
110
-func TestDataBufferAllocationWithExpected(t *testing.T) {
111
-	writes := [][]byte{
112
-		bytes.Repeat([]byte("a"), 1*1024), // allocates 16KB
113
-		bytes.Repeat([]byte("b"), 14*1024),
114
-		bytes.Repeat([]byte("c"), 15*1024), // allocates 16KB more
115
-		bytes.Repeat([]byte("d"), 2*1024),
116
-		bytes.Repeat([]byte("e"), 1*1024), // overflows 32KB expectation, allocates just 1KB
117
-	}
118
-	var wantRead bytes.Buffer
119
-	for _, p := range writes {
120
-		wantRead.Write(p)
121
-	}
122
-
123
-	testDataBuffer(t, wantRead.Bytes(), func(t *testing.T) *dataBuffer {
124
-		b := &dataBuffer{expected: 32 * 1024}
125
-		for _, p := range writes {
126
-			if n, err := b.Write(p); n != len(p) || err != nil {
127
-				t.Fatalf("Write(%q x %d)=%v,%v want %v,nil", p[:1], len(p), n, err, len(p))
128
-			}
129
-		}
130
-		want := [][]byte{
131
-			append(bytes.Repeat([]byte("a"), 1*1024), append(bytes.Repeat([]byte("b"), 14*1024), bytes.Repeat([]byte("c"), 1*1024)...)...),
132
-			append(bytes.Repeat([]byte("c"), 14*1024), bytes.Repeat([]byte("d"), 2*1024)...),
133
-			bytes.Repeat([]byte("e"), 1*1024),
134
-		}
135
-		if !reflect.DeepEqual(b.chunks, want) {
136
-			t.Errorf("dataBuffer.chunks\ngot:  %s\nwant: %s", fmtDataChunks(b.chunks), fmtDataChunks(want))
137
-		}
138
-		return b
139
-	})
140
-}
141
-
142
-func TestDataBufferWriteAfterPartialRead(t *testing.T) {
143
-	testDataBuffer(t, []byte("cdxyz"), func(t *testing.T) *dataBuffer {
144
-		b := &dataBuffer{}
145
-		if n, err := b.Write([]byte("abcd")); n != 4 || err != nil {
146
-			t.Fatalf("Write(\"abcd\")=%v,%v want 4,nil", n, err)
147
-		}
148
-		p := make([]byte, 2)
149
-		if n, err := b.Read(p); n != 2 || err != nil || !bytes.Equal(p, []byte("ab")) {
150
-			t.Fatalf("Read()=%q,%v,%v want \"ab\",2,nil", p, n, err)
151
-		}
152
-		if n, err := b.Write([]byte("xyz")); n != 3 || err != nil {
153
-			t.Fatalf("Write(\"xyz\")=%v,%v want 3,nil", n, err)
154
-		}
155
-		return b
156
-	})
157
-}

+ 0
- 133
vendor/golang.org/x/net/http2/errors.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"errors"
9
-	"fmt"
10
-)
11
-
12
-// An ErrCode is an unsigned 32-bit error code as defined in the HTTP/2 spec.
13
-type ErrCode uint32
14
-
15
-const (
16
-	ErrCodeNo                 ErrCode = 0x0
17
-	ErrCodeProtocol           ErrCode = 0x1
18
-	ErrCodeInternal           ErrCode = 0x2
19
-	ErrCodeFlowControl        ErrCode = 0x3
20
-	ErrCodeSettingsTimeout    ErrCode = 0x4
21
-	ErrCodeStreamClosed       ErrCode = 0x5
22
-	ErrCodeFrameSize          ErrCode = 0x6
23
-	ErrCodeRefusedStream      ErrCode = 0x7
24
-	ErrCodeCancel             ErrCode = 0x8
25
-	ErrCodeCompression        ErrCode = 0x9
26
-	ErrCodeConnect            ErrCode = 0xa
27
-	ErrCodeEnhanceYourCalm    ErrCode = 0xb
28
-	ErrCodeInadequateSecurity ErrCode = 0xc
29
-	ErrCodeHTTP11Required     ErrCode = 0xd
30
-)
31
-
32
-var errCodeName = map[ErrCode]string{
33
-	ErrCodeNo:                 "NO_ERROR",
34
-	ErrCodeProtocol:           "PROTOCOL_ERROR",
35
-	ErrCodeInternal:           "INTERNAL_ERROR",
36
-	ErrCodeFlowControl:        "FLOW_CONTROL_ERROR",
37
-	ErrCodeSettingsTimeout:    "SETTINGS_TIMEOUT",
38
-	ErrCodeStreamClosed:       "STREAM_CLOSED",
39
-	ErrCodeFrameSize:          "FRAME_SIZE_ERROR",
40
-	ErrCodeRefusedStream:      "REFUSED_STREAM",
41
-	ErrCodeCancel:             "CANCEL",
42
-	ErrCodeCompression:        "COMPRESSION_ERROR",
43
-	ErrCodeConnect:            "CONNECT_ERROR",
44
-	ErrCodeEnhanceYourCalm:    "ENHANCE_YOUR_CALM",
45
-	ErrCodeInadequateSecurity: "INADEQUATE_SECURITY",
46
-	ErrCodeHTTP11Required:     "HTTP_1_1_REQUIRED",
47
-}
48
-
49
-func (e ErrCode) String() string {
50
-	if s, ok := errCodeName[e]; ok {
51
-		return s
52
-	}
53
-	return fmt.Sprintf("unknown error code 0x%x", uint32(e))
54
-}
55
-
56
-// ConnectionError is an error that results in the termination of the
57
-// entire connection.
58
-type ConnectionError ErrCode
59
-
60
-func (e ConnectionError) Error() string { return fmt.Sprintf("connection error: %s", ErrCode(e)) }
61
-
62
-// StreamError is an error that only affects one stream within an
63
-// HTTP/2 connection.
64
-type StreamError struct {
65
-	StreamID uint32
66
-	Code     ErrCode
67
-	Cause    error // optional additional detail
68
-}
69
-
70
-func streamError(id uint32, code ErrCode) StreamError {
71
-	return StreamError{StreamID: id, Code: code}
72
-}
73
-
74
-func (e StreamError) Error() string {
75
-	if e.Cause != nil {
76
-		return fmt.Sprintf("stream error: stream ID %d; %v; %v", e.StreamID, e.Code, e.Cause)
77
-	}
78
-	return fmt.Sprintf("stream error: stream ID %d; %v", e.StreamID, e.Code)
79
-}
80
-
81
-// 6.9.1 The Flow Control Window
82
-// "If a sender receives a WINDOW_UPDATE that causes a flow control
83
-// window to exceed this maximum it MUST terminate either the stream
84
-// or the connection, as appropriate. For streams, [...]; for the
85
-// connection, a GOAWAY frame with a FLOW_CONTROL_ERROR code."
86
-type goAwayFlowError struct{}
87
-
88
-func (goAwayFlowError) Error() string { return "connection exceeded flow control window size" }
89
-
90
-// connError represents an HTTP/2 ConnectionError error code, along
91
-// with a string (for debugging) explaining why.
92
-//
93
-// Errors of this type are only returned by the frame parser functions
94
-// and converted into ConnectionError(Code), after stashing away
95
-// the Reason into the Framer's errDetail field, accessible via
96
-// the (*Framer).ErrorDetail method.
97
-type connError struct {
98
-	Code   ErrCode // the ConnectionError error code
99
-	Reason string  // additional reason
100
-}
101
-
102
-func (e connError) Error() string {
103
-	return fmt.Sprintf("http2: connection error: %v: %v", e.Code, e.Reason)
104
-}
105
-
106
-type pseudoHeaderError string
107
-
108
-func (e pseudoHeaderError) Error() string {
109
-	return fmt.Sprintf("invalid pseudo-header %q", string(e))
110
-}
111
-
112
-type duplicatePseudoHeaderError string
113
-
114
-func (e duplicatePseudoHeaderError) Error() string {
115
-	return fmt.Sprintf("duplicate pseudo-header %q", string(e))
116
-}
117
-
118
-type headerFieldNameError string
119
-
120
-func (e headerFieldNameError) Error() string {
121
-	return fmt.Sprintf("invalid header field name %q", string(e))
122
-}
123
-
124
-type headerFieldValueError string
125
-
126
-func (e headerFieldValueError) Error() string {
127
-	return fmt.Sprintf("invalid header field value %q", string(e))
128
-}
129
-
130
-var (
131
-	errMixPseudoHeaderTypes = errors.New("mix of request and response pseudo headers")
132
-	errPseudoAfterRegular   = errors.New("pseudo header field after regular")
133
-)

+ 0
- 24
vendor/golang.org/x/net/http2/errors_test.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import "testing"
8
-
9
-func TestErrCodeString(t *testing.T) {
10
-	tests := []struct {
11
-		err  ErrCode
12
-		want string
13
-	}{
14
-		{ErrCodeProtocol, "PROTOCOL_ERROR"},
15
-		{0xd, "HTTP_1_1_REQUIRED"},
16
-		{0xf, "unknown error code 0xf"},
17
-	}
18
-	for i, tt := range tests {
19
-		got := tt.err.String()
20
-		if got != tt.want {
21
-			t.Errorf("%d. Error = %q; want %q", i, got, tt.want)
22
-		}
23
-	}
24
-}

+ 0
- 50
vendor/golang.org/x/net/http2/flow.go View File

1
-// Copyright 2014 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
-// Flow control
6
-
7
-package http2
8
-
9
-// flow is the flow control window's size.
10
-type flow struct {
11
-	// n is the number of DATA bytes we're allowed to send.
12
-	// A flow is kept both on a conn and a per-stream.
13
-	n int32
14
-
15
-	// conn points to the shared connection-level flow that is
16
-	// shared by all streams on that conn. It is nil for the flow
17
-	// that's on the conn directly.
18
-	conn *flow
19
-}
20
-
21
-func (f *flow) setConnFlow(cf *flow) { f.conn = cf }
22
-
23
-func (f *flow) available() int32 {
24
-	n := f.n
25
-	if f.conn != nil && f.conn.n < n {
26
-		n = f.conn.n
27
-	}
28
-	return n
29
-}
30
-
31
-func (f *flow) take(n int32) {
32
-	if n > f.available() {
33
-		panic("internal error: took too much")
34
-	}
35
-	f.n -= n
36
-	if f.conn != nil {
37
-		f.conn.n -= n
38
-	}
39
-}
40
-
41
-// add adds n bytes (positive or negative) to the flow control window.
42
-// It returns false if the sum would exceed 2^31-1.
43
-func (f *flow) add(n int32) bool {
44
-	remain := (1<<31 - 1) - f.n
45
-	if n > remain {
46
-		return false
47
-	}
48
-	f.n += n
49
-	return true
50
-}

+ 0
- 53
vendor/golang.org/x/net/http2/flow_test.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import "testing"
8
-
9
-func TestFlow(t *testing.T) {
10
-	var st flow
11
-	var conn flow
12
-	st.add(3)
13
-	conn.add(2)
14
-
15
-	if got, want := st.available(), int32(3); got != want {
16
-		t.Errorf("available = %d; want %d", got, want)
17
-	}
18
-	st.setConnFlow(&conn)
19
-	if got, want := st.available(), int32(2); got != want {
20
-		t.Errorf("after parent setup, available = %d; want %d", got, want)
21
-	}
22
-
23
-	st.take(2)
24
-	if got, want := conn.available(), int32(0); got != want {
25
-		t.Errorf("after taking 2, conn = %d; want %d", got, want)
26
-	}
27
-	if got, want := st.available(), int32(0); got != want {
28
-		t.Errorf("after taking 2, stream = %d; want %d", got, want)
29
-	}
30
-}
31
-
32
-func TestFlowAdd(t *testing.T) {
33
-	var f flow
34
-	if !f.add(1) {
35
-		t.Fatal("failed to add 1")
36
-	}
37
-	if !f.add(-1) {
38
-		t.Fatal("failed to add -1")
39
-	}
40
-	if got, want := f.available(), int32(0); got != want {
41
-		t.Fatalf("size = %d; want %d", got, want)
42
-	}
43
-	if !f.add(1<<31 - 1) {
44
-		t.Fatal("failed to add 2^31-1")
45
-	}
46
-	if got, want := f.available(), int32(1<<31-1); got != want {
47
-		t.Fatalf("size = %d; want %d", got, want)
48
-	}
49
-	if f.add(1) {
50
-		t.Fatal("adding 1 to max shouldn't be allowed")
51
-	}
52
-
53
-}

+ 0
- 1579
vendor/golang.org/x/net/http2/frame.go
File diff suppressed because it is too large
View File


+ 0
- 1191
vendor/golang.org/x/net/http2/frame_test.go
File diff suppressed because it is too large
View File


+ 0
- 16
vendor/golang.org/x/net/http2/go16.go View File

1
-// Copyright 2016 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
-// +build go1.6
6
-
7
-package http2
8
-
9
-import (
10
-	"net/http"
11
-	"time"
12
-)
13
-
14
-func transportExpectContinueTimeout(t1 *http.Transport) time.Duration {
15
-	return t1.ExpectContinueTimeout
16
-}

+ 0
- 106
vendor/golang.org/x/net/http2/go17.go View File

1
-// Copyright 2016 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
-// +build go1.7
6
-
7
-package http2
8
-
9
-import (
10
-	"context"
11
-	"net"
12
-	"net/http"
13
-	"net/http/httptrace"
14
-	"time"
15
-)
16
-
17
-type contextContext interface {
18
-	context.Context
19
-}
20
-
21
-func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx contextContext, cancel func()) {
22
-	ctx, cancel = context.WithCancel(context.Background())
23
-	ctx = context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
24
-	if hs := opts.baseConfig(); hs != nil {
25
-		ctx = context.WithValue(ctx, http.ServerContextKey, hs)
26
-	}
27
-	return
28
-}
29
-
30
-func contextWithCancel(ctx contextContext) (_ contextContext, cancel func()) {
31
-	return context.WithCancel(ctx)
32
-}
33
-
34
-func requestWithContext(req *http.Request, ctx contextContext) *http.Request {
35
-	return req.WithContext(ctx)
36
-}
37
-
38
-type clientTrace httptrace.ClientTrace
39
-
40
-func reqContext(r *http.Request) context.Context { return r.Context() }
41
-
42
-func (t *Transport) idleConnTimeout() time.Duration {
43
-	if t.t1 != nil {
44
-		return t.t1.IdleConnTimeout
45
-	}
46
-	return 0
47
-}
48
-
49
-func setResponseUncompressed(res *http.Response) { res.Uncompressed = true }
50
-
51
-func traceGotConn(req *http.Request, cc *ClientConn) {
52
-	trace := httptrace.ContextClientTrace(req.Context())
53
-	if trace == nil || trace.GotConn == nil {
54
-		return
55
-	}
56
-	ci := httptrace.GotConnInfo{Conn: cc.tconn}
57
-	cc.mu.Lock()
58
-	ci.Reused = cc.nextStreamID > 1
59
-	ci.WasIdle = len(cc.streams) == 0 && ci.Reused
60
-	if ci.WasIdle && !cc.lastActive.IsZero() {
61
-		ci.IdleTime = time.Now().Sub(cc.lastActive)
62
-	}
63
-	cc.mu.Unlock()
64
-
65
-	trace.GotConn(ci)
66
-}
67
-
68
-func traceWroteHeaders(trace *clientTrace) {
69
-	if trace != nil && trace.WroteHeaders != nil {
70
-		trace.WroteHeaders()
71
-	}
72
-}
73
-
74
-func traceGot100Continue(trace *clientTrace) {
75
-	if trace != nil && trace.Got100Continue != nil {
76
-		trace.Got100Continue()
77
-	}
78
-}
79
-
80
-func traceWait100Continue(trace *clientTrace) {
81
-	if trace != nil && trace.Wait100Continue != nil {
82
-		trace.Wait100Continue()
83
-	}
84
-}
85
-
86
-func traceWroteRequest(trace *clientTrace, err error) {
87
-	if trace != nil && trace.WroteRequest != nil {
88
-		trace.WroteRequest(httptrace.WroteRequestInfo{Err: err})
89
-	}
90
-}
91
-
92
-func traceFirstResponseByte(trace *clientTrace) {
93
-	if trace != nil && trace.GotFirstResponseByte != nil {
94
-		trace.GotFirstResponseByte()
95
-	}
96
-}
97
-
98
-func requestTrace(req *http.Request) *clientTrace {
99
-	trace := httptrace.ContextClientTrace(req.Context())
100
-	return (*clientTrace)(trace)
101
-}
102
-
103
-// Ping sends a PING frame to the server and waits for the ack.
104
-func (cc *ClientConn) Ping(ctx context.Context) error {
105
-	return cc.ping(ctx)
106
-}

+ 0
- 36
vendor/golang.org/x/net/http2/go17_not18.go View File

1
-// Copyright 2016 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
-// +build go1.7,!go1.8
6
-
7
-package http2
8
-
9
-import "crypto/tls"
10
-
11
-// temporary copy of Go 1.7's private tls.Config.clone:
12
-func cloneTLSConfig(c *tls.Config) *tls.Config {
13
-	return &tls.Config{
14
-		Rand:                        c.Rand,
15
-		Time:                        c.Time,
16
-		Certificates:                c.Certificates,
17
-		NameToCertificate:           c.NameToCertificate,
18
-		GetCertificate:              c.GetCertificate,
19
-		RootCAs:                     c.RootCAs,
20
-		NextProtos:                  c.NextProtos,
21
-		ServerName:                  c.ServerName,
22
-		ClientAuth:                  c.ClientAuth,
23
-		ClientCAs:                   c.ClientCAs,
24
-		InsecureSkipVerify:          c.InsecureSkipVerify,
25
-		CipherSuites:                c.CipherSuites,
26
-		PreferServerCipherSuites:    c.PreferServerCipherSuites,
27
-		SessionTicketsDisabled:      c.SessionTicketsDisabled,
28
-		SessionTicketKey:            c.SessionTicketKey,
29
-		ClientSessionCache:          c.ClientSessionCache,
30
-		MinVersion:                  c.MinVersion,
31
-		MaxVersion:                  c.MaxVersion,
32
-		CurvePreferences:            c.CurvePreferences,
33
-		DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
34
-		Renegotiation:               c.Renegotiation,
35
-	}
36
-}

+ 0
- 56
vendor/golang.org/x/net/http2/go18.go View File

1
-// Copyright 2015 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
-// +build go1.8
6
-
7
-package http2
8
-
9
-import (
10
-	"crypto/tls"
11
-	"io"
12
-	"net/http"
13
-)
14
-
15
-func cloneTLSConfig(c *tls.Config) *tls.Config {
16
-	c2 := c.Clone()
17
-	c2.GetClientCertificate = c.GetClientCertificate // golang.org/issue/19264
18
-	return c2
19
-}
20
-
21
-var _ http.Pusher = (*responseWriter)(nil)
22
-
23
-// Push implements http.Pusher.
24
-func (w *responseWriter) Push(target string, opts *http.PushOptions) error {
25
-	internalOpts := pushOptions{}
26
-	if opts != nil {
27
-		internalOpts.Method = opts.Method
28
-		internalOpts.Header = opts.Header
29
-	}
30
-	return w.push(target, internalOpts)
31
-}
32
-
33
-func configureServer18(h1 *http.Server, h2 *Server) error {
34
-	if h2.IdleTimeout == 0 {
35
-		if h1.IdleTimeout != 0 {
36
-			h2.IdleTimeout = h1.IdleTimeout
37
-		} else {
38
-			h2.IdleTimeout = h1.ReadTimeout
39
-		}
40
-	}
41
-	return nil
42
-}
43
-
44
-func shouldLogPanic(panicValue interface{}) bool {
45
-	return panicValue != nil && panicValue != http.ErrAbortHandler
46
-}
47
-
48
-func reqGetBody(req *http.Request) func() (io.ReadCloser, error) {
49
-	return req.GetBody
50
-}
51
-
52
-func reqBodyIsNoBody(body io.ReadCloser) bool {
53
-	return body == http.NoBody
54
-}
55
-
56
-func go18httpNoBody() io.ReadCloser { return http.NoBody } // for tests only

+ 0
- 79
vendor/golang.org/x/net/http2/go18_test.go View File

1
-// Copyright 2016 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
-// +build go1.8
6
-
7
-package http2
8
-
9
-import (
10
-	"crypto/tls"
11
-	"net/http"
12
-	"testing"
13
-	"time"
14
-)
15
-
16
-// Tests that http2.Server.IdleTimeout is initialized from
17
-// http.Server.{Idle,Read}Timeout. http.Server.IdleTimeout was
18
-// added in Go 1.8.
19
-func TestConfigureServerIdleTimeout_Go18(t *testing.T) {
20
-	const timeout = 5 * time.Second
21
-	const notThisOne = 1 * time.Second
22
-
23
-	// With a zero http2.Server, verify that it copies IdleTimeout:
24
-	{
25
-		s1 := &http.Server{
26
-			IdleTimeout: timeout,
27
-			ReadTimeout: notThisOne,
28
-		}
29
-		s2 := &Server{}
30
-		if err := ConfigureServer(s1, s2); err != nil {
31
-			t.Fatal(err)
32
-		}
33
-		if s2.IdleTimeout != timeout {
34
-			t.Errorf("s2.IdleTimeout = %v; want %v", s2.IdleTimeout, timeout)
35
-		}
36
-	}
37
-
38
-	// And that it falls back to ReadTimeout:
39
-	{
40
-		s1 := &http.Server{
41
-			ReadTimeout: timeout,
42
-		}
43
-		s2 := &Server{}
44
-		if err := ConfigureServer(s1, s2); err != nil {
45
-			t.Fatal(err)
46
-		}
47
-		if s2.IdleTimeout != timeout {
48
-			t.Errorf("s2.IdleTimeout = %v; want %v", s2.IdleTimeout, timeout)
49
-		}
50
-	}
51
-
52
-	// Verify that s1's IdleTimeout doesn't overwrite an existing setting:
53
-	{
54
-		s1 := &http.Server{
55
-			IdleTimeout: notThisOne,
56
-		}
57
-		s2 := &Server{
58
-			IdleTimeout: timeout,
59
-		}
60
-		if err := ConfigureServer(s1, s2); err != nil {
61
-			t.Fatal(err)
62
-		}
63
-		if s2.IdleTimeout != timeout {
64
-			t.Errorf("s2.IdleTimeout = %v; want %v", s2.IdleTimeout, timeout)
65
-		}
66
-	}
67
-}
68
-
69
-func TestCertClone(t *testing.T) {
70
-	c := &tls.Config{
71
-		GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
72
-			panic("shouldn't be called")
73
-		},
74
-	}
75
-	c2 := cloneTLSConfig(c)
76
-	if c2.GetClientCertificate == nil {
77
-		t.Error("GetClientCertificate is nil")
78
-	}
79
-}

+ 0
- 16
vendor/golang.org/x/net/http2/go19.go View File

1
-// Copyright 2015 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
-// +build go1.9
6
-
7
-package http2
8
-
9
-import (
10
-	"net/http"
11
-)
12
-
13
-func configureServer19(s *http.Server, conf *Server) error {
14
-	s.RegisterOnShutdown(conf.state.startGracefulShutdown)
15
-	return nil
16
-}

+ 0
- 60
vendor/golang.org/x/net/http2/go19_test.go View File

1
-// Copyright 2017 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
-// +build go1.9
6
-
7
-package http2
8
-
9
-import (
10
-	"context"
11
-	"net/http"
12
-	"reflect"
13
-	"testing"
14
-	"time"
15
-)
16
-
17
-func TestServerGracefulShutdown(t *testing.T) {
18
-	var st *serverTester
19
-	handlerDone := make(chan struct{})
20
-	st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
21
-		defer close(handlerDone)
22
-		go st.ts.Config.Shutdown(context.Background())
23
-
24
-		ga := st.wantGoAway()
25
-		if ga.ErrCode != ErrCodeNo {
26
-			t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode)
27
-		}
28
-		if ga.LastStreamID != 1 {
29
-			t.Errorf("GOAWAY LastStreamID = %v; want 1", ga.LastStreamID)
30
-		}
31
-
32
-		w.Header().Set("x-foo", "bar")
33
-	})
34
-	defer st.Close()
35
-
36
-	st.greet()
37
-	st.bodylessReq1()
38
-
39
-	select {
40
-	case <-handlerDone:
41
-	case <-time.After(5 * time.Second):
42
-		t.Fatalf("server did not shutdown?")
43
-	}
44
-	hf := st.wantHeaders()
45
-	goth := st.decodeHeader(hf.HeaderBlockFragment())
46
-	wanth := [][2]string{
47
-		{":status", "200"},
48
-		{"x-foo", "bar"},
49
-		{"content-type", "text/plain; charset=utf-8"},
50
-		{"content-length", "0"},
51
-	}
52
-	if !reflect.DeepEqual(goth, wanth) {
53
-		t.Errorf("Got headers %v; want %v", goth, wanth)
54
-	}
55
-
56
-	n, err := st.cc.Read([]byte{0})
57
-	if n != 0 || err == nil {
58
-		t.Errorf("Read = %v, %v; want 0, non-nil", n, err)
59
-	}
60
-}

+ 0
- 170
vendor/golang.org/x/net/http2/gotrack.go View File

1
-// Copyright 2014 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
-// Defensive debug-only utility to track that functions run on the
6
-// goroutine that they're supposed to.
7
-
8
-package http2
9
-
10
-import (
11
-	"bytes"
12
-	"errors"
13
-	"fmt"
14
-	"os"
15
-	"runtime"
16
-	"strconv"
17
-	"sync"
18
-)
19
-
20
-var DebugGoroutines = os.Getenv("DEBUG_HTTP2_GOROUTINES") == "1"
21
-
22
-type goroutineLock uint64
23
-
24
-func newGoroutineLock() goroutineLock {
25
-	if !DebugGoroutines {
26
-		return 0
27
-	}
28
-	return goroutineLock(curGoroutineID())
29
-}
30
-
31
-func (g goroutineLock) check() {
32
-	if !DebugGoroutines {
33
-		return
34
-	}
35
-	if curGoroutineID() != uint64(g) {
36
-		panic("running on the wrong goroutine")
37
-	}
38
-}
39
-
40
-func (g goroutineLock) checkNotOn() {
41
-	if !DebugGoroutines {
42
-		return
43
-	}
44
-	if curGoroutineID() == uint64(g) {
45
-		panic("running on the wrong goroutine")
46
-	}
47
-}
48
-
49
-var goroutineSpace = []byte("goroutine ")
50
-
51
-func curGoroutineID() uint64 {
52
-	bp := littleBuf.Get().(*[]byte)
53
-	defer littleBuf.Put(bp)
54
-	b := *bp
55
-	b = b[:runtime.Stack(b, false)]
56
-	// Parse the 4707 out of "goroutine 4707 ["
57
-	b = bytes.TrimPrefix(b, goroutineSpace)
58
-	i := bytes.IndexByte(b, ' ')
59
-	if i < 0 {
60
-		panic(fmt.Sprintf("No space found in %q", b))
61
-	}
62
-	b = b[:i]
63
-	n, err := parseUintBytes(b, 10, 64)
64
-	if err != nil {
65
-		panic(fmt.Sprintf("Failed to parse goroutine ID out of %q: %v", b, err))
66
-	}
67
-	return n
68
-}
69
-
70
-var littleBuf = sync.Pool{
71
-	New: func() interface{} {
72
-		buf := make([]byte, 64)
73
-		return &buf
74
-	},
75
-}
76
-
77
-// parseUintBytes is like strconv.ParseUint, but using a []byte.
78
-func parseUintBytes(s []byte, base int, bitSize int) (n uint64, err error) {
79
-	var cutoff, maxVal uint64
80
-
81
-	if bitSize == 0 {
82
-		bitSize = int(strconv.IntSize)
83
-	}
84
-
85
-	s0 := s
86
-	switch {
87
-	case len(s) < 1:
88
-		err = strconv.ErrSyntax
89
-		goto Error
90
-
91
-	case 2 <= base && base <= 36:
92
-		// valid base; nothing to do
93
-
94
-	case base == 0:
95
-		// Look for octal, hex prefix.
96
-		switch {
97
-		case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
98
-			base = 16
99
-			s = s[2:]
100
-			if len(s) < 1 {
101
-				err = strconv.ErrSyntax
102
-				goto Error
103
-			}
104
-		case s[0] == '0':
105
-			base = 8
106
-		default:
107
-			base = 10
108
-		}
109
-
110
-	default:
111
-		err = errors.New("invalid base " + strconv.Itoa(base))
112
-		goto Error
113
-	}
114
-
115
-	n = 0
116
-	cutoff = cutoff64(base)
117
-	maxVal = 1<<uint(bitSize) - 1
118
-
119
-	for i := 0; i < len(s); i++ {
120
-		var v byte
121
-		d := s[i]
122
-		switch {
123
-		case '0' <= d && d <= '9':
124
-			v = d - '0'
125
-		case 'a' <= d && d <= 'z':
126
-			v = d - 'a' + 10
127
-		case 'A' <= d && d <= 'Z':
128
-			v = d - 'A' + 10
129
-		default:
130
-			n = 0
131
-			err = strconv.ErrSyntax
132
-			goto Error
133
-		}
134
-		if int(v) >= base {
135
-			n = 0
136
-			err = strconv.ErrSyntax
137
-			goto Error
138
-		}
139
-
140
-		if n >= cutoff {
141
-			// n*base overflows
142
-			n = 1<<64 - 1
143
-			err = strconv.ErrRange
144
-			goto Error
145
-		}
146
-		n *= uint64(base)
147
-
148
-		n1 := n + uint64(v)
149
-		if n1 < n || n1 > maxVal {
150
-			// n+v overflows
151
-			n = 1<<64 - 1
152
-			err = strconv.ErrRange
153
-			goto Error
154
-		}
155
-		n = n1
156
-	}
157
-
158
-	return n, nil
159
-
160
-Error:
161
-	return n, &strconv.NumError{Func: "ParseUint", Num: string(s0), Err: err}
162
-}
163
-
164
-// Return the first number n such that n*base >= 1<<64.
165
-func cutoff64(base int) uint64 {
166
-	if base < 2 {
167
-		return 0
168
-	}
169
-	return (1<<64-1)/uint64(base) + 1
170
-}

+ 0
- 33
vendor/golang.org/x/net/http2/gotrack_test.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"fmt"
9
-	"strings"
10
-	"testing"
11
-)
12
-
13
-func TestGoroutineLock(t *testing.T) {
14
-	oldDebug := DebugGoroutines
15
-	DebugGoroutines = true
16
-	defer func() { DebugGoroutines = oldDebug }()
17
-
18
-	g := newGoroutineLock()
19
-	g.check()
20
-
21
-	sawPanic := make(chan interface{})
22
-	go func() {
23
-		defer func() { sawPanic <- recover() }()
24
-		g.check() // should panic
25
-	}()
26
-	e := <-sawPanic
27
-	if e == nil {
28
-		t.Fatal("did not see panic from check in other goroutine")
29
-	}
30
-	if !strings.Contains(fmt.Sprint(e), "wrong goroutine") {
31
-		t.Errorf("expected on see panic about running on the wrong goroutine; got %v", e)
32
-	}
33
-}

+ 0
- 5
vendor/golang.org/x/net/http2/h2demo/.gitignore View File

1
-h2demo
2
-h2demo.linux
3
-client-id.dat
4
-client-secret.dat
5
-token.dat

+ 0
- 8
vendor/golang.org/x/net/http2/h2demo/Makefile View File

1
-h2demo.linux: h2demo.go
2
-	GOOS=linux go build --tags=h2demo -o h2demo.linux .
3
-
4
-FORCE:
5
-
6
-upload: FORCE
7
-	go install golang.org/x/build/cmd/upload
8
-	upload --verbose --osarch=linux-amd64 --tags=h2demo --file=go:golang.org/x/net/http2/h2demo --public http2-demo-server-tls/h2demo

+ 0
- 16
vendor/golang.org/x/net/http2/h2demo/README View File

1
-
2
-Client:
3
- -- Firefox nightly with about:config network.http.spdy.enabled.http2draft set true
4
- -- Chrome: go to chrome://flags/#enable-spdy4, save and restart (button at bottom)
5
-
6
-Make CA:
7
-$ openssl genrsa -out rootCA.key 2048
8
-$ openssl req -x509 -new -nodes -key rootCA.key -days 1024 -out rootCA.pem
9
-... install that to Firefox
10
-
11
-Make cert:
12
-$ openssl genrsa -out server.key 2048
13
-$ openssl req -new -key server.key -out server.csr
14
-$ openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 500
15
-
16
-

+ 0
- 538
vendor/golang.org/x/net/http2/h2demo/h2demo.go View File

1
-// Copyright 2014 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
-// +build h2demo
6
-
7
-package main
8
-
9
-import (
10
-	"bytes"
11
-	"crypto/tls"
12
-	"flag"
13
-	"fmt"
14
-	"hash/crc32"
15
-	"image"
16
-	"image/jpeg"
17
-	"io"
18
-	"io/ioutil"
19
-	"log"
20
-	"net"
21
-	"net/http"
22
-	"os"
23
-	"path"
24
-	"regexp"
25
-	"runtime"
26
-	"strconv"
27
-	"strings"
28
-	"sync"
29
-	"time"
30
-
31
-	"go4.org/syncutil/singleflight"
32
-	"golang.org/x/crypto/acme/autocert"
33
-	"golang.org/x/net/http2"
34
-)
35
-
36
-var (
37
-	prod = flag.Bool("prod", false, "Whether to configure itself to be the production http2.golang.org server.")
38
-
39
-	httpsAddr = flag.String("https_addr", "localhost:4430", "TLS address to listen on ('host:port' or ':port'). Required.")
40
-	httpAddr  = flag.String("http_addr", "", "Plain HTTP address to listen on ('host:port', or ':port'). Empty means no HTTP.")
41
-
42
-	hostHTTP  = flag.String("http_host", "", "Optional host or host:port to use for http:// links to this service. By default, this is implied from -http_addr.")
43
-	hostHTTPS = flag.String("https_host", "", "Optional host or host:port to use for http:// links to this service. By default, this is implied from -https_addr.")
44
-)
45
-
46
-func homeOldHTTP(w http.ResponseWriter, r *http.Request) {
47
-	io.WriteString(w, `<html>
48
-<body>
49
-<h1>Go + HTTP/2</h1>
50
-<p>Welcome to <a href="https://golang.org/">the Go language</a>'s <a href="https://http2.github.io/">HTTP/2</a> demo & interop server.</p>
51
-<p>Unfortunately, you're <b>not</b> using HTTP/2 right now. To do so:</p>
52
-<ul>
53
-   <li>Use Firefox Nightly or go to <b>about:config</b> and enable "network.http.spdy.enabled.http2draft"</li>
54
-   <li>Use Google Chrome Canary and/or go to <b>chrome://flags/#enable-spdy4</b> to <i>Enable SPDY/4</i> (Chrome's name for HTTP/2)</li>
55
-</ul>
56
-<p>See code & instructions for connecting at <a href="https://github.com/golang/net/tree/master/http2">https://github.com/golang/net/tree/master/http2</a>.</p>
57
-
58
-</body></html>`)
59
-}
60
-
61
-func home(w http.ResponseWriter, r *http.Request) {
62
-	if r.URL.Path != "/" {
63
-		http.NotFound(w, r)
64
-		return
65
-	}
66
-	io.WriteString(w, `<html>
67
-<body>
68
-<h1>Go + HTTP/2</h1>
69
-
70
-<p>Welcome to <a href="https://golang.org/">the Go language</a>'s <a
71
-href="https://http2.github.io/">HTTP/2</a> demo & interop server.</p>
72
-
73
-<p>Congratulations, <b>you're using HTTP/2 right now</b>.</p>
74
-
75
-<p>This server exists for others in the HTTP/2 community to test their HTTP/2 client implementations and point out flaws in our server.</p>
76
-
77
-<p>
78
-The code is at <a href="https://golang.org/x/net/http2">golang.org/x/net/http2</a> and
79
-is used transparently by the Go standard library from Go 1.6 and later.
80
-</p>
81
-
82
-<p>Contact info: <i>bradfitz@golang.org</i>, or <a
83
-href="https://golang.org/s/http2bug">file a bug</a>.</p>
84
-
85
-<h2>Handlers for testing</h2>
86
-<ul>
87
-  <li>GET <a href="/reqinfo">/reqinfo</a> to dump the request + headers received</li>
88
-  <li>GET <a href="/clockstream">/clockstream</a> streams the current time every second</li>
89
-  <li>GET <a href="/gophertiles">/gophertiles</a> to see a page with a bunch of images</li>
90
-  <li>GET <a href="/serverpush">/serverpush</a> to see a page with server push</li>
91
-  <li>GET <a href="/file/gopher.png">/file/gopher.png</a> for a small file (does If-Modified-Since, Content-Range, etc)</li>
92
-  <li>GET <a href="/file/go.src.tar.gz">/file/go.src.tar.gz</a> for a larger file (~10 MB)</li>
93
-  <li>GET <a href="/redirect">/redirect</a> to redirect back to / (this page)</li>
94
-  <li>GET <a href="/goroutines">/goroutines</a> to see all active goroutines in this server</li>
95
-  <li>PUT something to <a href="/crc32">/crc32</a> to get a count of number of bytes and its CRC-32</li>
96
-  <li>PUT something to <a href="/ECHO">/ECHO</a> and it will be streamed back to you capitalized</li>
97
-</ul>
98
-
99
-</body></html>`)
100
-}
101
-
102
-func reqInfoHandler(w http.ResponseWriter, r *http.Request) {
103
-	w.Header().Set("Content-Type", "text/plain")
104
-	fmt.Fprintf(w, "Method: %s\n", r.Method)
105
-	fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
106
-	fmt.Fprintf(w, "Host: %s\n", r.Host)
107
-	fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
108
-	fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
109
-	fmt.Fprintf(w, "URL: %#v\n", r.URL)
110
-	fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
111
-	fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
112
-	fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
113
-	fmt.Fprintf(w, "\nHeaders:\n")
114
-	r.Header.Write(w)
115
-}
116
-
117
-func crcHandler(w http.ResponseWriter, r *http.Request) {
118
-	if r.Method != "PUT" {
119
-		http.Error(w, "PUT required.", 400)
120
-		return
121
-	}
122
-	crc := crc32.NewIEEE()
123
-	n, err := io.Copy(crc, r.Body)
124
-	if err == nil {
125
-		w.Header().Set("Content-Type", "text/plain")
126
-		fmt.Fprintf(w, "bytes=%d, CRC32=%x", n, crc.Sum(nil))
127
-	}
128
-}
129
-
130
-type capitalizeReader struct {
131
-	r io.Reader
132
-}
133
-
134
-func (cr capitalizeReader) Read(p []byte) (n int, err error) {
135
-	n, err = cr.r.Read(p)
136
-	for i, b := range p[:n] {
137
-		if b >= 'a' && b <= 'z' {
138
-			p[i] = b - ('a' - 'A')
139
-		}
140
-	}
141
-	return
142
-}
143
-
144
-type flushWriter struct {
145
-	w io.Writer
146
-}
147
-
148
-func (fw flushWriter) Write(p []byte) (n int, err error) {
149
-	n, err = fw.w.Write(p)
150
-	if f, ok := fw.w.(http.Flusher); ok {
151
-		f.Flush()
152
-	}
153
-	return
154
-}
155
-
156
-func echoCapitalHandler(w http.ResponseWriter, r *http.Request) {
157
-	if r.Method != "PUT" {
158
-		http.Error(w, "PUT required.", 400)
159
-		return
160
-	}
161
-	io.Copy(flushWriter{w}, capitalizeReader{r.Body})
162
-}
163
-
164
-var (
165
-	fsGrp   singleflight.Group
166
-	fsMu    sync.Mutex // guards fsCache
167
-	fsCache = map[string]http.Handler{}
168
-)
169
-
170
-// fileServer returns a file-serving handler that proxies URL.
171
-// It lazily fetches URL on the first access and caches its contents forever.
172
-func fileServer(url string, latency time.Duration) http.Handler {
173
-	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
174
-		if latency > 0 {
175
-			time.Sleep(latency)
176
-		}
177
-		hi, err := fsGrp.Do(url, func() (interface{}, error) {
178
-			fsMu.Lock()
179
-			if h, ok := fsCache[url]; ok {
180
-				fsMu.Unlock()
181
-				return h, nil
182
-			}
183
-			fsMu.Unlock()
184
-
185
-			res, err := http.Get(url)
186
-			if err != nil {
187
-				return nil, err
188
-			}
189
-			defer res.Body.Close()
190
-			slurp, err := ioutil.ReadAll(res.Body)
191
-			if err != nil {
192
-				return nil, err
193
-			}
194
-
195
-			modTime := time.Now()
196
-			var h http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
197
-				http.ServeContent(w, r, path.Base(url), modTime, bytes.NewReader(slurp))
198
-			})
199
-			fsMu.Lock()
200
-			fsCache[url] = h
201
-			fsMu.Unlock()
202
-			return h, nil
203
-		})
204
-		if err != nil {
205
-			http.Error(w, err.Error(), 500)
206
-			return
207
-		}
208
-		hi.(http.Handler).ServeHTTP(w, r)
209
-	})
210
-}
211
-
212
-func clockStreamHandler(w http.ResponseWriter, r *http.Request) {
213
-	clientGone := w.(http.CloseNotifier).CloseNotify()
214
-	w.Header().Set("Content-Type", "text/plain")
215
-	ticker := time.NewTicker(1 * time.Second)
216
-	defer ticker.Stop()
217
-	fmt.Fprintf(w, "# ~1KB of junk to force browsers to start rendering immediately: \n")
218
-	io.WriteString(w, strings.Repeat("# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", 13))
219
-
220
-	for {
221
-		fmt.Fprintf(w, "%v\n", time.Now())
222
-		w.(http.Flusher).Flush()
223
-		select {
224
-		case <-ticker.C:
225
-		case <-clientGone:
226
-			log.Printf("Client %v disconnected from the clock", r.RemoteAddr)
227
-			return
228
-		}
229
-	}
230
-}
231
-
232
-func registerHandlers() {
233
-	tiles := newGopherTilesHandler()
234
-	push := newPushHandler()
235
-
236
-	mux2 := http.NewServeMux()
237
-	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
238
-		switch {
239
-		case r.URL.Path == "/gophertiles":
240
-			tiles.ServeHTTP(w, r) // allow HTTP/2 + HTTP/1.x
241
-			return
242
-		case strings.HasPrefix(r.URL.Path, "/serverpush"):
243
-			push.ServeHTTP(w, r) // allow HTTP/2 + HTTP/1.x
244
-			return
245
-		case r.TLS == nil: // do not allow HTTP/1.x for anything else
246
-			http.Redirect(w, r, "https://"+httpsHost()+"/", http.StatusFound)
247
-			return
248
-		}
249
-		if r.ProtoMajor == 1 {
250
-			if r.URL.Path == "/reqinfo" {
251
-				reqInfoHandler(w, r)
252
-				return
253
-			}
254
-			homeOldHTTP(w, r)
255
-			return
256
-		}
257
-		mux2.ServeHTTP(w, r)
258
-	})
259
-	mux2.HandleFunc("/", home)
260
-	mux2.Handle("/file/gopher.png", fileServer("https://golang.org/doc/gopher/frontpage.png", 0))
261
-	mux2.Handle("/file/go.src.tar.gz", fileServer("https://storage.googleapis.com/golang/go1.4.1.src.tar.gz", 0))
262
-	mux2.HandleFunc("/reqinfo", reqInfoHandler)
263
-	mux2.HandleFunc("/crc32", crcHandler)
264
-	mux2.HandleFunc("/ECHO", echoCapitalHandler)
265
-	mux2.HandleFunc("/clockstream", clockStreamHandler)
266
-	mux2.Handle("/gophertiles", tiles)
267
-	mux2.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
268
-		http.Redirect(w, r, "/", http.StatusFound)
269
-	})
270
-	stripHomedir := regexp.MustCompile(`/(Users|home)/\w+`)
271
-	mux2.HandleFunc("/goroutines", func(w http.ResponseWriter, r *http.Request) {
272
-		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
273
-		buf := make([]byte, 2<<20)
274
-		w.Write(stripHomedir.ReplaceAll(buf[:runtime.Stack(buf, true)], nil))
275
-	})
276
-}
277
-
278
-var pushResources = map[string]http.Handler{
279
-	"/serverpush/static/jquery.min.js": fileServer("https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js", 100*time.Millisecond),
280
-	"/serverpush/static/godocs.js":     fileServer("https://golang.org/lib/godoc/godocs.js", 100*time.Millisecond),
281
-	"/serverpush/static/playground.js": fileServer("https://golang.org/lib/godoc/playground.js", 100*time.Millisecond),
282
-	"/serverpush/static/style.css":     fileServer("https://golang.org/lib/godoc/style.css", 100*time.Millisecond),
283
-}
284
-
285
-func newPushHandler() http.Handler {
286
-	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
287
-		for path, handler := range pushResources {
288
-			if r.URL.Path == path {
289
-				handler.ServeHTTP(w, r)
290
-				return
291
-			}
292
-		}
293
-
294
-		cacheBust := time.Now().UnixNano()
295
-		if pusher, ok := w.(http.Pusher); ok {
296
-			for path := range pushResources {
297
-				url := fmt.Sprintf("%s?%d", path, cacheBust)
298
-				if err := pusher.Push(url, nil); err != nil {
299
-					log.Printf("Failed to push %v: %v", path, err)
300
-				}
301
-			}
302
-		}
303
-		time.Sleep(100 * time.Millisecond) // fake network latency + parsing time
304
-		if err := pushTmpl.Execute(w, struct {
305
-			CacheBust int64
306
-			HTTPSHost string
307
-			HTTPHost  string
308
-		}{
309
-			CacheBust: cacheBust,
310
-			HTTPSHost: httpsHost(),
311
-			HTTPHost:  httpHost(),
312
-		}); err != nil {
313
-			log.Printf("Executing server push template: %v", err)
314
-		}
315
-	})
316
-}
317
-
318
-func newGopherTilesHandler() http.Handler {
319
-	const gopherURL = "https://blog.golang.org/go-programming-language-turns-two_gophers.jpg"
320
-	res, err := http.Get(gopherURL)
321
-	if err != nil {
322
-		log.Fatal(err)
323
-	}
324
-	if res.StatusCode != 200 {
325
-		log.Fatalf("Error fetching %s: %v", gopherURL, res.Status)
326
-	}
327
-	slurp, err := ioutil.ReadAll(res.Body)
328
-	res.Body.Close()
329
-	if err != nil {
330
-		log.Fatal(err)
331
-	}
332
-	im, err := jpeg.Decode(bytes.NewReader(slurp))
333
-	if err != nil {
334
-		if len(slurp) > 1024 {
335
-			slurp = slurp[:1024]
336
-		}
337
-		log.Fatalf("Failed to decode gopher image: %v (got %q)", err, slurp)
338
-	}
339
-
340
-	type subImager interface {
341
-		SubImage(image.Rectangle) image.Image
342
-	}
343
-	const tileSize = 32
344
-	xt := im.Bounds().Max.X / tileSize
345
-	yt := im.Bounds().Max.Y / tileSize
346
-	var tile [][][]byte // y -> x -> jpeg bytes
347
-	for yi := 0; yi < yt; yi++ {
348
-		var row [][]byte
349
-		for xi := 0; xi < xt; xi++ {
350
-			si := im.(subImager).SubImage(image.Rectangle{
351
-				Min: image.Point{xi * tileSize, yi * tileSize},
352
-				Max: image.Point{(xi + 1) * tileSize, (yi + 1) * tileSize},
353
-			})
354
-			buf := new(bytes.Buffer)
355
-			if err := jpeg.Encode(buf, si, &jpeg.Options{Quality: 90}); err != nil {
356
-				log.Fatal(err)
357
-			}
358
-			row = append(row, buf.Bytes())
359
-		}
360
-		tile = append(tile, row)
361
-	}
362
-	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
363
-		ms, _ := strconv.Atoi(r.FormValue("latency"))
364
-		const nanosPerMilli = 1e6
365
-		if r.FormValue("x") != "" {
366
-			x, _ := strconv.Atoi(r.FormValue("x"))
367
-			y, _ := strconv.Atoi(r.FormValue("y"))
368
-			if ms <= 1000 {
369
-				time.Sleep(time.Duration(ms) * nanosPerMilli)
370
-			}
371
-			if x >= 0 && x < xt && y >= 0 && y < yt {
372
-				http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(tile[y][x]))
373
-				return
374
-			}
375
-		}
376
-		io.WriteString(w, "<html><body onload='showtimes()'>")
377
-		fmt.Fprintf(w, "A grid of %d tiled images is below. Compare:<p>", xt*yt)
378
-		for _, ms := range []int{0, 30, 200, 1000} {
379
-			d := time.Duration(ms) * nanosPerMilli
380
-			fmt.Fprintf(w, "[<a href='https://%s/gophertiles?latency=%d'>HTTP/2, %v latency</a>] [<a href='http://%s/gophertiles?latency=%d'>HTTP/1, %v latency</a>]<br>\n",
381
-				httpsHost(), ms, d,
382
-				httpHost(), ms, d,
383
-			)
384
-		}
385
-		io.WriteString(w, "<p>\n")
386
-		cacheBust := time.Now().UnixNano()
387
-		for y := 0; y < yt; y++ {
388
-			for x := 0; x < xt; x++ {
389
-				fmt.Fprintf(w, "<img width=%d height=%d src='/gophertiles?x=%d&y=%d&cachebust=%d&latency=%d'>",
390
-					tileSize, tileSize, x, y, cacheBust, ms)
391
-			}
392
-			io.WriteString(w, "<br/>\n")
393
-		}
394
-		io.WriteString(w, `<p><div id='loadtimes'></div></p>
395
-<script>
396
-function showtimes() {
397
-	var times = 'Times from connection start:<br>'
398
-	times += 'DOM loaded: ' + (window.performance.timing.domContentLoadedEventEnd - window.performance.timing.connectStart) + 'ms<br>'
399
-	times += 'DOM complete (images loaded): ' + (window.performance.timing.domComplete - window.performance.timing.connectStart) + 'ms<br>'
400
-	document.getElementById('loadtimes').innerHTML = times
401
-}
402
-</script>
403
-<hr><a href='/'>&lt;&lt Back to Go HTTP/2 demo server</a></body></html>`)
404
-	})
405
-}
406
-
407
-func httpsHost() string {
408
-	if *hostHTTPS != "" {
409
-		return *hostHTTPS
410
-	}
411
-	if v := *httpsAddr; strings.HasPrefix(v, ":") {
412
-		return "localhost" + v
413
-	} else {
414
-		return v
415
-	}
416
-}
417
-
418
-func httpHost() string {
419
-	if *hostHTTP != "" {
420
-		return *hostHTTP
421
-	}
422
-	if v := *httpAddr; strings.HasPrefix(v, ":") {
423
-		return "localhost" + v
424
-	} else {
425
-		return v
426
-	}
427
-}
428
-
429
-func serveProdTLS() error {
430
-	const cacheDir = "/var/cache/autocert"
431
-	if err := os.MkdirAll(cacheDir, 0700); err != nil {
432
-		return err
433
-	}
434
-	m := autocert.Manager{
435
-		Cache:      autocert.DirCache(cacheDir),
436
-		Prompt:     autocert.AcceptTOS,
437
-		HostPolicy: autocert.HostWhitelist("http2.golang.org"),
438
-	}
439
-	srv := &http.Server{
440
-		TLSConfig: &tls.Config{
441
-			GetCertificate: m.GetCertificate,
442
-		},
443
-	}
444
-	http2.ConfigureServer(srv, &http2.Server{
445
-		NewWriteScheduler: func() http2.WriteScheduler {
446
-			return http2.NewPriorityWriteScheduler(nil)
447
-		},
448
-	})
449
-	ln, err := net.Listen("tcp", ":443")
450
-	if err != nil {
451
-		return err
452
-	}
453
-	return srv.Serve(tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, srv.TLSConfig))
454
-}
455
-
456
-type tcpKeepAliveListener struct {
457
-	*net.TCPListener
458
-}
459
-
460
-func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
461
-	tc, err := ln.AcceptTCP()
462
-	if err != nil {
463
-		return
464
-	}
465
-	tc.SetKeepAlive(true)
466
-	tc.SetKeepAlivePeriod(3 * time.Minute)
467
-	return tc, nil
468
-}
469
-
470
-func serveProd() error {
471
-	errc := make(chan error, 2)
472
-	go func() { errc <- http.ListenAndServe(":80", nil) }()
473
-	go func() { errc <- serveProdTLS() }()
474
-	return <-errc
475
-}
476
-
477
-const idleTimeout = 5 * time.Minute
478
-const activeTimeout = 10 * time.Minute
479
-
480
-// TODO: put this into the standard library and actually send
481
-// PING frames and GOAWAY, etc: golang.org/issue/14204
482
-func idleTimeoutHook() func(net.Conn, http.ConnState) {
483
-	var mu sync.Mutex
484
-	m := map[net.Conn]*time.Timer{}
485
-	return func(c net.Conn, cs http.ConnState) {
486
-		mu.Lock()
487
-		defer mu.Unlock()
488
-		if t, ok := m[c]; ok {
489
-			delete(m, c)
490
-			t.Stop()
491
-		}
492
-		var d time.Duration
493
-		switch cs {
494
-		case http.StateNew, http.StateIdle:
495
-			d = idleTimeout
496
-		case http.StateActive:
497
-			d = activeTimeout
498
-		default:
499
-			return
500
-		}
501
-		m[c] = time.AfterFunc(d, func() {
502
-			log.Printf("closing idle conn %v after %v", c.RemoteAddr(), d)
503
-			go c.Close()
504
-		})
505
-	}
506
-}
507
-
508
-func main() {
509
-	var srv http.Server
510
-	flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
511
-	flag.Parse()
512
-	srv.Addr = *httpsAddr
513
-	srv.ConnState = idleTimeoutHook()
514
-
515
-	registerHandlers()
516
-
517
-	if *prod {
518
-		*hostHTTP = "http2.golang.org"
519
-		*hostHTTPS = "http2.golang.org"
520
-		log.Fatal(serveProd())
521
-	}
522
-
523
-	url := "https://" + httpsHost() + "/"
524
-	log.Printf("Listening on " + url)
525
-	http2.ConfigureServer(&srv, &http2.Server{})
526
-
527
-	if *httpAddr != "" {
528
-		go func() {
529
-			log.Printf("Listening on http://" + httpHost() + "/ (for unencrypted HTTP/1)")
530
-			log.Fatal(http.ListenAndServe(*httpAddr, nil))
531
-		}()
532
-	}
533
-
534
-	go func() {
535
-		log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
536
-	}()
537
-	select {}
538
-}

+ 0
- 302
vendor/golang.org/x/net/http2/h2demo/launch.go View File

1
-// Copyright 2014 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
-// +build ignore
6
-
7
-package main
8
-
9
-import (
10
-	"bufio"
11
-	"bytes"
12
-	"encoding/json"
13
-	"flag"
14
-	"fmt"
15
-	"io"
16
-	"io/ioutil"
17
-	"log"
18
-	"net/http"
19
-	"os"
20
-	"strings"
21
-	"time"
22
-
23
-	"golang.org/x/oauth2"
24
-	"golang.org/x/oauth2/google"
25
-	compute "google.golang.org/api/compute/v1"
26
-)
27
-
28
-var (
29
-	proj     = flag.String("project", "symbolic-datum-552", "name of Project")
30
-	zone     = flag.String("zone", "us-central1-a", "GCE zone")
31
-	mach     = flag.String("machinetype", "n1-standard-1", "Machine type")
32
-	instName = flag.String("instance_name", "http2-demo", "Name of VM instance.")
33
-	sshPub   = flag.String("ssh_public_key", "", "ssh public key file to authorize. Can modify later in Google's web UI anyway.")
34
-	staticIP = flag.String("static_ip", "130.211.116.44", "Static IP to use. If empty, automatic.")
35
-
36
-	writeObject  = flag.String("write_object", "", "If non-empty, a VM isn't created and the flag value is Google Cloud Storage bucket/object to write. The contents from stdin.")
37
-	publicObject = flag.Bool("write_object_is_public", false, "Whether the object created by --write_object should be public.")
38
-)
39
-
40
-func readFile(v string) string {
41
-	slurp, err := ioutil.ReadFile(v)
42
-	if err != nil {
43
-		log.Fatalf("Error reading %s: %v", v, err)
44
-	}
45
-	return strings.TrimSpace(string(slurp))
46
-}
47
-
48
-var config = &oauth2.Config{
49
-	// The client-id and secret should be for an "Installed Application" when using
50
-	// the CLI. Later we'll use a web application with a callback.
51
-	ClientID:     readFile("client-id.dat"),
52
-	ClientSecret: readFile("client-secret.dat"),
53
-	Endpoint:     google.Endpoint,
54
-	Scopes: []string{
55
-		compute.DevstorageFullControlScope,
56
-		compute.ComputeScope,
57
-		"https://www.googleapis.com/auth/sqlservice",
58
-		"https://www.googleapis.com/auth/sqlservice.admin",
59
-	},
60
-	RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
61
-}
62
-
63
-const baseConfig = `#cloud-config
64
-coreos:
65
-  units:
66
-    - name: h2demo.service
67
-      command: start
68
-      content: |
69
-        [Unit]
70
-        Description=HTTP2 Demo
71
-        
72
-        [Service]
73
-        ExecStartPre=/bin/bash -c 'mkdir -p /opt/bin && curl -s -o /opt/bin/h2demo http://storage.googleapis.com/http2-demo-server-tls/h2demo && chmod +x /opt/bin/h2demo'
74
-        ExecStart=/opt/bin/h2demo --prod
75
-        RestartSec=5s
76
-        Restart=always
77
-        Type=simple
78
-        
79
-        [Install]
80
-        WantedBy=multi-user.target
81
-`
82
-
83
-func main() {
84
-	flag.Parse()
85
-	if *proj == "" {
86
-		log.Fatalf("Missing --project flag")
87
-	}
88
-	prefix := "https://www.googleapis.com/compute/v1/projects/" + *proj
89
-	machType := prefix + "/zones/" + *zone + "/machineTypes/" + *mach
90
-
91
-	const tokenFileName = "token.dat"
92
-	tokenFile := tokenCacheFile(tokenFileName)
93
-	tokenSource := oauth2.ReuseTokenSource(nil, tokenFile)
94
-	token, err := tokenSource.Token()
95
-	if err != nil {
96
-		if *writeObject != "" {
97
-			log.Fatalf("Can't use --write_object without a valid token.dat file already cached.")
98
-		}
99
-		log.Printf("Error getting token from %s: %v", tokenFileName, err)
100
-		log.Printf("Get auth code from %v", config.AuthCodeURL("my-state"))
101
-		fmt.Print("\nEnter auth code: ")
102
-		sc := bufio.NewScanner(os.Stdin)
103
-		sc.Scan()
104
-		authCode := strings.TrimSpace(sc.Text())
105
-		token, err = config.Exchange(oauth2.NoContext, authCode)
106
-		if err != nil {
107
-			log.Fatalf("Error exchanging auth code for a token: %v", err)
108
-		}
109
-		if err := tokenFile.WriteToken(token); err != nil {
110
-			log.Fatalf("Error writing to %s: %v", tokenFileName, err)
111
-		}
112
-		tokenSource = oauth2.ReuseTokenSource(token, nil)
113
-	}
114
-
115
-	oauthClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
116
-
117
-	if *writeObject != "" {
118
-		writeCloudStorageObject(oauthClient)
119
-		return
120
-	}
121
-
122
-	computeService, _ := compute.New(oauthClient)
123
-
124
-	natIP := *staticIP
125
-	if natIP == "" {
126
-		// Try to find it by name.
127
-		aggAddrList, err := computeService.Addresses.AggregatedList(*proj).Do()
128
-		if err != nil {
129
-			log.Fatal(err)
130
-		}
131
-		// http://godoc.org/code.google.com/p/google-api-go-client/compute/v1#AddressAggregatedList
132
-	IPLoop:
133
-		for _, asl := range aggAddrList.Items {
134
-			for _, addr := range asl.Addresses {
135
-				if addr.Name == *instName+"-ip" && addr.Status == "RESERVED" {
136
-					natIP = addr.Address
137
-					break IPLoop
138
-				}
139
-			}
140
-		}
141
-	}
142
-
143
-	cloudConfig := baseConfig
144
-	if *sshPub != "" {
145
-		key := strings.TrimSpace(readFile(*sshPub))
146
-		cloudConfig += fmt.Sprintf("\nssh_authorized_keys:\n    - %s\n", key)
147
-	}
148
-	if os.Getenv("USER") == "bradfitz" {
149
-		cloudConfig += fmt.Sprintf("\nssh_authorized_keys:\n    - %s\n", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAwks9dwWKlRC+73gRbvYtVg0vdCwDSuIlyt4z6xa/YU/jTDynM4R4W10hm2tPjy8iR1k8XhDv4/qdxe6m07NjG/By1tkmGpm1mGwho4Pr5kbAAy/Qg+NLCSdAYnnE00FQEcFOC15GFVMOW2AzDGKisReohwH9eIzHPzdYQNPRWXE= bradfitz@papag.bradfitz.com")
150
-	}
151
-	const maxCloudConfig = 32 << 10 // per compute API docs
152
-	if len(cloudConfig) > maxCloudConfig {
153
-		log.Fatalf("cloud config length of %d bytes is over %d byte limit", len(cloudConfig), maxCloudConfig)
154
-	}
155
-
156
-	instance := &compute.Instance{
157
-		Name:        *instName,
158
-		Description: "Go Builder",
159
-		MachineType: machType,
160
-		Disks:       []*compute.AttachedDisk{instanceDisk(computeService)},
161
-		Tags: &compute.Tags{
162
-			Items: []string{"http-server", "https-server"},
163
-		},
164
-		Metadata: &compute.Metadata{
165
-			Items: []*compute.MetadataItems{
166
-				{
167
-					Key:   "user-data",
168
-					Value: &cloudConfig,
169
-				},
170
-			},
171
-		},
172
-		NetworkInterfaces: []*compute.NetworkInterface{
173
-			{
174
-				AccessConfigs: []*compute.AccessConfig{
175
-					{
176
-						Type:  "ONE_TO_ONE_NAT",
177
-						Name:  "External NAT",
178
-						NatIP: natIP,
179
-					},
180
-				},
181
-				Network: prefix + "/global/networks/default",
182
-			},
183
-		},
184
-		ServiceAccounts: []*compute.ServiceAccount{
185
-			{
186
-				Email: "default",
187
-				Scopes: []string{
188
-					compute.DevstorageFullControlScope,
189
-					compute.ComputeScope,
190
-				},
191
-			},
192
-		},
193
-	}
194
-
195
-	log.Printf("Creating instance...")
196
-	op, err := computeService.Instances.Insert(*proj, *zone, instance).Do()
197
-	if err != nil {
198
-		log.Fatalf("Failed to create instance: %v", err)
199
-	}
200
-	opName := op.Name
201
-	log.Printf("Created. Waiting on operation %v", opName)
202
-OpLoop:
203
-	for {
204
-		time.Sleep(2 * time.Second)
205
-		op, err := computeService.ZoneOperations.Get(*proj, *zone, opName).Do()
206
-		if err != nil {
207
-			log.Fatalf("Failed to get op %s: %v", opName, err)
208
-		}
209
-		switch op.Status {
210
-		case "PENDING", "RUNNING":
211
-			log.Printf("Waiting on operation %v", opName)
212
-			continue
213
-		case "DONE":
214
-			if op.Error != nil {
215
-				for _, operr := range op.Error.Errors {
216
-					log.Printf("Error: %+v", operr)
217
-				}
218
-				log.Fatalf("Failed to start.")
219
-			}
220
-			log.Printf("Success. %+v", op)
221
-			break OpLoop
222
-		default:
223
-			log.Fatalf("Unknown status %q: %+v", op.Status, op)
224
-		}
225
-	}
226
-
227
-	inst, err := computeService.Instances.Get(*proj, *zone, *instName).Do()
228
-	if err != nil {
229
-		log.Fatalf("Error getting instance after creation: %v", err)
230
-	}
231
-	ij, _ := json.MarshalIndent(inst, "", "    ")
232
-	log.Printf("Instance: %s", ij)
233
-}
234
-
235
-func instanceDisk(svc *compute.Service) *compute.AttachedDisk {
236
-	const imageURL = "https://www.googleapis.com/compute/v1/projects/coreos-cloud/global/images/coreos-stable-444-5-0-v20141016"
237
-	diskName := *instName + "-disk"
238
-
239
-	return &compute.AttachedDisk{
240
-		AutoDelete: true,
241
-		Boot:       true,
242
-		Type:       "PERSISTENT",
243
-		InitializeParams: &compute.AttachedDiskInitializeParams{
244
-			DiskName:    diskName,
245
-			SourceImage: imageURL,
246
-			DiskSizeGb:  50,
247
-		},
248
-	}
249
-}
250
-
251
-func writeCloudStorageObject(httpClient *http.Client) {
252
-	content := os.Stdin
253
-	const maxSlurp = 1 << 20
254
-	var buf bytes.Buffer
255
-	n, err := io.CopyN(&buf, content, maxSlurp)
256
-	if err != nil && err != io.EOF {
257
-		log.Fatalf("Error reading from stdin: %v, %v", n, err)
258
-	}
259
-	contentType := http.DetectContentType(buf.Bytes())
260
-
261
-	req, err := http.NewRequest("PUT", "https://storage.googleapis.com/"+*writeObject, io.MultiReader(&buf, content))
262
-	if err != nil {
263
-		log.Fatal(err)
264
-	}
265
-	req.Header.Set("x-goog-api-version", "2")
266
-	if *publicObject {
267
-		req.Header.Set("x-goog-acl", "public-read")
268
-	}
269
-	req.Header.Set("Content-Type", contentType)
270
-	res, err := httpClient.Do(req)
271
-	if err != nil {
272
-		log.Fatal(err)
273
-	}
274
-	if res.StatusCode != 200 {
275
-		res.Write(os.Stderr)
276
-		log.Fatalf("Failed.")
277
-	}
278
-	log.Printf("Success.")
279
-	os.Exit(0)
280
-}
281
-
282
-type tokenCacheFile string
283
-
284
-func (f tokenCacheFile) Token() (*oauth2.Token, error) {
285
-	slurp, err := ioutil.ReadFile(string(f))
286
-	if err != nil {
287
-		return nil, err
288
-	}
289
-	t := new(oauth2.Token)
290
-	if err := json.Unmarshal(slurp, t); err != nil {
291
-		return nil, err
292
-	}
293
-	return t, nil
294
-}
295
-
296
-func (f tokenCacheFile) WriteToken(t *oauth2.Token) error {
297
-	jt, err := json.Marshal(t)
298
-	if err != nil {
299
-		return err
300
-	}
301
-	return ioutil.WriteFile(string(f), jt, 0600)
302
-}

+ 0
- 27
vendor/golang.org/x/net/http2/h2demo/rootCA.key View File

1
------BEGIN RSA PRIVATE KEY-----
2
-MIIEowIBAAKCAQEAt5fAjp4fTcekWUTfzsp0kyih1OYbsGL0KX1eRbSSR8Od0+9Q
3
-62Hyny+GFwMTb4A/KU8mssoHvcceSAAbwfbxFK/+s51TobqUnORZrOoTZjkUygby
4
-XDSK99YBbcR1Pip8vwMTm4XKuLtCigeBBdjjAQdgUO28LENGlsMnmeYkJfODVGnV
5
-mr5Ltb9ANA8IKyTfsnHJ4iOCS/PlPbUj2q7YnoVLposUBMlgUb/CykX3mOoLb4yJ
6
-JQyA/iST6ZxiIEj36D4yWZ5lg7YJl+UiiBQHGCnPdGyipqV06ex0heYWcaiW8LWZ
7
-SUQ93jQ+WVCH8hT7DQO1dmsvUmXlq/JeAlwQ/QIDAQABAoIBAFFHV7JMAqPWnMYA
8
-nezY6J81v9+XN+7xABNWM2Q8uv4WdksbigGLTXR3/680Z2hXqJ7LMeC5XJACFT/e
9
-/Gr0vmpgOCygnCPfjGehGKpavtfksXV3edikUlnCXsOP1C//c1bFL+sMYmFCVgTx
10
-qYdDK8yKzXNGrKYT6q5YG7IglyRNV1rsQa8lM/5taFYiD1Ck/3tQi3YIq8Lcuser
11
-hrxsMABcQ6mi+EIvG6Xr4mfJug0dGJMHG4RG1UGFQn6RXrQq2+q53fC8ZbVUSi0j
12
-NQ918aKFzktwv+DouKU0ME4I9toks03gM860bAL7zCbKGmwR3hfgX/TqzVCWpG9E
13
-LDVfvekCgYEA8fk9N53jbBRmULUGEf4qWypcLGiZnNU0OeXWpbPV9aa3H0VDytA7
14
-8fCN2dPAVDPqlthMDdVe983NCNwp2Yo8ZimDgowyIAKhdC25s1kejuaiH9OAPj3c
15
-0f8KbriYX4n8zNHxFwK6Ae3pQ6EqOLJVCUsziUaZX9nyKY5aZlyX6xcCgYEAwjws
16
-K62PjC64U5wYddNLp+kNdJ4edx+a7qBb3mEgPvSFT2RO3/xafJyG8kQB30Mfstjd
17
-bRxyUV6N0vtX1zA7VQtRUAvfGCecpMo+VQZzcHXKzoRTnQ7eZg4Lmj5fQ9tOAKAo
18
-QCVBoSW/DI4PZL26CAMDcAba4Pa22ooLapoRIQsCgYA6pIfkkbxLNkpxpt2YwLtt
19
-Kr/590O7UaR9n6k8sW/aQBRDXNsILR1KDl2ifAIxpf9lnXgZJiwE7HiTfCAcW7c1
20
-nzwDCI0hWuHcMTS/NYsFYPnLsstyyjVZI3FY0h4DkYKV9Q9z3zJLQ2hz/nwoD3gy
21
-b2pHC7giFcTts1VPV4Nt8wKBgHeFn4ihHJweg76vZz3Z78w7VNRWGFklUalVdDK7
22
-gaQ7w2y/ROn/146mo0OhJaXFIFRlrpvdzVrU3GDf2YXJYDlM5ZRkObwbZADjksev
23
-WInzcgDy3KDg7WnPasRXbTfMU4t/AkW2p1QKbi3DnSVYuokDkbH2Beo45vxDxhKr
24
-C69RAoGBAIyo3+OJenoZmoNzNJl2WPW5MeBUzSh8T/bgyjFTdqFHF5WiYRD/lfHj
25
-x9Glyw2nutuT4hlOqHvKhgTYdDMsF2oQ72fe3v8Q5FU7FuKndNPEAyvKNXZaShVA
26
-hnlhv5DjXKb0wFWnt5PCCiQLtzG0yyHaITrrEme7FikkIcTxaX/Y
27
------END RSA PRIVATE KEY-----

+ 0
- 26
vendor/golang.org/x/net/http2/h2demo/rootCA.pem View File

1
------BEGIN CERTIFICATE-----
2
-MIIEWjCCA0KgAwIBAgIJALfRlWsI8YQHMA0GCSqGSIb3DQEBBQUAMHsxCzAJBgNV
3
-BAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNU2FuIEZyYW5jaXNjbzEUMBIG
4
-A1UEChMLQnJhZGZpdHppbmMxEjAQBgNVBAMTCWxvY2FsaG9zdDEdMBsGCSqGSIb3
5
-DQEJARYOYnJhZEBkYW5nYS5jb20wHhcNMTQwNzE1MjA0NjA1WhcNMTcwNTA0MjA0
6
-NjA1WjB7MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVNhbiBG
7
-cmFuY2lzY28xFDASBgNVBAoTC0JyYWRmaXR6aW5jMRIwEAYDVQQDEwlsb2NhbGhv
8
-c3QxHTAbBgkqhkiG9w0BCQEWDmJyYWRAZGFuZ2EuY29tMIIBIjANBgkqhkiG9w0B
9
-AQEFAAOCAQ8AMIIBCgKCAQEAt5fAjp4fTcekWUTfzsp0kyih1OYbsGL0KX1eRbSS
10
-R8Od0+9Q62Hyny+GFwMTb4A/KU8mssoHvcceSAAbwfbxFK/+s51TobqUnORZrOoT
11
-ZjkUygbyXDSK99YBbcR1Pip8vwMTm4XKuLtCigeBBdjjAQdgUO28LENGlsMnmeYk
12
-JfODVGnVmr5Ltb9ANA8IKyTfsnHJ4iOCS/PlPbUj2q7YnoVLposUBMlgUb/CykX3
13
-mOoLb4yJJQyA/iST6ZxiIEj36D4yWZ5lg7YJl+UiiBQHGCnPdGyipqV06ex0heYW
14
-caiW8LWZSUQ93jQ+WVCH8hT7DQO1dmsvUmXlq/JeAlwQ/QIDAQABo4HgMIHdMB0G
15
-A1UdDgQWBBRcAROthS4P4U7vTfjByC569R7E6DCBrQYDVR0jBIGlMIGigBRcAROt
16
-hS4P4U7vTfjByC569R7E6KF/pH0wezELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNB
17
-MRYwFAYDVQQHEw1TYW4gRnJhbmNpc2NvMRQwEgYDVQQKEwtCcmFkZml0emluYzES
18
-MBAGA1UEAxMJbG9jYWxob3N0MR0wGwYJKoZIhvcNAQkBFg5icmFkQGRhbmdhLmNv
19
-bYIJALfRlWsI8YQHMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAG6h
20
-U9f9sNH0/6oBbGGy2EVU0UgITUQIrFWo9rFkrW5k/XkDjQm+3lzjT0iGR4IxE/Ao
21
-eU6sQhua7wrWeFEn47GL98lnCsJdD7oZNhFmQ95Tb/LnDUjs5Yj9brP0NWzXfYU4
22
-UK2ZnINJRcJpB8iRCaCxE8DdcUF0XqIEq6pA272snoLmiXLMvNl3kYEdm+je6voD
23
-58SNVEUsztzQyXmJEhCpwVI0A6QCjzXj+qvpmw3ZZHi8JwXei8ZZBLTSFBki8Z7n
24
-sH9BBH38/SzUmAN4QHSPy1gjqm00OAE8NaYDkh/bzE4d7mLGGMWp/WE3KPSu82HF
25
-kPe6XoSbiLm/kxk32T0=
26
------END CERTIFICATE-----

+ 0
- 1
vendor/golang.org/x/net/http2/h2demo/rootCA.srl View File

1
-E2CE26BF3285059C

+ 0
- 20
vendor/golang.org/x/net/http2/h2demo/server.crt View File

1
------BEGIN CERTIFICATE-----
2
-MIIDPjCCAiYCCQDizia/MoUFnDANBgkqhkiG9w0BAQUFADB7MQswCQYDVQQGEwJV
3
-UzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDVNhbiBGcmFuY2lzY28xFDASBgNVBAoT
4
-C0JyYWRmaXR6aW5jMRIwEAYDVQQDEwlsb2NhbGhvc3QxHTAbBgkqhkiG9w0BCQEW
5
-DmJyYWRAZGFuZ2EuY29tMB4XDTE0MDcxNTIwNTAyN1oXDTE1MTEyNzIwNTAyN1ow
6
-RzELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMQswCQYDVQQHEwJTRjEeMBwGA1UE
7
-ChMVYnJhZGZpdHogaHR0cDIgc2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
8
-MIIBCgKCAQEAs1Y9CyLFrdL8VQWN1WaifDqaZFnoqjHhCMlc1TfG2zA+InDifx2l
9
-gZD3o8FeNnAcfM2sPlk3+ZleOYw9P/CklFVDlvqmpCv9ss/BEp/dDaWvy1LmJ4c2
10
-dbQJfmTxn7CV1H3TsVJvKdwFmdoABb41NoBp6+NNO7OtDyhbIMiCI0pL3Nefb3HL
11
-A7hIMo3DYbORTtJLTIH9W8YKrEWL0lwHLrYFx/UdutZnv+HjdmO6vCN4na55mjws
12
-/vjKQUmc7xeY7Xe20xDEG2oDKVkL2eD7FfyrYMS3rO1ExP2KSqlXYG/1S9I/fz88
13
-F0GK7HX55b5WjZCl2J3ERVdnv/0MQv+sYQIDAQABMA0GCSqGSIb3DQEBBQUAA4IB
14
-AQC0zL+n/YpRZOdulSu9tS8FxrstXqGWoxfe+vIUgqfMZ5+0MkjJ/vW0FqlLDl2R
15
-rn4XaR3e7FmWkwdDVbq/UB6lPmoAaFkCgh9/5oapMaclNVNnfF3fjCJfRr+qj/iD
16
-EmJStTIN0ZuUjAlpiACmfnpEU55PafT5Zx+i1yE4FGjw8bJpFoyD4Hnm54nGjX19
17
-KeCuvcYFUPnBm3lcL0FalF2AjqV02WTHYNQk7YF/oeO7NKBoEgvGvKG3x+xaOeBI
18
-dwvdq175ZsGul30h+QjrRlXhH/twcuaT3GSdoysDl9cCYE8f1Mk8PD6gan3uBCJU
19
-90p6/CbU71bGbfpM2PHot2fm
20
------END CERTIFICATE-----

+ 0
- 27
vendor/golang.org/x/net/http2/h2demo/server.key View File

1
------BEGIN RSA PRIVATE KEY-----
2
-MIIEowIBAAKCAQEAs1Y9CyLFrdL8VQWN1WaifDqaZFnoqjHhCMlc1TfG2zA+InDi
3
-fx2lgZD3o8FeNnAcfM2sPlk3+ZleOYw9P/CklFVDlvqmpCv9ss/BEp/dDaWvy1Lm
4
-J4c2dbQJfmTxn7CV1H3TsVJvKdwFmdoABb41NoBp6+NNO7OtDyhbIMiCI0pL3Nef
5
-b3HLA7hIMo3DYbORTtJLTIH9W8YKrEWL0lwHLrYFx/UdutZnv+HjdmO6vCN4na55
6
-mjws/vjKQUmc7xeY7Xe20xDEG2oDKVkL2eD7FfyrYMS3rO1ExP2KSqlXYG/1S9I/
7
-fz88F0GK7HX55b5WjZCl2J3ERVdnv/0MQv+sYQIDAQABAoIBADQ2spUwbY+bcz4p
8
-3M66ECrNQTBggP40gYl2XyHxGGOu2xhZ94f9ELf1hjRWU2DUKWco1rJcdZClV6q3
9
-qwmXvcM2Q/SMS8JW0ImkNVl/0/NqPxGatEnj8zY30d/L8hGFb0orzFu/XYA5gCP4
10
-NbN2WrXgk3ZLeqwcNxHHtSiJWGJ/fPyeDWAu/apy75u9Xf2GlzBZmV6HYD9EfK80
11
-LTlI60f5FO487CrJnboL7ovPJrIHn+k05xRQqwma4orpz932rTXnTjs9Lg6KtbQN
12
-a7PrqfAntIISgr11a66Mng3IYH1lYqJsWJJwX/xHT4WLEy0EH4/0+PfYemJekz2+
13
-Co62drECgYEA6O9zVJZXrLSDsIi54cfxA7nEZWm5CAtkYWeAHa4EJ+IlZ7gIf9sL
14
-W8oFcEfFGpvwVqWZ+AsQ70dsjXAv3zXaG0tmg9FtqWp7pzRSMPidifZcQwWkKeTO
15
-gJnFmnVyed8h6GfjTEu4gxo1/S5U0V+mYSha01z5NTnN6ltKx1Or3b0CgYEAxRgm
16
-S30nZxnyg/V7ys61AZhst1DG2tkZXEMcA7dYhabMoXPJAP/EfhlWwpWYYUs/u0gS
17
-Wwmf5IivX5TlYScgmkvb/NYz0u4ZmOXkLTnLPtdKKFXhjXJcHjUP67jYmOxNlJLp
18
-V4vLRnFxTpffAV+OszzRxsXX6fvruwZBANYJeXUCgYBVouLFsFgfWGYp2rpr9XP4
19
-KK25kvrBqF6JKOIDB1zjxNJ3pUMKrl8oqccCFoCyXa4oTM2kUX0yWxHfleUjrMq4
20
-yimwQKiOZmV7fVLSSjSw6e/VfBd0h3gb82ygcplZkN0IclkwTY5SNKqwn/3y07V5
21
-drqdhkrgdJXtmQ6O5YYECQKBgATERcDToQ1USlI4sKrB/wyv1AlG8dg/IebiVJ4e
22
-ZAyvcQmClFzq0qS+FiQUnB/WQw9TeeYrwGs1hxBHuJh16srwhLyDrbMvQP06qh8R
23
-48F8UXXSRec22dV9MQphaROhu2qZdv1AC0WD3tqov6L33aqmEOi+xi8JgbT/PLk5
24
-c/c1AoGBAI1A/02ryksW6/wc7/6SP2M2rTy4m1sD/GnrTc67EHnRcVBdKO6qH2RY
25
-nqC8YcveC2ZghgPTDsA3VGuzuBXpwY6wTyV99q6jxQJ6/xcrD9/NUG6Uwv/xfCxl
26
-IJLeBYEqQundSSny3VtaAUK8Ul1nxpTvVRNwtcyWTo8RHAAyNPWd
27
------END RSA PRIVATE KEY-----

+ 0
- 1991
vendor/golang.org/x/net/http2/h2demo/tmpl.go
File diff suppressed because it is too large
View File


+ 0
- 97
vendor/golang.org/x/net/http2/h2i/README.md View File

1
-# h2i
2
-
3
-**h2i** is an interactive HTTP/2 ("h2") console debugger. Miss the good ol'
4
-days of telnetting to your HTTP/1.n servers? We're bringing you
5
-back.
6
-
7
-Features:
8
-- send raw HTTP/2 frames
9
- - PING
10
- - SETTINGS
11
- - HEADERS
12
- - etc
13
-- type in HTTP/1.n and have it auto-HPACK/frame-ify it for HTTP/2
14
-- pretty print all received HTTP/2 frames from the peer (including HPACK decoding)
15
-- tab completion of commands, options
16
-
17
-Not yet features, but soon:
18
-- unnecessary CONTINUATION frames on short boundaries, to test peer implementations 
19
-- request bodies (DATA frames)
20
-- send invalid frames for testing server implementations (supported by underlying Framer)
21
-
22
-Later:
23
-- act like a server
24
-
25
-## Installation
26
-
27
-```
28
-$ go get golang.org/x/net/http2/h2i
29
-$ h2i <host>
30
-```
31
-
32
-## Demo
33
-
34
-```
35
-$ h2i
36
-Usage: h2i <hostname>
37
-  
38
-  -insecure
39
-        Whether to skip TLS cert validation
40
-  -nextproto string
41
-        Comma-separated list of NPN/ALPN protocol names to negotiate. (default "h2,h2-14")
42
-
43
-$ h2i google.com
44
-Connecting to google.com:443 ...
45
-Connected to 74.125.224.41:443
46
-Negotiated protocol "h2-14"
47
-[FrameHeader SETTINGS len=18]
48
-  [MAX_CONCURRENT_STREAMS = 100]
49
-  [INITIAL_WINDOW_SIZE = 1048576]
50
-  [MAX_FRAME_SIZE = 16384]
51
-[FrameHeader WINDOW_UPDATE len=4]
52
-  Window-Increment = 983041
53
-  
54
-h2i> PING h2iSayHI
55
-[FrameHeader PING flags=ACK len=8]
56
-  Data = "h2iSayHI"
57
-h2i> headers
58
-(as HTTP/1.1)> GET / HTTP/1.1
59
-(as HTTP/1.1)> Host: ip.appspot.com
60
-(as HTTP/1.1)> User-Agent: h2i/brad-n-blake
61
-(as HTTP/1.1)>  
62
-Opening Stream-ID 1:
63
- :authority = ip.appspot.com
64
- :method = GET
65
- :path = /
66
- :scheme = https
67
- user-agent = h2i/brad-n-blake
68
-[FrameHeader HEADERS flags=END_HEADERS stream=1 len=77]
69
-  :status = "200"
70
-  alternate-protocol = "443:quic,p=1"
71
-  content-length = "15"
72
-  content-type = "text/html"
73
-  date = "Fri, 01 May 2015 23:06:56 GMT"
74
-  server = "Google Frontend"
75
-[FrameHeader DATA flags=END_STREAM stream=1 len=15]
76
-  "173.164.155.78\n"
77
-[FrameHeader PING len=8]
78
-  Data = "\x00\x00\x00\x00\x00\x00\x00\x00"
79
-h2i> ping  
80
-[FrameHeader PING flags=ACK len=8]  
81
-  Data = "h2i_ping"  
82
-h2i> ping  
83
-[FrameHeader PING flags=ACK len=8]
84
-  Data = "h2i_ping"
85
-h2i> ping
86
-[FrameHeader GOAWAY len=22]
87
-  Last-Stream-ID = 1; Error-Code = PROTOCOL_ERROR (1)
88
-
89
-ReadFrame: EOF
90
-```
91
-
92
-## Status
93
-
94
-Quick few hour hack. So much yet to do. Feel free to file issues for
95
-bugs or wishlist items, but [@bmizerany](https://github.com/bmizerany/)
96
-and I aren't yet accepting pull requests until things settle down.
97
-

+ 0
- 522
vendor/golang.org/x/net/http2/h2i/h2i.go View File

1
-// Copyright 2015 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
-// +build !plan9,!solaris
6
-
7
-/*
8
-The h2i command is an interactive HTTP/2 console.
9
-
10
-Usage:
11
-  $ h2i [flags] <hostname>
12
-
13
-Interactive commands in the console: (all parts case-insensitive)
14
-
15
-  ping [data]
16
-  settings ack
17
-  settings FOO=n BAR=z
18
-  headers      (open a new stream by typing HTTP/1.1)
19
-*/
20
-package main
21
-
22
-import (
23
-	"bufio"
24
-	"bytes"
25
-	"crypto/tls"
26
-	"errors"
27
-	"flag"
28
-	"fmt"
29
-	"io"
30
-	"log"
31
-	"net"
32
-	"net/http"
33
-	"os"
34
-	"regexp"
35
-	"strconv"
36
-	"strings"
37
-
38
-	"golang.org/x/crypto/ssh/terminal"
39
-	"golang.org/x/net/http2"
40
-	"golang.org/x/net/http2/hpack"
41
-)
42
-
43
-// Flags
44
-var (
45
-	flagNextProto = flag.String("nextproto", "h2,h2-14", "Comma-separated list of NPN/ALPN protocol names to negotiate.")
46
-	flagInsecure  = flag.Bool("insecure", false, "Whether to skip TLS cert validation")
47
-	flagSettings  = flag.String("settings", "empty", "comma-separated list of KEY=value settings for the initial SETTINGS frame. The magic value 'empty' sends an empty initial settings frame, and the magic value 'omit' causes no initial settings frame to be sent.")
48
-	flagDial      = flag.String("dial", "", "optional ip:port to dial, to connect to a host:port but use a different SNI name (including a SNI name without DNS)")
49
-)
50
-
51
-type command struct {
52
-	run func(*h2i, []string) error // required
53
-
54
-	// complete optionally specifies tokens (case-insensitive) which are
55
-	// valid for this subcommand.
56
-	complete func() []string
57
-}
58
-
59
-var commands = map[string]command{
60
-	"ping": {run: (*h2i).cmdPing},
61
-	"settings": {
62
-		run: (*h2i).cmdSettings,
63
-		complete: func() []string {
64
-			return []string{
65
-				"ACK",
66
-				http2.SettingHeaderTableSize.String(),
67
-				http2.SettingEnablePush.String(),
68
-				http2.SettingMaxConcurrentStreams.String(),
69
-				http2.SettingInitialWindowSize.String(),
70
-				http2.SettingMaxFrameSize.String(),
71
-				http2.SettingMaxHeaderListSize.String(),
72
-			}
73
-		},
74
-	},
75
-	"quit":    {run: (*h2i).cmdQuit},
76
-	"headers": {run: (*h2i).cmdHeaders},
77
-}
78
-
79
-func usage() {
80
-	fmt.Fprintf(os.Stderr, "Usage: h2i <hostname>\n\n")
81
-	flag.PrintDefaults()
82
-}
83
-
84
-// withPort adds ":443" if another port isn't already present.
85
-func withPort(host string) string {
86
-	if _, _, err := net.SplitHostPort(host); err != nil {
87
-		return net.JoinHostPort(host, "443")
88
-	}
89
-	return host
90
-}
91
-
92
-// withoutPort strips the port from addr if present.
93
-func withoutPort(addr string) string {
94
-	if h, _, err := net.SplitHostPort(addr); err == nil {
95
-		return h
96
-	}
97
-	return addr
98
-}
99
-
100
-// h2i is the app's state.
101
-type h2i struct {
102
-	host   string
103
-	tc     *tls.Conn
104
-	framer *http2.Framer
105
-	term   *terminal.Terminal
106
-
107
-	// owned by the command loop:
108
-	streamID uint32
109
-	hbuf     bytes.Buffer
110
-	henc     *hpack.Encoder
111
-
112
-	// owned by the readFrames loop:
113
-	peerSetting map[http2.SettingID]uint32
114
-	hdec        *hpack.Decoder
115
-}
116
-
117
-func main() {
118
-	flag.Usage = usage
119
-	flag.Parse()
120
-	if flag.NArg() != 1 {
121
-		usage()
122
-		os.Exit(2)
123
-	}
124
-	log.SetFlags(0)
125
-
126
-	host := flag.Arg(0)
127
-	app := &h2i{
128
-		host:        host,
129
-		peerSetting: make(map[http2.SettingID]uint32),
130
-	}
131
-	app.henc = hpack.NewEncoder(&app.hbuf)
132
-
133
-	if err := app.Main(); err != nil {
134
-		if app.term != nil {
135
-			app.logf("%v\n", err)
136
-		} else {
137
-			fmt.Fprintf(os.Stderr, "%v\n", err)
138
-		}
139
-		os.Exit(1)
140
-	}
141
-	fmt.Fprintf(os.Stdout, "\n")
142
-}
143
-
144
-func (app *h2i) Main() error {
145
-	cfg := &tls.Config{
146
-		ServerName:         withoutPort(app.host),
147
-		NextProtos:         strings.Split(*flagNextProto, ","),
148
-		InsecureSkipVerify: *flagInsecure,
149
-	}
150
-
151
-	hostAndPort := *flagDial
152
-	if hostAndPort == "" {
153
-		hostAndPort = withPort(app.host)
154
-	}
155
-	log.Printf("Connecting to %s ...", hostAndPort)
156
-	tc, err := tls.Dial("tcp", hostAndPort, cfg)
157
-	if err != nil {
158
-		return fmt.Errorf("Error dialing %s: %v", hostAndPort, err)
159
-	}
160
-	log.Printf("Connected to %v", tc.RemoteAddr())
161
-	defer tc.Close()
162
-
163
-	if err := tc.Handshake(); err != nil {
164
-		return fmt.Errorf("TLS handshake: %v", err)
165
-	}
166
-	if !*flagInsecure {
167
-		if err := tc.VerifyHostname(app.host); err != nil {
168
-			return fmt.Errorf("VerifyHostname: %v", err)
169
-		}
170
-	}
171
-	state := tc.ConnectionState()
172
-	log.Printf("Negotiated protocol %q", state.NegotiatedProtocol)
173
-	if !state.NegotiatedProtocolIsMutual || state.NegotiatedProtocol == "" {
174
-		return fmt.Errorf("Could not negotiate protocol mutually")
175
-	}
176
-
177
-	if _, err := io.WriteString(tc, http2.ClientPreface); err != nil {
178
-		return err
179
-	}
180
-
181
-	app.framer = http2.NewFramer(tc, tc)
182
-
183
-	oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
184
-	if err != nil {
185
-		return err
186
-	}
187
-	defer terminal.Restore(0, oldState)
188
-
189
-	var screen = struct {
190
-		io.Reader
191
-		io.Writer
192
-	}{os.Stdin, os.Stdout}
193
-
194
-	app.term = terminal.NewTerminal(screen, "h2i> ")
195
-	lastWord := regexp.MustCompile(`.+\W(\w+)$`)
196
-	app.term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
197
-		if key != '\t' {
198
-			return
199
-		}
200
-		if pos != len(line) {
201
-			// TODO: we're being lazy for now, only supporting tab completion at the end.
202
-			return
203
-		}
204
-		// Auto-complete for the command itself.
205
-		if !strings.Contains(line, " ") {
206
-			var name string
207
-			name, _, ok = lookupCommand(line)
208
-			if !ok {
209
-				return
210
-			}
211
-			return name, len(name), true
212
-		}
213
-		_, c, ok := lookupCommand(line[:strings.IndexByte(line, ' ')])
214
-		if !ok || c.complete == nil {
215
-			return
216
-		}
217
-		if strings.HasSuffix(line, " ") {
218
-			app.logf("%s", strings.Join(c.complete(), " "))
219
-			return line, pos, true
220
-		}
221
-		m := lastWord.FindStringSubmatch(line)
222
-		if m == nil {
223
-			return line, len(line), true
224
-		}
225
-		soFar := m[1]
226
-		var match []string
227
-		for _, cand := range c.complete() {
228
-			if len(soFar) > len(cand) || !strings.EqualFold(cand[:len(soFar)], soFar) {
229
-				continue
230
-			}
231
-			match = append(match, cand)
232
-		}
233
-		if len(match) == 0 {
234
-			return
235
-		}
236
-		if len(match) > 1 {
237
-			// TODO: auto-complete any common prefix
238
-			app.logf("%s", strings.Join(match, " "))
239
-			return line, pos, true
240
-		}
241
-		newLine = line[:len(line)-len(soFar)] + match[0]
242
-		return newLine, len(newLine), true
243
-
244
-	}
245
-
246
-	errc := make(chan error, 2)
247
-	go func() { errc <- app.readFrames() }()
248
-	go func() { errc <- app.readConsole() }()
249
-	return <-errc
250
-}
251
-
252
-func (app *h2i) logf(format string, args ...interface{}) {
253
-	fmt.Fprintf(app.term, format+"\r\n", args...)
254
-}
255
-
256
-func (app *h2i) readConsole() error {
257
-	if s := *flagSettings; s != "omit" {
258
-		var args []string
259
-		if s != "empty" {
260
-			args = strings.Split(s, ",")
261
-		}
262
-		_, c, ok := lookupCommand("settings")
263
-		if !ok {
264
-			panic("settings command not found")
265
-		}
266
-		c.run(app, args)
267
-	}
268
-
269
-	for {
270
-		line, err := app.term.ReadLine()
271
-		if err == io.EOF {
272
-			return nil
273
-		}
274
-		if err != nil {
275
-			return fmt.Errorf("terminal.ReadLine: %v", err)
276
-		}
277
-		f := strings.Fields(line)
278
-		if len(f) == 0 {
279
-			continue
280
-		}
281
-		cmd, args := f[0], f[1:]
282
-		if _, c, ok := lookupCommand(cmd); ok {
283
-			err = c.run(app, args)
284
-		} else {
285
-			app.logf("Unknown command %q", line)
286
-		}
287
-		if err == errExitApp {
288
-			return nil
289
-		}
290
-		if err != nil {
291
-			return err
292
-		}
293
-	}
294
-}
295
-
296
-func lookupCommand(prefix string) (name string, c command, ok bool) {
297
-	prefix = strings.ToLower(prefix)
298
-	if c, ok = commands[prefix]; ok {
299
-		return prefix, c, ok
300
-	}
301
-
302
-	for full, candidate := range commands {
303
-		if strings.HasPrefix(full, prefix) {
304
-			if c.run != nil {
305
-				return "", command{}, false // ambiguous
306
-			}
307
-			c = candidate
308
-			name = full
309
-		}
310
-	}
311
-	return name, c, c.run != nil
312
-}
313
-
314
-var errExitApp = errors.New("internal sentinel error value to quit the console reading loop")
315
-
316
-func (a *h2i) cmdQuit(args []string) error {
317
-	if len(args) > 0 {
318
-		a.logf("the QUIT command takes no argument")
319
-		return nil
320
-	}
321
-	return errExitApp
322
-}
323
-
324
-func (a *h2i) cmdSettings(args []string) error {
325
-	if len(args) == 1 && strings.EqualFold(args[0], "ACK") {
326
-		return a.framer.WriteSettingsAck()
327
-	}
328
-	var settings []http2.Setting
329
-	for _, arg := range args {
330
-		if strings.EqualFold(arg, "ACK") {
331
-			a.logf("Error: ACK must be only argument with the SETTINGS command")
332
-			return nil
333
-		}
334
-		eq := strings.Index(arg, "=")
335
-		if eq == -1 {
336
-			a.logf("Error: invalid argument %q (expected SETTING_NAME=nnnn)", arg)
337
-			return nil
338
-		}
339
-		sid, ok := settingByName(arg[:eq])
340
-		if !ok {
341
-			a.logf("Error: unknown setting name %q", arg[:eq])
342
-			return nil
343
-		}
344
-		val, err := strconv.ParseUint(arg[eq+1:], 10, 32)
345
-		if err != nil {
346
-			a.logf("Error: invalid argument %q (expected SETTING_NAME=nnnn)", arg)
347
-			return nil
348
-		}
349
-		settings = append(settings, http2.Setting{
350
-			ID:  sid,
351
-			Val: uint32(val),
352
-		})
353
-	}
354
-	a.logf("Sending: %v", settings)
355
-	return a.framer.WriteSettings(settings...)
356
-}
357
-
358
-func settingByName(name string) (http2.SettingID, bool) {
359
-	for _, sid := range [...]http2.SettingID{
360
-		http2.SettingHeaderTableSize,
361
-		http2.SettingEnablePush,
362
-		http2.SettingMaxConcurrentStreams,
363
-		http2.SettingInitialWindowSize,
364
-		http2.SettingMaxFrameSize,
365
-		http2.SettingMaxHeaderListSize,
366
-	} {
367
-		if strings.EqualFold(sid.String(), name) {
368
-			return sid, true
369
-		}
370
-	}
371
-	return 0, false
372
-}
373
-
374
-func (app *h2i) cmdPing(args []string) error {
375
-	if len(args) > 1 {
376
-		app.logf("invalid PING usage: only accepts 0 or 1 args")
377
-		return nil // nil means don't end the program
378
-	}
379
-	var data [8]byte
380
-	if len(args) == 1 {
381
-		copy(data[:], args[0])
382
-	} else {
383
-		copy(data[:], "h2i_ping")
384
-	}
385
-	return app.framer.WritePing(false, data)
386
-}
387
-
388
-func (app *h2i) cmdHeaders(args []string) error {
389
-	if len(args) > 0 {
390
-		app.logf("Error: HEADERS doesn't yet take arguments.")
391
-		// TODO: flags for restricting window size, to force CONTINUATION
392
-		// frames.
393
-		return nil
394
-	}
395
-	var h1req bytes.Buffer
396
-	app.term.SetPrompt("(as HTTP/1.1)> ")
397
-	defer app.term.SetPrompt("h2i> ")
398
-	for {
399
-		line, err := app.term.ReadLine()
400
-		if err != nil {
401
-			return err
402
-		}
403
-		h1req.WriteString(line)
404
-		h1req.WriteString("\r\n")
405
-		if line == "" {
406
-			break
407
-		}
408
-	}
409
-	req, err := http.ReadRequest(bufio.NewReader(&h1req))
410
-	if err != nil {
411
-		app.logf("Invalid HTTP/1.1 request: %v", err)
412
-		return nil
413
-	}
414
-	if app.streamID == 0 {
415
-		app.streamID = 1
416
-	} else {
417
-		app.streamID += 2
418
-	}
419
-	app.logf("Opening Stream-ID %d:", app.streamID)
420
-	hbf := app.encodeHeaders(req)
421
-	if len(hbf) > 16<<10 {
422
-		app.logf("TODO: h2i doesn't yet write CONTINUATION frames. Copy it from transport.go")
423
-		return nil
424
-	}
425
-	return app.framer.WriteHeaders(http2.HeadersFrameParam{
426
-		StreamID:      app.streamID,
427
-		BlockFragment: hbf,
428
-		EndStream:     req.Method == "GET" || req.Method == "HEAD", // good enough for now
429
-		EndHeaders:    true,                                        // for now
430
-	})
431
-}
432
-
433
-func (app *h2i) readFrames() error {
434
-	for {
435
-		f, err := app.framer.ReadFrame()
436
-		if err != nil {
437
-			return fmt.Errorf("ReadFrame: %v", err)
438
-		}
439
-		app.logf("%v", f)
440
-		switch f := f.(type) {
441
-		case *http2.PingFrame:
442
-			app.logf("  Data = %q", f.Data)
443
-		case *http2.SettingsFrame:
444
-			f.ForeachSetting(func(s http2.Setting) error {
445
-				app.logf("  %v", s)
446
-				app.peerSetting[s.ID] = s.Val
447
-				return nil
448
-			})
449
-		case *http2.WindowUpdateFrame:
450
-			app.logf("  Window-Increment = %v", f.Increment)
451
-		case *http2.GoAwayFrame:
452
-			app.logf("  Last-Stream-ID = %d; Error-Code = %v (%d)", f.LastStreamID, f.ErrCode, f.ErrCode)
453
-		case *http2.DataFrame:
454
-			app.logf("  %q", f.Data())
455
-		case *http2.HeadersFrame:
456
-			if f.HasPriority() {
457
-				app.logf("  PRIORITY = %v", f.Priority)
458
-			}
459
-			if app.hdec == nil {
460
-				// TODO: if the user uses h2i to send a SETTINGS frame advertising
461
-				// something larger, we'll need to respect SETTINGS_HEADER_TABLE_SIZE
462
-				// and stuff here instead of using the 4k default. But for now:
463
-				tableSize := uint32(4 << 10)
464
-				app.hdec = hpack.NewDecoder(tableSize, app.onNewHeaderField)
465
-			}
466
-			app.hdec.Write(f.HeaderBlockFragment())
467
-		case *http2.PushPromiseFrame:
468
-			if app.hdec == nil {
469
-				// TODO: if the user uses h2i to send a SETTINGS frame advertising
470
-				// something larger, we'll need to respect SETTINGS_HEADER_TABLE_SIZE
471
-				// and stuff here instead of using the 4k default. But for now:
472
-				tableSize := uint32(4 << 10)
473
-				app.hdec = hpack.NewDecoder(tableSize, app.onNewHeaderField)
474
-			}
475
-			app.hdec.Write(f.HeaderBlockFragment())
476
-		}
477
-	}
478
-}
479
-
480
-// called from readLoop
481
-func (app *h2i) onNewHeaderField(f hpack.HeaderField) {
482
-	if f.Sensitive {
483
-		app.logf("  %s = %q (SENSITIVE)", f.Name, f.Value)
484
-	}
485
-	app.logf("  %s = %q", f.Name, f.Value)
486
-}
487
-
488
-func (app *h2i) encodeHeaders(req *http.Request) []byte {
489
-	app.hbuf.Reset()
490
-
491
-	// TODO(bradfitz): figure out :authority-vs-Host stuff between http2 and Go
492
-	host := req.Host
493
-	if host == "" {
494
-		host = req.URL.Host
495
-	}
496
-
497
-	path := req.RequestURI
498
-	if path == "" {
499
-		path = "/"
500
-	}
501
-
502
-	app.writeHeader(":authority", host) // probably not right for all sites
503
-	app.writeHeader(":method", req.Method)
504
-	app.writeHeader(":path", path)
505
-	app.writeHeader(":scheme", "https")
506
-
507
-	for k, vv := range req.Header {
508
-		lowKey := strings.ToLower(k)
509
-		if lowKey == "host" {
510
-			continue
511
-		}
512
-		for _, v := range vv {
513
-			app.writeHeader(lowKey, v)
514
-		}
515
-	}
516
-	return app.hbuf.Bytes()
517
-}
518
-
519
-func (app *h2i) writeHeader(name, value string) {
520
-	app.henc.WriteField(hpack.HeaderField{Name: name, Value: value})
521
-	app.logf(" %s = %s", name, value)
522
-}

+ 0
- 78
vendor/golang.org/x/net/http2/headermap.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"net/http"
9
-	"strings"
10
-)
11
-
12
-var (
13
-	commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case
14
-	commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case
15
-)
16
-
17
-func init() {
18
-	for _, v := range []string{
19
-		"accept",
20
-		"accept-charset",
21
-		"accept-encoding",
22
-		"accept-language",
23
-		"accept-ranges",
24
-		"age",
25
-		"access-control-allow-origin",
26
-		"allow",
27
-		"authorization",
28
-		"cache-control",
29
-		"content-disposition",
30
-		"content-encoding",
31
-		"content-language",
32
-		"content-length",
33
-		"content-location",
34
-		"content-range",
35
-		"content-type",
36
-		"cookie",
37
-		"date",
38
-		"etag",
39
-		"expect",
40
-		"expires",
41
-		"from",
42
-		"host",
43
-		"if-match",
44
-		"if-modified-since",
45
-		"if-none-match",
46
-		"if-unmodified-since",
47
-		"last-modified",
48
-		"link",
49
-		"location",
50
-		"max-forwards",
51
-		"proxy-authenticate",
52
-		"proxy-authorization",
53
-		"range",
54
-		"referer",
55
-		"refresh",
56
-		"retry-after",
57
-		"server",
58
-		"set-cookie",
59
-		"strict-transport-security",
60
-		"trailer",
61
-		"transfer-encoding",
62
-		"user-agent",
63
-		"vary",
64
-		"via",
65
-		"www-authenticate",
66
-	} {
67
-		chk := http.CanonicalHeaderKey(v)
68
-		commonLowerHeader[chk] = v
69
-		commonCanonHeader[v] = chk
70
-	}
71
-}
72
-
73
-func lowerHeader(v string) string {
74
-	if s, ok := commonLowerHeader[v]; ok {
75
-		return s
76
-	}
77
-	return strings.ToLower(v)
78
-}

+ 0
- 240
vendor/golang.org/x/net/http2/hpack/encode.go View File

1
-// Copyright 2014 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 hpack
6
-
7
-import (
8
-	"io"
9
-)
10
-
11
-const (
12
-	uint32Max              = ^uint32(0)
13
-	initialHeaderTableSize = 4096
14
-)
15
-
16
-type Encoder struct {
17
-	dynTab dynamicTable
18
-	// minSize is the minimum table size set by
19
-	// SetMaxDynamicTableSize after the previous Header Table Size
20
-	// Update.
21
-	minSize uint32
22
-	// maxSizeLimit is the maximum table size this encoder
23
-	// supports. This will protect the encoder from too large
24
-	// size.
25
-	maxSizeLimit uint32
26
-	// tableSizeUpdate indicates whether "Header Table Size
27
-	// Update" is required.
28
-	tableSizeUpdate bool
29
-	w               io.Writer
30
-	buf             []byte
31
-}
32
-
33
-// NewEncoder returns a new Encoder which performs HPACK encoding. An
34
-// encoded data is written to w.
35
-func NewEncoder(w io.Writer) *Encoder {
36
-	e := &Encoder{
37
-		minSize:         uint32Max,
38
-		maxSizeLimit:    initialHeaderTableSize,
39
-		tableSizeUpdate: false,
40
-		w:               w,
41
-	}
42
-	e.dynTab.table.init()
43
-	e.dynTab.setMaxSize(initialHeaderTableSize)
44
-	return e
45
-}
46
-
47
-// WriteField encodes f into a single Write to e's underlying Writer.
48
-// This function may also produce bytes for "Header Table Size Update"
49
-// if necessary. If produced, it is done before encoding f.
50
-func (e *Encoder) WriteField(f HeaderField) error {
51
-	e.buf = e.buf[:0]
52
-
53
-	if e.tableSizeUpdate {
54
-		e.tableSizeUpdate = false
55
-		if e.minSize < e.dynTab.maxSize {
56
-			e.buf = appendTableSize(e.buf, e.minSize)
57
-		}
58
-		e.minSize = uint32Max
59
-		e.buf = appendTableSize(e.buf, e.dynTab.maxSize)
60
-	}
61
-
62
-	idx, nameValueMatch := e.searchTable(f)
63
-	if nameValueMatch {
64
-		e.buf = appendIndexed(e.buf, idx)
65
-	} else {
66
-		indexing := e.shouldIndex(f)
67
-		if indexing {
68
-			e.dynTab.add(f)
69
-		}
70
-
71
-		if idx == 0 {
72
-			e.buf = appendNewName(e.buf, f, indexing)
73
-		} else {
74
-			e.buf = appendIndexedName(e.buf, f, idx, indexing)
75
-		}
76
-	}
77
-	n, err := e.w.Write(e.buf)
78
-	if err == nil && n != len(e.buf) {
79
-		err = io.ErrShortWrite
80
-	}
81
-	return err
82
-}
83
-
84
-// searchTable searches f in both stable and dynamic header tables.
85
-// The static header table is searched first. Only when there is no
86
-// exact match for both name and value, the dynamic header table is
87
-// then searched. If there is no match, i is 0. If both name and value
88
-// match, i is the matched index and nameValueMatch becomes true. If
89
-// only name matches, i points to that index and nameValueMatch
90
-// becomes false.
91
-func (e *Encoder) searchTable(f HeaderField) (i uint64, nameValueMatch bool) {
92
-	i, nameValueMatch = staticTable.search(f)
93
-	if nameValueMatch {
94
-		return i, true
95
-	}
96
-
97
-	j, nameValueMatch := e.dynTab.table.search(f)
98
-	if nameValueMatch || (i == 0 && j != 0) {
99
-		return j + uint64(staticTable.len()), nameValueMatch
100
-	}
101
-
102
-	return i, false
103
-}
104
-
105
-// SetMaxDynamicTableSize changes the dynamic header table size to v.
106
-// The actual size is bounded by the value passed to
107
-// SetMaxDynamicTableSizeLimit.
108
-func (e *Encoder) SetMaxDynamicTableSize(v uint32) {
109
-	if v > e.maxSizeLimit {
110
-		v = e.maxSizeLimit
111
-	}
112
-	if v < e.minSize {
113
-		e.minSize = v
114
-	}
115
-	e.tableSizeUpdate = true
116
-	e.dynTab.setMaxSize(v)
117
-}
118
-
119
-// SetMaxDynamicTableSizeLimit changes the maximum value that can be
120
-// specified in SetMaxDynamicTableSize to v. By default, it is set to
121
-// 4096, which is the same size of the default dynamic header table
122
-// size described in HPACK specification. If the current maximum
123
-// dynamic header table size is strictly greater than v, "Header Table
124
-// Size Update" will be done in the next WriteField call and the
125
-// maximum dynamic header table size is truncated to v.
126
-func (e *Encoder) SetMaxDynamicTableSizeLimit(v uint32) {
127
-	e.maxSizeLimit = v
128
-	if e.dynTab.maxSize > v {
129
-		e.tableSizeUpdate = true
130
-		e.dynTab.setMaxSize(v)
131
-	}
132
-}
133
-
134
-// shouldIndex reports whether f should be indexed.
135
-func (e *Encoder) shouldIndex(f HeaderField) bool {
136
-	return !f.Sensitive && f.Size() <= e.dynTab.maxSize
137
-}
138
-
139
-// appendIndexed appends index i, as encoded in "Indexed Header Field"
140
-// representation, to dst and returns the extended buffer.
141
-func appendIndexed(dst []byte, i uint64) []byte {
142
-	first := len(dst)
143
-	dst = appendVarInt(dst, 7, i)
144
-	dst[first] |= 0x80
145
-	return dst
146
-}
147
-
148
-// appendNewName appends f, as encoded in one of "Literal Header field
149
-// - New Name" representation variants, to dst and returns the
150
-// extended buffer.
151
-//
152
-// If f.Sensitive is true, "Never Indexed" representation is used. If
153
-// f.Sensitive is false and indexing is true, "Inremental Indexing"
154
-// representation is used.
155
-func appendNewName(dst []byte, f HeaderField, indexing bool) []byte {
156
-	dst = append(dst, encodeTypeByte(indexing, f.Sensitive))
157
-	dst = appendHpackString(dst, f.Name)
158
-	return appendHpackString(dst, f.Value)
159
-}
160
-
161
-// appendIndexedName appends f and index i referring indexed name
162
-// entry, as encoded in one of "Literal Header field - Indexed Name"
163
-// representation variants, to dst and returns the extended buffer.
164
-//
165
-// If f.Sensitive is true, "Never Indexed" representation is used. If
166
-// f.Sensitive is false and indexing is true, "Incremental Indexing"
167
-// representation is used.
168
-func appendIndexedName(dst []byte, f HeaderField, i uint64, indexing bool) []byte {
169
-	first := len(dst)
170
-	var n byte
171
-	if indexing {
172
-		n = 6
173
-	} else {
174
-		n = 4
175
-	}
176
-	dst = appendVarInt(dst, n, i)
177
-	dst[first] |= encodeTypeByte(indexing, f.Sensitive)
178
-	return appendHpackString(dst, f.Value)
179
-}
180
-
181
-// appendTableSize appends v, as encoded in "Header Table Size Update"
182
-// representation, to dst and returns the extended buffer.
183
-func appendTableSize(dst []byte, v uint32) []byte {
184
-	first := len(dst)
185
-	dst = appendVarInt(dst, 5, uint64(v))
186
-	dst[first] |= 0x20
187
-	return dst
188
-}
189
-
190
-// appendVarInt appends i, as encoded in variable integer form using n
191
-// bit prefix, to dst and returns the extended buffer.
192
-//
193
-// See
194
-// http://http2.github.io/http2-spec/compression.html#integer.representation
195
-func appendVarInt(dst []byte, n byte, i uint64) []byte {
196
-	k := uint64((1 << n) - 1)
197
-	if i < k {
198
-		return append(dst, byte(i))
199
-	}
200
-	dst = append(dst, byte(k))
201
-	i -= k
202
-	for ; i >= 128; i >>= 7 {
203
-		dst = append(dst, byte(0x80|(i&0x7f)))
204
-	}
205
-	return append(dst, byte(i))
206
-}
207
-
208
-// appendHpackString appends s, as encoded in "String Literal"
209
-// representation, to dst and returns the the extended buffer.
210
-//
211
-// s will be encoded in Huffman codes only when it produces strictly
212
-// shorter byte string.
213
-func appendHpackString(dst []byte, s string) []byte {
214
-	huffmanLength := HuffmanEncodeLength(s)
215
-	if huffmanLength < uint64(len(s)) {
216
-		first := len(dst)
217
-		dst = appendVarInt(dst, 7, huffmanLength)
218
-		dst = AppendHuffmanString(dst, s)
219
-		dst[first] |= 0x80
220
-	} else {
221
-		dst = appendVarInt(dst, 7, uint64(len(s)))
222
-		dst = append(dst, s...)
223
-	}
224
-	return dst
225
-}
226
-
227
-// encodeTypeByte returns type byte. If sensitive is true, type byte
228
-// for "Never Indexed" representation is returned. If sensitive is
229
-// false and indexing is true, type byte for "Incremental Indexing"
230
-// representation is returned. Otherwise, type byte for "Without
231
-// Indexing" is returned.
232
-func encodeTypeByte(indexing, sensitive bool) byte {
233
-	if sensitive {
234
-		return 0x10
235
-	}
236
-	if indexing {
237
-		return 0x40
238
-	}
239
-	return 0
240
-}

+ 0
- 386
vendor/golang.org/x/net/http2/hpack/encode_test.go View File

1
-// Copyright 2014 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 hpack
6
-
7
-import (
8
-	"bytes"
9
-	"encoding/hex"
10
-	"fmt"
11
-	"math/rand"
12
-	"reflect"
13
-	"strings"
14
-	"testing"
15
-)
16
-
17
-func TestEncoderTableSizeUpdate(t *testing.T) {
18
-	tests := []struct {
19
-		size1, size2 uint32
20
-		wantHex      string
21
-	}{
22
-		// Should emit 2 table size updates (2048 and 4096)
23
-		{2048, 4096, "3fe10f 3fe11f 82"},
24
-
25
-		// Should emit 1 table size update (2048)
26
-		{16384, 2048, "3fe10f 82"},
27
-	}
28
-	for _, tt := range tests {
29
-		var buf bytes.Buffer
30
-		e := NewEncoder(&buf)
31
-		e.SetMaxDynamicTableSize(tt.size1)
32
-		e.SetMaxDynamicTableSize(tt.size2)
33
-		if err := e.WriteField(pair(":method", "GET")); err != nil {
34
-			t.Fatal(err)
35
-		}
36
-		want := removeSpace(tt.wantHex)
37
-		if got := hex.EncodeToString(buf.Bytes()); got != want {
38
-			t.Errorf("e.SetDynamicTableSize %v, %v = %q; want %q", tt.size1, tt.size2, got, want)
39
-		}
40
-	}
41
-}
42
-
43
-func TestEncoderWriteField(t *testing.T) {
44
-	var buf bytes.Buffer
45
-	e := NewEncoder(&buf)
46
-	var got []HeaderField
47
-	d := NewDecoder(4<<10, func(f HeaderField) {
48
-		got = append(got, f)
49
-	})
50
-
51
-	tests := []struct {
52
-		hdrs []HeaderField
53
-	}{
54
-		{[]HeaderField{
55
-			pair(":method", "GET"),
56
-			pair(":scheme", "http"),
57
-			pair(":path", "/"),
58
-			pair(":authority", "www.example.com"),
59
-		}},
60
-		{[]HeaderField{
61
-			pair(":method", "GET"),
62
-			pair(":scheme", "http"),
63
-			pair(":path", "/"),
64
-			pair(":authority", "www.example.com"),
65
-			pair("cache-control", "no-cache"),
66
-		}},
67
-		{[]HeaderField{
68
-			pair(":method", "GET"),
69
-			pair(":scheme", "https"),
70
-			pair(":path", "/index.html"),
71
-			pair(":authority", "www.example.com"),
72
-			pair("custom-key", "custom-value"),
73
-		}},
74
-	}
75
-	for i, tt := range tests {
76
-		buf.Reset()
77
-		got = got[:0]
78
-		for _, hf := range tt.hdrs {
79
-			if err := e.WriteField(hf); err != nil {
80
-				t.Fatal(err)
81
-			}
82
-		}
83
-		_, err := d.Write(buf.Bytes())
84
-		if err != nil {
85
-			t.Errorf("%d. Decoder Write = %v", i, err)
86
-		}
87
-		if !reflect.DeepEqual(got, tt.hdrs) {
88
-			t.Errorf("%d. Decoded %+v; want %+v", i, got, tt.hdrs)
89
-		}
90
-	}
91
-}
92
-
93
-func TestEncoderSearchTable(t *testing.T) {
94
-	e := NewEncoder(nil)
95
-
96
-	e.dynTab.add(pair("foo", "bar"))
97
-	e.dynTab.add(pair("blake", "miz"))
98
-	e.dynTab.add(pair(":method", "GET"))
99
-
100
-	tests := []struct {
101
-		hf        HeaderField
102
-		wantI     uint64
103
-		wantMatch bool
104
-	}{
105
-		// Name and Value match
106
-		{pair("foo", "bar"), uint64(staticTable.len()) + 3, true},
107
-		{pair("blake", "miz"), uint64(staticTable.len()) + 2, true},
108
-		{pair(":method", "GET"), 2, true},
109
-
110
-		// Only name match because Sensitive == true. This is allowed to match
111
-		// any ":method" entry. The current implementation uses the last entry
112
-		// added in newStaticTable.
113
-		{HeaderField{":method", "GET", true}, 3, false},
114
-
115
-		// Only Name matches
116
-		{pair("foo", "..."), uint64(staticTable.len()) + 3, false},
117
-		{pair("blake", "..."), uint64(staticTable.len()) + 2, false},
118
-		// As before, this is allowed to match any ":method" entry.
119
-		{pair(":method", "..."), 3, false},
120
-
121
-		// None match
122
-		{pair("foo-", "bar"), 0, false},
123
-	}
124
-	for _, tt := range tests {
125
-		if gotI, gotMatch := e.searchTable(tt.hf); gotI != tt.wantI || gotMatch != tt.wantMatch {
126
-			t.Errorf("d.search(%+v) = %v, %v; want %v, %v", tt.hf, gotI, gotMatch, tt.wantI, tt.wantMatch)
127
-		}
128
-	}
129
-}
130
-
131
-func TestAppendVarInt(t *testing.T) {
132
-	tests := []struct {
133
-		n    byte
134
-		i    uint64
135
-		want []byte
136
-	}{
137
-		// Fits in a byte:
138
-		{1, 0, []byte{0}},
139
-		{2, 2, []byte{2}},
140
-		{3, 6, []byte{6}},
141
-		{4, 14, []byte{14}},
142
-		{5, 30, []byte{30}},
143
-		{6, 62, []byte{62}},
144
-		{7, 126, []byte{126}},
145
-		{8, 254, []byte{254}},
146
-
147
-		// Multiple bytes:
148
-		{5, 1337, []byte{31, 154, 10}},
149
-	}
150
-	for _, tt := range tests {
151
-		got := appendVarInt(nil, tt.n, tt.i)
152
-		if !bytes.Equal(got, tt.want) {
153
-			t.Errorf("appendVarInt(nil, %v, %v) = %v; want %v", tt.n, tt.i, got, tt.want)
154
-		}
155
-	}
156
-}
157
-
158
-func TestAppendHpackString(t *testing.T) {
159
-	tests := []struct {
160
-		s, wantHex string
161
-	}{
162
-		// Huffman encoded
163
-		{"www.example.com", "8c f1e3 c2e5 f23a 6ba0 ab90 f4ff"},
164
-
165
-		// Not Huffman encoded
166
-		{"a", "01 61"},
167
-
168
-		// zero length
169
-		{"", "00"},
170
-	}
171
-	for _, tt := range tests {
172
-		want := removeSpace(tt.wantHex)
173
-		buf := appendHpackString(nil, tt.s)
174
-		if got := hex.EncodeToString(buf); want != got {
175
-			t.Errorf("appendHpackString(nil, %q) = %q; want %q", tt.s, got, want)
176
-		}
177
-	}
178
-}
179
-
180
-func TestAppendIndexed(t *testing.T) {
181
-	tests := []struct {
182
-		i       uint64
183
-		wantHex string
184
-	}{
185
-		// 1 byte
186
-		{1, "81"},
187
-		{126, "fe"},
188
-
189
-		// 2 bytes
190
-		{127, "ff00"},
191
-		{128, "ff01"},
192
-	}
193
-	for _, tt := range tests {
194
-		want := removeSpace(tt.wantHex)
195
-		buf := appendIndexed(nil, tt.i)
196
-		if got := hex.EncodeToString(buf); want != got {
197
-			t.Errorf("appendIndex(nil, %v) = %q; want %q", tt.i, got, want)
198
-		}
199
-	}
200
-}
201
-
202
-func TestAppendNewName(t *testing.T) {
203
-	tests := []struct {
204
-		f        HeaderField
205
-		indexing bool
206
-		wantHex  string
207
-	}{
208
-		// Incremental indexing
209
-		{HeaderField{"custom-key", "custom-value", false}, true, "40 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
210
-
211
-		// Without indexing
212
-		{HeaderField{"custom-key", "custom-value", false}, false, "00 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
213
-
214
-		// Never indexed
215
-		{HeaderField{"custom-key", "custom-value", true}, true, "10 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
216
-		{HeaderField{"custom-key", "custom-value", true}, false, "10 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
217
-	}
218
-	for _, tt := range tests {
219
-		want := removeSpace(tt.wantHex)
220
-		buf := appendNewName(nil, tt.f, tt.indexing)
221
-		if got := hex.EncodeToString(buf); want != got {
222
-			t.Errorf("appendNewName(nil, %+v, %v) = %q; want %q", tt.f, tt.indexing, got, want)
223
-		}
224
-	}
225
-}
226
-
227
-func TestAppendIndexedName(t *testing.T) {
228
-	tests := []struct {
229
-		f        HeaderField
230
-		i        uint64
231
-		indexing bool
232
-		wantHex  string
233
-	}{
234
-		// Incremental indexing
235
-		{HeaderField{":status", "302", false}, 8, true, "48 82 6402"},
236
-
237
-		// Without indexing
238
-		{HeaderField{":status", "302", false}, 8, false, "08 82 6402"},
239
-
240
-		// Never indexed
241
-		{HeaderField{":status", "302", true}, 8, true, "18 82 6402"},
242
-		{HeaderField{":status", "302", true}, 8, false, "18 82 6402"},
243
-	}
244
-	for _, tt := range tests {
245
-		want := removeSpace(tt.wantHex)
246
-		buf := appendIndexedName(nil, tt.f, tt.i, tt.indexing)
247
-		if got := hex.EncodeToString(buf); want != got {
248
-			t.Errorf("appendIndexedName(nil, %+v, %v) = %q; want %q", tt.f, tt.indexing, got, want)
249
-		}
250
-	}
251
-}
252
-
253
-func TestAppendTableSize(t *testing.T) {
254
-	tests := []struct {
255
-		i       uint32
256
-		wantHex string
257
-	}{
258
-		// Fits into 1 byte
259
-		{30, "3e"},
260
-
261
-		// Extra byte
262
-		{31, "3f00"},
263
-		{32, "3f01"},
264
-	}
265
-	for _, tt := range tests {
266
-		want := removeSpace(tt.wantHex)
267
-		buf := appendTableSize(nil, tt.i)
268
-		if got := hex.EncodeToString(buf); want != got {
269
-			t.Errorf("appendTableSize(nil, %v) = %q; want %q", tt.i, got, want)
270
-		}
271
-	}
272
-}
273
-
274
-func TestEncoderSetMaxDynamicTableSize(t *testing.T) {
275
-	var buf bytes.Buffer
276
-	e := NewEncoder(&buf)
277
-	tests := []struct {
278
-		v           uint32
279
-		wantUpdate  bool
280
-		wantMinSize uint32
281
-		wantMaxSize uint32
282
-	}{
283
-		// Set new table size to 2048
284
-		{2048, true, 2048, 2048},
285
-
286
-		// Set new table size to 16384, but still limited to
287
-		// 4096
288
-		{16384, true, 2048, 4096},
289
-	}
290
-	for _, tt := range tests {
291
-		e.SetMaxDynamicTableSize(tt.v)
292
-		if got := e.tableSizeUpdate; tt.wantUpdate != got {
293
-			t.Errorf("e.tableSizeUpdate = %v; want %v", got, tt.wantUpdate)
294
-		}
295
-		if got := e.minSize; tt.wantMinSize != got {
296
-			t.Errorf("e.minSize = %v; want %v", got, tt.wantMinSize)
297
-		}
298
-		if got := e.dynTab.maxSize; tt.wantMaxSize != got {
299
-			t.Errorf("e.maxSize = %v; want %v", got, tt.wantMaxSize)
300
-		}
301
-	}
302
-}
303
-
304
-func TestEncoderSetMaxDynamicTableSizeLimit(t *testing.T) {
305
-	e := NewEncoder(nil)
306
-	// 4095 < initialHeaderTableSize means maxSize is truncated to
307
-	// 4095.
308
-	e.SetMaxDynamicTableSizeLimit(4095)
309
-	if got, want := e.dynTab.maxSize, uint32(4095); got != want {
310
-		t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
311
-	}
312
-	if got, want := e.maxSizeLimit, uint32(4095); got != want {
313
-		t.Errorf("e.maxSizeLimit = %v; want %v", got, want)
314
-	}
315
-	if got, want := e.tableSizeUpdate, true; got != want {
316
-		t.Errorf("e.tableSizeUpdate = %v; want %v", got, want)
317
-	}
318
-	// maxSize will be truncated to maxSizeLimit
319
-	e.SetMaxDynamicTableSize(16384)
320
-	if got, want := e.dynTab.maxSize, uint32(4095); got != want {
321
-		t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
322
-	}
323
-	// 8192 > current maxSizeLimit, so maxSize does not change.
324
-	e.SetMaxDynamicTableSizeLimit(8192)
325
-	if got, want := e.dynTab.maxSize, uint32(4095); got != want {
326
-		t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
327
-	}
328
-	if got, want := e.maxSizeLimit, uint32(8192); got != want {
329
-		t.Errorf("e.maxSizeLimit = %v; want %v", got, want)
330
-	}
331
-}
332
-
333
-func removeSpace(s string) string {
334
-	return strings.Replace(s, " ", "", -1)
335
-}
336
-
337
-func BenchmarkEncoderSearchTable(b *testing.B) {
338
-	e := NewEncoder(nil)
339
-
340
-	// A sample of possible header fields.
341
-	// This is not based on any actual data from HTTP/2 traces.
342
-	var possible []HeaderField
343
-	for _, f := range staticTable.ents {
344
-		if f.Value == "" {
345
-			possible = append(possible, f)
346
-			continue
347
-		}
348
-		// Generate 5 random values, except for cookie and set-cookie,
349
-		// which we know can have many values in practice.
350
-		num := 5
351
-		if f.Name == "cookie" || f.Name == "set-cookie" {
352
-			num = 25
353
-		}
354
-		for i := 0; i < num; i++ {
355
-			f.Value = fmt.Sprintf("%s-%d", f.Name, i)
356
-			possible = append(possible, f)
357
-		}
358
-	}
359
-	for k := 0; k < 10; k++ {
360
-		f := HeaderField{
361
-			Name:      fmt.Sprintf("x-header-%d", k),
362
-			Sensitive: rand.Int()%2 == 0,
363
-		}
364
-		for i := 0; i < 5; i++ {
365
-			f.Value = fmt.Sprintf("%s-%d", f.Name, i)
366
-			possible = append(possible, f)
367
-		}
368
-	}
369
-
370
-	// Add a random sample to the dynamic table. This very loosely simulates
371
-	// a history of 100 requests with 20 header fields per request.
372
-	for r := 0; r < 100*20; r++ {
373
-		f := possible[rand.Int31n(int32(len(possible)))]
374
-		// Skip if this is in the staticTable verbatim.
375
-		if _, has := staticTable.search(f); !has {
376
-			e.dynTab.add(f)
377
-		}
378
-	}
379
-
380
-	b.ResetTimer()
381
-	for n := 0; n < b.N; n++ {
382
-		for _, f := range possible {
383
-			e.searchTable(f)
384
-		}
385
-	}
386
-}

+ 0
- 490
vendor/golang.org/x/net/http2/hpack/hpack.go View File

1
-// Copyright 2014 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 hpack implements HPACK, a compression format for
6
-// efficiently representing HTTP header fields in the context of HTTP/2.
7
-//
8
-// See http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-09
9
-package hpack
10
-
11
-import (
12
-	"bytes"
13
-	"errors"
14
-	"fmt"
15
-)
16
-
17
-// A DecodingError is something the spec defines as a decoding error.
18
-type DecodingError struct {
19
-	Err error
20
-}
21
-
22
-func (de DecodingError) Error() string {
23
-	return fmt.Sprintf("decoding error: %v", de.Err)
24
-}
25
-
26
-// An InvalidIndexError is returned when an encoder references a table
27
-// entry before the static table or after the end of the dynamic table.
28
-type InvalidIndexError int
29
-
30
-func (e InvalidIndexError) Error() string {
31
-	return fmt.Sprintf("invalid indexed representation index %d", int(e))
32
-}
33
-
34
-// A HeaderField is a name-value pair. Both the name and value are
35
-// treated as opaque sequences of octets.
36
-type HeaderField struct {
37
-	Name, Value string
38
-
39
-	// Sensitive means that this header field should never be
40
-	// indexed.
41
-	Sensitive bool
42
-}
43
-
44
-// IsPseudo reports whether the header field is an http2 pseudo header.
45
-// That is, it reports whether it starts with a colon.
46
-// It is not otherwise guaranteed to be a valid pseudo header field,
47
-// though.
48
-func (hf HeaderField) IsPseudo() bool {
49
-	return len(hf.Name) != 0 && hf.Name[0] == ':'
50
-}
51
-
52
-func (hf HeaderField) String() string {
53
-	var suffix string
54
-	if hf.Sensitive {
55
-		suffix = " (sensitive)"
56
-	}
57
-	return fmt.Sprintf("header field %q = %q%s", hf.Name, hf.Value, suffix)
58
-}
59
-
60
-// Size returns the size of an entry per RFC 7541 section 4.1.
61
-func (hf HeaderField) Size() uint32 {
62
-	// http://http2.github.io/http2-spec/compression.html#rfc.section.4.1
63
-	// "The size of the dynamic table is the sum of the size of
64
-	// its entries. The size of an entry is the sum of its name's
65
-	// length in octets (as defined in Section 5.2), its value's
66
-	// length in octets (see Section 5.2), plus 32.  The size of
67
-	// an entry is calculated using the length of the name and
68
-	// value without any Huffman encoding applied."
69
-
70
-	// This can overflow if somebody makes a large HeaderField
71
-	// Name and/or Value by hand, but we don't care, because that
72
-	// won't happen on the wire because the encoding doesn't allow
73
-	// it.
74
-	return uint32(len(hf.Name) + len(hf.Value) + 32)
75
-}
76
-
77
-// A Decoder is the decoding context for incremental processing of
78
-// header blocks.
79
-type Decoder struct {
80
-	dynTab dynamicTable
81
-	emit   func(f HeaderField)
82
-
83
-	emitEnabled bool // whether calls to emit are enabled
84
-	maxStrLen   int  // 0 means unlimited
85
-
86
-	// buf is the unparsed buffer. It's only written to
87
-	// saveBuf if it was truncated in the middle of a header
88
-	// block. Because it's usually not owned, we can only
89
-	// process it under Write.
90
-	buf []byte // not owned; only valid during Write
91
-
92
-	// saveBuf is previous data passed to Write which we weren't able
93
-	// to fully parse before. Unlike buf, we own this data.
94
-	saveBuf bytes.Buffer
95
-}
96
-
97
-// NewDecoder returns a new decoder with the provided maximum dynamic
98
-// table size. The emitFunc will be called for each valid field
99
-// parsed, in the same goroutine as calls to Write, before Write returns.
100
-func NewDecoder(maxDynamicTableSize uint32, emitFunc func(f HeaderField)) *Decoder {
101
-	d := &Decoder{
102
-		emit:        emitFunc,
103
-		emitEnabled: true,
104
-	}
105
-	d.dynTab.table.init()
106
-	d.dynTab.allowedMaxSize = maxDynamicTableSize
107
-	d.dynTab.setMaxSize(maxDynamicTableSize)
108
-	return d
109
-}
110
-
111
-// ErrStringLength is returned by Decoder.Write when the max string length
112
-// (as configured by Decoder.SetMaxStringLength) would be violated.
113
-var ErrStringLength = errors.New("hpack: string too long")
114
-
115
-// SetMaxStringLength sets the maximum size of a HeaderField name or
116
-// value string. If a string exceeds this length (even after any
117
-// decompression), Write will return ErrStringLength.
118
-// A value of 0 means unlimited and is the default from NewDecoder.
119
-func (d *Decoder) SetMaxStringLength(n int) {
120
-	d.maxStrLen = n
121
-}
122
-
123
-// SetEmitFunc changes the callback used when new header fields
124
-// are decoded.
125
-// It must be non-nil. It does not affect EmitEnabled.
126
-func (d *Decoder) SetEmitFunc(emitFunc func(f HeaderField)) {
127
-	d.emit = emitFunc
128
-}
129
-
130
-// SetEmitEnabled controls whether the emitFunc provided to NewDecoder
131
-// should be called. The default is true.
132
-//
133
-// This facility exists to let servers enforce MAX_HEADER_LIST_SIZE
134
-// while still decoding and keeping in-sync with decoder state, but
135
-// without doing unnecessary decompression or generating unnecessary
136
-// garbage for header fields past the limit.
137
-func (d *Decoder) SetEmitEnabled(v bool) { d.emitEnabled = v }
138
-
139
-// EmitEnabled reports whether calls to the emitFunc provided to NewDecoder
140
-// are currently enabled. The default is true.
141
-func (d *Decoder) EmitEnabled() bool { return d.emitEnabled }
142
-
143
-// TODO: add method *Decoder.Reset(maxSize, emitFunc) to let callers re-use Decoders and their
144
-// underlying buffers for garbage reasons.
145
-
146
-func (d *Decoder) SetMaxDynamicTableSize(v uint32) {
147
-	d.dynTab.setMaxSize(v)
148
-}
149
-
150
-// SetAllowedMaxDynamicTableSize sets the upper bound that the encoded
151
-// stream (via dynamic table size updates) may set the maximum size
152
-// to.
153
-func (d *Decoder) SetAllowedMaxDynamicTableSize(v uint32) {
154
-	d.dynTab.allowedMaxSize = v
155
-}
156
-
157
-type dynamicTable struct {
158
-	// http://http2.github.io/http2-spec/compression.html#rfc.section.2.3.2
159
-	table          headerFieldTable
160
-	size           uint32 // in bytes
161
-	maxSize        uint32 // current maxSize
162
-	allowedMaxSize uint32 // maxSize may go up to this, inclusive
163
-}
164
-
165
-func (dt *dynamicTable) setMaxSize(v uint32) {
166
-	dt.maxSize = v
167
-	dt.evict()
168
-}
169
-
170
-func (dt *dynamicTable) add(f HeaderField) {
171
-	dt.table.addEntry(f)
172
-	dt.size += f.Size()
173
-	dt.evict()
174
-}
175
-
176
-// If we're too big, evict old stuff.
177
-func (dt *dynamicTable) evict() {
178
-	var n int
179
-	for dt.size > dt.maxSize && n < dt.table.len() {
180
-		dt.size -= dt.table.ents[n].Size()
181
-		n++
182
-	}
183
-	dt.table.evictOldest(n)
184
-}
185
-
186
-func (d *Decoder) maxTableIndex() int {
187
-	// This should never overflow. RFC 7540 Section 6.5.2 limits the size of
188
-	// the dynamic table to 2^32 bytes, where each entry will occupy more than
189
-	// one byte. Further, the staticTable has a fixed, small length.
190
-	return d.dynTab.table.len() + staticTable.len()
191
-}
192
-
193
-func (d *Decoder) at(i uint64) (hf HeaderField, ok bool) {
194
-	// See Section 2.3.3.
195
-	if i == 0 {
196
-		return
197
-	}
198
-	if i <= uint64(staticTable.len()) {
199
-		return staticTable.ents[i-1], true
200
-	}
201
-	if i > uint64(d.maxTableIndex()) {
202
-		return
203
-	}
204
-	// In the dynamic table, newer entries have lower indices.
205
-	// However, dt.ents[0] is the oldest entry. Hence, dt.ents is
206
-	// the reversed dynamic table.
207
-	dt := d.dynTab.table
208
-	return dt.ents[dt.len()-(int(i)-staticTable.len())], true
209
-}
210
-
211
-// Decode decodes an entire block.
212
-//
213
-// TODO: remove this method and make it incremental later? This is
214
-// easier for debugging now.
215
-func (d *Decoder) DecodeFull(p []byte) ([]HeaderField, error) {
216
-	var hf []HeaderField
217
-	saveFunc := d.emit
218
-	defer func() { d.emit = saveFunc }()
219
-	d.emit = func(f HeaderField) { hf = append(hf, f) }
220
-	if _, err := d.Write(p); err != nil {
221
-		return nil, err
222
-	}
223
-	if err := d.Close(); err != nil {
224
-		return nil, err
225
-	}
226
-	return hf, nil
227
-}
228
-
229
-func (d *Decoder) Close() error {
230
-	if d.saveBuf.Len() > 0 {
231
-		d.saveBuf.Reset()
232
-		return DecodingError{errors.New("truncated headers")}
233
-	}
234
-	return nil
235
-}
236
-
237
-func (d *Decoder) Write(p []byte) (n int, err error) {
238
-	if len(p) == 0 {
239
-		// Prevent state machine CPU attacks (making us redo
240
-		// work up to the point of finding out we don't have
241
-		// enough data)
242
-		return
243
-	}
244
-	// Only copy the data if we have to. Optimistically assume
245
-	// that p will contain a complete header block.
246
-	if d.saveBuf.Len() == 0 {
247
-		d.buf = p
248
-	} else {
249
-		d.saveBuf.Write(p)
250
-		d.buf = d.saveBuf.Bytes()
251
-		d.saveBuf.Reset()
252
-	}
253
-
254
-	for len(d.buf) > 0 {
255
-		err = d.parseHeaderFieldRepr()
256
-		if err == errNeedMore {
257
-			// Extra paranoia, making sure saveBuf won't
258
-			// get too large. All the varint and string
259
-			// reading code earlier should already catch
260
-			// overlong things and return ErrStringLength,
261
-			// but keep this as a last resort.
262
-			const varIntOverhead = 8 // conservative
263
-			if d.maxStrLen != 0 && int64(len(d.buf)) > 2*(int64(d.maxStrLen)+varIntOverhead) {
264
-				return 0, ErrStringLength
265
-			}
266
-			d.saveBuf.Write(d.buf)
267
-			return len(p), nil
268
-		}
269
-		if err != nil {
270
-			break
271
-		}
272
-	}
273
-	return len(p), err
274
-}
275
-
276
-// errNeedMore is an internal sentinel error value that means the
277
-// buffer is truncated and we need to read more data before we can
278
-// continue parsing.
279
-var errNeedMore = errors.New("need more data")
280
-
281
-type indexType int
282
-
283
-const (
284
-	indexedTrue indexType = iota
285
-	indexedFalse
286
-	indexedNever
287
-)
288
-
289
-func (v indexType) indexed() bool   { return v == indexedTrue }
290
-func (v indexType) sensitive() bool { return v == indexedNever }
291
-
292
-// returns errNeedMore if there isn't enough data available.
293
-// any other error is fatal.
294
-// consumes d.buf iff it returns nil.
295
-// precondition: must be called with len(d.buf) > 0
296
-func (d *Decoder) parseHeaderFieldRepr() error {
297
-	b := d.buf[0]
298
-	switch {
299
-	case b&128 != 0:
300
-		// Indexed representation.
301
-		// High bit set?
302
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.6.1
303
-		return d.parseFieldIndexed()
304
-	case b&192 == 64:
305
-		// 6.2.1 Literal Header Field with Incremental Indexing
306
-		// 0b10xxxxxx: top two bits are 10
307
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.1
308
-		return d.parseFieldLiteral(6, indexedTrue)
309
-	case b&240 == 0:
310
-		// 6.2.2 Literal Header Field without Indexing
311
-		// 0b0000xxxx: top four bits are 0000
312
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.2
313
-		return d.parseFieldLiteral(4, indexedFalse)
314
-	case b&240 == 16:
315
-		// 6.2.3 Literal Header Field never Indexed
316
-		// 0b0001xxxx: top four bits are 0001
317
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.6.2.3
318
-		return d.parseFieldLiteral(4, indexedNever)
319
-	case b&224 == 32:
320
-		// 6.3 Dynamic Table Size Update
321
-		// Top three bits are '001'.
322
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.6.3
323
-		return d.parseDynamicTableSizeUpdate()
324
-	}
325
-
326
-	return DecodingError{errors.New("invalid encoding")}
327
-}
328
-
329
-// (same invariants and behavior as parseHeaderFieldRepr)
330
-func (d *Decoder) parseFieldIndexed() error {
331
-	buf := d.buf
332
-	idx, buf, err := readVarInt(7, buf)
333
-	if err != nil {
334
-		return err
335
-	}
336
-	hf, ok := d.at(idx)
337
-	if !ok {
338
-		return DecodingError{InvalidIndexError(idx)}
339
-	}
340
-	d.buf = buf
341
-	return d.callEmit(HeaderField{Name: hf.Name, Value: hf.Value})
342
-}
343
-
344
-// (same invariants and behavior as parseHeaderFieldRepr)
345
-func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error {
346
-	buf := d.buf
347
-	nameIdx, buf, err := readVarInt(n, buf)
348
-	if err != nil {
349
-		return err
350
-	}
351
-
352
-	var hf HeaderField
353
-	wantStr := d.emitEnabled || it.indexed()
354
-	if nameIdx > 0 {
355
-		ihf, ok := d.at(nameIdx)
356
-		if !ok {
357
-			return DecodingError{InvalidIndexError(nameIdx)}
358
-		}
359
-		hf.Name = ihf.Name
360
-	} else {
361
-		hf.Name, buf, err = d.readString(buf, wantStr)
362
-		if err != nil {
363
-			return err
364
-		}
365
-	}
366
-	hf.Value, buf, err = d.readString(buf, wantStr)
367
-	if err != nil {
368
-		return err
369
-	}
370
-	d.buf = buf
371
-	if it.indexed() {
372
-		d.dynTab.add(hf)
373
-	}
374
-	hf.Sensitive = it.sensitive()
375
-	return d.callEmit(hf)
376
-}
377
-
378
-func (d *Decoder) callEmit(hf HeaderField) error {
379
-	if d.maxStrLen != 0 {
380
-		if len(hf.Name) > d.maxStrLen || len(hf.Value) > d.maxStrLen {
381
-			return ErrStringLength
382
-		}
383
-	}
384
-	if d.emitEnabled {
385
-		d.emit(hf)
386
-	}
387
-	return nil
388
-}
389
-
390
-// (same invariants and behavior as parseHeaderFieldRepr)
391
-func (d *Decoder) parseDynamicTableSizeUpdate() error {
392
-	buf := d.buf
393
-	size, buf, err := readVarInt(5, buf)
394
-	if err != nil {
395
-		return err
396
-	}
397
-	if size > uint64(d.dynTab.allowedMaxSize) {
398
-		return DecodingError{errors.New("dynamic table size update too large")}
399
-	}
400
-	d.dynTab.setMaxSize(uint32(size))
401
-	d.buf = buf
402
-	return nil
403
-}
404
-
405
-var errVarintOverflow = DecodingError{errors.New("varint integer overflow")}
406
-
407
-// readVarInt reads an unsigned variable length integer off the
408
-// beginning of p. n is the parameter as described in
409
-// http://http2.github.io/http2-spec/compression.html#rfc.section.5.1.
410
-//
411
-// n must always be between 1 and 8.
412
-//
413
-// The returned remain buffer is either a smaller suffix of p, or err != nil.
414
-// The error is errNeedMore if p doesn't contain a complete integer.
415
-func readVarInt(n byte, p []byte) (i uint64, remain []byte, err error) {
416
-	if n < 1 || n > 8 {
417
-		panic("bad n")
418
-	}
419
-	if len(p) == 0 {
420
-		return 0, p, errNeedMore
421
-	}
422
-	i = uint64(p[0])
423
-	if n < 8 {
424
-		i &= (1 << uint64(n)) - 1
425
-	}
426
-	if i < (1<<uint64(n))-1 {
427
-		return i, p[1:], nil
428
-	}
429
-
430
-	origP := p
431
-	p = p[1:]
432
-	var m uint64
433
-	for len(p) > 0 {
434
-		b := p[0]
435
-		p = p[1:]
436
-		i += uint64(b&127) << m
437
-		if b&128 == 0 {
438
-			return i, p, nil
439
-		}
440
-		m += 7
441
-		if m >= 63 { // TODO: proper overflow check. making this up.
442
-			return 0, origP, errVarintOverflow
443
-		}
444
-	}
445
-	return 0, origP, errNeedMore
446
-}
447
-
448
-// readString decodes an hpack string from p.
449
-//
450
-// wantStr is whether s will be used. If false, decompression and
451
-// []byte->string garbage are skipped if s will be ignored
452
-// anyway. This does mean that huffman decoding errors for non-indexed
453
-// strings past the MAX_HEADER_LIST_SIZE are ignored, but the server
454
-// is returning an error anyway, and because they're not indexed, the error
455
-// won't affect the decoding state.
456
-func (d *Decoder) readString(p []byte, wantStr bool) (s string, remain []byte, err error) {
457
-	if len(p) == 0 {
458
-		return "", p, errNeedMore
459
-	}
460
-	isHuff := p[0]&128 != 0
461
-	strLen, p, err := readVarInt(7, p)
462
-	if err != nil {
463
-		return "", p, err
464
-	}
465
-	if d.maxStrLen != 0 && strLen > uint64(d.maxStrLen) {
466
-		return "", nil, ErrStringLength
467
-	}
468
-	if uint64(len(p)) < strLen {
469
-		return "", p, errNeedMore
470
-	}
471
-	if !isHuff {
472
-		if wantStr {
473
-			s = string(p[:strLen])
474
-		}
475
-		return s, p[strLen:], nil
476
-	}
477
-
478
-	if wantStr {
479
-		buf := bufPool.Get().(*bytes.Buffer)
480
-		buf.Reset() // don't trust others
481
-		defer bufPool.Put(buf)
482
-		if err := huffmanDecode(buf, d.maxStrLen, p[:strLen]); err != nil {
483
-			buf.Reset()
484
-			return "", nil, err
485
-		}
486
-		s = buf.String()
487
-		buf.Reset() // be nice to GC
488
-	}
489
-	return s, p[strLen:], nil
490
-}

+ 0
- 722
vendor/golang.org/x/net/http2/hpack/hpack_test.go View File

1
-// Copyright 2014 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 hpack
6
-
7
-import (
8
-	"bytes"
9
-	"encoding/hex"
10
-	"fmt"
11
-	"math/rand"
12
-	"reflect"
13
-	"strings"
14
-	"testing"
15
-	"time"
16
-)
17
-
18
-func (d *Decoder) mustAt(idx int) HeaderField {
19
-	if hf, ok := d.at(uint64(idx)); !ok {
20
-		panic(fmt.Sprintf("bogus index %d", idx))
21
-	} else {
22
-		return hf
23
-	}
24
-}
25
-
26
-func TestDynamicTableAt(t *testing.T) {
27
-	d := NewDecoder(4096, nil)
28
-	at := d.mustAt
29
-	if got, want := at(2), (pair(":method", "GET")); got != want {
30
-		t.Errorf("at(2) = %v; want %v", got, want)
31
-	}
32
-	d.dynTab.add(pair("foo", "bar"))
33
-	d.dynTab.add(pair("blake", "miz"))
34
-	if got, want := at(staticTable.len()+1), (pair("blake", "miz")); got != want {
35
-		t.Errorf("at(dyn 1) = %v; want %v", got, want)
36
-	}
37
-	if got, want := at(staticTable.len()+2), (pair("foo", "bar")); got != want {
38
-		t.Errorf("at(dyn 2) = %v; want %v", got, want)
39
-	}
40
-	if got, want := at(3), (pair(":method", "POST")); got != want {
41
-		t.Errorf("at(3) = %v; want %v", got, want)
42
-	}
43
-}
44
-
45
-func TestDynamicTableSizeEvict(t *testing.T) {
46
-	d := NewDecoder(4096, nil)
47
-	if want := uint32(0); d.dynTab.size != want {
48
-		t.Fatalf("size = %d; want %d", d.dynTab.size, want)
49
-	}
50
-	add := d.dynTab.add
51
-	add(pair("blake", "eats pizza"))
52
-	if want := uint32(15 + 32); d.dynTab.size != want {
53
-		t.Fatalf("after pizza, size = %d; want %d", d.dynTab.size, want)
54
-	}
55
-	add(pair("foo", "bar"))
56
-	if want := uint32(15 + 32 + 6 + 32); d.dynTab.size != want {
57
-		t.Fatalf("after foo bar, size = %d; want %d", d.dynTab.size, want)
58
-	}
59
-	d.dynTab.setMaxSize(15 + 32 + 1 /* slop */)
60
-	if want := uint32(6 + 32); d.dynTab.size != want {
61
-		t.Fatalf("after setMaxSize, size = %d; want %d", d.dynTab.size, want)
62
-	}
63
-	if got, want := d.mustAt(staticTable.len()+1), (pair("foo", "bar")); got != want {
64
-		t.Errorf("at(dyn 1) = %v; want %v", got, want)
65
-	}
66
-	add(pair("long", strings.Repeat("x", 500)))
67
-	if want := uint32(0); d.dynTab.size != want {
68
-		t.Fatalf("after big one, size = %d; want %d", d.dynTab.size, want)
69
-	}
70
-}
71
-
72
-func TestDecoderDecode(t *testing.T) {
73
-	tests := []struct {
74
-		name       string
75
-		in         []byte
76
-		want       []HeaderField
77
-		wantDynTab []HeaderField // newest entry first
78
-	}{
79
-		// C.2.1 Literal Header Field with Indexing
80
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.1
81
-		{"C.2.1", dehex("400a 6375 7374 6f6d 2d6b 6579 0d63 7573 746f 6d2d 6865 6164 6572"),
82
-			[]HeaderField{pair("custom-key", "custom-header")},
83
-			[]HeaderField{pair("custom-key", "custom-header")},
84
-		},
85
-
86
-		// C.2.2 Literal Header Field without Indexing
87
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.2
88
-		{"C.2.2", dehex("040c 2f73 616d 706c 652f 7061 7468"),
89
-			[]HeaderField{pair(":path", "/sample/path")},
90
-			[]HeaderField{}},
91
-
92
-		// C.2.3 Literal Header Field never Indexed
93
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.3
94
-		{"C.2.3", dehex("1008 7061 7373 776f 7264 0673 6563 7265 74"),
95
-			[]HeaderField{{"password", "secret", true}},
96
-			[]HeaderField{}},
97
-
98
-		// C.2.4 Indexed Header Field
99
-		// http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.4
100
-		{"C.2.4", []byte("\x82"),
101
-			[]HeaderField{pair(":method", "GET")},
102
-			[]HeaderField{}},
103
-	}
104
-	for _, tt := range tests {
105
-		d := NewDecoder(4096, nil)
106
-		hf, err := d.DecodeFull(tt.in)
107
-		if err != nil {
108
-			t.Errorf("%s: %v", tt.name, err)
109
-			continue
110
-		}
111
-		if !reflect.DeepEqual(hf, tt.want) {
112
-			t.Errorf("%s: Got %v; want %v", tt.name, hf, tt.want)
113
-		}
114
-		gotDynTab := d.dynTab.reverseCopy()
115
-		if !reflect.DeepEqual(gotDynTab, tt.wantDynTab) {
116
-			t.Errorf("%s: dynamic table after = %v; want %v", tt.name, gotDynTab, tt.wantDynTab)
117
-		}
118
-	}
119
-}
120
-
121
-func (dt *dynamicTable) reverseCopy() (hf []HeaderField) {
122
-	hf = make([]HeaderField, len(dt.table.ents))
123
-	for i := range hf {
124
-		hf[i] = dt.table.ents[len(dt.table.ents)-1-i]
125
-	}
126
-	return
127
-}
128
-
129
-type encAndWant struct {
130
-	enc         []byte
131
-	want        []HeaderField
132
-	wantDynTab  []HeaderField
133
-	wantDynSize uint32
134
-}
135
-
136
-// C.3 Request Examples without Huffman Coding
137
-// http://http2.github.io/http2-spec/compression.html#rfc.section.C.3
138
-func TestDecodeC3_NoHuffman(t *testing.T) {
139
-	testDecodeSeries(t, 4096, []encAndWant{
140
-		{dehex("8286 8441 0f77 7777 2e65 7861 6d70 6c65 2e63 6f6d"),
141
-			[]HeaderField{
142
-				pair(":method", "GET"),
143
-				pair(":scheme", "http"),
144
-				pair(":path", "/"),
145
-				pair(":authority", "www.example.com"),
146
-			},
147
-			[]HeaderField{
148
-				pair(":authority", "www.example.com"),
149
-			},
150
-			57,
151
-		},
152
-		{dehex("8286 84be 5808 6e6f 2d63 6163 6865"),
153
-			[]HeaderField{
154
-				pair(":method", "GET"),
155
-				pair(":scheme", "http"),
156
-				pair(":path", "/"),
157
-				pair(":authority", "www.example.com"),
158
-				pair("cache-control", "no-cache"),
159
-			},
160
-			[]HeaderField{
161
-				pair("cache-control", "no-cache"),
162
-				pair(":authority", "www.example.com"),
163
-			},
164
-			110,
165
-		},
166
-		{dehex("8287 85bf 400a 6375 7374 6f6d 2d6b 6579 0c63 7573 746f 6d2d 7661 6c75 65"),
167
-			[]HeaderField{
168
-				pair(":method", "GET"),
169
-				pair(":scheme", "https"),
170
-				pair(":path", "/index.html"),
171
-				pair(":authority", "www.example.com"),
172
-				pair("custom-key", "custom-value"),
173
-			},
174
-			[]HeaderField{
175
-				pair("custom-key", "custom-value"),
176
-				pair("cache-control", "no-cache"),
177
-				pair(":authority", "www.example.com"),
178
-			},
179
-			164,
180
-		},
181
-	})
182
-}
183
-
184
-// C.4 Request Examples with Huffman Coding
185
-// http://http2.github.io/http2-spec/compression.html#rfc.section.C.4
186
-func TestDecodeC4_Huffman(t *testing.T) {
187
-	testDecodeSeries(t, 4096, []encAndWant{
188
-		{dehex("8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ff"),
189
-			[]HeaderField{
190
-				pair(":method", "GET"),
191
-				pair(":scheme", "http"),
192
-				pair(":path", "/"),
193
-				pair(":authority", "www.example.com"),
194
-			},
195
-			[]HeaderField{
196
-				pair(":authority", "www.example.com"),
197
-			},
198
-			57,
199
-		},
200
-		{dehex("8286 84be 5886 a8eb 1064 9cbf"),
201
-			[]HeaderField{
202
-				pair(":method", "GET"),
203
-				pair(":scheme", "http"),
204
-				pair(":path", "/"),
205
-				pair(":authority", "www.example.com"),
206
-				pair("cache-control", "no-cache"),
207
-			},
208
-			[]HeaderField{
209
-				pair("cache-control", "no-cache"),
210
-				pair(":authority", "www.example.com"),
211
-			},
212
-			110,
213
-		},
214
-		{dehex("8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925 a849 e95b b8e8 b4bf"),
215
-			[]HeaderField{
216
-				pair(":method", "GET"),
217
-				pair(":scheme", "https"),
218
-				pair(":path", "/index.html"),
219
-				pair(":authority", "www.example.com"),
220
-				pair("custom-key", "custom-value"),
221
-			},
222
-			[]HeaderField{
223
-				pair("custom-key", "custom-value"),
224
-				pair("cache-control", "no-cache"),
225
-				pair(":authority", "www.example.com"),
226
-			},
227
-			164,
228
-		},
229
-	})
230
-}
231
-
232
-// http://http2.github.io/http2-spec/compression.html#rfc.section.C.5
233
-// "This section shows several consecutive header lists, corresponding
234
-// to HTTP responses, on the same connection. The HTTP/2 setting
235
-// parameter SETTINGS_HEADER_TABLE_SIZE is set to the value of 256
236
-// octets, causing some evictions to occur."
237
-func TestDecodeC5_ResponsesNoHuff(t *testing.T) {
238
-	testDecodeSeries(t, 256, []encAndWant{
239
-		{dehex(`
240
-4803 3330 3258 0770 7269 7661 7465 611d
241
-4d6f 6e2c 2032 3120 4f63 7420 3230 3133
242
-2032 303a 3133 3a32 3120 474d 546e 1768
243
-7474 7073 3a2f 2f77 7777 2e65 7861 6d70
244
-6c65 2e63 6f6d
245
-`),
246
-			[]HeaderField{
247
-				pair(":status", "302"),
248
-				pair("cache-control", "private"),
249
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
250
-				pair("location", "https://www.example.com"),
251
-			},
252
-			[]HeaderField{
253
-				pair("location", "https://www.example.com"),
254
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
255
-				pair("cache-control", "private"),
256
-				pair(":status", "302"),
257
-			},
258
-			222,
259
-		},
260
-		{dehex("4803 3330 37c1 c0bf"),
261
-			[]HeaderField{
262
-				pair(":status", "307"),
263
-				pair("cache-control", "private"),
264
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
265
-				pair("location", "https://www.example.com"),
266
-			},
267
-			[]HeaderField{
268
-				pair(":status", "307"),
269
-				pair("location", "https://www.example.com"),
270
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
271
-				pair("cache-control", "private"),
272
-			},
273
-			222,
274
-		},
275
-		{dehex(`
276
-88c1 611d 4d6f 6e2c 2032 3120 4f63 7420
277
-3230 3133 2032 303a 3133 3a32 3220 474d
278
-54c0 5a04 677a 6970 7738 666f 6f3d 4153
279
-444a 4b48 514b 425a 584f 5157 454f 5049
280
-5541 5851 5745 4f49 553b 206d 6178 2d61
281
-6765 3d33 3630 303b 2076 6572 7369 6f6e
282
-3d31
283
-`),
284
-			[]HeaderField{
285
-				pair(":status", "200"),
286
-				pair("cache-control", "private"),
287
-				pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
288
-				pair("location", "https://www.example.com"),
289
-				pair("content-encoding", "gzip"),
290
-				pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
291
-			},
292
-			[]HeaderField{
293
-				pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
294
-				pair("content-encoding", "gzip"),
295
-				pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
296
-			},
297
-			215,
298
-		},
299
-	})
300
-}
301
-
302
-// http://http2.github.io/http2-spec/compression.html#rfc.section.C.6
303
-// "This section shows the same examples as the previous section, but
304
-// using Huffman encoding for the literal values. The HTTP/2 setting
305
-// parameter SETTINGS_HEADER_TABLE_SIZE is set to the value of 256
306
-// octets, causing some evictions to occur. The eviction mechanism
307
-// uses the length of the decoded literal values, so the same
308
-// evictions occurs as in the previous section."
309
-func TestDecodeC6_ResponsesHuffman(t *testing.T) {
310
-	testDecodeSeries(t, 256, []encAndWant{
311
-		{dehex(`
312
-4882 6402 5885 aec3 771a 4b61 96d0 7abe
313
-9410 54d4 44a8 2005 9504 0b81 66e0 82a6
314
-2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8
315
-e9ae 82ae 43d3
316
-`),
317
-			[]HeaderField{
318
-				pair(":status", "302"),
319
-				pair("cache-control", "private"),
320
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
321
-				pair("location", "https://www.example.com"),
322
-			},
323
-			[]HeaderField{
324
-				pair("location", "https://www.example.com"),
325
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
326
-				pair("cache-control", "private"),
327
-				pair(":status", "302"),
328
-			},
329
-			222,
330
-		},
331
-		{dehex("4883 640e ffc1 c0bf"),
332
-			[]HeaderField{
333
-				pair(":status", "307"),
334
-				pair("cache-control", "private"),
335
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
336
-				pair("location", "https://www.example.com"),
337
-			},
338
-			[]HeaderField{
339
-				pair(":status", "307"),
340
-				pair("location", "https://www.example.com"),
341
-				pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
342
-				pair("cache-control", "private"),
343
-			},
344
-			222,
345
-		},
346
-		{dehex(`
347
-88c1 6196 d07a be94 1054 d444 a820 0595
348
-040b 8166 e084 a62d 1bff c05a 839b d9ab
349
-77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b
350
-3960 d5af 2708 7f36 72c1 ab27 0fb5 291f
351
-9587 3160 65c0 03ed 4ee5 b106 3d50 07
352
-`),
353
-			[]HeaderField{
354
-				pair(":status", "200"),
355
-				pair("cache-control", "private"),
356
-				pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
357
-				pair("location", "https://www.example.com"),
358
-				pair("content-encoding", "gzip"),
359
-				pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
360
-			},
361
-			[]HeaderField{
362
-				pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
363
-				pair("content-encoding", "gzip"),
364
-				pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
365
-			},
366
-			215,
367
-		},
368
-	})
369
-}
370
-
371
-func testDecodeSeries(t *testing.T, size uint32, steps []encAndWant) {
372
-	d := NewDecoder(size, nil)
373
-	for i, step := range steps {
374
-		hf, err := d.DecodeFull(step.enc)
375
-		if err != nil {
376
-			t.Fatalf("Error at step index %d: %v", i, err)
377
-		}
378
-		if !reflect.DeepEqual(hf, step.want) {
379
-			t.Fatalf("At step index %d: Got headers %v; want %v", i, hf, step.want)
380
-		}
381
-		gotDynTab := d.dynTab.reverseCopy()
382
-		if !reflect.DeepEqual(gotDynTab, step.wantDynTab) {
383
-			t.Errorf("After step index %d, dynamic table = %v; want %v", i, gotDynTab, step.wantDynTab)
384
-		}
385
-		if d.dynTab.size != step.wantDynSize {
386
-			t.Errorf("After step index %d, dynamic table size = %v; want %v", i, d.dynTab.size, step.wantDynSize)
387
-		}
388
-	}
389
-}
390
-
391
-func TestHuffmanDecodeExcessPadding(t *testing.T) {
392
-	tests := [][]byte{
393
-		{0xff},                                   // Padding Exceeds 7 bits
394
-		{0x1f, 0xff},                             // {"a", 1 byte excess padding}
395
-		{0x1f, 0xff, 0xff},                       // {"a", 2 byte excess padding}
396
-		{0x1f, 0xff, 0xff, 0xff},                 // {"a", 3 byte excess padding}
397
-		{0xff, 0x9f, 0xff, 0xff, 0xff},           // {"a", 29 bit excess padding}
398
-		{'R', 0xbc, '0', 0xff, 0xff, 0xff, 0xff}, // Padding ends on partial symbol.
399
-	}
400
-	for i, in := range tests {
401
-		var buf bytes.Buffer
402
-		if _, err := HuffmanDecode(&buf, in); err != ErrInvalidHuffman {
403
-			t.Errorf("test-%d: decode(%q) = %v; want ErrInvalidHuffman", i, in, err)
404
-		}
405
-	}
406
-}
407
-
408
-func TestHuffmanDecodeEOS(t *testing.T) {
409
-	in := []byte{0xff, 0xff, 0xff, 0xff, 0xfc} // {EOS, "?"}
410
-	var buf bytes.Buffer
411
-	if _, err := HuffmanDecode(&buf, in); err != ErrInvalidHuffman {
412
-		t.Errorf("error = %v; want ErrInvalidHuffman", err)
413
-	}
414
-}
415
-
416
-func TestHuffmanDecodeMaxLengthOnTrailingByte(t *testing.T) {
417
-	in := []byte{0x00, 0x01} // {"0", "0", "0"}
418
-	var buf bytes.Buffer
419
-	if err := huffmanDecode(&buf, 2, in); err != ErrStringLength {
420
-		t.Errorf("error = %v; want ErrStringLength", err)
421
-	}
422
-}
423
-
424
-func TestHuffmanDecodeCorruptPadding(t *testing.T) {
425
-	in := []byte{0x00}
426
-	var buf bytes.Buffer
427
-	if _, err := HuffmanDecode(&buf, in); err != ErrInvalidHuffman {
428
-		t.Errorf("error = %v; want ErrInvalidHuffman", err)
429
-	}
430
-}
431
-
432
-func TestHuffmanDecode(t *testing.T) {
433
-	tests := []struct {
434
-		inHex, want string
435
-	}{
436
-		{"f1e3 c2e5 f23a 6ba0 ab90 f4ff", "www.example.com"},
437
-		{"a8eb 1064 9cbf", "no-cache"},
438
-		{"25a8 49e9 5ba9 7d7f", "custom-key"},
439
-		{"25a8 49e9 5bb8 e8b4 bf", "custom-value"},
440
-		{"6402", "302"},
441
-		{"aec3 771a 4b", "private"},
442
-		{"d07a be94 1054 d444 a820 0595 040b 8166 e082 a62d 1bff", "Mon, 21 Oct 2013 20:13:21 GMT"},
443
-		{"9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 d3", "https://www.example.com"},
444
-		{"9bd9 ab", "gzip"},
445
-		{"94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 3160 65c0 03ed 4ee5 b106 3d50 07",
446
-			"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
447
-	}
448
-	for i, tt := range tests {
449
-		var buf bytes.Buffer
450
-		in, err := hex.DecodeString(strings.Replace(tt.inHex, " ", "", -1))
451
-		if err != nil {
452
-			t.Errorf("%d. hex input error: %v", i, err)
453
-			continue
454
-		}
455
-		if _, err := HuffmanDecode(&buf, in); err != nil {
456
-			t.Errorf("%d. decode error: %v", i, err)
457
-			continue
458
-		}
459
-		if got := buf.String(); tt.want != got {
460
-			t.Errorf("%d. decode = %q; want %q", i, got, tt.want)
461
-		}
462
-	}
463
-}
464
-
465
-func TestAppendHuffmanString(t *testing.T) {
466
-	tests := []struct {
467
-		in, want string
468
-	}{
469
-		{"www.example.com", "f1e3 c2e5 f23a 6ba0 ab90 f4ff"},
470
-		{"no-cache", "a8eb 1064 9cbf"},
471
-		{"custom-key", "25a8 49e9 5ba9 7d7f"},
472
-		{"custom-value", "25a8 49e9 5bb8 e8b4 bf"},
473
-		{"302", "6402"},
474
-		{"private", "aec3 771a 4b"},
475
-		{"Mon, 21 Oct 2013 20:13:21 GMT", "d07a be94 1054 d444 a820 0595 040b 8166 e082 a62d 1bff"},
476
-		{"https://www.example.com", "9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 d3"},
477
-		{"gzip", "9bd9 ab"},
478
-		{"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
479
-			"94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 3160 65c0 03ed 4ee5 b106 3d50 07"},
480
-	}
481
-	for i, tt := range tests {
482
-		buf := []byte{}
483
-		want := strings.Replace(tt.want, " ", "", -1)
484
-		buf = AppendHuffmanString(buf, tt.in)
485
-		if got := hex.EncodeToString(buf); want != got {
486
-			t.Errorf("%d. encode = %q; want %q", i, got, want)
487
-		}
488
-	}
489
-}
490
-
491
-func TestHuffmanMaxStrLen(t *testing.T) {
492
-	const msg = "Some string"
493
-	huff := AppendHuffmanString(nil, msg)
494
-
495
-	testGood := func(max int) {
496
-		var out bytes.Buffer
497
-		if err := huffmanDecode(&out, max, huff); err != nil {
498
-			t.Errorf("For maxLen=%d, unexpected error: %v", max, err)
499
-		}
500
-		if out.String() != msg {
501
-			t.Errorf("For maxLen=%d, out = %q; want %q", max, out.String(), msg)
502
-		}
503
-	}
504
-	testGood(0)
505
-	testGood(len(msg))
506
-	testGood(len(msg) + 1)
507
-
508
-	var out bytes.Buffer
509
-	if err := huffmanDecode(&out, len(msg)-1, huff); err != ErrStringLength {
510
-		t.Errorf("err = %v; want ErrStringLength", err)
511
-	}
512
-}
513
-
514
-func TestHuffmanRoundtripStress(t *testing.T) {
515
-	const Len = 50 // of uncompressed string
516
-	input := make([]byte, Len)
517
-	var output bytes.Buffer
518
-	var huff []byte
519
-
520
-	n := 5000
521
-	if testing.Short() {
522
-		n = 100
523
-	}
524
-	seed := time.Now().UnixNano()
525
-	t.Logf("Seed = %v", seed)
526
-	src := rand.New(rand.NewSource(seed))
527
-	var encSize int64
528
-	for i := 0; i < n; i++ {
529
-		for l := range input {
530
-			input[l] = byte(src.Intn(256))
531
-		}
532
-		huff = AppendHuffmanString(huff[:0], string(input))
533
-		encSize += int64(len(huff))
534
-		output.Reset()
535
-		if err := huffmanDecode(&output, 0, huff); err != nil {
536
-			t.Errorf("Failed to decode %q -> %q -> error %v", input, huff, err)
537
-			continue
538
-		}
539
-		if !bytes.Equal(output.Bytes(), input) {
540
-			t.Errorf("Roundtrip failure on %q -> %q -> %q", input, huff, output.Bytes())
541
-		}
542
-	}
543
-	t.Logf("Compressed size of original: %0.02f%% (%v -> %v)", 100*(float64(encSize)/(Len*float64(n))), Len*n, encSize)
544
-}
545
-
546
-func TestHuffmanDecodeFuzz(t *testing.T) {
547
-	const Len = 50 // of compressed
548
-	var buf, zbuf bytes.Buffer
549
-
550
-	n := 5000
551
-	if testing.Short() {
552
-		n = 100
553
-	}
554
-	seed := time.Now().UnixNano()
555
-	t.Logf("Seed = %v", seed)
556
-	src := rand.New(rand.NewSource(seed))
557
-	numFail := 0
558
-	for i := 0; i < n; i++ {
559
-		zbuf.Reset()
560
-		if i == 0 {
561
-			// Start with at least one invalid one.
562
-			zbuf.WriteString("00\x91\xff\xff\xff\xff\xc8")
563
-		} else {
564
-			for l := 0; l < Len; l++ {
565
-				zbuf.WriteByte(byte(src.Intn(256)))
566
-			}
567
-		}
568
-
569
-		buf.Reset()
570
-		if err := huffmanDecode(&buf, 0, zbuf.Bytes()); err != nil {
571
-			if err == ErrInvalidHuffman {
572
-				numFail++
573
-				continue
574
-			}
575
-			t.Errorf("Failed to decode %q: %v", zbuf.Bytes(), err)
576
-			continue
577
-		}
578
-	}
579
-	t.Logf("%0.02f%% are invalid (%d / %d)", 100*float64(numFail)/float64(n), numFail, n)
580
-	if numFail < 1 {
581
-		t.Error("expected at least one invalid huffman encoding (test starts with one)")
582
-	}
583
-}
584
-
585
-func TestReadVarInt(t *testing.T) {
586
-	type res struct {
587
-		i        uint64
588
-		consumed int
589
-		err      error
590
-	}
591
-	tests := []struct {
592
-		n    byte
593
-		p    []byte
594
-		want res
595
-	}{
596
-		// Fits in a byte:
597
-		{1, []byte{0}, res{0, 1, nil}},
598
-		{2, []byte{2}, res{2, 1, nil}},
599
-		{3, []byte{6}, res{6, 1, nil}},
600
-		{4, []byte{14}, res{14, 1, nil}},
601
-		{5, []byte{30}, res{30, 1, nil}},
602
-		{6, []byte{62}, res{62, 1, nil}},
603
-		{7, []byte{126}, res{126, 1, nil}},
604
-		{8, []byte{254}, res{254, 1, nil}},
605
-
606
-		// Doesn't fit in a byte:
607
-		{1, []byte{1}, res{0, 0, errNeedMore}},
608
-		{2, []byte{3}, res{0, 0, errNeedMore}},
609
-		{3, []byte{7}, res{0, 0, errNeedMore}},
610
-		{4, []byte{15}, res{0, 0, errNeedMore}},
611
-		{5, []byte{31}, res{0, 0, errNeedMore}},
612
-		{6, []byte{63}, res{0, 0, errNeedMore}},
613
-		{7, []byte{127}, res{0, 0, errNeedMore}},
614
-		{8, []byte{255}, res{0, 0, errNeedMore}},
615
-
616
-		// Ignoring top bits:
617
-		{5, []byte{255, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 111
618
-		{5, []byte{159, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 100
619
-		{5, []byte{191, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 101
620
-
621
-		// Extra byte:
622
-		{5, []byte{191, 154, 10, 2}, res{1337, 3, nil}}, // extra byte
623
-
624
-		// Short a byte:
625
-		{5, []byte{191, 154}, res{0, 0, errNeedMore}},
626
-
627
-		// integer overflow:
628
-		{1, []byte{255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, res{0, 0, errVarintOverflow}},
629
-	}
630
-	for _, tt := range tests {
631
-		i, remain, err := readVarInt(tt.n, tt.p)
632
-		consumed := len(tt.p) - len(remain)
633
-		got := res{i, consumed, err}
634
-		if got != tt.want {
635
-			t.Errorf("readVarInt(%d, %v ~ %x) = %+v; want %+v", tt.n, tt.p, tt.p, got, tt.want)
636
-		}
637
-	}
638
-}
639
-
640
-// Fuzz crash, originally reported at https://github.com/bradfitz/http2/issues/56
641
-func TestHuffmanFuzzCrash(t *testing.T) {
642
-	got, err := HuffmanDecodeToString([]byte("00\x91\xff\xff\xff\xff\xc8"))
643
-	if got != "" {
644
-		t.Errorf("Got %q; want empty string", got)
645
-	}
646
-	if err != ErrInvalidHuffman {
647
-		t.Errorf("Err = %v; want ErrInvalidHuffman", err)
648
-	}
649
-}
650
-
651
-func pair(name, value string) HeaderField {
652
-	return HeaderField{Name: name, Value: value}
653
-}
654
-
655
-func dehex(s string) []byte {
656
-	s = strings.Replace(s, " ", "", -1)
657
-	s = strings.Replace(s, "\n", "", -1)
658
-	b, err := hex.DecodeString(s)
659
-	if err != nil {
660
-		panic(err)
661
-	}
662
-	return b
663
-}
664
-
665
-func TestEmitEnabled(t *testing.T) {
666
-	var buf bytes.Buffer
667
-	enc := NewEncoder(&buf)
668
-	enc.WriteField(HeaderField{Name: "foo", Value: "bar"})
669
-	enc.WriteField(HeaderField{Name: "foo", Value: "bar"})
670
-
671
-	numCallback := 0
672
-	var dec *Decoder
673
-	dec = NewDecoder(8<<20, func(HeaderField) {
674
-		numCallback++
675
-		dec.SetEmitEnabled(false)
676
-	})
677
-	if !dec.EmitEnabled() {
678
-		t.Errorf("initial emit enabled = false; want true")
679
-	}
680
-	if _, err := dec.Write(buf.Bytes()); err != nil {
681
-		t.Error(err)
682
-	}
683
-	if numCallback != 1 {
684
-		t.Errorf("num callbacks = %d; want 1", numCallback)
685
-	}
686
-	if dec.EmitEnabled() {
687
-		t.Errorf("emit enabled = true; want false")
688
-	}
689
-}
690
-
691
-func TestSaveBufLimit(t *testing.T) {
692
-	const maxStr = 1 << 10
693
-	var got []HeaderField
694
-	dec := NewDecoder(initialHeaderTableSize, func(hf HeaderField) {
695
-		got = append(got, hf)
696
-	})
697
-	dec.SetMaxStringLength(maxStr)
698
-	var frag []byte
699
-	frag = append(frag[:0], encodeTypeByte(false, false))
700
-	frag = appendVarInt(frag, 7, 3)
701
-	frag = append(frag, "foo"...)
702
-	frag = appendVarInt(frag, 7, 3)
703
-	frag = append(frag, "bar"...)
704
-
705
-	if _, err := dec.Write(frag); err != nil {
706
-		t.Fatal(err)
707
-	}
708
-
709
-	want := []HeaderField{{Name: "foo", Value: "bar"}}
710
-	if !reflect.DeepEqual(got, want) {
711
-		t.Errorf("After small writes, got %v; want %v", got, want)
712
-	}
713
-
714
-	frag = append(frag[:0], encodeTypeByte(false, false))
715
-	frag = appendVarInt(frag, 7, maxStr*3)
716
-	frag = append(frag, make([]byte, maxStr*3)...)
717
-
718
-	_, err := dec.Write(frag)
719
-	if err != ErrStringLength {
720
-		t.Fatalf("Write error = %v; want ErrStringLength", err)
721
-	}
722
-}

+ 0
- 212
vendor/golang.org/x/net/http2/hpack/huffman.go View File

1
-// Copyright 2014 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 hpack
6
-
7
-import (
8
-	"bytes"
9
-	"errors"
10
-	"io"
11
-	"sync"
12
-)
13
-
14
-var bufPool = sync.Pool{
15
-	New: func() interface{} { return new(bytes.Buffer) },
16
-}
17
-
18
-// HuffmanDecode decodes the string in v and writes the expanded
19
-// result to w, returning the number of bytes written to w and the
20
-// Write call's return value. At most one Write call is made.
21
-func HuffmanDecode(w io.Writer, v []byte) (int, error) {
22
-	buf := bufPool.Get().(*bytes.Buffer)
23
-	buf.Reset()
24
-	defer bufPool.Put(buf)
25
-	if err := huffmanDecode(buf, 0, v); err != nil {
26
-		return 0, err
27
-	}
28
-	return w.Write(buf.Bytes())
29
-}
30
-
31
-// HuffmanDecodeToString decodes the string in v.
32
-func HuffmanDecodeToString(v []byte) (string, error) {
33
-	buf := bufPool.Get().(*bytes.Buffer)
34
-	buf.Reset()
35
-	defer bufPool.Put(buf)
36
-	if err := huffmanDecode(buf, 0, v); err != nil {
37
-		return "", err
38
-	}
39
-	return buf.String(), nil
40
-}
41
-
42
-// ErrInvalidHuffman is returned for errors found decoding
43
-// Huffman-encoded strings.
44
-var ErrInvalidHuffman = errors.New("hpack: invalid Huffman-encoded data")
45
-
46
-// huffmanDecode decodes v to buf.
47
-// If maxLen is greater than 0, attempts to write more to buf than
48
-// maxLen bytes will return ErrStringLength.
49
-func huffmanDecode(buf *bytes.Buffer, maxLen int, v []byte) error {
50
-	n := rootHuffmanNode
51
-	// cur is the bit buffer that has not been fed into n.
52
-	// cbits is the number of low order bits in cur that are valid.
53
-	// sbits is the number of bits of the symbol prefix being decoded.
54
-	cur, cbits, sbits := uint(0), uint8(0), uint8(0)
55
-	for _, b := range v {
56
-		cur = cur<<8 | uint(b)
57
-		cbits += 8
58
-		sbits += 8
59
-		for cbits >= 8 {
60
-			idx := byte(cur >> (cbits - 8))
61
-			n = n.children[idx]
62
-			if n == nil {
63
-				return ErrInvalidHuffman
64
-			}
65
-			if n.children == nil {
66
-				if maxLen != 0 && buf.Len() == maxLen {
67
-					return ErrStringLength
68
-				}
69
-				buf.WriteByte(n.sym)
70
-				cbits -= n.codeLen
71
-				n = rootHuffmanNode
72
-				sbits = cbits
73
-			} else {
74
-				cbits -= 8
75
-			}
76
-		}
77
-	}
78
-	for cbits > 0 {
79
-		n = n.children[byte(cur<<(8-cbits))]
80
-		if n == nil {
81
-			return ErrInvalidHuffman
82
-		}
83
-		if n.children != nil || n.codeLen > cbits {
84
-			break
85
-		}
86
-		if maxLen != 0 && buf.Len() == maxLen {
87
-			return ErrStringLength
88
-		}
89
-		buf.WriteByte(n.sym)
90
-		cbits -= n.codeLen
91
-		n = rootHuffmanNode
92
-		sbits = cbits
93
-	}
94
-	if sbits > 7 {
95
-		// Either there was an incomplete symbol, or overlong padding.
96
-		// Both are decoding errors per RFC 7541 section 5.2.
97
-		return ErrInvalidHuffman
98
-	}
99
-	if mask := uint(1<<cbits - 1); cur&mask != mask {
100
-		// Trailing bits must be a prefix of EOS per RFC 7541 section 5.2.
101
-		return ErrInvalidHuffman
102
-	}
103
-
104
-	return nil
105
-}
106
-
107
-type node struct {
108
-	// children is non-nil for internal nodes
109
-	children []*node
110
-
111
-	// The following are only valid if children is nil:
112
-	codeLen uint8 // number of bits that led to the output of sym
113
-	sym     byte  // output symbol
114
-}
115
-
116
-func newInternalNode() *node {
117
-	return &node{children: make([]*node, 256)}
118
-}
119
-
120
-var rootHuffmanNode = newInternalNode()
121
-
122
-func init() {
123
-	if len(huffmanCodes) != 256 {
124
-		panic("unexpected size")
125
-	}
126
-	for i, code := range huffmanCodes {
127
-		addDecoderNode(byte(i), code, huffmanCodeLen[i])
128
-	}
129
-}
130
-
131
-func addDecoderNode(sym byte, code uint32, codeLen uint8) {
132
-	cur := rootHuffmanNode
133
-	for codeLen > 8 {
134
-		codeLen -= 8
135
-		i := uint8(code >> codeLen)
136
-		if cur.children[i] == nil {
137
-			cur.children[i] = newInternalNode()
138
-		}
139
-		cur = cur.children[i]
140
-	}
141
-	shift := 8 - codeLen
142
-	start, end := int(uint8(code<<shift)), int(1<<shift)
143
-	for i := start; i < start+end; i++ {
144
-		cur.children[i] = &node{sym: sym, codeLen: codeLen}
145
-	}
146
-}
147
-
148
-// AppendHuffmanString appends s, as encoded in Huffman codes, to dst
149
-// and returns the extended buffer.
150
-func AppendHuffmanString(dst []byte, s string) []byte {
151
-	rembits := uint8(8)
152
-
153
-	for i := 0; i < len(s); i++ {
154
-		if rembits == 8 {
155
-			dst = append(dst, 0)
156
-		}
157
-		dst, rembits = appendByteToHuffmanCode(dst, rembits, s[i])
158
-	}
159
-
160
-	if rembits < 8 {
161
-		// special EOS symbol
162
-		code := uint32(0x3fffffff)
163
-		nbits := uint8(30)
164
-
165
-		t := uint8(code >> (nbits - rembits))
166
-		dst[len(dst)-1] |= t
167
-	}
168
-
169
-	return dst
170
-}
171
-
172
-// HuffmanEncodeLength returns the number of bytes required to encode
173
-// s in Huffman codes. The result is round up to byte boundary.
174
-func HuffmanEncodeLength(s string) uint64 {
175
-	n := uint64(0)
176
-	for i := 0; i < len(s); i++ {
177
-		n += uint64(huffmanCodeLen[s[i]])
178
-	}
179
-	return (n + 7) / 8
180
-}
181
-
182
-// appendByteToHuffmanCode appends Huffman code for c to dst and
183
-// returns the extended buffer and the remaining bits in the last
184
-// element. The appending is not byte aligned and the remaining bits
185
-// in the last element of dst is given in rembits.
186
-func appendByteToHuffmanCode(dst []byte, rembits uint8, c byte) ([]byte, uint8) {
187
-	code := huffmanCodes[c]
188
-	nbits := huffmanCodeLen[c]
189
-
190
-	for {
191
-		if rembits > nbits {
192
-			t := uint8(code << (rembits - nbits))
193
-			dst[len(dst)-1] |= t
194
-			rembits -= nbits
195
-			break
196
-		}
197
-
198
-		t := uint8(code >> (nbits - rembits))
199
-		dst[len(dst)-1] |= t
200
-
201
-		nbits -= rembits
202
-		rembits = 8
203
-
204
-		if nbits == 0 {
205
-			break
206
-		}
207
-
208
-		dst = append(dst, 0)
209
-	}
210
-
211
-	return dst, rembits
212
-}

+ 0
- 479
vendor/golang.org/x/net/http2/hpack/tables.go View File

1
-// Copyright 2014 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 hpack
6
-
7
-import (
8
-	"fmt"
9
-)
10
-
11
-// headerFieldTable implements a list of HeaderFields.
12
-// This is used to implement the static and dynamic tables.
13
-type headerFieldTable struct {
14
-	// For static tables, entries are never evicted.
15
-	//
16
-	// For dynamic tables, entries are evicted from ents[0] and added to the end.
17
-	// Each entry has a unique id that starts at one and increments for each
18
-	// entry that is added. This unique id is stable across evictions, meaning
19
-	// it can be used as a pointer to a specific entry. As in hpack, unique ids
20
-	// are 1-based. The unique id for ents[k] is k + evictCount + 1.
21
-	//
22
-	// Zero is not a valid unique id.
23
-	//
24
-	// evictCount should not overflow in any remotely practical situation. In
25
-	// practice, we will have one dynamic table per HTTP/2 connection. If we
26
-	// assume a very powerful server that handles 1M QPS per connection and each
27
-	// request adds (then evicts) 100 entries from the table, it would still take
28
-	// 2M years for evictCount to overflow.
29
-	ents       []HeaderField
30
-	evictCount uint64
31
-
32
-	// byName maps a HeaderField name to the unique id of the newest entry with
33
-	// the same name. See above for a definition of "unique id".
34
-	byName map[string]uint64
35
-
36
-	// byNameValue maps a HeaderField name/value pair to the unique id of the newest
37
-	// entry with the same name and value. See above for a definition of "unique id".
38
-	byNameValue map[pairNameValue]uint64
39
-}
40
-
41
-type pairNameValue struct {
42
-	name, value string
43
-}
44
-
45
-func (t *headerFieldTable) init() {
46
-	t.byName = make(map[string]uint64)
47
-	t.byNameValue = make(map[pairNameValue]uint64)
48
-}
49
-
50
-// len reports the number of entries in the table.
51
-func (t *headerFieldTable) len() int {
52
-	return len(t.ents)
53
-}
54
-
55
-// addEntry adds a new entry.
56
-func (t *headerFieldTable) addEntry(f HeaderField) {
57
-	id := uint64(t.len()) + t.evictCount + 1
58
-	t.byName[f.Name] = id
59
-	t.byNameValue[pairNameValue{f.Name, f.Value}] = id
60
-	t.ents = append(t.ents, f)
61
-}
62
-
63
-// evictOldest evicts the n oldest entries in the table.
64
-func (t *headerFieldTable) evictOldest(n int) {
65
-	if n > t.len() {
66
-		panic(fmt.Sprintf("evictOldest(%v) on table with %v entries", n, t.len()))
67
-	}
68
-	for k := 0; k < n; k++ {
69
-		f := t.ents[k]
70
-		id := t.evictCount + uint64(k) + 1
71
-		if t.byName[f.Name] == id {
72
-			delete(t.byName, f.Name)
73
-		}
74
-		if p := (pairNameValue{f.Name, f.Value}); t.byNameValue[p] == id {
75
-			delete(t.byNameValue, p)
76
-		}
77
-	}
78
-	copy(t.ents, t.ents[n:])
79
-	for k := t.len() - n; k < t.len(); k++ {
80
-		t.ents[k] = HeaderField{} // so strings can be garbage collected
81
-	}
82
-	t.ents = t.ents[:t.len()-n]
83
-	if t.evictCount+uint64(n) < t.evictCount {
84
-		panic("evictCount overflow")
85
-	}
86
-	t.evictCount += uint64(n)
87
-}
88
-
89
-// search finds f in the table. If there is no match, i is 0.
90
-// If both name and value match, i is the matched index and nameValueMatch
91
-// becomes true. If only name matches, i points to that index and
92
-// nameValueMatch becomes false.
93
-//
94
-// The returned index is a 1-based HPACK index. For dynamic tables, HPACK says
95
-// that index 1 should be the newest entry, but t.ents[0] is the oldest entry,
96
-// meaning t.ents is reversed for dynamic tables. Hence, when t is a dynamic
97
-// table, the return value i actually refers to the entry t.ents[t.len()-i].
98
-//
99
-// All tables are assumed to be a dynamic tables except for the global
100
-// staticTable pointer.
101
-//
102
-// See Section 2.3.3.
103
-func (t *headerFieldTable) search(f HeaderField) (i uint64, nameValueMatch bool) {
104
-	if !f.Sensitive {
105
-		if id := t.byNameValue[pairNameValue{f.Name, f.Value}]; id != 0 {
106
-			return t.idToIndex(id), true
107
-		}
108
-	}
109
-	if id := t.byName[f.Name]; id != 0 {
110
-		return t.idToIndex(id), false
111
-	}
112
-	return 0, false
113
-}
114
-
115
-// idToIndex converts a unique id to an HPACK index.
116
-// See Section 2.3.3.
117
-func (t *headerFieldTable) idToIndex(id uint64) uint64 {
118
-	if id <= t.evictCount {
119
-		panic(fmt.Sprintf("id (%v) <= evictCount (%v)", id, t.evictCount))
120
-	}
121
-	k := id - t.evictCount - 1 // convert id to an index t.ents[k]
122
-	if t != staticTable {
123
-		return uint64(t.len()) - k // dynamic table
124
-	}
125
-	return k + 1
126
-}
127
-
128
-// http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-07#appendix-B
129
-var staticTable = newStaticTable()
130
-var staticTableEntries = [...]HeaderField{
131
-	{Name: ":authority"},
132
-	{Name: ":method", Value: "GET"},
133
-	{Name: ":method", Value: "POST"},
134
-	{Name: ":path", Value: "/"},
135
-	{Name: ":path", Value: "/index.html"},
136
-	{Name: ":scheme", Value: "http"},
137
-	{Name: ":scheme", Value: "https"},
138
-	{Name: ":status", Value: "200"},
139
-	{Name: ":status", Value: "204"},
140
-	{Name: ":status", Value: "206"},
141
-	{Name: ":status", Value: "304"},
142
-	{Name: ":status", Value: "400"},
143
-	{Name: ":status", Value: "404"},
144
-	{Name: ":status", Value: "500"},
145
-	{Name: "accept-charset"},
146
-	{Name: "accept-encoding", Value: "gzip, deflate"},
147
-	{Name: "accept-language"},
148
-	{Name: "accept-ranges"},
149
-	{Name: "accept"},
150
-	{Name: "access-control-allow-origin"},
151
-	{Name: "age"},
152
-	{Name: "allow"},
153
-	{Name: "authorization"},
154
-	{Name: "cache-control"},
155
-	{Name: "content-disposition"},
156
-	{Name: "content-encoding"},
157
-	{Name: "content-language"},
158
-	{Name: "content-length"},
159
-	{Name: "content-location"},
160
-	{Name: "content-range"},
161
-	{Name: "content-type"},
162
-	{Name: "cookie"},
163
-	{Name: "date"},
164
-	{Name: "etag"},
165
-	{Name: "expect"},
166
-	{Name: "expires"},
167
-	{Name: "from"},
168
-	{Name: "host"},
169
-	{Name: "if-match"},
170
-	{Name: "if-modified-since"},
171
-	{Name: "if-none-match"},
172
-	{Name: "if-range"},
173
-	{Name: "if-unmodified-since"},
174
-	{Name: "last-modified"},
175
-	{Name: "link"},
176
-	{Name: "location"},
177
-	{Name: "max-forwards"},
178
-	{Name: "proxy-authenticate"},
179
-	{Name: "proxy-authorization"},
180
-	{Name: "range"},
181
-	{Name: "referer"},
182
-	{Name: "refresh"},
183
-	{Name: "retry-after"},
184
-	{Name: "server"},
185
-	{Name: "set-cookie"},
186
-	{Name: "strict-transport-security"},
187
-	{Name: "transfer-encoding"},
188
-	{Name: "user-agent"},
189
-	{Name: "vary"},
190
-	{Name: "via"},
191
-	{Name: "www-authenticate"},
192
-}
193
-
194
-func newStaticTable() *headerFieldTable {
195
-	t := &headerFieldTable{}
196
-	t.init()
197
-	for _, e := range staticTableEntries[:] {
198
-		t.addEntry(e)
199
-	}
200
-	return t
201
-}
202
-
203
-var huffmanCodes = [256]uint32{
204
-	0x1ff8,
205
-	0x7fffd8,
206
-	0xfffffe2,
207
-	0xfffffe3,
208
-	0xfffffe4,
209
-	0xfffffe5,
210
-	0xfffffe6,
211
-	0xfffffe7,
212
-	0xfffffe8,
213
-	0xffffea,
214
-	0x3ffffffc,
215
-	0xfffffe9,
216
-	0xfffffea,
217
-	0x3ffffffd,
218
-	0xfffffeb,
219
-	0xfffffec,
220
-	0xfffffed,
221
-	0xfffffee,
222
-	0xfffffef,
223
-	0xffffff0,
224
-	0xffffff1,
225
-	0xffffff2,
226
-	0x3ffffffe,
227
-	0xffffff3,
228
-	0xffffff4,
229
-	0xffffff5,
230
-	0xffffff6,
231
-	0xffffff7,
232
-	0xffffff8,
233
-	0xffffff9,
234
-	0xffffffa,
235
-	0xffffffb,
236
-	0x14,
237
-	0x3f8,
238
-	0x3f9,
239
-	0xffa,
240
-	0x1ff9,
241
-	0x15,
242
-	0xf8,
243
-	0x7fa,
244
-	0x3fa,
245
-	0x3fb,
246
-	0xf9,
247
-	0x7fb,
248
-	0xfa,
249
-	0x16,
250
-	0x17,
251
-	0x18,
252
-	0x0,
253
-	0x1,
254
-	0x2,
255
-	0x19,
256
-	0x1a,
257
-	0x1b,
258
-	0x1c,
259
-	0x1d,
260
-	0x1e,
261
-	0x1f,
262
-	0x5c,
263
-	0xfb,
264
-	0x7ffc,
265
-	0x20,
266
-	0xffb,
267
-	0x3fc,
268
-	0x1ffa,
269
-	0x21,
270
-	0x5d,
271
-	0x5e,
272
-	0x5f,
273
-	0x60,
274
-	0x61,
275
-	0x62,
276
-	0x63,
277
-	0x64,
278
-	0x65,
279
-	0x66,
280
-	0x67,
281
-	0x68,
282
-	0x69,
283
-	0x6a,
284
-	0x6b,
285
-	0x6c,
286
-	0x6d,
287
-	0x6e,
288
-	0x6f,
289
-	0x70,
290
-	0x71,
291
-	0x72,
292
-	0xfc,
293
-	0x73,
294
-	0xfd,
295
-	0x1ffb,
296
-	0x7fff0,
297
-	0x1ffc,
298
-	0x3ffc,
299
-	0x22,
300
-	0x7ffd,
301
-	0x3,
302
-	0x23,
303
-	0x4,
304
-	0x24,
305
-	0x5,
306
-	0x25,
307
-	0x26,
308
-	0x27,
309
-	0x6,
310
-	0x74,
311
-	0x75,
312
-	0x28,
313
-	0x29,
314
-	0x2a,
315
-	0x7,
316
-	0x2b,
317
-	0x76,
318
-	0x2c,
319
-	0x8,
320
-	0x9,
321
-	0x2d,
322
-	0x77,
323
-	0x78,
324
-	0x79,
325
-	0x7a,
326
-	0x7b,
327
-	0x7ffe,
328
-	0x7fc,
329
-	0x3ffd,
330
-	0x1ffd,
331
-	0xffffffc,
332
-	0xfffe6,
333
-	0x3fffd2,
334
-	0xfffe7,
335
-	0xfffe8,
336
-	0x3fffd3,
337
-	0x3fffd4,
338
-	0x3fffd5,
339
-	0x7fffd9,
340
-	0x3fffd6,
341
-	0x7fffda,
342
-	0x7fffdb,
343
-	0x7fffdc,
344
-	0x7fffdd,
345
-	0x7fffde,
346
-	0xffffeb,
347
-	0x7fffdf,
348
-	0xffffec,
349
-	0xffffed,
350
-	0x3fffd7,
351
-	0x7fffe0,
352
-	0xffffee,
353
-	0x7fffe1,
354
-	0x7fffe2,
355
-	0x7fffe3,
356
-	0x7fffe4,
357
-	0x1fffdc,
358
-	0x3fffd8,
359
-	0x7fffe5,
360
-	0x3fffd9,
361
-	0x7fffe6,
362
-	0x7fffe7,
363
-	0xffffef,
364
-	0x3fffda,
365
-	0x1fffdd,
366
-	0xfffe9,
367
-	0x3fffdb,
368
-	0x3fffdc,
369
-	0x7fffe8,
370
-	0x7fffe9,
371
-	0x1fffde,
372
-	0x7fffea,
373
-	0x3fffdd,
374
-	0x3fffde,
375
-	0xfffff0,
376
-	0x1fffdf,
377
-	0x3fffdf,
378
-	0x7fffeb,
379
-	0x7fffec,
380
-	0x1fffe0,
381
-	0x1fffe1,
382
-	0x3fffe0,
383
-	0x1fffe2,
384
-	0x7fffed,
385
-	0x3fffe1,
386
-	0x7fffee,
387
-	0x7fffef,
388
-	0xfffea,
389
-	0x3fffe2,
390
-	0x3fffe3,
391
-	0x3fffe4,
392
-	0x7ffff0,
393
-	0x3fffe5,
394
-	0x3fffe6,
395
-	0x7ffff1,
396
-	0x3ffffe0,
397
-	0x3ffffe1,
398
-	0xfffeb,
399
-	0x7fff1,
400
-	0x3fffe7,
401
-	0x7ffff2,
402
-	0x3fffe8,
403
-	0x1ffffec,
404
-	0x3ffffe2,
405
-	0x3ffffe3,
406
-	0x3ffffe4,
407
-	0x7ffffde,
408
-	0x7ffffdf,
409
-	0x3ffffe5,
410
-	0xfffff1,
411
-	0x1ffffed,
412
-	0x7fff2,
413
-	0x1fffe3,
414
-	0x3ffffe6,
415
-	0x7ffffe0,
416
-	0x7ffffe1,
417
-	0x3ffffe7,
418
-	0x7ffffe2,
419
-	0xfffff2,
420
-	0x1fffe4,
421
-	0x1fffe5,
422
-	0x3ffffe8,
423
-	0x3ffffe9,
424
-	0xffffffd,
425
-	0x7ffffe3,
426
-	0x7ffffe4,
427
-	0x7ffffe5,
428
-	0xfffec,
429
-	0xfffff3,
430
-	0xfffed,
431
-	0x1fffe6,
432
-	0x3fffe9,
433
-	0x1fffe7,
434
-	0x1fffe8,
435
-	0x7ffff3,
436
-	0x3fffea,
437
-	0x3fffeb,
438
-	0x1ffffee,
439
-	0x1ffffef,
440
-	0xfffff4,
441
-	0xfffff5,
442
-	0x3ffffea,
443
-	0x7ffff4,
444
-	0x3ffffeb,
445
-	0x7ffffe6,
446
-	0x3ffffec,
447
-	0x3ffffed,
448
-	0x7ffffe7,
449
-	0x7ffffe8,
450
-	0x7ffffe9,
451
-	0x7ffffea,
452
-	0x7ffffeb,
453
-	0xffffffe,
454
-	0x7ffffec,
455
-	0x7ffffed,
456
-	0x7ffffee,
457
-	0x7ffffef,
458
-	0x7fffff0,
459
-	0x3ffffee,
460
-}
461
-
462
-var huffmanCodeLen = [256]uint8{
463
-	13, 23, 28, 28, 28, 28, 28, 28, 28, 24, 30, 28, 28, 30, 28, 28,
464
-	28, 28, 28, 28, 28, 28, 30, 28, 28, 28, 28, 28, 28, 28, 28, 28,
465
-	6, 10, 10, 12, 13, 6, 8, 11, 10, 10, 8, 11, 8, 6, 6, 6,
466
-	5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 8, 15, 6, 12, 10,
467
-	13, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
468
-	7, 7, 7, 7, 7, 7, 7, 7, 8, 7, 8, 13, 19, 13, 14, 6,
469
-	15, 5, 6, 5, 6, 5, 6, 6, 6, 5, 7, 7, 6, 6, 6, 5,
470
-	6, 7, 6, 5, 5, 6, 7, 7, 7, 7, 7, 15, 11, 14, 13, 28,
471
-	20, 22, 20, 20, 22, 22, 22, 23, 22, 23, 23, 23, 23, 23, 24, 23,
472
-	24, 24, 22, 23, 24, 23, 23, 23, 23, 21, 22, 23, 22, 23, 23, 24,
473
-	22, 21, 20, 22, 22, 23, 23, 21, 23, 22, 22, 24, 21, 22, 23, 23,
474
-	21, 21, 22, 21, 23, 22, 23, 23, 20, 22, 22, 22, 23, 22, 22, 23,
475
-	26, 26, 20, 19, 22, 23, 22, 25, 26, 26, 26, 27, 27, 26, 24, 25,
476
-	19, 21, 26, 27, 27, 26, 27, 24, 21, 21, 26, 26, 28, 27, 27, 27,
477
-	20, 24, 20, 21, 22, 21, 21, 23, 22, 22, 25, 25, 24, 24, 26, 23,
478
-	26, 27, 26, 26, 27, 27, 27, 27, 27, 28, 27, 27, 27, 27, 27, 26,
479
-}

+ 0
- 214
vendor/golang.org/x/net/http2/hpack/tables_test.go View File

1
-// Copyright 2017 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 hpack
6
-
7
-import (
8
-	"bufio"
9
-	"regexp"
10
-	"strconv"
11
-	"strings"
12
-	"testing"
13
-)
14
-
15
-func TestHeaderFieldTable(t *testing.T) {
16
-	table := &headerFieldTable{}
17
-	table.init()
18
-	table.addEntry(pair("key1", "value1-1"))
19
-	table.addEntry(pair("key2", "value2-1"))
20
-	table.addEntry(pair("key1", "value1-2"))
21
-	table.addEntry(pair("key3", "value3-1"))
22
-	table.addEntry(pair("key4", "value4-1"))
23
-	table.addEntry(pair("key2", "value2-2"))
24
-
25
-	// Tests will be run twice: once before evicting anything, and
26
-	// again after evicting the three oldest entries.
27
-	tests := []struct {
28
-		f                 HeaderField
29
-		beforeWantStaticI uint64
30
-		beforeWantMatch   bool
31
-		afterWantStaticI  uint64
32
-		afterWantMatch    bool
33
-	}{
34
-		{HeaderField{"key1", "value1-1", false}, 1, true, 0, false},
35
-		{HeaderField{"key1", "value1-2", false}, 3, true, 0, false},
36
-		{HeaderField{"key1", "value1-3", false}, 3, false, 0, false},
37
-		{HeaderField{"key2", "value2-1", false}, 2, true, 3, false},
38
-		{HeaderField{"key2", "value2-2", false}, 6, true, 3, true},
39
-		{HeaderField{"key2", "value2-3", false}, 6, false, 3, false},
40
-		{HeaderField{"key4", "value4-1", false}, 5, true, 2, true},
41
-		// Name match only, because sensitive.
42
-		{HeaderField{"key4", "value4-1", true}, 5, false, 2, false},
43
-		// Key not found.
44
-		{HeaderField{"key5", "value5-x", false}, 0, false, 0, false},
45
-	}
46
-
47
-	staticToDynamic := func(i uint64) uint64 {
48
-		if i == 0 {
49
-			return 0
50
-		}
51
-		return uint64(table.len()) - i + 1 // dynamic is the reversed table
52
-	}
53
-
54
-	searchStatic := func(f HeaderField) (uint64, bool) {
55
-		old := staticTable
56
-		staticTable = table
57
-		defer func() { staticTable = old }()
58
-		return staticTable.search(f)
59
-	}
60
-
61
-	searchDynamic := func(f HeaderField) (uint64, bool) {
62
-		return table.search(f)
63
-	}
64
-
65
-	for _, test := range tests {
66
-		gotI, gotMatch := searchStatic(test.f)
67
-		if wantI, wantMatch := test.beforeWantStaticI, test.beforeWantMatch; gotI != wantI || gotMatch != wantMatch {
68
-			t.Errorf("before evictions: searchStatic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
69
-		}
70
-		gotI, gotMatch = searchDynamic(test.f)
71
-		wantDynamicI := staticToDynamic(test.beforeWantStaticI)
72
-		if wantI, wantMatch := wantDynamicI, test.beforeWantMatch; gotI != wantI || gotMatch != wantMatch {
73
-			t.Errorf("before evictions: searchDynamic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
74
-		}
75
-	}
76
-
77
-	table.evictOldest(3)
78
-
79
-	for _, test := range tests {
80
-		gotI, gotMatch := searchStatic(test.f)
81
-		if wantI, wantMatch := test.afterWantStaticI, test.afterWantMatch; gotI != wantI || gotMatch != wantMatch {
82
-			t.Errorf("after evictions: searchStatic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
83
-		}
84
-		gotI, gotMatch = searchDynamic(test.f)
85
-		wantDynamicI := staticToDynamic(test.afterWantStaticI)
86
-		if wantI, wantMatch := wantDynamicI, test.afterWantMatch; gotI != wantI || gotMatch != wantMatch {
87
-			t.Errorf("after evictions: searchDynamic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
88
-		}
89
-	}
90
-}
91
-
92
-func TestHeaderFieldTable_LookupMapEviction(t *testing.T) {
93
-	table := &headerFieldTable{}
94
-	table.init()
95
-	table.addEntry(pair("key1", "value1-1"))
96
-	table.addEntry(pair("key2", "value2-1"))
97
-	table.addEntry(pair("key1", "value1-2"))
98
-	table.addEntry(pair("key3", "value3-1"))
99
-	table.addEntry(pair("key4", "value4-1"))
100
-	table.addEntry(pair("key2", "value2-2"))
101
-
102
-	// evict all pairs
103
-	table.evictOldest(table.len())
104
-
105
-	if l := table.len(); l > 0 {
106
-		t.Errorf("table.len() = %d, want 0", l)
107
-	}
108
-
109
-	if l := len(table.byName); l > 0 {
110
-		t.Errorf("len(table.byName) = %d, want 0", l)
111
-	}
112
-
113
-	if l := len(table.byNameValue); l > 0 {
114
-		t.Errorf("len(table.byNameValue) = %d, want 0", l)
115
-	}
116
-}
117
-
118
-func TestStaticTable(t *testing.T) {
119
-	fromSpec := `
120
-          +-------+-----------------------------+---------------+
121
-          | 1     | :authority                  |               |
122
-          | 2     | :method                     | GET           |
123
-          | 3     | :method                     | POST          |
124
-          | 4     | :path                       | /             |
125
-          | 5     | :path                       | /index.html   |
126
-          | 6     | :scheme                     | http          |
127
-          | 7     | :scheme                     | https         |
128
-          | 8     | :status                     | 200           |
129
-          | 9     | :status                     | 204           |
130
-          | 10    | :status                     | 206           |
131
-          | 11    | :status                     | 304           |
132
-          | 12    | :status                     | 400           |
133
-          | 13    | :status                     | 404           |
134
-          | 14    | :status                     | 500           |
135
-          | 15    | accept-charset              |               |
136
-          | 16    | accept-encoding             | gzip, deflate |
137
-          | 17    | accept-language             |               |
138
-          | 18    | accept-ranges               |               |
139
-          | 19    | accept                      |               |
140
-          | 20    | access-control-allow-origin |               |
141
-          | 21    | age                         |               |
142
-          | 22    | allow                       |               |
143
-          | 23    | authorization               |               |
144
-          | 24    | cache-control               |               |
145
-          | 25    | content-disposition         |               |
146
-          | 26    | content-encoding            |               |
147
-          | 27    | content-language            |               |
148
-          | 28    | content-length              |               |
149
-          | 29    | content-location            |               |
150
-          | 30    | content-range               |               |
151
-          | 31    | content-type                |               |
152
-          | 32    | cookie                      |               |
153
-          | 33    | date                        |               |
154
-          | 34    | etag                        |               |
155
-          | 35    | expect                      |               |
156
-          | 36    | expires                     |               |
157
-          | 37    | from                        |               |
158
-          | 38    | host                        |               |
159
-          | 39    | if-match                    |               |
160
-          | 40    | if-modified-since           |               |
161
-          | 41    | if-none-match               |               |
162
-          | 42    | if-range                    |               |
163
-          | 43    | if-unmodified-since         |               |
164
-          | 44    | last-modified               |               |
165
-          | 45    | link                        |               |
166
-          | 46    | location                    |               |
167
-          | 47    | max-forwards                |               |
168
-          | 48    | proxy-authenticate          |               |
169
-          | 49    | proxy-authorization         |               |
170
-          | 50    | range                       |               |
171
-          | 51    | referer                     |               |
172
-          | 52    | refresh                     |               |
173
-          | 53    | retry-after                 |               |
174
-          | 54    | server                      |               |
175
-          | 55    | set-cookie                  |               |
176
-          | 56    | strict-transport-security   |               |
177
-          | 57    | transfer-encoding           |               |
178
-          | 58    | user-agent                  |               |
179
-          | 59    | vary                        |               |
180
-          | 60    | via                         |               |
181
-          | 61    | www-authenticate            |               |
182
-          +-------+-----------------------------+---------------+
183
-`
184
-	bs := bufio.NewScanner(strings.NewReader(fromSpec))
185
-	re := regexp.MustCompile(`\| (\d+)\s+\| (\S+)\s*\| (\S(.*\S)?)?\s+\|`)
186
-	for bs.Scan() {
187
-		l := bs.Text()
188
-		if !strings.Contains(l, "|") {
189
-			continue
190
-		}
191
-		m := re.FindStringSubmatch(l)
192
-		if m == nil {
193
-			continue
194
-		}
195
-		i, err := strconv.Atoi(m[1])
196
-		if err != nil {
197
-			t.Errorf("Bogus integer on line %q", l)
198
-			continue
199
-		}
200
-		if i < 1 || i > staticTable.len() {
201
-			t.Errorf("Bogus index %d on line %q", i, l)
202
-			continue
203
-		}
204
-		if got, want := staticTable.ents[i-1].Name, m[2]; got != want {
205
-			t.Errorf("header index %d name = %q; want %q", i, got, want)
206
-		}
207
-		if got, want := staticTable.ents[i-1].Value, m[3]; got != want {
208
-			t.Errorf("header index %d value = %q; want %q", i, got, want)
209
-		}
210
-	}
211
-	if err := bs.Err(); err != nil {
212
-		t.Error(err)
213
-	}
214
-}

+ 0
- 391
vendor/golang.org/x/net/http2/http2.go View File

1
-// Copyright 2014 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 http2 implements the HTTP/2 protocol.
6
-//
7
-// This package is low-level and intended to be used directly by very
8
-// few people. Most users will use it indirectly through the automatic
9
-// use by the net/http package (from Go 1.6 and later).
10
-// For use in earlier Go versions see ConfigureServer. (Transport support
11
-// requires Go 1.6 or later)
12
-//
13
-// See https://http2.github.io/ for more information on HTTP/2.
14
-//
15
-// See https://http2.golang.org/ for a test server running this code.
16
-//
17
-package http2 // import "golang.org/x/net/http2"
18
-
19
-import (
20
-	"bufio"
21
-	"crypto/tls"
22
-	"errors"
23
-	"fmt"
24
-	"io"
25
-	"net/http"
26
-	"os"
27
-	"sort"
28
-	"strconv"
29
-	"strings"
30
-	"sync"
31
-
32
-	"golang.org/x/net/lex/httplex"
33
-)
34
-
35
-var (
36
-	VerboseLogs    bool
37
-	logFrameWrites bool
38
-	logFrameReads  bool
39
-	inTests        bool
40
-)
41
-
42
-func init() {
43
-	e := os.Getenv("GODEBUG")
44
-	if strings.Contains(e, "http2debug=1") {
45
-		VerboseLogs = true
46
-	}
47
-	if strings.Contains(e, "http2debug=2") {
48
-		VerboseLogs = true
49
-		logFrameWrites = true
50
-		logFrameReads = true
51
-	}
52
-}
53
-
54
-const (
55
-	// ClientPreface is the string that must be sent by new
56
-	// connections from clients.
57
-	ClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
58
-
59
-	// SETTINGS_MAX_FRAME_SIZE default
60
-	// http://http2.github.io/http2-spec/#rfc.section.6.5.2
61
-	initialMaxFrameSize = 16384
62
-
63
-	// NextProtoTLS is the NPN/ALPN protocol negotiated during
64
-	// HTTP/2's TLS setup.
65
-	NextProtoTLS = "h2"
66
-
67
-	// http://http2.github.io/http2-spec/#SettingValues
68
-	initialHeaderTableSize = 4096
69
-
70
-	initialWindowSize = 65535 // 6.9.2 Initial Flow Control Window Size
71
-
72
-	defaultMaxReadFrameSize = 1 << 20
73
-)
74
-
75
-var (
76
-	clientPreface = []byte(ClientPreface)
77
-)
78
-
79
-type streamState int
80
-
81
-// HTTP/2 stream states.
82
-//
83
-// See http://tools.ietf.org/html/rfc7540#section-5.1.
84
-//
85
-// For simplicity, the server code merges "reserved (local)" into
86
-// "half-closed (remote)". This is one less state transition to track.
87
-// The only downside is that we send PUSH_PROMISEs slightly less
88
-// liberally than allowable. More discussion here:
89
-// https://lists.w3.org/Archives/Public/ietf-http-wg/2016JulSep/0599.html
90
-//
91
-// "reserved (remote)" is omitted since the client code does not
92
-// support server push.
93
-const (
94
-	stateIdle streamState = iota
95
-	stateOpen
96
-	stateHalfClosedLocal
97
-	stateHalfClosedRemote
98
-	stateClosed
99
-)
100
-
101
-var stateName = [...]string{
102
-	stateIdle:             "Idle",
103
-	stateOpen:             "Open",
104
-	stateHalfClosedLocal:  "HalfClosedLocal",
105
-	stateHalfClosedRemote: "HalfClosedRemote",
106
-	stateClosed:           "Closed",
107
-}
108
-
109
-func (st streamState) String() string {
110
-	return stateName[st]
111
-}
112
-
113
-// Setting is a setting parameter: which setting it is, and its value.
114
-type Setting struct {
115
-	// ID is which setting is being set.
116
-	// See http://http2.github.io/http2-spec/#SettingValues
117
-	ID SettingID
118
-
119
-	// Val is the value.
120
-	Val uint32
121
-}
122
-
123
-func (s Setting) String() string {
124
-	return fmt.Sprintf("[%v = %d]", s.ID, s.Val)
125
-}
126
-
127
-// Valid reports whether the setting is valid.
128
-func (s Setting) Valid() error {
129
-	// Limits and error codes from 6.5.2 Defined SETTINGS Parameters
130
-	switch s.ID {
131
-	case SettingEnablePush:
132
-		if s.Val != 1 && s.Val != 0 {
133
-			return ConnectionError(ErrCodeProtocol)
134
-		}
135
-	case SettingInitialWindowSize:
136
-		if s.Val > 1<<31-1 {
137
-			return ConnectionError(ErrCodeFlowControl)
138
-		}
139
-	case SettingMaxFrameSize:
140
-		if s.Val < 16384 || s.Val > 1<<24-1 {
141
-			return ConnectionError(ErrCodeProtocol)
142
-		}
143
-	}
144
-	return nil
145
-}
146
-
147
-// A SettingID is an HTTP/2 setting as defined in
148
-// http://http2.github.io/http2-spec/#iana-settings
149
-type SettingID uint16
150
-
151
-const (
152
-	SettingHeaderTableSize      SettingID = 0x1
153
-	SettingEnablePush           SettingID = 0x2
154
-	SettingMaxConcurrentStreams SettingID = 0x3
155
-	SettingInitialWindowSize    SettingID = 0x4
156
-	SettingMaxFrameSize         SettingID = 0x5
157
-	SettingMaxHeaderListSize    SettingID = 0x6
158
-)
159
-
160
-var settingName = map[SettingID]string{
161
-	SettingHeaderTableSize:      "HEADER_TABLE_SIZE",
162
-	SettingEnablePush:           "ENABLE_PUSH",
163
-	SettingMaxConcurrentStreams: "MAX_CONCURRENT_STREAMS",
164
-	SettingInitialWindowSize:    "INITIAL_WINDOW_SIZE",
165
-	SettingMaxFrameSize:         "MAX_FRAME_SIZE",
166
-	SettingMaxHeaderListSize:    "MAX_HEADER_LIST_SIZE",
167
-}
168
-
169
-func (s SettingID) String() string {
170
-	if v, ok := settingName[s]; ok {
171
-		return v
172
-	}
173
-	return fmt.Sprintf("UNKNOWN_SETTING_%d", uint16(s))
174
-}
175
-
176
-var (
177
-	errInvalidHeaderFieldName  = errors.New("http2: invalid header field name")
178
-	errInvalidHeaderFieldValue = errors.New("http2: invalid header field value")
179
-)
180
-
181
-// validWireHeaderFieldName reports whether v is a valid header field
182
-// name (key). See httplex.ValidHeaderName for the base rules.
183
-//
184
-// Further, http2 says:
185
-//   "Just as in HTTP/1.x, header field names are strings of ASCII
186
-//   characters that are compared in a case-insensitive
187
-//   fashion. However, header field names MUST be converted to
188
-//   lowercase prior to their encoding in HTTP/2. "
189
-func validWireHeaderFieldName(v string) bool {
190
-	if len(v) == 0 {
191
-		return false
192
-	}
193
-	for _, r := range v {
194
-		if !httplex.IsTokenRune(r) {
195
-			return false
196
-		}
197
-		if 'A' <= r && r <= 'Z' {
198
-			return false
199
-		}
200
-	}
201
-	return true
202
-}
203
-
204
-var httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
205
-
206
-func init() {
207
-	for i := 100; i <= 999; i++ {
208
-		if v := http.StatusText(i); v != "" {
209
-			httpCodeStringCommon[i] = strconv.Itoa(i)
210
-		}
211
-	}
212
-}
213
-
214
-func httpCodeString(code int) string {
215
-	if s, ok := httpCodeStringCommon[code]; ok {
216
-		return s
217
-	}
218
-	return strconv.Itoa(code)
219
-}
220
-
221
-// from pkg io
222
-type stringWriter interface {
223
-	WriteString(s string) (n int, err error)
224
-}
225
-
226
-// A gate lets two goroutines coordinate their activities.
227
-type gate chan struct{}
228
-
229
-func (g gate) Done() { g <- struct{}{} }
230
-func (g gate) Wait() { <-g }
231
-
232
-// A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
233
-type closeWaiter chan struct{}
234
-
235
-// Init makes a closeWaiter usable.
236
-// It exists because so a closeWaiter value can be placed inside a
237
-// larger struct and have the Mutex and Cond's memory in the same
238
-// allocation.
239
-func (cw *closeWaiter) Init() {
240
-	*cw = make(chan struct{})
241
-}
242
-
243
-// Close marks the closeWaiter as closed and unblocks any waiters.
244
-func (cw closeWaiter) Close() {
245
-	close(cw)
246
-}
247
-
248
-// Wait waits for the closeWaiter to become closed.
249
-func (cw closeWaiter) Wait() {
250
-	<-cw
251
-}
252
-
253
-// bufferedWriter is a buffered writer that writes to w.
254
-// Its buffered writer is lazily allocated as needed, to minimize
255
-// idle memory usage with many connections.
256
-type bufferedWriter struct {
257
-	w  io.Writer     // immutable
258
-	bw *bufio.Writer // non-nil when data is buffered
259
-}
260
-
261
-func newBufferedWriter(w io.Writer) *bufferedWriter {
262
-	return &bufferedWriter{w: w}
263
-}
264
-
265
-// bufWriterPoolBufferSize is the size of bufio.Writer's
266
-// buffers created using bufWriterPool.
267
-//
268
-// TODO: pick a less arbitrary value? this is a bit under
269
-// (3 x typical 1500 byte MTU) at least. Other than that,
270
-// not much thought went into it.
271
-const bufWriterPoolBufferSize = 4 << 10
272
-
273
-var bufWriterPool = sync.Pool{
274
-	New: func() interface{} {
275
-		return bufio.NewWriterSize(nil, bufWriterPoolBufferSize)
276
-	},
277
-}
278
-
279
-func (w *bufferedWriter) Available() int {
280
-	if w.bw == nil {
281
-		return bufWriterPoolBufferSize
282
-	}
283
-	return w.bw.Available()
284
-}
285
-
286
-func (w *bufferedWriter) Write(p []byte) (n int, err error) {
287
-	if w.bw == nil {
288
-		bw := bufWriterPool.Get().(*bufio.Writer)
289
-		bw.Reset(w.w)
290
-		w.bw = bw
291
-	}
292
-	return w.bw.Write(p)
293
-}
294
-
295
-func (w *bufferedWriter) Flush() error {
296
-	bw := w.bw
297
-	if bw == nil {
298
-		return nil
299
-	}
300
-	err := bw.Flush()
301
-	bw.Reset(nil)
302
-	bufWriterPool.Put(bw)
303
-	w.bw = nil
304
-	return err
305
-}
306
-
307
-func mustUint31(v int32) uint32 {
308
-	if v < 0 || v > 2147483647 {
309
-		panic("out of range")
310
-	}
311
-	return uint32(v)
312
-}
313
-
314
-// bodyAllowedForStatus reports whether a given response status code
315
-// permits a body. See RFC 2616, section 4.4.
316
-func bodyAllowedForStatus(status int) bool {
317
-	switch {
318
-	case status >= 100 && status <= 199:
319
-		return false
320
-	case status == 204:
321
-		return false
322
-	case status == 304:
323
-		return false
324
-	}
325
-	return true
326
-}
327
-
328
-type httpError struct {
329
-	msg     string
330
-	timeout bool
331
-}
332
-
333
-func (e *httpError) Error() string   { return e.msg }
334
-func (e *httpError) Timeout() bool   { return e.timeout }
335
-func (e *httpError) Temporary() bool { return true }
336
-
337
-var errTimeout error = &httpError{msg: "http2: timeout awaiting response headers", timeout: true}
338
-
339
-type connectionStater interface {
340
-	ConnectionState() tls.ConnectionState
341
-}
342
-
343
-var sorterPool = sync.Pool{New: func() interface{} { return new(sorter) }}
344
-
345
-type sorter struct {
346
-	v []string // owned by sorter
347
-}
348
-
349
-func (s *sorter) Len() int           { return len(s.v) }
350
-func (s *sorter) Swap(i, j int)      { s.v[i], s.v[j] = s.v[j], s.v[i] }
351
-func (s *sorter) Less(i, j int) bool { return s.v[i] < s.v[j] }
352
-
353
-// Keys returns the sorted keys of h.
354
-//
355
-// The returned slice is only valid until s used again or returned to
356
-// its pool.
357
-func (s *sorter) Keys(h http.Header) []string {
358
-	keys := s.v[:0]
359
-	for k := range h {
360
-		keys = append(keys, k)
361
-	}
362
-	s.v = keys
363
-	sort.Sort(s)
364
-	return keys
365
-}
366
-
367
-func (s *sorter) SortStrings(ss []string) {
368
-	// Our sorter works on s.v, which sorter owns, so
369
-	// stash it away while we sort the user's buffer.
370
-	save := s.v
371
-	s.v = ss
372
-	sort.Sort(s)
373
-	s.v = save
374
-}
375
-
376
-// validPseudoPath reports whether v is a valid :path pseudo-header
377
-// value. It must be either:
378
-//
379
-//     *) a non-empty string starting with '/'
380
-//     *) the string '*', for OPTIONS requests.
381
-//
382
-// For now this is only used a quick check for deciding when to clean
383
-// up Opaque URLs before sending requests from the Transport.
384
-// See golang.org/issue/16847
385
-//
386
-// We used to enforce that the path also didn't start with "//", but
387
-// Google's GFE accepts such paths and Chrome sends them, so ignore
388
-// that part of the spec. See golang.org/issue/19103.
389
-func validPseudoPath(v string) bool {
390
-	return (len(v) > 0 && v[0] == '/') || v == "*"
391
-}

+ 0
- 199
vendor/golang.org/x/net/http2/http2_test.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"bytes"
9
-	"errors"
10
-	"flag"
11
-	"fmt"
12
-	"net/http"
13
-	"os/exec"
14
-	"strconv"
15
-	"strings"
16
-	"testing"
17
-
18
-	"golang.org/x/net/http2/hpack"
19
-)
20
-
21
-var knownFailing = flag.Bool("known_failing", false, "Run known-failing tests.")
22
-
23
-func condSkipFailingTest(t *testing.T) {
24
-	if !*knownFailing {
25
-		t.Skip("Skipping known-failing test without --known_failing")
26
-	}
27
-}
28
-
29
-func init() {
30
-	inTests = true
31
-	DebugGoroutines = true
32
-	flag.BoolVar(&VerboseLogs, "verboseh2", VerboseLogs, "Verbose HTTP/2 debug logging")
33
-}
34
-
35
-func TestSettingString(t *testing.T) {
36
-	tests := []struct {
37
-		s    Setting
38
-		want string
39
-	}{
40
-		{Setting{SettingMaxFrameSize, 123}, "[MAX_FRAME_SIZE = 123]"},
41
-		{Setting{1<<16 - 1, 123}, "[UNKNOWN_SETTING_65535 = 123]"},
42
-	}
43
-	for i, tt := range tests {
44
-		got := fmt.Sprint(tt.s)
45
-		if got != tt.want {
46
-			t.Errorf("%d. for %#v, string = %q; want %q", i, tt.s, got, tt.want)
47
-		}
48
-	}
49
-}
50
-
51
-type twriter struct {
52
-	t  testing.TB
53
-	st *serverTester // optional
54
-}
55
-
56
-func (w twriter) Write(p []byte) (n int, err error) {
57
-	if w.st != nil {
58
-		ps := string(p)
59
-		for _, phrase := range w.st.logFilter {
60
-			if strings.Contains(ps, phrase) {
61
-				return len(p), nil // no logging
62
-			}
63
-		}
64
-	}
65
-	w.t.Logf("%s", p)
66
-	return len(p), nil
67
-}
68
-
69
-// like encodeHeader, but don't add implicit pseudo headers.
70
-func encodeHeaderNoImplicit(t *testing.T, headers ...string) []byte {
71
-	var buf bytes.Buffer
72
-	enc := hpack.NewEncoder(&buf)
73
-	for len(headers) > 0 {
74
-		k, v := headers[0], headers[1]
75
-		headers = headers[2:]
76
-		if err := enc.WriteField(hpack.HeaderField{Name: k, Value: v}); err != nil {
77
-			t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err)
78
-		}
79
-	}
80
-	return buf.Bytes()
81
-}
82
-
83
-// Verify that curl has http2.
84
-func requireCurl(t *testing.T) {
85
-	out, err := dockerLogs(curl(t, "--version"))
86
-	if err != nil {
87
-		t.Skipf("failed to determine curl features; skipping test")
88
-	}
89
-	if !strings.Contains(string(out), "HTTP2") {
90
-		t.Skip("curl doesn't support HTTP2; skipping test")
91
-	}
92
-}
93
-
94
-func curl(t *testing.T, args ...string) (container string) {
95
-	out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "gohttp2/curl"}, args...)...).Output()
96
-	if err != nil {
97
-		t.Skipf("Failed to run curl in docker: %v, %s", err, out)
98
-	}
99
-	return strings.TrimSpace(string(out))
100
-}
101
-
102
-// Verify that h2load exists.
103
-func requireH2load(t *testing.T) {
104
-	out, err := dockerLogs(h2load(t, "--version"))
105
-	if err != nil {
106
-		t.Skipf("failed to probe h2load; skipping test: %s", out)
107
-	}
108
-	if !strings.Contains(string(out), "h2load nghttp2/") {
109
-		t.Skipf("h2load not present; skipping test. (Output=%q)", out)
110
-	}
111
-}
112
-
113
-func h2load(t *testing.T, args ...string) (container string) {
114
-	out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "--entrypoint=/usr/local/bin/h2load", "gohttp2/curl"}, args...)...).Output()
115
-	if err != nil {
116
-		t.Skipf("Failed to run h2load in docker: %v, %s", err, out)
117
-	}
118
-	return strings.TrimSpace(string(out))
119
-}
120
-
121
-type puppetCommand struct {
122
-	fn   func(w http.ResponseWriter, r *http.Request)
123
-	done chan<- bool
124
-}
125
-
126
-type handlerPuppet struct {
127
-	ch chan puppetCommand
128
-}
129
-
130
-func newHandlerPuppet() *handlerPuppet {
131
-	return &handlerPuppet{
132
-		ch: make(chan puppetCommand),
133
-	}
134
-}
135
-
136
-func (p *handlerPuppet) act(w http.ResponseWriter, r *http.Request) {
137
-	for cmd := range p.ch {
138
-		cmd.fn(w, r)
139
-		cmd.done <- true
140
-	}
141
-}
142
-
143
-func (p *handlerPuppet) done() { close(p.ch) }
144
-func (p *handlerPuppet) do(fn func(http.ResponseWriter, *http.Request)) {
145
-	done := make(chan bool)
146
-	p.ch <- puppetCommand{fn, done}
147
-	<-done
148
-}
149
-func dockerLogs(container string) ([]byte, error) {
150
-	out, err := exec.Command("docker", "wait", container).CombinedOutput()
151
-	if err != nil {
152
-		return out, err
153
-	}
154
-	exitStatus, err := strconv.Atoi(strings.TrimSpace(string(out)))
155
-	if err != nil {
156
-		return out, errors.New("unexpected exit status from docker wait")
157
-	}
158
-	out, err = exec.Command("docker", "logs", container).CombinedOutput()
159
-	exec.Command("docker", "rm", container).Run()
160
-	if err == nil && exitStatus != 0 {
161
-		err = fmt.Errorf("exit status %d: %s", exitStatus, out)
162
-	}
163
-	return out, err
164
-}
165
-
166
-func kill(container string) {
167
-	exec.Command("docker", "kill", container).Run()
168
-	exec.Command("docker", "rm", container).Run()
169
-}
170
-
171
-func cleanDate(res *http.Response) {
172
-	if d := res.Header["Date"]; len(d) == 1 {
173
-		d[0] = "XXX"
174
-	}
175
-}
176
-
177
-func TestSorterPoolAllocs(t *testing.T) {
178
-	ss := []string{"a", "b", "c"}
179
-	h := http.Header{
180
-		"a": nil,
181
-		"b": nil,
182
-		"c": nil,
183
-	}
184
-	sorter := new(sorter)
185
-
186
-	if allocs := testing.AllocsPerRun(100, func() {
187
-		sorter.SortStrings(ss)
188
-	}); allocs >= 1 {
189
-		t.Logf("SortStrings allocs = %v; want <1", allocs)
190
-	}
191
-
192
-	if allocs := testing.AllocsPerRun(5, func() {
193
-		if len(sorter.Keys(h)) != 3 {
194
-			t.Fatal("wrong result")
195
-		}
196
-	}); allocs > 0 {
197
-		t.Logf("Keys allocs = %v; want <1", allocs)
198
-	}
199
-}

+ 0
- 21
vendor/golang.org/x/net/http2/not_go16.go View File

1
-// Copyright 2015 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
-// +build !go1.6
6
-
7
-package http2
8
-
9
-import (
10
-	"net/http"
11
-	"time"
12
-)
13
-
14
-func configureTransport(t1 *http.Transport) (*Transport, error) {
15
-	return nil, errTransportVersion
16
-}
17
-
18
-func transportExpectContinueTimeout(t1 *http.Transport) time.Duration {
19
-	return 0
20
-
21
-}

+ 0
- 87
vendor/golang.org/x/net/http2/not_go17.go View File

1
-// Copyright 2016 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
-// +build !go1.7
6
-
7
-package http2
8
-
9
-import (
10
-	"crypto/tls"
11
-	"net"
12
-	"net/http"
13
-	"time"
14
-)
15
-
16
-type contextContext interface {
17
-	Done() <-chan struct{}
18
-	Err() error
19
-}
20
-
21
-type fakeContext struct{}
22
-
23
-func (fakeContext) Done() <-chan struct{} { return nil }
24
-func (fakeContext) Err() error            { panic("should not be called") }
25
-
26
-func reqContext(r *http.Request) fakeContext {
27
-	return fakeContext{}
28
-}
29
-
30
-func setResponseUncompressed(res *http.Response) {
31
-	// Nothing.
32
-}
33
-
34
-type clientTrace struct{}
35
-
36
-func requestTrace(*http.Request) *clientTrace { return nil }
37
-func traceGotConn(*http.Request, *ClientConn) {}
38
-func traceFirstResponseByte(*clientTrace)     {}
39
-func traceWroteHeaders(*clientTrace)          {}
40
-func traceWroteRequest(*clientTrace, error)   {}
41
-func traceGot100Continue(trace *clientTrace)  {}
42
-func traceWait100Continue(trace *clientTrace) {}
43
-
44
-func nop() {}
45
-
46
-func serverConnBaseContext(c net.Conn, opts *ServeConnOpts) (ctx contextContext, cancel func()) {
47
-	return nil, nop
48
-}
49
-
50
-func contextWithCancel(ctx contextContext) (_ contextContext, cancel func()) {
51
-	return ctx, nop
52
-}
53
-
54
-func requestWithContext(req *http.Request, ctx contextContext) *http.Request {
55
-	return req
56
-}
57
-
58
-// temporary copy of Go 1.6's private tls.Config.clone:
59
-func cloneTLSConfig(c *tls.Config) *tls.Config {
60
-	return &tls.Config{
61
-		Rand:                     c.Rand,
62
-		Time:                     c.Time,
63
-		Certificates:             c.Certificates,
64
-		NameToCertificate:        c.NameToCertificate,
65
-		GetCertificate:           c.GetCertificate,
66
-		RootCAs:                  c.RootCAs,
67
-		NextProtos:               c.NextProtos,
68
-		ServerName:               c.ServerName,
69
-		ClientAuth:               c.ClientAuth,
70
-		ClientCAs:                c.ClientCAs,
71
-		InsecureSkipVerify:       c.InsecureSkipVerify,
72
-		CipherSuites:             c.CipherSuites,
73
-		PreferServerCipherSuites: c.PreferServerCipherSuites,
74
-		SessionTicketsDisabled:   c.SessionTicketsDisabled,
75
-		SessionTicketKey:         c.SessionTicketKey,
76
-		ClientSessionCache:       c.ClientSessionCache,
77
-		MinVersion:               c.MinVersion,
78
-		MaxVersion:               c.MaxVersion,
79
-		CurvePreferences:         c.CurvePreferences,
80
-	}
81
-}
82
-
83
-func (cc *ClientConn) Ping(ctx contextContext) error {
84
-	return cc.ping(ctx)
85
-}
86
-
87
-func (t *Transport) idleConnTimeout() time.Duration { return 0 }

+ 0
- 29
vendor/golang.org/x/net/http2/not_go18.go View File

1
-// Copyright 2016 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
-// +build !go1.8
6
-
7
-package http2
8
-
9
-import (
10
-	"io"
11
-	"net/http"
12
-)
13
-
14
-func configureServer18(h1 *http.Server, h2 *Server) error {
15
-	// No IdleTimeout to sync prior to Go 1.8.
16
-	return nil
17
-}
18
-
19
-func shouldLogPanic(panicValue interface{}) bool {
20
-	return panicValue != nil
21
-}
22
-
23
-func reqGetBody(req *http.Request) func() (io.ReadCloser, error) {
24
-	return nil
25
-}
26
-
27
-func reqBodyIsNoBody(io.ReadCloser) bool { return false }
28
-
29
-func go18httpNoBody() io.ReadCloser { return nil } // for tests only

+ 0
- 16
vendor/golang.org/x/net/http2/not_go19.go View File

1
-// Copyright 2016 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
-// +build !go1.9
6
-
7
-package http2
8
-
9
-import (
10
-	"net/http"
11
-)
12
-
13
-func configureServer19(s *http.Server, conf *Server) error {
14
-	// not supported prior to go1.9
15
-	return nil
16
-}

+ 0
- 163
vendor/golang.org/x/net/http2/pipe.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"errors"
9
-	"io"
10
-	"sync"
11
-)
12
-
13
-// pipe is a goroutine-safe io.Reader/io.Writer pair. It's like
14
-// io.Pipe except there are no PipeReader/PipeWriter halves, and the
15
-// underlying buffer is an interface. (io.Pipe is always unbuffered)
16
-type pipe struct {
17
-	mu       sync.Mutex
18
-	c        sync.Cond     // c.L lazily initialized to &p.mu
19
-	b        pipeBuffer    // nil when done reading
20
-	err      error         // read error once empty. non-nil means closed.
21
-	breakErr error         // immediate read error (caller doesn't see rest of b)
22
-	donec    chan struct{} // closed on error
23
-	readFn   func()        // optional code to run in Read before error
24
-}
25
-
26
-type pipeBuffer interface {
27
-	Len() int
28
-	io.Writer
29
-	io.Reader
30
-}
31
-
32
-func (p *pipe) Len() int {
33
-	p.mu.Lock()
34
-	defer p.mu.Unlock()
35
-	if p.b == nil {
36
-		return 0
37
-	}
38
-	return p.b.Len()
39
-}
40
-
41
-// Read waits until data is available and copies bytes
42
-// from the buffer into p.
43
-func (p *pipe) Read(d []byte) (n int, err error) {
44
-	p.mu.Lock()
45
-	defer p.mu.Unlock()
46
-	if p.c.L == nil {
47
-		p.c.L = &p.mu
48
-	}
49
-	for {
50
-		if p.breakErr != nil {
51
-			return 0, p.breakErr
52
-		}
53
-		if p.b != nil && p.b.Len() > 0 {
54
-			return p.b.Read(d)
55
-		}
56
-		if p.err != nil {
57
-			if p.readFn != nil {
58
-				p.readFn()     // e.g. copy trailers
59
-				p.readFn = nil // not sticky like p.err
60
-			}
61
-			p.b = nil
62
-			return 0, p.err
63
-		}
64
-		p.c.Wait()
65
-	}
66
-}
67
-
68
-var errClosedPipeWrite = errors.New("write on closed buffer")
69
-
70
-// Write copies bytes from p into the buffer and wakes a reader.
71
-// It is an error to write more data than the buffer can hold.
72
-func (p *pipe) Write(d []byte) (n int, err error) {
73
-	p.mu.Lock()
74
-	defer p.mu.Unlock()
75
-	if p.c.L == nil {
76
-		p.c.L = &p.mu
77
-	}
78
-	defer p.c.Signal()
79
-	if p.err != nil {
80
-		return 0, errClosedPipeWrite
81
-	}
82
-	if p.breakErr != nil {
83
-		return len(d), nil // discard when there is no reader
84
-	}
85
-	return p.b.Write(d)
86
-}
87
-
88
-// CloseWithError causes the next Read (waking up a current blocked
89
-// Read if needed) to return the provided err after all data has been
90
-// read.
91
-//
92
-// The error must be non-nil.
93
-func (p *pipe) CloseWithError(err error) { p.closeWithError(&p.err, err, nil) }
94
-
95
-// BreakWithError causes the next Read (waking up a current blocked
96
-// Read if needed) to return the provided err immediately, without
97
-// waiting for unread data.
98
-func (p *pipe) BreakWithError(err error) { p.closeWithError(&p.breakErr, err, nil) }
99
-
100
-// closeWithErrorAndCode is like CloseWithError but also sets some code to run
101
-// in the caller's goroutine before returning the error.
102
-func (p *pipe) closeWithErrorAndCode(err error, fn func()) { p.closeWithError(&p.err, err, fn) }
103
-
104
-func (p *pipe) closeWithError(dst *error, err error, fn func()) {
105
-	if err == nil {
106
-		panic("err must be non-nil")
107
-	}
108
-	p.mu.Lock()
109
-	defer p.mu.Unlock()
110
-	if p.c.L == nil {
111
-		p.c.L = &p.mu
112
-	}
113
-	defer p.c.Signal()
114
-	if *dst != nil {
115
-		// Already been done.
116
-		return
117
-	}
118
-	p.readFn = fn
119
-	if dst == &p.breakErr {
120
-		p.b = nil
121
-	}
122
-	*dst = err
123
-	p.closeDoneLocked()
124
-}
125
-
126
-// requires p.mu be held.
127
-func (p *pipe) closeDoneLocked() {
128
-	if p.donec == nil {
129
-		return
130
-	}
131
-	// Close if unclosed. This isn't racy since we always
132
-	// hold p.mu while closing.
133
-	select {
134
-	case <-p.donec:
135
-	default:
136
-		close(p.donec)
137
-	}
138
-}
139
-
140
-// Err returns the error (if any) first set by BreakWithError or CloseWithError.
141
-func (p *pipe) Err() error {
142
-	p.mu.Lock()
143
-	defer p.mu.Unlock()
144
-	if p.breakErr != nil {
145
-		return p.breakErr
146
-	}
147
-	return p.err
148
-}
149
-
150
-// Done returns a channel which is closed if and when this pipe is closed
151
-// with CloseWithError.
152
-func (p *pipe) Done() <-chan struct{} {
153
-	p.mu.Lock()
154
-	defer p.mu.Unlock()
155
-	if p.donec == nil {
156
-		p.donec = make(chan struct{})
157
-		if p.err != nil || p.breakErr != nil {
158
-			// Already hit an error.
159
-			p.closeDoneLocked()
160
-		}
161
-	}
162
-	return p.donec
163
-}

+ 0
- 130
vendor/golang.org/x/net/http2/pipe_test.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"bytes"
9
-	"errors"
10
-	"io"
11
-	"io/ioutil"
12
-	"testing"
13
-)
14
-
15
-func TestPipeClose(t *testing.T) {
16
-	var p pipe
17
-	p.b = new(bytes.Buffer)
18
-	a := errors.New("a")
19
-	b := errors.New("b")
20
-	p.CloseWithError(a)
21
-	p.CloseWithError(b)
22
-	_, err := p.Read(make([]byte, 1))
23
-	if err != a {
24
-		t.Errorf("err = %v want %v", err, a)
25
-	}
26
-}
27
-
28
-func TestPipeDoneChan(t *testing.T) {
29
-	var p pipe
30
-	done := p.Done()
31
-	select {
32
-	case <-done:
33
-		t.Fatal("done too soon")
34
-	default:
35
-	}
36
-	p.CloseWithError(io.EOF)
37
-	select {
38
-	case <-done:
39
-	default:
40
-		t.Fatal("should be done")
41
-	}
42
-}
43
-
44
-func TestPipeDoneChan_ErrFirst(t *testing.T) {
45
-	var p pipe
46
-	p.CloseWithError(io.EOF)
47
-	done := p.Done()
48
-	select {
49
-	case <-done:
50
-	default:
51
-		t.Fatal("should be done")
52
-	}
53
-}
54
-
55
-func TestPipeDoneChan_Break(t *testing.T) {
56
-	var p pipe
57
-	done := p.Done()
58
-	select {
59
-	case <-done:
60
-		t.Fatal("done too soon")
61
-	default:
62
-	}
63
-	p.BreakWithError(io.EOF)
64
-	select {
65
-	case <-done:
66
-	default:
67
-		t.Fatal("should be done")
68
-	}
69
-}
70
-
71
-func TestPipeDoneChan_Break_ErrFirst(t *testing.T) {
72
-	var p pipe
73
-	p.BreakWithError(io.EOF)
74
-	done := p.Done()
75
-	select {
76
-	case <-done:
77
-	default:
78
-		t.Fatal("should be done")
79
-	}
80
-}
81
-
82
-func TestPipeCloseWithError(t *testing.T) {
83
-	p := &pipe{b: new(bytes.Buffer)}
84
-	const body = "foo"
85
-	io.WriteString(p, body)
86
-	a := errors.New("test error")
87
-	p.CloseWithError(a)
88
-	all, err := ioutil.ReadAll(p)
89
-	if string(all) != body {
90
-		t.Errorf("read bytes = %q; want %q", all, body)
91
-	}
92
-	if err != a {
93
-		t.Logf("read error = %v, %v", err, a)
94
-	}
95
-	// Read and Write should fail.
96
-	if n, err := p.Write([]byte("abc")); err != errClosedPipeWrite || n != 0 {
97
-		t.Errorf("Write(abc) after close\ngot %v, %v\nwant 0, %v", n, err, errClosedPipeWrite)
98
-	}
99
-	if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
100
-		t.Errorf("Read() after close\ngot %v, nil\nwant 0, %v", n, errClosedPipeWrite)
101
-	}
102
-}
103
-
104
-func TestPipeBreakWithError(t *testing.T) {
105
-	p := &pipe{b: new(bytes.Buffer)}
106
-	io.WriteString(p, "foo")
107
-	a := errors.New("test err")
108
-	p.BreakWithError(a)
109
-	all, err := ioutil.ReadAll(p)
110
-	if string(all) != "" {
111
-		t.Errorf("read bytes = %q; want empty string", all)
112
-	}
113
-	if err != a {
114
-		t.Logf("read error = %v, %v", err, a)
115
-	}
116
-	if p.b != nil {
117
-		t.Errorf("buffer should be nil after BreakWithError")
118
-	}
119
-	// Write should succeed silently.
120
-	if n, err := p.Write([]byte("abc")); err != nil || n != 3 {
121
-		t.Errorf("Write(abc) after break\ngot %v, %v\nwant 0, nil", n, err)
122
-	}
123
-	if p.b != nil {
124
-		t.Errorf("buffer should be nil after Write")
125
-	}
126
-	// Read should fail.
127
-	if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
128
-		t.Errorf("Read() after close\ngot %v, nil\nwant 0, not nil", n)
129
-	}
130
-}

+ 0
- 2857
vendor/golang.org/x/net/http2/server.go
File diff suppressed because it is too large
View File


+ 0
- 521
vendor/golang.org/x/net/http2/server_push_test.go View File

1
-// Copyright 2016 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
-// +build go1.8
6
-
7
-package http2
8
-
9
-import (
10
-	"errors"
11
-	"fmt"
12
-	"io"
13
-	"io/ioutil"
14
-	"net/http"
15
-	"reflect"
16
-	"strconv"
17
-	"sync"
18
-	"testing"
19
-	"time"
20
-)
21
-
22
-func TestServer_Push_Success(t *testing.T) {
23
-	const (
24
-		mainBody   = "<html>index page</html>"
25
-		pushedBody = "<html>pushed page</html>"
26
-		userAgent  = "testagent"
27
-		cookie     = "testcookie"
28
-	)
29
-
30
-	var stURL string
31
-	checkPromisedReq := func(r *http.Request, wantMethod string, wantH http.Header) error {
32
-		if got, want := r.Method, wantMethod; got != want {
33
-			return fmt.Errorf("promised Req.Method=%q, want %q", got, want)
34
-		}
35
-		if got, want := r.Header, wantH; !reflect.DeepEqual(got, want) {
36
-			return fmt.Errorf("promised Req.Header=%q, want %q", got, want)
37
-		}
38
-		if got, want := "https://"+r.Host, stURL; got != want {
39
-			return fmt.Errorf("promised Req.Host=%q, want %q", got, want)
40
-		}
41
-		if r.Body == nil {
42
-			return fmt.Errorf("nil Body")
43
-		}
44
-		if buf, err := ioutil.ReadAll(r.Body); err != nil || len(buf) != 0 {
45
-			return fmt.Errorf("ReadAll(Body)=%q,%v, want '',nil", buf, err)
46
-		}
47
-		return nil
48
-	}
49
-
50
-	errc := make(chan error, 3)
51
-	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
52
-		switch r.URL.RequestURI() {
53
-		case "/":
54
-			// Push "/pushed?get" as a GET request, using an absolute URL.
55
-			opt := &http.PushOptions{
56
-				Header: http.Header{
57
-					"User-Agent": {userAgent},
58
-				},
59
-			}
60
-			if err := w.(http.Pusher).Push(stURL+"/pushed?get", opt); err != nil {
61
-				errc <- fmt.Errorf("error pushing /pushed?get: %v", err)
62
-				return
63
-			}
64
-			// Push "/pushed?head" as a HEAD request, using a path.
65
-			opt = &http.PushOptions{
66
-				Method: "HEAD",
67
-				Header: http.Header{
68
-					"User-Agent": {userAgent},
69
-					"Cookie":     {cookie},
70
-				},
71
-			}
72
-			if err := w.(http.Pusher).Push("/pushed?head", opt); err != nil {
73
-				errc <- fmt.Errorf("error pushing /pushed?head: %v", err)
74
-				return
75
-			}
76
-			w.Header().Set("Content-Type", "text/html")
77
-			w.Header().Set("Content-Length", strconv.Itoa(len(mainBody)))
78
-			w.WriteHeader(200)
79
-			io.WriteString(w, mainBody)
80
-			errc <- nil
81
-
82
-		case "/pushed?get":
83
-			wantH := http.Header{}
84
-			wantH.Set("User-Agent", userAgent)
85
-			if err := checkPromisedReq(r, "GET", wantH); err != nil {
86
-				errc <- fmt.Errorf("/pushed?get: %v", err)
87
-				return
88
-			}
89
-			w.Header().Set("Content-Type", "text/html")
90
-			w.Header().Set("Content-Length", strconv.Itoa(len(pushedBody)))
91
-			w.WriteHeader(200)
92
-			io.WriteString(w, pushedBody)
93
-			errc <- nil
94
-
95
-		case "/pushed?head":
96
-			wantH := http.Header{}
97
-			wantH.Set("User-Agent", userAgent)
98
-			wantH.Set("Cookie", cookie)
99
-			if err := checkPromisedReq(r, "HEAD", wantH); err != nil {
100
-				errc <- fmt.Errorf("/pushed?head: %v", err)
101
-				return
102
-			}
103
-			w.WriteHeader(204)
104
-			errc <- nil
105
-
106
-		default:
107
-			errc <- fmt.Errorf("unknown RequestURL %q", r.URL.RequestURI())
108
-		}
109
-	})
110
-	stURL = st.ts.URL
111
-
112
-	// Send one request, which should push two responses.
113
-	st.greet()
114
-	getSlash(st)
115
-	for k := 0; k < 3; k++ {
116
-		select {
117
-		case <-time.After(2 * time.Second):
118
-			t.Errorf("timeout waiting for handler %d to finish", k)
119
-		case err := <-errc:
120
-			if err != nil {
121
-				t.Fatal(err)
122
-			}
123
-		}
124
-	}
125
-
126
-	checkPushPromise := func(f Frame, promiseID uint32, wantH [][2]string) error {
127
-		pp, ok := f.(*PushPromiseFrame)
128
-		if !ok {
129
-			return fmt.Errorf("got a %T; want *PushPromiseFrame", f)
130
-		}
131
-		if !pp.HeadersEnded() {
132
-			return fmt.Errorf("want END_HEADERS flag in PushPromiseFrame")
133
-		}
134
-		if got, want := pp.PromiseID, promiseID; got != want {
135
-			return fmt.Errorf("got PromiseID %v; want %v", got, want)
136
-		}
137
-		gotH := st.decodeHeader(pp.HeaderBlockFragment())
138
-		if !reflect.DeepEqual(gotH, wantH) {
139
-			return fmt.Errorf("got promised headers %v; want %v", gotH, wantH)
140
-		}
141
-		return nil
142
-	}
143
-	checkHeaders := func(f Frame, wantH [][2]string) error {
144
-		hf, ok := f.(*HeadersFrame)
145
-		if !ok {
146
-			return fmt.Errorf("got a %T; want *HeadersFrame", f)
147
-		}
148
-		gotH := st.decodeHeader(hf.HeaderBlockFragment())
149
-		if !reflect.DeepEqual(gotH, wantH) {
150
-			return fmt.Errorf("got response headers %v; want %v", gotH, wantH)
151
-		}
152
-		return nil
153
-	}
154
-	checkData := func(f Frame, wantData string) error {
155
-		df, ok := f.(*DataFrame)
156
-		if !ok {
157
-			return fmt.Errorf("got a %T; want *DataFrame", f)
158
-		}
159
-		if gotData := string(df.Data()); gotData != wantData {
160
-			return fmt.Errorf("got response data %q; want %q", gotData, wantData)
161
-		}
162
-		return nil
163
-	}
164
-
165
-	// Stream 1 has 2 PUSH_PROMISE + HEADERS + DATA
166
-	// Stream 2 has HEADERS + DATA
167
-	// Stream 4 has HEADERS
168
-	expected := map[uint32][]func(Frame) error{
169
-		1: {
170
-			func(f Frame) error {
171
-				return checkPushPromise(f, 2, [][2]string{
172
-					{":method", "GET"},
173
-					{":scheme", "https"},
174
-					{":authority", st.ts.Listener.Addr().String()},
175
-					{":path", "/pushed?get"},
176
-					{"user-agent", userAgent},
177
-				})
178
-			},
179
-			func(f Frame) error {
180
-				return checkPushPromise(f, 4, [][2]string{
181
-					{":method", "HEAD"},
182
-					{":scheme", "https"},
183
-					{":authority", st.ts.Listener.Addr().String()},
184
-					{":path", "/pushed?head"},
185
-					{"cookie", cookie},
186
-					{"user-agent", userAgent},
187
-				})
188
-			},
189
-			func(f Frame) error {
190
-				return checkHeaders(f, [][2]string{
191
-					{":status", "200"},
192
-					{"content-type", "text/html"},
193
-					{"content-length", strconv.Itoa(len(mainBody))},
194
-				})
195
-			},
196
-			func(f Frame) error {
197
-				return checkData(f, mainBody)
198
-			},
199
-		},
200
-		2: {
201
-			func(f Frame) error {
202
-				return checkHeaders(f, [][2]string{
203
-					{":status", "200"},
204
-					{"content-type", "text/html"},
205
-					{"content-length", strconv.Itoa(len(pushedBody))},
206
-				})
207
-			},
208
-			func(f Frame) error {
209
-				return checkData(f, pushedBody)
210
-			},
211
-		},
212
-		4: {
213
-			func(f Frame) error {
214
-				return checkHeaders(f, [][2]string{
215
-					{":status", "204"},
216
-				})
217
-			},
218
-		},
219
-	}
220
-
221
-	consumed := map[uint32]int{}
222
-	for k := 0; len(expected) > 0; k++ {
223
-		f, err := st.readFrame()
224
-		if err != nil {
225
-			for id, left := range expected {
226
-				t.Errorf("stream %d: missing %d frames", id, len(left))
227
-			}
228
-			t.Fatalf("readFrame %d: %v", k, err)
229
-		}
230
-		id := f.Header().StreamID
231
-		label := fmt.Sprintf("stream %d, frame %d", id, consumed[id])
232
-		if len(expected[id]) == 0 {
233
-			t.Fatalf("%s: unexpected frame %#+v", label, f)
234
-		}
235
-		check := expected[id][0]
236
-		expected[id] = expected[id][1:]
237
-		if len(expected[id]) == 0 {
238
-			delete(expected, id)
239
-		}
240
-		if err := check(f); err != nil {
241
-			t.Fatalf("%s: %v", label, err)
242
-		}
243
-		consumed[id]++
244
-	}
245
-}
246
-
247
-func TestServer_Push_SuccessNoRace(t *testing.T) {
248
-	// Regression test for issue #18326. Ensure the request handler can mutate
249
-	// pushed request headers without racing with the PUSH_PROMISE write.
250
-	errc := make(chan error, 2)
251
-	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
252
-		switch r.URL.RequestURI() {
253
-		case "/":
254
-			opt := &http.PushOptions{
255
-				Header: http.Header{"User-Agent": {"testagent"}},
256
-			}
257
-			if err := w.(http.Pusher).Push("/pushed", opt); err != nil {
258
-				errc <- fmt.Errorf("error pushing: %v", err)
259
-				return
260
-			}
261
-			w.WriteHeader(200)
262
-			errc <- nil
263
-
264
-		case "/pushed":
265
-			// Update request header, ensure there is no race.
266
-			r.Header.Set("User-Agent", "newagent")
267
-			r.Header.Set("Cookie", "cookie")
268
-			w.WriteHeader(200)
269
-			errc <- nil
270
-
271
-		default:
272
-			errc <- fmt.Errorf("unknown RequestURL %q", r.URL.RequestURI())
273
-		}
274
-	})
275
-
276
-	// Send one request, which should push one response.
277
-	st.greet()
278
-	getSlash(st)
279
-	for k := 0; k < 2; k++ {
280
-		select {
281
-		case <-time.After(2 * time.Second):
282
-			t.Errorf("timeout waiting for handler %d to finish", k)
283
-		case err := <-errc:
284
-			if err != nil {
285
-				t.Fatal(err)
286
-			}
287
-		}
288
-	}
289
-}
290
-
291
-func TestServer_Push_RejectRecursivePush(t *testing.T) {
292
-	// Expect two requests, but might get three if there's a bug and the second push succeeds.
293
-	errc := make(chan error, 3)
294
-	handler := func(w http.ResponseWriter, r *http.Request) error {
295
-		baseURL := "https://" + r.Host
296
-		switch r.URL.Path {
297
-		case "/":
298
-			if err := w.(http.Pusher).Push(baseURL+"/push1", nil); err != nil {
299
-				return fmt.Errorf("first Push()=%v, want nil", err)
300
-			}
301
-			return nil
302
-
303
-		case "/push1":
304
-			if got, want := w.(http.Pusher).Push(baseURL+"/push2", nil), ErrRecursivePush; got != want {
305
-				return fmt.Errorf("Push()=%v, want %v", got, want)
306
-			}
307
-			return nil
308
-
309
-		default:
310
-			return fmt.Errorf("unexpected path: %q", r.URL.Path)
311
-		}
312
-	}
313
-	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
314
-		errc <- handler(w, r)
315
-	})
316
-	defer st.Close()
317
-	st.greet()
318
-	getSlash(st)
319
-	if err := <-errc; err != nil {
320
-		t.Errorf("First request failed: %v", err)
321
-	}
322
-	if err := <-errc; err != nil {
323
-		t.Errorf("Second request failed: %v", err)
324
-	}
325
-}
326
-
327
-func testServer_Push_RejectSingleRequest(t *testing.T, doPush func(http.Pusher, *http.Request) error, settings ...Setting) {
328
-	// Expect one request, but might get two if there's a bug and the push succeeds.
329
-	errc := make(chan error, 2)
330
-	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
331
-		errc <- doPush(w.(http.Pusher), r)
332
-	})
333
-	defer st.Close()
334
-	st.greet()
335
-	if err := st.fr.WriteSettings(settings...); err != nil {
336
-		st.t.Fatalf("WriteSettings: %v", err)
337
-	}
338
-	st.wantSettingsAck()
339
-	getSlash(st)
340
-	if err := <-errc; err != nil {
341
-		t.Error(err)
342
-	}
343
-	// Should not get a PUSH_PROMISE frame.
344
-	hf := st.wantHeaders()
345
-	if !hf.StreamEnded() {
346
-		t.Error("stream should end after headers")
347
-	}
348
-}
349
-
350
-func TestServer_Push_RejectIfDisabled(t *testing.T) {
351
-	testServer_Push_RejectSingleRequest(t,
352
-		func(p http.Pusher, r *http.Request) error {
353
-			if got, want := p.Push("https://"+r.Host+"/pushed", nil), http.ErrNotSupported; got != want {
354
-				return fmt.Errorf("Push()=%v, want %v", got, want)
355
-			}
356
-			return nil
357
-		},
358
-		Setting{SettingEnablePush, 0})
359
-}
360
-
361
-func TestServer_Push_RejectWhenNoConcurrentStreams(t *testing.T) {
362
-	testServer_Push_RejectSingleRequest(t,
363
-		func(p http.Pusher, r *http.Request) error {
364
-			if got, want := p.Push("https://"+r.Host+"/pushed", nil), ErrPushLimitReached; got != want {
365
-				return fmt.Errorf("Push()=%v, want %v", got, want)
366
-			}
367
-			return nil
368
-		},
369
-		Setting{SettingMaxConcurrentStreams, 0})
370
-}
371
-
372
-func TestServer_Push_RejectWrongScheme(t *testing.T) {
373
-	testServer_Push_RejectSingleRequest(t,
374
-		func(p http.Pusher, r *http.Request) error {
375
-			if err := p.Push("http://"+r.Host+"/pushed", nil); err == nil {
376
-				return errors.New("Push() should have failed (push target URL is http)")
377
-			}
378
-			return nil
379
-		})
380
-}
381
-
382
-func TestServer_Push_RejectMissingHost(t *testing.T) {
383
-	testServer_Push_RejectSingleRequest(t,
384
-		func(p http.Pusher, r *http.Request) error {
385
-			if err := p.Push("https:pushed", nil); err == nil {
386
-				return errors.New("Push() should have failed (push target URL missing host)")
387
-			}
388
-			return nil
389
-		})
390
-}
391
-
392
-func TestServer_Push_RejectRelativePath(t *testing.T) {
393
-	testServer_Push_RejectSingleRequest(t,
394
-		func(p http.Pusher, r *http.Request) error {
395
-			if err := p.Push("../test", nil); err == nil {
396
-				return errors.New("Push() should have failed (push target is a relative path)")
397
-			}
398
-			return nil
399
-		})
400
-}
401
-
402
-func TestServer_Push_RejectForbiddenMethod(t *testing.T) {
403
-	testServer_Push_RejectSingleRequest(t,
404
-		func(p http.Pusher, r *http.Request) error {
405
-			if err := p.Push("https://"+r.Host+"/pushed", &http.PushOptions{Method: "POST"}); err == nil {
406
-				return errors.New("Push() should have failed (cannot promise a POST)")
407
-			}
408
-			return nil
409
-		})
410
-}
411
-
412
-func TestServer_Push_RejectForbiddenHeader(t *testing.T) {
413
-	testServer_Push_RejectSingleRequest(t,
414
-		func(p http.Pusher, r *http.Request) error {
415
-			header := http.Header{
416
-				"Content-Length":   {"10"},
417
-				"Content-Encoding": {"gzip"},
418
-				"Trailer":          {"Foo"},
419
-				"Te":               {"trailers"},
420
-				"Host":             {"test.com"},
421
-				":authority":       {"test.com"},
422
-			}
423
-			if err := p.Push("https://"+r.Host+"/pushed", &http.PushOptions{Header: header}); err == nil {
424
-				return errors.New("Push() should have failed (forbidden headers)")
425
-			}
426
-			return nil
427
-		})
428
-}
429
-
430
-func TestServer_Push_StateTransitions(t *testing.T) {
431
-	const body = "foo"
432
-
433
-	gotPromise := make(chan bool)
434
-	finishedPush := make(chan bool)
435
-
436
-	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
437
-		switch r.URL.RequestURI() {
438
-		case "/":
439
-			if err := w.(http.Pusher).Push("/pushed", nil); err != nil {
440
-				t.Errorf("Push error: %v", err)
441
-			}
442
-			// Don't finish this request until the push finishes so we don't
443
-			// nondeterministically interleave output frames with the push.
444
-			<-finishedPush
445
-		case "/pushed":
446
-			<-gotPromise
447
-		}
448
-		w.Header().Set("Content-Type", "text/html")
449
-		w.Header().Set("Content-Length", strconv.Itoa(len(body)))
450
-		w.WriteHeader(200)
451
-		io.WriteString(w, body)
452
-	})
453
-	defer st.Close()
454
-
455
-	st.greet()
456
-	if st.stream(2) != nil {
457
-		t.Fatal("stream 2 should be empty")
458
-	}
459
-	if got, want := st.streamState(2), stateIdle; got != want {
460
-		t.Fatalf("streamState(2)=%v, want %v", got, want)
461
-	}
462
-	getSlash(st)
463
-	// After the PUSH_PROMISE is sent, the stream should be stateHalfClosedRemote.
464
-	st.wantPushPromise()
465
-	if got, want := st.streamState(2), stateHalfClosedRemote; got != want {
466
-		t.Fatalf("streamState(2)=%v, want %v", got, want)
467
-	}
468
-	// We stall the HTTP handler for "/pushed" until the above check. If we don't
469
-	// stall the handler, then the handler might write HEADERS and DATA and finish
470
-	// the stream before we check st.streamState(2) -- should that happen, we'll
471
-	// see stateClosed and fail the above check.
472
-	close(gotPromise)
473
-	st.wantHeaders()
474
-	if df := st.wantData(); !df.StreamEnded() {
475
-		t.Fatal("expected END_STREAM flag on DATA")
476
-	}
477
-	if got, want := st.streamState(2), stateClosed; got != want {
478
-		t.Fatalf("streamState(2)=%v, want %v", got, want)
479
-	}
480
-	close(finishedPush)
481
-}
482
-
483
-func TestServer_Push_RejectAfterGoAway(t *testing.T) {
484
-	var readyOnce sync.Once
485
-	ready := make(chan struct{})
486
-	errc := make(chan error, 2)
487
-	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
488
-		select {
489
-		case <-ready:
490
-		case <-time.After(5 * time.Second):
491
-			errc <- fmt.Errorf("timeout waiting for GOAWAY to be processed")
492
-		}
493
-		if got, want := w.(http.Pusher).Push("https://"+r.Host+"/pushed", nil), http.ErrNotSupported; got != want {
494
-			errc <- fmt.Errorf("Push()=%v, want %v", got, want)
495
-		}
496
-		errc <- nil
497
-	})
498
-	defer st.Close()
499
-	st.greet()
500
-	getSlash(st)
501
-
502
-	// Send GOAWAY and wait for it to be processed.
503
-	st.fr.WriteGoAway(1, ErrCodeNo, nil)
504
-	go func() {
505
-		for {
506
-			select {
507
-			case <-ready:
508
-				return
509
-			default:
510
-			}
511
-			st.sc.serveMsgCh <- func(loopNum int) {
512
-				if !st.sc.pushEnabled {
513
-					readyOnce.Do(func() { close(ready) })
514
-				}
515
-			}
516
-		}
517
-	}()
518
-	if err := <-errc; err != nil {
519
-		t.Error(err)
520
-	}
521
-}

+ 0
- 3721
vendor/golang.org/x/net/http2/server_test.go
File diff suppressed because it is too large
View File


+ 0
- 5021
vendor/golang.org/x/net/http2/testdata/draft-ietf-httpbis-http2.xml
File diff suppressed because it is too large
View File


+ 0
- 2221
vendor/golang.org/x/net/http2/transport.go
File diff suppressed because it is too large
View File


+ 0
- 3320
vendor/golang.org/x/net/http2/transport_test.go
File diff suppressed because it is too large
View File


+ 0
- 370
vendor/golang.org/x/net/http2/write.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import (
8
-	"bytes"
9
-	"fmt"
10
-	"log"
11
-	"net/http"
12
-	"net/url"
13
-	"time"
14
-
15
-	"golang.org/x/net/http2/hpack"
16
-	"golang.org/x/net/lex/httplex"
17
-)
18
-
19
-// writeFramer is implemented by any type that is used to write frames.
20
-type writeFramer interface {
21
-	writeFrame(writeContext) error
22
-
23
-	// staysWithinBuffer reports whether this writer promises that
24
-	// it will only write less than or equal to size bytes, and it
25
-	// won't Flush the write context.
26
-	staysWithinBuffer(size int) bool
27
-}
28
-
29
-// writeContext is the interface needed by the various frame writer
30
-// types below. All the writeFrame methods below are scheduled via the
31
-// frame writing scheduler (see writeScheduler in writesched.go).
32
-//
33
-// This interface is implemented by *serverConn.
34
-//
35
-// TODO: decide whether to a) use this in the client code (which didn't
36
-// end up using this yet, because it has a simpler design, not
37
-// currently implementing priorities), or b) delete this and
38
-// make the server code a bit more concrete.
39
-type writeContext interface {
40
-	Framer() *Framer
41
-	Flush() error
42
-	CloseConn() error
43
-	// HeaderEncoder returns an HPACK encoder that writes to the
44
-	// returned buffer.
45
-	HeaderEncoder() (*hpack.Encoder, *bytes.Buffer)
46
-}
47
-
48
-// writeEndsStream reports whether w writes a frame that will transition
49
-// the stream to a half-closed local state. This returns false for RST_STREAM,
50
-// which closes the entire stream (not just the local half).
51
-func writeEndsStream(w writeFramer) bool {
52
-	switch v := w.(type) {
53
-	case *writeData:
54
-		return v.endStream
55
-	case *writeResHeaders:
56
-		return v.endStream
57
-	case nil:
58
-		// This can only happen if the caller reuses w after it's
59
-		// been intentionally nil'ed out to prevent use. Keep this
60
-		// here to catch future refactoring breaking it.
61
-		panic("writeEndsStream called on nil writeFramer")
62
-	}
63
-	return false
64
-}
65
-
66
-type flushFrameWriter struct{}
67
-
68
-func (flushFrameWriter) writeFrame(ctx writeContext) error {
69
-	return ctx.Flush()
70
-}
71
-
72
-func (flushFrameWriter) staysWithinBuffer(max int) bool { return false }
73
-
74
-type writeSettings []Setting
75
-
76
-func (s writeSettings) staysWithinBuffer(max int) bool {
77
-	const settingSize = 6 // uint16 + uint32
78
-	return frameHeaderLen+settingSize*len(s) <= max
79
-
80
-}
81
-
82
-func (s writeSettings) writeFrame(ctx writeContext) error {
83
-	return ctx.Framer().WriteSettings([]Setting(s)...)
84
-}
85
-
86
-type writeGoAway struct {
87
-	maxStreamID uint32
88
-	code        ErrCode
89
-}
90
-
91
-func (p *writeGoAway) writeFrame(ctx writeContext) error {
92
-	err := ctx.Framer().WriteGoAway(p.maxStreamID, p.code, nil)
93
-	if p.code != 0 {
94
-		ctx.Flush() // ignore error: we're hanging up on them anyway
95
-		time.Sleep(50 * time.Millisecond)
96
-		ctx.CloseConn()
97
-	}
98
-	return err
99
-}
100
-
101
-func (*writeGoAway) staysWithinBuffer(max int) bool { return false } // flushes
102
-
103
-type writeData struct {
104
-	streamID  uint32
105
-	p         []byte
106
-	endStream bool
107
-}
108
-
109
-func (w *writeData) String() string {
110
-	return fmt.Sprintf("writeData(stream=%d, p=%d, endStream=%v)", w.streamID, len(w.p), w.endStream)
111
-}
112
-
113
-func (w *writeData) writeFrame(ctx writeContext) error {
114
-	return ctx.Framer().WriteData(w.streamID, w.endStream, w.p)
115
-}
116
-
117
-func (w *writeData) staysWithinBuffer(max int) bool {
118
-	return frameHeaderLen+len(w.p) <= max
119
-}
120
-
121
-// handlerPanicRST is the message sent from handler goroutines when
122
-// the handler panics.
123
-type handlerPanicRST struct {
124
-	StreamID uint32
125
-}
126
-
127
-func (hp handlerPanicRST) writeFrame(ctx writeContext) error {
128
-	return ctx.Framer().WriteRSTStream(hp.StreamID, ErrCodeInternal)
129
-}
130
-
131
-func (hp handlerPanicRST) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
132
-
133
-func (se StreamError) writeFrame(ctx writeContext) error {
134
-	return ctx.Framer().WriteRSTStream(se.StreamID, se.Code)
135
-}
136
-
137
-func (se StreamError) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
138
-
139
-type writePingAck struct{ pf *PingFrame }
140
-
141
-func (w writePingAck) writeFrame(ctx writeContext) error {
142
-	return ctx.Framer().WritePing(true, w.pf.Data)
143
-}
144
-
145
-func (w writePingAck) staysWithinBuffer(max int) bool { return frameHeaderLen+len(w.pf.Data) <= max }
146
-
147
-type writeSettingsAck struct{}
148
-
149
-func (writeSettingsAck) writeFrame(ctx writeContext) error {
150
-	return ctx.Framer().WriteSettingsAck()
151
-}
152
-
153
-func (writeSettingsAck) staysWithinBuffer(max int) bool { return frameHeaderLen <= max }
154
-
155
-// splitHeaderBlock splits headerBlock into fragments so that each fragment fits
156
-// in a single frame, then calls fn for each fragment. firstFrag/lastFrag are true
157
-// for the first/last fragment, respectively.
158
-func splitHeaderBlock(ctx writeContext, headerBlock []byte, fn func(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error) error {
159
-	// For now we're lazy and just pick the minimum MAX_FRAME_SIZE
160
-	// that all peers must support (16KB). Later we could care
161
-	// more and send larger frames if the peer advertised it, but
162
-	// there's little point. Most headers are small anyway (so we
163
-	// generally won't have CONTINUATION frames), and extra frames
164
-	// only waste 9 bytes anyway.
165
-	const maxFrameSize = 16384
166
-
167
-	first := true
168
-	for len(headerBlock) > 0 {
169
-		frag := headerBlock
170
-		if len(frag) > maxFrameSize {
171
-			frag = frag[:maxFrameSize]
172
-		}
173
-		headerBlock = headerBlock[len(frag):]
174
-		if err := fn(ctx, frag, first, len(headerBlock) == 0); err != nil {
175
-			return err
176
-		}
177
-		first = false
178
-	}
179
-	return nil
180
-}
181
-
182
-// writeResHeaders is a request to write a HEADERS and 0+ CONTINUATION frames
183
-// for HTTP response headers or trailers from a server handler.
184
-type writeResHeaders struct {
185
-	streamID    uint32
186
-	httpResCode int         // 0 means no ":status" line
187
-	h           http.Header // may be nil
188
-	trailers    []string    // if non-nil, which keys of h to write. nil means all.
189
-	endStream   bool
190
-
191
-	date          string
192
-	contentType   string
193
-	contentLength string
194
-}
195
-
196
-func encKV(enc *hpack.Encoder, k, v string) {
197
-	if VerboseLogs {
198
-		log.Printf("http2: server encoding header %q = %q", k, v)
199
-	}
200
-	enc.WriteField(hpack.HeaderField{Name: k, Value: v})
201
-}
202
-
203
-func (w *writeResHeaders) staysWithinBuffer(max int) bool {
204
-	// TODO: this is a common one. It'd be nice to return true
205
-	// here and get into the fast path if we could be clever and
206
-	// calculate the size fast enough, or at least a conservative
207
-	// uppper bound that usually fires. (Maybe if w.h and
208
-	// w.trailers are nil, so we don't need to enumerate it.)
209
-	// Otherwise I'm afraid that just calculating the length to
210
-	// answer this question would be slower than the ~2µs benefit.
211
-	return false
212
-}
213
-
214
-func (w *writeResHeaders) writeFrame(ctx writeContext) error {
215
-	enc, buf := ctx.HeaderEncoder()
216
-	buf.Reset()
217
-
218
-	if w.httpResCode != 0 {
219
-		encKV(enc, ":status", httpCodeString(w.httpResCode))
220
-	}
221
-
222
-	encodeHeaders(enc, w.h, w.trailers)
223
-
224
-	if w.contentType != "" {
225
-		encKV(enc, "content-type", w.contentType)
226
-	}
227
-	if w.contentLength != "" {
228
-		encKV(enc, "content-length", w.contentLength)
229
-	}
230
-	if w.date != "" {
231
-		encKV(enc, "date", w.date)
232
-	}
233
-
234
-	headerBlock := buf.Bytes()
235
-	if len(headerBlock) == 0 && w.trailers == nil {
236
-		panic("unexpected empty hpack")
237
-	}
238
-
239
-	return splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
240
-}
241
-
242
-func (w *writeResHeaders) writeHeaderBlock(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error {
243
-	if firstFrag {
244
-		return ctx.Framer().WriteHeaders(HeadersFrameParam{
245
-			StreamID:      w.streamID,
246
-			BlockFragment: frag,
247
-			EndStream:     w.endStream,
248
-			EndHeaders:    lastFrag,
249
-		})
250
-	} else {
251
-		return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
252
-	}
253
-}
254
-
255
-// writePushPromise is a request to write a PUSH_PROMISE and 0+ CONTINUATION frames.
256
-type writePushPromise struct {
257
-	streamID uint32   // pusher stream
258
-	method   string   // for :method
259
-	url      *url.URL // for :scheme, :authority, :path
260
-	h        http.Header
261
-
262
-	// Creates an ID for a pushed stream. This runs on serveG just before
263
-	// the frame is written. The returned ID is copied to promisedID.
264
-	allocatePromisedID func() (uint32, error)
265
-	promisedID         uint32
266
-}
267
-
268
-func (w *writePushPromise) staysWithinBuffer(max int) bool {
269
-	// TODO: see writeResHeaders.staysWithinBuffer
270
-	return false
271
-}
272
-
273
-func (w *writePushPromise) writeFrame(ctx writeContext) error {
274
-	enc, buf := ctx.HeaderEncoder()
275
-	buf.Reset()
276
-
277
-	encKV(enc, ":method", w.method)
278
-	encKV(enc, ":scheme", w.url.Scheme)
279
-	encKV(enc, ":authority", w.url.Host)
280
-	encKV(enc, ":path", w.url.RequestURI())
281
-	encodeHeaders(enc, w.h, nil)
282
-
283
-	headerBlock := buf.Bytes()
284
-	if len(headerBlock) == 0 {
285
-		panic("unexpected empty hpack")
286
-	}
287
-
288
-	return splitHeaderBlock(ctx, headerBlock, w.writeHeaderBlock)
289
-}
290
-
291
-func (w *writePushPromise) writeHeaderBlock(ctx writeContext, frag []byte, firstFrag, lastFrag bool) error {
292
-	if firstFrag {
293
-		return ctx.Framer().WritePushPromise(PushPromiseParam{
294
-			StreamID:      w.streamID,
295
-			PromiseID:     w.promisedID,
296
-			BlockFragment: frag,
297
-			EndHeaders:    lastFrag,
298
-		})
299
-	} else {
300
-		return ctx.Framer().WriteContinuation(w.streamID, lastFrag, frag)
301
-	}
302
-}
303
-
304
-type write100ContinueHeadersFrame struct {
305
-	streamID uint32
306
-}
307
-
308
-func (w write100ContinueHeadersFrame) writeFrame(ctx writeContext) error {
309
-	enc, buf := ctx.HeaderEncoder()
310
-	buf.Reset()
311
-	encKV(enc, ":status", "100")
312
-	return ctx.Framer().WriteHeaders(HeadersFrameParam{
313
-		StreamID:      w.streamID,
314
-		BlockFragment: buf.Bytes(),
315
-		EndStream:     false,
316
-		EndHeaders:    true,
317
-	})
318
-}
319
-
320
-func (w write100ContinueHeadersFrame) staysWithinBuffer(max int) bool {
321
-	// Sloppy but conservative:
322
-	return 9+2*(len(":status")+len("100")) <= max
323
-}
324
-
325
-type writeWindowUpdate struct {
326
-	streamID uint32 // or 0 for conn-level
327
-	n        uint32
328
-}
329
-
330
-func (wu writeWindowUpdate) staysWithinBuffer(max int) bool { return frameHeaderLen+4 <= max }
331
-
332
-func (wu writeWindowUpdate) writeFrame(ctx writeContext) error {
333
-	return ctx.Framer().WriteWindowUpdate(wu.streamID, wu.n)
334
-}
335
-
336
-// encodeHeaders encodes an http.Header. If keys is not nil, then (k, h[k])
337
-// is encoded only only if k is in keys.
338
-func encodeHeaders(enc *hpack.Encoder, h http.Header, keys []string) {
339
-	if keys == nil {
340
-		sorter := sorterPool.Get().(*sorter)
341
-		// Using defer here, since the returned keys from the
342
-		// sorter.Keys method is only valid until the sorter
343
-		// is returned:
344
-		defer sorterPool.Put(sorter)
345
-		keys = sorter.Keys(h)
346
-	}
347
-	for _, k := range keys {
348
-		vv := h[k]
349
-		k = lowerHeader(k)
350
-		if !validWireHeaderFieldName(k) {
351
-			// Skip it as backup paranoia. Per
352
-			// golang.org/issue/14048, these should
353
-			// already be rejected at a higher level.
354
-			continue
355
-		}
356
-		isTE := k == "transfer-encoding"
357
-		for _, v := range vv {
358
-			if !httplex.ValidHeaderFieldValue(v) {
359
-				// TODO: return an error? golang.org/issue/14048
360
-				// For now just omit it.
361
-				continue
362
-			}
363
-			// TODO: more of "8.1.2.2 Connection-Specific Header Fields"
364
-			if isTE && v != "trailers" {
365
-				continue
366
-			}
367
-			encKV(enc, k, v)
368
-		}
369
-	}
370
-}

+ 0
- 242
vendor/golang.org/x/net/http2/writesched.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import "fmt"
8
-
9
-// WriteScheduler is the interface implemented by HTTP/2 write schedulers.
10
-// Methods are never called concurrently.
11
-type WriteScheduler interface {
12
-	// OpenStream opens a new stream in the write scheduler.
13
-	// It is illegal to call this with streamID=0 or with a streamID that is
14
-	// already open -- the call may panic.
15
-	OpenStream(streamID uint32, options OpenStreamOptions)
16
-
17
-	// CloseStream closes a stream in the write scheduler. Any frames queued on
18
-	// this stream should be discarded. It is illegal to call this on a stream
19
-	// that is not open -- the call may panic.
20
-	CloseStream(streamID uint32)
21
-
22
-	// AdjustStream adjusts the priority of the given stream. This may be called
23
-	// on a stream that has not yet been opened or has been closed. Note that
24
-	// RFC 7540 allows PRIORITY frames to be sent on streams in any state. See:
25
-	// https://tools.ietf.org/html/rfc7540#section-5.1
26
-	AdjustStream(streamID uint32, priority PriorityParam)
27
-
28
-	// Push queues a frame in the scheduler. In most cases, this will not be
29
-	// called with wr.StreamID()!=0 unless that stream is currently open. The one
30
-	// exception is RST_STREAM frames, which may be sent on idle or closed streams.
31
-	Push(wr FrameWriteRequest)
32
-
33
-	// Pop dequeues the next frame to write. Returns false if no frames can
34
-	// be written. Frames with a given wr.StreamID() are Pop'd in the same
35
-	// order they are Push'd.
36
-	Pop() (wr FrameWriteRequest, ok bool)
37
-}
38
-
39
-// OpenStreamOptions specifies extra options for WriteScheduler.OpenStream.
40
-type OpenStreamOptions struct {
41
-	// PusherID is zero if the stream was initiated by the client. Otherwise,
42
-	// PusherID names the stream that pushed the newly opened stream.
43
-	PusherID uint32
44
-}
45
-
46
-// FrameWriteRequest is a request to write a frame.
47
-type FrameWriteRequest struct {
48
-	// write is the interface value that does the writing, once the
49
-	// WriteScheduler has selected this frame to write. The write
50
-	// functions are all defined in write.go.
51
-	write writeFramer
52
-
53
-	// stream is the stream on which this frame will be written.
54
-	// nil for non-stream frames like PING and SETTINGS.
55
-	stream *stream
56
-
57
-	// done, if non-nil, must be a buffered channel with space for
58
-	// 1 message and is sent the return value from write (or an
59
-	// earlier error) when the frame has been written.
60
-	done chan error
61
-}
62
-
63
-// StreamID returns the id of the stream this frame will be written to.
64
-// 0 is used for non-stream frames such as PING and SETTINGS.
65
-func (wr FrameWriteRequest) StreamID() uint32 {
66
-	if wr.stream == nil {
67
-		if se, ok := wr.write.(StreamError); ok {
68
-			// (*serverConn).resetStream doesn't set
69
-			// stream because it doesn't necessarily have
70
-			// one. So special case this type of write
71
-			// message.
72
-			return se.StreamID
73
-		}
74
-		return 0
75
-	}
76
-	return wr.stream.id
77
-}
78
-
79
-// DataSize returns the number of flow control bytes that must be consumed
80
-// to write this entire frame. This is 0 for non-DATA frames.
81
-func (wr FrameWriteRequest) DataSize() int {
82
-	if wd, ok := wr.write.(*writeData); ok {
83
-		return len(wd.p)
84
-	}
85
-	return 0
86
-}
87
-
88
-// Consume consumes min(n, available) bytes from this frame, where available
89
-// is the number of flow control bytes available on the stream. Consume returns
90
-// 0, 1, or 2 frames, where the integer return value gives the number of frames
91
-// returned.
92
-//
93
-// If flow control prevents consuming any bytes, this returns (_, _, 0). If
94
-// the entire frame was consumed, this returns (wr, _, 1). Otherwise, this
95
-// returns (consumed, rest, 2), where 'consumed' contains the consumed bytes and
96
-// 'rest' contains the remaining bytes. The consumed bytes are deducted from the
97
-// underlying stream's flow control budget.
98
-func (wr FrameWriteRequest) Consume(n int32) (FrameWriteRequest, FrameWriteRequest, int) {
99
-	var empty FrameWriteRequest
100
-
101
-	// Non-DATA frames are always consumed whole.
102
-	wd, ok := wr.write.(*writeData)
103
-	if !ok || len(wd.p) == 0 {
104
-		return wr, empty, 1
105
-	}
106
-
107
-	// Might need to split after applying limits.
108
-	allowed := wr.stream.flow.available()
109
-	if n < allowed {
110
-		allowed = n
111
-	}
112
-	if wr.stream.sc.maxFrameSize < allowed {
113
-		allowed = wr.stream.sc.maxFrameSize
114
-	}
115
-	if allowed <= 0 {
116
-		return empty, empty, 0
117
-	}
118
-	if len(wd.p) > int(allowed) {
119
-		wr.stream.flow.take(allowed)
120
-		consumed := FrameWriteRequest{
121
-			stream: wr.stream,
122
-			write: &writeData{
123
-				streamID: wd.streamID,
124
-				p:        wd.p[:allowed],
125
-				// Even if the original had endStream set, there
126
-				// are bytes remaining because len(wd.p) > allowed,
127
-				// so we know endStream is false.
128
-				endStream: false,
129
-			},
130
-			// Our caller is blocking on the final DATA frame, not
131
-			// this intermediate frame, so no need to wait.
132
-			done: nil,
133
-		}
134
-		rest := FrameWriteRequest{
135
-			stream: wr.stream,
136
-			write: &writeData{
137
-				streamID:  wd.streamID,
138
-				p:         wd.p[allowed:],
139
-				endStream: wd.endStream,
140
-			},
141
-			done: wr.done,
142
-		}
143
-		return consumed, rest, 2
144
-	}
145
-
146
-	// The frame is consumed whole.
147
-	// NB: This cast cannot overflow because allowed is <= math.MaxInt32.
148
-	wr.stream.flow.take(int32(len(wd.p)))
149
-	return wr, empty, 1
150
-}
151
-
152
-// String is for debugging only.
153
-func (wr FrameWriteRequest) String() string {
154
-	var des string
155
-	if s, ok := wr.write.(fmt.Stringer); ok {
156
-		des = s.String()
157
-	} else {
158
-		des = fmt.Sprintf("%T", wr.write)
159
-	}
160
-	return fmt.Sprintf("[FrameWriteRequest stream=%d, ch=%v, writer=%v]", wr.StreamID(), wr.done != nil, des)
161
-}
162
-
163
-// replyToWriter sends err to wr.done and panics if the send must block
164
-// This does nothing if wr.done is nil.
165
-func (wr *FrameWriteRequest) replyToWriter(err error) {
166
-	if wr.done == nil {
167
-		return
168
-	}
169
-	select {
170
-	case wr.done <- err:
171
-	default:
172
-		panic(fmt.Sprintf("unbuffered done channel passed in for type %T", wr.write))
173
-	}
174
-	wr.write = nil // prevent use (assume it's tainted after wr.done send)
175
-}
176
-
177
-// writeQueue is used by implementations of WriteScheduler.
178
-type writeQueue struct {
179
-	s []FrameWriteRequest
180
-}
181
-
182
-func (q *writeQueue) empty() bool { return len(q.s) == 0 }
183
-
184
-func (q *writeQueue) push(wr FrameWriteRequest) {
185
-	q.s = append(q.s, wr)
186
-}
187
-
188
-func (q *writeQueue) shift() FrameWriteRequest {
189
-	if len(q.s) == 0 {
190
-		panic("invalid use of queue")
191
-	}
192
-	wr := q.s[0]
193
-	// TODO: less copy-happy queue.
194
-	copy(q.s, q.s[1:])
195
-	q.s[len(q.s)-1] = FrameWriteRequest{}
196
-	q.s = q.s[:len(q.s)-1]
197
-	return wr
198
-}
199
-
200
-// consume consumes up to n bytes from q.s[0]. If the frame is
201
-// entirely consumed, it is removed from the queue. If the frame
202
-// is partially consumed, the frame is kept with the consumed
203
-// bytes removed. Returns true iff any bytes were consumed.
204
-func (q *writeQueue) consume(n int32) (FrameWriteRequest, bool) {
205
-	if len(q.s) == 0 {
206
-		return FrameWriteRequest{}, false
207
-	}
208
-	consumed, rest, numresult := q.s[0].Consume(n)
209
-	switch numresult {
210
-	case 0:
211
-		return FrameWriteRequest{}, false
212
-	case 1:
213
-		q.shift()
214
-	case 2:
215
-		q.s[0] = rest
216
-	}
217
-	return consumed, true
218
-}
219
-
220
-type writeQueuePool []*writeQueue
221
-
222
-// put inserts an unused writeQueue into the pool.
223
-func (p *writeQueuePool) put(q *writeQueue) {
224
-	for i := range q.s {
225
-		q.s[i] = FrameWriteRequest{}
226
-	}
227
-	q.s = q.s[:0]
228
-	*p = append(*p, q)
229
-}
230
-
231
-// get returns an empty writeQueue.
232
-func (p *writeQueuePool) get() *writeQueue {
233
-	ln := len(*p)
234
-	if ln == 0 {
235
-		return new(writeQueue)
236
-	}
237
-	x := ln - 1
238
-	q := (*p)[x]
239
-	(*p)[x] = nil
240
-	*p = (*p)[:x]
241
-	return q
242
-}

+ 0
- 452
vendor/golang.org/x/net/http2/writesched_priority.go View File

1
-// Copyright 2016 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 http2
6
-
7
-import (
8
-	"fmt"
9
-	"math"
10
-	"sort"
11
-)
12
-
13
-// RFC 7540, Section 5.3.5: the default weight is 16.
14
-const priorityDefaultWeight = 15 // 16 = 15 + 1
15
-
16
-// PriorityWriteSchedulerConfig configures a priorityWriteScheduler.
17
-type PriorityWriteSchedulerConfig struct {
18
-	// MaxClosedNodesInTree controls the maximum number of closed streams to
19
-	// retain in the priority tree. Setting this to zero saves a small amount
20
-	// of memory at the cost of performance.
21
-	//
22
-	// See RFC 7540, Section 5.3.4:
23
-	//   "It is possible for a stream to become closed while prioritization
24
-	//   information ... is in transit. ... This potentially creates suboptimal
25
-	//   prioritization, since the stream could be given a priority that is
26
-	//   different from what is intended. To avoid these problems, an endpoint
27
-	//   SHOULD retain stream prioritization state for a period after streams
28
-	//   become closed. The longer state is retained, the lower the chance that
29
-	//   streams are assigned incorrect or default priority values."
30
-	MaxClosedNodesInTree int
31
-
32
-	// MaxIdleNodesInTree controls the maximum number of idle streams to
33
-	// retain in the priority tree. Setting this to zero saves a small amount
34
-	// of memory at the cost of performance.
35
-	//
36
-	// See RFC 7540, Section 5.3.4:
37
-	//   Similarly, streams that are in the "idle" state can be assigned
38
-	//   priority or become a parent of other streams. This allows for the
39
-	//   creation of a grouping node in the dependency tree, which enables
40
-	//   more flexible expressions of priority. Idle streams begin with a
41
-	//   default priority (Section 5.3.5).
42
-	MaxIdleNodesInTree int
43
-
44
-	// ThrottleOutOfOrderWrites enables write throttling to help ensure that
45
-	// data is delivered in priority order. This works around a race where
46
-	// stream B depends on stream A and both streams are about to call Write
47
-	// to queue DATA frames. If B wins the race, a naive scheduler would eagerly
48
-	// write as much data from B as possible, but this is suboptimal because A
49
-	// is a higher-priority stream. With throttling enabled, we write a small
50
-	// amount of data from B to minimize the amount of bandwidth that B can
51
-	// steal from A.
52
-	ThrottleOutOfOrderWrites bool
53
-}
54
-
55
-// NewPriorityWriteScheduler constructs a WriteScheduler that schedules
56
-// frames by following HTTP/2 priorities as described in RFC 7540 Section 5.3.
57
-// If cfg is nil, default options are used.
58
-func NewPriorityWriteScheduler(cfg *PriorityWriteSchedulerConfig) WriteScheduler {
59
-	if cfg == nil {
60
-		// For justification of these defaults, see:
61
-		// https://docs.google.com/document/d/1oLhNg1skaWD4_DtaoCxdSRN5erEXrH-KnLrMwEpOtFY
62
-		cfg = &PriorityWriteSchedulerConfig{
63
-			MaxClosedNodesInTree:     10,
64
-			MaxIdleNodesInTree:       10,
65
-			ThrottleOutOfOrderWrites: false,
66
-		}
67
-	}
68
-
69
-	ws := &priorityWriteScheduler{
70
-		nodes:                make(map[uint32]*priorityNode),
71
-		maxClosedNodesInTree: cfg.MaxClosedNodesInTree,
72
-		maxIdleNodesInTree:   cfg.MaxIdleNodesInTree,
73
-		enableWriteThrottle:  cfg.ThrottleOutOfOrderWrites,
74
-	}
75
-	ws.nodes[0] = &ws.root
76
-	if cfg.ThrottleOutOfOrderWrites {
77
-		ws.writeThrottleLimit = 1024
78
-	} else {
79
-		ws.writeThrottleLimit = math.MaxInt32
80
-	}
81
-	return ws
82
-}
83
-
84
-type priorityNodeState int
85
-
86
-const (
87
-	priorityNodeOpen priorityNodeState = iota
88
-	priorityNodeClosed
89
-	priorityNodeIdle
90
-)
91
-
92
-// priorityNode is a node in an HTTP/2 priority tree.
93
-// Each node is associated with a single stream ID.
94
-// See RFC 7540, Section 5.3.
95
-type priorityNode struct {
96
-	q            writeQueue        // queue of pending frames to write
97
-	id           uint32            // id of the stream, or 0 for the root of the tree
98
-	weight       uint8             // the actual weight is weight+1, so the value is in [1,256]
99
-	state        priorityNodeState // open | closed | idle
100
-	bytes        int64             // number of bytes written by this node, or 0 if closed
101
-	subtreeBytes int64             // sum(node.bytes) of all nodes in this subtree
102
-
103
-	// These links form the priority tree.
104
-	parent     *priorityNode
105
-	kids       *priorityNode // start of the kids list
106
-	prev, next *priorityNode // doubly-linked list of siblings
107
-}
108
-
109
-func (n *priorityNode) setParent(parent *priorityNode) {
110
-	if n == parent {
111
-		panic("setParent to self")
112
-	}
113
-	if n.parent == parent {
114
-		return
115
-	}
116
-	// Unlink from current parent.
117
-	if parent := n.parent; parent != nil {
118
-		if n.prev == nil {
119
-			parent.kids = n.next
120
-		} else {
121
-			n.prev.next = n.next
122
-		}
123
-		if n.next != nil {
124
-			n.next.prev = n.prev
125
-		}
126
-	}
127
-	// Link to new parent.
128
-	// If parent=nil, remove n from the tree.
129
-	// Always insert at the head of parent.kids (this is assumed by walkReadyInOrder).
130
-	n.parent = parent
131
-	if parent == nil {
132
-		n.next = nil
133
-		n.prev = nil
134
-	} else {
135
-		n.next = parent.kids
136
-		n.prev = nil
137
-		if n.next != nil {
138
-			n.next.prev = n
139
-		}
140
-		parent.kids = n
141
-	}
142
-}
143
-
144
-func (n *priorityNode) addBytes(b int64) {
145
-	n.bytes += b
146
-	for ; n != nil; n = n.parent {
147
-		n.subtreeBytes += b
148
-	}
149
-}
150
-
151
-// walkReadyInOrder iterates over the tree in priority order, calling f for each node
152
-// with a non-empty write queue. When f returns true, this funcion returns true and the
153
-// walk halts. tmp is used as scratch space for sorting.
154
-//
155
-// f(n, openParent) takes two arguments: the node to visit, n, and a bool that is true
156
-// if any ancestor p of n is still open (ignoring the root node).
157
-func (n *priorityNode) walkReadyInOrder(openParent bool, tmp *[]*priorityNode, f func(*priorityNode, bool) bool) bool {
158
-	if !n.q.empty() && f(n, openParent) {
159
-		return true
160
-	}
161
-	if n.kids == nil {
162
-		return false
163
-	}
164
-
165
-	// Don't consider the root "open" when updating openParent since
166
-	// we can't send data frames on the root stream (only control frames).
167
-	if n.id != 0 {
168
-		openParent = openParent || (n.state == priorityNodeOpen)
169
-	}
170
-
171
-	// Common case: only one kid or all kids have the same weight.
172
-	// Some clients don't use weights; other clients (like web browsers)
173
-	// use mostly-linear priority trees.
174
-	w := n.kids.weight
175
-	needSort := false
176
-	for k := n.kids.next; k != nil; k = k.next {
177
-		if k.weight != w {
178
-			needSort = true
179
-			break
180
-		}
181
-	}
182
-	if !needSort {
183
-		for k := n.kids; k != nil; k = k.next {
184
-			if k.walkReadyInOrder(openParent, tmp, f) {
185
-				return true
186
-			}
187
-		}
188
-		return false
189
-	}
190
-
191
-	// Uncommon case: sort the child nodes. We remove the kids from the parent,
192
-	// then re-insert after sorting so we can reuse tmp for future sort calls.
193
-	*tmp = (*tmp)[:0]
194
-	for n.kids != nil {
195
-		*tmp = append(*tmp, n.kids)
196
-		n.kids.setParent(nil)
197
-	}
198
-	sort.Sort(sortPriorityNodeSiblings(*tmp))
199
-	for i := len(*tmp) - 1; i >= 0; i-- {
200
-		(*tmp)[i].setParent(n) // setParent inserts at the head of n.kids
201
-	}
202
-	for k := n.kids; k != nil; k = k.next {
203
-		if k.walkReadyInOrder(openParent, tmp, f) {
204
-			return true
205
-		}
206
-	}
207
-	return false
208
-}
209
-
210
-type sortPriorityNodeSiblings []*priorityNode
211
-
212
-func (z sortPriorityNodeSiblings) Len() int      { return len(z) }
213
-func (z sortPriorityNodeSiblings) Swap(i, k int) { z[i], z[k] = z[k], z[i] }
214
-func (z sortPriorityNodeSiblings) Less(i, k int) bool {
215
-	// Prefer the subtree that has sent fewer bytes relative to its weight.
216
-	// See sections 5.3.2 and 5.3.4.
217
-	wi, bi := float64(z[i].weight+1), float64(z[i].subtreeBytes)
218
-	wk, bk := float64(z[k].weight+1), float64(z[k].subtreeBytes)
219
-	if bi == 0 && bk == 0 {
220
-		return wi >= wk
221
-	}
222
-	if bk == 0 {
223
-		return false
224
-	}
225
-	return bi/bk <= wi/wk
226
-}
227
-
228
-type priorityWriteScheduler struct {
229
-	// root is the root of the priority tree, where root.id = 0.
230
-	// The root queues control frames that are not associated with any stream.
231
-	root priorityNode
232
-
233
-	// nodes maps stream ids to priority tree nodes.
234
-	nodes map[uint32]*priorityNode
235
-
236
-	// maxID is the maximum stream id in nodes.
237
-	maxID uint32
238
-
239
-	// lists of nodes that have been closed or are idle, but are kept in
240
-	// the tree for improved prioritization. When the lengths exceed either
241
-	// maxClosedNodesInTree or maxIdleNodesInTree, old nodes are discarded.
242
-	closedNodes, idleNodes []*priorityNode
243
-
244
-	// From the config.
245
-	maxClosedNodesInTree int
246
-	maxIdleNodesInTree   int
247
-	writeThrottleLimit   int32
248
-	enableWriteThrottle  bool
249
-
250
-	// tmp is scratch space for priorityNode.walkReadyInOrder to reduce allocations.
251
-	tmp []*priorityNode
252
-
253
-	// pool of empty queues for reuse.
254
-	queuePool writeQueuePool
255
-}
256
-
257
-func (ws *priorityWriteScheduler) OpenStream(streamID uint32, options OpenStreamOptions) {
258
-	// The stream may be currently idle but cannot be opened or closed.
259
-	if curr := ws.nodes[streamID]; curr != nil {
260
-		if curr.state != priorityNodeIdle {
261
-			panic(fmt.Sprintf("stream %d already opened", streamID))
262
-		}
263
-		curr.state = priorityNodeOpen
264
-		return
265
-	}
266
-
267
-	// RFC 7540, Section 5.3.5:
268
-	//  "All streams are initially assigned a non-exclusive dependency on stream 0x0.
269
-	//  Pushed streams initially depend on their associated stream. In both cases,
270
-	//  streams are assigned a default weight of 16."
271
-	parent := ws.nodes[options.PusherID]
272
-	if parent == nil {
273
-		parent = &ws.root
274
-	}
275
-	n := &priorityNode{
276
-		q:      *ws.queuePool.get(),
277
-		id:     streamID,
278
-		weight: priorityDefaultWeight,
279
-		state:  priorityNodeOpen,
280
-	}
281
-	n.setParent(parent)
282
-	ws.nodes[streamID] = n
283
-	if streamID > ws.maxID {
284
-		ws.maxID = streamID
285
-	}
286
-}
287
-
288
-func (ws *priorityWriteScheduler) CloseStream(streamID uint32) {
289
-	if streamID == 0 {
290
-		panic("violation of WriteScheduler interface: cannot close stream 0")
291
-	}
292
-	if ws.nodes[streamID] == nil {
293
-		panic(fmt.Sprintf("violation of WriteScheduler interface: unknown stream %d", streamID))
294
-	}
295
-	if ws.nodes[streamID].state != priorityNodeOpen {
296
-		panic(fmt.Sprintf("violation of WriteScheduler interface: stream %d already closed", streamID))
297
-	}
298
-
299
-	n := ws.nodes[streamID]
300
-	n.state = priorityNodeClosed
301
-	n.addBytes(-n.bytes)
302
-
303
-	q := n.q
304
-	ws.queuePool.put(&q)
305
-	n.q.s = nil
306
-	if ws.maxClosedNodesInTree > 0 {
307
-		ws.addClosedOrIdleNode(&ws.closedNodes, ws.maxClosedNodesInTree, n)
308
-	} else {
309
-		ws.removeNode(n)
310
-	}
311
-}
312
-
313
-func (ws *priorityWriteScheduler) AdjustStream(streamID uint32, priority PriorityParam) {
314
-	if streamID == 0 {
315
-		panic("adjustPriority on root")
316
-	}
317
-
318
-	// If streamID does not exist, there are two cases:
319
-	// - A closed stream that has been removed (this will have ID <= maxID)
320
-	// - An idle stream that is being used for "grouping" (this will have ID > maxID)
321
-	n := ws.nodes[streamID]
322
-	if n == nil {
323
-		if streamID <= ws.maxID || ws.maxIdleNodesInTree == 0 {
324
-			return
325
-		}
326
-		ws.maxID = streamID
327
-		n = &priorityNode{
328
-			q:      *ws.queuePool.get(),
329
-			id:     streamID,
330
-			weight: priorityDefaultWeight,
331
-			state:  priorityNodeIdle,
332
-		}
333
-		n.setParent(&ws.root)
334
-		ws.nodes[streamID] = n
335
-		ws.addClosedOrIdleNode(&ws.idleNodes, ws.maxIdleNodesInTree, n)
336
-	}
337
-
338
-	// Section 5.3.1: A dependency on a stream that is not currently in the tree
339
-	// results in that stream being given a default priority (Section 5.3.5).
340
-	parent := ws.nodes[priority.StreamDep]
341
-	if parent == nil {
342
-		n.setParent(&ws.root)
343
-		n.weight = priorityDefaultWeight
344
-		return
345
-	}
346
-
347
-	// Ignore if the client tries to make a node its own parent.
348
-	if n == parent {
349
-		return
350
-	}
351
-
352
-	// Section 5.3.3:
353
-	//   "If a stream is made dependent on one of its own dependencies, the
354
-	//   formerly dependent stream is first moved to be dependent on the
355
-	//   reprioritized stream's previous parent. The moved dependency retains
356
-	//   its weight."
357
-	//
358
-	// That is: if parent depends on n, move parent to depend on n.parent.
359
-	for x := parent.parent; x != nil; x = x.parent {
360
-		if x == n {
361
-			parent.setParent(n.parent)
362
-			break
363
-		}
364
-	}
365
-
366
-	// Section 5.3.3: The exclusive flag causes the stream to become the sole
367
-	// dependency of its parent stream, causing other dependencies to become
368
-	// dependent on the exclusive stream.
369
-	if priority.Exclusive {
370
-		k := parent.kids
371
-		for k != nil {
372
-			next := k.next
373
-			if k != n {
374
-				k.setParent(n)
375
-			}
376
-			k = next
377
-		}
378
-	}
379
-
380
-	n.setParent(parent)
381
-	n.weight = priority.Weight
382
-}
383
-
384
-func (ws *priorityWriteScheduler) Push(wr FrameWriteRequest) {
385
-	var n *priorityNode
386
-	if id := wr.StreamID(); id == 0 {
387
-		n = &ws.root
388
-	} else {
389
-		n = ws.nodes[id]
390
-		if n == nil {
391
-			// id is an idle or closed stream. wr should not be a HEADERS or
392
-			// DATA frame. However, wr can be a RST_STREAM. In this case, we
393
-			// push wr onto the root, rather than creating a new priorityNode,
394
-			// since RST_STREAM is tiny and the stream's priority is unknown
395
-			// anyway. See issue #17919.
396
-			if wr.DataSize() > 0 {
397
-				panic("add DATA on non-open stream")
398
-			}
399
-			n = &ws.root
400
-		}
401
-	}
402
-	n.q.push(wr)
403
-}
404
-
405
-func (ws *priorityWriteScheduler) Pop() (wr FrameWriteRequest, ok bool) {
406
-	ws.root.walkReadyInOrder(false, &ws.tmp, func(n *priorityNode, openParent bool) bool {
407
-		limit := int32(math.MaxInt32)
408
-		if openParent {
409
-			limit = ws.writeThrottleLimit
410
-		}
411
-		wr, ok = n.q.consume(limit)
412
-		if !ok {
413
-			return false
414
-		}
415
-		n.addBytes(int64(wr.DataSize()))
416
-		// If B depends on A and B continuously has data available but A
417
-		// does not, gradually increase the throttling limit to allow B to
418
-		// steal more and more bandwidth from A.
419
-		if openParent {
420
-			ws.writeThrottleLimit += 1024
421
-			if ws.writeThrottleLimit < 0 {
422
-				ws.writeThrottleLimit = math.MaxInt32
423
-			}
424
-		} else if ws.enableWriteThrottle {
425
-			ws.writeThrottleLimit = 1024
426
-		}
427
-		return true
428
-	})
429
-	return wr, ok
430
-}
431
-
432
-func (ws *priorityWriteScheduler) addClosedOrIdleNode(list *[]*priorityNode, maxSize int, n *priorityNode) {
433
-	if maxSize == 0 {
434
-		return
435
-	}
436
-	if len(*list) == maxSize {
437
-		// Remove the oldest node, then shift left.
438
-		ws.removeNode((*list)[0])
439
-		x := (*list)[1:]
440
-		copy(*list, x)
441
-		*list = (*list)[:len(x)]
442
-	}
443
-	*list = append(*list, n)
444
-}
445
-
446
-func (ws *priorityWriteScheduler) removeNode(n *priorityNode) {
447
-	for k := n.kids; k != nil; k = k.next {
448
-		k.setParent(n.parent)
449
-	}
450
-	n.setParent(nil)
451
-	delete(ws.nodes, n.id)
452
-}

+ 0
- 541
vendor/golang.org/x/net/http2/writesched_priority_test.go View File

1
-// Copyright 2016 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 http2
6
-
7
-import (
8
-	"bytes"
9
-	"fmt"
10
-	"sort"
11
-	"testing"
12
-)
13
-
14
-func defaultPriorityWriteScheduler() *priorityWriteScheduler {
15
-	return NewPriorityWriteScheduler(nil).(*priorityWriteScheduler)
16
-}
17
-
18
-func checkPriorityWellFormed(ws *priorityWriteScheduler) error {
19
-	for id, n := range ws.nodes {
20
-		if id != n.id {
21
-			return fmt.Errorf("bad ws.nodes: ws.nodes[%d] = %d", id, n.id)
22
-		}
23
-		if n.parent == nil {
24
-			if n.next != nil || n.prev != nil {
25
-				return fmt.Errorf("bad node %d: nil parent but prev/next not nil", id)
26
-			}
27
-			continue
28
-		}
29
-		found := false
30
-		for k := n.parent.kids; k != nil; k = k.next {
31
-			if k.id == id {
32
-				found = true
33
-				break
34
-			}
35
-		}
36
-		if !found {
37
-			return fmt.Errorf("bad node %d: not found in parent %d kids list", id, n.parent.id)
38
-		}
39
-	}
40
-	return nil
41
-}
42
-
43
-func fmtTree(ws *priorityWriteScheduler, fmtNode func(*priorityNode) string) string {
44
-	var ids []int
45
-	for _, n := range ws.nodes {
46
-		ids = append(ids, int(n.id))
47
-	}
48
-	sort.Ints(ids)
49
-
50
-	var buf bytes.Buffer
51
-	for _, id := range ids {
52
-		if buf.Len() != 0 {
53
-			buf.WriteString(" ")
54
-		}
55
-		if id == 0 {
56
-			buf.WriteString(fmtNode(&ws.root))
57
-		} else {
58
-			buf.WriteString(fmtNode(ws.nodes[uint32(id)]))
59
-		}
60
-	}
61
-	return buf.String()
62
-}
63
-
64
-func fmtNodeParentSkipRoot(n *priorityNode) string {
65
-	switch {
66
-	case n.id == 0:
67
-		return ""
68
-	case n.parent == nil:
69
-		return fmt.Sprintf("%d{parent:nil}", n.id)
70
-	default:
71
-		return fmt.Sprintf("%d{parent:%d}", n.id, n.parent.id)
72
-	}
73
-}
74
-
75
-func fmtNodeWeightParentSkipRoot(n *priorityNode) string {
76
-	switch {
77
-	case n.id == 0:
78
-		return ""
79
-	case n.parent == nil:
80
-		return fmt.Sprintf("%d{weight:%d,parent:nil}", n.id, n.weight)
81
-	default:
82
-		return fmt.Sprintf("%d{weight:%d,parent:%d}", n.id, n.weight, n.parent.id)
83
-	}
84
-}
85
-
86
-func TestPriorityTwoStreams(t *testing.T) {
87
-	ws := defaultPriorityWriteScheduler()
88
-	ws.OpenStream(1, OpenStreamOptions{})
89
-	ws.OpenStream(2, OpenStreamOptions{})
90
-
91
-	want := "1{weight:15,parent:0} 2{weight:15,parent:0}"
92
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
93
-		t.Errorf("After open\ngot  %q\nwant %q", got, want)
94
-	}
95
-
96
-	// Move 1's parent to 2.
97
-	ws.AdjustStream(1, PriorityParam{
98
-		StreamDep: 2,
99
-		Weight:    32,
100
-		Exclusive: false,
101
-	})
102
-	want = "1{weight:32,parent:2} 2{weight:15,parent:0}"
103
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
104
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
105
-	}
106
-
107
-	if err := checkPriorityWellFormed(ws); err != nil {
108
-		t.Error(err)
109
-	}
110
-}
111
-
112
-func TestPriorityAdjustExclusiveZero(t *testing.T) {
113
-	// 1, 2, and 3 are all children of the 0 stream.
114
-	// Exclusive reprioritization to any of the streams should bring
115
-	// the rest of the streams under the reprioritized stream.
116
-	ws := defaultPriorityWriteScheduler()
117
-	ws.OpenStream(1, OpenStreamOptions{})
118
-	ws.OpenStream(2, OpenStreamOptions{})
119
-	ws.OpenStream(3, OpenStreamOptions{})
120
-
121
-	want := "1{weight:15,parent:0} 2{weight:15,parent:0} 3{weight:15,parent:0}"
122
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
123
-		t.Errorf("After open\ngot  %q\nwant %q", got, want)
124
-	}
125
-
126
-	ws.AdjustStream(2, PriorityParam{
127
-		StreamDep: 0,
128
-		Weight:    20,
129
-		Exclusive: true,
130
-	})
131
-	want = "1{weight:15,parent:2} 2{weight:20,parent:0} 3{weight:15,parent:2}"
132
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
133
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
134
-	}
135
-
136
-	if err := checkPriorityWellFormed(ws); err != nil {
137
-		t.Error(err)
138
-	}
139
-}
140
-
141
-func TestPriorityAdjustOwnParent(t *testing.T) {
142
-	// Assigning a node as its own parent should have no effect.
143
-	ws := defaultPriorityWriteScheduler()
144
-	ws.OpenStream(1, OpenStreamOptions{})
145
-	ws.OpenStream(2, OpenStreamOptions{})
146
-	ws.AdjustStream(2, PriorityParam{
147
-		StreamDep: 2,
148
-		Weight:    20,
149
-		Exclusive: true,
150
-	})
151
-	want := "1{weight:15,parent:0} 2{weight:15,parent:0}"
152
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
153
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
154
-	}
155
-	if err := checkPriorityWellFormed(ws); err != nil {
156
-		t.Error(err)
157
-	}
158
-}
159
-
160
-func TestPriorityClosedStreams(t *testing.T) {
161
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{MaxClosedNodesInTree: 2}).(*priorityWriteScheduler)
162
-	ws.OpenStream(1, OpenStreamOptions{})
163
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
164
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 2})
165
-	ws.OpenStream(4, OpenStreamOptions{PusherID: 3})
166
-
167
-	// Close the first three streams. We lose 1, but keep 2 and 3.
168
-	ws.CloseStream(1)
169
-	ws.CloseStream(2)
170
-	ws.CloseStream(3)
171
-
172
-	want := "2{weight:15,parent:0} 3{weight:15,parent:2} 4{weight:15,parent:3}"
173
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
174
-		t.Errorf("After close\ngot  %q\nwant %q", got, want)
175
-	}
176
-	if err := checkPriorityWellFormed(ws); err != nil {
177
-		t.Error(err)
178
-	}
179
-
180
-	// Adding a stream as an exclusive child of 1 gives it default
181
-	// priorities, since 1 is gone.
182
-	ws.OpenStream(5, OpenStreamOptions{})
183
-	ws.AdjustStream(5, PriorityParam{StreamDep: 1, Weight: 15, Exclusive: true})
184
-
185
-	// Adding a stream as an exclusive child of 2 should work, since 2 is not gone.
186
-	ws.OpenStream(6, OpenStreamOptions{})
187
-	ws.AdjustStream(6, PriorityParam{StreamDep: 2, Weight: 15, Exclusive: true})
188
-
189
-	want = "2{weight:15,parent:0} 3{weight:15,parent:6} 4{weight:15,parent:3} 5{weight:15,parent:0} 6{weight:15,parent:2}"
190
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
191
-		t.Errorf("After add streams\ngot  %q\nwant %q", got, want)
192
-	}
193
-	if err := checkPriorityWellFormed(ws); err != nil {
194
-		t.Error(err)
195
-	}
196
-}
197
-
198
-func TestPriorityClosedStreamsDisabled(t *testing.T) {
199
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{}).(*priorityWriteScheduler)
200
-	ws.OpenStream(1, OpenStreamOptions{})
201
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
202
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 2})
203
-
204
-	// Close the first two streams. We keep only 3.
205
-	ws.CloseStream(1)
206
-	ws.CloseStream(2)
207
-
208
-	want := "3{weight:15,parent:0}"
209
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
210
-		t.Errorf("After close\ngot  %q\nwant %q", got, want)
211
-	}
212
-	if err := checkPriorityWellFormed(ws); err != nil {
213
-		t.Error(err)
214
-	}
215
-}
216
-
217
-func TestPriorityIdleStreams(t *testing.T) {
218
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{MaxIdleNodesInTree: 2}).(*priorityWriteScheduler)
219
-	ws.AdjustStream(1, PriorityParam{StreamDep: 0, Weight: 15}) // idle
220
-	ws.AdjustStream(2, PriorityParam{StreamDep: 0, Weight: 15}) // idle
221
-	ws.AdjustStream(3, PriorityParam{StreamDep: 2, Weight: 20}) // idle
222
-	ws.OpenStream(4, OpenStreamOptions{})
223
-	ws.OpenStream(5, OpenStreamOptions{})
224
-	ws.OpenStream(6, OpenStreamOptions{})
225
-	ws.AdjustStream(4, PriorityParam{StreamDep: 1, Weight: 15})
226
-	ws.AdjustStream(5, PriorityParam{StreamDep: 2, Weight: 15})
227
-	ws.AdjustStream(6, PriorityParam{StreamDep: 3, Weight: 15})
228
-
229
-	want := "2{weight:15,parent:0} 3{weight:20,parent:2} 4{weight:15,parent:0} 5{weight:15,parent:2} 6{weight:15,parent:3}"
230
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
231
-		t.Errorf("After open\ngot  %q\nwant %q", got, want)
232
-	}
233
-	if err := checkPriorityWellFormed(ws); err != nil {
234
-		t.Error(err)
235
-	}
236
-}
237
-
238
-func TestPriorityIdleStreamsDisabled(t *testing.T) {
239
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{}).(*priorityWriteScheduler)
240
-	ws.AdjustStream(1, PriorityParam{StreamDep: 0, Weight: 15}) // idle
241
-	ws.AdjustStream(2, PriorityParam{StreamDep: 0, Weight: 15}) // idle
242
-	ws.AdjustStream(3, PriorityParam{StreamDep: 2, Weight: 20}) // idle
243
-	ws.OpenStream(4, OpenStreamOptions{})
244
-
245
-	want := "4{weight:15,parent:0}"
246
-	if got := fmtTree(ws, fmtNodeWeightParentSkipRoot); got != want {
247
-		t.Errorf("After open\ngot  %q\nwant %q", got, want)
248
-	}
249
-	if err := checkPriorityWellFormed(ws); err != nil {
250
-		t.Error(err)
251
-	}
252
-}
253
-
254
-func TestPrioritySection531NonExclusive(t *testing.T) {
255
-	// Example from RFC 7540 Section 5.3.1.
256
-	// A,B,C,D = 1,2,3,4
257
-	ws := defaultPriorityWriteScheduler()
258
-	ws.OpenStream(1, OpenStreamOptions{})
259
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
260
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 1})
261
-	ws.OpenStream(4, OpenStreamOptions{})
262
-	ws.AdjustStream(4, PriorityParam{
263
-		StreamDep: 1,
264
-		Weight:    15,
265
-		Exclusive: false,
266
-	})
267
-	want := "1{parent:0} 2{parent:1} 3{parent:1} 4{parent:1}"
268
-	if got := fmtTree(ws, fmtNodeParentSkipRoot); got != want {
269
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
270
-	}
271
-	if err := checkPriorityWellFormed(ws); err != nil {
272
-		t.Error(err)
273
-	}
274
-}
275
-
276
-func TestPrioritySection531Exclusive(t *testing.T) {
277
-	// Example from RFC 7540 Section 5.3.1.
278
-	// A,B,C,D = 1,2,3,4
279
-	ws := defaultPriorityWriteScheduler()
280
-	ws.OpenStream(1, OpenStreamOptions{})
281
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
282
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 1})
283
-	ws.OpenStream(4, OpenStreamOptions{})
284
-	ws.AdjustStream(4, PriorityParam{
285
-		StreamDep: 1,
286
-		Weight:    15,
287
-		Exclusive: true,
288
-	})
289
-	want := "1{parent:0} 2{parent:4} 3{parent:4} 4{parent:1}"
290
-	if got := fmtTree(ws, fmtNodeParentSkipRoot); got != want {
291
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
292
-	}
293
-	if err := checkPriorityWellFormed(ws); err != nil {
294
-		t.Error(err)
295
-	}
296
-}
297
-
298
-func makeSection533Tree() *priorityWriteScheduler {
299
-	// Initial tree from RFC 7540 Section 5.3.3.
300
-	// A,B,C,D,E,F = 1,2,3,4,5,6
301
-	ws := defaultPriorityWriteScheduler()
302
-	ws.OpenStream(1, OpenStreamOptions{})
303
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
304
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 1})
305
-	ws.OpenStream(4, OpenStreamOptions{PusherID: 3})
306
-	ws.OpenStream(5, OpenStreamOptions{PusherID: 3})
307
-	ws.OpenStream(6, OpenStreamOptions{PusherID: 4})
308
-	return ws
309
-}
310
-
311
-func TestPrioritySection533NonExclusive(t *testing.T) {
312
-	// Example from RFC 7540 Section 5.3.3.
313
-	// A,B,C,D,E,F = 1,2,3,4,5,6
314
-	ws := defaultPriorityWriteScheduler()
315
-	ws.OpenStream(1, OpenStreamOptions{})
316
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
317
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 1})
318
-	ws.OpenStream(4, OpenStreamOptions{PusherID: 3})
319
-	ws.OpenStream(5, OpenStreamOptions{PusherID: 3})
320
-	ws.OpenStream(6, OpenStreamOptions{PusherID: 4})
321
-	ws.AdjustStream(1, PriorityParam{
322
-		StreamDep: 4,
323
-		Weight:    15,
324
-		Exclusive: false,
325
-	})
326
-	want := "1{parent:4} 2{parent:1} 3{parent:1} 4{parent:0} 5{parent:3} 6{parent:4}"
327
-	if got := fmtTree(ws, fmtNodeParentSkipRoot); got != want {
328
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
329
-	}
330
-	if err := checkPriorityWellFormed(ws); err != nil {
331
-		t.Error(err)
332
-	}
333
-}
334
-
335
-func TestPrioritySection533Exclusive(t *testing.T) {
336
-	// Example from RFC 7540 Section 5.3.3.
337
-	// A,B,C,D,E,F = 1,2,3,4,5,6
338
-	ws := defaultPriorityWriteScheduler()
339
-	ws.OpenStream(1, OpenStreamOptions{})
340
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
341
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 1})
342
-	ws.OpenStream(4, OpenStreamOptions{PusherID: 3})
343
-	ws.OpenStream(5, OpenStreamOptions{PusherID: 3})
344
-	ws.OpenStream(6, OpenStreamOptions{PusherID: 4})
345
-	ws.AdjustStream(1, PriorityParam{
346
-		StreamDep: 4,
347
-		Weight:    15,
348
-		Exclusive: true,
349
-	})
350
-	want := "1{parent:4} 2{parent:1} 3{parent:1} 4{parent:0} 5{parent:3} 6{parent:1}"
351
-	if got := fmtTree(ws, fmtNodeParentSkipRoot); got != want {
352
-		t.Errorf("After adjust\ngot  %q\nwant %q", got, want)
353
-	}
354
-	if err := checkPriorityWellFormed(ws); err != nil {
355
-		t.Error(err)
356
-	}
357
-}
358
-
359
-func checkPopAll(ws WriteScheduler, order []uint32) error {
360
-	for k, id := range order {
361
-		wr, ok := ws.Pop()
362
-		if !ok {
363
-			return fmt.Errorf("Pop[%d]: got ok=false, want %d (order=%v)", k, id, order)
364
-		}
365
-		if got := wr.StreamID(); got != id {
366
-			return fmt.Errorf("Pop[%d]: got %v, want %d (order=%v)", k, got, id, order)
367
-		}
368
-	}
369
-	wr, ok := ws.Pop()
370
-	if ok {
371
-		return fmt.Errorf("Pop[%d]: got %v, want ok=false (order=%v)", len(order), wr.StreamID(), order)
372
-	}
373
-	return nil
374
-}
375
-
376
-func TestPriorityPopFrom533Tree(t *testing.T) {
377
-	ws := makeSection533Tree()
378
-
379
-	ws.Push(makeWriteHeadersRequest(3 /*C*/))
380
-	ws.Push(makeWriteNonStreamRequest())
381
-	ws.Push(makeWriteHeadersRequest(5 /*E*/))
382
-	ws.Push(makeWriteHeadersRequest(1 /*A*/))
383
-	t.Log("tree:", fmtTree(ws, fmtNodeParentSkipRoot))
384
-
385
-	if err := checkPopAll(ws, []uint32{0 /*NonStream*/, 1, 3, 5}); err != nil {
386
-		t.Error(err)
387
-	}
388
-}
389
-
390
-func TestPriorityPopFromLinearTree(t *testing.T) {
391
-	ws := defaultPriorityWriteScheduler()
392
-	ws.OpenStream(1, OpenStreamOptions{})
393
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
394
-	ws.OpenStream(3, OpenStreamOptions{PusherID: 2})
395
-	ws.OpenStream(4, OpenStreamOptions{PusherID: 3})
396
-
397
-	ws.Push(makeWriteHeadersRequest(3))
398
-	ws.Push(makeWriteHeadersRequest(4))
399
-	ws.Push(makeWriteHeadersRequest(1))
400
-	ws.Push(makeWriteHeadersRequest(2))
401
-	ws.Push(makeWriteNonStreamRequest())
402
-	ws.Push(makeWriteNonStreamRequest())
403
-	t.Log("tree:", fmtTree(ws, fmtNodeParentSkipRoot))
404
-
405
-	if err := checkPopAll(ws, []uint32{0, 0 /*NonStreams*/, 1, 2, 3, 4}); err != nil {
406
-		t.Error(err)
407
-	}
408
-}
409
-
410
-func TestPriorityFlowControl(t *testing.T) {
411
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{ThrottleOutOfOrderWrites: false})
412
-	ws.OpenStream(1, OpenStreamOptions{})
413
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
414
-
415
-	sc := &serverConn{maxFrameSize: 16}
416
-	st1 := &stream{id: 1, sc: sc}
417
-	st2 := &stream{id: 2, sc: sc}
418
-
419
-	ws.Push(FrameWriteRequest{&writeData{1, make([]byte, 16), false}, st1, nil})
420
-	ws.Push(FrameWriteRequest{&writeData{2, make([]byte, 16), false}, st2, nil})
421
-	ws.AdjustStream(2, PriorityParam{StreamDep: 1})
422
-
423
-	// No flow-control bytes available.
424
-	if wr, ok := ws.Pop(); ok {
425
-		t.Fatalf("Pop(limited by flow control)=%v,true, want false", wr)
426
-	}
427
-
428
-	// Add enough flow-control bytes to write st2 in two Pop calls.
429
-	// Should write data from st2 even though it's lower priority than st1.
430
-	for i := 1; i <= 2; i++ {
431
-		st2.flow.add(8)
432
-		wr, ok := ws.Pop()
433
-		if !ok {
434
-			t.Fatalf("Pop(%d)=false, want true", i)
435
-		}
436
-		if got, want := wr.DataSize(), 8; got != want {
437
-			t.Fatalf("Pop(%d)=%d bytes, want %d bytes", i, got, want)
438
-		}
439
-	}
440
-}
441
-
442
-func TestPriorityThrottleOutOfOrderWrites(t *testing.T) {
443
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{ThrottleOutOfOrderWrites: true})
444
-	ws.OpenStream(1, OpenStreamOptions{})
445
-	ws.OpenStream(2, OpenStreamOptions{PusherID: 1})
446
-
447
-	sc := &serverConn{maxFrameSize: 4096}
448
-	st1 := &stream{id: 1, sc: sc}
449
-	st2 := &stream{id: 2, sc: sc}
450
-	st1.flow.add(4096)
451
-	st2.flow.add(4096)
452
-	ws.Push(FrameWriteRequest{&writeData{2, make([]byte, 4096), false}, st2, nil})
453
-	ws.AdjustStream(2, PriorityParam{StreamDep: 1})
454
-
455
-	// We have enough flow-control bytes to write st2 in a single Pop call.
456
-	// However, due to out-of-order write throttling, the first call should
457
-	// only write 1KB.
458
-	wr, ok := ws.Pop()
459
-	if !ok {
460
-		t.Fatalf("Pop(st2.first)=false, want true")
461
-	}
462
-	if got, want := wr.StreamID(), uint32(2); got != want {
463
-		t.Fatalf("Pop(st2.first)=stream %d, want stream %d", got, want)
464
-	}
465
-	if got, want := wr.DataSize(), 1024; got != want {
466
-		t.Fatalf("Pop(st2.first)=%d bytes, want %d bytes", got, want)
467
-	}
468
-
469
-	// Now add data on st1. This should take precedence.
470
-	ws.Push(FrameWriteRequest{&writeData{1, make([]byte, 4096), false}, st1, nil})
471
-	wr, ok = ws.Pop()
472
-	if !ok {
473
-		t.Fatalf("Pop(st1)=false, want true")
474
-	}
475
-	if got, want := wr.StreamID(), uint32(1); got != want {
476
-		t.Fatalf("Pop(st1)=stream %d, want stream %d", got, want)
477
-	}
478
-	if got, want := wr.DataSize(), 4096; got != want {
479
-		t.Fatalf("Pop(st1)=%d bytes, want %d bytes", got, want)
480
-	}
481
-
482
-	// Should go back to writing 1KB from st2.
483
-	wr, ok = ws.Pop()
484
-	if !ok {
485
-		t.Fatalf("Pop(st2.last)=false, want true")
486
-	}
487
-	if got, want := wr.StreamID(), uint32(2); got != want {
488
-		t.Fatalf("Pop(st2.last)=stream %d, want stream %d", got, want)
489
-	}
490
-	if got, want := wr.DataSize(), 1024; got != want {
491
-		t.Fatalf("Pop(st2.last)=%d bytes, want %d bytes", got, want)
492
-	}
493
-}
494
-
495
-func TestPriorityWeights(t *testing.T) {
496
-	ws := defaultPriorityWriteScheduler()
497
-	ws.OpenStream(1, OpenStreamOptions{})
498
-	ws.OpenStream(2, OpenStreamOptions{})
499
-
500
-	sc := &serverConn{maxFrameSize: 8}
501
-	st1 := &stream{id: 1, sc: sc}
502
-	st2 := &stream{id: 2, sc: sc}
503
-	st1.flow.add(40)
504
-	st2.flow.add(40)
505
-
506
-	ws.Push(FrameWriteRequest{&writeData{1, make([]byte, 40), false}, st1, nil})
507
-	ws.Push(FrameWriteRequest{&writeData{2, make([]byte, 40), false}, st2, nil})
508
-	ws.AdjustStream(1, PriorityParam{StreamDep: 0, Weight: 34})
509
-	ws.AdjustStream(2, PriorityParam{StreamDep: 0, Weight: 9})
510
-
511
-	// st1 gets 3.5x the bandwidth of st2 (3.5 = (34+1)/(9+1)).
512
-	// The maximum frame size is 8 bytes. The write sequence should be:
513
-	//   st1, total bytes so far is (st1=8,  st=0)
514
-	//   st2, total bytes so far is (st1=8,  st=8)
515
-	//   st1, total bytes so far is (st1=16, st=8)
516
-	//   st1, total bytes so far is (st1=24, st=8)   // 3x bandwidth
517
-	//   st1, total bytes so far is (st1=32, st=8)   // 4x bandwidth
518
-	//   st2, total bytes so far is (st1=32, st=16)  // 2x bandwidth
519
-	//   st1, total bytes so far is (st1=40, st=16)
520
-	//   st2, total bytes so far is (st1=40, st=24)
521
-	//   st2, total bytes so far is (st1=40, st=32)
522
-	//   st2, total bytes so far is (st1=40, st=40)
523
-	if err := checkPopAll(ws, []uint32{1, 2, 1, 1, 1, 2, 1, 2, 2, 2}); err != nil {
524
-		t.Error(err)
525
-	}
526
-}
527
-
528
-func TestPriorityRstStreamOnNonOpenStreams(t *testing.T) {
529
-	ws := NewPriorityWriteScheduler(&PriorityWriteSchedulerConfig{
530
-		MaxClosedNodesInTree: 0,
531
-		MaxIdleNodesInTree:   0,
532
-	})
533
-	ws.OpenStream(1, OpenStreamOptions{})
534
-	ws.CloseStream(1)
535
-	ws.Push(FrameWriteRequest{write: streamError(1, ErrCodeProtocol)})
536
-	ws.Push(FrameWriteRequest{write: streamError(2, ErrCodeProtocol)})
537
-
538
-	if err := checkPopAll(ws, []uint32{1, 2}); err != nil {
539
-		t.Error(err)
540
-	}
541
-}

+ 0
- 72
vendor/golang.org/x/net/http2/writesched_random.go View File

1
-// Copyright 2014 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 http2
6
-
7
-import "math"
8
-
9
-// NewRandomWriteScheduler constructs a WriteScheduler that ignores HTTP/2
10
-// priorities. Control frames like SETTINGS and PING are written before DATA
11
-// frames, but if no control frames are queued and multiple streams have queued
12
-// HEADERS or DATA frames, Pop selects a ready stream arbitrarily.
13
-func NewRandomWriteScheduler() WriteScheduler {
14
-	return &randomWriteScheduler{sq: make(map[uint32]*writeQueue)}
15
-}
16
-
17
-type randomWriteScheduler struct {
18
-	// zero are frames not associated with a specific stream.
19
-	zero writeQueue
20
-
21
-	// sq contains the stream-specific queues, keyed by stream ID.
22
-	// When a stream is idle or closed, it's deleted from the map.
23
-	sq map[uint32]*writeQueue
24
-
25
-	// pool of empty queues for reuse.
26
-	queuePool writeQueuePool
27
-}
28
-
29
-func (ws *randomWriteScheduler) OpenStream(streamID uint32, options OpenStreamOptions) {
30
-	// no-op: idle streams are not tracked
31
-}
32
-
33
-func (ws *randomWriteScheduler) CloseStream(streamID uint32) {
34
-	q, ok := ws.sq[streamID]
35
-	if !ok {
36
-		return
37
-	}
38
-	delete(ws.sq, streamID)
39
-	ws.queuePool.put(q)
40
-}
41
-
42
-func (ws *randomWriteScheduler) AdjustStream(streamID uint32, priority PriorityParam) {
43
-	// no-op: priorities are ignored
44
-}
45
-
46
-func (ws *randomWriteScheduler) Push(wr FrameWriteRequest) {
47
-	id := wr.StreamID()
48
-	if id == 0 {
49
-		ws.zero.push(wr)
50
-		return
51
-	}
52
-	q, ok := ws.sq[id]
53
-	if !ok {
54
-		q = ws.queuePool.get()
55
-		ws.sq[id] = q
56
-	}
57
-	q.push(wr)
58
-}
59
-
60
-func (ws *randomWriteScheduler) Pop() (FrameWriteRequest, bool) {
61
-	// Control frames first.
62
-	if !ws.zero.empty() {
63
-		return ws.zero.shift(), true
64
-	}
65
-	// Iterate over all non-idle streams until finding one that can be consumed.
66
-	for _, q := range ws.sq {
67
-		if wr, ok := q.consume(math.MaxInt32); ok {
68
-			return wr, true
69
-		}
70
-	}
71
-	return FrameWriteRequest{}, false
72
-}

+ 0
- 0
vendor/golang.org/x/net/http2/writesched_random_test.go View File


Some files were not shown because too many files changed in this diff