Lines Matching refs:http
2 * HTTP routines for CUPS.
14 * missing or damaged, see the license at "http://www.cups.org/".
43 static void http_content_coding_finish(http_t *http);
44 static void http_content_coding_start(http_t *http,
55 static ssize_t http_read(http_t *http, char *buffer, size_t length);
56 static ssize_t http_read_buffered(http_t *http, char *buffer, size_t length);
57 static ssize_t http_read_chunk(http_t *http, char *buffer, size_t length);
58 static int http_send(http_t *http, http_state_t request,
60 static ssize_t http_write(http_t *http, const char *buffer,
62 static ssize_t http_write_chunk(http_t *http, const char *buffer,
64 static off_t http_set_length(http_t *http);
66 static void http_set_wait(http_t *http);
69 static int http_tls_upgrade(http_t *http);
113 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
119 http_t * /* O - HTTP connection or @code NULL@ */
124 http_t *http; /* HTTP connection */
143 if ((http = http_create(NULL, 0, &addrlist, AF_UNSPEC,
154 if ((http->fd = accept(fd, (struct sockaddr *)&(http->addrlist->addr),
158 httpClose(http);
163 http->hostaddr = &(http->addrlist->addr);
165 if (httpAddrLocalhost(http->hostaddr))
166 strlcpy(http->hostname, "localhost", sizeof(http->hostname));
168 httpAddrString(http->hostaddr, http->hostname, sizeof(http->hostname));
176 setsockopt(http->fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val));
187 setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val));
194 fcntl(http->fd, F_SETFD, FD_CLOEXEC);
197 return (http);
241 httpBlocking(http_t *http, /* I - HTTP connection */
244 if (http)
246 http->blocking = b;
247 http_set_wait(http);
257 httpCheck(http_t *http) /* I - HTTP connection */
259 return (httpWait(http, 0));
270 httpClearCookie(http_t *http) /* I - HTTP connection */
272 if (!http)
275 if (http->cookie)
277 free(http->cookie);
278 http->cookie = NULL;
284 * 'httpClearFields()' - Clear HTTP request fields.
288 httpClearFields(http_t *http) /* I - HTTP connection */
290 DEBUG_printf(("httpClearFields(http=%p)", (void *)http));
292 if (http)
294 memset(http->fields, 0, sizeof(http->fields));
296 if (http->mode == _HTTP_MODE_CLIENT)
298 if (http->hostname[0] == '/')
299 httpSetField(http, HTTP_FIELD_HOST, "localhost");
301 httpSetField(http, HTTP_FIELD_HOST, http->hostname);
304 if (http->field_authorization)
306 free(http->field_authorization);
307 http->field_authorization = NULL;
310 if (http->accept_encoding)
312 _cupsStrFree(http->accept_encoding);
313 http->accept_encoding = NULL;
316 if (http->allow)
318 _cupsStrFree(http->allow);
319 http->allow = NULL;
322 if (http->server)
324 _cupsStrFree(http->server);
325 http->server = NULL;
328 http->expect = (http_status_t)0;
334 * 'httpClose()' - Close an HTTP connection.
338 httpClose(http_t *http) /* I - HTTP connection */
345 DEBUG_printf(("httpClose(http=%p)", (void *)http));
351 if (!http)
358 _httpDisconnect(http);
364 httpAddrFreeList(http->addrlist);
366 if (http->cookie)
367 free(http->cookie);
370 if (http->gssctx != GSS_C_NO_CONTEXT)
371 gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER);
373 if (http->gssname != GSS_C_NO_NAME)
374 gss_release_name(&minor_status, &http->gssname);
378 if (http->auth_ref)
379 AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
382 httpClearFields(http);
384 if (http->authstring && http->authstring != http->_authstring)
385 free(http->authstring);
387 free(http);
416 * 'httpConnect()' - Connect to a HTTP server.
423 http_t * /* O - New HTTP connection */
433 * 'httpConnect2()' - Connect to a HTTP server.
438 http_t * /* O - New HTTP connection */
449 http_t *http; /* New HTTP connection */
455 * Create the HTTP structure...
458 if ((http = http_create(host, port, addrlist, family, encryption, blocking,
466 if (msec == 0 || !httpReconnect2(http, msec, cancel))
467 return (http);
473 httpClose(http);
480 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
488 http_t * /* O - New HTTP connection */
507 httpDelete(http_t *http, /* I - HTTP connection */
510 return (http_send(http, HTTP_STATE_DELETE, uri));
515 * '_httpDisconnect()' - Disconnect a HTTP connection.
519 _httpDisconnect(http_t *http) /* I - HTTP connection */
522 if (http->tls)
523 _httpTLSStop(http);
526 httpAddrClose(NULL, http->fd);
528 http->fd = -1;
537 httpEncryption(http_t *http, /* I - HTTP connection */
540 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http, e));
543 if (!http)
546 if (http->mode == _HTTP_MODE_CLIENT)
548 http->encryption = e;
550 if ((http->encryption == HTTP_ENCRYPTION_ALWAYS && !http->tls) ||
551 (http->encryption == HTTP_ENCRYPTION_NEVER && http->tls))
552 return (httpReconnect2(http, 30000, NULL));
553 else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls)
554 return (http_tls_upgrade(http));
560 if (e == HTTP_ENCRYPTION_NEVER && http->tls)
563 http->encryption = e;
564 if (e != HTTP_ENCRYPTION_IF_REQUESTED && !http->tls)
565 return (_httpTLSStart(http));
583 httpError(http_t *http) /* I - HTTP connection */
585 if (http)
586 return (http->error);
593 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
612 * 'httpFlush()' - Flush data read from a HTTP connection.
616 httpFlush(http_t *http) /* I - HTTP connection */
623 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http, httpStateString(http->state)));
629 if (http->state == HTTP_STATE_WAITING)
636 blocking = http->blocking;
637 http->blocking = 0;
643 oldstate = http->state;
644 while (httpRead2(http, buffer, sizeof(buffer)) > 0);
651 http->blocking = blocking;
653 if (http->state == oldstate && http->state != HTTP_STATE_WAITING &&
654 http->fd >= 0)
661 if (http->coding)
662 http_content_coding_finish(http);
667 http->state = HTTP_STATE_WAITING;
670 if (http->tls)
671 _httpTLSStop(http);
674 httpAddrClose(NULL, http->fd);
676 http->fd = -1;
682 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
688 httpFlushWrite(http_t *http) /* I - HTTP connection */
693 DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http, http ? http->data_encoding : 100));
695 if (!http || !http->wused)
697 DEBUG_puts(http ? "1httpFlushWrite: Write buffer is empty." :
702 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
703 bytes = http_write_chunk(http, httphttp->wused);
705 bytes = http_write(http, http->wbuffer, (size_t)http->wused);
707 http->wused = 0;
744 httpGet(http_t *http, /* I - HTTP connection */
747 return (http_send(http, HTTP_STATE_GET, uri));
760 httpGetActivity(http_t *http) /* I - HTTP connection */
762 return (http ? http->activity : 0);
778 httpGetAuthString(http_t *http) /* I - HTTP connection */
780 if (http)
781 return (http->authstring);
794 httpGetBlocking(http_t *http) /* I - HTTP connection */
796 return (http ? http->blocking : 0);
804 * This function uses the value of the Accepts-Encoding HTTP header and must be
815 httpGetContentEncoding(http_t *http) /* I - HTTP connection */
818 if (http && http->accept_encoding)
834 strlcpy(temp, http->accept_encoding, sizeof(temp));
895 httpGetCookie(http_t *http) /* I - HTTP connection */
897 return (http ? http->cookie : NULL);
912 httpGetEncryption(http_t *http) /* I - HTTP connection */
914 return (http ? http->encryption : HTTP_ENCRYPTION_IF_REQUESTED);
922 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
928 httpGetExpect(http_t *http) /* I - HTTP connection */
930 if (!http)
933 return (http->expect);
944 httpGetFd(http_t *http) /* I - HTTP connection */
946 return (http ? http->fd : -1);
955 httpGetField(http_t *http, /* I - HTTP connection */
958 if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX)
964 return (http->accept_encoding);
967 return (http->allow);
970 return (http->server);
973 if (http->field_authorization)
980 return (http->field_authorization);
984 return (http->fields[field]);
996 httpGetKeepAlive(http_t *http) /* I - HTTP connection */
998 return (http ? http->keep_alive : HTTP_KEEPALIVE_OFF);
1013 httpGetLength(http_t *http) /* I - HTTP connection */
1019 if (http)
1021 httpGetLength2(http);
1023 return (http->_data_remaining);
1041 httpGetLength2(http_t *http) /* I - HTTP connection */
1046 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http, httpStateString(http->state)));
1048 if (!http)
1051 if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked"))
1059 * The following is a hack for HTTP servers that don't send a
1066 if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
1073 if (http->status >= HTTP_STATUS_MULTIPLE_CHOICES ||
1074 http->state == HTTP_STATE_OPTIONS ||
1075 (http->state == HTTP_STATE_GET && http->mode == _HTTP_MODE_SERVER) ||
1076 http->state == HTTP_STATE_HEAD ||
1077 (http->state == HTTP_STATE_PUT && http->mode == _HTTP_MODE_CLIENT) ||
1078 http->state == HTTP_STATE_DELETE ||
1079 http->state == HTTP_STATE_TRACE ||
1080 http->state == HTTP_STATE_CONNECT)
1085 else if ((remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH],
1104 httpGetPending(http_t *http) /* I - HTTP connection */
1106 return (http ? (size_t)http->wused : 0);
1117 httpGetReady(http_t *http) /* I - HTTP connection */
1119 if (!http)
1121 else if (http->used > 0)
1122 return ((size_t)http->used);
1124 else if (http->tls)
1125 return (_httpTLSPending(http));
1143 httpGetRemaining(http_t *http) /* I - HTTP connection */
1145 return (http ? (size_t)http->data_remaining : 0);
1150 * 'httpGets()' - Get a line of text from a HTTP connection.
1156 http_t *http) /* I - HTTP connection */
1166 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line, length, (void *)http));
1168 if (!http || !line || length <= 1)
1175 http->error = 0;
1192 while (http->used == 0)
1198 while (!_httpWait(http, http->wait_value, 1))
1200 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
1205 http->error = WSAETIMEDOUT;
1207 http->error = ETIMEDOUT;
1212 bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used));
1229 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
1232 http->error = WSAGetLastError();
1234 else if (WSAGetLastError() != http->error)
1236 http->error = WSAGetLastError();
1247 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
1249 else if (!http->timeout_cb && errno == EAGAIN)
1252 http->error = errno;
1254 else if (errno != http->error)
1256 http->error = errno;
1265 http->error = EPIPE;
1274 http->used += (int)bytes;
1281 for (bufptr = http->buffer, bufend = http->buffer + http->used;
1296 http->used -= (int)(bufptr - http->buffer);
1297 if (http->used > 0)
1298 memmove(http->buffer, bufptr, (size_t)http->used);
1306 http->activity = time(NULL);
1323 * 'httpGetState()' - Get the current state of the HTTP request.
1326 http_state_t /* O - HTTP state */
1327 httpGetState(http_t *http) /* I - HTTP connection */
1329 return (http ? http->state : HTTP_STATE_ERROR);
1334 * 'httpGetStatus()' - Get the status of the last HTTP request.
1339 http_status_t /* O - HTTP status */
1340 httpGetStatus(http_t *http) /* I - HTTP connection */
1342 return (http ? http->status : HTTP_STATUS_ERROR);
1353 httpGetSubField(http_t *http, /* I - HTTP connection */
1358 return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE));
1369 httpGetSubField2(http_t *http, /* I - HTTP connection */
1380 DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)http, field, name, (void *)value, valuelen));
1382 if (!http || !name || !value || valuelen < 2 ||
1388 for (fptr = http->fields[field]; *fptr;)
1494 * 'httpGetVersion()' - Get the HTTP version at the other end.
1498 httpGetVersion(http_t *http) /* I - HTTP connection */
1500 return (http ? http->version : HTTP_VERSION_1_0);
1509 httpHead(http_t *http, /* I - HTTP connection */
1512 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http, uri));
1513 return (http_send(http, HTTP_STATE_HEAD, uri));
1518 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1519 * default HTTP proxy (if any).
1581 httpIsChunked(http_t *http) /* I - HTTP connection */
1583 return (http ? http->data_encoding == HTTP_ENCODING_CHUNKED : 0);
1596 httpIsEncrypted(http_t *http) /* I - HTTP connection */
1598 return (http ? http->tls != NULL : 0);
1607 httpOptions(http_t *http, /* I - HTTP connection */
1610 return (http_send(http, HTTP_STATE_OPTIONS, uri));
1615 * 'httpPeek()' - Peek at data from a HTTP connection.
1617 * This function copies available data from the given HTTP connection, reading
1627 httpPeek(http_t *http, /* I - HTTP connection */
1635 DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
1637 if (http == NULL || buffer == NULL)
1640 http->activity = time(NULL);
1641 http->error = 0;
1646 if (http->data_encoding == HTTP_ENCODING_CHUNKED &&
1647 http->data_remaining <= 0)
1651 if (httpGets(len, sizeof(len), http) == NULL)
1660 if (!httpGets(len, sizeof(len), http))
1667 http->data_remaining = strtoll(len, NULL, 16);
1669 if (http->data_remaining < 0)
1677 CUPS_LLCAST http->data_remaining));
1679 if (http->data_remaining <= 0 && http->data_encoding != HTTP_ENCODING_FIELDS)
1687 if (http->coding >= _HTTP_CODING_GUNZIP)
1688 http_content_coding_finish(http);
1691 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
1692 httpGets(len, sizeof(len), http);
1694 if (http->state == HTTP_STATE_POST_RECV)
1695 http->state ++;
1697 http->state = HTTP_STATE_STATUS;
1700 httpStateString(http->state)));
1706 http->data_encoding = HTTP_ENCODING_FIELDS;
1710 else if (length > (size_t)http->data_remaining)
1711 length = (size_t)http->data_remaining;
1714 if (http->used == 0 &&
1715 (http->coding == _HTTP_CODING_IDENTITY ||
1716 (http->coding >= _HTTP_CODING_GUNZIP && http->stream.avail_in == 0)))
1718 if (http->used == 0)
1727 if (!http->blocking)
1729 while (!httpWait(http, http->wait_value))
1731 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
1738 if ((size_t)http->data_remaining > sizeof(http->buffer))
1739 buflen = sizeof(http->buffer);
1741 buflen = (ssize_t)http->data_remaining;
1744 bytes = http_read(http, http->buffer, (size_t)buflen);
1751 http_debug_hex("httpPeek", http->buffer, (int)bytes);
1754 http->used = (int)bytes;
1759 if (http->coding >= _HTTP_CODING_GUNZIP)
1765 if (http->used > 0 && http->stream.avail_in < HTTP_MAX_BUFFER)
1767 size_t buflen = buflen = HTTP_MAX_BUFFER - http->stream.avail_in;
1770 if (http->stream.avail_in > 0 &&
1771 http->stream.next_in > http->sbuffer)
1772 memmove(http->sbuffer, http->stream.next_in, http->stream.avail_in);
1774 http->stream.next_in = http->sbuffer;
1776 if (buflen > (size_t)http->data_remaining)
1777 buflen = (size_t)http->data_remaining;
1779 if (buflen > (size_t)http->used)
1780 buflen = (size_t)http->used;
1785 memcpy(http->sbuffer + http->stream.avail_in, http->buffer, buflen);
1786 http->stream.avail_in += buflen;
1787 http->used -= (int)buflen;
1788 http->data_remaining -= (off_t)buflen;
1790 if (http->used > 0)
1791 memmove(http->buffer, http->buffer + buflen, (size_t)http->used);
1795 (int)http->stream.avail_in));
1797 if (inflateCopy(&stream, &(http->stream)) != Z_OK)
1800 http->error = ENOMEM;
1814 http_debug_hex("2httpPeek", (char *)http->sbuffer, (int)http->stream.avail_in);
1817 http->error = EIO;
1821 bytes = (ssize_t)(length - http->stream.avail_out);
1831 if (http->used > 0)
1833 if (length > (size_t)http->used)
1834 length = (size_t)http->used;
1841 memcpy(buffer, http->buffer, length);
1852 http->error = WSAGetLastError();
1857 http->error = errno;
1862 http->error = EPIPE;
1875 httpPost(http_t *http, /* I - HTTP connection */
1878 return (http_send(http, HTTP_STATE_POST, uri));
1883 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1889 httpPrintf(http_t *http, /* I - HTTP connection */
1898 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http, format));
1906 if (http->data_encoding == HTTP_ENCODING_FIELDS)
1907 return ((int)httpWrite2(http, buf, (size_t)bytes));
1910 if (http->wused)
1914 if (httpFlushWrite(http) < 0)
1918 return ((int)http_write(http, buf, (size_t)bytes));
1928 httpPut(http_t *http, /* I - HTTP connection */
1931 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http, uri));
1932 return (http_send(http, HTTP_STATE_PUT, uri));
1937 * 'httpRead()' - Read data from a HTTP connection.
1946 httpRead(http_t *http, /* I - HTTP connection */
1950 return ((int)httpRead2(http, buffer, (size_t)length));
1955 * 'httpRead2()' - Read data from a HTTP connection.
1961 httpRead2(http_t *http, /* I - HTTP connection */
1969 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)http, (void *)buffer, CUPS_LLCAST length, http->coding, http->data_encoding, CUPS_LLCAST http->data_remaining));
1971 DEBUG_printf(("httpRead2(http=%p, buffer=%p, length=" CUPS_LLFMT ") data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)http, (void *)buffer, CUPS_LLCAST length, http->data_encoding, CUPS_LLCAST http->data_remaining));
1974 if (http == NULL || buffer == NULL)
1977 http->activity = time(NULL);
1978 http->error = 0;
1984 if (http->coding >= _HTTP_CODING_GUNZIP)
1988 if (http->stream.avail_in > 0)
1993 (int)http->stream.avail_in, (int)length));
1995 http->stream.next_out = (Bytef *)buffer;
1996 http->stream.avail_out = (uInt)length;
1998 if ((zerr = inflate(&(http->stream), Z_SYNC_FLUSH)) < Z_OK)
2002 http_debug_hex("2httpRead2", (char *)http->sbuffer, (int)http->stream.avail_in);
2005 http->error = EIO;
2009 bytes = (ssize_t)(length - http->stream.avail_out);
2012 http->stream.avail_in, http->stream.avail_out,
2020 ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)http->stream.avail_in;
2025 if (http->stream.avail_in > 0 &&
2026 http->stream.next_in > http->sbuffer)
2027 memmove(http->sbuffer, http->stream.next_in, http->stream.avail_in);
2029 http->stream.next_in = http->sbuffer;
2034 if (http->data_remaining > 0)
2036 if (buflen > http->data_remaining)
2037 buflen = (ssize_t)http->data_remaining;
2039 bytes = http_read_buffered(http, (char *)http->sbuffer + http->stream.avail_in, (size_t)buflen);
2041 else if (http->data_encoding == HTTP_ENCODING_CHUNKED)
2042 bytes = http_read_chunk(http, (char *)http->sbuffer + http->stream.avail_in, (size_t)buflen);
2054 http->data_remaining -= bytes;
2055 http->stream.avail_in += (uInt)bytes;
2057 if (http->data_remaining <= 0 &&
2058 http->data_encoding == HTTP_ENCODING_CHUNKED)
2066 httpGets(len, sizeof(len), http);
2079 if (http->data_remaining == 0 && http->data_encoding == HTTP_ENCODING_CHUNKED)
2081 if ((bytes = http_read_chunk(http, buffer, length)) > 0)
2083 http->data_remaining -= bytes;
2085 if (http->data_remaining <= 0)
2093 httpGets(len, sizeof(len), http);
2097 else if (http->data_remaining <= 0)
2110 if (length > (size_t)http->data_remaining)
2111 length = (size_t)http->data_remaining;
2113 if ((bytes = http_read_buffered(http, buffer, length)) > 0)
2115 http->data_remaining -= bytes;
2117 if (http->data_remaining <= 0 &&
2118 http->data_encoding == HTTP_ENCODING_CHUNKED)
2126 httpGets(len, sizeof(len), http);
2133 (http->coding == _HTTP_CODING_IDENTITY ||
2134 (http->coding >= _HTTP_CODING_GUNZIP && http->stream.avail_in == 0)) &&
2136 ((http->data_remaining <= 0 &&
2137 http->data_encoding == HTTP_ENCODING_LENGTH) ||
2138 (http->data_encoding == HTTP_ENCODING_CHUNKED && bytes == 0)))
2141 if (http->coding >= _HTTP_CODING_GUNZIP)
2142 http_content_coding_finish(http);
2145 if (http->state == HTTP_STATE_POST_RECV)
2146 http->state ++;
2147 else if (http->state == HTTP_STATE_GET_SEND ||
2148 http->state == HTTP_STATE_POST_SEND)
2149 http->state = HTTP_STATE_WAITING;
2151 http->state = HTTP_STATE_STATUS;
2154 httpStateString(http->state)));
2162 * 'httpReadRequest()' - Read a HTTP request from a connection.
2168 httpReadRequest(http_t *http, /* I - HTTP connection */
2172 char line[4096], /* HTTP request line */
2173 *req_method, /* HTTP request method */
2174 *req_uri, /* HTTP request URI */
2175 *req_version; /* HTTP request version number string */
2182 DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT ")", (void *)http, (void *)uri, CUPS_LLCAST urilen));
2187 if (!http || !uri || urilen < 1)
2192 else if (http->state != HTTP_STATE_WAITING)
2195 httpStateString(http->state)));
2203 httpClearFields(http);
2205 http->activity = time(NULL);
2206 http->data_encoding = HTTP_ENCODING_FIELDS;
2207 http->data_remaining = 0;
2208 http->keep_alive = HTTP_KEEPALIVE_OFF;
2209 http->status = HTTP_STATUS_OK;
2210 http->version = HTTP_VERSION_1_1;
2216 if (!httpGets(line, sizeof(line), http))
2274 http->state = HTTP_STATE_OPTIONS;
2276 http->state = HTTP_STATE_GET;
2278 http->state = HTTP_STATE_HEAD;
2280 http->state = HTTP_STATE_POST;
2282 http->state = HTTP_STATE_PUT;
2284 http->state = HTTP_STATE_DELETE;
2286 http->state = HTTP_STATE_TRACE;
2288 http->state = HTTP_STATE_CONNECT;
2297 httpStateString(http->state)));
2299 if (!strcmp(req_version, "HTTP/1.0"))
2301 http->version = HTTP_VERSION_1_0;
2302 http->keep_alive = HTTP_KEEPALIVE_OFF;
2304 else if (!strcmp(req_version, "HTTP/1.1"))
2306 http->version = HTTP_VERSION_1_1;
2307 http->keep_alive = HTTP_KEEPALIVE_ON;
2319 return (http->state);
2324 * 'httpReconnect()' - Reconnect to a HTTP server.
2333 httpReconnect(http_t *http) /* I - HTTP connection */
2335 DEBUG_printf(("httpReconnect(http=%p)", (void *)http));
2337 return (httpReconnect2(http, 30000, NULL));
2342 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2347 httpReconnect2(http_t *http, /* I - HTTP connection */
2358 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel));
2360 if (!http)
2367 if (http->tls)
2370 _httpTLSStop(http);
2378 if (http->fd >= 0)
2380 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http->fd));
2382 httpAddrClose(NULL, http->fd);
2384 http->fd = -1;
2391 http->state = HTTP_STATE_WAITING;
2392 http->version = HTTP_VERSION_1_1;
2393 http->keep_alive = HTTP_KEEPALIVE_OFF;
2394 memset(&http->_hostaddr, 0, sizeof(http->_hostaddr));
2395 http->data_encoding = HTTP_ENCODING_FIELDS;
2396 http->_data_remaining = 0;
2397 http->used = 0;
2398 http->data_remaining = 0;
2399 http->hostaddr = NULL;
2400 http->wused = 0;
2407 for (current = http->addrlist; current; current = current->next)
2413 if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, cancel)) == NULL)
2420 http->error = WSAGetLastError();
2422 http->error = errno;
2424 http->status = HTTP_STATUS_ERROR;
2427 strerror(http->error)));
2432 DEBUG_printf(("2httpReconnect2: New socket=%d", http->fd));
2434 if (http->timeout_value > 0)
2435 http_set_timeout(http->fd, http->timeout_value);
2437 http->hostaddr = &(addr->addr);
2438 http->error = 0;
2441 if (http->encryption == HTTP_ENCRYPTION_ALWAYS)
2447 if (_httpTLSStart(http) != 0)
2449 httpAddrClose(NULL, http->fd);
2454 else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls_upgrade)
2455 return (http_tls_upgrade(http));
2459 httpAddrString(http->hostaddr, temp, sizeof(temp)),
2460 httpAddrPort(http->hostaddr)));
2470 * the HTTP connection object. You must still call @link httpSetField@ to set
2471 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2479 httpSetAuthString(http_t *http, /* I - HTTP connection */
2487 if (!http)
2490 if (http->authstring && http->authstring != http->_authstring)
2491 free(http->authstring);
2493 http->authstring = http->_authstring;
2504 if (len > sizeof(http->_authstring))
2507 len = sizeof(http->_authstring);
2509 http->authstring = temp;
2513 snprintf(http->authstring, len, "%s %s", scheme, data);
2515 strlcpy(http->authstring, scheme, len);
2523 http->_authstring[0] = '\0';
2536 httpSetCredentials(http_t *http, /* I - HTTP connection */
2539 if (!http || cupsArrayCount(credentials) < 1)
2543 _httpFreeCredentials(http->tls_credentials);
2545 http->tls_credentials = _httpCreateCredentials(credentials);
2548 return (http->tls_credentials ? 0 : -1);
2559 httpSetCookie(http_t *http, /* I - Connection */
2562 if (!http)
2565 if (http->cookie)
2566 free(http->cookie);
2569 http->cookie = strdup(cookie);
2571 http->cookie = NULL;
2576 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2585 httpSetDefaultField(http_t *http, /* I - HTTP connection */
2589 DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, http_fields[field], value));
2591 if (!http)
2597 if (http->default_accept_encoding)
2598 _cupsStrFree(http->default_accept_encoding);
2600 http->default_accept_encoding = value ? _cupsStrAlloc(value) : NULL;
2604 if (http->default_server)
2605 _cupsStrFree(http->default_server);
2607 http->default_server = value ? _cupsStrAlloc(value) : NULL;
2611 if (http->default_user_agent)
2612 _cupsStrFree(http->default_user_agent);
2614 http->default_user_agent = value ? _cupsStrAlloc(value) : NULL;
2634 httpSetExpect(http_t *http, /* I - HTTP connection */
2635 http_status_t expect) /* I - HTTP status to expect
2638 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http, expect));
2640 if (http)
2641 http->expect = expect;
2646 * 'httpSetField()' - Set the value of an HTTP header.
2650 httpSetField(http_t *http, /* I - HTTP connection */
2654 DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, http_fields[field], value));
2656 if (http == NULL ||
2665 if (http->accept_encoding)
2666 _cupsStrFree(http->accept_encoding);
2668 http->accept_encoding = _cupsStrAlloc(value);
2672 if (http->allow)
2673 _cupsStrFree(http->allow);
2675 http->allow = _cupsStrAlloc(value);
2679 if (http->server)
2680 _cupsStrFree(http->server);
2682 http->server = _cupsStrAlloc(value);
2687 if (http->fields[HTTP_FIELD_WWW_AUTHENTICATE][0] &&
2699 strlcpy(http->fields[field], value, HTTP_MAX_VALUE);
2710 if (http->field_authorization)
2711 free(http->field_authorization);
2713 http->field_authorization = strdup(value);
2733 snprintf(http->fields[HTTP_FIELD_HOST],
2734 sizeof(http->fields[HTTP_FIELD_HOST]), "[%s]", value);
2742 ptr = http->fields[HTTP_FIELD_HOST];
2755 http->data_encoding != HTTP_ENCODING_FIELDS)
2758 http_content_coding_start(http, value);
2772 http_t *http, /* I - HTTP
2775 if (http)
2776 http->keep_alive = keep_alive;
2787 httpSetLength(http_t *http, /* I - HTTP connection */
2790 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT ")", (void *)http, CUPS_LLCAST length));
2792 if (!http)
2797 strlcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked",
2799 http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0';
2803 http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0';
2804 snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE,
2813 * The optional timeout callback receives both the HTTP connection and a user
2821 http_t *http, /* I - HTTP connection */
2827 if (!http || timeout <= 0.0)
2830 http->timeout_cb = cb;
2831 http->timeout_data = user_data;
2832 http->timeout_value = timeout;
2834 if (http->fd >= 0)
2835 http_set_timeout(http->fd, timeout);
2837 http_set_wait(http);
2842 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2848 httpShutdown(http_t *http) /* I - HTTP connection */
2850 if (!http || http->fd < 0)
2854 if (http->tls)
2855 _httpTLSStop(http);
2859 shutdown(http->fd, SD_RECEIVE); /* Microsoft-ism... */
2861 shutdown(http->fd, SHUT_RD);
2873 httpTrace(http_t *http, /* I - HTTP connection */
2876 return (http_send(http, HTTP_STATE_TRACE, uri));
2881 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2884 * and only retrieves a single status line from the HTTP connection.
2888 _httpUpdate(http_t *http, /* I - HTTP connection */
2889 http_status_t *status) /* O - Current HTTP status */
2894 int major, minor; /* HTTP version numbers */
2897 DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpStateString(http->state)));
2903 if (!httpGets(line, sizeof(line), http))
2922 if (http->status == HTTP_STATUS_CONTINUE)
2924 *status = http->status;
2928 if (http->status < HTTP_STATUS_BAD_REQUEST)
2929 http->digest_tries = 0;
2932 if (http->status == HTTP_STATUS_SWITCHING_PROTOCOLS && !http->tls)
2934 if (_httpTLSStart(http) != 0)
2936 httpAddrClose(NULL, http->fd);
2938 *status = http->status = HTTP_STATUS_ERROR;
2947 if (http_set_length(http) < 0)
2950 http->error = EINVAL;
2951 http->status = *status = HTTP_STATUS_ERROR;
2955 switch (http->state)
2961 http->state ++;
2964 httpStateString(http->state)));
2971 http->state = HTTP_STATE_WAITING;
2979 http_content_coding_start(http,
2980 httpGetField(http, HTTP_FIELD_CONTENT_ENCODING));
2983 *status = http->status;
2986 else if (!strncmp(line, "HTTP/", 5) && http->mode == _HTTP_MODE_CLIENT)
2994 if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &intstatus) != 3)
2996 *status = http->status = HTTP_STATUS_ERROR;
3000 httpClearFields(http);
3002 http->version = (http_version_t)(major * 100 + minor);
3003 *status = http->status = (http_status_t)intstatus;
3027 http->expect = (http_status_t)atoi(value);
3035 httpSetCookie(http, value);
3038 httpSetField(http, field, value);
3047 http->error = EINVAL;
3048 http->status = *status = HTTP_STATUS_ERROR;
3057 * 'httpUpdate()' - Update the current HTTP state for incoming data.
3060 http_status_t /* O - HTTP status */
3061 httpUpdate(http_t *http) /* I - HTTP connection */
3066 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http, httpStateString(http->state)));
3072 if (http->wused)
3076 if (httpFlushWrite(http) < 0)
3084 if (http->state == HTTP_STATE_WAITING)
3091 while (_httpUpdate(http, &status));
3097 if (http->error == EPIPE && http->status > HTTP_STATUS_CONTINUE)
3099 DEBUG_printf(("1httpUpdate: Returning status %d...", http->status));
3100 return (http->status);
3103 if (http->error)
3105 DEBUG_printf(("1httpUpdate: socket error %d - %s", http->error,
3106 strerror(http->error)));
3107 http->status = HTTP_STATUS_ERROR;
3124 _httpWait(http_t *http, /* I - HTTP connection */
3137 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http, msec, usessl));
3139 if (http->fd < 0)
3141 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http->fd));
3150 if (http->tls && _httpTLSPending(http))
3162 pfd.fd = http->fd;
3175 FD_SET(http->fd, &input_set);
3177 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec, http->fd));
3184 nfds = select(http->fd + 1, &input_set, NULL, NULL, &timeout);
3187 nfds = select(http->fd + 1, &input_set, NULL, NULL, NULL);
3213 httpWait(http_t *http, /* I - HTTP connection */
3220 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http, msec));
3222 if (http == NULL)
3225 if (http->used)
3232 if (http->coding >= _HTTP_CODING_GUNZIP && http->stream.avail_in > 0)
3243 if (http->wused)
3247 if (httpFlushWrite(http) < 0)
3255 return (_httpWait(http, msec, 1));
3260 * 'httpWrite()' - Write data to a HTTP connection.
3269 httpWrite(http_t *http, /* I - HTTP connection */
3273 return ((int)httpWrite2(http, buffer, (size_t)length));
3278 * 'httpWrite2()' - Write data to a HTTP connection.
3284 httpWrite2(http_t *http, /* I - HTTP connection */
3291 DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
3297 if (!http || !buffer)
3307 http->activity = time(NULL);
3314 if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE)
3316 DEBUG_printf(("1httpWrite2: http->coding=%d", http->coding));
3320 http_content_coding_finish(http);
3328 http->stream.next_in = (Bytef *)buffer;
3329 http->stream.avail_in = (uInt)length;
3331 while (deflate(&(http->stream), Z_NO_FLUSH) == Z_OK)
3333 DEBUG_printf(("1httpWrite2: avail_out=%d", http->stream.avail_out));
3335 if (http->stream.avail_out > 0)
3338 slen = _HTTP_MAX_SBUFFER - http->stream.avail_out;
3342 if (slen > 0 && http->data_encoding == HTTP_ENCODING_CHUNKED)
3343 sret = http_write_chunk(http, (char *)http->sbuffer, slen);
3345 sret = http_write(http, (char *)http->sbuffer, slen);
3355 http->stream.next_out = (Bytef *)http->sbuffer;
3356 http->stream.avail_out = (uInt)_HTTP_MAX_SBUFFER;
3366 if (http->wused && (length + (size_t)http->wused) > sizeof(http->wbuffer))
3369 CUPS_LLFMT ")", http->wused, CUPS_LLCAST length));
3371 httpFlushWrite(http);
3374 if ((length + (size_t)http->wused) <= sizeof(http->wbuffer) && length < sizeof(http->wbuffer))
3383 memcpy(http->wbuffer + http->wused, buffer, length);
3384 http->wused += (int)length;
3396 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
3397 bytes = (ssize_t)http_write_chunk(http, buffer, length);
3399 bytes = (ssize_t)http_write(http, buffer, length);
3405 if (http->data_encoding == HTTP_ENCODING_LENGTH)
3406 http->data_remaining -= bytes;
3415 if ((http->data_encoding == HTTP_ENCODING_CHUNKED && length == 0) ||
3416 (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0))
3424 if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE)
3425 http_content_coding_finish(http);
3428 if (http->wused)
3430 if (httpFlushWrite(http) < 0)
3434 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
3440 http_write(http, "0\r\n\r\n", 5);
3446 http->data_encoding = HTTP_ENCODING_FIELDS;
3447 http->data_remaining = 0;
3450 if (http->state == HTTP_STATE_POST_RECV)
3451 http->state ++;
3452 else if (http->state == HTTP_STATE_POST_SEND ||
3453 http->state == HTTP_STATE_GET_SEND)
3454 http->state = HTTP_STATE_WAITING;
3456 http->state = HTTP_STATE_STATUS;
3459 httpStateString(http->state)));
3469 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3475 httpWriteResponse(http_t *http, /* I - HTTP connection */
3486 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http, status));
3488 if (!http || status < HTTP_STATUS_CONTINUE)
3498 if (!http->fields[HTTP_FIELD_DATE][0])
3499 httpSetField(http, HTTP_FIELD_DATE, httpGetDateString(time(NULL)));
3501 if (status >= HTTP_STATUS_BAD_REQUEST && http->keep_alive)
3503 http->keep_alive = HTTP_KEEPALIVE_OFF;
3504 httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "");
3507 if (http->version == HTTP_VERSION_1_1)
3509 if (!http->fields[HTTP_FIELD_CONNECTION][0])
3511 if (http->keep_alive)
3512 httpSetField(http, HTTP_FIELD_CONNECTION, "Keep-Alive");
3514 httpSetField(http, HTTP_FIELD_CONNECTION, "close");
3517 if (http->keep_alive && !http->fields[HTTP_FIELD_KEEP_ALIVE][0])
3518 httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "timeout=10");
3525 if (!http->fields[HTTP_FIELD_CONNECTION][0])
3526 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade");
3528 if (!http->fields[HTTP_FIELD_UPGRADE][0])
3529 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0");
3531 if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0])
3532 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "0");
3536 if (!http->server)
3537 httpSetField(http, HTTP_FIELD_SERVER,
3538 http->default_server ? http->default_server : CUPS_MINIMAL);
3544 if (!http->accept_encoding)
3545 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING,
3546 http->default_accept_encoding ? http->default_accept_encoding :
3557 old_encoding = http->data_encoding;
3558 old_remaining = http->data_remaining;
3559 http->data_encoding = HTTP_ENCODING_FIELDS;
3561 if (httpPrintf(http, "HTTP/%d.%d %d %s\r\n", http->version / 100,
3562 http->version % 100, (int)status, httpStatus(status)) < 0)
3564 http->status = HTTP_STATUS_ERROR;
3579 if ((value = httpGetField(http, i)) != NULL && *value)
3581 if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1)
3583 http->status = HTTP_STATUS_ERROR;
3589 if (http->cookie)
3591 if (strchr(http->cookie, ';'))
3593 if (httpPrintf(http, "Set-Cookie: %s\r\n", http->cookie) < 1)
3595 http->status = HTTP_STATUS_ERROR;
3599 else if (httpPrintf(http, "Set-Cookie: %s; path=/; httponly;%s\r\n", http->cookie, http->tls ? " secure;" : "") < 1)
3601 http->status = HTTP_STATUS_ERROR;
3610 if (httpPrintf(http, "X-Frame-Options: DENY\r\n"
3613 http->status = HTTP_STATUS_ERROR;
3618 if (httpWrite2(http, "\r\n", 2) < 2)
3620 http->status = HTTP_STATUS_ERROR;
3624 if (httpFlushWrite(http) < 0)
3626 http->status = HTTP_STATUS_ERROR;
3637 http->data_encoding = old_encoding;
3638 http->data_remaining = old_remaining;
3641 http->_data_remaining = (int)old_remaining;
3643 http->_data_remaining = INT_MAX;
3645 else if (http->state == HTTP_STATE_OPTIONS ||
3646 http->state == HTTP_STATE_HEAD ||
3647 http->state == HTTP_STATE_PUT ||
3648 http->state == HTTP_STATE_TRACE ||
3649 http->state == HTTP_STATE_CONNECT ||
3650 http->state == HTTP_STATE_STATUS)
3653 "was %s.", httpStateString(http->state)));
3654 http->state = HTTP_STATE_WAITING;
3663 http_set_length(http);
3665 if (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0)
3668 "was %s.", httpStateString(http->state)));
3669 http->state = HTTP_STATE_WAITING;
3673 if (http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_GET)
3674 http->state ++;
3682 http_content_coding_start(http,
3683 httpGetField(http, HTTP_FIELD_CONTENT_ENCODING));
3699 http_t *http) /* I - HTTP connection */
3706 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http));
3707 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http->coding));
3709 switch (http->coding)
3713 http->stream.next_in = dummy;
3714 http->stream.avail_in = 0;
3718 zerr = deflate(&(http->stream), Z_FINISH);
3719 bytes = _HTTP_MAX_SBUFFER - http->stream.avail_out;
3725 if (http->data_encoding == HTTP_ENCODING_CHUNKED)
3726 http_write_chunk(http, (char *)http->sbuffer, bytes);
3728 http_write(http, (char *)http->sbuffer, bytes);
3731 http->stream.next_out = (Bytef *)http->sbuffer;
3732 http->stream.avail_out = (uInt)_HTTP_MAX_SBUFFER;
3736 deflateEnd(&(http->stream));
3738 free(http->sbuffer);
3739 http->sbuffer = NULL;
3741 if (http->wused)
3742 httpFlushWrite(http);
3747 inflateEnd(&(http->stream));
3748 free(http->sbuffer);
3749 http->sbuffer = NULL;
3756 http->coding = _HTTP_CODING_IDENTITY;
3766 http_t *http, /* I - HTTP connection */
3773 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http, value));
3775 if (http->coding != _HTTP_CODING_IDENTITY)
3777 DEBUG_printf(("1http_content_coding_start: http->coding already %d.",
3778 http->coding));
3783 if (http->state == HTTP_STATE_GET_SEND ||
3784 http->state == HTTP_STATE_POST_SEND)
3785 coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_GZIP :
3787 else if (http->state == HTTP_STATE_POST_RECV ||
3788 http->state == HTTP_STATE_PUT_RECV)
3789 coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_GZIP :
3799 if (http->state == HTTP_STATE_GET_SEND ||
3800 http->state == HTTP_STATE_POST_SEND)
3801 coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_DEFLATE :
3803 else if (http->state == HTTP_STATE_POST_RECV ||
3804 http->state == HTTP_STATE_PUT_RECV)
3805 coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_DEFLATE :
3819 memset(&(http->stream), 0, sizeof(http->stream));
3825 if (http->wused)
3826 httpFlushWrite(http);
3828 if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL)
3830 http->status = HTTP_STATUS_ERROR;
3831 http->error = errno;
3841 if ((zerr = deflateInit2(&(http->stream), Z_DEFAULT_COMPRESSION,
3846 http->status = HTTP_STATUS_ERROR;
3847 http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL;
3851 http->stream.next_out = (Bytef *)http->sbuffer;
3852 http->stream.avail_out = (uInt)_HTTP_MAX_SBUFFER;
3857 if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL)
3859 http->status = HTTP_STATUS_ERROR;
3860 http->error = errno;
3869 if ((zerr = inflateInit2(&(http->stream),
3873 free(http->sbuffer);
3874 http->sbuffer = NULL;
3875 http->status = HTTP_STATUS_ERROR;
3876 http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL;
3880 http->stream.avail_in = 0;
3881 http->stream.next_in = http->sbuffer;
3888 http->coding = coding;
3890 DEBUG_printf(("1http_content_coding_start: http->coding now %d.",
3891 http->coding));
3897 * 'http_create()' - Create an unconnected HTTP connection.
3900 static http_t * /* O - HTTP connection */
3910 http_t *http; /* New HTTP connection */
3944 if ((http = calloc(sizeof(http_t), 1)) == NULL)
3952 * Initialize the HTTP data...
3955 http->mode = mode;
3956 http->activity = time(NULL);
3957 http->addrlist = myaddrlist;
3958 http->blocking = blocking;
3959 http->fd = -1;
3961 http->gssctx = GSS_C_NO_CONTEXT;
3962 http->gssname = GSS_C_NO_NAME;
3964 http->status = HTTP_STATUS_CONTINUE;
3965 http->version = HTTP_VERSION_1_1;
3968 strlcpy(http->hostname, host, sizeof(http->hostname));
3971 http->encryption = HTTP_ENCRYPTION_ALWAYS;
3973 http->encryption = encryption;
3975 http_set_wait(http);
3981 return (http);
4043 * 'http_read()' - Read a buffer from a HTTP connection.
4050 http_read(http_t *http, /* I - HTTP connection */
4057 DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
4059 if (!http->blocking)
4061 while (!httpWait(http, http->wait_value))
4063 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
4076 if (http->tls)
4077 bytes = _httpTLSRead(http, buffer, (int)length);
4080 bytes = recv(http->fd, buffer, length, 0);
4087 http->error = WSAGetLastError();
4092 if (!http->timeout_cb ||
4093 !(*http->timeout_cb)(http, http->timeout_data))
4095 http->error = WSAEWOULDBLOCK;
4104 if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data))
4106 http->error = errno;
4109 else if (!http->timeout_cb && errno != EAGAIN)
4111 http->error = errno;
4117 http->error = errno;
4138 http->error = WSAGetLastError();
4140 if (errno == EINTR || (errno == EAGAIN && !http->timeout_cb))
4143 http->error = errno;
4148 http->error = EPIPE;
4157 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4159 * This function reads data from the HTTP buffer or from the socket, as needed.
4163 http_read_buffered(http_t *http, /* I - HTTP connection */
4170 DEBUG_printf(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT ") used=%d", (void *)http, (void *)buffer, CUPS_LLCAST length, http->used));
4172 if (http->used > 0)
4174 if (length > (size_t)http->used)
4175 bytes = (ssize_t)http->used;
4182 memcpy(buffer, http->buffer, (size_t)bytes);
4183 http->used -= (int)bytes;
4185 if (http->used > 0)
4186 memmove(http->buffer, http->buffer + bytes, (size_t)http->used);
4189 bytes = http_read(http, buffer, length);
4196 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4203 http_read_chunk(http_t *http, /* I - HTTP connection */
4207 DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
4209 if (http->data_remaining <= 0)
4213 if (!httpGets(len, sizeof(len), http))
4222 if (!httpGets(len, sizeof(len), http))
4229 http->data_remaining = strtoll(len, NULL, 16);
4231 if (http->data_remaining < 0)
4234 CUPS_LLFMT ")", len, CUPS_LLCAST http->data_remaining));
4239 len, CUPS_LLCAST http->data_remaining));
4241 if (http->data_remaining == 0)
4247 httpGets(len, sizeof(len), http);
4252 CUPS_LLCAST http->data_remaining));
4254 if (http->data_remaining <= 0)
4256 else if (length > (size_t)http->data_remaining)
4257 length = (size_t)http->data_remaining;
4259 return (http_read_buffered(http, buffer, length));
4268 http_send(http_t *http, /* I - HTTP connection */
4295 DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http, codes[request], uri));
4297 if (http == NULL || uri == NULL)
4304 if (!http->fields[HTTP_FIELD_USER_AGENT][0])
4306 if (http->default_user_agent)
4307 httpSetField(http, HTTP_FIELD_USER_AGENT, http->default_user_agent);
4309 httpSetField(http, HTTP_FIELD_USER_AGENT, cupsUserAgent());
4316 if (!http->accept_encoding && http->default_accept_encoding)
4317 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING,
4318 http->default_accept_encoding);
4330 if (http->fd < 0 || http->status == HTTP_STATUS_ERROR ||
4331 http->status >= HTTP_STATUS_BAD_REQUEST)
4334 http->fd, http->status, http->tls_upgrade));
4336 if (httpReconnect2(http, 30000, NULL))
4344 if (http->wused)
4346 if (httpFlushWrite(http) < 0)
4347 if (httpReconnect2(http, 30000, NULL))
4355 http->state = request;
4356 http->data_encoding = HTTP_ENCODING_FIELDS;
4359 http->state ++;
4361 http->status = HTTP_STATUS_CONTINUE;
4364 if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls)
4366 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade");
4367 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0");
4371 if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1)
4373 http->status = HTTP_STATUS_ERROR;
4378 if ((value = httpGetField(http, i)) != NULL && *value)
4384 if (httpPrintf(http, "Host: %s:%d\r\n", value,
4385 httpAddrPort(http->hostaddr)) < 1)
4387 http->status = HTTP_STATUS_ERROR;
4391 else if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1)
4393 http->status = HTTP_STATUS_ERROR;
4398 if (http->cookie)
4399 if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1)
4401 http->status = HTTP_STATUS_ERROR;
4405 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http->expect,
4406 http->mode, http->state));
4408 if (http->expect == HTTP_STATUS_CONTINUE && http->mode == _HTTP_MODE_CLIENT &&
4409 (http->state == HTTP_STATE_POST_RECV ||
4410 http->state == HTTP_STATE_PUT_RECV))
4411 if (httpPrintf(http, "Expect: 100-continue\r\n") < 1)
4413 http->status = HTTP_STATUS_ERROR;
4417 if (httpPrintf(http, "\r\n") < 1)
4419 http->status = HTTP_STATUS_ERROR;
4423 if (httpFlushWrite(http) < 0)
4426 http_set_length(http);
4427 httpClearFields(http);
4433 if (http->field_authorization && http->authstring &&
4434 (!strncmp(http->authstring, "Negotiate", 9) ||
4435 !strncmp(http->authstring, "AuthRef", 7)))
4437 http->_authstring[0] = '\0';
4439 if (http->authstring != http->_authstring)
4440 free(http->authstring);
4442 http->authstring = http->_authstring;
4454 http_set_length(http_t *http) /* I - Connection */
4459 DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http, http->mode, httpStateString(http->state)));
4461 if ((remaining = httpGetLength2(http)) >= 0)
4463 if (http->mode == _HTTP_MODE_SERVER &&
4464 http->state != HTTP_STATE_GET_SEND &&
4465 http->state != HTTP_STATE_PUT &&
4466 http->state != HTTP_STATE_POST &&
4467 http->state != HTTP_STATE_POST_SEND)
4473 if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING],
4478 http->data_encoding = HTTP_ENCODING_CHUNKED;
4484 http->data_encoding = HTTP_ENCODING_LENGTH;
4489 http->data_remaining = remaining;
4492 http->_data_remaining = (int)remaining;
4494 http->_data_remaining = INT_MAX;
4532 http_set_wait(http_t *http) /* I - HTTP connection */
4534 if (http->blocking)
4536 http->wait_value = (int)(http->timeout_value * 1000);
4538 if (http->wait_value <= 0)
4539 http->wait_value = 60000;
4542 http->wait_value = 10000;
4552 http_tls_upgrade(http_t *http) /* I - HTTP connection */
4555 http_t myhttp; /* Local copy of HTTP data */
4558 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http));
4565 httpFlush(http);
4568 * Copy the HTTP data to a local variable so we can do the OPTIONS
4572 memcpy(&myhttp, http, sizeof(myhttp));
4579 http->tls_upgrade = 1;
4580 http->field_authorization = NULL; /* Don't free the auth string */
4582 httpClearFields(http);
4583 httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade");
4584 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0");
4586 if ((ret = httpOptions(http, "*")) == 0)
4592 while (httpUpdate(http) == HTTP_STATUS_CONTINUE);
4596 * Restore the HTTP request data...
4599 memcpy(http->fields, myhttp.fields, sizeof(http->fields));
4600 http->data_encoding = myhttp.data_encoding;
4601 http->data_remaining = myhttp.data_remaining;
4602 http->_data_remaining = myhttp._data_remaining;
4603 http->expect = myhttp.expect;
4604 http->field_authorization = myhttp.field_authorization;
4605 http->digest_tries = myhttp.digest_tries;
4606 http->tls_upgrade = 0;
4612 if (!http->tls)
4615 * Server does not support HTTP upgrade...
4618 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!");
4621 httpAddrClose(NULL, http->fd);
4623 http->fd = -1;
4634 * 'http_write()' - Write a buffer to a HTTP connection.
4638 http_write(http_t *http, /* I - HTTP connection */
4646 DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
4647 http->error = 0;
4654 if (http->timeout_cb)
4667 pfd.fd = http->fd;
4670 while ((nfds = poll(&pfd, 1, http->wait_value)) < 0 &&
4678 FD_SET(http->fd, &output_set);
4680 timeout.tv_sec = http->wait_value / 1000;
4681 timeout.tv_usec = 1000 * (http->wait_value % 1000);
4683 nfds = select(http->fd + 1, NULL, &output_set, NULL, &timeout);
4695 http->error = errno;
4698 else if (nfds == 0 && !(*http->timeout_cb)(http, http->timeout_data))
4701 http->error = WSAEWOULDBLOCK;
4703 http->error = EWOULDBLOCK;
4712 if (http->tls)
4713 bytes = _httpTLSWrite(http, buffer, (int)length);
4716 bytes = send(http->fd, buffer, length, 0);
4728 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
4731 http->error = WSAGetLastError();
4733 else if (WSAGetLastError() != http->error &&
4736 http->error = WSAGetLastError();
4745 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
4747 else if (!http->timeout_cb && errno == EAGAIN)
4750 http->error = errno;
4752 else if (errno != http->error && errno != ECONNRESET)
4754 http->error = errno;
4760 strerror(http->error)));
4785 http_write_chunk(http_t *http, /* I - HTTP connection */
4793 DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length));
4800 if (http_write(http, header, strlen(header)) < 0)
4806 if ((bytes = http_write(http, buffer, length)) < 0)
4812 if (http_write(http, "\r\n", 2) < 0)