-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
118 lines (104 loc) · 2.33 KB
/
parser.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
/*
* Parses a RESP request and returns a Command struct containing
* the command type and any arguments.
*/
package main
import (
"io"
"log"
"net"
"strconv"
"strings"
)
type Command struct {
Type string
Args []string
}
type RespReader struct {
currentPos int
buffer []byte
arrLen int
}
/*
* Parses a RESP request and returns the parsed command and args.
*/
func parseResp(conn net.Conn) (Command) {
reader := NewRespReader(conn)
element, err := reader.NextElement()
if err == io.EOF {
panic("No commands found")
}
switch element := strings.ToUpper(element); element {
case "ECHO":
arg, err := reader.NextElement()
if err != nil {
log.Panic("Not enough arguments for ECHO")
}
return Command{Type: element, Args: []string{arg}}
case "PING":
return Command{Type: element}
case "COMMAND":
arg, err := reader.NextElement()
if err != nil {
log.Panic("Not enough arguments for COMMAND")
}
return Command{Type: element, Args: []string{arg}}
default:
// Catch all
return Command{Type: element}
}
}
/*
* Returns a RespReader after looking for an array length.
* Can panic if the request is malformed.
*/
func NewRespReader(conn net.Conn) *RespReader {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
if err != io.EOF {
log.Panic(err)
}
} else if buf[0] != '*' {
log.Panic("Expected *, got ", buf[0])
}
i := 1
for ; buf[i] != '\r'; i++ {}
arrLen, _ := strconv.Atoi(string(buf[1:i]))
return &RespReader{
currentPos: i+2, // Skip CRLF
buffer: buf,
arrLen: arrLen,
}
}
/*
* Returns the next element in the request.
* Will return io.EOF if a CRLF is not reached.
*/
func (reader *RespReader) NextElement() (string, error) {
i, err := reader.FindCRLF()
if err != nil {
return "", err
}
reader.currentPos = i+2
i, err = reader.FindCRLF()
if err != nil {
return "", err
}
str := string(reader.buffer[reader.currentPos:i])
reader.currentPos = i+2
return str, nil
}
/*
* Returns the index of the next CRLF.
* Will return io.EOF if a CRLF is not reached.
*/
func (reader *RespReader) FindCRLF() (int, error) {
// Advance i until CRLF, return buf[currentPos:i] and update currentPos to i+2
for i := reader.currentPos; i < len(reader.buffer); i++ {
if reader.buffer[i] == '\r' {
return i, nil
}
}
return 0, io.EOF
}