diff --git a/adapter-socket.go b/adapter-socket.go index a0faee7..535c9c4 100644 --- a/adapter-socket.go +++ b/adapter-socket.go @@ -12,22 +12,23 @@ 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"} 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,13 +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 -func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Response, error) { - for i := 0; i < h.requestRetryLimit; i++ { +//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() if err != nil { @@ -61,7 +63,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 } @@ -104,7 +106,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) } @@ -137,7 +139,7 @@ func (h *SocketAdapter) GetSocket() (string, error) { } } - return "", socketNotFoundError + return "", SocketNotFoundError } //GetCert returns the current CUPs authentication certificate by searching CertSearchPaths @@ -161,7 +163,7 @@ func (h *SocketAdapter) GetCert() (string, error) { return buf.String(), nil } - return "", certNotFoundError + return "", CertNotFoundError } func (h *SocketAdapter) GetHttpUri(namespace string, object interface{}) string { @@ -184,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 }