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