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
|
package receivepack
import (
"fmt"
"strings"
"codeberg.org/lindenii/furgit/common/iowrap"
common "codeberg.org/lindenii/furgit/network/protocol/v0v1/server"
objectid "codeberg.org/lindenii/furgit/object/id"
)
// Session is one stateful server-side receive-pack protocol session.
//
// Labels: MT-Unsafe.
type Session struct {
base *common.Session
supported Capabilities
negotiated Capabilities
}
// NewSession creates one receive-pack session over one common server session.
//
// Labels: Deps-Borrowed, Life-Parent.
func NewSession(base *common.Session, supported Capabilities) *Session {
return &Session{
base: base,
supported: supported,
}
}
// AdvertiseRefs writes one receive-pack ref advertisement.
func (session *Session) AdvertiseRefs(ad common.Advertisement) error {
return session.base.AdvertiseRefs(ad, session.supported.Tokens(session.base.Algorithm()))
}
// ReadRequest reads one receive-pack request through optional push-options.
func (session *Session) ReadRequest() (*Request, error) {
req := &Request{}
var sawCommands bool
for {
frame, err := session.base.ReadFrame()
if err != nil {
return nil, err
}
switch frame.Type {
case common.FrameFlush:
goto afterCommands
case common.FrameData:
case common.FrameDelim, common.FrameResponseEnd:
return nil, &ProtocolError{Reason: fmt.Sprintf("unexpected packet type %v", frame.Type)}
}
payload := string(frame.Payload)
if strings.HasPrefix(payload, "shallow ") {
line := trimOneLF(payload)
shallowID, err := parseObjectID(session.base.Algorithm(), line[len("shallow "):])
if err != nil {
return nil, err
}
req.Shallow = append(req.Shallow, shallowID)
continue
}
if strings.HasPrefix(payload, "push-cert\x00") {
if sawCommands {
return nil, &ProtocolError{Reason: "got both push certificate and unsigned commands"}
}
capabilityTokens, err := parseCapabilityList(payload[len("push-cert\x00"):])
if err != nil {
return nil, err
}
requested, err := parseRequestedCapabilities(
capabilityTokens,
session.supported,
session.base.Algorithm(),
)
if err != nil {
return nil, err
}
req.Capabilities = requested
cert, err := session.readPushCertificate()
if err != nil {
return nil, err
}
req.PushCert = cert
req.Commands = append(req.Commands, cert.Commands...)
sawCommands = true
continue
}
line := trimOneLF(payload)
if !sawCommands && strings.Contains(line, "\x00") {
commandPart, capPart, _ := strings.Cut(line, "\x00")
capabilityTokens, err := parseCapabilityList(capPart)
if err != nil {
return nil, err
}
requested, err := parseRequestedCapabilities(
capabilityTokens,
session.supported,
session.base.Algorithm(),
)
if err != nil {
return nil, err
}
req.Capabilities = requested
line = commandPart
}
cmd, err := parseCommand(session.base.Algorithm(), line)
if err != nil {
return nil, err
}
req.Commands = append(req.Commands, cmd)
sawCommands = true
}
afterCommands:
if req.Capabilities.PushOptions {
for {
frame, err := session.base.ReadFrame()
if err != nil {
return nil, err
}
switch frame.Type {
case common.FrameFlush:
goto afterPushOptions
case common.FrameData:
req.PushOptions = append(req.PushOptions, trimOneLF(string(frame.Payload)))
case common.FrameDelim, common.FrameResponseEnd:
return nil, &ProtocolError{Reason: fmt.Sprintf("unexpected packet type %v", frame.Type)}
}
}
}
afterPushOptions:
req.DeleteOnly = deleteOnly(req.Commands)
req.PackExpected = len(req.Commands) > 0 && !req.DeleteOnly
session.negotiated = req.Capabilities
if req.Capabilities.SideBand64K {
session.base.EnableSideBand64K()
}
return req, nil
}
// WriteProgress writes one progress packet.
func (session *Session) WriteProgress(p []byte) error {
return session.base.WriteProgress(p)
}
// ProgressWriter returns one chunking writer for sideband progress output.
//
// When side-band-64k was not negotiated, writes are discarded.
//
// Labels: Life-Parent.
func (session *Session) ProgressWriter() iowrap.WriteFlusher {
return session.base.ProgressWriter()
}
// WriteError writes one fatal error packet.
func (session *Session) WriteError(p []byte) error {
return session.base.WriteError(p)
}
// ErrorWriter returns one chunking writer for sideband error output.
//
// When side-band-64k was not negotiated, writes are discarded.
//
// Labels: Life-Parent.
func (session *Session) ErrorWriter() iowrap.WriteFlusher {
return session.base.ErrorWriter()
}
func trimOneLF(s string) string {
return strings.TrimSuffix(s, "\n")
}
func parseObjectID(algo objectid.Algorithm, s string) (objectid.ObjectID, error) {
id, err := objectid.ParseHex(algo, s)
if err != nil {
return objectid.ObjectID{}, &ProtocolError{
Reason: fmt.Sprintf("invalid object id %q", s),
}
}
return id, nil
}
func commandIsDelete(cmd Command) bool {
return cmd.NewID == objectid.Zero(cmd.NewID.Algorithm())
}
func deleteOnly(commands []Command) bool {
if len(commands) == 0 {
return false
}
for _, cmd := range commands {
if !commandIsDelete(cmd) {
return false
}
}
return true
}
func parseCommand(algo objectid.Algorithm, line string) (Command, error) {
fields := strings.Fields(line)
if len(fields) != 3 {
return Command{}, &ProtocolError{Reason: fmt.Sprintf("malformed command %q", line)}
}
oldID, err := parseObjectID(algo, fields[0])
if err != nil {
return Command{}, err
}
newID, err := parseObjectID(algo, fields[1])
if err != nil {
return Command{}, err
}
return Command{OldID: oldID, NewID: newID, Name: fields[2]}, nil
}
func (session *Session) readPushCertificate() (*PushCertificate, error) {
cert := &PushCertificate{}
inCommands := false
inSignature := false
for {
frame, err := session.base.ReadFrame()
if err != nil {
return nil, err
}
switch frame.Type {
case common.FrameFlush:
return nil, &ProtocolError{Reason: "unexpected flush inside push certificate"}
case common.FrameData:
case common.FrameDelim, common.FrameResponseEnd:
return nil, &ProtocolError{Reason: fmt.Sprintf("unexpected packet type %v", frame.Type)}
}
line := string(frame.Payload)
if line == "push-cert-end\n" {
return cert, nil
}
if !inCommands {
if line == "\n" {
inCommands = true
continue
}
trimmed := trimOneLF(line)
cert.HeaderLines = append(cert.HeaderLines, trimmed)
if strings.HasPrefix(trimmed, "push-option ") {
cert.EmbeddedOption = append(cert.EmbeddedOption, trimmed[len("push-option "):])
}
continue
}
if !inSignature {
trimmed := trimOneLF(line)
cmd, err := parseCommand(session.base.Algorithm(), trimmed)
if err == nil {
cert.Commands = append(cert.Commands, cmd)
continue
}
inSignature = true
}
cert.SignatureLines = append(cert.SignatureLines, trimOneLF(line))
}
}
|