From 90afa97083a424fee1ef98d0c26bb8268ac49e04 Mon Sep 17 00:00:00 2001 From: Kory Prince Date: Tue, 27 Apr 2021 10:29:48 -0500 Subject: [PATCH 1/4] socket: make errors public --- adapter-socket.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adapter-socket.go b/adapter-socket.go index a0faee7..a3e84da 100644 --- a/adapter-socket.go +++ b/adapter-socket.go @@ -12,8 +12,8 @@ import ( "strconv" ) -var socketNotFoundError = errors.New("unable to locate CUPS socket") -var certNotFoundError = errors.New("unable to locate CUPS certificate") +var SocketNotFoundError = errors.New("unable to locate CUPS socket") +var CertNotFoundError = errors.New("unable to locate CUPS certificate") var ( DefaultSocketSearchPaths = []string{"/var/run/cupsd", "/var/run/cups/cups.sock", "/run/cups/cups.sock"} @@ -61,7 +61,7 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Respo // if cert isn't found, do a request to generate it cert, err := h.GetCert() - if err != nil && err != certNotFoundError { + if err != nil && err != CertNotFoundError { return nil, err } @@ -137,7 +137,7 @@ func (h *SocketAdapter) GetSocket() (string, error) { } } - return "", socketNotFoundError + return "", SocketNotFoundError } //GetCert returns the current CUPs authentication certificate by searching CertSearchPaths @@ -161,7 +161,7 @@ func (h *SocketAdapter) GetCert() (string, error) { return buf.String(), nil } - return "", certNotFoundError + return "", CertNotFoundError } func (h *SocketAdapter) GetHttpUri(namespace string, object interface{}) string { From c951de4acf2210e8a33d30954f050f9901f373d7 Mon Sep 17 00:00:00 2001 From: Kory Prince Date: Tue, 27 Apr 2021 10:32:21 -0500 Subject: [PATCH 2/4] socket: allow additional data to be decoded --- adapter-socket.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adapter-socket.go b/adapter-socket.go index a3e84da..5c734fb 100644 --- a/adapter-socket.go +++ b/adapter-socket.go @@ -40,8 +40,9 @@ func NewSocketAdapter(host string, useTLS bool) *SocketAdapter { } } -//DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred -func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Response, error) { +//DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred. +//Additional data will be written to an io.Writer if additionalData is not nil +func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Writer) (*Response, error) { for i := 0; i < h.requestRetryLimit; i++ { // encode request payload, err := r.Encode() @@ -104,7 +105,7 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Respo resp.Body.Close() // decode reply - ippResp, err := NewResponseDecoder(bytes.NewReader(buf.Bytes())).Decode(nil) + ippResp, err := NewResponseDecoder(bytes.NewReader(buf.Bytes())).Decode(additionalData) if err != nil { return nil, fmt.Errorf("unable to decode IPP response: %v", err) } From b20b6caad777dca0b18d20645c274a7fc417b9a1 Mon Sep 17 00:00:00 2001 From: Kory Prince Date: Tue, 27 Apr 2021 10:37:02 -0500 Subject: [PATCH 3/4] socket: make retry a public field so it can be changed --- adapter-socket.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/adapter-socket.go b/adapter-socket.go index 5c734fb..01c5600 100644 --- a/adapter-socket.go +++ b/adapter-socket.go @@ -20,14 +20,15 @@ var ( DefaultCertSearchPaths = []string{"/etc/cups/certs/0", "/run/cups/certs/0"} ) -const defaultRequestRetryLimit = 3 +const DefaultRequestRetryLimit = 3 type SocketAdapter struct { host string useTLS bool SocketSearchPaths []string CertSearchPaths []string - requestRetryLimit int + //RequestRetryLimit is the number of times a request will be retried when receiving an authorized status. This usually happens when a CUPs cert is expired, and a retry will use the newly generated cert. Default 3. + RequestRetryLimit int } func NewSocketAdapter(host string, useTLS bool) *SocketAdapter { @@ -36,14 +37,14 @@ func NewSocketAdapter(host string, useTLS bool) *SocketAdapter { useTLS: useTLS, SocketSearchPaths: DefaultSocketSearchPaths, CertSearchPaths: DefaultCertSearchPaths, - requestRetryLimit: defaultRequestRetryLimit, + RequestRetryLimit: DefaultRequestRetryLimit, } } //DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred. //Additional data will be written to an io.Writer if additionalData is not nil func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Writer) (*Response, error) { - for i := 0; i < h.requestRetryLimit; i++ { + for i := 0; i < h.RequestRetryLimit; i++ { // encode request payload, err := r.Encode() if err != nil { From 033011db62338b0e23ef48fc01fc532c5ad55e74 Mon Sep 17 00:00:00 2001 From: Kory Prince Date: Tue, 27 Apr 2021 10:44:01 -0500 Subject: [PATCH 4/4] socket: fill out TestConnection implementation --- adapter-socket.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/adapter-socket.go b/adapter-socket.go index 01c5600..535c9c4 100644 --- a/adapter-socket.go +++ b/adapter-socket.go @@ -186,5 +186,14 @@ func (h *SocketAdapter) GetHttpUri(namespace string, object interface{}) string } func (h *SocketAdapter) TestConnection() error { + sock, err := h.GetSocket() + if err != nil { + return err + } + conn, err := net.Dial("unix", sock) + if err != nil { + return err + } + conn.Close() return nil }