-
Notifications
You must be signed in to change notification settings - Fork 0
/
receivereq.go
448 lines (429 loc) · 12.4 KB
/
receivereq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Modified by Giacomo Tartari
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pkt
import (
"bytes"
"fmt"
"io"
"strings"
)
type ReceiveRequestState int
const (
ReceiveRequestBegin ReceiveRequestState = iota
ReceiveRequestScanCommandAndCapabilities
ReceiveRequestScanCommand
ReceiveRequestScanCert
ReceiveRequestScanCertVersion
ReceiveRequestScanCertPusher
ReceiveRequestScanCertPusheeOrNonce
ReceiveRequestScanCertNonce
ReceiveRequestScanOptionalCertPushOptions
ReceiveRequestScanCertCommand
ReceiveRequestScanCertGPGLine
ReceiveRequestScanOptionalPushOptions
ReceiveRequestScanPushOptions
ReceiveRequestScanPackFile
)
// ReceiveRequestChunk is a chunk of a protocol v1
// git-receive-pack request.
type ReceiveRequestChunk struct {
ClientShallow string
Capabilities []string
OldObjectID string
NewObjectID string
RefName string
EndOfCommands bool
StartOfPushCert bool
PushCertHeader bool
Pusher string
Pushee string
Nonce string
CertPushOption string
EndOfCertPushOptions bool
GPGSignaturePart []byte
EndOfPushCert bool
PushOption string
EndOfPushOptions bool
PackStream []byte
}
// EncodeToPktLine serializes the chunk.
func (c *ReceiveRequestChunk) EncodeToPktLine() []byte {
if c.ClientShallow != "" {
return BytesPacket([]byte(fmt.Sprintf("shallow %s\n", c.ClientShallow))).EncodeToPktLine()
}
if c.OldObjectID != "" && c.NewObjectID != "" && c.RefName != "" {
if len(c.Capabilities) != 0 {
return BytesPacket([]byte(fmt.Sprintf("%s %s %s\x00%s\n", c.OldObjectID, c.NewObjectID, c.RefName, strings.Join(c.Capabilities, " ")))).EncodeToPktLine()
}
return BytesPacket([]byte(fmt.Sprintf("%s %s %s\n", c.OldObjectID, c.NewObjectID, c.RefName))).EncodeToPktLine()
}
if c.EndOfCommands {
return FlushPacket{}.EncodeToPktLine()
}
if c.PushOption != "" {
return BytesPacket([]byte(fmt.Sprintf("%s\n", c.PushOption))).EncodeToPktLine()
}
if c.EndOfPushOptions {
return FlushPacket{}.EncodeToPktLine()
}
if c.StartOfPushCert {
return BytesPacket([]byte(fmt.Sprintf("push-cert\x00%s\n", strings.Join(c.Capabilities, " ")))).EncodeToPktLine()
}
if c.PushCertHeader {
return BytesPacket([]byte("certificate version 0.1\n")).EncodeToPktLine()
}
if c.Pusher != "" {
return BytesPacket([]byte(fmt.Sprintf("pusher %s\n", c.Pusher))).EncodeToPktLine()
}
if c.Pushee != "" {
return BytesPacket([]byte(fmt.Sprintf("pushee %s\n", c.Pushee))).EncodeToPktLine()
}
if c.Nonce != "" {
return BytesPacket([]byte(fmt.Sprintf("nonce %s\n", c.Nonce))).EncodeToPktLine()
}
if c.CertPushOption != "" {
return BytesPacket([]byte(fmt.Sprintf("push-option %s\n", c.CertPushOption))).EncodeToPktLine()
}
if c.EndOfCertPushOptions {
return BytesPacket([]byte("\n")).EncodeToPktLine()
}
if len(c.GPGSignaturePart) != 0 {
return BytesPacket(c.GPGSignaturePart).EncodeToPktLine()
}
if c.EndOfPushCert {
return BytesPacket([]byte("push-cert-end\n")).EncodeToPktLine()
}
// TODO
if len(c.PackStream) != 0 {
return c.PackStream
}
panic("impossible chunk")
}
// ReceiveRequest provides an interface for reading a protocol v1
// git-receive-pack request.
type ReceiveRequest struct {
scanner *PacketScanner
state ReceiveRequestState
err error
curr *ReceiveRequestChunk
}
// NewReceiveRequest returns a new ProtocolV1ReceivePackRequest to
// read from rd.
func NewReceiveRequest(rd io.Reader) *ReceiveRequest {
return &ReceiveRequest{scanner: NewPacketScanner(rd)}
}
// Err returns the first non-EOF error that was encountered by the
// ProtocolV1ReceivePackRequest.
func (r *ReceiveRequest) Err() error {
return r.err
}
// Chunk returns the most recent chunk generated by a call to Scan.
func (r *ReceiveRequest) Chunk() *ReceiveRequestChunk {
return r.curr
}
// Scan advances the scanner to the next packet. It returns false when the scan
// stops, either by reaching the end of the input or an error. After scan
// returns false, the Err method will return any error that occurred during
// scanning, except that if it was io.EOF, Err will return nil.
func (r *ReceiveRequest) Scan() bool {
if r.err != nil {
return false
}
if !r.scanner.Scan() {
r.err = r.scanner.Err()
if r.err == nil && r.state != ReceiveRequestScanOptionalPushOptions && r.state != ReceiveRequestScanPackFile {
r.err = SyntaxError("early EOF")
}
return false
}
pkt := r.scanner.Packet()
transition:
switch r.state {
case ReceiveRequestBegin:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
if bytes.HasPrefix(bp, []byte("shallow ")) {
r.curr = &ReceiveRequestChunk{
ClientShallow: strings.TrimPrefix(strings.TrimSuffix(string(bp), "\n"), "shallow "),
}
return true
}
if bytes.HasPrefix(bp, []byte("push-cert\x00")) {
r.state = ReceiveRequestScanCert
goto transition
}
r.state = ReceiveRequestScanCommandAndCapabilities
goto transition
case ReceiveRequestScanCommandAndCapabilities:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
zss := bytes.SplitN(bp, []byte{0}, 2)
if len(zss) != 2 {
r.err = SyntaxError("cannot split into two: " + string(bp))
return false
}
caps := []string{}
if capStr := strings.TrimPrefix(strings.TrimSuffix(string(zss[1]), "\n"), " "); capStr != "" {
// This is to avoid strings.Split("", " ") => []string{""}.
caps = strings.Split(capStr, " ")
}
ss := strings.SplitN(string(zss[0]), " ", 3)
if len(ss) != 3 {
r.err = SyntaxError("cannot split into three: " + string(zss[0]))
return false
}
r.state = ReceiveRequestScanCommand
r.curr = &ReceiveRequestChunk{
Capabilities: caps,
OldObjectID: ss[0],
NewObjectID: ss[1],
RefName: ss[2],
}
return true
case ReceiveRequestScanCommand:
switch p := pkt.(type) {
case FlushPacket:
r.state = ReceiveRequestScanOptionalPushOptions
r.curr = &ReceiveRequestChunk{
EndOfCommands: true,
}
return true
case BytesPacket:
ss := strings.SplitN(strings.TrimSuffix(string(p), "\n"), " ", 3)
if len(ss) != 3 {
r.err = SyntaxError("cannot split into three: " + string(p))
return false
}
r.curr = &ReceiveRequestChunk{
OldObjectID: ss[0],
NewObjectID: ss[1],
RefName: ss[2],
}
return true
default:
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", p))
return false
}
case ReceiveRequestScanCert:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
zss := bytes.SplitN(bp, []byte{0}, 2)
if len(zss) != 2 {
r.err = SyntaxError("cannot split into two: " + string(bp))
return false
}
caps := []string{}
if capStr := strings.TrimPrefix(strings.TrimSuffix(string(zss[1]), "\n"), " "); capStr != "" {
// This is to avoid strings.Split("", " ") => []string{""}.
caps = strings.Split(capStr, " ")
}
r.state = ReceiveRequestScanCertVersion
r.curr = &ReceiveRequestChunk{
Capabilities: caps,
StartOfPushCert: true,
}
return true
case ReceiveRequestScanCertVersion:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
if string(bp) != "certificate version 0.1\n" {
r.err = SyntaxError(fmt.Sprintf("unexpected certificate version: %#q", string(bp)))
return false
}
r.state = ReceiveRequestScanCertPusher
r.curr = &ReceiveRequestChunk{
PushCertHeader: true,
}
return true
case ReceiveRequestScanCertPusher:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
ss := strings.SplitN(strings.TrimSuffix(string(bp), "\n"), " ", 2)
if len(ss) != 2 {
r.err = SyntaxError("cannot split into two: " + string(bp))
return false
}
if ss[0] != "pusher" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", string(bp)))
return false
}
r.state = ReceiveRequestScanCertPusheeOrNonce
r.curr = &ReceiveRequestChunk{
Pusher: ss[1],
}
return true
case ReceiveRequestScanCertPusheeOrNonce:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
ss := strings.SplitN(strings.TrimSuffix(string(bp), "\n"), " ", 2)
if len(ss) != 2 {
r.err = SyntaxError("cannot split into two: " + string(bp))
return false
}
if ss[0] == "nonce" {
r.state = ReceiveRequestScanCertNonce
goto transition
}
if ss[0] != "pushee" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", string(bp)))
return false
}
r.state = ReceiveRequestScanCertNonce
r.curr = &ReceiveRequestChunk{
Pushee: ss[1],
}
return true
case ReceiveRequestScanCertNonce:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
ss := strings.SplitN(strings.TrimSuffix(string(bp), "\n"), " ", 2)
if len(ss) != 2 {
r.err = SyntaxError("cannot split into two: " + string(bp))
return false
}
if ss[0] != "nonce" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", string(bp)))
return false
}
r.state = ReceiveRequestScanOptionalCertPushOptions
r.curr = &ReceiveRequestChunk{
Nonce: ss[1],
}
return true
case ReceiveRequestScanOptionalCertPushOptions:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
if string(bp) == "\n" {
r.state = ReceiveRequestScanCertCommand
r.curr = &ReceiveRequestChunk{
EndOfCertPushOptions: true,
}
return true
}
ss := strings.SplitN(strings.TrimSuffix(string(bp), "\n"), " ", 2)
if len(ss) != 2 {
r.err = SyntaxError("cannot split into two: " + string(bp))
return false
}
if ss[0] != "push-option" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", string(bp)))
return false
}
r.curr = &ReceiveRequestChunk{
CertPushOption: ss[1],
}
return true
case ReceiveRequestScanCertCommand:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
if string(bp) == "-----BEGIN PGP SIGNATURE-----\n" {
r.state = ReceiveRequestScanCertGPGLine
goto transition
}
ss := strings.SplitN(strings.TrimSuffix(string(bp), "\n"), " ", 3)
if len(ss) != 3 {
r.err = SyntaxError("cannot split into three: " + string(bp))
return false
}
r.curr = &ReceiveRequestChunk{
OldObjectID: ss[0],
NewObjectID: ss[1],
RefName: ss[2],
}
return true
case ReceiveRequestScanCertGPGLine:
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
if string(bp) == "push-cert-end\n" {
r.state = ReceiveRequestScanPushOptions
r.curr = &ReceiveRequestChunk{
EndOfPushCert: true,
}
return true
}
r.curr = &ReceiveRequestChunk{
GPGSignaturePart: bp,
}
return true
case ReceiveRequestScanOptionalPushOptions:
if _, ok := pkt.(PackFileIndicatorPacket); ok {
r.state = ReceiveRequestScanPackFile
goto transition
}
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
r.state = ReceiveRequestScanPushOptions
r.curr = &ReceiveRequestChunk{
PushOption: strings.TrimSuffix(string(bp), "\n"),
}
return true
case ReceiveRequestScanPushOptions:
switch p := pkt.(type) {
case FlushPacket:
r.state = ReceiveRequestScanPackFile
r.curr = &ReceiveRequestChunk{
EndOfPushOptions: true,
}
return true
case BytesPacket:
r.curr = &ReceiveRequestChunk{
PushOption: strings.TrimSuffix(string(p), "\n"),
}
return true
default:
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", p))
return false
}
case ReceiveRequestScanPackFile:
r.curr = &ReceiveRequestChunk{
PackStream: pkt.EncodeToPktLine(),
}
return true
}
panic("impossible state")
}