aboutsummaryrefslogtreecommitdiff
path: root/protocol.go
blob: b2a4cb6008522bd7ff88964304b26e6f17f06831 (plain)
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
package main

// postmap -v 'user' /path/to.socket

import (
	"encoding"
	"errors"
	"fmt"
	"io"
	"strings"
)

// Postfix request.
type Request struct {
	Name string
	Key  string
}

func (s *Request) UnmarshalText(buf []byte) error {
	strs := strings.SplitN(string(buf), " ", 2)
	if len(strs) > 0 {
		s.Name = strs[0]
	}
	if len(strs) > 1 {
		s.Key = strs[1]
	}
	if len(strs) > 2 {
		return fmt.Errorf("bad format")
	}

	return nil
}

func (s *Request) String() string {
	return fmt.Sprintf("%s %s", s.Name, s.Key)
}

// Value found.
type ReplyOK struct {
	Data string
}

func (s *ReplyOK) String() string {
	return fmt.Sprintf("OK %s", s.Data)
}

// Value not found.
type ReplyNotFound struct{}

func (s *ReplyNotFound) String() string {
	return "NOTFOUND "
}

// Something happened..
type ReplyPerm struct {
	Reason string
}

func (s *ReplyPerm) String() string {
	return fmt.Sprintf("PERM %s", s.Reason)
}

func readNetString(r io.Reader, v encoding.TextUnmarshaler) (int, error) {
	var strLen int

	bytesRead, err := fmt.Fscanf(r, "%d:", &strLen)
	if err != nil {
		return bytesRead, err
	}

	payload := make([]byte, strLen)
	n, err := r.Read(payload)
	if err != nil {
		return bytesRead, err
	}
	bytesRead += n

	buf := make([]byte, 1)
	n, err = r.Read(buf)
	bytesRead += n
	if err != nil {
		return bytesRead, err
	}

	if buf[0] != ',' {
		return bytesRead, errors.New("bad format")
	}

	err = v.UnmarshalText(payload)
	if err != nil {
		return bytesRead, err
	}

	return bytesRead, nil
}

func writeNetString(w io.Writer, v fmt.Stringer) (int, error) {
	str := v.String()
	resp := fmt.Sprintf("%d:%s,", len(str), str)
	return w.Write([]byte(resp))
}