Fix lint warnings.

This commit was merged in pull request #11.
This commit is contained in:
2021-01-09 12:10:43 +01:00
parent f449b535db
commit 0805cec129
19 changed files with 85 additions and 84 deletions

View File

@@ -24,7 +24,7 @@ func main() {
request := ipp.NewRequest(ipp.GetPrinterAttributes, 10) request := ipp.NewRequest(ipp.GetPrinterAttributes, 10)
request.AddOperatonAttribute(ipp.NewCharSetValue("attributes-charset", "utf-8")) request.AddOperatonAttribute(ipp.NewCharSetValue("attributes-charset", "utf-8"))
request.AddOperatonAttribute(ipp.NewNaturalLanguage("attributes-natural-language", "en")) request.AddOperatonAttribute(ipp.NewNaturalLanguage("attributes-natural-language", "en"))
request.AddOperatonAttribute(ipp.NewUriValue("printer-uri", "ipp://"+printerUri)) request.AddOperatonAttribute(ipp.NewURIValue("printer-uri", "ipp://"+printerUri))
r := request.Marshal() r := request.Marshal()
b := bytes.NewBuffer(r) b := bytes.NewBuffer(r)
httpResponse, err := http.Post("http://"+"brn30055cb5e3ae.local:631/ipp/print", "application/ipp", b) httpResponse, err := http.Post("http://"+"brn30055cb5e3ae.local:631/ipp/print", "application/ipp", b)

View File

@@ -37,15 +37,15 @@ func (b *boolean) unmarshal(byteStream io.Reader) {
log.Warn("Unmarshal of boolean is not implemented yet") log.Warn("Unmarshal of boolean is not implemented yet")
} }
func (e *boolean) marshal() []byte { func (b *boolean) marshal() []byte {
l := 5 + len(e.name) l := 5 + len(b.name)
b := make([]byte, 0, l) ba := make([]byte, 0, l)
buf := bytes.NewBuffer(b) buf := bytes.NewBuffer(ba)
buf.WriteByte(byte(booleanValueTag)) buf.WriteByte(byte(booleanValueTag))
binary.Write(buf, binary.BigEndian, uint16(len(e.name))) binary.Write(buf, binary.BigEndian, uint16(len(b.name)))
buf.WriteString(e.name) buf.WriteString(b.name)
binary.Write(buf, binary.BigEndian, uint16(1)) binary.Write(buf, binary.BigEndian, uint16(1))
if e.value { if b.value {
buf.WriteByte(byte(1)) buf.WriteByte(byte(1))
} else { } else {
buf.WriteByte(byte(0)) buf.WriteByte(byte(0))
@@ -53,7 +53,7 @@ func (e *boolean) marshal() []byte {
return buf.Bytes() return buf.Bytes()
} }
func (e *boolean) size() int { func (b *boolean) size() int {
l := 5 + len(e.name) l := 5 + len(b.name)
return l return l
} }

View File

@@ -34,7 +34,7 @@ func (c *charSetValue) unmarshal(byteStream io.Reader) {
func (c *charSetValue) marshal() []byte { func (c *charSetValue) marshal() []byte {
l := 5 + len(c.name) + len(c.value) l := 5 + len(c.name) + len(c.value)
b := make([]byte, l, l) b := make([]byte, l)
b[0] = byte(charsetValueTag) b[0] = byte(charsetValueTag)
marshalNameValue(c.name, c.value, b[1:]) marshalNameValue(c.name, c.value, b[1:])
return b return b

View File

@@ -32,8 +32,8 @@ func (e *enum) size() int {
return 9 + len(e.name) return 9 + len(e.name)
} }
func (i *enum) addValue(v interface{}) { func (e *enum) addValue(v interface{}) {
i.values = append(i.values, v.(int32)) e.values = append(e.values, v.(int32))
} }
func (e *enum) marshal() []byte { func (e *enum) marshal() []byte {

View File

@@ -1,3 +1,4 @@
//Package ipp provides functonality to handle ipp messages
package ipp package ipp
import ( import (
@@ -56,26 +57,26 @@ const (
memberAttrNameValueTag tag = 0x4a memberAttrNameValueTag tag = 0x4a
) )
// Operation-id, defined in rfc8011 // OperationID is defined in rfc8011
type OperationId uint16 type OperationID uint16
const ( const (
PrintJob OperationId = 0x0002 PrintJob OperationID = 0x0002
PrintURI OperationId = 0x0003 PrintURI OperationID = 0x0003
ValidateJob OperationId = 0x0004 ValidateJob OperationID = 0x0004
CreateJob OperationId = 0x0005 CreateJob OperationID = 0x0005
SendDocument OperationId = 0x0006 SendDocument OperationID = 0x0006
SendURI OperationId = 0x0007 SendURI OperationID = 0x0007
CancelJob OperationId = 0x0008 CancelJob OperationID = 0x0008
GetJobAttributes OperationId = 0x0009 GetJobAttributes OperationID = 0x0009
GetJobs OperationId = 0x000a GetJobs OperationID = 0x000a
GetPrinterAttributes OperationId = 0x000b GetPrinterAttributes OperationID = 0x000b
HoldJob OperationId = 0x000c HoldJob OperationID = 0x000c
ReleaseJob OperationId = 0x000d ReleaseJob OperationID = 0x000d
RestartJob OperationId = 0x000e RestartJob OperationID = 0x000e
PausePrinter OperationId = 0x0010 PausePrinter OperationID = 0x0010
ResumePrinter OperationId = 0x0011 ResumePrinter OperationID = 0x0011
PurgeJobs OperationId = 0x0012 PurgeJobs OperationID = 0x0012
) )
type printerState int32 type printerState int32
@@ -166,14 +167,14 @@ func (a *attributes) String() string {
return s return s
} }
func (as *attributes) addAttribute(group tag, a Attribute) { func (a *attributes) addAttribute(group tag, attr Attribute) {
switch group { switch group {
case operationAttributes: case operationAttributes:
as.operation = append(as.operation, a) a.operation = append(a.operation, attr)
case jobAttributes: case jobAttributes:
as.job = append(as.job, a) a.job = append(a.job, attr)
case printerAttributes: case printerAttributes:
as.printer = append(as.printer, a) a.printer = append(a.printer, attr)
default: default:
log.Error("Unknown attribute group") log.Error("Unknown attribute group")
} }
@@ -210,7 +211,7 @@ func UnMarshalAttributues(body io.Reader) *attributes {
a.addAttribute(currentAttributeGroup, c) a.addAttribute(currentAttributeGroup, c)
log.Debugf("%v %v", c.name, c.value) log.Debugf("%v %v", c.name, c.value)
case uriValueTag: case uriValueTag:
u := NewUriValue("", "") u := NewURIValue("", "")
u.unmarshal(body) u.unmarshal(body)
a.addAttribute(currentAttributeGroup, u) a.addAttribute(currentAttributeGroup, u)
log.Debugf("%v %v", u.name, u.value) log.Debugf("%v %v", u.name, u.value)

View File

@@ -14,33 +14,33 @@ func NewNameWithoutLanguage(name, value string) *NameWithoutLanguage {
return c return c
} }
func (c NameWithoutLanguage) Name() string { func (n NameWithoutLanguage) Name() string {
return c.name return n.name
} }
func (c NameWithoutLanguage) String() string { func (n NameWithoutLanguage) String() string {
return c.name + ":" + c.value return n.name + ":" + n.value
} }
func (c *NameWithoutLanguage) valueTag() tag { func (n *NameWithoutLanguage) valueTag() tag {
return nameWithoutLanguageValueTag return nameWithoutLanguageValueTag
} }
func (c *NameWithoutLanguage) unmarshal(byteStream io.Reader) { func (n *NameWithoutLanguage) unmarshal(byteStream io.Reader) {
c.name, c.value = unmarshalSingleValue(byteStream) n.name, n.value = unmarshalSingleValue(byteStream)
} }
func (c *NameWithoutLanguage) marshal() []byte { func (n *NameWithoutLanguage) marshal() []byte {
l := 5 + len(c.name) + len(c.value) l := 5 + len(n.name) + len(n.value)
b := make([]byte, l, l) b := make([]byte, l)
b[0] = byte(nameWithoutLanguageValueTag) b[0] = byte(nameWithoutLanguageValueTag)
marshalNameValue(c.name, c.value, b[1:]) marshalNameValue(n.name, n.value, b[1:])
return b return b
} }
func (c *NameWithoutLanguage) size() int { func (n *NameWithoutLanguage) size() int {
l := 1 + 4 // The attribute tag + 2 lengths l := 1 + 4 // The attribute tag + 2 lengths
l += len(c.name) l += len(n.name)
l += len(c.value) l += len(n.value)
return l return l
} }

View File

@@ -34,7 +34,7 @@ func (c *naturalLanguage) unmarshal(byteStream io.Reader) {
func (c *naturalLanguage) marshal() []byte { func (c *naturalLanguage) marshal() []byte {
l := 5 + len(c.name) + len(c.value) l := 5 + len(c.name) + len(c.value)
b := make([]byte, l, l) b := make([]byte, l)
b[0] = byte(naturalLanguageValueTag) b[0] = byte(naturalLanguageValueTag)
marshalNameValue(c.name, c.value, b[1:]) marshalNameValue(c.name, c.value, b[1:])
return b return b

View File

@@ -36,7 +36,7 @@ var (
_operationId_index_1 = [...]uint8{0, 12, 25, 34} _operationId_index_1 = [...]uint8{0, 12, 25, 34}
) )
func (i OperationId) String() string { func (i OperationID) String() string {
switch { switch {
case 2 <= i && i <= 14: case 2 <= i && i <= 14:
i -= 2 i -= 2

View File

@@ -9,27 +9,27 @@ import (
type ippMessageHeader struct { type ippMessageHeader struct {
versionNumber versionNumber versionNumber versionNumber
operationId OperationId operationID OperationID
requestId uint32 requestID uint32
} }
func (h *ippMessageHeader) unmarshal(byteStream io.Reader) { func (h *ippMessageHeader) unmarshal(byteStream io.Reader) {
binary.Read(byteStream, binary.BigEndian, &h.versionNumber) binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
binary.Read(byteStream, binary.BigEndian, &h.operationId) binary.Read(byteStream, binary.BigEndian, &h.operationID)
binary.Read(byteStream, binary.BigEndian, &h.requestId) binary.Read(byteStream, binary.BigEndian, &h.requestID)
} }
func (h *ippMessageHeader) marshal() []byte { func (h *ippMessageHeader) marshal() []byte {
b := make([]byte, 0, 8) b := make([]byte, 0, 8)
buf := bytes.NewBuffer(b) buf := bytes.NewBuffer(b)
binary.Write(buf, binary.BigEndian, h.versionNumber) binary.Write(buf, binary.BigEndian, h.versionNumber)
binary.Write(buf, binary.BigEndian, h.operationId) binary.Write(buf, binary.BigEndian, h.operationID)
binary.Write(buf, binary.BigEndian, h.requestId) binary.Write(buf, binary.BigEndian, h.requestID)
return buf.Bytes() return buf.Bytes()
} }
func (h ippMessageHeader) String() string { func (h ippMessageHeader) String() string {
return fmt.Sprintf("Version number: %v Operation Id: %v Request Id: %v", h.versionNumber, h.operationId, h.requestId) return fmt.Sprintf("Version number: %v Operation Id: %v Request Id: %v", h.versionNumber, h.operationID, h.requestID)
} }
type AddValuer interface { type AddValuer interface {
@@ -41,10 +41,10 @@ type Request struct {
header ippMessageHeader header ippMessageHeader
} }
func NewRequest(op OperationId, requestId uint32) *Request { func NewRequest(op OperationID, requestID uint32) *Request {
r := new(Request) r := new(Request)
r.header.operationId = op r.header.operationID = op
r.header.requestId = requestId r.header.requestID = requestID
r.header.versionNumber = 0x0200 r.header.versionNumber = 0x0200
r.a = new(attributes) r.a = new(attributes)
return r return r
@@ -60,12 +60,12 @@ func (r *Request) UnMarshal(body io.Reader) {
r.a = UnMarshalAttributues(body) r.a = UnMarshalAttributues(body)
} }
func (r *Request) RequestId() uint32 { func (r *Request) RequestID() uint32 {
return r.header.requestId return r.header.requestID
} }
func (r *Request) Operation() OperationId { func (r *Request) Operation() OperationID {
return r.header.operationId return r.header.operationID
} }
func (r *Request) GetAttribute(name string) Attribute { func (r *Request) GetAttribute(name string) Attribute {

View File

@@ -64,8 +64,8 @@ func TestUnmarshalRequestPrinterAttributes(T *testing.T) {
req.UnMarshal(buf) req.UnMarshal(buf)
fmt.Print(req) fmt.Print(req)
assert.Equal(T, versionNumber(0x0101), req.header.versionNumber, "Wrong version number") assert.Equal(T, versionNumber(0x0101), req.header.versionNumber, "Wrong version number")
assert.Equal(T, GetPrinterAttributes, req.header.operationId, "Wrong Operation") assert.Equal(T, GetPrinterAttributes, req.header.operationID, "Wrong Operation")
assert.Equal(T, uint32(17), req.header.requestId, "Wrong request id") assert.Equal(T, uint32(17), req.header.requestID, "Wrong request id")
assert.Len(T, req.a.operation, 4) assert.Len(T, req.a.operation, 4)
v := req.GetAttribute("requested-attributes").(*keyWord).sos.values v := req.GetAttribute("requested-attributes").(*keyWord).sos.values
assert.Len(T, v, 7) assert.Len(T, v, 7)

View File

@@ -9,18 +9,18 @@ import (
type ippResponseHeader struct { type ippResponseHeader struct {
versionNumber versionNumber versionNumber versionNumber
statusCode statusCode statusCode statusCode
requestId uint32 requestID uint32
} }
func (h ippResponseHeader) String() string { func (h ippResponseHeader) String() string {
return fmt.Sprintf("Version number: %v Status code: %v Request Id: %v", h.versionNumber, h.statusCode, h.requestId) return fmt.Sprintf("Version number: %v Status code: %v Request Id: %v", h.versionNumber, h.statusCode, h.requestID)
} }
func (h *ippResponseHeader) marshal() []byte { func (h *ippResponseHeader) marshal() []byte {
a := make([]byte, 8, 8) a := make([]byte, 8)
binary.BigEndian.PutUint16(a[0:2], uint16(h.versionNumber)) binary.BigEndian.PutUint16(a[0:2], uint16(h.versionNumber))
binary.BigEndian.PutUint16(a[2:4], uint16(h.statusCode)) binary.BigEndian.PutUint16(a[2:4], uint16(h.statusCode))
binary.BigEndian.PutUint32(a[4:8], h.requestId) binary.BigEndian.PutUint32(a[4:8], h.requestID)
return a return a
} }
@@ -28,7 +28,7 @@ func (h *ippResponseHeader) marshal() []byte {
func (h *ippResponseHeader) unmarshal(byteStream io.Reader) { func (h *ippResponseHeader) unmarshal(byteStream io.Reader) {
binary.Read(byteStream, binary.BigEndian, &h.versionNumber) binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
binary.Read(byteStream, binary.BigEndian, &h.statusCode) binary.Read(byteStream, binary.BigEndian, &h.statusCode)
binary.Read(byteStream, binary.BigEndian, &h.requestId) binary.Read(byteStream, binary.BigEndian, &h.requestID)
} }
type Response struct { type Response struct {
@@ -36,11 +36,11 @@ type Response struct {
header ippResponseHeader header ippResponseHeader
} }
func NewResponse(code statusCode, requestId uint32) *Response { func NewResponse(code statusCode, requestID uint32) *Response {
r := new(Response) r := new(Response)
r.a = new(attributes) r.a = new(attributes)
r.header.versionNumber = 0x0101 r.header.versionNumber = 0x0101
r.header.requestId = requestId r.header.requestID = requestID
r.header.statusCode = code r.header.statusCode = code
return r return r
} }

View File

@@ -10,7 +10,7 @@ func TestMarshalResponseHeader(T *testing.T) {
h.versionNumber = 0x0101 h.versionNumber = 0x0101
h.statusCode = SuccessfulOk h.statusCode = SuccessfulOk
h.requestId = 0xdeadbeef h.requestID = 0xdeadbeef
b := h.marshal() b := h.marshal()
fmt.Printf("% x\n", b) fmt.Printf("% x\n", b)

View File

@@ -44,7 +44,7 @@ func (s *setOfStrings) marshal() []byte {
for i := range s.values[1:] { for i := range s.values[1:] {
l += 5 + len(s.values[i+1]) l += 5 + len(s.values[i+1])
} }
res := make([]byte, l, l) res := make([]byte, l)
p := 0 p := 0
res[p] = byte(s.vTag) res[p] = byte(s.vTag)
p += 1 p += 1

View File

@@ -32,7 +32,7 @@ func (c *textWithoutLanguage) unmarshal(byteStream io.Reader) {
func (c *textWithoutLanguage) marshal() []byte { func (c *textWithoutLanguage) marshal() []byte {
l := 5 + len(c.name) + len(c.value) l := 5 + len(c.name) + len(c.value)
b := make([]byte, l, l) b := make([]byte, l)
b[0] = byte(textWithoutLanguageValueTag) b[0] = byte(textWithoutLanguageValueTag)
marshalNameValue(c.name, c.value, b[1:]) marshalNameValue(c.name, c.value, b[1:])
return b return b

View File

@@ -9,7 +9,7 @@ type uriValue struct {
value string value string
} }
func NewUriValue(name, value string) *uriValue { func NewURIValue(name, value string) *uriValue {
u := new(uriValue) u := new(uriValue)
u.name = name u.name = name
u.value = value u.value = value

View File

@@ -3,6 +3,6 @@ package main
import "ippserver/packages/ipp" import "ippserver/packages/ipp"
func handleGetJobs(r *ipp.Request) *ipp.Response { func handleGetJobs(r *ipp.Request) *ipp.Response {
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestId()) response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestID())
return response return response
} }

View File

@@ -3,13 +3,13 @@ package main
import "ippserver/packages/ipp" import "ippserver/packages/ipp"
func handleGetPrinterAttributes(r *ipp.Request) *ipp.Response { func handleGetPrinterAttributes(r *ipp.Request) *ipp.Response {
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestId()) response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestID())
var a ipp.Attribute var a ipp.Attribute
a = ipp.NewCharSetValue("attributes-charset", "utf-8") a = ipp.NewCharSetValue("attributes-charset", "utf-8")
response.AddOperatonAttribute(a) response.AddOperatonAttribute(a)
a = ipp.NewNaturalLanguage("attributes-natural-language", "en") a = ipp.NewNaturalLanguage("attributes-natural-language", "en")
response.AddOperatonAttribute(a) response.AddOperatonAttribute(a)
a = ipp.NewUriValue("printer-uri", "ipp://drpork:1234/ipp/print") a = ipp.NewURIValue("printer-uri", "ipp://drpork:1234/ipp/print")
response.AddOperatonAttribute(a) response.AddOperatonAttribute(a)
a = ipp.NewtextWithoutLanguage("printer-make-and-model", "ChroBro 001") a = ipp.NewtextWithoutLanguage("printer-make-and-model", "ChroBro 001")
response.AddOperatonAttribute(a) response.AddOperatonAttribute(a)

View File

@@ -16,7 +16,7 @@ func handlePrintJob(r *ipp.Request, byteStream io.Reader) *ipp.Response {
} }
defer f.Close() defer f.Close()
io.Copy(f, byteStream) io.Copy(f, byteStream)
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestId()) response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestID())
return response return response
} }

View File

@@ -57,7 +57,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
case ipp.GetJobs: case ipp.GetJobs:
response = handleGetJobs(request) response = handleGetJobs(request)
default: default:
response = ipp.NewResponse(ipp.ClientErrorBadRequest, request.RequestId()) response = ipp.NewResponse(ipp.ClientErrorBadRequest, request.RequestID())
} }
log.Infof("Response:\n%v\n", response) log.Infof("Response:\n%v\n", response)