-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitutils.go
More file actions
76 lines (67 loc) · 1.55 KB
/
gitutils.go
File metadata and controls
76 lines (67 loc) · 1.55 KB
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
package gitutils
import (
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"unicode"
)
const FlushPkt = 0
const DelimiterPkt = 1
const ResponseEndPkt = 2
func PktLine(s string) string {
len_s := len(s)
if len_s > 65515 {
return PktLine("ERR To long response.")
}
for i := 0; i < len_s; i++ {
if s[i] > unicode.MaxASCII {
return PktLine("ERR Non ASCII character found.")
}
}
length := len_s + 5
return fmt.Sprintf("%04x%s\n", length, s)
}
func WriteGitProtocol(w http.ResponseWriter, lines []string) {
for _, s := range lines {
fmt.Fprint(w, PktLine(s))
}
fmt.Fprint(w, "0000")
}
func ReadGitProtocol(r io.ReadCloser) ([]string, error) {
var lines []string
for {
b := make([]byte, 4)
n, err := r.Read(b)
if err == io.EOF {
break
} else if n != 4 {
return lines, errors.New("hex too short")
} else {
num, err0 := strconv.ParseUint(string(b), 16, 16)
if err0 != nil {
return lines, errors.New("wrong hex value")
} else if num >= 4 {
b_val := make([]byte, num-4)
n_val, err1 := r.Read(b_val)
if uint64(n_val) != num-4 || err1 == io.EOF {
return lines, errors.New("packet too short")
} else {
lines = append(lines, "p"+strings.TrimSuffix(string(b_val), "\n"))
}
} else if num == 3 {
return lines, errors.New("hex is 0003")
} else if num == FlushPkt {
lines = append(lines, "flush")
} else if num == DelimiterPkt {
lines = append(lines, "delimiter")
} else if num == ResponseEndPkt {
lines = append(lines, "responseend")
}
}
}
r.Close()
return lines, nil
}