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