-
Notifications
You must be signed in to change notification settings - Fork 6
/
Params.go
65 lines (51 loc) · 1.04 KB
/
Params.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
package main
import (
"encoding/binary"
"io"
)
type (
// Params is a simple type used for format parameters for FastCGI.
Params map[string]string
)
func NewParams() Params {
return make(map[string]string)
}
// Size will predict the needed space for encoding all parameters.
func (p Params) Size() uint16 {
accum := 0
for key, value := range p {
// two bytes for setting length.
accum += 2
// ... and the actual values.
accum += len(key)
accum += len(value)
}
return uint16(accum)
}
// Write the encoded parameters to w.
func (p Params) Write(w io.Writer) error {
for key, value := range p {
keyLen := len(key)
valueLen := len(value)
if keyLen > 255 || valueLen > 255 {
continue
}
err := binary.Write(w, binary.BigEndian, byte(keyLen))
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, byte(valueLen))
if err != nil {
return err
}
_, err = w.Write([]byte(key))
if err != nil {
return err
}
_, err = w.Write([]byte(value))
if err != nil {
return err
}
}
return nil
}