2 * "$Id: request.c 7946 2008-09-16 23:27:54Z mike $"
4 * IPP utilities for CUPS.
6 * Copyright 2007-2013 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__ */
50 # define MSG_DONTWAIT 0
51 #endif /* !MSG_DONTWAIT */
55 * 'cupsDoFileRequest()' - Do an IPP request with a file.
57 * This function sends the IPP request and attached file to the specified
58 * server, retrying and authenticating as necessary. The request is freed with
62 ipp_t
* /* O - Response data */
63 cupsDoFileRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
64 ipp_t
*request
, /* I - IPP request */
65 const char *resource
, /* I - HTTP resource for POST */
66 const char *filename
) /* I - File to send or @code NULL@ for none */
68 ipp_t
*response
; /* IPP response data */
69 int infile
; /* Input file */
72 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
73 "filename=\"%s\")", http
, request
,
74 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
79 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
82 * Can't get file information!
85 _cupsSetError(errno
== ENOENT
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
96 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
106 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
108 * This function sends the IPP request with the optional input file "infile" to
109 * the specified server, retrying and authenticating as necessary. The request
110 * is freed with @link ippDelete@.
112 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
113 * all of the data from the file after the IPP request message.
115 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
116 * all of the data after the IPP response message to the file.
118 * @since CUPS 1.3/OS X 10.5@
121 ipp_t
* /* O - Response data */
122 cupsDoIORequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
123 ipp_t
*request
, /* I - IPP request */
124 const char *resource
, /* I - HTTP resource for POST */
125 int infile
, /* I - File to read from or -1 for none */
126 int outfile
) /* I - File to write to or -1 for none */
128 ipp_t
*response
= NULL
; /* IPP response data */
129 size_t length
= 0; /* Content-Length value */
130 http_status_t status
; /* Status of HTTP request */
131 struct stat fileinfo
; /* File information */
132 int bytes
; /* Number of bytes read/written */
133 char buffer
[32768]; /* Output buffer */
136 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
137 "infile=%d, outfile=%d)", http
, request
,
138 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
139 resource
, infile
, outfile
));
142 * Range check input...
145 if (!request
|| !resource
)
149 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
155 * Get the default connection as needed...
159 if ((http
= _cupsConnect()) == NULL
)
167 * See if we have a file to send...
172 if (fstat(infile
, &fileinfo
))
175 * Can't get file information!
178 _cupsSetError(errno
== EBADF
? IPP_NOT_FOUND
: IPP_NOT_AUTHORIZED
,
187 if (fileinfo
.st_mode
& _S_IFDIR
)
189 if (S_ISDIR(fileinfo
.st_mode
))
193 * Can't send a directory...
198 _cupsSetError(IPP_NOT_POSSIBLE
, strerror(EISDIR
), 0);
204 if (!S_ISREG(fileinfo
.st_mode
))
205 length
= 0; /* Chunk when piping */
208 length
= ippLength(request
) + fileinfo
.st_size
;
211 length
= ippLength(request
);
213 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
214 (long)ippLength(request
), (long)length
));
217 * Clear any "Local" authentication data since it is probably stale...
220 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
221 httpSetAuthString(http
, NULL
, NULL
);
224 * Loop until we can send the request without authorization problems.
227 while (response
== NULL
)
229 DEBUG_puts("2cupsDoIORequest: setup...");
232 * Send the request...
235 status
= cupsSendRequest(http
, request
, resource
, length
);
237 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
239 if (status
== HTTP_CONTINUE
&& request
->state
== IPP_DATA
&& infile
>= 0)
241 DEBUG_puts("2cupsDoIORequest: file write...");
244 * Send the file with the request...
248 if (S_ISREG(fileinfo
.st_mode
))
250 lseek(infile
, 0, SEEK_SET
);
252 while ((bytes
= (int)read(infile
, buffer
, sizeof(buffer
))) > 0)
254 if ((status
= cupsWriteRequestData(http
, buffer
, bytes
))
261 * Get the server's response...
264 if (status
!= HTTP_ERROR
)
266 response
= cupsGetResponse(http
, resource
);
267 status
= httpGetStatus(http
);
270 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
272 if (status
== HTTP_ERROR
||
273 (status
>= HTTP_BAD_REQUEST
&& status
!= HTTP_UNAUTHORIZED
&&
274 status
!= HTTP_UPGRADE_REQUIRED
))
276 _cupsSetHTTPError(status
);
280 if (response
&& outfile
>= 0)
283 * Write trailing data to file...
286 while ((bytes
= (int)httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
287 if (write(outfile
, buffer
, bytes
) < bytes
)
291 if (http
->state
!= HTTP_WAITING
)
294 * Flush any remaining data...
302 * Delete the original request and return the response...
312 * 'cupsDoRequest()' - Do an IPP request.
314 * This function sends the IPP request to the specified server, retrying
315 * and authenticating as necessary. The request is freed with @link ippDelete@.
318 ipp_t
* /* O - Response data */
319 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
320 ipp_t
*request
, /* I - IPP request */
321 const char *resource
) /* I - HTTP resource for POST */
323 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
325 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
328 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
333 * 'cupsGetResponse()' - Get a response to an IPP request.
335 * Use this function to get the response for an IPP request sent using
336 * @link cupsSendRequest@. For requests that return additional data, use
337 * @link cupsReadResponseData@ after getting a successful response,
338 * otherwise call @link httpFlush@ to complete the response processing.
340 * @since CUPS 1.4/OS X 10.6@
343 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
344 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
345 const char *resource
) /* I - HTTP resource for POST */
347 http_status_t status
; /* HTTP status */
348 ipp_state_t state
; /* IPP read state */
349 ipp_t
*response
= NULL
; /* IPP response */
352 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http
, resource
));
355 * Connect to the default server as needed...
359 http
= _cupsConnect();
361 if (!http
|| (http
->state
!= HTTP_POST_RECV
&& http
->state
!= HTTP_POST_SEND
))
365 * Check for an unfinished chunked request...
368 if (http
->data_encoding
== HTTP_ENCODE_CHUNKED
)
371 * Send a 0-length chunk to finish off the request...
374 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
376 if (httpWrite2(http
, "", 0) < 0)
381 * Wait for a response from the server...
384 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
389 status
= httpUpdate(http
);
391 while (status
== HTTP_STATUS_CONTINUE
);
393 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
395 if (status
== HTTP_OK
)
398 * Get the IPP response...
403 while ((state
= ippRead(http
, response
)) != IPP_DATA
)
404 if (state
== IPP_ERROR
)
407 if (state
== IPP_ERROR
)
410 * Flush remaining data and delete the response...
413 DEBUG_puts("1cupsGetResponse: IPP read error!");
420 http
->status
= status
= HTTP_ERROR
;
421 http
->error
= EINVAL
;
424 else if (status
!= HTTP_ERROR
)
427 * Flush any error message...
433 * Then handle encryption and authentication...
436 if (status
== HTTP_UNAUTHORIZED
)
439 * See if we can do authentication...
442 DEBUG_puts("2cupsGetResponse: Need authorization...");
444 if (!cupsDoAuthentication(http
, "POST", resource
))
447 http
->status
= status
= HTTP_AUTHORIZATION_CANCELED
;
451 else if (status
== HTTP_UPGRADE_REQUIRED
)
454 * Force a reconnect with encryption...
457 DEBUG_puts("2cupsGetResponse: Need encryption...");
459 if (!httpReconnect(http
))
460 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
);
462 #endif /* HAVE_SSL */
467 ipp_attribute_t
*attr
; /* status-message attribute */
470 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
472 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
473 ippErrorString(response
->request
.status
.status_code
),
474 attr
? attr
->values
[0].string
.text
: ""));
476 _cupsSetError(response
->request
.status
.status_code
,
477 attr
? attr
->values
[0].string
.text
:
478 ippErrorString(response
->request
.status
.status_code
), 0);
486 * 'cupsLastError()' - Return the last IPP status code received on the current
490 ipp_status_t
/* O - IPP status code from last request */
493 return (_cupsGlobals()->last_error
);
498 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
501 * @since CUPS 1.2/OS X 10.5@
504 const char * /* O - status-message text from last request */
505 cupsLastErrorString(void)
507 return (_cupsGlobals()->last_status_message
);
512 * '_cupsNextDelay()' - Return the next retry delay value.
514 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
516 * Pass 0 for the current delay value to initialize the sequence.
519 int /* O - Next delay value */
520 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
521 int *previous
) /* IO - Previous delay value */
523 int next
; /* Next delay value */
528 next
= (current
+ *previous
) % 12;
529 *previous
= next
< current
? 0 : current
;
542 * 'cupsReadResponseData()' - Read additional data after the IPP response.
544 * This function is used after @link cupsGetResponse@ to read the PPD or document
545 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
548 * @since CUPS 1.4/OS X 10.6@
551 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
552 cupsReadResponseData(
553 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
554 char *buffer
, /* I - Buffer to use */
555 size_t length
) /* I - Number of bytes to read */
558 * Get the default connection as needed...
561 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
562 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
566 _cups_globals_t
*cg
= _cupsGlobals();
567 /* Pointer to library globals */
569 if ((http
= cg
->http
) == NULL
)
571 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
577 * Then read from the HTTP connection...
580 return (httpRead2(http
, buffer
, length
));
585 * 'cupsSendRequest()' - Send an IPP request.
587 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
588 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
589 * and @link cupsReadResponseData@ to read any additional data following the
590 * response. Only one request can be sent/queued at a time per @code http_t@
593 * Returns the initial HTTP status code, which will be @code HTTP_CONTINUE@
594 * on a successful send of the request.
596 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
597 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
599 * @since CUPS 1.4/OS X 10.6@
602 http_status_t
/* O - Initial HTTP status */
603 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
604 ipp_t
*request
, /* I - IPP request */
605 const char *resource
, /* I - Resource path */
606 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
608 http_status_t status
; /* Status of HTTP request */
609 int got_status
; /* Did we get the status? */
610 ipp_state_t state
; /* State of IPP processing */
611 http_status_t expect
; /* Expect: header to use */
614 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
615 "length=" CUPS_LLFMT
")", http
, request
,
616 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
617 resource
, CUPS_LLCAST length
));
620 * Range check input...
623 if (!request
|| !resource
)
625 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(EINVAL
), 0);
631 * Get the default connection as needed...
635 if ((http
= _cupsConnect()) == NULL
)
636 return (HTTP_SERVICE_UNAVAILABLE
);
639 * If the prior request was not flushed out, do so now...
642 if (http
->state
== HTTP_GET_SEND
||
643 http
->state
== HTTP_POST_SEND
)
645 DEBUG_puts("2cupsSendRequest: Flush prior response.");
648 else if (http
->state
!= HTTP_WAITING
)
650 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
651 "reconnecting.", http
->state
));
652 if (httpReconnect(http
))
658 * See if we have an auth-info attribute and are communicating over
659 * a non-local link. If so, encrypt the link so that we can pass
660 * the authentication information securely...
663 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
664 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
665 httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
667 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
668 return (HTTP_SERVICE_UNAVAILABLE
);
670 #endif /* HAVE_SSL */
673 * Reconnect if the last response had a "Connection: close"...
676 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
678 DEBUG_puts("2cupsSendRequest: Connection: close");
679 httpClearFields(http
);
680 if (httpReconnect(http
))
682 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
683 return (HTTP_SERVICE_UNAVAILABLE
);
688 * Loop until we can send the request without authorization problems.
691 expect
= HTTP_CONTINUE
;
695 DEBUG_puts("2cupsSendRequest: Setup...");
698 * Setup the HTTP variables needed...
701 httpClearFields(http
);
702 httpSetExpect(http
, expect
);
703 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
704 httpSetLength(http
, length
);
707 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
710 * Do not use cached Kerberos credentials since they will look like a
714 _cupsSetNegotiateAuthString(http
, "POST", resource
);
716 #endif /* HAVE_GSSAPI */
718 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
720 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
726 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
728 if (httpPost(http
, resource
))
730 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
731 if (httpReconnect(http
))
733 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
734 return (HTTP_SERVICE_UNAVAILABLE
);
741 * Send the IPP data...
744 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
746 request
->state
= IPP_IDLE
;
747 status
= HTTP_CONTINUE
;
750 while ((state
= ippWrite(http
, request
)) != IPP_DATA
)
751 if (state
== IPP_ERROR
)
753 else if (httpCheck(http
))
757 _httpUpdate(http
, &status
);
758 if (status
>= HTTP_MULTIPLE_CHOICES
)
762 if (state
== IPP_ERROR
)
764 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
766 http
->status
= HTTP_ERROR
;
767 http
->state
= HTTP_WAITING
;
773 * Wait up to 1 second to get the 100-continue response as needed...
778 if (expect
== HTTP_CONTINUE
)
780 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
782 if (httpWait(http
, 1000))
783 _httpUpdate(http
, &status
);
785 else if (httpCheck(http
))
786 _httpUpdate(http
, &status
);
789 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
792 * Process the current HTTP status...
795 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
797 int temp_status
; /* Temporary status */
799 _cupsSetHTTPError(status
);
803 temp_status
= httpUpdate(http
);
805 while (temp_status
!= HTTP_STATUS_ERROR
&&
806 http
->state
== HTTP_STATE_POST_RECV
);
813 case HTTP_STATUS_CONTINUE
:
814 case HTTP_STATUS_OK
:
815 case HTTP_STATUS_ERROR
:
816 DEBUG_printf(("1cupsSendRequest: Returning %d.", status
));
819 case HTTP_STATUS_UNAUTHORIZED
:
820 if (cupsDoAuthentication(http
, "POST", resource
))
822 DEBUG_puts("1cupsSendRequest: Returning HTTP_AUTHORIZATION_CANCELED.");
823 return (HTTP_AUTHORIZATION_CANCELED
);
826 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_UNAUTHORIZED.");
828 if (httpReconnect(http
))
830 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
831 return (HTTP_SERVICE_UNAVAILABLE
);
836 case HTTP_STATUS_UPGRADE_REQUIRED
:
838 * Flush any error message, reconnect, and then upgrade with
842 DEBUG_puts("2cupsSendRequest: Reconnecting after "
843 "HTTP_UPGRADE_REQUIRED.");
845 if (httpReconnect(http
))
847 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
848 return (HTTP_SERVICE_UNAVAILABLE
);
851 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
852 if (httpEncryption(http
, HTTP_ENCRYPT_REQUIRED
))
854 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
855 return (HTTP_SERVICE_UNAVAILABLE
);
858 #endif /* HAVE_SSL */
860 case HTTP_STATUS_EXPECTATION_FAILED
:
862 * Don't try using the Expect: header the next time around...
865 expect
= (http_status_t
)0;
867 DEBUG_puts("2cupsSendRequest: Reconnecting after "
868 "HTTP_EXPECTATION_FAILED.");
870 if (httpReconnect(http
))
872 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
873 return (HTTP_SERVICE_UNAVAILABLE
);
879 * Some other error...
889 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
891 * This function is used after @link cupsSendRequest@ to provide a PPD and
892 * after @link cupsStartDocument@ to provide a document file.
894 * @since CUPS 1.4/OS X 10.6@
897 http_status_t
/* O - @code HTTP_CONTINUE@ if OK or HTTP status on error */
898 cupsWriteRequestData(
899 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
900 const char *buffer
, /* I - Bytes to write */
901 size_t length
) /* I - Number of bytes to write */
903 int wused
; /* Previous bytes in buffer */
907 * Get the default connection as needed...
910 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
911 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
915 _cups_globals_t
*cg
= _cupsGlobals();
916 /* Pointer to library globals */
918 if ((http
= cg
->http
) == NULL
)
920 _cupsSetError(IPP_INTERNAL_ERROR
, _("No active connection"), 1);
921 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
927 * Then write to the HTTP connection...
932 if (httpWrite2(http
, buffer
, length
) < 0)
934 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_ERROR.");
935 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(http
->error
), 0);
940 * Finally, check if we have any pending data from the server...
943 if (length
>= HTTP_MAX_BUFFER
||
944 http
->wused
< wused
||
945 (wused
> 0 && http
->wused
== length
))
948 * We've written something to the server, so check for response data...
951 if (_httpWait(http
, 0, 1))
953 http_status_t status
; /* Status from _httpUpdate */
955 _httpUpdate(http
, &status
);
956 if (status
>= HTTP_MULTIPLE_CHOICES
)
958 _cupsSetHTTPError(status
);
962 status
= httpUpdate(http
);
964 while (status
!= HTTP_ERROR
&& http
->state
== HTTP_POST_RECV
);
969 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
974 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_CONTINUE.");
975 return (HTTP_CONTINUE
);
980 * '_cupsConnect()' - Get the default server connection...
983 http_t
* /* O - HTTP connection */
986 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
990 * See if we are connected to the same server...
996 * Compare the connection hostname, port, and encryption settings to
997 * the cached defaults; these were initialized the first time we
1001 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
1002 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
1003 (cg
->http
->encryption
!= cg
->encryption
&&
1004 cg
->http
->encryption
== HTTP_ENCRYPT_NEVER
))
1007 * Need to close the current connection because something has changed...
1010 httpClose(cg
->http
);
1016 * Same server, see if the connection is still established...
1019 char ch
; /* Connection check byte */
1022 if (recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
) < 0 &&
1023 WSAGetLastError() != WSAEWOULDBLOCK
)
1025 if (recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
) < 0 &&
1026 errno
!= EWOULDBLOCK
)
1030 * Nope, close the connection...
1033 httpClose(cg
->http
);
1040 * (Re)connect as needed...
1045 if ((cg
->http
= httpConnectEncrypt(cupsServer(), ippPort(),
1046 cupsEncryption())) == NULL
)
1049 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, NULL
, 0);
1051 _cupsSetError(IPP_SERVICE_UNAVAILABLE
,
1052 _("Unable to connect to host."), 1);
1057 * Return the cached connection...
1065 * '_cupsSetError()' - Set the last IPP status code and status-message.
1069 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
1070 const char *message
, /* I - status-message value */
1071 int localize
) /* I - Localize the message? */
1073 _cups_globals_t
*cg
; /* Global data */
1076 if (!message
&& errno
)
1078 message
= strerror(errno
);
1082 cg
= _cupsGlobals();
1083 cg
->last_error
= status
;
1085 if (cg
->last_status_message
)
1087 _cupsStrFree(cg
->last_status_message
);
1089 cg
->last_status_message
= NULL
;
1097 * Get the message catalog...
1100 if (!cg
->lang_default
)
1101 cg
->lang_default
= cupsLangDefault();
1103 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
1107 cg
->last_status_message
= _cupsStrAlloc(message
);
1110 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1111 ippErrorString(cg
->last_error
), cg
->last_status_message
));
1116 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1120 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
1124 case HTTP_NOT_FOUND
:
1125 _cupsSetError(IPP_NOT_FOUND
, httpStatus(status
), 0);
1128 case HTTP_UNAUTHORIZED
:
1129 _cupsSetError(IPP_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1132 case HTTP_AUTHORIZATION_CANCELED
:
1133 _cupsSetError(IPP_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1136 case HTTP_FORBIDDEN
:
1137 _cupsSetError(IPP_FORBIDDEN
, httpStatus(status
), 0);
1140 case HTTP_BAD_REQUEST
:
1141 _cupsSetError(IPP_BAD_REQUEST
, httpStatus(status
), 0);
1144 case HTTP_REQUEST_TOO_LARGE
:
1145 _cupsSetError(IPP_REQUEST_VALUE
, httpStatus(status
), 0);
1148 case HTTP_NOT_IMPLEMENTED
:
1149 _cupsSetError(IPP_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1152 case HTTP_NOT_SUPPORTED
:
1153 _cupsSetError(IPP_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1156 case HTTP_UPGRADE_REQUIRED
:
1157 _cupsSetError(IPP_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1160 case HTTP_PKI_ERROR
:
1161 _cupsSetError(IPP_PKI_ERROR
, httpStatus(status
), 0);
1165 _cupsSetError(IPP_INTERNAL_ERROR
, strerror(errno
), 0);
1169 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1170 "IPP_SERVICE_UNAVAILABLE!", status
));
1171 _cupsSetError(IPP_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);
1178 * End of "$Id: request.c 7946 2008-09-16 23:27:54Z mike $".