@@ -41,14 +41,14 @@ func NewSocketAdapter(host string, useTLS bool) *SocketAdapter {
41
41
}
42
42
}
43
43
44
- //DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred.
45
- //Additional data will be written to an io.Writer if additionalData is not nil
44
+ // SendRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred.
45
+ // Additional data will be written to an io.Writer if additionalData is not nil
46
46
func (h * SocketAdapter ) SendRequest (url string , r * Request , additionalData io.Writer ) (* Response , error ) {
47
47
for i := 0 ; i < h .RequestRetryLimit ; i ++ {
48
48
// encode request
49
49
payload , err := r .Encode ()
50
50
if err != nil {
51
- return nil , fmt .Errorf ("unable to encode IPP request: %v " , err )
51
+ return nil , fmt .Errorf ("unable to encode IPP request: %w " , err )
52
52
}
53
53
54
54
var body io.Reader
@@ -64,7 +64,7 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Wr
64
64
65
65
req , err := http .NewRequest ("POST" , url , body )
66
66
if err != nil {
67
- return nil , fmt .Errorf ("unable to create HTTP request: %v " , err )
67
+ return nil , fmt .Errorf ("unable to create HTTP request: %w " , err )
68
68
}
69
69
70
70
sock , err := h .GetSocket ()
@@ -91,39 +91,42 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Wr
91
91
}
92
92
93
93
// send request
94
- resp , err := unixClient .Do (req )
94
+ httpResp , err := unixClient .Do (req )
95
95
if err != nil {
96
- return nil , fmt .Errorf ("unable to perform HTTP request: %v " , err )
96
+ return nil , fmt .Errorf ("unable to perform HTTP request: %w " , err )
97
97
}
98
98
99
- if resp .StatusCode == http .StatusUnauthorized {
99
+ if httpResp .StatusCode == http .StatusUnauthorized {
100
100
// retry with newly generated cert
101
- resp .Body .Close ()
101
+ httpResp .Body .Close ()
102
102
continue
103
103
}
104
104
105
- if resp .StatusCode != http .StatusOK {
106
- resp .Body .Close ()
107
- return nil , fmt .Errorf ("server did not return Status OK: %d" , resp .StatusCode )
105
+ if httpResp .StatusCode != http .StatusOK {
106
+ httpResp .Body .Close ()
107
+ return nil , fmt .Errorf ("server did not return Status OK: %d" , httpResp .StatusCode )
108
108
}
109
109
110
110
// buffer response to avoid read issues
111
111
buf := new (bytes.Buffer )
112
- if _ , err := io .Copy (buf , resp .Body ); err != nil {
113
- resp .Body .Close ()
114
- return nil , fmt .Errorf ("unable to buffer response: %v" , err )
112
+ if httpResp .ContentLength > 0 {
113
+ buf .Grow (int (httpResp .ContentLength ))
114
+ }
115
+ if _ , err := io .Copy (buf , httpResp .Body ); err != nil {
116
+ httpResp .Body .Close ()
117
+ return nil , fmt .Errorf ("unable to buffer response: %w" , err )
115
118
}
116
119
117
- resp .Body .Close ()
120
+ httpResp .Body .Close ()
118
121
119
122
// decode reply
120
- ippResp , err := NewResponseDecoder (bytes . NewReader ( buf . Bytes ()) ).Decode (additionalData )
123
+ ippResp , err := NewResponseDecoder (buf ).Decode (additionalData )
121
124
if err != nil {
122
- return nil , fmt .Errorf ("unable to decode IPP response: %v " , err )
125
+ return nil , fmt .Errorf ("unable to decode IPP response: %w " , err )
123
126
}
124
127
125
128
if err = ippResp .CheckForErrors (); err != nil {
126
- return nil , fmt .Errorf ("received error IPP response: %v " , err )
129
+ return nil , fmt .Errorf ("received error IPP response: %w " , err )
127
130
}
128
131
129
132
return ippResp , nil
@@ -132,7 +135,7 @@ func (h *SocketAdapter) SendRequest(url string, r *Request, additionalData io.Wr
132
135
return nil , errors .New ("request retry limit exceeded" )
133
136
}
134
137
135
- //GetSocket returns the path to the cupsd socket by searching SocketSearchPaths
138
+ // GetSocket returns the path to the cupsd socket by searching SocketSearchPaths
136
139
func (h * SocketAdapter ) GetSocket () (string , error ) {
137
140
for _ , path := range h .SocketSearchPaths {
138
141
fi , err := os .Stat (path )
@@ -142,7 +145,7 @@ func (h *SocketAdapter) GetSocket() (string, error) {
142
145
} else if os .IsPermission (err ) {
143
146
return "" , errors .New ("unable to access socket: Access denied" )
144
147
}
145
- return "" , fmt .Errorf ("unable to access socket: %v " , err )
148
+ return "" , fmt .Errorf ("unable to access socket: %w " , err )
146
149
}
147
150
148
151
if fi .Mode ()& os .ModeSocket != 0 {
@@ -153,7 +156,7 @@ func (h *SocketAdapter) GetSocket() (string, error) {
153
156
return "" , SocketNotFoundError
154
157
}
155
158
156
- //GetCert returns the current CUPs authentication certificate by searching CertSearchPaths
159
+ // GetCert returns the current CUPs authentication certificate by searching CertSearchPaths
157
160
func (h * SocketAdapter ) GetCert () (string , error ) {
158
161
for _ , path := range h .CertSearchPaths {
159
162
f , err := os .Open (path )
@@ -163,13 +166,13 @@ func (h *SocketAdapter) GetCert() (string, error) {
163
166
} else if os .IsPermission (err ) {
164
167
return "" , errors .New ("unable to access certificate: Access denied" )
165
168
}
166
- return "" , fmt .Errorf ("unable to access certificate: %v " , err )
169
+ return "" , fmt .Errorf ("unable to access certificate: %w " , err )
167
170
}
168
171
defer f .Close ()
169
172
170
173
buf := new (bytes.Buffer )
171
174
if _ , err := io .Copy (buf , f ); err != nil {
172
- return "" , fmt .Errorf ("unable to access certificate: %v " , err )
175
+ return "" , fmt .Errorf ("unable to access certificate: %w " , err )
173
176
}
174
177
return buf .String (), nil
175
178
}
0 commit comments