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_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_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_STATUS_ERROR_INTERNAL
, 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_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_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_STATUS_ERROR_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_STATUS_CONTINUE
&& request
->state
== IPP_STATE_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
))
255 != HTTP_STATUS_CONTINUE
)
261 * Get the server's response...
264 if (status
!= HTTP_STATUS_ERROR
)
266 response
= cupsGetResponse(http
, resource
);
267 status
= httpGetStatus(http
);
270 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
272 if (status
== HTTP_STATUS_ERROR
||
273 (status
>= HTTP_STATUS_BAD_REQUEST
&& status
!= HTTP_STATUS_UNAUTHORIZED
&&
274 status
!= HTTP_STATUS_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_STATE_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_STATE_POST_RECV
&&
362 http
->state
!= HTTP_STATE_POST_SEND
))
366 * Check for an unfinished chunked request...
369 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
372 * Send a 0-length chunk to finish off the request...
375 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
377 if (httpWrite2(http
, "", 0) < 0)
382 * Wait for a response from the server...
385 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
390 status
= httpUpdate(http
);
392 while (status
== HTTP_STATUS_CONTINUE
);
394 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
396 if (status
== HTTP_STATUS_OK
)
399 * Get the IPP response...
404 while ((state
= ippRead(http
, response
)) != IPP_STATE_DATA
)
405 if (state
== IPP_STATE_ERROR
)
408 if (state
== IPP_STATE_ERROR
)
411 * Flush remaining data and delete the response...
414 DEBUG_puts("1cupsGetResponse: IPP read error!");
421 http
->status
= status
= HTTP_STATUS_ERROR
;
422 http
->error
= EINVAL
;
425 else if (status
!= HTTP_STATUS_ERROR
)
428 * Flush any error message...
434 * Then handle encryption and authentication...
437 if (status
== HTTP_STATUS_UNAUTHORIZED
)
440 * See if we can do authentication...
443 DEBUG_puts("2cupsGetResponse: Need authorization...");
445 if (!cupsDoAuthentication(http
, "POST", resource
))
446 httpReconnect2(http
, 30000, NULL
);
448 http
->status
= status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
452 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
455 * Force a reconnect with encryption...
458 DEBUG_puts("2cupsGetResponse: Need encryption...");
460 if (!httpReconnect2(http
, 30000, NULL
))
461 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
463 #endif /* HAVE_SSL */
468 ipp_attribute_t
*attr
; /* status-message attribute */
471 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
473 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
474 ippErrorString(response
->request
.status
.status_code
),
475 attr
? attr
->values
[0].string
.text
: ""));
477 _cupsSetError(response
->request
.status
.status_code
,
478 attr
? attr
->values
[0].string
.text
:
479 ippErrorString(response
->request
.status
.status_code
), 0);
487 * 'cupsLastError()' - Return the last IPP status code received on the current
491 ipp_status_t
/* O - IPP status code from last request */
494 return (_cupsGlobals()->last_error
);
499 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
502 * @since CUPS 1.2/OS X 10.5@
505 const char * /* O - status-message text from last request */
506 cupsLastErrorString(void)
508 return (_cupsGlobals()->last_status_message
);
513 * '_cupsNextDelay()' - Return the next retry delay value.
515 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
517 * Pass 0 for the current delay value to initialize the sequence.
520 int /* O - Next delay value */
521 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
522 int *previous
) /* IO - Previous delay value */
524 int next
; /* Next delay value */
529 next
= (current
+ *previous
) % 12;
530 *previous
= next
< current
? 0 : current
;
543 * 'cupsReadResponseData()' - Read additional data after the IPP response.
545 * This function is used after @link cupsGetResponse@ to read the PPD or document
546 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
549 * @since CUPS 1.4/OS X 10.6@
552 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
553 cupsReadResponseData(
554 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
555 char *buffer
, /* I - Buffer to use */
556 size_t length
) /* I - Number of bytes to read */
559 * Get the default connection as needed...
562 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
563 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
567 _cups_globals_t
*cg
= _cupsGlobals();
568 /* Pointer to library globals */
570 if ((http
= cg
->http
) == NULL
)
572 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
578 * Then read from the HTTP connection...
581 return (httpRead2(http
, buffer
, length
));
586 * 'cupsSendRequest()' - Send an IPP request.
588 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
589 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
590 * and @link cupsReadResponseData@ to read any additional data following the
591 * response. Only one request can be sent/queued at a time per @code http_t@
594 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
595 * on a successful send of the request.
597 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
598 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
600 * @since CUPS 1.4/OS X 10.6@
603 http_status_t
/* O - Initial HTTP status */
604 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
605 ipp_t
*request
, /* I - IPP request */
606 const char *resource
, /* I - Resource path */
607 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
609 http_status_t status
; /* Status of HTTP request */
610 int got_status
; /* Did we get the status? */
611 ipp_state_t state
; /* State of IPP processing */
612 http_status_t expect
; /* Expect: header to use */
615 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
616 "length=" CUPS_LLFMT
")", http
, request
,
617 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
618 resource
, CUPS_LLCAST length
));
621 * Range check input...
624 if (!request
|| !resource
)
626 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
628 return (HTTP_STATUS_ERROR
);
632 * Get the default connection as needed...
636 if ((http
= _cupsConnect()) == NULL
)
637 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
640 * If the prior request was not flushed out, do so now...
643 if (http
->state
== HTTP_STATE_GET_SEND
||
644 http
->state
== HTTP_STATE_POST_SEND
)
646 DEBUG_puts("2cupsSendRequest: Flush prior response.");
649 else if (http
->state
!= HTTP_STATE_WAITING
)
651 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
652 "reconnecting.", http
->state
));
653 if (httpReconnect2(http
, 30000, NULL
))
654 return (HTTP_STATUS_ERROR
);
659 * See if we have an auth-info attribute and are communicating over
660 * a non-local link. If so, encrypt the link so that we can pass
661 * the authentication information securely...
664 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
665 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
666 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
668 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
669 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
671 #endif /* HAVE_SSL */
674 * Reconnect if the last response had a "Connection: close"...
677 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
679 DEBUG_puts("2cupsSendRequest: Connection: close");
680 httpClearFields(http
);
681 if (httpReconnect2(http
, 30000, NULL
))
683 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
684 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
689 * Loop until we can send the request without authorization problems.
692 expect
= HTTP_STATUS_CONTINUE
;
696 DEBUG_puts("2cupsSendRequest: Setup...");
699 * Setup the HTTP variables needed...
702 httpClearFields(http
);
703 httpSetExpect(http
, expect
);
704 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
705 httpSetLength(http
, length
);
708 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
711 * Do not use cached Kerberos credentials since they will look like a
715 _cupsSetNegotiateAuthString(http
, "POST", resource
);
717 #endif /* HAVE_GSSAPI */
719 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
721 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
727 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
729 if (httpPost(http
, resource
))
731 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
732 if (httpReconnect2(http
, 30000, NULL
))
734 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
735 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
742 * Send the IPP data...
745 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
747 request
->state
= IPP_STATE_IDLE
;
748 status
= HTTP_STATUS_CONTINUE
;
751 while ((state
= ippWrite(http
, request
)) != IPP_STATE_DATA
)
752 if (state
== IPP_STATE_ERROR
)
754 else if (httpCheck(http
))
758 _httpUpdate(http
, &status
);
759 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
763 if (state
== IPP_STATE_ERROR
)
765 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
767 http
->status
= HTTP_STATUS_ERROR
;
768 http
->state
= HTTP_STATE_WAITING
;
770 return (HTTP_STATUS_ERROR
);
774 * Wait up to 1 second to get the 100-continue response as needed...
779 if (expect
== HTTP_STATUS_CONTINUE
)
781 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
783 if (httpWait(http
, 1000))
784 _httpUpdate(http
, &status
);
786 else if (httpCheck(http
))
787 _httpUpdate(http
, &status
);
790 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
793 * Process the current HTTP status...
796 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
798 int temp_status
; /* Temporary status */
800 _cupsSetHTTPError(status
);
804 temp_status
= httpUpdate(http
);
806 while (temp_status
!= HTTP_STATUS_ERROR
&&
807 http
->state
== HTTP_STATE_POST_RECV
);
814 case HTTP_STATUS_CONTINUE
:
815 case HTTP_STATUS_OK
:
816 case HTTP_STATUS_ERROR
:
817 DEBUG_printf(("1cupsSendRequest: Returning %d.", status
));
820 case HTTP_STATUS_UNAUTHORIZED
:
821 if (cupsDoAuthentication(http
, "POST", resource
))
823 DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
824 return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
);
827 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
829 if (httpReconnect2(http
, 30000, NULL
))
831 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
832 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
837 case HTTP_STATUS_UPGRADE_REQUIRED
:
839 * Flush any error message, reconnect, and then upgrade with
843 DEBUG_puts("2cupsSendRequest: Reconnecting after "
844 "HTTP_STATUS_UPGRADE_REQUIRED.");
846 if (httpReconnect2(http
, 30000, NULL
))
848 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
849 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
852 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
853 if (httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
855 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
856 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
859 #endif /* HAVE_SSL */
861 case HTTP_STATUS_EXPECTATION_FAILED
:
863 * Don't try using the Expect: header the next time around...
866 expect
= (http_status_t
)0;
868 DEBUG_puts("2cupsSendRequest: Reconnecting after "
869 "HTTP_EXPECTATION_FAILED.");
871 if (httpReconnect2(http
, 30000, NULL
))
873 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
874 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
880 * Some other error...
890 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
892 * This function is used after @link cupsSendRequest@ to provide a PPD and
893 * after @link cupsStartDocument@ to provide a document file.
895 * @since CUPS 1.4/OS X 10.6@
898 http_status_t
/* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
899 cupsWriteRequestData(
900 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
901 const char *buffer
, /* I - Bytes to write */
902 size_t length
) /* I - Number of bytes to write */
904 int wused
; /* Previous bytes in buffer */
908 * Get the default connection as needed...
911 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
912 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
916 _cups_globals_t
*cg
= _cupsGlobals();
917 /* Pointer to library globals */
919 if ((http
= cg
->http
) == NULL
)
921 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
922 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
923 return (HTTP_STATUS_ERROR
);
928 * Then write to the HTTP connection...
933 if (httpWrite2(http
, buffer
, length
) < 0)
935 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
936 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(http
->error
), 0);
937 return (HTTP_STATUS_ERROR
);
941 * Finally, check if we have any pending data from the server...
944 if (length
>= HTTP_MAX_BUFFER
||
945 http
->wused
< wused
||
946 (wused
> 0 && http
->wused
== length
))
949 * We've written something to the server, so check for response data...
952 if (_httpWait(http
, 0, 1))
954 http_status_t status
; /* Status from _httpUpdate */
956 _httpUpdate(http
, &status
);
957 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
959 _cupsSetHTTPError(status
);
963 status
= httpUpdate(http
);
965 while (status
!= HTTP_STATUS_ERROR
&& http
->state
== HTTP_STATE_POST_RECV
);
970 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
975 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
976 return (HTTP_STATUS_CONTINUE
);
981 * '_cupsConnect()' - Get the default server connection...
984 http_t
* /* O - HTTP connection */
987 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
991 * See if we are connected to the same server...
997 * Compare the connection hostname, port, and encryption settings to
998 * the cached defaults; these were initialized the first time we
1002 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
1003 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
1004 (cg
->http
->encryption
!= cg
->encryption
&&
1005 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
1008 * Need to close the current connection because something has changed...
1011 httpClose(cg
->http
);
1017 * Same server, see if the connection is still established...
1020 char ch
; /* Connection check byte */
1023 if (recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
) < 0 &&
1024 WSAGetLastError() != WSAEWOULDBLOCK
)
1026 if (recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
) < 0 &&
1027 errno
!= EWOULDBLOCK
)
1031 * Nope, close the connection...
1034 httpClose(cg
->http
);
1041 * (Re)connect as needed...
1046 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
1047 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1050 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
1052 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
1053 _("Unable to connect to host."), 1);
1058 * Return the cached connection...
1066 * '_cupsSetError()' - Set the last IPP status code and status-message.
1070 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
1071 const char *message
, /* I - status-message value */
1072 int localize
) /* I - Localize the message? */
1074 _cups_globals_t
*cg
; /* Global data */
1077 if (!message
&& errno
)
1079 message
= strerror(errno
);
1083 cg
= _cupsGlobals();
1084 cg
->last_error
= status
;
1086 if (cg
->last_status_message
)
1088 _cupsStrFree(cg
->last_status_message
);
1090 cg
->last_status_message
= NULL
;
1098 * Get the message catalog...
1101 if (!cg
->lang_default
)
1102 cg
->lang_default
= cupsLangDefault();
1104 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
1108 cg
->last_status_message
= _cupsStrAlloc(message
);
1111 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1112 ippErrorString(cg
->last_error
), cg
->last_status_message
));
1117 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1121 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
1125 case HTTP_STATUS_NOT_FOUND
:
1126 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, httpStatus(status
), 0);
1129 case HTTP_STATUS_UNAUTHORIZED
:
1130 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1133 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
:
1134 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1137 case HTTP_STATUS_FORBIDDEN
:
1138 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN
, httpStatus(status
), 0);
1141 case HTTP_STATUS_BAD_REQUEST
:
1142 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST
, httpStatus(status
), 0);
1145 case HTTP_STATUS_REQUEST_TOO_LARGE
:
1146 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE
, httpStatus(status
), 0);
1149 case HTTP_STATUS_NOT_IMPLEMENTED
:
1150 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1153 case HTTP_STATUS_NOT_SUPPORTED
:
1154 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1157 case HTTP_STATUS_UPGRADE_REQUIRED
:
1158 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1161 case HTTP_STATUS_CUPS_PKI_ERROR
:
1162 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, httpStatus(status
), 0);
1165 case HTTP_STATUS_ERROR
:
1166 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1170 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1171 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status
));
1172 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);