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...
134 if (!http
&& (http
= _cupsConnect()) == NULL
)
142 * See if we have a file to send...
147 if (fstat(infile
, &fileinfo
))
150 * Can't get file information!
153 _cupsSetError(errno
== EBADF
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
, NULL
, 0);
160 if (fileinfo
.st_mode
& _S_IFDIR
)
162 if (S_ISDIR(fileinfo
.st_mode
))
166 * Can't send a directory...
169 _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE
, strerror(EISDIR
), 0);
176 if (!S_ISREG(fileinfo
.st_mode
))
177 length
= 0; /* Chunk when piping */
180 length
= ippLength(request
) + (size_t)fileinfo
.st_size
;
183 length
= ippLength(request
);
185 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld", (long)ippLength(request
), (long)length
));
188 * Clear any "Local" authentication data since it is probably stale...
191 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
192 httpSetAuthString(http
, NULL
, NULL
);
195 * Loop until we can send the request without authorization problems.
198 while (response
== NULL
)
200 DEBUG_puts("2cupsDoIORequest: setup...");
203 * Send the request...
206 status
= cupsSendRequest(http
, request
, resource
, length
);
208 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
210 if (status
== HTTP_STATUS_CONTINUE
&& request
->state
== IPP_STATE_DATA
&& infile
>= 0)
212 DEBUG_puts("2cupsDoIORequest: file write...");
215 * Send the file with the request...
219 if (S_ISREG(fileinfo
.st_mode
))
221 lseek(infile
, 0, SEEK_SET
);
223 while ((bytes
= read(infile
, buffer
, sizeof(buffer
))) > 0)
225 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
))
226 != HTTP_STATUS_CONTINUE
)
232 * Get the server's response...
235 if (status
<= HTTP_STATUS_CONTINUE
|| status
== HTTP_STATUS_OK
)
237 response
= cupsGetResponse(http
, resource
);
238 status
= httpGetStatus(http
);
241 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
243 if (status
== HTTP_STATUS_ERROR
||
244 (status
>= HTTP_STATUS_BAD_REQUEST
&& status
!= HTTP_STATUS_UNAUTHORIZED
&&
245 status
!= HTTP_STATUS_UPGRADE_REQUIRED
))
247 _cupsSetHTTPError(status
);
251 if (response
&& outfile
>= 0)
254 * Write trailing data to file...
257 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
258 if (write(outfile
, buffer
, (size_t)bytes
) < bytes
)
262 if (http
->state
!= HTTP_STATE_WAITING
)
265 * Flush any remaining data...
273 * Delete the original request and return the response...
283 * 'cupsDoRequest()' - Do an IPP request.
285 * This function sends the IPP request to the specified server, retrying
286 * and authenticating as necessary. The request is freed with @link ippDelete@.
289 ipp_t
* /* O - Response data */
290 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
291 ipp_t
*request
, /* I - IPP request */
292 const char *resource
) /* I - HTTP resource for POST */
294 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", (void *)http
, (void *)request
, request
? ippOpString(request
->request
.op
.operation_id
) : "?", resource
));
296 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
301 * 'cupsGetResponse()' - Get a response to an IPP request.
303 * Use this function to get the response for an IPP request sent using
304 * @link cupsSendRequest@. For requests that return additional data, use
305 * @link cupsReadResponseData@ after getting a successful response,
306 * otherwise call @link httpFlush@ to complete the response processing.
308 * @since CUPS 1.4/macOS 10.6@
311 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
312 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
313 const char *resource
) /* I - HTTP resource for POST */
315 http_status_t status
; /* HTTP status */
316 ipp_state_t state
; /* IPP read state */
317 ipp_t
*response
= NULL
; /* IPP response */
320 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", (void *)http
, resource
));
321 DEBUG_printf(("1cupsGetResponse: http->state=%d", http
? http
->state
: HTTP_STATE_ERROR
));
324 * Connect to the default server as needed...
329 _cups_globals_t
*cg
= _cupsGlobals();
330 /* Pointer to library globals */
332 if ((http
= cg
->http
) == NULL
)
334 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection."), 1);
335 DEBUG_puts("1cupsGetResponse: No active connection - returning NULL.");
340 if (http
->state
!= HTTP_STATE_POST_RECV
&& http
->state
!= HTTP_STATE_POST_SEND
)
342 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request sent."), 1);
343 DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL.");
348 * Check for an unfinished chunked request...
351 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
354 * Send a 0-length chunk to finish off the request...
357 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
359 if (httpWrite2(http
, "", 0) < 0)
364 * Wait for a response from the server...
367 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
372 status
= httpUpdate(http
);
374 while (status
== HTTP_STATUS_CONTINUE
);
376 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
378 if (status
== HTTP_STATUS_OK
)
381 * Get the IPP response...
386 while ((state
= ippRead(http
, response
)) != IPP_STATE_DATA
)
387 if (state
== IPP_STATE_ERROR
)
390 if (state
== IPP_STATE_ERROR
)
393 * Flush remaining data and delete the response...
396 DEBUG_puts("1cupsGetResponse: IPP read error!");
403 http
->status
= status
= HTTP_STATUS_ERROR
;
404 http
->error
= EINVAL
;
407 else if (status
!= HTTP_STATUS_ERROR
)
410 * Flush any error message...
416 * Then handle encryption and authentication...
419 if (status
== HTTP_STATUS_UNAUTHORIZED
)
422 * See if we can do authentication...
425 DEBUG_puts("2cupsGetResponse: Need authorization...");
427 if (!cupsDoAuthentication(http
, "POST", resource
))
428 httpReconnect2(http
, 30000, NULL
);
430 http
->status
= status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
434 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
437 * Force a reconnect with encryption...
440 DEBUG_puts("2cupsGetResponse: Need encryption...");
442 if (!httpReconnect2(http
, 30000, NULL
))
443 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
445 #endif /* HAVE_SSL */
450 ipp_attribute_t
*attr
; /* status-message attribute */
453 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
455 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
456 ippErrorString(response
->request
.status
.status_code
),
457 attr
? attr
->values
[0].string
.text
: ""));
459 _cupsSetError(response
->request
.status
.status_code
,
460 attr
? attr
->values
[0].string
.text
:
461 ippErrorString(response
->request
.status
.status_code
), 0);
469 * 'cupsLastError()' - Return the last IPP status code received on the current
473 ipp_status_t
/* O - IPP status code from last request */
476 return (_cupsGlobals()->last_error
);
481 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
484 * @since CUPS 1.2/macOS 10.5@
487 const char * /* O - status-message text from last request */
488 cupsLastErrorString(void)
490 return (_cupsGlobals()->last_status_message
);
495 * '_cupsNextDelay()' - Return the next retry delay value.
497 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
499 * Pass 0 for the current delay value to initialize the sequence.
502 int /* O - Next delay value */
503 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
504 int *previous
) /* IO - Previous delay value */
506 int next
; /* Next delay value */
511 next
= (current
+ *previous
) % 12;
512 *previous
= next
< current
? 0 : current
;
525 * 'cupsReadResponseData()' - Read additional data after the IPP response.
527 * This function is used after @link cupsGetResponse@ to read the PPD or document
528 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
531 * @since CUPS 1.4/macOS 10.6@
534 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
535 cupsReadResponseData(
536 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
537 char *buffer
, /* I - Buffer to use */
538 size_t length
) /* I - Number of bytes to read */
541 * Get the default connection as needed...
544 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
548 _cups_globals_t
*cg
= _cupsGlobals();
549 /* Pointer to library globals */
551 if ((http
= cg
->http
) == NULL
)
553 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
559 * Then read from the HTTP connection...
562 return (httpRead2(http
, buffer
, length
));
567 * 'cupsSendRequest()' - Send an IPP request.
569 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
570 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
571 * and @link cupsReadResponseData@ to read any additional data following the
572 * response. Only one request can be sent/queued at a time per @code http_t@
575 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
576 * on a successful send of the request.
578 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
579 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
581 * @since CUPS 1.4/macOS 10.6@
584 http_status_t
/* O - Initial HTTP status */
585 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
586 ipp_t
*request
, /* I - IPP request */
587 const char *resource
, /* I - Resource path */
588 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
590 http_status_t status
; /* Status of HTTP request */
591 int got_status
; /* Did we get the status? */
592 ipp_state_t state
; /* State of IPP processing */
593 http_status_t expect
; /* Expect: header to use */
594 char date
[256]; /* Date: header value */
597 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
));
600 * Range check input...
603 if (!request
|| !resource
)
605 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
607 return (HTTP_STATUS_ERROR
);
611 * Get the default connection as needed...
614 if (!http
&& (http
= _cupsConnect()) == NULL
)
615 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
618 * If the prior request was not flushed out, do so now...
621 if (http
->state
== HTTP_STATE_GET_SEND
||
622 http
->state
== HTTP_STATE_POST_SEND
)
624 DEBUG_puts("2cupsSendRequest: Flush prior response.");
627 else if (http
->state
!= HTTP_STATE_WAITING
)
629 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
630 "reconnecting.", http
->state
));
631 if (httpReconnect2(http
, 30000, NULL
))
632 return (HTTP_STATUS_ERROR
);
637 * See if we have an auth-info attribute and are communicating over
638 * a non-local link. If so, encrypt the link so that we can pass
639 * the authentication information securely...
642 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
643 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
644 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
646 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
647 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
649 #endif /* HAVE_SSL */
652 * Reconnect if the last response had a "Connection: close"...
655 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
657 DEBUG_puts("2cupsSendRequest: Connection: close");
658 httpClearFields(http
);
659 if (httpReconnect2(http
, 30000, NULL
))
661 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
662 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
667 * Loop until we can send the request without authorization problems.
670 expect
= HTTP_STATUS_CONTINUE
;
674 DEBUG_puts("2cupsSendRequest: Setup...");
677 * Setup the HTTP variables needed...
680 httpClearFields(http
);
681 httpSetExpect(http
, expect
);
682 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
683 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString2(time(NULL
), date
, (int)sizeof(date
)));
684 httpSetLength(http
, length
);
687 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
690 * Do not use cached Kerberos credentials since they will look like a
694 _cupsSetNegotiateAuthString(http
, "POST", resource
);
696 #endif /* HAVE_GSSAPI */
698 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
700 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
706 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
708 if (httpPost(http
, resource
))
710 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
711 if (httpReconnect2(http
, 30000, NULL
))
713 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
714 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
721 * Send the IPP data...
724 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
726 request
->state
= IPP_STATE_IDLE
;
727 status
= HTTP_STATUS_CONTINUE
;
730 while ((state
= ippWrite(http
, request
)) != IPP_STATE_DATA
)
736 _httpUpdate(http
, &status
);
737 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
740 else if (state
== IPP_STATE_ERROR
)
744 if (state
== IPP_STATE_ERROR
)
747 * We weren't able to send the IPP request. But did we already get a HTTP
751 if (!got_status
|| status
< HTTP_STATUS_MULTIPLE_CHOICES
)
754 * No, something else went wrong.
757 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
759 http
->status
= HTTP_STATUS_ERROR
;
760 http
->state
= HTTP_STATE_WAITING
;
762 return (HTTP_STATUS_ERROR
);
767 * Wait up to 1 second to get the 100-continue response as needed...
772 if (expect
== HTTP_STATUS_CONTINUE
)
774 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
776 if (httpWait(http
, 1000))
777 _httpUpdate(http
, &status
);
779 else if (httpCheck(http
))
780 _httpUpdate(http
, &status
);
783 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
786 * Process the current HTTP status...
789 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
791 int temp_status
; /* Temporary status */
793 _cupsSetHTTPError(status
);
797 temp_status
= httpUpdate(http
);
799 while (temp_status
!= HTTP_STATUS_ERROR
&&
800 http
->state
== HTTP_STATE_POST_RECV
);
807 case HTTP_STATUS_CONTINUE
:
808 case HTTP_STATUS_OK
:
809 case HTTP_STATUS_ERROR
:
810 DEBUG_printf(("1cupsSendRequest: Returning %d.", status
));
813 case HTTP_STATUS_UNAUTHORIZED
:
814 if (cupsDoAuthentication(http
, "POST", resource
))
816 DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
817 return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
);
820 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
822 if (httpReconnect2(http
, 30000, NULL
))
824 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
825 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
830 case HTTP_STATUS_UPGRADE_REQUIRED
:
832 * Flush any error message, reconnect, and then upgrade with
836 DEBUG_puts("2cupsSendRequest: Reconnecting after "
837 "HTTP_STATUS_UPGRADE_REQUIRED.");
839 if (httpReconnect2(http
, 30000, NULL
))
841 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
842 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
845 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
846 if (httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
848 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
849 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
852 #endif /* HAVE_SSL */
854 case HTTP_STATUS_EXPECTATION_FAILED
:
856 * Don't try using the Expect: header the next time around...
859 expect
= (http_status_t
)0;
861 DEBUG_puts("2cupsSendRequest: Reconnecting after "
862 "HTTP_EXPECTATION_FAILED.");
864 if (httpReconnect2(http
, 30000, NULL
))
866 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
867 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
873 * Some other error...
883 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
885 * This function is used after @link cupsSendRequest@ to provide a PPD and
886 * after @link cupsStartDocument@ to provide a document file.
888 * @since CUPS 1.4/macOS 10.6@
891 http_status_t
/* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
892 cupsWriteRequestData(
893 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
894 const char *buffer
, /* I - Bytes to write */
895 size_t length
) /* I - Number of bytes to write */
897 int wused
; /* Previous bytes in buffer */
901 * Get the default connection as needed...
904 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
908 _cups_globals_t
*cg
= _cupsGlobals();
909 /* Pointer to library globals */
911 if ((http
= cg
->http
) == NULL
)
913 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
914 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
915 return (HTTP_STATUS_ERROR
);
920 * Then write to the HTTP connection...
925 if (httpWrite2(http
, buffer
, length
) < 0)
927 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
928 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(http
->error
), 0);
929 return (HTTP_STATUS_ERROR
);
933 * Finally, check if we have any pending data from the server...
936 if (length
>= HTTP_MAX_BUFFER
||
937 http
->wused
< wused
||
938 (wused
> 0 && (size_t)http
->wused
== length
))
941 * We've written something to the server, so check for response data...
944 if (_httpWait(http
, 0, 1))
946 http_status_t status
; /* Status from _httpUpdate */
948 _httpUpdate(http
, &status
);
949 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
951 _cupsSetHTTPError(status
);
955 status
= httpUpdate(http
);
957 while (status
!= HTTP_STATUS_ERROR
&& http
->state
== HTTP_STATE_POST_RECV
);
962 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
967 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
968 return (HTTP_STATUS_CONTINUE
);
973 * '_cupsConnect()' - Get the default server connection...
976 http_t
* /* O - HTTP connection */
979 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
983 * See if we are connected to the same server...
989 * Compare the connection hostname, port, and encryption settings to
990 * the cached defaults; these were initialized the first time we
994 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
996 (httpAddrFamily(cg
->http
->hostaddr
) != AF_LOCAL
&& cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
)) ||
998 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
999 #endif /* AF_LOCAL */
1000 (cg
->http
->encryption
!= cg
->encryption
&&
1001 cg
->http
->encryption
== HTTP_ENCRYPTION_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 */
1017 ssize_t n
; /* Number of bytes */
1020 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
)) == 0 ||
1021 (n
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
))
1023 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
)) == 0 ||
1024 (n
< 0 && errno
!= EWOULDBLOCK
))
1028 * Nope, close the connection...
1031 httpClose(cg
->http
);
1038 * (Re)connect as needed...
1043 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
1044 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1047 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
1049 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
1050 _("Unable to connect to host."), 1);
1055 * Return the cached connection...
1063 * '_cupsSetError()' - Set the last IPP status code and status-message.
1067 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
1068 const char *message
, /* I - status-message value */
1069 int localize
) /* I - Localize the message? */
1071 _cups_globals_t
*cg
; /* Global data */
1074 if (!message
&& errno
)
1076 message
= strerror(errno
);
1080 cg
= _cupsGlobals();
1081 cg
->last_error
= status
;
1083 if (cg
->last_status_message
)
1085 _cupsStrFree(cg
->last_status_message
);
1087 cg
->last_status_message
= NULL
;
1095 * Get the message catalog...
1098 if (!cg
->lang_default
)
1099 cg
->lang_default
= cupsLangDefault();
1101 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
1105 cg
->last_status_message
= _cupsStrAlloc(message
);
1108 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1109 ippErrorString(cg
->last_error
), cg
->last_status_message
));
1114 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1118 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
1122 case HTTP_STATUS_NOT_FOUND
:
1123 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, httpStatus(status
), 0);
1126 case HTTP_STATUS_UNAUTHORIZED
:
1127 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1130 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
:
1131 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1134 case HTTP_STATUS_FORBIDDEN
:
1135 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN
, httpStatus(status
), 0);
1138 case HTTP_STATUS_BAD_REQUEST
:
1139 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST
, httpStatus(status
), 0);
1142 case HTTP_STATUS_REQUEST_TOO_LARGE
:
1143 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE
, httpStatus(status
), 0);
1146 case HTTP_STATUS_NOT_IMPLEMENTED
:
1147 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1150 case HTTP_STATUS_NOT_SUPPORTED
:
1151 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1154 case HTTP_STATUS_UPGRADE_REQUIRED
:
1155 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1158 case HTTP_STATUS_CUPS_PKI_ERROR
:
1159 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, httpStatus(status
), 0);
1162 case HTTP_STATUS_ERROR
:
1163 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1167 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1168 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status
));
1169 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);