-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadreq.go
268 lines (251 loc) · 7.09 KB
/
uploadreq.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
// 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 (
"fmt"
"io"
"strconv"
"strings"
)
type UploadRequestState int
const (
UploadRequestBegin UploadRequestState = iota
UploadRequestScanWants
UploadRequestScanShallows
UploadRequestScanDepth
UploadRequestScanFilter
UploadRequestBeginNegotiationOrDoneOrEnd
UploadRequestNegotiation
UploadRequestScanHaves
UploadRequestEnd
)
// UploadRequestChunk is a chunk of a protocol v1 git-upload-pack
// request.
type UploadRequestChunk struct {
Capabilities []string
WantObjectID string
ShallowObjectID string
DeepenDepth int
// Not documented, but seconds from UNIX epoch.
DeepenSince uint64
DeepenNotRef string
FilterSpec string
HaveObjectID string
EndOneRound bool
NoMoreNegotiation bool
}
// EncodeToPktLine serializes the chunk.
func (c *UploadRequestChunk) EncodeToPktLine() []byte {
if len(c.Capabilities) > 0 && c.WantObjectID != "" {
return BytesPacket([]byte(fmt.Sprintf("want %s %s\n", c.WantObjectID, strings.Join(c.Capabilities, " ")))).EncodeToPktLine()
}
if c.WantObjectID != "" {
return BytesPacket([]byte(fmt.Sprintf("want %s\n", c.WantObjectID))).EncodeToPktLine()
}
if c.ShallowObjectID != "" {
return BytesPacket([]byte(fmt.Sprintf("shallow %s\n", c.ShallowObjectID))).EncodeToPktLine()
}
if c.DeepenDepth != 0 {
return BytesPacket([]byte(fmt.Sprintf("deepen %d\n", c.DeepenDepth))).EncodeToPktLine()
}
if c.DeepenSince != 0 {
return BytesPacket([]byte(fmt.Sprintf("deepen-since %d\n", c.DeepenSince))).EncodeToPktLine()
}
if c.DeepenNotRef != "" {
return BytesPacket([]byte(fmt.Sprintf("deepen-not %s\n", c.DeepenNotRef))).EncodeToPktLine()
}
if c.FilterSpec != "" {
return BytesPacket([]byte(fmt.Sprintf("filter %s\n", c.FilterSpec))).EncodeToPktLine()
}
if c.HaveObjectID != "" {
return BytesPacket([]byte(fmt.Sprintf("have %s\n", c.HaveObjectID))).EncodeToPktLine()
}
if c.EndOneRound {
return FlushPacket{}.EncodeToPktLine()
}
if c.NoMoreNegotiation {
return BytesPacket([]byte("done\n")).EncodeToPktLine()
}
panic("impossible chunk")
}
// UploadRequest provides an interface for reading a protocol v1
// git-upload-pack request.
type UploadRequest struct {
scanner *PacketScanner
state UploadRequestState
err error
curr *UploadRequestChunk
}
// NewUploadRequest returns a new UploadRequest to
// read from rd.
func NewUploadRequest(rd io.Reader) *UploadRequest {
return &UploadRequest{scanner: NewPacketScanner(rd)}
}
// Err returns the first non-EOF error that was encountered by the
// ProtocolV1UploadPackRequest.
func (r *UploadRequest) Err() error {
return r.err
}
// Chunk returns the most recent chunk generated by a call to Scan.
func (r *UploadRequest) Chunk() *UploadRequestChunk {
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 *UploadRequest) Scan() bool {
if r.err != nil || r.state == UploadRequestEnd {
return false
}
if !r.scanner.Scan() {
r.err = r.scanner.Err()
if r.err == nil && r.state != UploadRequestBeginNegotiationOrDoneOrEnd {
r.err = SyntaxError("early EOF")
}
return false
}
pkt := r.scanner.Packet()
if r.state == UploadRequestBegin {
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
ss := strings.SplitN(string(bp), " ", 3)
if len(ss) < 2 {
r.err = SyntaxError("cannot split wants: " + string(bp))
return false
}
caps := []string{}
if len(ss) == 3 {
if capStr := strings.TrimSuffix(ss[2], "\n"); capStr != "" {
// This is to avoid strings.Split("", " ") => []string{""}.
caps = strings.Split(capStr, " ")
}
}
if ss[0] != "want" {
r.err = SyntaxError("the first packet is not want: " + string(bp))
}
r.state = UploadRequestScanWants
r.curr = &UploadRequestChunk{
Capabilities: caps,
WantObjectID: ss[1],
}
return true
}
if _, ok := pkt.(FlushPacket); ok {
r.state = UploadRequestBeginNegotiationOrDoneOrEnd
r.curr = &UploadRequestChunk{
EndOneRound: true,
}
return true
}
bp, ok := pkt.(BytesPacket)
if !ok {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
s := strings.TrimSuffix(string(bp), "\n")
if s == "done" {
if r.state == UploadRequestNegotiation || r.state == UploadRequestBeginNegotiationOrDoneOrEnd {
r.state = UploadRequestEnd
r.curr = &UploadRequestChunk{
NoMoreNegotiation: true,
}
return true
}
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
ss := strings.SplitN(s, " ", 2)
if len(ss) != 2 {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
switch r.state {
case UploadRequestScanWants:
if ss[0] == "want" {
r.curr = &UploadRequestChunk{
WantObjectID: ss[1],
}
return true
}
fallthrough
case UploadRequestScanShallows:
if ss[0] == "shallow" {
r.state = UploadRequestScanShallows
r.curr = &UploadRequestChunk{
ShallowObjectID: ss[1],
}
return true
}
fallthrough
case UploadRequestScanDepth:
if ss[0] == "deepen" {
depth, err := strconv.ParseInt(ss[1], 10, strconv.IntSize)
if err != nil {
r.err = SyntaxError("cannot parse depth")
return false
}
r.state = UploadRequestScanFilter
r.curr = &UploadRequestChunk{
DeepenDepth: int(depth),
}
return true
}
if ss[0] == "deepen-since" {
since, err := strconv.ParseUint(ss[1], 10, 64)
if err != nil {
r.err = SyntaxError("cannot parse depth")
return false
}
r.state = UploadRequestScanFilter
r.curr = &UploadRequestChunk{
DeepenSince: since,
}
return true
}
if ss[0] == "deepen-not" {
r.state = UploadRequestScanFilter
r.curr = &UploadRequestChunk{
DeepenNotRef: ss[1],
}
return true
}
fallthrough
case UploadRequestScanFilter:
if ss[0] != "filter" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
r.state = UploadRequestNegotiation
r.curr = &UploadRequestChunk{
FilterSpec: ss[1],
}
return true
case UploadRequestNegotiation, UploadRequestBeginNegotiationOrDoneOrEnd:
if ss[0] != "have" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
r.state = UploadRequestNegotiation
r.curr = &UploadRequestChunk{
HaveObjectID: ss[1],
}
return true
}
panic("impossible state")
}