| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/hex" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"strconv" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/go-git/go-git/v5/plumbing" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 13 |
) |
| 14 |
|
| 15 |
// Decode reads the next upload-request form its input and |
| 16 |
// stores it in the UploadRequest. |
| 17 |
func (req *UploadRequest) Decode(r io.Reader) error { |
| 18 |
d := newUlReqDecoder(r) |
| 19 |
return d.Decode(req) |
| 20 |
} |
| 21 |
|
| 22 |
type ulReqDecoder struct { |
| 23 |
s *pktline.Scanner // a pkt-line scanner from the input stream |
| 24 |
line []byte // current pkt-line contents, use parser.nextLine() to make it advance |
| 25 |
nLine int // current pkt-line number for debugging, begins at 1 |
| 26 |
err error // sticky error, use the parser.error() method to fill this out |
| 27 |
data *UploadRequest // parsed data is stored here |
| 28 |
} |
| 29 |
|
| 30 |
func newUlReqDecoder(r io.Reader) *ulReqDecoder { |
| 31 |
return &ulReqDecoder{ |
| 32 |
s: pktline.NewScanner(r), |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func (d *ulReqDecoder) Decode(v *UploadRequest) error { |
| 37 |
d.data = v |
| 38 |
|
| 39 |
for state := d.decodeFirstWant; state != nil; { |
| 40 |
state = state() |
| 41 |
} |
| 42 |
|
| 43 |
return d.err |
| 44 |
} |
| 45 |
|
| 46 |
// fills out the parser sticky error |
| 47 |
func (d *ulReqDecoder) error(format string, a ...interface{}) { |
| 48 |
msg := fmt.Sprintf( |
| 49 |
"pkt-line %d: %s", d.nLine, |
| 50 |
fmt.Sprintf(format, a...), |
| 51 |
) |
| 52 |
|
| 53 |
d.err = NewErrUnexpectedData(msg, d.line) |
| 54 |
} |
| 55 |
|
| 56 |
// Reads a new pkt-line from the scanner, makes its payload available as |
| 57 |
// p.line and increments p.nLine. A successful invocation returns true, |
| 58 |
// otherwise, false is returned and the sticky error is filled out |
| 59 |
// accordingly. Trims eols at the end of the payloads. |
| 60 |
func (d *ulReqDecoder) nextLine() bool { |
| 61 |
d.nLine++ |
| 62 |
|
| 63 |
if !d.s.Scan() { |
| 64 |
if d.err = d.s.Err(); d.err != nil { |
| 65 |
return false |
| 66 |
} |
| 67 |
|
| 68 |
d.error("EOF") |
| 69 |
return false |
| 70 |
} |
| 71 |
|
| 72 |
d.line = d.s.Bytes() |
| 73 |
d.line = bytes.TrimSuffix(d.line, eol) |
| 74 |
|
| 75 |
return true |
| 76 |
} |
| 77 |
|
| 78 |
// Expected format: want <hash>[ capabilities] |
| 79 |
func (d *ulReqDecoder) decodeFirstWant() stateFn { |
| 80 |
if ok := d.nextLine(); !ok { |
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
if !bytes.HasPrefix(d.line, want) { |
| 85 |
d.error("missing 'want ' prefix") |
| 86 |
return nil |
| 87 |
} |
| 88 |
d.line = bytes.TrimPrefix(d.line, want) |
| 89 |
|
| 90 |
hash, ok := d.readHash() |
| 91 |
if !ok { |
| 92 |
return nil |
| 93 |
} |
| 94 |
d.data.Wants = append(d.data.Wants, hash) |
| 95 |
|
| 96 |
return d.decodeCaps |
| 97 |
} |
| 98 |
|
| 99 |
func (d *ulReqDecoder) readHash() (plumbing.Hash, bool) { |
| 100 |
if len(d.line) < hashSize { |
| 101 |
d.err = fmt.Errorf("malformed hash: %v", d.line) |
| 102 |
return plumbing.ZeroHash, false |
| 103 |
} |
| 104 |
|
| 105 |
var hash plumbing.Hash |
| 106 |
if _, err := hex.Decode(hash[:], d.line[:hashSize]); err != nil { |
| 107 |
d.error("invalid hash text: %s", err) |
| 108 |
return plumbing.ZeroHash, false |
| 109 |
} |
| 110 |
d.line = d.line[hashSize:] |
| 111 |
|
| 112 |
return hash, true |
| 113 |
} |
| 114 |
|
| 115 |
// Expected format: sp cap1 sp cap2 sp cap3... |
| 116 |
func (d *ulReqDecoder) decodeCaps() stateFn { |
| 117 |
d.line = bytes.TrimPrefix(d.line, sp) |
| 118 |
if err := d.data.Capabilities.Decode(d.line); err != nil { |
| 119 |
d.error("invalid capabilities: %s", err) |
| 120 |
} |
| 121 |
|
| 122 |
return d.decodeOtherWants |
| 123 |
} |
| 124 |
|
| 125 |
// Expected format: want <hash> |
| 126 |
func (d *ulReqDecoder) decodeOtherWants() stateFn { |
| 127 |
if ok := d.nextLine(); !ok { |
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
if bytes.HasPrefix(d.line, shallow) { |
| 132 |
return d.decodeShallow |
| 133 |
} |
| 134 |
|
| 135 |
if bytes.HasPrefix(d.line, deepen) { |
| 136 |
return d.decodeDeepen |
| 137 |
} |
| 138 |
|
| 139 |
if len(d.line) == 0 { |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
if !bytes.HasPrefix(d.line, want) { |
| 144 |
d.error("unexpected payload while expecting a want: %q", d.line) |
| 145 |
return nil |
| 146 |
} |
| 147 |
d.line = bytes.TrimPrefix(d.line, want) |
| 148 |
|
| 149 |
hash, ok := d.readHash() |
| 150 |
if !ok { |
| 151 |
return nil |
| 152 |
} |
| 153 |
d.data.Wants = append(d.data.Wants, hash) |
| 154 |
|
| 155 |
return d.decodeOtherWants |
| 156 |
} |
| 157 |
|
| 158 |
// Expected format: shallow <hash> |
| 159 |
func (d *ulReqDecoder) decodeShallow() stateFn { |
| 160 |
if bytes.HasPrefix(d.line, deepen) { |
| 161 |
return d.decodeDeepen |
| 162 |
} |
| 163 |
|
| 164 |
if len(d.line) == 0 { |
| 165 |
return nil |
| 166 |
} |
| 167 |
|
| 168 |
if !bytes.HasPrefix(d.line, shallow) { |
| 169 |
d.error("unexpected payload while expecting a shallow: %q", d.line) |
| 170 |
return nil |
| 171 |
} |
| 172 |
d.line = bytes.TrimPrefix(d.line, shallow) |
| 173 |
|
| 174 |
hash, ok := d.readHash() |
| 175 |
if !ok { |
| 176 |
return nil |
| 177 |
} |
| 178 |
d.data.Shallows = append(d.data.Shallows, hash) |
| 179 |
|
| 180 |
if ok := d.nextLine(); !ok { |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
return d.decodeShallow |
| 185 |
} |
| 186 |
|
| 187 |
// Expected format: deepen <n> / deepen-since <ul> / deepen-not <ref> |
| 188 |
func (d *ulReqDecoder) decodeDeepen() stateFn { |
| 189 |
if bytes.HasPrefix(d.line, deepenCommits) { |
| 190 |
return d.decodeDeepenCommits |
| 191 |
} |
| 192 |
|
| 193 |
if bytes.HasPrefix(d.line, deepenSince) { |
| 194 |
return d.decodeDeepenSince |
| 195 |
} |
| 196 |
|
| 197 |
if bytes.HasPrefix(d.line, deepenReference) { |
| 198 |
return d.decodeDeepenReference |
| 199 |
} |
| 200 |
|
| 201 |
if len(d.line) == 0 { |
| 202 |
return nil |
| 203 |
} |
| 204 |
|
| 205 |
d.error("unexpected deepen specification: %q", d.line) |
| 206 |
return nil |
| 207 |
} |
| 208 |
|
| 209 |
func (d *ulReqDecoder) decodeDeepenCommits() stateFn { |
| 210 |
d.line = bytes.TrimPrefix(d.line, deepenCommits) |
| 211 |
|
| 212 |
var n int |
| 213 |
if n, d.err = strconv.Atoi(string(d.line)); d.err != nil { |
| 214 |
return nil |
| 215 |
} |
| 216 |
if n < 0 { |
| 217 |
d.err = fmt.Errorf("negative depth") |
| 218 |
return nil |
| 219 |
} |
| 220 |
d.data.Depth = DepthCommits(n) |
| 221 |
|
| 222 |
return d.decodeFlush |
| 223 |
} |
| 224 |
|
| 225 |
func (d *ulReqDecoder) decodeDeepenSince() stateFn { |
| 226 |
d.line = bytes.TrimPrefix(d.line, deepenSince) |
| 227 |
|
| 228 |
var secs int64 |
| 229 |
secs, d.err = strconv.ParseInt(string(d.line), 10, 64) |
| 230 |
if d.err != nil { |
| 231 |
return nil |
| 232 |
} |
| 233 |
t := time.Unix(secs, 0).UTC() |
| 234 |
d.data.Depth = DepthSince(t) |
| 235 |
|
| 236 |
return d.decodeFlush |
| 237 |
} |
| 238 |
|
| 239 |
func (d *ulReqDecoder) decodeDeepenReference() stateFn { |
| 240 |
d.line = bytes.TrimPrefix(d.line, deepenReference) |
| 241 |
|
| 242 |
d.data.Depth = DepthReference(string(d.line)) |
| 243 |
|
| 244 |
return d.decodeFlush |
| 245 |
} |
| 246 |
|
| 247 |
func (d *ulReqDecoder) decodeFlush() stateFn { |
| 248 |
if ok := d.nextLine(); !ok { |
| 249 |
return nil |
| 250 |
} |
| 251 |
|
| 252 |
if len(d.line) != 0 { |
| 253 |
d.err = fmt.Errorf("unexpected payload while expecting a flush-pkt: %q", d.line) |
| 254 |
} |
| 255 |
|
| 256 |
return nil |
| 257 |
} |