]>
Commit | Line | Data |
---|---|---|
ecdc0628 | 1 | /* |
7e86f2f6 | 2 | * IPP utilities for CUPS. |
ecdc0628 | 3 | * |
76ae98d6 | 4 | * Copyright 2007-2017 by Apple Inc. |
7e86f2f6 | 5 | * Copyright 1997-2007 by Easy Software Products. |
ecdc0628 | 6 | * |
7e86f2f6 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
57b7b66b | 11 | * missing or damaged, see the license at "http://www.cups.org/". |
ecdc0628 | 12 | * |
7e86f2f6 | 13 | * This file is subject to the Apple OS-Developed Software exception. |
ecdc0628 | 14 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include "cups-private.h" |
ecdc0628 | 21 | #include <fcntl.h> |
22 | #include <sys/stat.h> | |
23 | #if defined(WIN32) || defined(__EMX__) | |
24 | # include <io.h> | |
25 | #else | |
26 | # include <unistd.h> | |
27 | #endif /* WIN32 || __EMX__ */ | |
b94498cf | 28 | #ifndef O_BINARY |
29 | # define O_BINARY 0 | |
30 | #endif /* O_BINARY */ | |
db8b865d MS |
31 | #ifndef MSG_DONTWAIT |
32 | # define MSG_DONTWAIT 0 | |
33 | #endif /* !MSG_DONTWAIT */ | |
ecdc0628 | 34 | |
35 | ||
36 | /* | |
37 | * 'cupsDoFileRequest()' - Do an IPP request with a file. | |
38 | * | |
5a9febac MS |
39 | * This function sends the IPP request and attached file to the specified |
40 | * server, retrying and authenticating as necessary. The request is freed with | |
41 | * @link ippDelete@. | |
ecdc0628 | 42 | */ |
43 | ||
44 | ipp_t * /* O - Response data */ | |
568fa3fa | 45 | cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ecdc0628 | 46 | ipp_t *request, /* I - IPP request */ |
47 | const char *resource, /* I - HTTP resource for POST */ | |
568fa3fa | 48 | const char *filename) /* I - File to send or @code NULL@ for none */ |
b94498cf | 49 | { |
50 | ipp_t *response; /* IPP response data */ | |
51 | int infile; /* Input file */ | |
52 | ||
53 | ||
807315e6 | 54 | DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", filename=\"%s\")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource, filename)); |
a603edef | 55 | |
b94498cf | 56 | if (filename) |
57 | { | |
58 | if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0) | |
59 | { | |
60 | /* | |
61 | * Can't get file information! | |
62 | */ | |
63 | ||
cb7f98ee | 64 | _cupsSetError(errno == ENOENT ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED, |
749b1e90 | 65 | NULL, 0); |
b94498cf | 66 | |
67 | ippDelete(request); | |
68 | ||
69 | return (NULL); | |
70 | } | |
71 | } | |
72 | else | |
73 | infile = -1; | |
74 | ||
75 | response = cupsDoIORequest(http, request, resource, infile, -1); | |
76 | ||
77 | if (infile >= 0) | |
78 | close(infile); | |
79 | ||
80 | return (response); | |
81 | } | |
82 | ||
83 | ||
84 | /* | |
85 | * 'cupsDoIORequest()' - Do an IPP request with file descriptors. | |
86 | * | |
5a9febac MS |
87 | * This function sends the IPP request with the optional input file "infile" to |
88 | * the specified server, retrying and authenticating as necessary. The request | |
89 | * is freed with @link ippDelete@. | |
b94498cf | 90 | * |
5a9febac | 91 | * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies |
b94498cf | 92 | * all of the data from the file after the IPP request message. |
93 | * | |
5a9febac | 94 | * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies |
b94498cf | 95 | * all of the data after the IPP response message to the file. |
96 | * | |
8072030b | 97 | * @since CUPS 1.3/macOS 10.5@ |
b94498cf | 98 | */ |
99 | ||
100 | ipp_t * /* O - Response data */ | |
568fa3fa | 101 | cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
b94498cf | 102 | ipp_t *request, /* I - IPP request */ |
103 | const char *resource, /* I - HTTP resource for POST */ | |
104 | int infile, /* I - File to read from or -1 for none */ | |
105 | int outfile) /* I - File to write to or -1 for none */ | |
ecdc0628 | 106 | { |
3d052e43 MS |
107 | ipp_t *response = NULL; /* IPP response data */ |
108 | size_t length = 0; /* Content-Length value */ | |
ecdc0628 | 109 | http_status_t status; /* Status of HTTP request */ |
ecdc0628 | 110 | struct stat fileinfo; /* File information */ |
7e86f2f6 | 111 | ssize_t bytes; /* Number of bytes read/written */ |
a74454a7 | 112 | char buffer[32768]; /* Output buffer */ |
ecdc0628 | 113 | |
114 | ||
807315e6 | 115 | DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", infile=%d, outfile=%d)", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource, infile, outfile)); |
3d052e43 MS |
116 | |
117 | /* | |
118 | * Range check input... | |
119 | */ | |
ecdc0628 | 120 | |
3d052e43 | 121 | if (!request || !resource) |
ecdc0628 | 122 | { |
3d052e43 | 123 | ippDelete(request); |
ecdc0628 | 124 | |
cb7f98ee | 125 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
ecdc0628 | 126 | |
127 | return (NULL); | |
128 | } | |
129 | ||
3d052e43 MS |
130 | /* |
131 | * Get the default connection as needed... | |
132 | */ | |
133 | ||
134 | if (!http) | |
a603edef | 135 | if ((http = _cupsConnect()) == NULL) |
e60ec91f MS |
136 | { |
137 | ippDelete(request); | |
138 | ||
a603edef | 139 | return (NULL); |
e60ec91f | 140 | } |
3d052e43 | 141 | |
ecdc0628 | 142 | /* |
143 | * See if we have a file to send... | |
144 | */ | |
145 | ||
b94498cf | 146 | if (infile >= 0) |
ecdc0628 | 147 | { |
b94498cf | 148 | if (fstat(infile, &fileinfo)) |
ecdc0628 | 149 | { |
150 | /* | |
151 | * Can't get file information! | |
152 | */ | |
153 | ||
cb7f98ee | 154 | _cupsSetError(errno == EBADF ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED, |
749b1e90 | 155 | NULL, 0); |
ecdc0628 | 156 | |
157 | ippDelete(request); | |
158 | ||
159 | return (NULL); | |
160 | } | |
161 | ||
162 | #ifdef WIN32 | |
163 | if (fileinfo.st_mode & _S_IFDIR) | |
164 | #else | |
165 | if (S_ISDIR(fileinfo.st_mode)) | |
166 | #endif /* WIN32 */ | |
167 | { | |
168 | /* | |
169 | * Can't send a directory... | |
170 | */ | |
171 | ||
172 | ippDelete(request); | |
173 | ||
cb7f98ee | 174 | _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE, strerror(EISDIR), 0); |
ecdc0628 | 175 | |
176 | return (NULL); | |
177 | } | |
3d052e43 MS |
178 | |
179 | #ifndef WIN32 | |
180 | if (!S_ISREG(fileinfo.st_mode)) | |
181 | length = 0; /* Chunk when piping */ | |
182 | else | |
183 | #endif /* !WIN32 */ | |
7e86f2f6 | 184 | length = ippLength(request) + (size_t)fileinfo.st_size; |
3d052e43 MS |
185 | } |
186 | else | |
187 | length = ippLength(request); | |
188 | ||
e07d4801 | 189 | DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld", |
52f6f666 MS |
190 | (long)ippLength(request), (long)length)); |
191 | ||
b9faaae1 MS |
192 | /* |
193 | * Clear any "Local" authentication data since it is probably stale... | |
194 | */ | |
195 | ||
196 | if (http->authstring && !strncmp(http->authstring, "Local ", 6)) | |
197 | httpSetAuthString(http, NULL, NULL); | |
198 | ||
3d052e43 MS |
199 | /* |
200 | * Loop until we can send the request without authorization problems. | |
201 | */ | |
202 | ||
203 | while (response == NULL) | |
204 | { | |
e07d4801 | 205 | DEBUG_puts("2cupsDoIORequest: setup..."); |
3d052e43 MS |
206 | |
207 | /* | |
208 | * Send the request... | |
209 | */ | |
210 | ||
211 | status = cupsSendRequest(http, request, resource, length); | |
212 | ||
e07d4801 | 213 | DEBUG_printf(("2cupsDoIORequest: status=%d", status)); |
3d052e43 | 214 | |
cb7f98ee | 215 | if (status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA && infile >= 0) |
3d052e43 | 216 | { |
e07d4801 | 217 | DEBUG_puts("2cupsDoIORequest: file write..."); |
3d052e43 MS |
218 | |
219 | /* | |
220 | * Send the file with the request... | |
221 | */ | |
222 | ||
223 | #ifndef WIN32 | |
224 | if (S_ISREG(fileinfo.st_mode)) | |
225 | #endif /* WIN32 */ | |
226 | lseek(infile, 0, SEEK_SET); | |
227 | ||
7e86f2f6 | 228 | while ((bytes = read(infile, buffer, sizeof(buffer))) > 0) |
3d052e43 | 229 | { |
7e86f2f6 | 230 | if ((status = cupsWriteRequestData(http, buffer, (size_t)bytes)) |
cb7f98ee | 231 | != HTTP_STATUS_CONTINUE) |
83e08001 | 232 | break; |
3d052e43 MS |
233 | } |
234 | } | |
235 | ||
236 | /* | |
237 | * Get the server's response... | |
238 | */ | |
239 | ||
ad6f5490 | 240 | if (status <= HTTP_STATUS_CONTINUE || status == HTTP_STATUS_OK) |
3d052e43 MS |
241 | { |
242 | response = cupsGetResponse(http, resource); | |
83e08001 | 243 | status = httpGetStatus(http); |
3d052e43 MS |
244 | } |
245 | ||
f11a948a MS |
246 | DEBUG_printf(("2cupsDoIORequest: status=%d", status)); |
247 | ||
cb7f98ee MS |
248 | if (status == HTTP_STATUS_ERROR || |
249 | (status >= HTTP_STATUS_BAD_REQUEST && status != HTTP_STATUS_UNAUTHORIZED && | |
250 | status != HTTP_STATUS_UPGRADE_REQUIRED)) | |
ee571f26 MS |
251 | { |
252 | _cupsSetHTTPError(status); | |
568fa3fa | 253 | break; |
ee571f26 | 254 | } |
568fa3fa | 255 | |
85dda01c | 256 | if (response && outfile >= 0) |
3d052e43 | 257 | { |
85dda01c MS |
258 | /* |
259 | * Write trailing data to file... | |
260 | */ | |
3d052e43 | 261 | |
7e86f2f6 MS |
262 | while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0) |
263 | if (write(outfile, buffer, (size_t)bytes) < bytes) | |
85dda01c MS |
264 | break; |
265 | } | |
3d052e43 | 266 | |
cb7f98ee | 267 | if (http->state != HTTP_STATE_WAITING) |
85dda01c MS |
268 | { |
269 | /* | |
270 | * Flush any remaining data... | |
271 | */ | |
272 | ||
273 | httpFlush(http); | |
3d052e43 MS |
274 | } |
275 | } | |
276 | ||
277 | /* | |
278 | * Delete the original request and return the response... | |
279 | */ | |
6d2f911b | 280 | |
3d052e43 MS |
281 | ippDelete(request); |
282 | ||
283 | return (response); | |
284 | } | |
285 | ||
286 | ||
287 | /* | |
288 | * 'cupsDoRequest()' - Do an IPP request. | |
289 | * | |
290 | * This function sends the IPP request to the specified server, retrying | |
5a9febac | 291 | * and authenticating as necessary. The request is freed with @link ippDelete@. |
3d052e43 MS |
292 | */ |
293 | ||
294 | ipp_t * /* O - Response data */ | |
568fa3fa | 295 | cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
296 | ipp_t *request, /* I - IPP request */ |
297 | const char *resource) /* I - HTTP resource for POST */ | |
298 | { | |
807315e6 | 299 | DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource)); |
a603edef | 300 | |
b19ccc9e | 301 | return (cupsDoIORequest(http, request, resource, -1, -1)); |
3d052e43 MS |
302 | } |
303 | ||
304 | ||
305 | /* | |
306 | * 'cupsGetResponse()' - Get a response to an IPP request. | |
307 | * | |
308 | * Use this function to get the response for an IPP request sent using | |
5a9febac MS |
309 | * @link cupsSendRequest@. For requests that return additional data, use |
310 | * @link cupsReadResponseData@ after getting a successful response, | |
311 | * otherwise call @link httpFlush@ to complete the response processing. | |
3d052e43 | 312 | * |
8072030b | 313 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 MS |
314 | */ |
315 | ||
568fa3fa MS |
316 | ipp_t * /* O - Response or @code NULL@ on HTTP error */ |
317 | cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
3d052e43 MS |
318 | const char *resource) /* I - HTTP resource for POST */ |
319 | { | |
320 | http_status_t status; /* HTTP status */ | |
321 | ipp_state_t state; /* IPP read state */ | |
322 | ipp_t *response = NULL; /* IPP response */ | |
323 | ||
324 | ||
807315e6 | 325 | DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", (void *)http, resource)); |
82bf2283 | 326 | DEBUG_printf(("1cupsGetResponse: http->state=%d", http ? http->state : HTTP_STATE_ERROR)); |
3d052e43 MS |
327 | |
328 | /* | |
329 | * Connect to the default server as needed... | |
330 | */ | |
331 | ||
332 | if (!http) | |
82bf2283 MS |
333 | { |
334 | _cups_globals_t *cg = _cupsGlobals(); | |
335 | /* Pointer to library globals */ | |
336 | ||
337 | if ((http = cg->http) == NULL) | |
338 | { | |
339 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection."), 1); | |
340 | DEBUG_puts("1cupsGetResponse: No active connection - returning NULL."); | |
341 | return (NULL); | |
342 | } | |
343 | } | |
3d052e43 | 344 | |
82bf2283 MS |
345 | if (http->state != HTTP_STATE_POST_RECV && http->state != HTTP_STATE_POST_SEND) |
346 | { | |
347 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request sent."), 1); | |
348 | DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL."); | |
3d052e43 | 349 | return (NULL); |
82bf2283 | 350 | } |
3d052e43 MS |
351 | |
352 | /* | |
353 | * Check for an unfinished chunked request... | |
354 | */ | |
355 | ||
cb7f98ee | 356 | if (http->data_encoding == HTTP_ENCODING_CHUNKED) |
3d052e43 MS |
357 | { |
358 | /* | |
359 | * Send a 0-length chunk to finish off the request... | |
360 | */ | |
361 | ||
e07d4801 | 362 | DEBUG_puts("2cupsGetResponse: Finishing chunked POST..."); |
3d052e43 MS |
363 | |
364 | if (httpWrite2(http, "", 0) < 0) | |
365 | return (NULL); | |
366 | } | |
367 | ||
368 | /* | |
369 | * Wait for a response from the server... | |
370 | */ | |
371 | ||
e07d4801 | 372 | DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...", |
1ff0402e | 373 | http->status)); |
3d052e43 | 374 | |
e60ec91f MS |
375 | do |
376 | { | |
1ff0402e | 377 | status = httpUpdate(http); |
e60ec91f | 378 | } |
a469f8a5 | 379 | while (status == HTTP_STATUS_CONTINUE); |
3d052e43 | 380 | |
e07d4801 | 381 | DEBUG_printf(("2cupsGetResponse: status=%d", status)); |
3d052e43 | 382 | |
cb7f98ee | 383 | if (status == HTTP_STATUS_OK) |
3d052e43 MS |
384 | { |
385 | /* | |
386 | * Get the IPP response... | |
387 | */ | |
388 | ||
389 | response = ippNew(); | |
390 | ||
cb7f98ee MS |
391 | while ((state = ippRead(http, response)) != IPP_STATE_DATA) |
392 | if (state == IPP_STATE_ERROR) | |
3d052e43 MS |
393 | break; |
394 | ||
cb7f98ee | 395 | if (state == IPP_STATE_ERROR) |
3d052e43 MS |
396 | { |
397 | /* | |
f14324a7 | 398 | * Flush remaining data and delete the response... |
3d052e43 MS |
399 | */ |
400 | ||
e07d4801 | 401 | DEBUG_puts("1cupsGetResponse: IPP read error!"); |
3d052e43 | 402 | |
f14324a7 MS |
403 | httpFlush(http); |
404 | ||
3d052e43 MS |
405 | ippDelete(response); |
406 | response = NULL; | |
407 | ||
cb7f98ee | 408 | http->status = status = HTTP_STATUS_ERROR; |
a29fd7dd | 409 | http->error = EINVAL; |
3d052e43 MS |
410 | } |
411 | } | |
cb7f98ee | 412 | else if (status != HTTP_STATUS_ERROR) |
3d052e43 MS |
413 | { |
414 | /* | |
415 | * Flush any error message... | |
416 | */ | |
417 | ||
418 | httpFlush(http); | |
419 | ||
420 | /* | |
421 | * Then handle encryption and authentication... | |
422 | */ | |
423 | ||
cb7f98ee | 424 | if (status == HTTP_STATUS_UNAUTHORIZED) |
3d052e43 MS |
425 | { |
426 | /* | |
427 | * See if we can do authentication... | |
428 | */ | |
429 | ||
e07d4801 | 430 | DEBUG_puts("2cupsGetResponse: Need authorization..."); |
568fa3fa | 431 | |
e07d4801 | 432 | if (!cupsDoAuthentication(http, "POST", resource)) |
cb7f98ee | 433 | httpReconnect2(http, 30000, NULL); |
f11a948a | 434 | else |
cb7f98ee | 435 | http->status = status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
3d052e43 MS |
436 | } |
437 | ||
438 | #ifdef HAVE_SSL | |
cb7f98ee | 439 | else if (status == HTTP_STATUS_UPGRADE_REQUIRED) |
3d052e43 MS |
440 | { |
441 | /* | |
442 | * Force a reconnect with encryption... | |
443 | */ | |
444 | ||
e07d4801 | 445 | DEBUG_puts("2cupsGetResponse: Need encryption..."); |
3d052e43 | 446 | |
cb7f98ee MS |
447 | if (!httpReconnect2(http, 30000, NULL)) |
448 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED); | |
3d052e43 MS |
449 | } |
450 | #endif /* HAVE_SSL */ | |
451 | } | |
452 | ||
453 | if (response) | |
454 | { | |
455 | ipp_attribute_t *attr; /* status-message attribute */ | |
456 | ||
457 | ||
458 | attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT); | |
459 | ||
e07d4801 | 460 | DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"", |
a603edef MS |
461 | ippErrorString(response->request.status.status_code), |
462 | attr ? attr->values[0].string.text : "")); | |
463 | ||
3d052e43 | 464 | _cupsSetError(response->request.status.status_code, |
749b1e90 MS |
465 | attr ? attr->values[0].string.text : |
466 | ippErrorString(response->request.status.status_code), 0); | |
3d052e43 | 467 | } |
3d052e43 MS |
468 | |
469 | return (response); | |
470 | } | |
471 | ||
472 | ||
6d2f911b | 473 | /* |
5a9febac MS |
474 | * 'cupsLastError()' - Return the last IPP status code received on the current |
475 | * thread. | |
6d2f911b MS |
476 | */ |
477 | ||
478 | ipp_status_t /* O - IPP status code from last request */ | |
479 | cupsLastError(void) | |
480 | { | |
481 | return (_cupsGlobals()->last_error); | |
482 | } | |
483 | ||
484 | ||
485 | /* | |
5a9febac MS |
486 | * 'cupsLastErrorString()' - Return the last IPP status-message received on the |
487 | * current thread. | |
6d2f911b | 488 | * |
8072030b | 489 | * @since CUPS 1.2/macOS 10.5@ |
6d2f911b MS |
490 | */ |
491 | ||
492 | const char * /* O - status-message text from last request */ | |
493 | cupsLastErrorString(void) | |
494 | { | |
495 | return (_cupsGlobals()->last_status_message); | |
496 | } | |
497 | ||
498 | ||
f14324a7 MS |
499 | /* |
500 | * '_cupsNextDelay()' - Return the next retry delay value. | |
501 | * | |
502 | * This function currently returns the Fibonacci sequence 1 1 2 3 5 8. | |
503 | * | |
504 | * Pass 0 for the current delay value to initialize the sequence. | |
505 | */ | |
506 | ||
507 | int /* O - Next delay value */ | |
508 | _cupsNextDelay(int current, /* I - Current delay value or 0 */ | |
509 | int *previous) /* IO - Previous delay value */ | |
510 | { | |
511 | int next; /* Next delay value */ | |
512 | ||
513 | ||
514 | if (current > 0) | |
515 | { | |
516 | next = (current + *previous) % 12; | |
517 | *previous = next < current ? 0 : current; | |
518 | } | |
519 | else | |
520 | { | |
521 | next = 1; | |
522 | *previous = 0; | |
523 | } | |
524 | ||
525 | return (next); | |
526 | } | |
527 | ||
528 | ||
3d052e43 MS |
529 | /* |
530 | * 'cupsReadResponseData()' - Read additional data after the IPP response. | |
531 | * | |
5a9febac MS |
532 | * This function is used after @link cupsGetResponse@ to read the PPD or document |
533 | * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests, | |
534 | * respectively. | |
3d052e43 | 535 | * |
8072030b | 536 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 MS |
537 | */ |
538 | ||
539 | ssize_t /* O - Bytes read, 0 on EOF, -1 on error */ | |
540 | cupsReadResponseData( | |
568fa3fa | 541 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
542 | char *buffer, /* I - Buffer to use */ |
543 | size_t length) /* I - Number of bytes to read */ | |
544 | { | |
545 | /* | |
546 | * Get the default connection as needed... | |
547 | */ | |
548 | ||
807315e6 | 549 | DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length)); |
a603edef | 550 | |
3d052e43 MS |
551 | if (!http) |
552 | { | |
553 | _cups_globals_t *cg = _cupsGlobals(); | |
554 | /* Pointer to library globals */ | |
555 | ||
556 | if ((http = cg->http) == NULL) | |
557 | { | |
cb7f98ee | 558 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1); |
3d052e43 MS |
559 | return (-1); |
560 | } | |
ecdc0628 | 561 | } |
ecdc0628 | 562 | |
3d052e43 MS |
563 | /* |
564 | * Then read from the HTTP connection... | |
565 | */ | |
566 | ||
567 | return (httpRead2(http, buffer, length)); | |
568 | } | |
569 | ||
570 | ||
571 | /* | |
572 | * 'cupsSendRequest()' - Send an IPP request. | |
573 | * | |
5a9febac MS |
574 | * Use @link cupsWriteRequestData@ to write any additional data (document, PPD |
575 | * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response, | |
576 | * and @link cupsReadResponseData@ to read any additional data following the | |
577 | * response. Only one request can be sent/queued at a time per @code http_t@ | |
578 | * connection. | |
579 | * | |
cb7f98ee | 580 | * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@ |
5a9febac | 581 | * on a successful send of the request. |
3d052e43 | 582 | * |
5a9febac MS |
583 | * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and |
584 | * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@. | |
3d052e43 | 585 | * |
8072030b | 586 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 MS |
587 | */ |
588 | ||
589 | http_status_t /* O - Initial HTTP status */ | |
568fa3fa | 590 | cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
591 | ipp_t *request, /* I - IPP request */ |
592 | const char *resource, /* I - Resource path */ | |
ae71f5de | 593 | size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */ |
3d052e43 | 594 | { |
a469f8a5 MS |
595 | http_status_t status; /* Status of HTTP request */ |
596 | int got_status; /* Did we get the status? */ | |
597 | ipp_state_t state; /* State of IPP processing */ | |
598 | http_status_t expect; /* Expect: header to use */ | |
76ae98d6 | 599 | char date[256]; /* Date: header value */ |
3d052e43 MS |
600 | |
601 | ||
807315e6 | 602 | DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", length=" CUPS_LLFMT ")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource, CUPS_LLCAST length)); |
3d052e43 MS |
603 | |
604 | /* | |
605 | * Range check input... | |
606 | */ | |
607 | ||
608 | if (!request || !resource) | |
609 | { | |
cb7f98ee | 610 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); |
3d052e43 | 611 | |
cb7f98ee | 612 | return (HTTP_STATUS_ERROR); |
3d052e43 MS |
613 | } |
614 | ||
615 | /* | |
616 | * Get the default connection as needed... | |
617 | */ | |
618 | ||
619 | if (!http) | |
a603edef | 620 | if ((http = _cupsConnect()) == NULL) |
cb7f98ee | 621 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
3d052e43 | 622 | |
a4845881 MS |
623 | /* |
624 | * If the prior request was not flushed out, do so now... | |
625 | */ | |
626 | ||
cb7f98ee MS |
627 | if (http->state == HTTP_STATE_GET_SEND || |
628 | http->state == HTTP_STATE_POST_SEND) | |
10ddcf65 MS |
629 | { |
630 | DEBUG_puts("2cupsSendRequest: Flush prior response."); | |
a4845881 | 631 | httpFlush(http); |
10ddcf65 | 632 | } |
cb7f98ee | 633 | else if (http->state != HTTP_STATE_WAITING) |
a4845881 | 634 | { |
82cc1f9a MS |
635 | DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), " |
636 | "reconnecting.", http->state)); | |
cb7f98ee MS |
637 | if (httpReconnect2(http, 30000, NULL)) |
638 | return (HTTP_STATUS_ERROR); | |
a4845881 MS |
639 | } |
640 | ||
7594b224 | 641 | #ifdef HAVE_SSL |
642 | /* | |
643 | * See if we have an auth-info attribute and are communicating over | |
644 | * a non-local link. If so, encrypt the link so that we can pass | |
645 | * the authentication information securely... | |
646 | */ | |
647 | ||
648 | if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) && | |
649 | !httpAddrLocalhost(http->hostaddr) && !http->tls && | |
cb7f98ee | 650 | httpEncryption(http, HTTP_ENCRYPTION_REQUIRED)) |
b19ccc9e | 651 | { |
10ddcf65 | 652 | DEBUG_puts("1cupsSendRequest: Unable to encrypt connection."); |
cb7f98ee | 653 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
b19ccc9e | 654 | } |
7594b224 | 655 | #endif /* HAVE_SSL */ |
656 | ||
1ff0402e MS |
657 | /* |
658 | * Reconnect if the last response had a "Connection: close"... | |
659 | */ | |
660 | ||
88f9aafc | 661 | if (!_cups_strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close")) |
10ddcf65 MS |
662 | { |
663 | DEBUG_puts("2cupsSendRequest: Connection: close"); | |
664 | httpClearFields(http); | |
cb7f98ee | 665 | if (httpReconnect2(http, 30000, NULL)) |
b19ccc9e | 666 | { |
10ddcf65 | 667 | DEBUG_puts("1cupsSendRequest: Unable to reconnect."); |
cb7f98ee | 668 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
b19ccc9e | 669 | } |
10ddcf65 | 670 | } |
1ff0402e | 671 | |
ecdc0628 | 672 | /* |
673 | * Loop until we can send the request without authorization problems. | |
674 | */ | |
675 | ||
cb7f98ee | 676 | expect = HTTP_STATUS_CONTINUE; |
ecdc0628 | 677 | |
3d052e43 | 678 | for (;;) |
ecdc0628 | 679 | { |
e07d4801 | 680 | DEBUG_puts("2cupsSendRequest: Setup..."); |
ecdc0628 | 681 | |
682 | /* | |
683 | * Setup the HTTP variables needed... | |
684 | */ | |
685 | ||
ecdc0628 | 686 | httpClearFields(http); |
0268488e | 687 | httpSetExpect(http, expect); |
ecdc0628 | 688 | httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp"); |
76ae98d6 | 689 | httpSetField(http, HTTP_FIELD_DATE, httpGetDateString2(time(NULL), date, (int)sizeof(date))); |
0268488e MS |
690 | httpSetLength(http, length); |
691 | ||
692 | #ifdef HAVE_GSSAPI | |
693 | if (http->authstring && !strncmp(http->authstring, "Negotiate", 9)) | |
694 | { | |
695 | /* | |
696 | * Do not use cached Kerberos credentials since they will look like a | |
697 | * "replay" attack... | |
698 | */ | |
699 | ||
eac3a0a0 | 700 | _cupsSetNegotiateAuthString(http, "POST", resource); |
0268488e | 701 | } |
0268488e | 702 | #endif /* HAVE_GSSAPI */ |
f14324a7 | 703 | |
ecdc0628 | 704 | httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring); |
705 | ||
e07d4801 | 706 | DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring)); |
ecdc0628 | 707 | |
708 | /* | |
709 | * Try the request... | |
710 | */ | |
711 | ||
e07d4801 | 712 | DEBUG_puts("2cupsSendRequest: Sending HTTP POST..."); |
ecdc0628 | 713 | |
714 | if (httpPost(http, resource)) | |
715 | { | |
10ddcf65 | 716 | DEBUG_puts("2cupsSendRequest: POST failed, reconnecting."); |
cb7f98ee | 717 | if (httpReconnect2(http, 30000, NULL)) |
b19ccc9e | 718 | { |
10ddcf65 | 719 | DEBUG_puts("1cupsSendRequest: Unable to reconnect."); |
cb7f98ee | 720 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
b19ccc9e | 721 | } |
ecdc0628 | 722 | else |
723 | continue; | |
724 | } | |
725 | ||
726 | /* | |
d6ae789d | 727 | * Send the IPP data... |
ecdc0628 | 728 | */ |
729 | ||
e07d4801 | 730 | DEBUG_puts("2cupsSendRequest: Writing IPP request..."); |
b423cd4c | 731 | |
cb7f98ee MS |
732 | request->state = IPP_STATE_IDLE; |
733 | status = HTTP_STATUS_CONTINUE; | |
d6ae789d | 734 | got_status = 0; |
735 | ||
cb7f98ee | 736 | while ((state = ippWrite(http, request)) != IPP_STATE_DATA) |
88a98866 MS |
737 | { |
738 | if (httpCheck(http)) | |
d6ae789d | 739 | { |
740 | got_status = 1; | |
741 | ||
e60ec91f | 742 | _httpUpdate(http, &status); |
cb7f98ee | 743 | if (status >= HTTP_STATUS_MULTIPLE_CHOICES) |
d6ae789d | 744 | break; |
745 | } | |
88a98866 MS |
746 | else if (state == IPP_STATE_ERROR) |
747 | break; | |
748 | } | |
d6ae789d | 749 | |
cb7f98ee | 750 | if (state == IPP_STATE_ERROR) |
22c9029b | 751 | { |
88a98866 MS |
752 | /* |
753 | * We weren't able to send the IPP request. But did we already get a HTTP | |
754 | * error status? | |
755 | */ | |
10ddcf65 | 756 | |
88a98866 MS |
757 | if (!got_status || status < HTTP_STATUS_MULTIPLE_CHOICES) |
758 | { | |
759 | /* | |
760 | * No, something else went wrong. | |
761 | */ | |
22c9029b | 762 | |
88a98866 MS |
763 | DEBUG_puts("1cupsSendRequest: Unable to send IPP request."); |
764 | ||
765 | http->status = HTTP_STATUS_ERROR; | |
766 | http->state = HTTP_STATE_WAITING; | |
767 | ||
768 | return (HTTP_STATUS_ERROR); | |
769 | } | |
22c9029b MS |
770 | } |
771 | ||
3d052e43 MS |
772 | /* |
773 | * Wait up to 1 second to get the 100-continue response as needed... | |
774 | */ | |
ecdc0628 | 775 | |
e60ec91f | 776 | if (!got_status) |
3d052e43 | 777 | { |
cb7f98ee | 778 | if (expect == HTTP_STATUS_CONTINUE) |
e60ec91f MS |
779 | { |
780 | DEBUG_puts("2cupsSendRequest: Waiting for 100-continue..."); | |
a603edef | 781 | |
e60ec91f MS |
782 | if (httpWait(http, 1000)) |
783 | _httpUpdate(http, &status); | |
784 | } | |
785 | else if (httpCheck(http)) | |
786 | _httpUpdate(http, &status); | |
d6ae789d | 787 | } |
ecdc0628 | 788 | |
e07d4801 | 789 | DEBUG_printf(("2cupsSendRequest: status=%d", status)); |
a603edef | 790 | |
ecdc0628 | 791 | /* |
3d052e43 | 792 | * Process the current HTTP status... |
ecdc0628 | 793 | */ |
794 | ||
a469f8a5 | 795 | if (status >= HTTP_STATUS_MULTIPLE_CHOICES) |
83e08001 | 796 | { |
a469f8a5 MS |
797 | int temp_status; /* Temporary status */ |
798 | ||
83e08001 MS |
799 | _cupsSetHTTPError(status); |
800 | ||
801 | do | |
802 | { | |
a469f8a5 | 803 | temp_status = httpUpdate(http); |
83e08001 | 804 | } |
a469f8a5 MS |
805 | while (temp_status != HTTP_STATUS_ERROR && |
806 | http->state == HTTP_STATE_POST_RECV); | |
83e08001 | 807 | |
ba55dc12 | 808 | httpFlush(http); |
83e08001 | 809 | } |
ba55dc12 | 810 | |
3d052e43 | 811 | switch (status) |
ecdc0628 | 812 | { |
a469f8a5 MS |
813 | case HTTP_STATUS_CONTINUE : |
814 | case HTTP_STATUS_OK : | |
815 | case HTTP_STATUS_ERROR : | |
10ddcf65 | 816 | DEBUG_printf(("1cupsSendRequest: Returning %d.", status)); |
3d052e43 | 817 | return (status); |
ecdc0628 | 818 | |
a469f8a5 | 819 | case HTTP_STATUS_UNAUTHORIZED : |
ba55dc12 | 820 | if (cupsDoAuthentication(http, "POST", resource)) |
10ddcf65 | 821 | { |
cb7f98ee MS |
822 | DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED."); |
823 | return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED); | |
10ddcf65 MS |
824 | } |
825 | ||
cb7f98ee | 826 | DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED."); |
ba55dc12 | 827 | |
cb7f98ee | 828 | if (httpReconnect2(http, 30000, NULL)) |
f11a948a | 829 | { |
10ddcf65 | 830 | DEBUG_puts("1cupsSendRequest: Unable to reconnect."); |
cb7f98ee | 831 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
f11a948a | 832 | } |
ba55dc12 | 833 | break; |
89d46774 | 834 | |
ecdc0628 | 835 | #ifdef HAVE_SSL |
a469f8a5 | 836 | case HTTP_STATUS_UPGRADE_REQUIRED : |
3d052e43 MS |
837 | /* |
838 | * Flush any error message, reconnect, and then upgrade with | |
839 | * encryption... | |
840 | */ | |
ecdc0628 | 841 | |
10ddcf65 | 842 | DEBUG_puts("2cupsSendRequest: Reconnecting after " |
cb7f98ee | 843 | "HTTP_STATUS_UPGRADE_REQUIRED."); |
10ddcf65 | 844 | |
cb7f98ee | 845 | if (httpReconnect2(http, 30000, NULL)) |
b19ccc9e | 846 | { |
10ddcf65 | 847 | DEBUG_puts("1cupsSendRequest: Unable to reconnect."); |
cb7f98ee | 848 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
b19ccc9e | 849 | } |
ecdc0628 | 850 | |
10ddcf65 | 851 | DEBUG_puts("2cupsSendRequest: Upgrading to TLS."); |
cb7f98ee | 852 | if (httpEncryption(http, HTTP_ENCRYPTION_REQUIRED)) |
ba55dc12 | 853 | { |
10ddcf65 | 854 | DEBUG_puts("1cupsSendRequest: Unable to encrypt connection."); |
cb7f98ee | 855 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
ba55dc12 MS |
856 | } |
857 | break; | |
ecdc0628 | 858 | #endif /* HAVE_SSL */ |
f301802f | 859 | |
a469f8a5 | 860 | case HTTP_STATUS_EXPECTATION_FAILED : |
3d052e43 MS |
861 | /* |
862 | * Don't try using the Expect: header the next time around... | |
863 | */ | |
ecdc0628 | 864 | |
3d052e43 | 865 | expect = (http_status_t)0; |
52f6f666 | 866 | |
10ddcf65 MS |
867 | DEBUG_puts("2cupsSendRequest: Reconnecting after " |
868 | "HTTP_EXPECTATION_FAILED."); | |
869 | ||
cb7f98ee | 870 | if (httpReconnect2(http, 30000, NULL)) |
52f6f666 | 871 | { |
10ddcf65 | 872 | DEBUG_puts("1cupsSendRequest: Unable to reconnect."); |
cb7f98ee | 873 | return (HTTP_STATUS_SERVICE_UNAVAILABLE); |
52f6f666 | 874 | } |
a603edef | 875 | break; |
ecdc0628 | 876 | |
3d052e43 MS |
877 | default : |
878 | /* | |
879 | * Some other error... | |
880 | */ | |
ecdc0628 | 881 | |
3d052e43 | 882 | return (status); |
b94498cf | 883 | } |
884 | } | |
3d052e43 MS |
885 | } |
886 | ||
887 | ||
888 | /* | |
889 | * 'cupsWriteRequestData()' - Write additional data after an IPP request. | |
890 | * | |
568fa3fa MS |
891 | * This function is used after @link cupsSendRequest@ to provide a PPD and |
892 | * after @link cupsStartDocument@ to provide a document file. | |
3d052e43 | 893 | * |
8072030b | 894 | * @since CUPS 1.4/macOS 10.6@ |
3d052e43 | 895 | */ |
ecdc0628 | 896 | |
cb7f98ee | 897 | http_status_t /* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */ |
3d052e43 | 898 | cupsWriteRequestData( |
568fa3fa | 899 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
3d052e43 MS |
900 | const char *buffer, /* I - Bytes to write */ |
901 | size_t length) /* I - Number of bytes to write */ | |
902 | { | |
38e73f87 MS |
903 | int wused; /* Previous bytes in buffer */ |
904 | ||
905 | ||
ecdc0628 | 906 | /* |
3d052e43 | 907 | * Get the default connection as needed... |
ecdc0628 | 908 | */ |
ecdc0628 | 909 | |
807315e6 | 910 | DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length)); |
a603edef | 911 | |
3d052e43 | 912 | if (!http) |
ecdc0628 | 913 | { |
3d052e43 MS |
914 | _cups_globals_t *cg = _cupsGlobals(); |
915 | /* Pointer to library globals */ | |
ecdc0628 | 916 | |
3d052e43 MS |
917 | if ((http = cg->http) == NULL) |
918 | { | |
cb7f98ee MS |
919 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1); |
920 | DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR."); | |
921 | return (HTTP_STATUS_ERROR); | |
3d052e43 | 922 | } |
ecdc0628 | 923 | } |
ecdc0628 | 924 | |
3d052e43 MS |
925 | /* |
926 | * Then write to the HTTP connection... | |
927 | */ | |
ecdc0628 | 928 | |
38e73f87 MS |
929 | wused = http->wused; |
930 | ||
3d052e43 | 931 | if (httpWrite2(http, buffer, length) < 0) |
10d09e33 | 932 | { |
cb7f98ee MS |
933 | DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR."); |
934 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0); | |
935 | return (HTTP_STATUS_ERROR); | |
10d09e33 | 936 | } |
ecdc0628 | 937 | |
3d052e43 MS |
938 | /* |
939 | * Finally, check if we have any pending data from the server... | |
940 | */ | |
ecdc0628 | 941 | |
f11a948a | 942 | if (length >= HTTP_MAX_BUFFER || |
38e73f87 | 943 | http->wused < wused || |
7e86f2f6 | 944 | (wused > 0 && (size_t)http->wused == length)) |
38e73f87 MS |
945 | { |
946 | /* | |
947 | * We've written something to the server, so check for response data... | |
948 | */ | |
949 | ||
950 | if (_httpWait(http, 0, 1)) | |
ba55dc12 | 951 | { |
e60ec91f | 952 | http_status_t status; /* Status from _httpUpdate */ |
ba55dc12 | 953 | |
e60ec91f | 954 | _httpUpdate(http, &status); |
cb7f98ee | 955 | if (status >= HTTP_STATUS_MULTIPLE_CHOICES) |
10d09e33 MS |
956 | { |
957 | _cupsSetHTTPError(status); | |
83e08001 MS |
958 | |
959 | do | |
960 | { | |
961 | status = httpUpdate(http); | |
962 | } | |
cb7f98ee | 963 | while (status != HTTP_STATUS_ERROR && http->state == HTTP_STATE_POST_RECV); |
83e08001 | 964 | |
ba55dc12 | 965 | httpFlush(http); |
10d09e33 | 966 | } |
ba55dc12 | 967 | |
10d09e33 | 968 | DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status)); |
ba55dc12 MS |
969 | return (status); |
970 | } | |
38e73f87 MS |
971 | } |
972 | ||
cb7f98ee MS |
973 | DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE."); |
974 | return (HTTP_STATUS_CONTINUE); | |
ecdc0628 | 975 | } |
976 | ||
977 | ||
6d2f911b MS |
978 | /* |
979 | * '_cupsConnect()' - Get the default server connection... | |
980 | */ | |
981 | ||
982 | http_t * /* O - HTTP connection */ | |
983 | _cupsConnect(void) | |
984 | { | |
985 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
986 | ||
987 | ||
988 | /* | |
989 | * See if we are connected to the same server... | |
990 | */ | |
991 | ||
992 | if (cg->http) | |
993 | { | |
994 | /* | |
995 | * Compare the connection hostname, port, and encryption settings to | |
996 | * the cached defaults; these were initialized the first time we | |
997 | * connected... | |
998 | */ | |
999 | ||
1000 | if (strcmp(cg->http->hostname, cg->server) || | |
a469f8a5 | 1001 | cg->ipp_port != httpAddrPort(cg->http->hostaddr) || |
6d2f911b | 1002 | (cg->http->encryption != cg->encryption && |
cb7f98ee | 1003 | cg->http->encryption == HTTP_ENCRYPTION_NEVER)) |
6d2f911b MS |
1004 | { |
1005 | /* | |
1006 | * Need to close the current connection because something has changed... | |
1007 | */ | |
1008 | ||
1009 | httpClose(cg->http); | |
1010 | cg->http = NULL; | |
1011 | } | |
0fa6c7fa MS |
1012 | else |
1013 | { | |
1014 | /* | |
1015 | * Same server, see if the connection is still established... | |
1016 | */ | |
1017 | ||
80360a5e MS |
1018 | char ch; /* Connection check byte */ |
1019 | ssize_t n; /* Number of bytes */ | |
0fa6c7fa | 1020 | |
db8b865d | 1021 | #ifdef WIN32 |
80360a5e MS |
1022 | if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK)) == 0 || |
1023 | (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK)) | |
db8b865d | 1024 | #else |
80360a5e MS |
1025 | if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK | MSG_DONTWAIT)) == 0 || |
1026 | (n < 0 && errno != EWOULDBLOCK)) | |
db8b865d | 1027 | #endif /* WIN32 */ |
0fa6c7fa MS |
1028 | { |
1029 | /* | |
1030 | * Nope, close the connection... | |
1031 | */ | |
1032 | ||
1033 | httpClose(cg->http); | |
1034 | cg->http = NULL; | |
1035 | } | |
1036 | } | |
6d2f911b MS |
1037 | } |
1038 | ||
1039 | /* | |
1040 | * (Re)connect as needed... | |
1041 | */ | |
1042 | ||
1043 | if (!cg->http) | |
1044 | { | |
cb7f98ee MS |
1045 | if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, |
1046 | cupsEncryption(), 1, 30000, NULL)) == NULL) | |
6d2f911b MS |
1047 | { |
1048 | if (errno) | |
cb7f98ee | 1049 | _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0); |
6d2f911b | 1050 | else |
cb7f98ee | 1051 | _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, |
6d2f911b MS |
1052 | _("Unable to connect to host."), 1); |
1053 | } | |
1054 | } | |
1055 | ||
1056 | /* | |
1057 | * Return the cached connection... | |
1058 | */ | |
1059 | ||
1060 | return (cg->http); | |
1061 | } | |
1062 | ||
1063 | ||
ecdc0628 | 1064 | /* |
1065 | * '_cupsSetError()' - Set the last IPP status code and status-message. | |
1066 | */ | |
1067 | ||
1068 | void | |
1069 | _cupsSetError(ipp_status_t status, /* I - IPP status code */ | |
749b1e90 MS |
1070 | const char *message, /* I - status-message value */ |
1071 | int localize) /* I - Localize the message? */ | |
ecdc0628 | 1072 | { |
1073 | _cups_globals_t *cg; /* Global data */ | |
1074 | ||
1075 | ||
749b1e90 MS |
1076 | if (!message && errno) |
1077 | { | |
1078 | message = strerror(errno); | |
1079 | localize = 0; | |
1080 | } | |
1081 | ||
ecdc0628 | 1082 | cg = _cupsGlobals(); |
1083 | cg->last_error = status; | |
1084 | ||
1085 | if (cg->last_status_message) | |
1086 | { | |
749b1e90 | 1087 | _cupsStrFree(cg->last_status_message); |
ecdc0628 | 1088 | |
1089 | cg->last_status_message = NULL; | |
1090 | } | |
1091 | ||
1092 | if (message) | |
749b1e90 MS |
1093 | { |
1094 | if (localize) | |
1095 | { | |
1096 | /* | |
1097 | * Get the message catalog... | |
1098 | */ | |
1099 | ||
1100 | if (!cg->lang_default) | |
1101 | cg->lang_default = cupsLangDefault(); | |
1102 | ||
1103 | cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default, | |
1104 | message)); | |
1105 | } | |
1106 | else | |
1107 | cg->last_status_message = _cupsStrAlloc(message); | |
1108 | } | |
a603edef | 1109 | |
e07d4801 MS |
1110 | DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"", |
1111 | ippErrorString(cg->last_error), cg->last_status_message)); | |
ecdc0628 | 1112 | } |
1113 | ||
1114 | ||
1115 | /* | |
355e94dc MS |
1116 | * '_cupsSetHTTPError()' - Set the last error using the HTTP status. |
1117 | */ | |
1118 | ||
1119 | void | |
1120 | _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */ | |
1121 | { | |
1122 | switch (status) | |
1123 | { | |
cb7f98ee MS |
1124 | case HTTP_STATUS_NOT_FOUND : |
1125 | _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, httpStatus(status), 0); | |
355e94dc MS |
1126 | break; |
1127 | ||
cb7f98ee MS |
1128 | case HTTP_STATUS_UNAUTHORIZED : |
1129 | _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED, httpStatus(status), 0); | |
355e94dc MS |
1130 | break; |
1131 | ||
cb7f98ee MS |
1132 | case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED : |
1133 | _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED, httpStatus(status), 0); | |
7cf5915e MS |
1134 | break; |
1135 | ||
cb7f98ee MS |
1136 | case HTTP_STATUS_FORBIDDEN : |
1137 | _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN, httpStatus(status), 0); | |
355e94dc MS |
1138 | break; |
1139 | ||
cb7f98ee MS |
1140 | case HTTP_STATUS_BAD_REQUEST : |
1141 | _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST, httpStatus(status), 0); | |
355e94dc MS |
1142 | break; |
1143 | ||
cb7f98ee MS |
1144 | case HTTP_STATUS_REQUEST_TOO_LARGE : |
1145 | _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE, httpStatus(status), 0); | |
355e94dc MS |
1146 | break; |
1147 | ||
cb7f98ee MS |
1148 | case HTTP_STATUS_NOT_IMPLEMENTED : |
1149 | _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, httpStatus(status), 0); | |
355e94dc MS |
1150 | break; |
1151 | ||
cb7f98ee MS |
1152 | case HTTP_STATUS_NOT_SUPPORTED : |
1153 | _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, httpStatus(status), 0); | |
355e94dc MS |
1154 | break; |
1155 | ||
cb7f98ee MS |
1156 | case HTTP_STATUS_UPGRADE_REQUIRED : |
1157 | _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED, httpStatus(status), 0); | |
7cf5915e MS |
1158 | break; |
1159 | ||
cb7f98ee MS |
1160 | case HTTP_STATUS_CUPS_PKI_ERROR : |
1161 | _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, httpStatus(status), 0); | |
7cf5915e MS |
1162 | break; |
1163 | ||
cb7f98ee MS |
1164 | case HTTP_STATUS_ERROR : |
1165 | _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0); | |
10d09e33 MS |
1166 | break; |
1167 | ||
355e94dc | 1168 | default : |
e07d4801 | 1169 | DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to " |
cb7f98ee MS |
1170 | "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status)); |
1171 | _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, httpStatus(status), 0); | |
355e94dc MS |
1172 | break; |
1173 | } | |
1174 | } |