forked from phin1x/go-ipp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
178 lines (139 loc) · 3.79 KB
/
response.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package ipp
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
type Attributes map[string][]Attribute
type Response struct {
ProtocolVersionMajor uint8
ProtocolVersionMinor uint8
StatusCode uint16
RequestId int32
OperationAttributes Attributes
Printers []Attributes
Jobs []Attributes
Data []byte
}
func (r *Response) CheckForErrors() error {
if r.StatusCode != 0 {
if len(r.OperationAttributes["status-message"]) == 0 {
return fmt.Errorf("ipp server return error code %d but no status message", r.StatusCode)
}
return errors.New(r.OperationAttributes["status-message"][0].Value.(string))
}
return nil
}
type ResponseDecoder struct {
reader io.Reader
}
func NewResponseDecoder(r io.Reader) *ResponseDecoder {
return &ResponseDecoder{
reader: r,
}
}
func (d *ResponseDecoder) Decode() (*Response, error) {
/*
1 byte: Protocol Major Version - b
1 byte: Protocol Minor Version - b
2 byte: Status ID - h
4 byte: Request ID - i
1 byte: Operation Attribute Byte (\0x01)
N times: Attributes
1 byte: Attribute End Byte (\0x03)
*/
resp := new(Response)
// wrap the reader so we have more functionality
//reader := bufio.NewReader(d.reader)
if err := binary.Read(d.reader, binary.BigEndian, &resp.ProtocolVersionMajor); err != nil {
return nil, err
}
if err := binary.Read(d.reader, binary.BigEndian, &resp.ProtocolVersionMinor); err != nil {
return nil, err
}
if err := binary.Read(d.reader, binary.BigEndian, &resp.StatusCode); err != nil {
return nil, err
}
if err := binary.Read(d.reader, binary.BigEndian, &resp.RequestId); err != nil {
return nil, err
}
startByteSlice := make([]byte, 1)
tag := TagCupsInvalid
previousAttributeName := ""
tempAttributes := make(Attributes)
tagSet := false
attribDecoder := NewAttributeDecoder(d.reader)
// decode attribute buffer
for {
if _, err := d.reader.Read(startByteSlice); err != nil {
return nil, err
}
startByte := startByteSlice[0]
// check if attributes are completed
if uint8(startByte) == TagEnd {
break
}
if uint8(startByte) == TagOperation {
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
tempAttributes = make(Attributes)
}
tag = TagOperation
tagSet = true
}
if uint8(startByte) == TagJob {
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
tempAttributes = make(Attributes)
}
tag = TagJob
tagSet = true
}
if uint8(startByte) == TagPrinter {
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
tempAttributes = make(Attributes)
}
tag = TagPrinter
tagSet = true
}
if tagSet {
if _, err := d.reader.Read(startByteSlice); err != nil {
return nil, err
}
startByte = startByteSlice[0]
}
attrib, err := attribDecoder.Decode(Tag(startByte))
if err != nil {
return nil, err
}
if attrib.Name != "" {
tempAttributes[attrib.Name] = append(tempAttributes[attrib.Name], *attrib)
previousAttributeName = attrib.Name
} else {
tempAttributes[previousAttributeName] = append(tempAttributes[previousAttributeName], *attrib)
}
tagSet = false
}
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
}
if _, err := d.reader.Read(resp.Data); err != nil {
return nil, err
}
if resp.StatusCode != 0 {
return resp, errors.New(resp.OperationAttributes["status-message"][0].Value.(string))
}
return resp, nil
}
func appendAttribute(resp *Response, tag Tag, attr map[string][]Attribute) {
switch tag {
case TagOperation:
resp.OperationAttributes = attr
case TagPrinter:
resp.Printers = append(resp.Printers, attr)
case TagJob:
resp.Jobs = append(resp.Jobs, attr)
}
}