+ return (_cupsGlobals()->last_status_message);
+}
+
+
+/*
+ * '_cupsNextDelay()' - Return the next retry delay value.
+ *
+ * This function currently returns the Fibonacci sequence 1 1 2 3 5 8.
+ *
+ * Pass 0 for the current delay value to initialize the sequence.
+ */
+
+int /* O - Next delay value */
+_cupsNextDelay(int current, /* I - Current delay value or 0 */
+ int *previous) /* IO - Previous delay value */
+{
+ int next; /* Next delay value */
+
+
+ if (current > 0)
+ {
+ next = (current + *previous) % 12;
+ *previous = next < current ? 0 : current;
+ }
+ else
+ {
+ next = 1;
+ *previous = 0;
+ }
+
+ return (next);
+}
+
+
+/*
+ * 'cupsReadResponseData()' - Read additional data after the IPP response.
+ *
+ * This function is used after @link cupsGetResponse@ to read the PPD or document
+ * files from @code CUPS_GET_PPD@ and @code CUPS_GET_DOCUMENT@ requests,
+ * respectively.
+ *
+ * @since CUPS 1.4/macOS 10.6@
+ */
+
+ssize_t /* O - Bytes read, 0 on EOF, -1 on error */
+cupsReadResponseData(
+ http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
+ char *buffer, /* I - Buffer to use */
+ size_t length) /* I - Number of bytes to read */
+{
+ /*
+ * Get the default connection as needed...
+ */
+
+ DEBUG_printf(("cupsReadResponseData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
+
+ if (!http)
+ {
+ _cups_globals_t *cg = _cupsGlobals();
+ /* Pointer to library globals */
+
+ if ((http = cg->http) == NULL)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
+ return (-1);
+ }
+ }
+
+ /*
+ * Then read from the HTTP connection...
+ */
+
+ return (httpRead2(http, buffer, length));
+}
+
+
+/*
+ * 'cupsSendRequest()' - Send an IPP request.
+ *
+ * Use @link cupsWriteRequestData@ to write any additional data (document, PPD
+ * file, etc.) for the request, @link cupsGetResponse@ to get the IPP response,
+ * and @link cupsReadResponseData@ to read any additional data following the
+ * response. Only one request can be sent/queued at a time per @code http_t@
+ * connection.
+ *
+ * Returns the initial HTTP status code, which will be @code HTTP_STATUS_CONTINUE@
+ * on a successful send of the request.
+ *
+ * Note: Unlike @link cupsDoFileRequest@, @link cupsDoIORequest@, and
+ * @link cupsDoRequest@, the request is NOT freed with @link ippDelete@.
+ *
+ * @since CUPS 1.4/macOS 10.6@
+ */
+
+http_status_t /* O - Initial HTTP status */
+cupsSendRequest(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
+ ipp_t *request, /* I - IPP request */
+ const char *resource, /* I - Resource path */
+ size_t length) /* I - Length of data to follow or @code CUPS_LENGTH_VARIABLE@ */
+{
+ http_status_t status; /* Status of HTTP request */
+ int got_status; /* Did we get the status? */
+ ipp_state_t state; /* State of IPP processing */
+ http_status_t expect; /* Expect: header to use */
+ char date[256]; /* Date: header value */
+ int digest; /* Are we using Digest authentication? */
+
+
+ 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));
+
+ /*
+ * Range check input...
+ */
+
+ if (!request || !resource)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
+
+ return (HTTP_STATUS_ERROR);
+ }
+
+ /*
+ * Get the default connection as needed...
+ */
+
+ if (!http && (http = _cupsConnect()) == NULL)
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+
+ /*
+ * If the prior request was not flushed out, do so now...
+ */
+
+ if (http->state == HTTP_STATE_GET_SEND ||
+ http->state == HTTP_STATE_POST_SEND)
+ {
+ DEBUG_puts("2cupsSendRequest: Flush prior response.");
+ httpFlush(http);
+ }
+ else if (http->state != HTTP_STATE_WAITING)
+ {
+ DEBUG_printf(("1cupsSendRequest: Unknown HTTP state (%d), "
+ "reconnecting.", http->state));
+ if (httpReconnect2(http, 30000, NULL))
+ return (HTTP_STATUS_ERROR);
+ }
+
+#ifdef HAVE_SSL
+ /*
+ * See if we have an auth-info attribute and are communicating over
+ * a non-local link. If so, encrypt the link so that we can pass
+ * the authentication information securely...
+ */
+
+ if (ippFindAttribute(request, "auth-info", IPP_TAG_TEXT) &&
+ !httpAddrLocalhost(http->hostaddr) && !http->tls &&
+ httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+#endif /* HAVE_SSL */
+
+ /*
+ * Reconnect if the last response had a "Connection: close"...
+ */
+
+ if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+ {
+ DEBUG_puts("2cupsSendRequest: Connection: close");
+ httpClearFields(http);
+ if (httpReconnect2(http, 30000, NULL))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+ }
+
+ /*
+ * Loop until we can send the request without authorization problems.
+ */
+
+ expect = HTTP_STATUS_CONTINUE;
+
+ for (;;)
+ {
+ DEBUG_puts("2cupsSendRequest: Setup...");
+
+ /*
+ * Setup the HTTP variables needed...
+ */
+
+ httpClearFields(http);
+ httpSetExpect(http, expect);
+ httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
+ httpSetField(http, HTTP_FIELD_DATE, httpGetDateString2(time(NULL), date, (int)sizeof(date)));
+ httpSetLength(http, length);
+
+ digest = http->authstring && !strncmp(http->authstring, "Digest ", 7);
+
+ if (digest)
+ {
+ /*
+ * Update the Digest authentication string...
+ */
+
+ _httpSetDigestAuthString(http, http->nextnonce, "POST", resource);
+ }
+
+#ifdef HAVE_GSSAPI
+ if (http->authstring && !strncmp(http->authstring, "Negotiate", 9))
+ {
+ /*
+ * Do not use cached Kerberos credentials since they will look like a
+ * "replay" attack...
+ */
+
+ _cupsSetNegotiateAuthString(http, "POST", resource);
+ }
+#endif /* HAVE_GSSAPI */
+
+ httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
+
+ DEBUG_printf(("2cupsSendRequest: authstring=\"%s\"", http->authstring));
+
+ /*
+ * Try the request...
+ */
+
+ DEBUG_puts("2cupsSendRequest: Sending HTTP POST...");
+
+ if (httpPost(http, resource))
+ {
+ DEBUG_puts("2cupsSendRequest: POST failed, reconnecting.");
+ if (httpReconnect2(http, 30000, NULL))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+ else
+ continue;
+ }
+
+ /*
+ * Send the IPP data...
+ */
+
+ DEBUG_puts("2cupsSendRequest: Writing IPP request...");
+
+ request->state = IPP_STATE_IDLE;
+ status = HTTP_STATUS_CONTINUE;
+ got_status = 0;
+
+ while ((state = ippWrite(http, request)) != IPP_STATE_DATA)
+ {
+ if (httpCheck(http))
+ {
+ got_status = 1;
+
+ _httpUpdate(http, &status);
+ if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
+ break;
+ }
+ else if (state == IPP_STATE_ERROR)
+ break;
+ }
+
+ if (state == IPP_STATE_ERROR)
+ {
+ /*
+ * We weren't able to send the IPP request. But did we already get a HTTP
+ * error status?
+ */
+
+ if (!got_status || status < HTTP_STATUS_MULTIPLE_CHOICES)
+ {
+ /*
+ * No, something else went wrong.
+ */
+
+ DEBUG_puts("1cupsSendRequest: Unable to send IPP request.");
+
+ http->status = HTTP_STATUS_ERROR;
+ http->state = HTTP_STATE_WAITING;
+
+ return (HTTP_STATUS_ERROR);
+ }
+ }
+
+ /*
+ * Wait up to 1 second to get the 100-continue response as needed...
+ */
+
+ if (!got_status || (digest && status == HTTP_STATUS_CONTINUE))
+ {
+ if (expect == HTTP_STATUS_CONTINUE || digest)
+ {
+ DEBUG_puts("2cupsSendRequest: Waiting for 100-continue...");
+
+ if (httpWait(http, 1000))
+ _httpUpdate(http, &status);
+ }
+ else if (httpCheck(http))
+ _httpUpdate(http, &status);
+ }
+
+ DEBUG_printf(("2cupsSendRequest: status=%d", status));
+
+ /*
+ * Process the current HTTP status...
+ */
+
+ if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
+ {
+ int temp_status; /* Temporary status */
+
+ _cupsSetHTTPError(status);
+
+ do
+ {
+ temp_status = httpUpdate(http);
+ }
+ while (temp_status != HTTP_STATUS_ERROR &&
+ http->state == HTTP_STATE_POST_RECV);
+
+ httpFlush(http);
+ }
+
+ switch (status)
+ {
+ case HTTP_STATUS_CONTINUE :
+ case HTTP_STATUS_OK :
+ case HTTP_STATUS_ERROR :
+ DEBUG_printf(("1cupsSendRequest: Returning %d.", status));
+ return (status);
+
+ case HTTP_STATUS_UNAUTHORIZED :
+ if (cupsDoAuthentication(http, "POST", resource))
+ {
+ DEBUG_puts("1cupsSendRequest: Returning HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED.");
+ return (HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED);
+ }
+
+ DEBUG_puts("2cupsSendRequest: Reconnecting after HTTP_STATUS_UNAUTHORIZED.");
+
+ if (httpReconnect2(http, 30000, NULL))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+ break;
+
+#ifdef HAVE_SSL
+ case HTTP_STATUS_UPGRADE_REQUIRED :
+ /*
+ * Flush any error message, reconnect, and then upgrade with
+ * encryption...
+ */
+
+ DEBUG_puts("2cupsSendRequest: Reconnecting after "
+ "HTTP_STATUS_UPGRADE_REQUIRED.");
+
+ if (httpReconnect2(http, 30000, NULL))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+
+ DEBUG_puts("2cupsSendRequest: Upgrading to TLS.");
+ if (httpEncryption(http, HTTP_ENCRYPTION_REQUIRED))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to encrypt connection.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+ break;
+#endif /* HAVE_SSL */
+
+ case HTTP_STATUS_EXPECTATION_FAILED :
+ /*
+ * Don't try using the Expect: header the next time around...
+ */
+
+ expect = (http_status_t)0;
+
+ DEBUG_puts("2cupsSendRequest: Reconnecting after "
+ "HTTP_EXPECTATION_FAILED.");
+
+ if (httpReconnect2(http, 30000, NULL))
+ {
+ DEBUG_puts("1cupsSendRequest: Unable to reconnect.");
+ return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+ }
+ break;
+
+ default :
+ /*
+ * Some other error...
+ */
+
+ return (status);
+ }
+ }
+}
+
+
+/*
+ * 'cupsWriteRequestData()' - Write additional data after an IPP request.
+ *
+ * This function is used after @link cupsSendRequest@ to provide a PPD and
+ * after @link cupsStartDocument@ to provide a document file.
+ *
+ * @since CUPS 1.4/macOS 10.6@
+ */
+
+http_status_t /* O - @code HTTP_STATUS_CONTINUE@ if OK or HTTP status on error */
+cupsWriteRequestData(
+ http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
+ const char *buffer, /* I - Bytes to write */
+ size_t length) /* I - Number of bytes to write */
+{
+ int wused; /* Previous bytes in buffer */
+
+
+ /*
+ * Get the default connection as needed...
+ */
+
+ DEBUG_printf(("cupsWriteRequestData(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
+
+ if (!http)
+ {
+ _cups_globals_t *cg = _cupsGlobals();
+ /* Pointer to library globals */
+
+ if ((http = cg->http) == NULL)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No active connection"), 1);
+ DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
+ return (HTTP_STATUS_ERROR);
+ }
+ }
+
+ /*
+ * Then write to the HTTP connection...
+ */
+
+ wused = http->wused;
+
+ if (httpWrite2(http, buffer, length) < 0)
+ {
+ DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_ERROR.");
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(http->error), 0);
+ return (HTTP_STATUS_ERROR);
+ }
+
+ /*
+ * Finally, check if we have any pending data from the server...
+ */
+
+ if (length >= HTTP_MAX_BUFFER ||
+ http->wused < wused ||
+ (wused > 0 && (size_t)http->wused == length))
+ {
+ /*
+ * We've written something to the server, so check for response data...
+ */
+
+ if (_httpWait(http, 0, 1))
+ {
+ http_status_t status; /* Status from _httpUpdate */
+
+ _httpUpdate(http, &status);
+ if (status >= HTTP_STATUS_MULTIPLE_CHOICES)
+ {
+ _cupsSetHTTPError(status);
+
+ do
+ {
+ status = httpUpdate(http);
+ }
+ while (status != HTTP_STATUS_ERROR && http->state == HTTP_STATE_POST_RECV);
+
+ httpFlush(http);
+ }
+
+ DEBUG_printf(("1cupsWriteRequestData: Returning %d.\n", status));
+ return (status);
+ }
+ }
+
+ DEBUG_puts("1cupsWriteRequestData: Returning HTTP_STATUS_CONTINUE.");
+ return (HTTP_STATUS_CONTINUE);
+}
+
+
+/*
+ * '_cupsConnect()' - Get the default server connection...
+ */
+
+http_t * /* O - HTTP connection */
+_cupsConnect(void)
+{
+ _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
+
+
+ /*
+ * See if we are connected to the same server...
+ */
+
+ if (cg->http)
+ {
+ /*
+ * Compare the connection hostname, port, and encryption settings to
+ * the cached defaults; these were initialized the first time we
+ * connected...
+ */
+
+ if (strcmp(cg->http->hostname, cg->server) ||
+#ifdef AF_LOCAL
+ (httpAddrFamily(cg->http->hostaddr) != AF_LOCAL && cg->ipp_port != httpAddrPort(cg->http->hostaddr)) ||
+#else
+ cg->ipp_port != httpAddrPort(cg->http->hostaddr) ||
+#endif /* AF_LOCAL */
+ (cg->http->encryption != cg->encryption &&
+ cg->http->encryption == HTTP_ENCRYPTION_NEVER))
+ {
+ /*
+ * Need to close the current connection because something has changed...
+ */
+
+ httpClose(cg->http);
+ cg->http = NULL;
+ }
+ else
+ {
+ /*
+ * Same server, see if the connection is still established...
+ */
+
+ char ch; /* Connection check byte */
+ ssize_t n; /* Number of bytes */
+
+#ifdef _WIN32
+ if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK)) == 0 ||
+ (n < 0 && WSAGetLastError() != WSAEWOULDBLOCK))
+#else
+ if ((n = recv(cg->http->fd, &ch, 1, MSG_PEEK | MSG_DONTWAIT)) == 0 ||
+ (n < 0 && errno != EWOULDBLOCK))
+#endif /* _WIN32 */
+ {
+ /*
+ * Nope, close the connection...
+ */
+
+ httpClose(cg->http);
+ cg->http = NULL;
+ }
+ }
+ }
+
+ /*
+ * (Re)connect as needed...
+ */
+
+ if (!cg->http)
+ {
+ if ((cg->http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC,
+ cupsEncryption(), 1, 30000, NULL)) == NULL)
+ {
+ if (errno)
+ _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE, NULL, 0);
+ else
+ _cupsSetError(IPP_STATUS_ERROR_SERVICE_UNAVAILABLE,
+ _("Unable to connect to host."), 1);
+ }
+ }
+
+ /*
+ * Return the cached connection...
+ */
+
+ return (cg->http);