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