4 * IPP utilities for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include "cups-private.h"
25 #if defined(WIN32) || defined(__EMX__)
29 #endif /* WIN32 || __EMX__ */
34 # define MSG_DONTWAIT 0
35 #endif /* !MSG_DONTWAIT */
39 * 'cupsDoFileRequest()' - Do an IPP request with a file.
41 * This function sends the IPP request and attached file to the specified
42 * server, retrying and authenticating as necessary. The request is freed with
46 ipp_t
* /* O - Response data */
47 cupsDoFileRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
48 ipp_t
*request
, /* I - IPP request */
49 const char *resource
, /* I - HTTP resource for POST */
50 const char *filename
) /* I - File to send or @code NULL@ for none */
52 ipp_t
*response
; /* IPP response data */
53 int infile
; /* Input file */
56 DEBUG_printf(("cupsDoFileRequest(http=%p, request=%p(%s), resource=\"%s\", "
57 "filename=\"%s\")", http
, request
,
58 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
63 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
66 * Can't get file information!
69 _cupsSetError(errno
== ENOENT
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
,
80 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
90 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
92 * This function sends the IPP request with the optional input file "infile" to
93 * the specified server, retrying and authenticating as necessary. The request
94 * is freed with @link ippDelete@.
96 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
97 * all of the data from the file after the IPP request message.
99 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
100 * all of the data after the IPP response message to the file.
102 * @since CUPS 1.3/OS X 10.5@
105 ipp_t
* /* O - Response data */
106 cupsDoIORequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
107 ipp_t
*request
, /* I - IPP request */
108 const char *resource
, /* I - HTTP resource for POST */
109 int infile
, /* I - File to read from or -1 for none */
110 int outfile
) /* I - File to write to or -1 for none */
112 ipp_t
*response
= NULL
; /* IPP response data */
113 size_t length
= 0; /* Content-Length value */
114 http_status_t status
; /* Status of HTTP request */
115 struct stat fileinfo
; /* File information */
116 ssize_t bytes
; /* Number of bytes read/written */
117 char buffer
[32768]; /* Output buffer */
120 DEBUG_printf(("cupsDoIORequest(http=%p, request=%p(%s), resource=\"%s\", "
121 "infile=%d, outfile=%d)", http
, request
,
122 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
123 resource
, infile
, outfile
));
126 * Range check input...
129 if (!request
|| !resource
)
133 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
139 * Get the default connection as needed...
143 if ((http
= _cupsConnect()) == NULL
)
151 * See if we have a file to send...
156 if (fstat(infile
, &fileinfo
))
159 * Can't get file information!
162 _cupsSetError(errno
== EBADF
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
,
171 if (fileinfo
.st_mode
& _S_IFDIR
)
173 if (S_ISDIR(fileinfo
.st_mode
))
177 * Can't send a directory...
182 _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE
, strerror(EISDIR
), 0);
188 if (!S_ISREG(fileinfo
.st_mode
))
189 length
= 0; /* Chunk when piping */
192 length
= ippLength(request
) + (size_t)fileinfo
.st_size
;
195 length
= ippLength(request
);
197 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld",
198 (long)ippLength(request
), (long)length
));
201 * Clear any "Local" authentication data since it is probably stale...
204 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
205 httpSetAuthString(http
, NULL
, NULL
);
208 * Loop until we can send the request without authorization problems.
211 while (response
== NULL
)
213 DEBUG_puts("2cupsDoIORequest: setup...");
216 * Send the request...
219 status
= cupsSendRequest(http
, request
, resource
, length
);
221 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
223 if (status
== HTTP_STATUS_CONTINUE
&& request
->state
== IPP_STATE_DATA
&& infile
>= 0)
225 DEBUG_puts("2cupsDoIORequest: file write...");
228 * Send the file with the request...
232 if (S_ISREG(fileinfo
.st_mode
))
234 lseek(infile
, 0, SEEK_SET
);
236 while ((bytes
= read(infile
, buffer
, sizeof(buffer
))) > 0)
238 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
))
239 != HTTP_STATUS_CONTINUE
)
245 * Get the server's response...
248 if (status
<= HTTP_STATUS_CONTINUE
|| status
== HTTP_STATUS_OK
)
250 response
= cupsGetResponse(http
, resource
);
251 status
= httpGetStatus(http
);
254 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
256 if (status
== HTTP_STATUS_ERROR
||
257 (status
>= HTTP_STATUS_BAD_REQUEST
&& status
!= HTTP_STATUS_UNAUTHORIZED
&&
258 status
!= HTTP_STATUS_UPGRADE_REQUIRED
))
260 _cupsSetHTTPError(status
);
264 if (response
&& outfile
>= 0)
267 * Write trailing data to file...
270 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
271 if (write(outfile
, buffer
, (size_t)bytes
) < bytes
)
275 if (http
->state
!= HTTP_STATE_WAITING
)
278 * Flush any remaining data...
286 * Delete the original request and return the response...
296 * 'cupsDoRequest()' - Do an IPP request.
298 * This function sends the IPP request to the specified server, retrying
299 * and authenticating as necessary. The request is freed with @link ippDelete@.
302 ipp_t
* /* O - Response data */
303 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
304 ipp_t
*request
, /* I - IPP request */
305 const char *resource
) /* I - HTTP resource for POST */
307 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")",
309 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
312 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
317 * 'cupsGetResponse()' - Get a response to an IPP request.
319 * Use this function to get the response for an IPP request sent using
320 * @link cupsSendRequest@. For requests that return additional data, use
321 * @link cupsReadResponseData@ after getting a successful response,
322 * otherwise call @link httpFlush@ to complete the response processing.
324 * @since CUPS 1.4/OS X 10.6@
327 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
328 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
329 const char *resource
) /* I - HTTP resource for POST */
331 http_status_t status
; /* HTTP status */
332 ipp_state_t state
; /* IPP read state */
333 ipp_t
*response
= NULL
; /* IPP response */
336 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", http
, resource
));
337 DEBUG_printf(("1cupsGetResponse: http->state=%d", http
? http
->state
: HTTP_STATE_ERROR
));
340 * Connect to the default server as needed...
345 _cups_globals_t
*cg
= _cupsGlobals();
346 /* Pointer to library globals */
348 if ((http
= cg
->http
) == NULL
)
350 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection."), 1);
351 DEBUG_puts("1cupsGetResponse: No active connection - returning NULL.");
356 if (http
->state
!= HTTP_STATE_POST_RECV
&& http
->state
!= HTTP_STATE_POST_SEND
)
358 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request sent."), 1);
359 DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL.");
364 * Check for an unfinished chunked request...
367 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
370 * Send a 0-length chunk to finish off the request...
373 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
375 if (httpWrite2(http
, "", 0) < 0)
380 * Wait for a response from the server...
383 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
388 status
= httpUpdate(http
);
390 while (status
== HTTP_STATUS_CONTINUE
);
392 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
394 if (status
== HTTP_STATUS_OK
)
397 * Get the IPP response...
402 while ((state
= ippRead(http
, response
)) != IPP_STATE_DATA
)
403 if (state
== IPP_STATE_ERROR
)
406 if (state
== IPP_STATE_ERROR
)
409 * Flush remaining data and delete the response...
412 DEBUG_puts("1cupsGetResponse: IPP read error!");
419 http
->status
= status
= HTTP_STATUS_ERROR
;
420 http
->error
= EINVAL
;
423 else if (status
!= HTTP_STATUS_ERROR
)
426 * Flush any error message...
432 * Then handle encryption and authentication...
435 if (status
== HTTP_STATUS_UNAUTHORIZED
)
438 * See if we can do authentication...
441 DEBUG_puts("2cupsGetResponse: Need authorization...");
443 if (!cupsDoAuthentication(http
, "POST", resource
))
444 httpReconnect2(http
, 30000, NULL
);
446 http
->status
= status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
450 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
453 * Force a reconnect with encryption...
456 DEBUG_puts("2cupsGetResponse: Need encryption...");
458 if (!httpReconnect2(http
, 30000, NULL
))
459 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
461 #endif /* HAVE_SSL */
466 ipp_attribute_t
*attr
; /* status-message attribute */
469 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
471 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
472 ippErrorString(response
->request
.status
.status_code
),
473 attr
? attr
->values
[0].string
.text
: ""));
475 _cupsSetError(response
->request
.status
.status_code
,
476 attr
? attr
->values
[0].string
.text
:
477 ippErrorString(response
->request
.status
.status_code
), 0);
485 * 'cupsLastError()' - Return the last IPP status code received on the current
489 ipp_status_t
/* O - IPP status code from last request */
492 return (_cupsGlobals()->last_error
);
497 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
500 * @since CUPS 1.2/OS X 10.5@
503 const char * /* O - status-message text from last request */
504 cupsLastErrorString(void)
506 return (_cupsGlobals()->last_status_message
);
511 * '_cupsNextDelay()' - Return the next retry delay value.
513 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
515 * Pass 0 for the current delay value to initialize the sequence.
518 int /* O - Next delay value */
519 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
520 int *previous
) /* IO - Previous delay value */
522 int next
; /* Next delay value */
527 next
= (current
+ *previous
) % 12;
528 *previous
= next
< current
? 0 : current
;
541 * 'cupsReadResponseData()' - Read additional data after the IPP response.
543 * This function is used after @link cupsGetResponse@ to read the PPD or document
544 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
547 * @since CUPS 1.4/OS X 10.6@
550 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
551 cupsReadResponseData(
552 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
553 char *buffer
, /* I - Buffer to use */
554 size_t length
) /* I - Number of bytes to read */
557 * Get the default connection as needed...
560 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, "
561 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
565 _cups_globals_t
*cg
= _cupsGlobals();
566 /* Pointer to library globals */
568 if ((http
= cg
->http
) == NULL
)
570 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
576 * Then read from the HTTP connection...
579 return (httpRead2(http
, buffer
, length
));
584 * 'cupsSendRequest()' - Send an IPP request.
586 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
587 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
588 * and @link cupsReadResponseData@ to read any additional data following the
589 * response. Only one request can be sent/queued at a time per @code http_t@
592 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
593 * on a successful send of the request.
595 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
596 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
598 * @since CUPS 1.4/OS X 10.6@
601 http_status_t
/* O - Initial HTTP status */
602 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
603 ipp_t
*request
, /* I - IPP request */
604 const char *resource
, /* I - Resource path */
605 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
607 http_status_t status
; /* Status of HTTP request */
608 int got_status
; /* Did we get the status? */
609 ipp_state_t state
; /* State of IPP processing */
610 http_status_t expect
; /* Expect: header to use */
613 DEBUG_printf(("cupsSendRequest(http=%p, request=%p(%s), resource=\"%s\", "
614 "length=" CUPS_LLFMT
")", http
, request
,
615 request
? ippOpString(request
->request
.op
.operation_id
) : "?",
616 resource
, CUPS_LLCAST length
));
619 * Range check input...
622 if (!request
|| !resource
)
624 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
626 return (HTTP_STATUS_ERROR
);
630 * Get the default connection as needed...
634 if ((http
= _cupsConnect()) == NULL
)
635 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
638 * If the prior request was not flushed out, do so now...
641 if (http
->state
== HTTP_STATE_GET_SEND
||
642 http
->state
== HTTP_STATE_POST_SEND
)
644 DEBUG_puts("2cupsSendRequest: Flush prior response.");
647 else if (http
->state
!= HTTP_STATE_WAITING
)
649 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
650 "reconnecting.", http
->state
));
651 if (httpReconnect2(http
, 30000, NULL
))
652 return (HTTP_STATUS_ERROR
);
657 * See if we have an auth-info attribute and are communicating over
658 * a non-local link. If so, encrypt the link so that we can pass
659 * the authentication information securely...
662 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
663 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
664 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
666 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
667 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
669 #endif /* HAVE_SSL */
672 * Reconnect if the last response had a "Connection: close"...
675 if (!_cups_strcasecmp(http
->fields
[HTTP_FIELD_CONNECTION
], "close"))
677 DEBUG_puts("2cupsSendRequest: Connection: close");
678 httpClearFields(http
);
679 if (httpReconnect2(http
, 30000, NULL
))
681 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
682 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
687 * Loop until we can send the request without authorization problems.
690 expect
= HTTP_STATUS_CONTINUE
;
694 DEBUG_puts("2cupsSendRequest: Setup...");
697 * Setup the HTTP variables needed...
700 httpClearFields(http
);
701 httpSetExpect(http
, expect
);
702 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
703 httpSetLength(http
, length
);
706 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
709 * Do not use cached Kerberos credentials since they will look like a
713 _cupsSetNegotiateAuthString(http
, "POST", resource
);
715 #endif /* HAVE_GSSAPI */
717 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
719 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
725 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
727 if (httpPost(http
, resource
))
729 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
730 if (httpReconnect2(http
, 30000, NULL
))
732 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
733 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
740 * Send the IPP data...
743 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
745 request
->state
= IPP_STATE_IDLE
;
746 status
= HTTP_STATUS_CONTINUE
;
749 while ((state
= ippWrite(http
, request
)) != IPP_STATE_DATA
)
755 _httpUpdate(http
, &status
);
756 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
759 else if (state
== IPP_STATE_ERROR
)
763 if (state
== IPP_STATE_ERROR
)
766 * We weren't able to send the IPP request. But did we already get a HTTP
770 if (!got_status
|| status
< HTTP_STATUS_MULTIPLE_CHOICES
)
773 * No, something else went wrong.
776 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
778 http
->status
= HTTP_STATUS_ERROR
;
779 http
->state
= HTTP_STATE_WAITING
;
781 return (HTTP_STATUS_ERROR
);
786 * Wait up to 1 second to get the 100-continue response as needed...
791 if (expect
== HTTP_STATUS_CONTINUE
)
793 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
795 if (httpWait(http
, 1000))
796 _httpUpdate(http
, &status
);
798 else if (httpCheck(http
))
799 _httpUpdate(http
, &status
);
802 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
805 * Process the current HTTP status...
808 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
810 int temp_status
; /* Temporary status */
812 _cupsSetHTTPError(status
);
816 temp_status
= httpUpdate(http
);
818 while (temp_status
!= HTTP_STATUS_ERROR
&&
819 http
->state
== HTTP_STATE_POST_RECV
);
826 case HTTP_STATUS_CONTINUE
:
827 case HTTP_STATUS_OK
:
828 case HTTP_STATUS_ERROR
:
829 DEBUG_printf(("1cupsSendRequest: Returning %d.", status
));
832 case HTTP_STATUS_UNAUTHORIZED
:
833 if (cupsDoAuthentication(http
, "POST", resource
))
835 DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
836 return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
);
839 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
841 if (httpReconnect2(http
, 30000, NULL
))
843 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
844 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
849 case HTTP_STATUS_UPGRADE_REQUIRED
:
851 * Flush any error message, reconnect, and then upgrade with
855 DEBUG_puts("2cupsSendRequest: Reconnecting after "
856 "HTTP_STATUS_UPGRADE_REQUIRED.");
858 if (httpReconnect2(http
, 30000, NULL
))
860 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
861 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
864 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
865 if (httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
867 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
868 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
871 #endif /* HAVE_SSL */
873 case HTTP_STATUS_EXPECTATION_FAILED
:
875 * Don't try using the Expect: header the next time around...
878 expect
= (http_status_t
)0;
880 DEBUG_puts("2cupsSendRequest: Reconnecting after "
881 "HTTP_EXPECTATION_FAILED.");
883 if (httpReconnect2(http
, 30000, NULL
))
885 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
886 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
892 * Some other error...
902 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
904 * This function is used after @link cupsSendRequest@ to provide a PPD and
905 * after @link cupsStartDocument@ to provide a document file.
907 * @since CUPS 1.4/OS X 10.6@
910 http_status_t
/* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
911 cupsWriteRequestData(
912 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
913 const char *buffer
, /* I - Bytes to write */
914 size_t length
) /* I - Number of bytes to write */
916 int wused
; /* Previous bytes in buffer */
920 * Get the default connection as needed...
923 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, "
924 "length=" CUPS_LLFMT
")", http
, buffer
, CUPS_LLCAST length
));
928 _cups_globals_t
*cg
= _cupsGlobals();
929 /* Pointer to library globals */
931 if ((http
= cg
->http
) == NULL
)
933 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
934 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
935 return (HTTP_STATUS_ERROR
);
940 * Then write to the HTTP connection...
945 if (httpWrite2(http
, buffer
, length
) < 0)
947 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
948 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(http
->error
), 0);
949 return (HTTP_STATUS_ERROR
);
953 * Finally, check if we have any pending data from the server...
956 if (length
>= HTTP_MAX_BUFFER
||
957 http
->wused
< wused
||
958 (wused
> 0 && (size_t)http
->wused
== length
))
961 * We've written something to the server, so check for response data...
964 if (_httpWait(http
, 0, 1))
966 http_status_t status
; /* Status from _httpUpdate */
968 _httpUpdate(http
, &status
);
969 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
971 _cupsSetHTTPError(status
);
975 status
= httpUpdate(http
);
977 while (status
!= HTTP_STATUS_ERROR
&& http
->state
== HTTP_STATE_POST_RECV
);
982 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
987 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
988 return (HTTP_STATUS_CONTINUE
);
993 * '_cupsConnect()' - Get the default server connection...
996 http_t
* /* O - HTTP connection */
999 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
1003 * See if we are connected to the same server...
1009 * Compare the connection hostname, port, and encryption settings to
1010 * the cached defaults; these were initialized the first time we
1014 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
1015 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
1016 (cg
->http
->encryption
!= cg
->encryption
&&
1017 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
1020 * Need to close the current connection because something has changed...
1023 httpClose(cg
->http
);
1029 * Same server, see if the connection is still established...
1032 char ch
; /* Connection check byte */
1033 ssize_t n
; /* Number of bytes */
1036 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
)) == 0 ||
1037 (n
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
))
1039 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
)) == 0 ||
1040 (n
< 0 && errno
!= EWOULDBLOCK
))
1044 * Nope, close the connection...
1047 httpClose(cg
->http
);
1054 * (Re)connect as needed...
1059 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
1060 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1063 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
1065 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
1066 _("Unable to connect to host."), 1);
1071 * Return the cached connection...
1079 * '_cupsSetError()' - Set the last IPP status code and status-message.
1083 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
1084 const char *message
, /* I - status-message value */
1085 int localize
) /* I - Localize the message? */
1087 _cups_globals_t
*cg
; /* Global data */
1090 if (!message
&& errno
)
1092 message
= strerror(errno
);
1096 cg
= _cupsGlobals();
1097 cg
->last_error
= status
;
1099 if (cg
->last_status_message
)
1101 _cupsStrFree(cg
->last_status_message
);
1103 cg
->last_status_message
= NULL
;
1111 * Get the message catalog...
1114 if (!cg
->lang_default
)
1115 cg
->lang_default
= cupsLangDefault();
1117 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
1121 cg
->last_status_message
= _cupsStrAlloc(message
);
1124 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1125 ippErrorString(cg
->last_error
), cg
->last_status_message
));
1130 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1134 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
1138 case HTTP_STATUS_NOT_FOUND
:
1139 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, httpStatus(status
), 0);
1142 case HTTP_STATUS_UNAUTHORIZED
:
1143 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1146 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
:
1147 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1150 case HTTP_STATUS_FORBIDDEN
:
1151 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN
, httpStatus(status
), 0);
1154 case HTTP_STATUS_BAD_REQUEST
:
1155 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST
, httpStatus(status
), 0);
1158 case HTTP_STATUS_REQUEST_TOO_LARGE
:
1159 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE
, httpStatus(status
), 0);
1162 case HTTP_STATUS_NOT_IMPLEMENTED
:
1163 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1166 case HTTP_STATUS_NOT_SUPPORTED
:
1167 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1170 case HTTP_STATUS_UPGRADE_REQUIRED
:
1171 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1174 case HTTP_STATUS_CUPS_PKI_ERROR
:
1175 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, httpStatus(status
), 0);
1178 case HTTP_STATUS_ERROR
:
1179 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1183 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1184 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status
));
1185 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);