-
Notifications
You must be signed in to change notification settings - Fork 0
/
pktline.go
122 lines (108 loc) · 3.04 KB
/
pktline.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
package githttp
import (
"errors"
"fmt"
"io"
"strconv"
)
var (
// ErrFlush is returned whtn the client sends an explicit flush packet.
ErrFlush = errors.New("flush")
)
const (
pktLineHeaderLength = 4
)
// A PktLineWriter implements git pkt-line protocol on top of an io.Writer. The
// documentation for the protocol can be found in
// https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt
type PktLineWriter struct {
w io.Writer
}
// NewPktLineWriter creates a new pkt-line based on the supplied Writer.
func NewPktLineWriter(w io.Writer) *PktLineWriter {
return &PktLineWriter{
w: w,
}
}
// Flush sends a flush-pkt, which is a special value in the pkt-line protocol.
func (w *PktLineWriter) Flush() error {
_, err := w.w.Write([]byte("0000"))
return err
}
// Close sends a flush-pkt.
func (w *PktLineWriter) Close() error {
return w.Flush()
}
// WritePktLine sends one pkt-line.
func (w *PktLineWriter) WritePktLine(data []byte) error {
if len(data)+pktLineHeaderLength > 0x10000 {
return errors.New("data too long")
}
if _, err := w.w.Write([]byte(fmt.Sprintf("%04x", pktLineHeaderLength+len(data)))); err != nil {
return err
}
_, err := w.w.Write(data)
return err
}
// A PktLineReader implements git pkt-line protocol on top of an io.Reader. The
// documentation for the protocol can be found in
// https://github.com/git/git/blob/master/Documentation/technical/protocol-common.txt
type PktLineReader struct {
r io.Reader
}
// NewPktLineReader creates a new pkt-line based on the supplied Reader.
func NewPktLineReader(r io.Reader) *PktLineReader {
return &PktLineReader{
r: r,
}
}
// ReadPktLine returns the next pkt-line. The special value of pkt-flush is
// represented by ErrFlush, to distinguish it from the empty pkt-line.
func (r *PktLineReader) ReadPktLine() ([]byte, error) {
hexLength := make([]byte, pktLineHeaderLength)
if _, err := io.ReadFull(r.r, hexLength); err != nil {
return nil, err
}
length, err := strconv.ParseUint(string(hexLength), 16, 16)
if err != nil {
return nil, err
}
if length == 0 {
return nil, ErrFlush
}
if length < pktLineHeaderLength {
return nil, io.ErrUnexpectedEOF
}
data := make([]byte, length-pktLineHeaderLength)
if _, err := io.ReadFull(r.r, data); err != nil {
return nil, err
}
return data, nil
}
// PktLineResponse represents an expected entry from PktLineReader.
type PktLineResponse struct {
Line string
Err error
}
// ComparePktLineResponse compares what is being read from the supplied Reader
// when interpreted by a PktLineReader against an expected list of
// PktLineResponses.
func ComparePktLineResponse(
r io.Reader,
expectedResponse []PktLineResponse,
) ([]PktLineResponse, bool) {
reader := NewPktLineReader(r)
actual := make([]PktLineResponse, 0)
ok := true
for _, expected := range expectedResponse {
line, err := reader.ReadPktLine()
actual = append(actual, PktLineResponse{string(line), err})
if expected.Err != err {
ok = false
}
if expected.Err == nil && expected.Line != string(line) {
ok = false
}
}
return actual, ok
}