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"
15 #include "debug-internal.h"
18 #if defined(_WIN32) || defined(__EMX__)
22 #endif /* _WIN32 || __EMX__ */
27 # define MSG_DONTWAIT 0
28 #endif /* !MSG_DONTWAIT */
32 * 'cupsDoFileRequest()' - Do an IPP request with a file.
34 * This function sends the IPP request and attached file to the specified
35 * server, retrying and authenticating as necessary. The request is freed with
39 ipp_t * /* O - Response data */
40 cupsDoFileRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
41 ipp_t *request, /* I - IPP request */
42 const char *resource, /* I - HTTP resource for POST */
43 const char *filename) /* I - File to send or @code NULL@ for none */
45 ipp_t *response; /* IPP response data */
46 int infile; /* Input file */
49 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));
53 if ((infile = open(filename, O_RDONLY | O_BINARY)) < 0)
56 * Can't get file information!
59 _cupsSetError(errno == ENOENT ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED,
70 response = cupsDoIORequest(http, request, resource, infile, -1);
80 * 'cupsDoIORequest()' - Do an IPP request with file descriptors.
82 * This function sends the IPP request with the optional input file "infile" to
83 * the specified server, retrying and authenticating as necessary. The request
84 * is freed with @link ippDelete@.
86 * If "infile" is a valid file descriptor, @code cupsDoIORequest@ copies
87 * all of the data from the file after the IPP request message.
89 * If "outfile" is a valid file descriptor, @code cupsDoIORequest@ copies
90 * all of the data after the IPP response message to the file.
92 * @since CUPS 1.3/macOS 10.5@
95 ipp_t * /* O - Response data */
96 cupsDoIORequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
97 ipp_t *request, /* I - IPP request */
98 const char *resource, /* I - HTTP resource for POST */
99 int infile, /* I - File to read from or -1 for none */
100 int outfile) /* I - File to write to or -1 for none */
102 ipp_t *response = NULL; /* IPP response data */
103 size_t length = 0; /* Content-Length value */
104 http_status_t status; /* Status of HTTP request */
105 struct stat fileinfo; /* File information */
106 ssize_t bytes; /* Number of bytes read/written */
107 char buffer[32768]; /* Output buffer */
110 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));
113 * Range check input...
116 if (!request || !resource)
120 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
126 * Get the default connection as needed...
129 if (!http && (http = _cupsConnect()) == NULL)
137 * See if we have a file to send...
142 if (fstat(infile, &fileinfo))
145 * Can't get file information!
148 _cupsSetError(errno == EBADF ? IPP_STATUS_ERROR_NOT_FOUND : IPP_STATUS_ERROR_NOT_AUTHORIZED, NULL, 0);
155 if (fileinfo.st_mode & _S_IFDIR)
157 if (S_ISDIR(fileinfo.st_mode))
161 * Can't send a directory...
164 _cupsSetError(IPP_STATUS_ERROR_NOT_POSSIBLE, strerror(EISDIR), 0);
171 if (!S_ISREG(fileinfo.st_mode))
172 length = 0; /* Chunk when piping */
175 length = ippLength(request) + (size_t)fileinfo.st_size;
178 length = ippLength(request);
180 DEBUG_printf(("2cupsDoIORequest: Request length=%ld, total length=%ld", (long)ippLength(request), (long)length));
183 * Clear any "Local" authentication data since it is probably stale...
186 if (http->authstring && !strncmp(http->authstring, "Local ", 6))
187 httpSetAuthString(http, NULL, NULL);
190 * Loop until we can send the request without authorization problems.
193 while (response == NULL)
195 DEBUG_puts("2cupsDoIORequest: setup...");
198 * Send the request...
201 status = cupsSendRequest(http, request, resource, length);
203 DEBUG_printf(("2cupsDoIORequest: status=%d", status));
205 if (status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA && infile >= 0)
207 DEBUG_puts("2cupsDoIORequest: file write...");
210 * Send the file with the request...
214 if (S_ISREG(fileinfo.st_mode))
216 lseek(infile, 0, SEEK_SET);
218 while ((bytes = read(infile, buffer, sizeof(buffer))) > 0)
220 if ((status = cupsWriteRequestData(http, buffer, (size_t)bytes))
221 != HTTP_STATUS_CONTINUE)
227 * Get the server's response...
230 if (status <= HTTP_STATUS_CONTINUE || status == HTTP_STATUS_OK)
232 response = cupsGetResponse(http, resource);
233 status = httpGetStatus(http);
236 DEBUG_printf(("2cupsDoIORequest: status=%d", status));
238 if (status == HTTP_STATUS_ERROR ||
239 (status >= HTTP_STATUS_BAD_REQUEST && status != HTTP_STATUS_UNAUTHORIZED &&
240 status != HTTP_STATUS_UPGRADE_REQUIRED))
242 _cupsSetHTTPError(status);
246 if (response && outfile >= 0)
249 * Write trailing data to file...
252 while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
253 if (write(outfile, buffer, (size_t)bytes) < bytes)
257 if (http->state != HTTP_STATE_WAITING)
260 * Flush any remaining data...
268 * Delete the original request and return the response...
278 * 'cupsDoRequest()' - Do an IPP request.
280 * This function sends the IPP request to the specified server, retrying
281 * and authenticating as necessary. The request is freed with @link ippDelete@.
284 ipp_t * /* O - Response data */
285 cupsDoRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
286 ipp_t *request, /* I - IPP request */
287 const char *resource) /* I - HTTP resource for POST */
289 DEBUG_printf(("cupsDoRequest(http=%p, request=%p(%s), resource=\"%s\")", (void *)http, (void *)request, request ? ippOpString(request->request.op.operation_id) : "?", resource));
291 return (cupsDoIORequest(http, request, resource, -1, -1));
296 * 'cupsGetResponse()' - Get a response to an IPP request.
298 * Use this function to get the response for an IPP request sent using
299 * @link cupsSendRequest@. For requests that return additional data, use
300 * @link cupsReadResponseData@ after getting a successful response,
301 * otherwise call @link httpFlush@ to complete the response processing.
303 * @since CUPS 1.4/macOS 10.6@
306 ipp_t * /* O - Response or @code NULL@ on HTTP error */
307 cupsGetResponse(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
308 const char *resource) /* I - HTTP resource for POST */
310 http_status_t status; /* HTTP status */
311 ipp_state_t state; /* IPP read state */
312 ipp_t *response = NULL; /* IPP response */
315 DEBUG_printf(("cupsGetResponse(http=%p, resource=\"%s\")", (void *)http, resource));
316 DEBUG_printf(("1cupsGetResponse: http->state=%d", http ? http->state : HTTP_STATE_ERROR));
319 * Connect to the default server as needed...
324 _cups_globals_t *cg = _cupsGlobals();
325 /* Pointer to library globals */
327 if ((http = cg->http) == NULL)
329 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection."), 1);
330 DEBUG_puts("1cupsGetResponse: No active connection - returning NULL.");
335 if (http->state != HTTP_STATE_POST_RECV && http->state != HTTP_STATE_POST_SEND)
337 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No request sent."), 1);
338 DEBUG_puts("1cupsGetResponse: Not in POST state - returning NULL.");
343 * Check for an unfinished chunked request...
346 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
349 * Send a 0-length chunk to finish off the request...
352 DEBUG_puts("2cupsGetResponse: Finishing chunked POST...");
354 if (httpWrite2(http, "", 0) < 0)
359 * Wait for a response from the server...
362 DEBUG_printf(("2cupsGetResponse: Update loop, http->status=%d...",
367 status = httpUpdate(http);
369 while (status == HTTP_STATUS_CONTINUE);
371 DEBUG_printf(("2cupsGetResponse: status=%d", status));
373 if (status == HTTP_STATUS_OK)
376 * Get the IPP response...
381 while ((state = ippRead(http, response)) != IPP_STATE_DATA)
382 if (state == IPP_STATE_ERROR)
385 if (state == IPP_STATE_ERROR)
388 * Flush remaining data and delete the response...
391 DEBUG_puts("1cupsGetResponse: IPP read error!");
398 http->status = status = HTTP_STATUS_ERROR;
399 http->error = EINVAL;
402 else if (status != HTTP_STATUS_ERROR)
405 * Flush any error message...
411 * Then handle encryption and authentication...
414 if (status == HTTP_STATUS_UNAUTHORIZED)
417 * See if we can do authentication...
420 DEBUG_puts("2cupsGetResponse: Need authorization...");
422 if (!cupsDoAuthentication(http, "POST", resource))
423 httpReconnect2(http, 30000, NULL);
425 http->status = status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
429 else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
432 * Force a reconnect with encryption...
435 DEBUG_puts("2cupsGetResponse: Need encryption...");
437 if (!httpReconnect2(http, 30000, NULL))
438 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
440 #endif /* HAVE_SSL */
445 ipp_attribute_t *attr; /* status-message attribute */
448 attr = ippFindAttribute(response, "status-message", IPP_TAG_TEXT);
450 DEBUG_printf(("1cupsGetResponse: status-code=%s, status-message=\"%s\"",
451 ippErrorString(response->request.status.status_code),
452 attr ? attr->values[0].string.text : ""));
454 _cupsSetError(response->request.status.status_code,
455 attr ? attr->values[0].string.text :
456 ippErrorString(response->request.status.status_code), 0);
464 * 'cupsLastError()' - Return the last IPP status code received on the current
468 ipp_status_t /* O - IPP status code from last request */
471 return (_cupsGlobals()->last_error);
476 * 'cupsLastErrorString()' - Return the last IPP status-message received on the
479 * @since CUPS 1.2/macOS 10.5@
482 const char * /* O - status-message text from last request */
483 cupsLastErrorString(void)
485 return (_cupsGlobals()->last_status_message);
490 * '_cupsNextDelay()' - Return the next retry delay value.
492 * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
494 * Pass 0 for the current delay value to initialize the sequence.
497 int /* O - Next delay value */
498 _cupsNextDelay(int current, /* I - Current delay value or 0 */
499 int *previous) /* IO - Previous delay value */
501 int next; /* Next delay value */
506 next = (current + *previous) % 12;
507 *previous = next < current ? 0 : current;
520 * 'cupsReadResponseData()' - Read additional data after the IPP response.
522 * This function is used after @link cupsGetResponse@ to read the PPD or document
523 * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
526 * @since CUPS 1.4/macOS 10.6@
529 ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
530 cupsReadResponseData(
531 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
532 char *buffer, /* I - Buffer to use */
533 size_t length) /* I - Number of bytes to read */
536 * Get the default connection as needed...
539 DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
543 _cups_globals_t *cg = _cupsGlobals();
544 /* Pointer to library globals */
546 if ((http = cg->http) == NULL)
548 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
554 * Then read from the HTTP connection...
557 return (httpRead2(http, buffer, length));
562 * 'cupsSendRequest()' - Send an IPP request.
564 * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
565 * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
566 * and @link cupsReadResponseData@ to read any additional data following the
567 * response. Only one request can be sent/queued at a time per @code http_t@
570 * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
571 * on a successful send of the request.
573 * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
574 * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
576 * @since CUPS 1.4/macOS 10.6@
579 http_status_t /* O - Initial HTTP status */
580 cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
581 ipp_t *request, /* I - IPP request */
582 const char *resource, /* I - Resource path */
583 size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
585 http_status_t status; /* Status of HTTP request */
586 int got_status; /* Did we get the status? */
587 ipp_state_t state; /* State of IPP processing */
588 http_status_t expect; /* Expect: header to use */
589 char date[256]; /* Date: header value */
590 int digest; /* Are we using Digest authentication? */
593 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));
596 * Range check input...
599 if (!request || !resource)
601 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
603 return (HTTP_STATUS_ERROR);
607 * Get the default connection as needed...
610 if (!http && (http = _cupsConnect()) == NULL)
611 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
614 * If the prior request was not flushed out, do so now...
617 if (http->state == HTTP_STATE_GET_SEND ||
618 http->state == HTTP_STATE_POST_SEND)
620 DEBUG_puts("2cupsSendRequest: Flush prior response.");
623 else if (http->state != HTTP_STATE_WAITING)
625 DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
626 "reconnecting.", http->state));
627 if (httpReconnect2(http, 30000, NULL))
628 return (HTTP_STATUS_ERROR);
633 * See if we have an auth-info attribute and are communicating over
634 * a non-local link. If so, encrypt the link so that we can pass
635 * the authentication information securely...
638 if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
639 !httpAddrLocalhost(http->hostaddr) && !http->tls &&
640 httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
642 DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
643 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
645 #endif /* HAVE_SSL */
648 * Reconnect if the last response had a "Connection: close"...
651 if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
653 DEBUG_puts("2cupsSendRequest: Connection: close");
654 httpClearFields(http);
655 if (httpReconnect2(http, 30000, NULL))
657 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
658 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
663 * Loop until we can send the request without authorization problems.
666 expect = HTTP_STATUS_CONTINUE;
670 DEBUG_puts("2cupsSendRequest: Setup...");
673 * Setup the HTTP variables needed...
676 httpClearFields(http);
677 httpSetExpect(http, expect);
678 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
679 httpSetField(http, HTTP_FIELD_DATE, httpGetDateString2(time(NULL), date, (int)sizeof(date)));
680 httpSetLength(http, length);
682 digest = http->authstring && !strncmp(http->authstring, "Digest ", 7);
687 * Update the Digest authentication string...
690 _httpSetDigestAuthString(http, http->nextnonce, "POST", resource);
694 if (http->authstring && !strncmp(http->authstring, "Negotiate", 9))
697 * Do not use cached Kerberos credentials since they will look like a
701 _cupsSetNegotiateAuthString(http, "POST", resource);
703 #endif /* HAVE_GSSAPI */
705 httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
707 DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring));
713 DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
715 if (httpPost(http, resource))
717 DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
718 if (httpReconnect2(http, 30000, NULL))
720 DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
721 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
728 * Send the IPP data...
731 DEBUG_puts("2cupsSendRequest: Writing IPP request...");
733 request->state = IPP_STATE_IDLE;
734 status = HTTP_STATUS_CONTINUE;
737 while ((state = ippWrite(http, request)) != IPP_STATE_DATA)
743 _httpUpdate(http, &status);
744 if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
747 else if (state == IPP_STATE_ERROR)
751 if (state == IPP_STATE_ERROR)
754 * We weren't able to send the IPP request. But did we already get a HTTP
758 if (!got_status || status < HTTP_STATUS_MULTIPLE_CHOICES)
761 * No, something else went wrong.
764 DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
766 http->status = HTTP_STATUS_ERROR;
767 http->state = HTTP_STATE_WAITING;
769 return (HTTP_STATUS_ERROR);
774 * Wait up to 1 second to get the 100-continue response as needed...
777 if (!got_status || (digest && status == HTTP_STATUS_CONTINUE))
779 if (expect == HTTP_STATUS_CONTINUE || digest)
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/macOS 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, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
915 _cups_globals_t *cg = _cupsGlobals();
916 /* Pointer to library globals */
918 if ((http = cg->http) == NULL)
920 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
921 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
922 return (HTTP_STATUS_ERROR);
927 * Then write to the HTTP connection...
932 if (httpWrite2(http, buffer, length) < 0)
934 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
935 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0);
936 return (HTTP_STATUS_ERROR);
940 * Finally, check if we have any pending data from the server...
943 if (length >= HTTP_MAX_BUFFER ||
944 http->wused < wused ||
945 (wused > 0 && (size_t)http->wused == length))
948 * We've written something to the server, so check for response data...
951 if (_httpWait(http, 0, 1))
953 http_status_t status; /* Status from _httpUpdate */
955 _httpUpdate(http, &status);
956 if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
958 _cupsSetHTTPError(status);
962 status = httpUpdate(http);
964 while (status != HTTP_STATUS_ERROR && http->state == HTTP_STATE_POST_RECV);
969 DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status));
974 DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
975 return (HTTP_STATUS_CONTINUE);
980 * '_cupsConnect()' - Get the default server connection...
983 http_t * /* O - HTTP connection */
986 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
990 * See if we are connected to the same server...
996 * Compare the connection hostname, port, and encryption settings to
997 * the cached defaults; these were initialized the first time we
1001 if (strcmp(cg->http->hostname, cg->server) ||
1003 (httpAddrFamily(cg->http->hostaddr) != AF_LOCAL && cg->ipp_port != httpAddrPort(cg->http->hostaddr)) ||
1005 cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
1006 #endif /* AF_LOCAL */
1007 (cg->http->encryption != cg->encryption &&
1008 cg->http->encryption == HTTP_ENCRYPTION_NEVER))
1011 * Need to close the current connection because something has changed...
1014 httpClose(cg->http);
1020 * Same server, see if the connection is still established...
1023 char ch; /* Connection check byte */
1024 ssize_t n; /* Number of bytes */
1027 if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK)) == 0 ||
1028 (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK))
1030 if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK | MSG_DONTWAIT)) == 0 ||
1031 (n < 0 && errno != EWOULDBLOCK))
1035 * Nope, close the connection...
1038 httpClose(cg->http);
1045 * (Re)connect as needed...
1050 if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
1051 cupsEncryption(), 1, 30000, NULL)) == NULL)
1054 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
1056 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
1057 _("Unable to connect to host."), 1);
1062 * Return the cached connection...
1070 * '_cupsSetError()' - Set the last IPP status code and status-message.
1074 _cupsSetError(ipp_status_t status, /* I - IPP status code */
1075 const char *message, /* I - status-message value */
1076 int localize) /* I - Localize the message? */
1078 _cups_globals_t *cg; /* Global data */
1081 if (!message && errno)
1083 message = strerror(errno);
1087 cg = _cupsGlobals();
1088 cg->last_error = status;
1090 if (cg->last_status_message)
1092 _cupsStrFree(cg->last_status_message);
1094 cg->last_status_message = NULL;
1102 * Get the message catalog...
1105 if (!cg->lang_default)
1106 cg->lang_default = cupsLangDefault();
1108 cg->last_status_message = _cupsStrAlloc(_cupsLangString(cg->lang_default,
1112 cg->last_status_message = _cupsStrAlloc(message);
1115 DEBUG_printf(("4_cupsSetError: last_error=%s, last_status_message=\"%s\"",
1116 ippErrorString(cg->last_error), cg->last_status_message));
1121 * '_cupsSetHTTPError()' - Set the last error using the HTTP status.
1125 _cupsSetHTTPError(http_status_t status) /* I - HTTP status code */
1129 case HTTP_STATUS_NOT_FOUND :
1130 _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, httpStatus(status), 0);
1133 case HTTP_STATUS_UNAUTHORIZED :
1134 _cupsSetError(IPP_STATUS_ERROR_NOT_AUTHENTICATED, httpStatus(status), 0);
1137 case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED :
1138 _cupsSetError(IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED, httpStatus(status), 0);
1141 case HTTP_STATUS_FORBIDDEN :
1142 _cupsSetError(IPP_STATUS_ERROR_FORBIDDEN, httpStatus(status), 0);
1145 case HTTP_STATUS_BAD_REQUEST :
1146 _cupsSetError(IPP_STATUS_ERROR_BAD_REQUEST, httpStatus(status), 0);
1149 case HTTP_STATUS_REQUEST_TOO_LARGE :
1150 _cupsSetError(IPP_STATUS_ERROR_REQUEST_VALUE, httpStatus(status), 0);
1153 case HTTP_STATUS_NOT_IMPLEMENTED :
1154 _cupsSetError(IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED, httpStatus(status), 0);
1157 case HTTP_STATUS_NOT_SUPPORTED :
1158 _cupsSetError(IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED, httpStatus(status), 0);
1161 case HTTP_STATUS_UPGRADE_REQUIRED :
1162 _cupsSetError(IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED, httpStatus(status), 0);
1165 case HTTP_STATUS_CUPS_PKI_ERROR :
1166 _cupsSetError(IPP_STATUS_ERROR_CUPS_PKI, httpStatus(status), 0);
1169 case HTTP_STATUS_ERROR :
1170 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
1174 DEBUG_printf(("4_cupsSetHTTPError: HTTP error %d mapped to "
1175 "IPP_STATUS_ERROR_SERVICE_UNAVAILABLE!", status));
1176 _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, httpStatus(status), 0);