-
Notifications
You must be signed in to change notification settings - Fork 1
/
t2p.parse.go
70 lines (58 loc) · 1.2 KB
/
t2p.parse.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
package elio
import (
"bufio"
"bytes"
"io"
"strings"
)
const delimiterLfLf string = "\n\n"
const delimiterCrLfCrLf string = "\r\n\r\n"
// T2pOnParse control on parse
func T2pOnParse(in []byte) (lenParsed, lenDelimit int, ok bool) {
ok = true
if lenParsed = bytes.Index(in, []byte(delimiterLfLf)); 0 <= lenParsed {
lenDelimit = len(delimiterLfLf)
} else if lenParsed = bytes.Index(in, []byte(delimiterCrLfCrLf)); 0 <= lenParsed {
lenDelimit = len(delimiterCrLfCrLf)
} else {
ok = false
}
return lenParsed, lenDelimit, ok
}
// T2pParseCommand parse command
func T2pParseCommand(in []byte) (out []byte) {
lenIn := len(in)
if 0 < lenIn {
out = in[:lenIn]
var c = 0
// todo: process request in here
for i := 0; i < lenIn; i++ {
if '\b' == in[i] {
if 0 < c {
c--
}
} else {
out[c] = in[i]
c++
}
}
out = out[:c]
}
return out
}
// T2PParse T2P parse
func T2PParse(in []byte) (out []string) {
buffer := bytes.NewBuffer(in)
reader := bufio.NewReader(buffer)
for {
line, _, err := reader.ReadLine()
if io.EOF == err {
break
}
l := strings.TrimSpace(string(line))
if "" != l {
out = append(out, strings.TrimSpace(string(l)))
}
}
return out
}