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