]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/request.c
Merge changes from CUPS 1.4svn-r8492.
[thirdparty/cups.git] / cups / request.c
1 /*
2 * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
3 *
4 * IPP utilities for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2009 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 * cupsReadResponseData() - Read additional data after the IPP response.
24 * cupsSendRequest() - Send an IPP request.
25 * cupsWriteRequestData() - Write additional data after an IPP request.
26 * _cupsSetError() - Set the last IPP status code and status-message.
27 * _cupsSetHTTPError() - Set the last error using the HTTP status.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include "globals.h"
35 #include "debug.h"
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #if defined(WIN32) || defined(__EMX__)
41 # include <io.h>
42 #else
43 # include <unistd.h>
44 #endif /* WIN32 || __EMX__ */
45 #ifndef O_BINARY
46 # define O_BINARY 0
47 #endif /* O_BINARY */
48
49
50 /*
51 * 'cupsDoFileRequest()' - Do an IPP request with a file.
52 *
53 * This function sends the IPP request to the specified server, retrying
54 * and authenticating as necessary. The request is freed with @link ippDelete@
55 * after receiving a valid IPP response.
56 */
57
58 ipp_t * /* O - Response data */
59 cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
60 ipp_t *request, /* I - IPP request */
61 const char *resource, /* I - HTTP resource for POST */
62 const char *filename) /* I - File to send or @code NULL@ for none */
63 {
64 ipp_t *response; /* IPP response data */
65 int infile; /* Input file */
66
67
68 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
69 "filename=\"%s\")\n", http, request,
70 request ? ippOpString(request->request.op.operation_id) : "?",
71 resource ? resource : "(null)",
72 filename ? filename : "(null)"));
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)\n", http, request,
135 request ? ippOpString(request->request.op.operation_id) : "?",
136 resource ? resource : "(null)", 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 return (NULL);
158
159 /*
160 * See if we have a file to send...
161 */
162
163 if (infile >= 0)
164 {
165 if (fstat(infile, &fileinfo))
166 {
167 /*
168 * Can't get file information!
169 */
170
171 _cupsSetError(errno == EBADF ? IPP_NOT_FOUND : IPP_NOT_AUTHORIZED,
172 NULL, 0);
173
174 ippDelete(request);
175
176 return (NULL);
177 }
178
179 #ifdef WIN32
180 if (fileinfo.st_mode & _S_IFDIR)
181 #else
182 if (S_ISDIR(fileinfo.st_mode))
183 #endif /* WIN32 */
184 {
185 /*
186 * Can't send a directory...
187 */
188
189 ippDelete(request);
190
191 _cupsSetError(IPP_NOT_POSSIBLE, strerror(EISDIR), 0);
192
193 return (NULL);
194 }
195
196 #ifndef WIN32
197 if (!S_ISREG(fileinfo.st_mode))
198 length = 0; /* Chunk when piping */
199 else
200 #endif /* !WIN32 */
201 length = ippLength(request) + fileinfo.st_size;
202 }
203 else
204 length = ippLength(request);
205
206 DEBUG_printf(("cupsDoIORequest: Request length=%ld, total length=%ld",
207 (long)ippLength(request), (long)length));
208
209 /*
210 * Clear any "Local" authentication data since it is probably stale...
211 */
212
213 if (http->authstring && !strncmp(http->authstring, "Local ", 6))
214 httpSetAuthString(http, NULL, NULL);
215
216 /*
217 * Loop until we can send the request without authorization problems.
218 */
219
220 while (response == NULL)
221 {
222 DEBUG_puts("cupsDoIORequest: setup...");
223
224 /*
225 * Send the request...
226 */
227
228 status = cupsSendRequest(http, request, resource, length);
229
230 DEBUG_printf(("cupsDoIORequest: status=%d\n", status));
231
232 if (status == HTTP_CONTINUE && request->state == IPP_DATA && infile >= 0)
233 {
234 DEBUG_puts("cupsDoIORequest: file write...");
235
236 /*
237 * Send the file with the request...
238 */
239
240 #ifndef WIN32
241 if (S_ISREG(fileinfo.st_mode))
242 #endif /* WIN32 */
243 lseek(infile, 0, SEEK_SET);
244
245 while ((bytes = (int)read(infile, buffer, sizeof(buffer))) > 0)
246 {
247 if (httpCheck(http))
248 {
249 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
250 break;
251 }
252
253 if (httpWrite2(http, buffer, bytes) < bytes)
254 break;
255 }
256 }
257
258 /*
259 * Get the server's response...
260 */
261
262 if (status == HTTP_CONTINUE || status == HTTP_OK)
263 {
264 response = cupsGetResponse(http, resource);
265 status = http->status;
266 }
267
268 if (status == HTTP_FORBIDDEN || status == HTTP_ERROR ||
269 status >= HTTP_SERVER_ERROR)
270 {
271 _cupsSetHTTPError(status);
272 break;
273 }
274
275 if (response)
276 {
277 if (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 else
288 {
289 /*
290 * Flush any remaining data...
291 */
292
293 httpFlush(http);
294 }
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\")\n",
322 http, request,
323 request ? ippOpString(request->request.op.operation_id) : "?",
324 resource ? resource : "(null)"));
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 *
337 * @since CUPS 1.4@
338 */
339
340 ipp_t * /* O - Response or @code NULL@ on HTTP error */
341 cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
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
349 DEBUG_printf(("cupsGetReponse(http=%p, resource=\"%s\")\n", http,
350 resource ? resource : "(null)"));
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("cupsGetResponse: 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(("cupsGetResponse: Update loop, http->status=%d...\n",
383 http->status));
384
385 status = http->status;
386 while (status == HTTP_CONTINUE)
387 status = httpUpdate(http);
388
389 DEBUG_printf(("cupsGetResponse: status=%d\n", status));
390
391 if (status == HTTP_OK)
392 {
393 /*
394 * Get the IPP response...
395 */
396
397 response = ippNew();
398
399 while ((state = ippRead(http, response)) != IPP_DATA)
400 if (state == IPP_ERROR)
401 break;
402
403 if (state == IPP_ERROR)
404 {
405 /*
406 * Delete the response...
407 */
408
409 DEBUG_puts("cupsGetResponse: IPP read error!");
410
411 ippDelete(response);
412 response = NULL;
413
414 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
415 }
416 }
417 else if (status != HTTP_ERROR)
418 {
419 /*
420 * Flush any error message...
421 */
422
423 httpFlush(http);
424
425 /*
426 * Then handle encryption and authentication...
427 */
428
429 if (status == HTTP_UNAUTHORIZED)
430 {
431 /*
432 * See if we can do authentication...
433 */
434
435 int auth_result;
436
437 DEBUG_puts("cupsGetResponse: Need authorization...");
438
439 if ((auth_result = cupsDoAuthentication(http, "POST", resource)) == 0)
440 httpReconnect(http);
441 else if (auth_result < 0)
442 http->status = status = HTTP_FORBIDDEN;
443 }
444
445 #ifdef HAVE_SSL
446 else if (status == HTTP_UPGRADE_REQUIRED)
447 {
448 /*
449 * Force a reconnect with encryption...
450 */
451
452 DEBUG_puts("cupsGetResponse: Need encryption...");
453
454 if (!httpReconnect(http))
455 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
456 }
457 #endif /* HAVE_SSL */
458 }
459
460 if (response)
461 {
462 ipp_attribute_t *attr; /* status-message attribute */
463
464
465 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
466
467 DEBUG_printf(("cupsGetResponse: status-code=%s, status-message=\"%s\"\n",
468 ippErrorString(response->request.status.status_code),
469 attr ? attr->values[0].string.text : ""));
470
471 _cupsSetError(response->request.status.status_code,
472 attr ? attr->values[0].string.text :
473 ippErrorString(response->request.status.status_code), 0);
474 }
475 else if (status != HTTP_OK)
476 _cupsSetHTTPError(status);
477
478 return (response);
479 }
480
481
482 /*
483 * 'cupsReadResponseData()' - Read additional data after the IPP response.
484 *
485 * This function is used after cupsGetResponse() to read the PPD or document
486 * files for CUPS_GET_PPD and CUPS_GET_DOCUMENT requests, respectively.
487 *
488 * @since CUPS 1.4@
489 */
490
491 ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
492 cupsReadResponseData(
493 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
494 char *buffer, /* I - Buffer to use */
495 size_t length) /* I - Number of bytes to read */
496 {
497 /*
498 * Get the default connection as needed...
499 */
500
501 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
502 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
503
504 if (!http)
505 {
506 _cups_globals_t *cg = _cupsGlobals();
507 /* Pointer to library globals */
508
509 if ((http = cg->http) == NULL)
510 {
511 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
512 return (-1);
513 }
514 }
515
516 /*
517 * Then read from the HTTP connection...
518 */
519
520 return (httpRead2(http, buffer, length));
521 }
522
523
524 /*
525 * 'cupsSendRequest()' - Send an IPP request.
526 *
527 * Use httpWrite() to write any additional data (document, PPD file, etc.)
528 * for the request, cupsGetResponse() to get the IPP response, and httpRead()
529 * to read any additional data following the response. Only one request can be
530 * sent/queued at a time.
531 *
532 * Unlike cupsDoFileRequest(), cupsDoIORequest(), and cupsDoRequest(), the
533 * request is not freed.
534 *
535 * @since CUPS 1.4@
536 */
537
538 http_status_t /* O - Initial HTTP status */
539 cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
540 ipp_t *request, /* I - IPP request */
541 const char *resource, /* I - Resource path */
542 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
543 {
544 http_status_t status; /* Status of HTTP request */
545 int got_status; /* Did we get the status? */
546 ipp_state_t state; /* State of IPP processing */
547 http_status_t expect; /* Expect: header to use */
548
549
550 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
551 "length=" CUPS_LLFMT ")\n", http, request,
552 request ? ippOpString(request->request.op.operation_id) : "?",
553 resource ? resource : "(null)", CUPS_LLCAST length));
554
555 /*
556 * Range check input...
557 */
558
559 if (!request || !resource)
560 {
561 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
562
563 return (HTTP_ERROR);
564 }
565
566 /*
567 * Get the default connection as needed...
568 */
569
570 if (!http)
571 if ((http = _cupsConnect()) == NULL)
572 return (HTTP_SERVICE_UNAVAILABLE);
573
574 #ifdef HAVE_SSL
575 /*
576 * See if we have an auth-info attribute and are communicating over
577 * a non-local link. If so, encrypt the link so that we can pass
578 * the authentication information securely...
579 */
580
581 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
582 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
583 httpEncryption(http, HTTP_ENCRYPT_REQUIRED))
584 {
585 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
586 return (HTTP_SERVICE_UNAVAILABLE);
587 }
588 #endif /* HAVE_SSL */
589
590 /*
591 * Reconnect if the last response had a "Connection: close"...
592 */
593
594 if (!strcasecmp(http->fields[HTTP_FIELD_CONNECTION], "close"))
595 if (httpReconnect(http))
596 {
597 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
598 return (HTTP_SERVICE_UNAVAILABLE);
599 }
600
601 /*
602 * Loop until we can send the request without authorization problems.
603 */
604
605 expect = HTTP_CONTINUE;
606
607 for (;;)
608 {
609 DEBUG_puts("cupsSendRequest: Setup...");
610
611 /*
612 * Setup the HTTP variables needed...
613 */
614
615 httpClearFields(http);
616 httpSetLength(http, length);
617 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
618 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
619 httpSetExpect(http, expect);
620
621 DEBUG_printf(("cupsSendRequest: authstring=\"%s\"\n", http->authstring));
622
623 /*
624 * Try the request...
625 */
626
627 DEBUG_puts("cupsSendRequest: Sending HTTP POST...");
628
629 if (httpPost(http, resource))
630 {
631 if (httpReconnect(http))
632 {
633 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
634 return (HTTP_SERVICE_UNAVAILABLE);
635 }
636 else
637 continue;
638 }
639
640 /*
641 * Send the IPP data...
642 */
643
644 DEBUG_puts("cupsSendRequest: Writing IPP request...");
645
646 request->state = IPP_IDLE;
647 status = HTTP_CONTINUE;
648 got_status = 0;
649
650 while ((state = ippWrite(http, request)) != IPP_DATA)
651 if (state == IPP_ERROR)
652 break;
653 else if (httpCheck(http))
654 {
655 got_status = 1;
656
657 if ((status = httpUpdate(http)) != HTTP_CONTINUE)
658 break;
659 }
660
661 /*
662 * Wait up to 1 second to get the 100-continue response as needed...
663 */
664
665 if (!got_status && expect == HTTP_CONTINUE)
666 {
667 DEBUG_puts("cupsSendRequest: Waiting for 100-continue...");
668
669 if (httpWait(http, 1000))
670 status = httpUpdate(http);
671 }
672 else if (httpCheck(http))
673 status = httpUpdate(http);
674
675 DEBUG_printf(("cupsSendRequest: status=%d\n", status));
676
677 /*
678 * Process the current HTTP status...
679 */
680
681 switch (status)
682 {
683 case HTTP_ERROR :
684 case HTTP_CONTINUE :
685 case HTTP_OK :
686 return (status);
687
688 case HTTP_UNAUTHORIZED :
689 if (!cupsDoAuthentication(http, "POST", resource))
690 if (httpReconnect(http))
691 {
692 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
693 return (HTTP_SERVICE_UNAVAILABLE);
694 }
695
696 return (status);
697
698 #ifdef HAVE_SSL
699 case HTTP_UPGRADE_REQUIRED :
700 /*
701 * Flush any error message, reconnect, and then upgrade with
702 * encryption...
703 */
704
705 if (httpReconnect(http))
706 {
707 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
708 return (HTTP_SERVICE_UNAVAILABLE);
709 }
710
711 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
712
713 return (status);
714 #endif /* HAVE_SSL */
715
716 case HTTP_EXPECTATION_FAILED :
717 /*
718 * Don't try using the Expect: header the next time around...
719 */
720
721 expect = (http_status_t)0;
722
723 if (httpReconnect(http))
724 {
725 _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0);
726 return (HTTP_SERVICE_UNAVAILABLE);
727 }
728 break;
729
730 default :
731 /*
732 * Some other error...
733 */
734
735 return (status);
736 }
737 }
738 }
739
740
741 /*
742 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
743 *
744 * This function is used after @link cupsSendRequest@ to provide a PPD and
745 * after @link cupsStartDocument@ to provide a document file.
746 *
747 * @since CUPS 1.4@
748 */
749
750 http_status_t /* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
751 cupsWriteRequestData(
752 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
753 const char *buffer, /* I - Bytes to write */
754 size_t length) /* I - Number of bytes to write */
755 {
756 int wused; /* Previous bytes in buffer */
757
758
759 /*
760 * Get the default connection as needed...
761 */
762
763 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
764 "length=" CUPS_LLFMT ")\n", http, buffer, CUPS_LLCAST length));
765
766 if (!http)
767 {
768 _cups_globals_t *cg = _cupsGlobals();
769 /* Pointer to library globals */
770
771 if ((http = cg->http) == NULL)
772 {
773 _cupsSetError(IPP_INTERNAL_ERROR, _("No active connection"), 1);
774 return (HTTP_ERROR);
775 }
776 }
777
778 /*
779 * Then write to the HTTP connection...
780 */
781
782 wused = http->wused;
783
784 if (httpWrite2(http, buffer, length) < 0)
785 return (HTTP_ERROR);
786
787 /*
788 * Finally, check if we have any pending data from the server...
789 */
790
791 if (length > HTTP_MAX_BUFFER ||
792 http->wused < wused ||
793 (wused > 0 && http->wused == length))
794 {
795 /*
796 * We've written something to the server, so check for response data...
797 */
798
799 if (_httpWait(http, 0, 1))
800 return (httpUpdate(http));
801 }
802
803 return (HTTP_CONTINUE);
804 }
805
806
807 /*
808 * '_cupsSetError()' - Set the last IPP status code and status-message.
809 */
810
811 void
812 _cupsSetError(ipp_status_t status, /* I - IPP status code */
813 const char *message, /* I - status-message value */
814 int localize) /* I - Localize the message? */
815 {
816 _cups_globals_t *cg; /* Global data */
817
818
819 if (!message && errno)
820 {
821 message = strerror(errno);
822 localize = 0;
823 }
824
825 cg = _cupsGlobals();
826 cg->last_error = status;
827
828 if (cg->last_status_message)
829 {
830 _cupsStrFree(cg->last_status_message);
831
832 cg->last_status_message = NULL;
833 }
834
835 if (message)
836 {
837 if (localize)
838 {
839 /*
840 * Get the message catalog...
841 */
842
843 if (!cg->lang_default)
844 cg->lang_default = cupsLangDefault();
845
846 cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default,
847 message));
848 }
849 else
850 cg->last_status_message = _cupsStrAlloc(message);
851 }
852
853 DEBUG_printf(("_cupsSetError: last_error=%s, last_status_message=\"%s\"\n",
854 ippErrorString(cg->last_error),
855 cg->last_status_message ? cg->last_status_message : ""));
856 }
857
858
859 /*
860 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
861 */
862
863 void
864 _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
865 {
866 switch (status)
867 {
868 case HTTP_NOT_FOUND :
869 _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0);
870 break;
871
872 case HTTP_UNAUTHORIZED :
873 _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0);
874 break;
875
876 case HTTP_FORBIDDEN :
877 _cupsSetError(IPP_FORBIDDEN, httpStatus(status), 0);
878 break;
879
880 case HTTP_BAD_REQUEST :
881 _cupsSetError(IPP_BAD_REQUEST, httpStatus(status), 0);
882 break;
883
884 case HTTP_REQUEST_TOO_LARGE :
885 _cupsSetError(IPP_REQUEST_VALUE, httpStatus(status), 0);
886 break;
887
888 case HTTP_NOT_IMPLEMENTED :
889 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
890 break;
891
892 case HTTP_NOT_SUPPORTED :
893 _cupsSetError(IPP_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
894 break;
895
896 default :
897 DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
898 status));
899 _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0);
900 break;
901 }
902 }
903
904
905 /*
906 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".
907 */