This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathv1uploadpackreq.go
267 lines (250 loc) · 7.99 KB
/
v1uploadpackreq.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
// 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 gitprotocolio
import (
"fmt"
"io"
"strconv"
"strings"
)
type protocolV1UploadPackRequestState int
const (
protocolV1UploadPackRequestStateBegin protocolV1UploadPackRequestState = iota
protocolV1UploadPackRequestStateScanWants
protocolV1UploadPackRequestStateScanShallows
protocolV1UploadPackRequestStateScanDepth
protocolV1UploadPackRequestStateScanFilter
protocolV1UploadPackRequestStateBeginNegotiationOrDoneOrEnd
protocolV1UploadPackRequestStateNegotiation
protocolV1UploadPackRequestStateScanHaves
protocolV1UploadPackRequestStateEnd
)
// ProtocolV1UploadPackRequestChunk is a chunk of a protocol v1 git-upload-pack
// request.
type ProtocolV1UploadPackRequestChunk 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 *ProtocolV1UploadPackRequestChunk) 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")
}
// ProtocolV1UploadPackRequest provides an interface for reading a protocol v1
// git-upload-pack request.
type ProtocolV1UploadPackRequest struct {
scanner *PacketScanner
state protocolV1UploadPackRequestState
err error
curr *ProtocolV1UploadPackRequestChunk
}
// NewProtocolV1UploadPackRequest returns a new ProtocolV1UploadPackRequest to
// read from rd.
func NewProtocolV1UploadPackRequest(rd io.Reader) *ProtocolV1UploadPackRequest {
return &ProtocolV1UploadPackRequest{scanner: NewPacketScanner(rd)}
}
// Err returns the first non-EOF error that was encountered by the
// ProtocolV1UploadPackRequest.
func (r *ProtocolV1UploadPackRequest) Err() error {
return r.err
}
// Chunk returns the most recent chunk generated by a call to Scan.
func (r *ProtocolV1UploadPackRequest) Chunk() *ProtocolV1UploadPackRequestChunk {
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 *ProtocolV1UploadPackRequest) Scan() bool {
if r.err != nil || r.state == protocolV1UploadPackRequestStateEnd {
return false
}
if !r.scanner.Scan() {
r.err = r.scanner.Err()
if r.err == nil && r.state != protocolV1UploadPackRequestStateBeginNegotiationOrDoneOrEnd {
r.err = SyntaxError("early EOF")
}
return false
}
pkt := r.scanner.Packet()
if r.state == protocolV1UploadPackRequestStateBegin {
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 = protocolV1UploadPackRequestStateScanWants
r.curr = &ProtocolV1UploadPackRequestChunk{
Capabilities: caps,
WantObjectID: ss[1],
}
return true
}
if _, ok := pkt.(FlushPacket); ok {
r.state = protocolV1UploadPackRequestStateBeginNegotiationOrDoneOrEnd
r.curr = &ProtocolV1UploadPackRequestChunk{
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 == protocolV1UploadPackRequestStateNegotiation || r.state == protocolV1UploadPackRequestStateBeginNegotiationOrDoneOrEnd {
r.state = protocolV1UploadPackRequestStateEnd
r.curr = &ProtocolV1UploadPackRequestChunk{
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 protocolV1UploadPackRequestStateScanWants:
if ss[0] == "want" {
r.curr = &ProtocolV1UploadPackRequestChunk{
WantObjectID: ss[1],
}
return true
}
fallthrough
case protocolV1UploadPackRequestStateScanShallows:
if ss[0] == "shallow" {
r.state = protocolV1UploadPackRequestStateScanShallows
r.curr = &ProtocolV1UploadPackRequestChunk{
ShallowObjectID: ss[1],
}
return true
}
fallthrough
case protocolV1UploadPackRequestStateScanDepth:
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 = protocolV1UploadPackRequestStateScanFilter
r.curr = &ProtocolV1UploadPackRequestChunk{
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 = protocolV1UploadPackRequestStateScanFilter
r.curr = &ProtocolV1UploadPackRequestChunk{
DeepenSince: since,
}
return true
}
if ss[0] == "deepen-not" {
r.state = protocolV1UploadPackRequestStateScanFilter
r.curr = &ProtocolV1UploadPackRequestChunk{
DeepenNotRef: ss[1],
}
return true
}
fallthrough
case protocolV1UploadPackRequestStateScanFilter:
if ss[0] != "filter" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
r.state = protocolV1UploadPackRequestStateNegotiation
r.curr = &ProtocolV1UploadPackRequestChunk{
FilterSpec: ss[1],
}
return true
case protocolV1UploadPackRequestStateNegotiation, protocolV1UploadPackRequestStateBeginNegotiationOrDoneOrEnd:
if ss[0] != "have" {
r.err = SyntaxError(fmt.Sprintf("unexpected packet: %#v", pkt))
return false
}
r.state = protocolV1UploadPackRequestStateNegotiation
r.curr = &ProtocolV1UploadPackRequestChunk{
HaveObjectID: ss[1],
}
return true
}
panic("impossible state")
}