2 * IPP utilities for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
23 #if defined(WIN32) || defined(__EMX__)
27 #endif /* WIN32 || __EMX__ */
32 # define MSG_DONTWAIT 0
33 #endif /* !MSG_DONTWAIT */
37 * 'cupsDoFileRequest()' - Do an IPP request with a file.
39 * This function sends the IPP request and attached file to the specified
40 * server, retrying and authenticating as necessary. The request is freed with
44 ipp_t
* /* O - Response data */
45 cupsDoFileRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
46 ipp_t
*request
, /* I - IPP request */
47 const char *resource
, /* I - HTTP resource for POST */
48 const char *filename
) /* I - File to send or @code NULL@ for none */
50 ipp_t
*response
; /* IPP response data */
51 int infile
; /* Input file */
54 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", filename=\"%s\")", (void *)http
, (void *)request
, request
? ippOpString(request
->request
.op
.operation_id
) : "?", resource
, filename
));
58 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
61 * Can't get file information!
64 _cupsSetError(errno
== ENOENT
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
,
75 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
85 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
87 * This function sends the IPP request with the optional input file "infile" to
88 * the specified server, retrying and authenticating as necessary. The request
89 * is freed with @link ippDelete@.
91 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
92 * all of the data from the file after the IPP request message.
94 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
95 * all of the data after the IPP response message to the file.
97 * @since CUPS 1.3/macOS 10.5@
100 ipp_t
* /* O - Response data */
101 cupsDoIORequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
102 ipp_t
*request
, /* I - IPP request */
103 const char *resource
, /* I - HTTP resource for POST */
104 int infile
, /* I - File to read from or -1 for none */
105 int outfile
) /* I - File to write to or -1 for none */
107 ipp_t
*response
= NULL
; /* IPP response data */
108 size_t length
= 0; /* Content-Length value */
109 http_status_t status
; /* Status of HTTP request */
110 struct stat fileinfo
; /* File information */
111 ssize_t bytes
; /* Number of bytes read/written */
112 char buffer
[32768]; /* Output buffer */
115 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", infile=%d, outfile=%d)", (void *)http
, (void *)request
, request
? ippOpString(request
->request
.op
.operation_id
) : "?", resource
, infile
, outfile
));
118 * Range check input...
121 if (!request
|| !resource
)
125 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
131 * Get the default connection as needed...
135 if ((http
= _cupsConnect()) == NULL
)
143 * See if we have a file to send...
148 if (fstat(infile
, &fileinfo
))
151 * Can't get file information!
154 _cupsSetError(errno
== EBADF
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
,
163 if (fileinfo
.st_mode
& _S_IFDIR
)
165 if (S_ISDIR(fileinfo
.st_mode
))
169 * Can't send a directory...
174 _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE
, strerror(EISDIR
), 0);
180 if (!S_ISREG(fileinfo
.st_mode
))
181 length
= 0; /* Chunk when piping */
184 length
= ippLength(request
) + (size_t)fileinfo
.st_size
;
187 length
= ippLength(request
);
189 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
190 (long)ippLength(request
), (long)length
));
193 * Clear any "Local" authentication data since it is probably stale...
196 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
197 httpSetAuthString(http
, NULL
, NULL
);
200 * Loop until we can send the request without authorization problems.
203 while (response
== NULL
)
205 DEBUG_puts("2cupsDoIORequest: setup...");
208 * Send the request...
211 status
= cupsSendRequest(http
, request
, resource
, length
);
213 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
215 if (status
== HTTP_STATUS_CONTINUE
&& request
->state
== IPP_STATE_DATA
&& infile
>= 0)
217 DEBUG_puts("2cupsDoIORequest: file write...");
220 * Send the file with the request...
224 if (S_ISREG(fileinfo
.st_mode
))
226 lseek(infile
, 0, SEEK_SET
);
228 while ((bytes
= read(infile
, buffer
, sizeof(buffer
))) > 0)
230 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
))
231 != HTTP_STATUS_CONTINUE
)
237 * Get the server's response...
240 if (status
<= HTTP_STATUS_CONTINUE
|| status
== HTTP_STATUS_OK
)
242 response
= cupsGetResponse(http
, resource
);
243 status
= httpGetStatus(http
);
246 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
248 if (status
== HTTP_STATUS_ERROR
||
249 (status
>= HTTP_STATUS_BAD_REQUEST
&& status
!= HTTP_STATUS_UNAUTHORIZED
&&
250 status
!= HTTP_STATUS_UPGRADE_REQUIRED
))
252 _cupsSetHTTPError(status
);
256 if (response
&& outfile
>= 0)
259 * Write trailing data to file...
262 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
263 if (write(outfile
, buffer
, (size_t)bytes
) < bytes
)
267 if (http
->state
!= HTTP_STATE_WAITING
)
270 * Flush any remaining data...
278 * Delete the original request and return the response...
288 * 'cupsDoRequest()' - Do an IPP request.
290 * This function sends the IPP request to the specified server, retrying
291 * and authenticating as necessary. The request is freed with @link ippDelete@.
294 ipp_t
* /* O - Response data */
295 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
296 ipp_t
*request
, /* I - IPP request */
297 const char *resource
) /* I - HTTP resource for POST */
299 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", (void *)http
, (void *)request
, request
? ippOpString(request
->request
.op
.operation_id
) : "?", resource
));
301 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
306 * 'cupsGetResponse()' - Get a response to an IPP request.
308 * Use this function to get the response for an IPP request sent using
309 * @link cupsSendRequest@. For requests that return additional data, use
310 * @link cupsReadResponseData@ after getting a successful response,
311 * otherwise call @link httpFlush@ to complete the response processing.
313 * @since CUPS 1.4/macOS 10.6@
316 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
317 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
318 const char *resource
) /* I - HTTP resource for POST */
320 http_status_t status
; /* HTTP status */
321 ipp_state_t state
; /* IPP read state */
322 ipp_t
*response
= NULL
; /* IPP response */
325 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", (void *)http
, resource
));
326 DEBUG_printf(("1cupsGetResponse: http->state=%d", http
? http
->state
: HTTP_STATE_ERROR
));
329 * Connect to the default server as needed...
334 _cups_globals_t
*cg
= _cupsGlobals();
335 /* Pointer to library globals */
337 if ((http
= cg
->http
) == NULL
)
339 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection."), 1);
340 DEBUG_puts("1cupsGetResponse: No active connection - returning NULL.");
345 if (http
->state
!= HTTP_STATE_POST_RECV
&& http
->state
!= HTTP_STATE_POST_SEND
)
347 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request sent."), 1);
348 DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL.");
353 * Check for an unfinished chunked request...
356 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
359 * Send a 0-length chunk to finish off the request...
362 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
364 if (httpWrite2(http
, "", 0) < 0)
369 * Wait for a response from the server...
372 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
377 status
= httpUpdate(http
);
379 while (status
== HTTP_STATUS_CONTINUE
);
381 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
383 if (status
== HTTP_STATUS_OK
)
386 * Get the IPP response...
391 while ((state
= ippRead(http
, response
)) != IPP_STATE_DATA
)
392 if (state
== IPP_STATE_ERROR
)
395 if (state
== IPP_STATE_ERROR
)
398 * Flush remaining data and delete the response...
401 DEBUG_puts("1cupsGetResponse: IPP read error!");
408 http
->status
= status
= HTTP_STATUS_ERROR
;
409 http
->error
= EINVAL
;
412 else if (status
!= HTTP_STATUS_ERROR
)
415 * Flush any error message...
421 * Then handle encryption and authentication...
424 if (status
== HTTP_STATUS_UNAUTHORIZED
)
427 * See if we can do authentication...
430 DEBUG_puts("2cupsGetResponse: Need authorization...");
432 if (!cupsDoAuthentication(http
, "POST", resource
))
433 httpReconnect2(http
, 30000, NULL
);
435 http
->status
= status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
439 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
442 * Force a reconnect with encryption...
445 DEBUG_puts("2cupsGetResponse: Need encryption...");
447 if (!httpReconnect2(http
, 30000, NULL
))
448 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
450 #endif /* HAVE_SSL */
455 ipp_attribute_t
*attr
; /* status-message attribute */
458 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
460 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
461 ippErrorString(response
->request
.status
.status_code
),
462 attr
? attr
->values
[0].string
.text
: ""));
464 _cupsSetError(response
->request
.status
.status_code
,
465 attr
? attr
->values
[0].string
.text
:
466 ippErrorString(response
->request
.status
.status_code
), 0);
474 * 'cupsLastError()' - Return the last IPP status code received on the current
478 ipp_status_t
/* O - IPP status code from last request */
481 return (_cupsGlobals()->last_error
);
486 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
489 * @since CUPS 1.2/macOS 10.5@
492 const char * /* O - status-message text from last request */
493 cupsLastErrorString(void)
495 return (_cupsGlobals()->last_status_message
);
500 * '_cupsNextDelay()' - Return the next retry delay value.
502 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
504 * Pass 0 for the current delay value to initialize the sequence.
507 int /* O - Next delay value */
508 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
509 int *previous
) /* IO - Previous delay value */
511 int next
; /* Next delay value */
516 next
= (current
+ *previous
) % 12;
517 *previous
= next
< current
? 0 : current
;
530 * 'cupsReadResponseData()' - Read additional data after the IPP response.
532 * This function is used after @link cupsGetResponse@ to read the PPD or document
533 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
536 * @since CUPS 1.4/macOS 10.6@
539 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
540 cupsReadResponseData(
541 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
542 char *buffer
, /* I - Buffer to use */
543 size_t length
) /* I - Number of bytes to read */
546 * Get the default connection as needed...
549 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
553 _cups_globals_t
*cg
= _cupsGlobals();
554 /* Pointer to library globals */
556 if ((http
= cg
->http
) == NULL
)
558 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
564 * Then read from the HTTP connection...
567 return (httpRead2(http
, buffer
, length
));
572 * 'cupsSendRequest()' - Send an IPP request.
574 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
575 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
576 * and @link cupsReadResponseData@ to read any additional data following the
577 * response. Only one request can be sent/queued at a time per @code http_t@
580 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
581 * on a successful send of the request.
583 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
584 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
586 * @since CUPS 1.4/macOS 10.6@
589 http_status_t
/* O - Initial HTTP status */
590 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
591 ipp_t
*request
, /* I - IPP request */
592 const char *resource
, /* I - Resource path */
593 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
595 http_status_t status
; /* Status of HTTP request */
596 int got_status
; /* Did we get the status? */
597 ipp_state_t state
; /* State of IPP processing */
598 http_status_t expect
; /* Expect: header to use */
599 char date
[256]; /* Date: header value */
602 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", length=" CUPS_LLFMT
")", (void *)http
, (void *)request
, request
? ippOpString(request
->request
.op
.operation_id
) : "?", resource
, CUPS_LLCAST length
));
605 * Range check input...
608 if (!request
|| !resource
)
610 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
612 return (HTTP_STATUS_ERROR
);
616 * Get the default connection as needed...
620 if ((http
= _cupsConnect()) == NULL
)
621 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
624 * If the prior request was not flushed out, do so now...
627 if (http
->state
== HTTP_STATE_GET_SEND
||
628 http
->state
== HTTP_STATE_POST_SEND
)
630 DEBUG_puts("2cupsSendRequest: Flush prior response.");
633 else if (http
->state
!= HTTP_STATE_WAITING
)
635 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
636 "reconnecting.", http
->state
));
637 if (httpReconnect2(http
, 30000, NULL
))
638 return (HTTP_STATUS_ERROR
);
643 * See if we have an auth-info attribute and are communicating over
644 * a non-local link. If so, encrypt the link so that we can pass
645 * the authentication information securely...
648 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
649 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
650 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
652 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
653 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
655 #endif /* HAVE_SSL */
658 * Reconnect if the last response had a "Connection: close"...
661 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
663 DEBUG_puts("2cupsSendRequest: Connection: close");
664 httpClearFields(http
);
665 if (httpReconnect2(http
, 30000, NULL
))
667 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
668 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
673 * Loop until we can send the request without authorization problems.
676 expect
= HTTP_STATUS_CONTINUE
;
680 DEBUG_puts("2cupsSendRequest: Setup...");
683 * Setup the HTTP variables needed...
686 httpClearFields(http
);
687 httpSetExpect(http
, expect
);
688 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
689 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString2(time(NULL
), date
, (int)sizeof(date
)));
690 httpSetLength(http
, length
);
693 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
696 * Do not use cached Kerberos credentials since they will look like a
700 _cupsSetNegotiateAuthString(http
, "POST", resource
);
702 #endif /* HAVE_GSSAPI */
704 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
706 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
712 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
714 if (httpPost(http
, resource
))
716 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
717 if (httpReconnect2(http
, 30000, NULL
))
719 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
720 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
727 * Send the IPP data...
730 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
732 request
->state
= IPP_STATE_IDLE
;
733 status
= HTTP_STATUS_CONTINUE
;
736 while ((state
= ippWrite(http
, request
)) != IPP_STATE_DATA
)
742 _httpUpdate(http
, &status
);
743 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
746 else if (state
== IPP_STATE_ERROR
)
750 if (state
== IPP_STATE_ERROR
)
753 * We weren't able to send the IPP request. But did we already get a HTTP
757 if (!got_status
|| status
< HTTP_STATUS_MULTIPLE_CHOICES
)
760 * No, something else went wrong.
763 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
765 http
->status
= HTTP_STATUS_ERROR
;
766 http
->state
= HTTP_STATE_WAITING
;
768 return (HTTP_STATUS_ERROR
);
773 * Wait up to 1 second to get the 100-continue response as needed...
778 if (expect
== HTTP_STATUS_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_STATUS_CUPS_AUTHORIZATION_CANCELED.");
823 return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
);
826 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
828 if (httpReconnect2(http
, 30000, NULL
))
830 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
831 return (HTTP_STATUS_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_STATUS_UPGRADE_REQUIRED.");
845 if (httpReconnect2(http
, 30000, NULL
))
847 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
848 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
851 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
852 if (httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
854 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
855 return (HTTP_STATUS_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 (httpReconnect2(http
, 30000, NULL
))
872 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
873 return (HTTP_STATUS_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/macOS 10.6@
897 http_status_t
/* O - @code HTTP_STATUS_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, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
914 _cups_globals_t
*cg
= _cupsGlobals();
915 /* Pointer to library globals */
917 if ((http
= cg
->http
) == NULL
)
919 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
920 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
921 return (HTTP_STATUS_ERROR
);
926 * Then write to the HTTP connection...
931 if (httpWrite2(http
, buffer
, length
) < 0)
933 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
934 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(http
->error
), 0);
935 return (HTTP_STATUS_ERROR
);
939 * Finally, check if we have any pending data from the server...
942 if (length
>= HTTP_MAX_BUFFER
||
943 http
->wused
< wused
||
944 (wused
> 0 && (size_t)http
->wused
== length
))
947 * We've written something to the server, so check for response data...
950 if (_httpWait(http
, 0, 1))
952 http_status_t status
; /* Status from _httpUpdate */
954 _httpUpdate(http
, &status
);
955 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
957 _cupsSetHTTPError(status
);
961 status
= httpUpdate(http
);
963 while (status
!= HTTP_STATUS_ERROR
&& http
->state
== HTTP_STATE_POST_RECV
);
968 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
973 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
974 return (HTTP_STATUS_CONTINUE
);
979 * '_cupsConnect()' - Get the default server connection...
982 http_t
* /* O - HTTP connection */
985 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
989 * See if we are connected to the same server...
995 * Compare the connection hostname, port, and encryption settings to
996 * the cached defaults; these were initialized the first time we
1000 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
1001 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
1002 (cg
->http
->encryption
!= cg
->encryption
&&
1003 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
1006 * Need to close the current connection because something has changed...
1009 httpClose(cg
->http
);
1015 * Same server, see if the connection is still established...
1018 char ch
; /* Connection check byte */
1019 ssize_t n
; /* Number of bytes */
1022 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
)) == 0 ||
1023 (n
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
))
1025 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
)) == 0 ||
1026 (n
< 0 && errno
!= EWOULDBLOCK
))
1030 * Nope, close the connection...
1033 httpClose(cg
->http
);
1040 * (Re)connect as needed...
1045 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
1046 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1049 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
1051 _cupsSetError(IPP_STATUS_ERROR_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_STATUS_NOT_FOUND
:
1125 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, httpStatus(status
), 0);
1128 case HTTP_STATUS_UNAUTHORIZED
:
1129 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1132 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
:
1133 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1136 case HTTP_STATUS_FORBIDDEN
:
1137 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN
, httpStatus(status
), 0);
1140 case HTTP_STATUS_BAD_REQUEST
:
1141 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST
, httpStatus(status
), 0);
1144 case HTTP_STATUS_REQUEST_TOO_LARGE
:
1145 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE
, httpStatus(status
), 0);
1148 case HTTP_STATUS_NOT_IMPLEMENTED
:
1149 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1152 case HTTP_STATUS_NOT_SUPPORTED
:
1153 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1156 case HTTP_STATUS_UPGRADE_REQUIRED
:
1157 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1160 case HTTP_STATUS_CUPS_PKI_ERROR
:
1161 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, httpStatus(status
), 0);
1164 case HTTP_STATUS_ERROR
:
1165 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1169 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1170 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status
));
1171 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);