2 * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
4 * IPP utilities for CUPS.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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.
35 * Include necessary headers...
38 #include "cups-private.h"
41 #if defined(WIN32) || defined(__EMX__)
45 #endif /* WIN32 || __EMX__ */
52 * 'cupsDoFileRequest()' - Do an IPP request with a file.
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
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 */
65 ipp_t
*response
; /* IPP response data */
66 int infile
; /* Input file */
69 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
70 "filename=\"%s\")", http
, request
,
71 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
76 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
79 * Can't get file information!
82 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
93 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
103 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
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@.
109 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
110 * all of the data from the file after the IPP request message.
112 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
113 * all of the data after the IPP response message to the file.
115 * @since CUPS 1.3/OS X 10.5@
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 */
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 */
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
));
139 * Range check input...
142 if (!request
|| !resource
)
146 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
152 * Get the default connection as needed...
156 if ((http
= _cupsConnect()) == NULL
)
164 * See if we have a file to send...
169 if (fstat(infile
, &fileinfo
))
172 * Can't get file information!
175 _cupsSetError(errno
== EBADF
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
184 if (fileinfo
.st_mode
& _S_IFDIR
)
186 if (S_ISDIR(fileinfo
.st_mode
))
190 * Can't send a directory...
195 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
), 0);
201 if (!S_ISREG(fileinfo
.st_mode
))
202 length
= 0; /* Chunk when piping */
205 length
= ippLength(request
) + fileinfo
.st_size
;
208 length
= ippLength(request
);
210 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
211 (long)ippLength(request
), (long)length
));
214 * Clear any "Local" authentication data since it is probably stale...
217 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
218 httpSetAuthString(http
, NULL
, NULL
);
221 * Loop until we can send the request without authorization problems.
224 while (response
== NULL
)
226 DEBUG_puts("2cupsDoIORequest: setup...");
229 * Send the request...
232 status
= cupsSendRequest(http
, request
, resource
, length
);
234 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
236 if (status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&& infile
>= 0)
238 DEBUG_puts("2cupsDoIORequest: file write...");
241 * Send the file with the request...
245 if (S_ISREG(fileinfo
.st_mode
))
247 lseek(infile
, 0, SEEK_SET
);
249 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
251 if ((status
= cupsWriteRequestData(http
, buffer
, bytes
))
258 * Get the server's response...
261 if (status
!= HTTP_ERROR
)
263 response
= cupsGetResponse(http
, resource
);
264 status
= httpGetStatus(http
);
267 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
269 if (status
== HTTP_ERROR
||
270 (status
>= HTTP_BAD_REQUEST
&& status
!= HTTP_UNAUTHORIZED
&&
271 status
!= HTTP_UPGRADE_REQUIRED
))
273 _cupsSetHTTPError(status
);
277 if (response
&& outfile
>= 0)
280 * Write trailing data to file...
283 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
284 if (write(outfile
, buffer
, bytes
) < bytes
)
288 if (http
->state
!= HTTP_WAITING
)
291 * Flush any remaining data...
299 * Delete the original request and return the response...
309 * 'cupsDoRequest()' - Do an IPP request.
311 * This function sends the IPP request to the specified server, retrying
312 * and authenticating as necessary. The request is freed with @link ippDelete@.
315 ipp_t
* /* O - Response data */
316 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
317 ipp_t
*request
, /* I - IPP request */
318 const char *resource
) /* I - HTTP resource for POST */
320 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
322 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
325 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
330 * 'cupsGetResponse()' - Get a response to an IPP request.
332 * Use this function to get the response for an IPP request sent using
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.
337 * @since CUPS 1.4/OS X 10.6@
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 */
344 http_status_t status
; /* HTTP status */
345 ipp_state_t state
; /* IPP read state */
346 ipp_t
*response
= NULL
; /* IPP response */
349 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http
, resource
));
352 * Connect to the default server as needed...
356 http
= _cupsConnect();
358 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
362 * Check for an unfinished chunked request...
365 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
368 * Send a 0-length chunk to finish off the request...
371 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
373 if (httpWrite2(http
, "", 0) < 0)
378 * Wait for a response from the server...
381 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
386 status
= httpUpdate(http
);
388 while (status
== HTTP_STATUS_CONTINUE
);
390 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
392 if (status
== HTTP_OK
)
395 * Get the IPP response...
400 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
401 if (state
== IPP_ERROR
)
404 if (state
== IPP_ERROR
)
407 * Flush remaining data and delete the response...
410 DEBUG_puts("1cupsGetResponse: IPP read error!");
417 http
->status
= status
= HTTP_ERROR
;
418 http
->error
= EINVAL
;
421 else if (status
!= HTTP_ERROR
)
424 * Flush any error message...
430 * Then handle encryption and authentication...
433 if (status
== HTTP_UNAUTHORIZED
)
436 * See if we can do authentication...
439 DEBUG_puts("2cupsGetResponse: Need authorization...");
441 if (!cupsDoAuthentication(http
, "POST", resource
))
444 http
->status
= status
= HTTP_AUTHORIZATION_CANCELED
;
448 else if (status
== HTTP_UPGRADE_REQUIRED
)
451 * Force a reconnect with encryption...
454 DEBUG_puts("2cupsGetResponse: Need encryption...");
456 if (!httpReconnect(http
))
457 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
459 #endif /* HAVE_SSL */
464 ipp_attribute_t
*attr
; /* status-message attribute */
467 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
469 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
470 ippErrorString(response
->request
.status
.status_code
),
471 attr
? attr
->values
[0].string
.text
: ""));
473 _cupsSetError(response
->request
.status
.status_code
,
474 attr
? attr
->values
[0].string
.text
:
475 ippErrorString(response
->request
.status
.status_code
), 0);
483 * 'cupsLastError()' - Return the last IPP status code received on the current
487 ipp_status_t
/* O - IPP status code from last request */
490 return (_cupsGlobals()->last_error
);
495 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
498 * @since CUPS 1.2/OS X 10.5@
501 const char * /* O - status-message text from last request */
502 cupsLastErrorString(void)
504 return (_cupsGlobals()->last_status_message
);
509 * '_cupsNextDelay()' - Return the next retry delay value.
511 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
513 * Pass 0 for the current delay value to initialize the sequence.
516 int /* O - Next delay value */
517 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
518 int *previous
) /* IO - Previous delay value */
520 int next
; /* Next delay value */
525 next
= (current
+ *previous
) % 12;
526 *previous
= next
< current
? 0 : current
;
539 * 'cupsReadResponseData()' - Read additional data after the IPP response.
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,
545 * @since CUPS 1.4/OS X 10.6@
548 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
549 cupsReadResponseData(
550 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
551 char *buffer
, /* I - Buffer to use */
552 size_t length
) /* I - Number of bytes to read */
555 * Get the default connection as needed...
558 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
559 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
563 _cups_globals_t
*cg
= _cupsGlobals();
564 /* Pointer to library globals */
566 if ((http
= cg
->http
) == NULL
)
568 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
574 * Then read from the HTTP connection...
577 return (httpRead2(http
, buffer
, length
));
582 * 'cupsSendRequest()' - Send an IPP request.
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@
590 * Returns the initial HTTP status code, which will be @code HTTP_CONTINUE@
591 * on a successful send of the request.
593 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
594 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
596 * @since CUPS 1.4/OS X 10.6@
599 http_status_t
/* O - Initial HTTP status */
600 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
601 ipp_t
*request
, /* I - IPP request */
602 const char *resource
, /* I - Resource path */
603 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
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 */
611 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
612 "length=" CUPS_LLFMT
")", http
, request
,
613 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
614 resource
, CUPS_LLCAST length
));
617 * Range check input...
620 if (!request
|| !resource
)
622 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
628 * Get the default connection as needed...
632 if ((http
= _cupsConnect()) == NULL
)
633 return (HTTP_SERVICE_UNAVAILABLE
);
636 * If the prior request was not flushed out, do so now...
639 if (http
->state
== HTTP_GET_SEND
||
640 http
->state
== HTTP_POST_SEND
)
642 DEBUG_puts("2cupsSendRequest: Flush prior response.");
645 else if (http
->state
!= HTTP_WAITING
)
647 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
648 "reconnecting.", http
->state
));
649 if (httpReconnect(http
))
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...
660 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
661 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
662 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
664 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
665 return (HTTP_SERVICE_UNAVAILABLE
);
667 #endif /* HAVE_SSL */
670 * Reconnect if the last response had a "Connection: close"...
673 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
675 DEBUG_puts("2cupsSendRequest: Connection: close");
676 httpClearFields(http
);
677 if (httpReconnect(http
))
679 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
680 return (HTTP_SERVICE_UNAVAILABLE
);
685 * Loop until we can send the request without authorization problems.
688 expect
= HTTP_CONTINUE
;
692 DEBUG_puts("2cupsSendRequest: Setup...");
695 * Setup the HTTP variables needed...
698 httpClearFields(http
);
699 httpSetExpect(http
, expect
);
700 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
701 httpSetLength(http
, length
);
704 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
707 * Do not use cached Kerberos credentials since they will look like a
711 _cupsSetNegotiateAuthString(http
, "POST", resource
);
713 #endif /* HAVE_GSSAPI */
715 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
717 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
723 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
725 if (httpPost(http
, resource
))
727 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
728 if (httpReconnect(http
))
730 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
731 return (HTTP_SERVICE_UNAVAILABLE
);
738 * Send the IPP data...
741 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
743 request
->state
= IPP_IDLE
;
744 status
= HTTP_CONTINUE
;
747 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
748 if (state
== IPP_ERROR
)
750 else if (httpCheck(http
))
754 _httpUpdate(http
, &status
);
755 if (status
>= HTTP_MULTIPLE_CHOICES
)
759 if (state
== IPP_ERROR
)
761 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
763 http
->status
= HTTP_ERROR
;
764 http
->state
= HTTP_WAITING
;
770 * Wait up to 1 second to get the 100-continue response as needed...
775 if (expect
== HTTP_CONTINUE
)
777 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
779 if (httpWait(http
, 1000))
780 _httpUpdate(http
, &status
);
782 else if (httpCheck(http
))
783 _httpUpdate(http
, &status
);
786 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
789 * Process the current HTTP status...
792 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
794 int temp_status
; /* Temporary status */
796 _cupsSetHTTPError(status
);
800 temp_status
= httpUpdate(http
);
802 while (temp_status
!= HTTP_STATUS_ERROR
&&
803 http
->state
== HTTP_STATE_POST_RECV
);
810 case HTTP_STATUS_CONTINUE
:
811 case HTTP_STATUS_OK
:
812 case HTTP_STATUS_ERROR
:
813 DEBUG_printf(("1cupsSendRequest: Returning %d.", status
));
816 case HTTP_STATUS_UNAUTHORIZED
:
817 if (cupsDoAuthentication(http
, "POST", resource
))
819 DEBUG_puts("1cupsSendRequest: Returning HTTP_AUTHORIZATION_CANCELED.");
820 return (HTTP_AUTHORIZATION_CANCELED
);
823 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_UNAUTHORIZED.");
825 if (httpReconnect(http
))
827 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
828 return (HTTP_SERVICE_UNAVAILABLE
);
833 case HTTP_STATUS_UPGRADE_REQUIRED
:
835 * Flush any error message, reconnect, and then upgrade with
839 DEBUG_puts("2cupsSendRequest: Reconnecting after "
840 "HTTP_UPGRADE_REQUIRED.");
842 if (httpReconnect(http
))
844 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
845 return (HTTP_SERVICE_UNAVAILABLE
);
848 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
849 if (httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
851 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
852 return (HTTP_SERVICE_UNAVAILABLE
);
855 #endif /* HAVE_SSL */
857 case HTTP_STATUS_EXPECTATION_FAILED
:
859 * Don't try using the Expect: header the next time around...
862 expect
= (http_status_t
)0;
864 DEBUG_puts("2cupsSendRequest: Reconnecting after "
865 "HTTP_EXPECTATION_FAILED.");
867 if (httpReconnect(http
))
869 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
870 return (HTTP_SERVICE_UNAVAILABLE
);
876 * Some other error...
886 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
888 * This function is used after @link cupsSendRequest@ to provide a PPD and
889 * after @link cupsStartDocument@ to provide a document file.
891 * @since CUPS 1.4/OS X 10.6@
894 http_status_t
/* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
895 cupsWriteRequestData(
896 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
897 const char *buffer
, /* I - Bytes to write */
898 size_t length
) /* I - Number of bytes to write */
900 int wused
; /* Previous bytes in buffer */
904 * Get the default connection as needed...
907 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
908 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
912 _cups_globals_t
*cg
= _cupsGlobals();
913 /* Pointer to library globals */
915 if ((http
= cg
->http
) == NULL
)
917 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
918 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
924 * Then write to the HTTP connection...
929 if (httpWrite2(http
, buffer
, length
) < 0)
931 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
932 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(http
->error
), 0);
937 * Finally, check if we have any pending data from the server...
940 if (length
>= HTTP_MAX_BUFFER
||
941 http
->wused
< wused
||
942 (wused
> 0 && http
->wused
== length
))
945 * We've written something to the server, so check for response data...
948 if (_httpWait(http
, 0, 1))
950 http_status_t status
; /* Status from _httpUpdate */
952 _httpUpdate(http
, &status
);
953 if (status
>= HTTP_MULTIPLE_CHOICES
)
955 _cupsSetHTTPError(status
);
959 status
= httpUpdate(http
);
961 while (status
!= HTTP_ERROR
&& http
->state
== HTTP_POST_RECV
);
966 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
971 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_CONTINUE.");
972 return (HTTP_CONTINUE
);
977 * '_cupsConnect()' - Get the default server connection...
980 http_t
* /* O - HTTP connection */
983 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
987 * See if we are connected to the same server...
993 * Compare the connection hostname, port, and encryption settings to
994 * the cached defaults; these were initialized the first time we
998 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
999 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
1000 (cg
->http
->encryption
!= cg
->encryption
&&
1001 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
1004 * Need to close the current connection because something has changed...
1007 httpClose(cg
->http
);
1013 * Same server, see if the connection is still established...
1016 char ch
; /* Connection check byte */
1018 if (recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
) < 0 &&
1019 errno
!= EWOULDBLOCK
)
1022 * Nope, close the connection...
1025 httpClose(cg
->http
);
1032 * (Re)connect as needed...
1037 if ((cg
->http
= httpConnectEncrypt(cupsServer(), ippPort(),
1038 cupsEncryption())) == NULL
)
1041 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
1043 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
1044 _("Unable to connect to host."), 1);
1049 * Return the cached connection...
1057 * '_cupsSetError()' - Set the last IPP status code and status-message.
1061 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
1062 const char *message
, /* I - status-message value */
1063 int localize
) /* I - Localize the message? */
1065 _cups_globals_t
*cg
; /* Global data */
1068 if (!message
&& errno
)
1070 message
= strerror(errno
);
1074 cg
= _cupsGlobals();
1075 cg
->last_error
= status
;
1077 if (cg
->last_status_message
)
1079 _cupsStrFree(cg
->last_status_message
);
1081 cg
->last_status_message
= NULL
;
1089 * Get the message catalog...
1092 if (!cg
->lang_default
)
1093 cg
->lang_default
= cupsLangDefault();
1095 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
1099 cg
->last_status_message
= _cupsStrAlloc(message
);
1102 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1103 ippErrorString(cg
->last_error
), cg
->last_status_message
));
1108 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1112 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
1116 case HTTP_NOT_FOUND
:
1117 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
), 0);
1120 case HTTP_UNAUTHORIZED
:
1121 _cupsSetError(IPP_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1124 case HTTP_AUTHORIZATION_CANCELED
:
1125 _cupsSetError(IPP_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1128 case HTTP_FORBIDDEN
:
1129 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
), 0);
1132 case HTTP_BAD_REQUEST
:
1133 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
), 0);
1136 case HTTP_REQUEST_TOO_LARGE
:
1137 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
), 0);
1140 case HTTP_NOT_IMPLEMENTED
:
1141 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1144 case HTTP_NOT_SUPPORTED
:
1145 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1148 case HTTP_UPGRADE_REQUIRED
:
1149 _cupsSetError(IPP_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1152 case HTTP_PKI_ERROR
:
1153 _cupsSetError(IPP_PKI_ERROR
, httpStatus(status
), 0);
1157 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
1161 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1162 "IPP_SERVICE_UNAVAILABLE!", status
));
1163 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);
1170 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".