go-ipp/response.go

182 lines
3.8 KiB
Go
Raw Normal View History

2019-03-10 17:43:47 +00:00
package ipp
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
type Attributes map[string][]Attribute
type Response struct {
ProtocolVersionMajor uint8
ProtocolVersionMinor uint8
StatusCode uint16
2019-03-13 21:54:08 +00:00
RequestId int32
2019-03-10 17:43:47 +00:00
OperationAttributes Attributes
Printers []Attributes
Jobs []Attributes
data io.Writer
2019-03-10 17:43:47 +00:00
}
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(data io.Writer) (*Response, error) {
2019-03-10 17:43:47 +00:00
/*
2019-03-13 21:54:08 +00:00
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)
2019-03-10 17:43:47 +00:00
*/
resp := new(Response)
resp.data = data
2019-03-10 17:43:47 +00:00
// wrap the reader so we have more functionality
2019-03-13 21:54:08 +00:00
//reader := bufio.NewReader(d.reader)
2019-03-10 17:43:47 +00:00
2019-03-13 21:54:08 +00:00
if err := binary.Read(d.reader, binary.BigEndian, &resp.ProtocolVersionMajor); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
if err := binary.Read(d.reader, binary.BigEndian, &resp.ProtocolVersionMinor); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
if err := binary.Read(d.reader, binary.BigEndian, &resp.StatusCode); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
if err := binary.Read(d.reader, binary.BigEndian, &resp.RequestId); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
startByteSlice := make([]byte, 1)
2019-03-10 17:43:47 +00:00
tag := TagCupsInvalid
previousAttributeName := ""
tempAttributes := make(Attributes)
tagSet := false
2019-03-13 21:54:08 +00:00
attribDecoder := NewAttributeDecoder(d.reader)
2019-03-10 17:43:47 +00:00
// decode attribute buffer
for {
2019-03-13 21:54:08 +00:00
if _, err := d.reader.Read(startByteSlice); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
startByte := startByteSlice[0]
2019-03-10 17:43:47 +00:00
// check if attributes are completed
if startByte == TagEnd {
2019-03-10 17:43:47 +00:00
break
}
if startByte == TagOperation {
2019-03-10 17:43:47 +00:00
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
tempAttributes = make(Attributes)
}
tag = TagOperation
tagSet = true
}
if startByte == TagJob {
2019-03-10 17:43:47 +00:00
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
tempAttributes = make(Attributes)
}
tag = TagJob
tagSet = true
}
if startByte == TagPrinter {
2019-03-10 17:43:47 +00:00
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
tempAttributes = make(Attributes)
}
tag = TagPrinter
tagSet = true
}
2019-03-13 21:54:08 +00:00
if tagSet {
if _, err := d.reader.Read(startByteSlice); err != nil {
2019-03-10 17:43:47 +00:00
return nil, err
}
2019-03-13 21:54:08 +00:00
startByte = startByteSlice[0]
2019-03-10 17:43:47 +00:00
}
2019-03-13 21:54:08 +00:00
attrib, err := attribDecoder.Decode(Tag(startByte))
2019-03-10 17:43:47 +00:00
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)
}
2019-03-13 21:54:08 +00:00
tagSet = false
2019-03-10 17:43:47 +00:00
}
if len(tempAttributes) > 0 && tag != TagCupsInvalid {
appendAttribute(resp, tag, tempAttributes)
}
if resp.data != nil {
if _, err := io.Copy(resp.data, d.reader); err != nil {
return nil, err
}
2019-03-10 17:43:47 +00:00
}
2019-03-15 15:20:02 +00:00
if resp.StatusCode != 0 {
return resp, errors.New(resp.OperationAttributes["status-message"][0].Value.(string))
}
2019-03-10 17:43:47 +00:00
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)
}
}