2 * IPP utilities for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include "cups-private.h"
17 #if defined(WIN32) || defined(__EMX__)
21 #endif /* WIN32 || __EMX__ */
26 # define MSG_DONTWAIT 0
27 #endif /* !MSG_DONTWAIT */
31 * 'cupsDoFileRequest()' - Do an IPP request with a file.
33 * This function sends the IPP request and attached file to the specified
34 * server, retrying and authenticating as necessary. The request is freed with
38 ipp_t
* /* O - Response data */
39 cupsDoFileRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
40 ipp_t
*request
, /* I - IPP request */
41 const char *resource
, /* I - HTTP resource for POST */
42 const char *filename
) /* I - File to send or @code NULL@ for none */
44 ipp_t
*response
; /* IPP response data */
45 int infile
; /* Input file */
48 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
));
52 if ((infile
= open(filename
, O_RDONLY
| O_BINARY
)) < 0)
55 * Can't get file information!
58 _cupsSetError(errno
== ENOENT
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
,
69 response
= cupsDoIORequest(http
, request
, resource
, infile
, -1);
79 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
81 * This function sends the IPP request with the optional input file "infile" to
82 * the specified server, retrying and authenticating as necessary. The request
83 * is freed with @link ippDelete@.
85 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
86 * all of the data from the file after the IPP request message.
88 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
89 * all of the data after the IPP response message to the file.
91 * @since CUPS 1.3/macOS 10.5@
94 ipp_t
* /* O - Response data */
95 cupsDoIORequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
96 ipp_t
*request
, /* I - IPP request */
97 const char *resource
, /* I - HTTP resource for POST */
98 int infile
, /* I - File to read from or -1 for none */
99 int outfile
) /* I - File to write to or -1 for none */
101 ipp_t
*response
= NULL
; /* IPP response data */
102 size_t length
= 0; /* Content-Length value */
103 http_status_t status
; /* Status of HTTP request */
104 struct stat fileinfo
; /* File information */
105 ssize_t bytes
; /* Number of bytes read/written */
106 char buffer
[32768]; /* Output buffer */
109 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
));
112 * Range check input...
115 if (!request
|| !resource
)
119 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
125 * Get the default connection as needed...
128 if (!http
&& (http
= _cupsConnect()) == NULL
)
136 * See if we have a file to send...
141 if (fstat(infile
, &fileinfo
))
144 * Can't get file information!
147 _cupsSetError(errno
== EBADF
? IPP_STATUS_ERROR_NOT_FOUND
: IPP_STATUS_ERROR_NOT_AUTHORIZED
, NULL
, 0);
154 if (fileinfo
.st_mode
& _S_IFDIR
)
156 if (S_ISDIR(fileinfo
.st_mode
))
160 * Can't send a directory...
163 _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE
, strerror(EISDIR
), 0);
170 if (!S_ISREG(fileinfo
.st_mode
))
171 length
= 0; /* Chunk when piping */
174 length
= ippLength(request
) + (size_t)fileinfo
.st_size
;
177 length
= ippLength(request
);
179 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld", (long)ippLength(request
), (long)length
));
182 * Clear any "Local" authentication data since it is probably stale...
185 if (http
->authstring
&& !strncmp(http
->authstring
, "Local ", 6))
186 httpSetAuthString(http
, NULL
, NULL
);
189 * Loop until we can send the request without authorization problems.
192 while (response
== NULL
)
194 DEBUG_puts("2cupsDoIORequest: setup...");
197 * Send the request...
200 status
= cupsSendRequest(http
, request
, resource
, length
);
202 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
204 if (status
== HTTP_STATUS_CONTINUE
&& request
->state
== IPP_STATE_DATA
&& infile
>= 0)
206 DEBUG_puts("2cupsDoIORequest: file write...");
209 * Send the file with the request...
213 if (S_ISREG(fileinfo
.st_mode
))
215 lseek(infile
, 0, SEEK_SET
);
217 while ((bytes
= read(infile
, buffer
, sizeof(buffer
))) > 0)
219 if ((status
= cupsWriteRequestData(http
, buffer
, (size_t)bytes
))
220 != HTTP_STATUS_CONTINUE
)
226 * Get the server's response...
229 if (status
<= HTTP_STATUS_CONTINUE
|| status
== HTTP_STATUS_OK
)
231 response
= cupsGetResponse(http
, resource
);
232 status
= httpGetStatus(http
);
235 DEBUG_printf(("2cupsDoIORequest: status=%d", status
));
237 if (status
== HTTP_STATUS_ERROR
||
238 (status
>= HTTP_STATUS_BAD_REQUEST
&& status
!= HTTP_STATUS_UNAUTHORIZED
&&
239 status
!= HTTP_STATUS_UPGRADE_REQUIRED
))
241 _cupsSetHTTPError(status
);
245 if (response
&& outfile
>= 0)
248 * Write trailing data to file...
251 while ((bytes
= httpRead2(http
, buffer
, sizeof(buffer
))) > 0)
252 if (write(outfile
, buffer
, (size_t)bytes
) < bytes
)
256 if (http
->state
!= HTTP_STATE_WAITING
)
259 * Flush any remaining data...
267 * Delete the original request and return the response...
277 * 'cupsDoRequest()' - Do an IPP request.
279 * This function sends the IPP request to the specified server, retrying
280 * and authenticating as necessary. The request is freed with @link ippDelete@.
283 ipp_t
* /* O - Response data */
284 cupsDoRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
285 ipp_t
*request
, /* I - IPP request */
286 const char *resource
) /* I - HTTP resource for POST */
288 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", (void *)http
, (void *)request
, request
? ippOpString(request
->request
.op
.operation_id
) : "?", resource
));
290 return (cupsDoIORequest(http
, request
, resource
, -1, -1));
295 * 'cupsGetResponse()' - Get a response to an IPP request.
297 * Use this function to get the response for an IPP request sent using
298 * @link cupsSendRequest@. For requests that return additional data, use
299 * @link cupsReadResponseData@ after getting a successful response,
300 * otherwise call @link httpFlush@ to complete the response processing.
302 * @since CUPS 1.4/macOS 10.6@
305 ipp_t
* /* O - Response or @code NULL@ on HTTP error */
306 cupsGetResponse(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
307 const char *resource
) /* I - HTTP resource for POST */
309 http_status_t status
; /* HTTP status */
310 ipp_state_t state
; /* IPP read state */
311 ipp_t
*response
= NULL
; /* IPP response */
314 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", (void *)http
, resource
));
315 DEBUG_printf(("1cupsGetResponse: http->state=%d", http
? http
->state
: HTTP_STATE_ERROR
));
318 * Connect to the default server as needed...
323 _cups_globals_t
*cg
= _cupsGlobals();
324 /* Pointer to library globals */
326 if ((http
= cg
->http
) == NULL
)
328 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection."), 1);
329 DEBUG_puts("1cupsGetResponse: No active connection - returning NULL.");
334 if (http
->state
!= HTTP_STATE_POST_RECV
&& http
->state
!= HTTP_STATE_POST_SEND
)
336 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No request sent."), 1);
337 DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL.");
342 * Check for an unfinished chunked request...
345 if (http
->data_encoding
== HTTP_ENCODING_CHUNKED
)
348 * Send a 0-length chunk to finish off the request...
351 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
353 if (httpWrite2(http
, "", 0) < 0)
358 * Wait for a response from the server...
361 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
366 status
= httpUpdate(http
);
368 while (status
== HTTP_STATUS_CONTINUE
);
370 DEBUG_printf(("2cupsGetResponse: status=%d", status
));
372 if (status
== HTTP_STATUS_OK
)
375 * Get the IPP response...
380 while ((state
= ippRead(http
, response
)) != IPP_STATE_DATA
)
381 if (state
== IPP_STATE_ERROR
)
384 if (state
== IPP_STATE_ERROR
)
387 * Flush remaining data and delete the response...
390 DEBUG_puts("1cupsGetResponse: IPP read error!");
397 http
->status
= status
= HTTP_STATUS_ERROR
;
398 http
->error
= EINVAL
;
401 else if (status
!= HTTP_STATUS_ERROR
)
404 * Flush any error message...
410 * Then handle encryption and authentication...
413 if (status
== HTTP_STATUS_UNAUTHORIZED
)
416 * See if we can do authentication...
419 DEBUG_puts("2cupsGetResponse: Need authorization...");
421 if (!cupsDoAuthentication(http
, "POST", resource
))
422 httpReconnect2(http
, 30000, NULL
);
424 http
->status
= status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
428 else if (status
== HTTP_STATUS_UPGRADE_REQUIRED
)
431 * Force a reconnect with encryption...
434 DEBUG_puts("2cupsGetResponse: Need encryption...");
436 if (!httpReconnect2(http
, 30000, NULL
))
437 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
);
439 #endif /* HAVE_SSL */
444 ipp_attribute_t
*attr
; /* status-message attribute */
447 attr
= ippFindAttribute(response
, "status-message", IPP_TAG_TEXT
);
449 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
450 ippErrorString(response
->request
.status
.status_code
),
451 attr
? attr
->values
[0].string
.text
: ""));
453 _cupsSetError(response
->request
.status
.status_code
,
454 attr
? attr
->values
[0].string
.text
:
455 ippErrorString(response
->request
.status
.status_code
), 0);
463 * 'cupsLastError()' - Return the last IPP status code received on the current
467 ipp_status_t
/* O - IPP status code from last request */
470 return (_cupsGlobals()->last_error
);
475 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
478 * @since CUPS 1.2/macOS 10.5@
481 const char * /* O - status-message text from last request */
482 cupsLastErrorString(void)
484 return (_cupsGlobals()->last_status_message
);
489 * '_cupsNextDelay()' - Return the next retry delay value.
491 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
493 * Pass 0 for the current delay value to initialize the sequence.
496 int /* O - Next delay value */
497 _cupsNextDelay(int current
, /* I - Current delay value or 0 */
498 int *previous
) /* IO - Previous delay value */
500 int next
; /* Next delay value */
505 next
= (current
+ *previous
) % 12;
506 *previous
= next
< current
? 0 : current
;
519 * 'cupsReadResponseData()' - Read additional data after the IPP response.
521 * This function is used after @link cupsGetResponse@ to read the PPD or document
522 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
525 * @since CUPS 1.4/macOS 10.6@
528 ssize_t
/* O - Bytes read, 0 on EOF, -1 on error */
529 cupsReadResponseData(
530 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
531 char *buffer
, /* I - Buffer to use */
532 size_t length
) /* I - Number of bytes to read */
535 * Get the default connection as needed...
538 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
542 _cups_globals_t
*cg
= _cupsGlobals();
543 /* Pointer to library globals */
545 if ((http
= cg
->http
) == NULL
)
547 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
553 * Then read from the HTTP connection...
556 return (httpRead2(http
, buffer
, length
));
561 * 'cupsSendRequest()' - Send an IPP request.
563 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
564 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
565 * and @link cupsReadResponseData@ to read any additional data following the
566 * response. Only one request can be sent/queued at a time per @code http_t@
569 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
570 * on a successful send of the request.
572 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
573 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
575 * @since CUPS 1.4/macOS 10.6@
578 http_status_t
/* O - Initial HTTP status */
579 cupsSendRequest(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
580 ipp_t
*request
, /* I - IPP request */
581 const char *resource
, /* I - Resource path */
582 size_t length
) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
584 http_status_t status
; /* Status of HTTP request */
585 int got_status
; /* Did we get the status? */
586 ipp_state_t state
; /* State of IPP processing */
587 http_status_t expect
; /* Expect: header to use */
588 char date
[256]; /* Date: header value */
589 int digest
; /* Are we using Digest authentication? */
592 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
));
595 * Range check input...
598 if (!request
|| !resource
)
600 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
602 return (HTTP_STATUS_ERROR
);
606 * Get the default connection as needed...
609 if (!http
&& (http
= _cupsConnect()) == NULL
)
610 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
613 * If the prior request was not flushed out, do so now...
616 if (http
->state
== HTTP_STATE_GET_SEND
||
617 http
->state
== HTTP_STATE_POST_SEND
)
619 DEBUG_puts("2cupsSendRequest: Flush prior response.");
622 else if (http
->state
!= HTTP_STATE_WAITING
)
624 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
625 "reconnecting.", http
->state
));
626 if (httpReconnect2(http
, 30000, NULL
))
627 return (HTTP_STATUS_ERROR
);
632 * See if we have an auth-info attribute and are communicating over
633 * a non-local link. If so, encrypt the link so that we can pass
634 * the authentication information securely...
637 if (ippFindAttribute(request
, "auth-info", IPP_TAG_TEXT
) &&
638 !httpAddrLocalhost(http
->hostaddr
) && !http
->tls
&&
639 httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
641 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
642 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
644 #endif /* HAVE_SSL */
647 * Reconnect if the last response had a "Connection: close"...
650 if (!_cups_strcasecmp(httpGetField(http
, HTTP_FIELD_CONNECTION
), "close"))
652 DEBUG_puts("2cupsSendRequest: Connection: close");
653 httpClearFields(http
);
654 if (httpReconnect2(http
, 30000, NULL
))
656 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
657 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
662 * Loop until we can send the request without authorization problems.
665 expect
= HTTP_STATUS_CONTINUE
;
669 DEBUG_puts("2cupsSendRequest: Setup...");
672 * Setup the HTTP variables needed...
675 httpClearFields(http
);
676 httpSetExpect(http
, expect
);
677 httpSetField(http
, HTTP_FIELD_CONTENT_TYPE
, "application/ipp");
678 httpSetField(http
, HTTP_FIELD_DATE
, httpGetDateString2(time(NULL
), date
, (int)sizeof(date
)));
679 httpSetLength(http
, length
);
681 digest
= http
->authstring
&& !strncmp(http
->authstring
, "Digest ", 7);
686 * Update the Digest authentication string...
689 if (http
->nextnonce
[0])
691 strlcpy(http
->nonce
, http
->nextnonce
, sizeof(http
->nonce
));
692 http
->nonce_count
= 1;
693 http
->nextnonce
[0] = '\0';
696 http
->nonce_count
++;
698 _httpSetDigestAuthString(http
, "POST", resource
);
702 if (http
->authstring
&& !strncmp(http
->authstring
, "Negotiate", 9))
705 * Do not use cached Kerberos credentials since they will look like a
709 _cupsSetNegotiateAuthString(http
, "POST", resource
);
711 #endif /* HAVE_GSSAPI */
713 httpSetField(http
, HTTP_FIELD_AUTHORIZATION
, http
->authstring
);
715 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http
->authstring
));
721 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
723 if (httpPost(http
, resource
))
725 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
726 if (httpReconnect2(http
, 30000, NULL
))
728 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
729 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
736 * Send the IPP data...
739 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
741 request
->state
= IPP_STATE_IDLE
;
742 status
= HTTP_STATUS_CONTINUE
;
745 while ((state
= ippWrite(http
, request
)) != IPP_STATE_DATA
)
751 _httpUpdate(http
, &status
);
752 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
755 else if (state
== IPP_STATE_ERROR
)
759 if (state
== IPP_STATE_ERROR
)
762 * We weren't able to send the IPP request. But did we already get a HTTP
766 if (!got_status
|| status
< HTTP_STATUS_MULTIPLE_CHOICES
)
769 * No, something else went wrong.
772 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
774 http
->status
= HTTP_STATUS_ERROR
;
775 http
->state
= HTTP_STATE_WAITING
;
777 return (HTTP_STATUS_ERROR
);
782 * Wait up to 1 second to get the 100-continue response as needed...
785 if (!got_status
|| (digest
&& status
== HTTP_STATUS_CONTINUE
))
787 if (expect
== HTTP_STATUS_CONTINUE
|| digest
)
789 DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
791 if (httpWait(http
, 1000))
792 _httpUpdate(http
, &status
);
794 else if (httpCheck(http
))
795 _httpUpdate(http
, &status
);
798 DEBUG_printf(("2cupsSendRequest: status=%d", status
));
801 * Process the current HTTP status...
804 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
806 int temp_status
; /* Temporary status */
808 _cupsSetHTTPError(status
);
812 temp_status
= httpUpdate(http
);
814 while (temp_status
!= HTTP_STATUS_ERROR
&&
815 http
->state
== HTTP_STATE_POST_RECV
);
822 case HTTP_STATUS_CONTINUE
:
823 case HTTP_STATUS_OK
:
824 case HTTP_STATUS_ERROR
:
825 DEBUG_printf(("1cupsSendRequest: Returning %d.", status
));
828 case HTTP_STATUS_UNAUTHORIZED
:
829 if (cupsDoAuthentication(http
, "POST", resource
))
831 DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
832 return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
);
835 DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
837 if (httpReconnect2(http
, 30000, NULL
))
839 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
840 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
845 case HTTP_STATUS_UPGRADE_REQUIRED
:
847 * Flush any error message, reconnect, and then upgrade with
851 DEBUG_puts("2cupsSendRequest: Reconnecting after "
852 "HTTP_STATUS_UPGRADE_REQUIRED.");
854 if (httpReconnect2(http
, 30000, NULL
))
856 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
857 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
860 DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
861 if (httpEncryption(http
, HTTP_ENCRYPTION_REQUIRED
))
863 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
864 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
867 #endif /* HAVE_SSL */
869 case HTTP_STATUS_EXPECTATION_FAILED
:
871 * Don't try using the Expect: header the next time around...
874 expect
= (http_status_t
)0;
876 DEBUG_puts("2cupsSendRequest: Reconnecting after "
877 "HTTP_EXPECTATION_FAILED.");
879 if (httpReconnect2(http
, 30000, NULL
))
881 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
882 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
888 * Some other error...
898 * 'cupsWriteRequestData()' - Write additional data after an IPP request.
900 * This function is used after @link cupsSendRequest@ to provide a PPD and
901 * after @link cupsStartDocument@ to provide a document file.
903 * @since CUPS 1.4/macOS 10.6@
906 http_status_t
/* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
907 cupsWriteRequestData(
908 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
909 const char *buffer
, /* I - Bytes to write */
910 size_t length
) /* I - Number of bytes to write */
912 int wused
; /* Previous bytes in buffer */
916 * Get the default connection as needed...
919 DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, length=" CUPS_LLFMT
")", (void *)http
, (void *)buffer
, CUPS_LLCAST length
));
923 _cups_globals_t
*cg
= _cupsGlobals();
924 /* Pointer to library globals */
926 if ((http
= cg
->http
) == NULL
)
928 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No active connection"), 1);
929 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
930 return (HTTP_STATUS_ERROR
);
935 * Then write to the HTTP connection...
940 if (httpWrite2(http
, buffer
, length
) < 0)
942 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
943 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(http
->error
), 0);
944 return (HTTP_STATUS_ERROR
);
948 * Finally, check if we have any pending data from the server...
951 if (length
>= HTTP_MAX_BUFFER
||
952 http
->wused
< wused
||
953 (wused
> 0 && (size_t)http
->wused
== length
))
956 * We've written something to the server, so check for response data...
959 if (_httpWait(http
, 0, 1))
961 http_status_t status
; /* Status from _httpUpdate */
963 _httpUpdate(http
, &status
);
964 if (status
>= HTTP_STATUS_MULTIPLE_CHOICES
)
966 _cupsSetHTTPError(status
);
970 status
= httpUpdate(http
);
972 while (status
!= HTTP_STATUS_ERROR
&& http
->state
== HTTP_STATE_POST_RECV
);
977 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status
));
982 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
983 return (HTTP_STATUS_CONTINUE
);
988 * '_cupsConnect()' - Get the default server connection...
991 http_t
* /* O - HTTP connection */
994 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
998 * See if we are connected to the same server...
1004 * Compare the connection hostname, port, and encryption settings to
1005 * the cached defaults; these were initialized the first time we
1009 if (strcmp(cg
->http
->hostname
, cg
->server
) ||
1011 (httpAddrFamily(cg
->http
->hostaddr
) != AF_LOCAL
&& cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
)) ||
1013 cg
->ipp_port
!= httpAddrPort(cg
->http
->hostaddr
) ||
1014 #endif /* AF_LOCAL */
1015 (cg
->http
->encryption
!= cg
->encryption
&&
1016 cg
->http
->encryption
== HTTP_ENCRYPTION_NEVER
))
1019 * Need to close the current connection because something has changed...
1022 httpClose(cg
->http
);
1028 * Same server, see if the connection is still established...
1031 char ch
; /* Connection check byte */
1032 ssize_t n
; /* Number of bytes */
1035 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
)) == 0 ||
1036 (n
< 0 && WSAGetLastError() != WSAEWOULDBLOCK
))
1038 if ((n
= recv(cg
->http
->fd
, &ch
, 1, MSG_PEEK
| MSG_DONTWAIT
)) == 0 ||
1039 (n
< 0 && errno
!= EWOULDBLOCK
))
1043 * Nope, close the connection...
1046 httpClose(cg
->http
);
1053 * (Re)connect as needed...
1058 if ((cg
->http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
,
1059 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1062 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, NULL
, 0);
1064 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
,
1065 _("Unable to connect to host."), 1);
1070 * Return the cached connection...
1078 * '_cupsSetError()' - Set the last IPP status code and status-message.
1082 _cupsSetError(ipp_status_t status
, /* I - IPP status code */
1083 const char *message
, /* I - status-message value */
1084 int localize
) /* I - Localize the message? */
1086 _cups_globals_t
*cg
; /* Global data */
1089 if (!message
&& errno
)
1091 message
= strerror(errno
);
1095 cg
= _cupsGlobals();
1096 cg
->last_error
= status
;
1098 if (cg
->last_status_message
)
1100 _cupsStrFree(cg
->last_status_message
);
1102 cg
->last_status_message
= NULL
;
1110 * Get the message catalog...
1113 if (!cg
->lang_default
)
1114 cg
->lang_default
= cupsLangDefault();
1116 cg
->last_status_message
= _cupsStrAlloc(_cupsLangString(cg
->lang_default
,
1120 cg
->last_status_message
= _cupsStrAlloc(message
);
1123 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1124 ippErrorString(cg
->last_error
), cg
->last_status_message
));
1129 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1133 _cupsSetHTTPError(http_status_t status
) /* I - HTTP status code */
1137 case HTTP_STATUS_NOT_FOUND
:
1138 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND
, httpStatus(status
), 0);
1141 case HTTP_STATUS_UNAUTHORIZED
:
1142 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED
, httpStatus(status
), 0);
1145 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
:
1146 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED
, httpStatus(status
), 0);
1149 case HTTP_STATUS_FORBIDDEN
:
1150 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN
, httpStatus(status
), 0);
1153 case HTTP_STATUS_BAD_REQUEST
:
1154 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST
, httpStatus(status
), 0);
1157 case HTTP_STATUS_REQUEST_TOO_LARGE
:
1158 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE
, httpStatus(status
), 0);
1161 case HTTP_STATUS_NOT_IMPLEMENTED
:
1162 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED
, httpStatus(status
), 0);
1165 case HTTP_STATUS_NOT_SUPPORTED
:
1166 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
, httpStatus(status
), 0);
1169 case HTTP_STATUS_UPGRADE_REQUIRED
:
1170 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED
, httpStatus(status
), 0);
1173 case HTTP_STATUS_CUPS_PKI_ERROR
:
1174 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI
, httpStatus(status
), 0);
1177 case HTTP_STATUS_ERROR
:
1178 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
1182 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1183 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status
));
1184 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE
, httpStatus(status
), 0);