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