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