Lines Matching full:http
2 * HTTP routines for CUPS.
41 static void http_add_field(http_t *http, http_field_t field, const char *value, int append);
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);
114 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
120 http_t * /* O - HTTP connection or @code NULL@ */
125 http_t *http; /* HTTP connection */ in httpAcceptConnection() local
144 if ((http = http_create(NULL, 0, &addrlist, AF_UNSPEC, in httpAcceptConnection()
155 if ((http->fd = accept(fd, (struct sockaddr *)&(http->addrlist->addr), in httpAcceptConnection()
159 httpClose(http); in httpAcceptConnection()
164 http->hostaddr = &(http->addrlist->addr); in httpAcceptConnection()
166 if (httpAddrLocalhost(http->hostaddr)) in httpAcceptConnection()
167 strlcpy(http->hostname, "localhost", sizeof(http->hostname)); in httpAcceptConnection()
169 httpAddrString(http->hostaddr, http->hostname, sizeof(http->hostname)); in httpAcceptConnection()
177 setsockopt(http->fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val)); in httpAcceptConnection()
188 setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val)); in httpAcceptConnection()
195 fcntl(http->fd, F_SETFD, FD_CLOEXEC); in httpAcceptConnection()
198 return (http); in httpAcceptConnection()
242 httpBlocking(http_t *http, /* I - HTTP connection */ in httpBlocking() argument
245 if (http) in httpBlocking()
247 http->blocking = b; in httpBlocking()
248 http_set_wait(http); in httpBlocking()
258 httpCheck(http_t *http) /* I - HTTP connection */ in httpCheck() argument
260 return (httpWait(http, 0)); in httpCheck()
271 httpClearCookie(http_t *http) /* I - HTTP connection */ in httpClearCookie() argument
273 if (!http) in httpClearCookie()
276 if (http->cookie) in httpClearCookie()
278 free(http->cookie); in httpClearCookie()
279 http->cookie = NULL; in httpClearCookie()
285 * 'httpClearFields()' - Clear HTTP request fields.
289 httpClearFields(http_t *http) /* I - HTTP connection */ in httpClearFields() argument
294 DEBUG_printf(("httpClearFields(http=%p)", (void *)http)); in httpClearFields()
296 if (http) in httpClearFields()
298 memset(http->_fields, 0, sizeof(http->fields)); in httpClearFields()
302 if (http->fields[field] && http->fields[field] != http->_fields[field]) in httpClearFields()
303 free(http->fields[field]); in httpClearFields()
305 http->fields[field] = NULL; in httpClearFields()
308 if (http->mode == _HTTP_MODE_CLIENT) in httpClearFields()
310 if (http->hostname[0] == '/') in httpClearFields()
311 httpSetField(http, HTTP_FIELD_HOST, "localhost"); in httpClearFields()
313 httpSetField(http, HTTP_FIELD_HOST, http->hostname); in httpClearFields()
316 http->expect = (http_status_t)0; in httpClearFields()
322 * 'httpClose()' - Close an HTTP connection.
326 httpClose(http_t *http) /* I - HTTP connection */ in httpClose() argument
333 DEBUG_printf(("httpClose(http=%p)", (void *)http)); in httpClose()
339 if (!http) in httpClose()
346 _httpDisconnect(http); in httpClose()
352 httpAddrFreeList(http->addrlist); in httpClose()
354 if (http->cookie) in httpClose()
355 free(http->cookie); in httpClose()
358 if (http->gssctx != GSS_C_NO_CONTEXT) in httpClose()
359 gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); in httpClose()
361 if (http->gssname != GSS_C_NO_NAME) in httpClose()
362 gss_release_name(&minor_status, &http->gssname); in httpClose()
366 if (http->auth_ref) in httpClose()
367 AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); in httpClose()
370 httpClearFields(http); in httpClose()
372 if (http->authstring && http->authstring != http->_authstring) in httpClose()
373 free(http->authstring); in httpClose()
375 free(http); in httpClose()
404 * 'httpConnect()' - Connect to a HTTP server.
411 http_t * /* O - New HTTP connection */
421 * 'httpConnect2()' - Connect to a HTTP server.
426 http_t * /* O - New HTTP connection */
437 http_t *http; /* New HTTP connection */ in httpConnect2() local
443 * Create the HTTP structure... in httpConnect2()
446 if ((http = http_create(host, port, addrlist, family, encryption, blocking, in httpConnect2()
454 if (msec == 0 || !httpReconnect2(http, msec, cancel)) in httpConnect2()
455 return (http); in httpConnect2()
461 httpClose(http); in httpConnect2()
468 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
476 http_t * /* O - New HTTP connection */
495 httpDelete(http_t *http, /* I - HTTP connection */ in httpDelete() argument
498 return (http_send(http, HTTP_STATE_DELETE, uri)); in httpDelete()
503 * '_httpDisconnect()' - Disconnect a HTTP connection.
507 _httpDisconnect(http_t *http) /* I - HTTP connection */ in _httpDisconnect() argument
510 if (http->tls) in _httpDisconnect()
511 _httpTLSStop(http); in _httpDisconnect()
514 httpAddrClose(NULL, http->fd); in _httpDisconnect()
516 http->fd = -1; in _httpDisconnect()
525 httpEncryption(http_t *http, /* I - HTTP connection */ in httpEncryption() argument
528 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http, e)); in httpEncryption()
531 if (!http) in httpEncryption()
534 if (http->mode == _HTTP_MODE_CLIENT) in httpEncryption()
536 http->encryption = e; in httpEncryption()
538 if ((http->encryption == HTTP_ENCRYPTION_ALWAYS && !http->tls) || in httpEncryption()
539 (http->encryption == HTTP_ENCRYPTION_NEVER && http->tls)) in httpEncryption()
540 return (httpReconnect2(http, 30000, NULL)); in httpEncryption()
541 else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls) in httpEncryption()
542 return (http_tls_upgrade(http)); in httpEncryption()
548 if (e == HTTP_ENCRYPTION_NEVER && http->tls) in httpEncryption()
551 http->encryption = e; in httpEncryption()
552 if (e != HTTP_ENCRYPTION_IF_REQUESTED && !http->tls) in httpEncryption()
553 return (_httpTLSStart(http)); in httpEncryption()
571 httpError(http_t *http) /* I - HTTP connection */ in httpError() argument
573 if (http) in httpError()
574 return (http->error); in httpError()
581 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
600 * 'httpFlush()' - Flush data read from a HTTP connection.
604 httpFlush(http_t *http) /* I - HTTP connection */ in httpFlush() argument
611 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http, httpStateString(http->state))); in httpFlush()
617 if (http->state == HTTP_STATE_WAITING) in httpFlush()
624 blocking = http->blocking; in httpFlush()
625 http->blocking = 0; in httpFlush()
631 oldstate = http->state; in httpFlush()
632 while (httpRead2(http, buffer, sizeof(buffer)) > 0); in httpFlush()
639 http->blocking = blocking; in httpFlush()
641 if (http->state == oldstate && http->state != HTTP_STATE_WAITING && in httpFlush()
642 http->fd >= 0) in httpFlush()
649 if (http->coding) in httpFlush()
650 http_content_coding_finish(http); in httpFlush()
655 http->state = HTTP_STATE_WAITING; in httpFlush()
658 if (http->tls) in httpFlush()
659 _httpTLSStop(http); in httpFlush()
662 httpAddrClose(NULL, http->fd); in httpFlush()
664 http->fd = -1; in httpFlush()
670 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
676 httpFlushWrite(http_t *http) /* I - HTTP connection */ in httpFlushWrite() argument
681 …DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http, http ? http->data_encoding… in httpFlushWrite()
683 if (!http || !http->wused) in httpFlushWrite()
685 DEBUG_puts(http ? "1httpFlushWrite: Write buffer is empty." : in httpFlushWrite()
690 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpFlushWrite()
691 bytes = http_write_chunk(http, http->wbuffer, (size_t)http->wused); in httpFlushWrite()
693 bytes = http_write(http, http->wbuffer, (size_t)http->wused); in httpFlushWrite()
695 http->wused = 0; in httpFlushWrite()
732 httpGet(http_t *http, /* I - HTTP connection */ in httpGet() argument
735 return (http_send(http, HTTP_STATE_GET, uri)); in httpGet()
748 httpGetActivity(http_t *http) /* I - HTTP connection */ in httpGetActivity() argument
750 return (http ? http->activity : 0); in httpGetActivity()
766 httpGetAuthString(http_t *http) /* I - HTTP connection */ in httpGetAuthString() argument
768 if (http) in httpGetAuthString()
769 return (http->authstring); in httpGetAuthString()
782 httpGetBlocking(http_t *http) /* I - HTTP connection */ in httpGetBlocking() argument
784 return (http ? http->blocking : 0); in httpGetBlocking()
792 * This function uses the value of the Accepts-Encoding HTTP header and must be
803 httpGetContentEncoding(http_t *http) /* I - HTTP connection */ in httpGetContentEncoding() argument
806 if (http && http->fields[HTTP_FIELD_ACCEPT_ENCODING]) in httpGetContentEncoding()
822 strlcpy(temp, http->fields[HTTP_FIELD_ACCEPT_ENCODING], sizeof(temp)); in httpGetContentEncoding()
883 httpGetCookie(http_t *http) /* I - HTTP connection */ in httpGetCookie() argument
885 return (http ? http->cookie : NULL); in httpGetCookie()
900 httpGetEncryption(http_t *http) /* I - HTTP connection */ in httpGetEncryption() argument
902 return (http ? http->encryption : HTTP_ENCRYPTION_IF_REQUESTED); in httpGetEncryption()
910 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
916 httpGetExpect(http_t *http) /* I - HTTP connection */ in httpGetExpect() argument
918 if (!http) in httpGetExpect()
921 return (http->expect); in httpGetExpect()
932 httpGetFd(http_t *http) /* I - HTTP connection */ in httpGetFd() argument
934 return (http ? http->fd : -1); in httpGetFd()
943 httpGetField(http_t *http, /* I - HTTP connection */ in httpGetField() argument
946 if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) in httpGetField()
948 else if (http->fields[field]) in httpGetField()
949 return (http->fields[field]); in httpGetField()
962 httpGetKeepAlive(http_t *http) /* I - HTTP connection */ in httpGetKeepAlive() argument
964 return (http ? http->keep_alive : HTTP_KEEPALIVE_OFF); in httpGetKeepAlive()
979 httpGetLength(http_t *http) /* I - HTTP connection */ in httpGetLength() argument
985 if (http) in httpGetLength()
987 httpGetLength2(http); in httpGetLength()
989 return (http->_data_remaining); in httpGetLength()
1007 httpGetLength2(http_t *http) /* I - HTTP connection */ in httpGetLength2() argument
1012 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http, httpStateString(http->state))); in httpGetLength2()
1014 if (!http) in httpGetLength2()
1017 …if (http->fields[HTTP_FIELD_TRANSFER_ENCODING] && !_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSF… in httpGetLength2()
1025 * The following is a hack for HTTP servers that don't send a in httpGetLength2()
1032 if (!http->fields[HTTP_FIELD_CONTENT_LENGTH] || !http->fields[HTTP_FIELD_CONTENT_LENGTH][0]) in httpGetLength2()
1039 if (http->status >= HTTP_STATUS_MULTIPLE_CHOICES || in httpGetLength2()
1040 http->state == HTTP_STATE_OPTIONS || in httpGetLength2()
1041 (http->state == HTTP_STATE_GET && http->mode == _HTTP_MODE_SERVER) || in httpGetLength2()
1042 http->state == HTTP_STATE_HEAD || in httpGetLength2()
1043 (http->state == HTTP_STATE_PUT && http->mode == _HTTP_MODE_CLIENT) || in httpGetLength2()
1044 http->state == HTTP_STATE_DELETE || in httpGetLength2()
1045 http->state == HTTP_STATE_TRACE || in httpGetLength2()
1046 http->state == HTTP_STATE_CONNECT) in httpGetLength2()
1051 else if ((remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH], in httpGetLength2()
1070 httpGetPending(http_t *http) /* I - HTTP connection */ in httpGetPending() argument
1072 return (http ? (size_t)http->wused : 0); in httpGetPending()
1083 httpGetReady(http_t *http) /* I - HTTP connection */ in httpGetReady() argument
1085 if (!http) in httpGetReady()
1087 else if (http->used > 0) in httpGetReady()
1088 return ((size_t)http->used); in httpGetReady()
1090 else if (http->tls) in httpGetReady()
1091 return (_httpTLSPending(http)); in httpGetReady()
1109 httpGetRemaining(http_t *http) /* I - HTTP connection */ in httpGetRemaining() argument
1111 return (http ? (size_t)http->data_remaining : 0); in httpGetRemaining()
1116 * 'httpGets()' - Get a line of text from a HTTP connection.
1122 http_t *http) /* I - HTTP connection */ in httpGets() argument
1132 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line, length, (void *)http)); in httpGets()
1134 if (!http || !line || length <= 1) in httpGets()
1141 http->error = 0; in httpGets()
1158 while (http->used == 0) in httpGets()
1164 while (!_httpWait(http, http->wait_value, 1)) in httpGets()
1166 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpGets()
1171 http->error = WSAETIMEDOUT; in httpGets()
1173 http->error = ETIMEDOUT; in httpGets()
1178 bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used)); in httpGets()
1195 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpGets()
1198 http->error = WSAGetLastError(); in httpGets()
1200 else if (WSAGetLastError() != http->error) in httpGets()
1202 http->error = WSAGetLastError(); in httpGets()
1213 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpGets()
1215 else if (!http->timeout_cb && errno == EAGAIN) in httpGets()
1218 http->error = errno; in httpGets()
1220 else if (errno != http->error) in httpGets()
1222 http->error = errno; in httpGets()
1231 http->error = EPIPE; in httpGets()
1240 http->used += (int)bytes; in httpGets()
1247 for (bufptr = http->buffer, bufend = http->buffer + http->used; in httpGets()
1262 http->used -= (int)(bufptr - http->buffer); in httpGets()
1263 if (http->used > 0) in httpGets()
1264 memmove(http->buffer, bufptr, (size_t)http->used); in httpGets()
1272 http->activity = time(NULL); in httpGets()
1289 * 'httpGetState()' - Get the current state of the HTTP request.
1292 http_state_t /* O - HTTP state */
1293 httpGetState(http_t *http) /* I - HTTP connection */ in httpGetState() argument
1295 return (http ? http->state : HTTP_STATE_ERROR); in httpGetState()
1300 * 'httpGetStatus()' - Get the status of the last HTTP request.
1305 http_status_t /* O - HTTP status */
1306 httpGetStatus(http_t *http) /* I - HTTP connection */ in httpGetStatus() argument
1308 return (http ? http->status : HTTP_STATUS_ERROR); in httpGetStatus()
1319 httpGetSubField(http_t *http, /* I - HTTP connection */ in httpGetSubField() argument
1324 return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE)); in httpGetSubField()
1335 httpGetSubField2(http_t *http, /* I - HTTP connection */ in httpGetSubField2() argument
1346 …DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)… in httpGetSubField2()
1351 if (!http || !name || !value || valuelen < 2 || in httpGetSubField2()
1352 field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX || !http->fields[field]) in httpGetSubField2()
1357 for (fptr = http->fields[field]; *fptr;) in httpGetSubField2()
1463 * 'httpGetVersion()' - Get the HTTP version at the other end.
1467 httpGetVersion(http_t *http) /* I - HTTP connection */ in httpGetVersion() argument
1469 return (http ? http->version : HTTP_VERSION_1_0); in httpGetVersion()
1478 httpHead(http_t *http, /* I - HTTP connection */ in httpHead() argument
1481 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http, uri)); in httpHead()
1482 return (http_send(http, HTTP_STATE_HEAD, uri)); in httpHead()
1487 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1488 * default HTTP proxy (if any).
1550 httpIsChunked(http_t *http) /* I - HTTP connection */ in httpIsChunked() argument
1552 return (http ? http->data_encoding == HTTP_ENCODING_CHUNKED : 0); in httpIsChunked()
1565 httpIsEncrypted(http_t *http) /* I - HTTP connection */ in httpIsEncrypted() argument
1567 return (http ? http->tls != NULL : 0); in httpIsEncrypted()
1576 httpOptions(http_t *http, /* I - HTTP connection */ in httpOptions() argument
1579 return (http_send(http, HTTP_STATE_OPTIONS, uri)); in httpOptions()
1584 * 'httpPeek()' - Peek at data from a HTTP connection.
1586 * This function copies available data from the given HTTP connection, reading
1596 httpPeek(http_t *http, /* I - HTTP connection */ in httpPeek() argument
1604 …DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer,… in httpPeek()
1606 if (http == NULL || buffer == NULL) in httpPeek()
1609 http->activity = time(NULL); in httpPeek()
1610 http->error = 0; in httpPeek()
1615 if (http->data_encoding == HTTP_ENCODING_CHUNKED && in httpPeek()
1616 http->data_remaining <= 0) in httpPeek()
1620 if (httpGets(len, sizeof(len), http) == NULL) in httpPeek()
1629 if (!httpGets(len, sizeof(len), http)) in httpPeek()
1636 http->data_remaining = strtoll(len, NULL, 16); in httpPeek()
1638 if (http->data_remaining < 0) in httpPeek()
1646 CUPS_LLCAST http->data_remaining)); in httpPeek()
1648 if (http->data_remaining <= 0 && http->data_encoding != HTTP_ENCODING_FIELDS) in httpPeek()
1656 if (http->coding >= _HTTP_CODING_GUNZIP) in httpPeek()
1657 http_content_coding_finish(http); in httpPeek()
1660 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpPeek()
1661 httpGets(len, sizeof(len), http); in httpPeek()
1663 if (http->state == HTTP_STATE_POST_RECV) in httpPeek()
1664 http->state ++; in httpPeek()
1666 http->state = HTTP_STATE_STATUS; in httpPeek()
1669 httpStateString(http->state))); in httpPeek()
1675 http->data_encoding = HTTP_ENCODING_FIELDS; in httpPeek()
1679 else if (length > (size_t)http->data_remaining) in httpPeek()
1680 length = (size_t)http->data_remaining; in httpPeek()
1683 if (http->used == 0 && in httpPeek()
1684 (http->coding == _HTTP_CODING_IDENTITY || in httpPeek()
1685 (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in == 0))) in httpPeek()
1687 if (http->used == 0) in httpPeek()
1696 if (!http->blocking) in httpPeek()
1698 while (!httpWait(http, http->wait_value)) in httpPeek()
1700 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpPeek()
1707 if ((size_t)http->data_remaining > sizeof(http->buffer)) in httpPeek()
1708 buflen = sizeof(http->buffer); in httpPeek()
1710 buflen = (ssize_t)http->data_remaining; in httpPeek()
1713 bytes = http_read(http, http->buffer, (size_t)buflen); in httpPeek()
1720 http_debug_hex("httpPeek", http->buffer, (int)bytes); in httpPeek()
1723 http->used = (int)bytes; in httpPeek()
1728 if (http->coding >= _HTTP_CODING_GUNZIP) in httpPeek()
1734 if (http->used > 0 && ((z_stream *)http->stream)->avail_in < HTTP_MAX_BUFFER) in httpPeek()
1736 size_t buflen = HTTP_MAX_BUFFER - ((z_stream *)http->stream)->avail_in; in httpPeek()
1739 if (((z_stream *)http->stream)->avail_in > 0 && in httpPeek()
1740 ((z_stream *)http->stream)->next_in > http->sbuffer) in httpPeek()
1741 … memmove(http->sbuffer, ((z_stream *)http->stream)->next_in, ((z_stream *)http->stream)->avail_in); in httpPeek()
1743 ((z_stream *)http->stream)->next_in = http->sbuffer; in httpPeek()
1745 if (buflen > (size_t)http->data_remaining) in httpPeek()
1746 buflen = (size_t)http->data_remaining; in httpPeek()
1748 if (buflen > (size_t)http->used) in httpPeek()
1749 buflen = (size_t)http->used; in httpPeek()
1754 memcpy(http->sbuffer + ((z_stream *)http->stream)->avail_in, http->buffer, buflen); in httpPeek()
1755 ((z_stream *)http->stream)->avail_in += buflen; in httpPeek()
1756 http->used -= (int)buflen; in httpPeek()
1757 http->data_remaining -= (off_t)buflen; in httpPeek()
1759 if (http->used > 0) in httpPeek()
1760 memmove(http->buffer, http->buffer + buflen, (size_t)http->used); in httpPeek()
1764 (int)((z_stream *)http->stream)->avail_in)); in httpPeek()
1766 if (inflateCopy(&stream, (z_stream *)http->stream) != Z_OK) in httpPeek()
1769 http->error = ENOMEM; in httpPeek()
1783 http_debug_hex("2httpPeek", (char *)http->sbuffer, (int)((z_stream *)http->stream)->avail_in); in httpPeek()
1786 http->error = EIO; in httpPeek()
1790 bytes = (ssize_t)(length - ((z_stream *)http->stream)->avail_out); in httpPeek()
1800 if (http->used > 0) in httpPeek()
1802 if (length > (size_t)http->used) in httpPeek()
1803 length = (size_t)http->used; in httpPeek()
1810 memcpy(buffer, http->buffer, length); in httpPeek()
1821 http->error = WSAGetLastError(); in httpPeek()
1826 http->error = errno; in httpPeek()
1831 http->error = EPIPE; in httpPeek()
1844 httpPost(http_t *http, /* I - HTTP connection */ in httpPost() argument
1847 return (http_send(http, HTTP_STATE_POST, uri)); in httpPost()
1852 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1858 httpPrintf(http_t *http, /* I - HTTP connection */ in httpPrintf() argument
1867 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http, format)); in httpPrintf()
1877 http->error = ENOMEM; in httpPrintf()
1880 else if (http->data_encoding == HTTP_ENCODING_FIELDS) in httpPrintf()
1881 return ((int)httpWrite2(http, buf, (size_t)bytes)); in httpPrintf()
1884 if (http->wused) in httpPrintf()
1888 if (httpFlushWrite(http) < 0) in httpPrintf()
1892 return ((int)http_write(http, buf, (size_t)bytes)); in httpPrintf()
1902 httpPut(http_t *http, /* I - HTTP connection */ in httpPut() argument
1905 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http, uri)); in httpPut()
1906 return (http_send(http, HTTP_STATE_PUT, uri)); in httpPut()
1911 * 'httpRead()' - Read data from a HTTP connection.
1920 httpRead(http_t *http, /* I - HTTP connection */ in httpRead() argument
1924 return ((int)httpRead2(http, buffer, (size_t)length)); in httpRead()
1929 * 'httpRead2()' - Read data from a HTTP connection.
1935 httpRead2(http_t *http, /* I - HTTP connection */ in httpRead2() argument
1943 …http=%p, buffer=%p, length=" CUPS_LLFMT ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT,… in httpRead2()
1945 …http=%p, buffer=%p, length=" CUPS_LLFMT ") data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)h… in httpRead2()
1948 if (http == NULL || buffer == NULL) in httpRead2()
1951 http->activity = time(NULL); in httpRead2()
1952 http->error = 0; in httpRead2()
1958 if (http->coding >= _HTTP_CODING_GUNZIP) in httpRead2()
1962 if (((z_stream *)http->stream)->avail_in > 0) in httpRead2()
1967 (int)((z_stream *)http->stream)->avail_in, (int)length)); in httpRead2()
1969 ((z_stream *)http->stream)->next_out = (Bytef *)buffer; in httpRead2()
1970 ((z_stream *)http->stream)->avail_out = (uInt)length; in httpRead2()
1972 if ((zerr = inflate((z_stream *)http->stream, Z_SYNC_FLUSH)) < Z_OK) in httpRead2()
1976 … http_debug_hex("2httpRead2", (char *)http->sbuffer, (int)((z_stream *)http->stream)->avail_in); in httpRead2()
1979 http->error = EIO; in httpRead2()
1983 bytes = (ssize_t)(length - ((z_stream *)http->stream)->avail_out); in httpRead2()
1986 ((z_stream *)http->stream)->avail_in, ((z_stream *)http->stream)->avail_out, in httpRead2()
1994 ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)((z_stream *)http->stream)->avail_in; in httpRead2()
1999 if (((z_stream *)http->stream)->avail_in > 0 && in httpRead2()
2000 ((z_stream *)http->stream)->next_in > http->sbuffer) in httpRead2()
2001 … memmove(http->sbuffer, ((z_stream *)http->stream)->next_in, ((z_stream *)http->stream)->avail_in); in httpRead2()
2003 ((z_stream *)http->stream)->next_in = http->sbuffer; in httpRead2()
2008 if (http->data_remaining > 0) in httpRead2()
2010 if (buflen > http->data_remaining) in httpRead2()
2011 buflen = (ssize_t)http->data_remaining; in httpRead2()
2013 …bytes = http_read_buffered(http, (char *)http->sbuffer + ((z_stream *)http->stream)->avail_in, (si… in httpRead2()
2015 else if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2016 …bytes = http_read_chunk(http, (char *)http->sbuffer + ((z_stream *)http->stream)->avail_in, (size_… in httpRead2()
2028 http->data_remaining -= bytes; in httpRead2()
2029 ((z_stream *)http->stream)->avail_in += (uInt)bytes; in httpRead2()
2031 if (http->data_remaining <= 0 && in httpRead2()
2032 http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2040 httpGets(len, sizeof(len), http); in httpRead2()
2053 if (http->data_remaining == 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2055 if ((bytes = http_read_chunk(http, buffer, length)) > 0) in httpRead2()
2057 http->data_remaining -= bytes; in httpRead2()
2059 if (http->data_remaining <= 0) in httpRead2()
2067 httpGets(len, sizeof(len), http); in httpRead2()
2071 else if (http->data_remaining <= 0) in httpRead2()
2084 if (length > (size_t)http->data_remaining) in httpRead2()
2085 length = (size_t)http->data_remaining; in httpRead2()
2087 if ((bytes = http_read_buffered(http, buffer, length)) > 0) in httpRead2()
2089 http->data_remaining -= bytes; in httpRead2()
2091 if (http->data_remaining <= 0 && in httpRead2()
2092 http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2100 httpGets(len, sizeof(len), http); in httpRead2()
2107 (http->coding == _HTTP_CODING_IDENTITY || in httpRead2()
2108 (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in == 0)) && in httpRead2()
2110 ((http->data_remaining <= 0 && in httpRead2()
2111 http->data_encoding == HTTP_ENCODING_LENGTH) || in httpRead2()
2112 (http->data_encoding == HTTP_ENCODING_CHUNKED && bytes == 0))) in httpRead2()
2115 if (http->coding >= _HTTP_CODING_GUNZIP) in httpRead2()
2116 http_content_coding_finish(http); in httpRead2()
2119 if (http->state == HTTP_STATE_POST_RECV) in httpRead2()
2120 http->state ++; in httpRead2()
2121 else if (http->state == HTTP_STATE_GET_SEND || in httpRead2()
2122 http->state == HTTP_STATE_POST_SEND) in httpRead2()
2123 http->state = HTTP_STATE_WAITING; in httpRead2()
2125 http->state = HTTP_STATE_STATUS; in httpRead2()
2128 httpStateString(http->state))); in httpRead2()
2136 * 'httpReadRequest()' - Read a HTTP request from a connection.
2142 httpReadRequest(http_t *http, /* I - HTTP connection */ in httpReadRequest() argument
2146 char line[4096], /* HTTP request line */ in httpReadRequest()
2147 *req_method, /* HTTP request method */ in httpReadRequest()
2148 *req_uri, /* HTTP request URI */ in httpReadRequest()
2149 *req_version; /* HTTP request version number string */ in httpReadRequest()
2156 …DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT ")", (void *)http, (void *)uri… in httpReadRequest()
2161 if (!http || !uri || urilen < 1) in httpReadRequest()
2166 else if (http->state != HTTP_STATE_WAITING) in httpReadRequest()
2169 httpStateString(http->state))); in httpReadRequest()
2177 httpClearFields(http); in httpReadRequest()
2179 http->activity = time(NULL); in httpReadRequest()
2180 http->data_encoding = HTTP_ENCODING_FIELDS; in httpReadRequest()
2181 http->data_remaining = 0; in httpReadRequest()
2182 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpReadRequest()
2183 http->status = HTTP_STATUS_OK; in httpReadRequest()
2184 http->version = HTTP_VERSION_1_1; in httpReadRequest()
2190 if (!httpGets(line, sizeof(line), http)) in httpReadRequest()
2248 http->state = HTTP_STATE_OPTIONS; in httpReadRequest()
2250 http->state = HTTP_STATE_GET; in httpReadRequest()
2252 http->state = HTTP_STATE_HEAD; in httpReadRequest()
2254 http->state = HTTP_STATE_POST; in httpReadRequest()
2256 http->state = HTTP_STATE_PUT; in httpReadRequest()
2258 http->state = HTTP_STATE_DELETE; in httpReadRequest()
2260 http->state = HTTP_STATE_TRACE; in httpReadRequest()
2262 http->state = HTTP_STATE_CONNECT; in httpReadRequest()
2271 httpStateString(http->state))); in httpReadRequest()
2273 if (!strcmp(req_version, "HTTP/1.0")) in httpReadRequest()
2275 http->version = HTTP_VERSION_1_0; in httpReadRequest()
2276 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpReadRequest()
2278 else if (!strcmp(req_version, "HTTP/1.1")) in httpReadRequest()
2280 http->version = HTTP_VERSION_1_1; in httpReadRequest()
2281 http->keep_alive = HTTP_KEEPALIVE_ON; in httpReadRequest()
2293 return (http->state); in httpReadRequest()
2298 * 'httpReconnect()' - Reconnect to a HTTP server.
2307 httpReconnect(http_t *http) /* I - HTTP connection */ in httpReconnect() argument
2309 DEBUG_printf(("httpReconnect(http=%p)", (void *)http)); in httpReconnect()
2311 return (httpReconnect2(http, 30000, NULL)); in httpReconnect()
2316 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2321 httpReconnect2(http_t *http, /* I - HTTP connection */ in httpReconnect2() argument
2332 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel)); in httpReconnect2()
2334 if (!http) in httpReconnect2()
2341 if (http->tls) in httpReconnect2()
2344 _httpTLSStop(http); in httpReconnect2()
2352 if (http->fd >= 0) in httpReconnect2()
2354 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http->fd)); in httpReconnect2()
2356 httpAddrClose(NULL, http->fd); in httpReconnect2()
2358 http->fd = -1; in httpReconnect2()
2365 http->state = HTTP_STATE_WAITING; in httpReconnect2()
2366 http->version = HTTP_VERSION_1_1; in httpReconnect2()
2367 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpReconnect2()
2368 memset(&http->_hostaddr, 0, sizeof(http->_hostaddr)); in httpReconnect2()
2369 http->data_encoding = HTTP_ENCODING_FIELDS; in httpReconnect2()
2370 http->_data_remaining = 0; in httpReconnect2()
2371 http->used = 0; in httpReconnect2()
2372 http->data_remaining = 0; in httpReconnect2()
2373 http->hostaddr = NULL; in httpReconnect2()
2374 http->wused = 0; in httpReconnect2()
2381 for (current = http->addrlist; current; current = current->next) in httpReconnect2()
2387 if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, cancel)) == NULL) in httpReconnect2()
2394 http->error = WSAGetLastError(); in httpReconnect2()
2396 http->error = errno; in httpReconnect2()
2398 http->status = HTTP_STATUS_ERROR; in httpReconnect2()
2401 strerror(http->error))); in httpReconnect2()
2406 DEBUG_printf(("2httpReconnect2: New socket=%d", http->fd)); in httpReconnect2()
2408 if (http->timeout_value > 0) in httpReconnect2()
2409 http_set_timeout(http->fd, http->timeout_value); in httpReconnect2()
2411 http->hostaddr = &(addr->addr); in httpReconnect2()
2412 http->error = 0; in httpReconnect2()
2415 if (http->encryption == HTTP_ENCRYPTION_ALWAYS) in httpReconnect2()
2421 if (_httpTLSStart(http) != 0) in httpReconnect2()
2423 httpAddrClose(NULL, http->fd); in httpReconnect2()
2428 else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls_upgrade) in httpReconnect2()
2429 return (http_tls_upgrade(http)); in httpReconnect2()
2433 httpAddrString(http->hostaddr, temp, sizeof(temp)), in httpReconnect2()
2434 httpAddrPort(http->hostaddr))); in httpReconnect2()
2444 * the HTTP connection object. You must still call @link httpSetField@ to set
2445 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2453 httpSetAuthString(http_t *http, /* I - HTTP connection */ in httpSetAuthString() argument
2461 if (!http) in httpSetAuthString()
2464 if (http->authstring && http->authstring != http->_authstring) in httpSetAuthString()
2465 free(http->authstring); in httpSetAuthString()
2467 http->authstring = http->_authstring; in httpSetAuthString()
2478 if (len > sizeof(http->_authstring)) in httpSetAuthString()
2481 len = sizeof(http->_authstring); in httpSetAuthString()
2483 http->authstring = temp; in httpSetAuthString()
2487 snprintf(http->authstring, len, "%s %s", scheme, data); in httpSetAuthString()
2489 strlcpy(http->authstring, scheme, len); in httpSetAuthString()
2497 http->_authstring[0] = '\0'; in httpSetAuthString()
2510 httpSetCredentials(http_t *http, /* I - HTTP connection */ in httpSetCredentials() argument
2513 if (!http || cupsArrayCount(credentials) < 1) in httpSetCredentials()
2517 _httpFreeCredentials(http->tls_credentials); in httpSetCredentials()
2519 http->tls_credentials = _httpCreateCredentials(credentials); in httpSetCredentials()
2522 return (http->tls_credentials ? 0 : -1); in httpSetCredentials()
2533 httpSetCookie(http_t *http, /* I - Connection */ in httpSetCookie() argument
2536 if (!http) in httpSetCookie()
2539 if (http->cookie) in httpSetCookie()
2540 free(http->cookie); in httpSetCookie()
2543 http->cookie = strdup(cookie); in httpSetCookie()
2545 http->cookie = NULL; in httpSetCookie()
2550 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2559 httpSetDefaultField(http_t *http, /* I - HTTP connection */ in httpSetDefaultField() argument
2563 …DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, htt… in httpSetDefaultField()
2565 if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) in httpSetDefaultField()
2568 if (http->default_fields[field]) in httpSetDefaultField()
2569 free(http->default_fields[field]); in httpSetDefaultField()
2571 http->default_fields[field] = value ? strdup(value) : NULL; in httpSetDefaultField()
2585 httpSetExpect(http_t *http, /* I - HTTP connection */ in httpSetExpect() argument
2586 http_status_t expect) /* I - HTTP status to expect in httpSetExpect()
2589 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http, expect)); in httpSetExpect()
2591 if (http) in httpSetExpect()
2592 http->expect = expect; in httpSetExpect()
2597 * 'httpSetField()' - Set the value of an HTTP header.
2601 httpSetField(http_t *http, /* I - HTTP connection */ in httpSetField() argument
2605 …DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, http_field… in httpSetField()
2607 if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX || !value) in httpSetField()
2610 http_add_field(http, field, value, 0); in httpSetField()
2622 http_t *http, /* I - HTTP connection */ in httpSetKeepAlive() argument
2625 if (http) in httpSetKeepAlive()
2626 http->keep_alive = keep_alive; in httpSetKeepAlive()
2637 httpSetLength(http_t *http, /* I - HTTP connection */ in httpSetLength() argument
2640 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT ")", (void *)http, CUPS_LLCAST length)); in httpSetLength()
2642 if (!http) in httpSetLength()
2647 httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked"); in httpSetLength()
2648 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, ""); in httpSetLength()
2656 httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, ""); in httpSetLength()
2657 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, len); in httpSetLength()
2665 * The optional timeout callback receives both the HTTP connection and a user
2673 http_t *http, /* I - HTTP connection */ in httpSetTimeout() argument
2679 if (!http || timeout <= 0.0) in httpSetTimeout()
2682 http->timeout_cb = cb; in httpSetTimeout()
2683 http->timeout_data = user_data; in httpSetTimeout()
2684 http->timeout_value = timeout; in httpSetTimeout()
2686 if (http->fd >= 0) in httpSetTimeout()
2687 http_set_timeout(http->fd, timeout); in httpSetTimeout()
2689 http_set_wait(http); in httpSetTimeout()
2694 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2700 httpShutdown(http_t *http) /* I - HTTP connection */ in httpShutdown() argument
2702 if (!http || http->fd < 0) in httpShutdown()
2706 if (http->tls) in httpShutdown()
2707 _httpTLSStop(http); in httpShutdown()
2711 shutdown(http->fd, SD_RECEIVE); /* Microsoft-ism... */ in httpShutdown()
2713 shutdown(http->fd, SHUT_RD); in httpShutdown()
2725 httpTrace(http_t *http, /* I - HTTP connection */ in httpTrace() argument
2728 return (http_send(http, HTTP_STATE_TRACE, uri)); in httpTrace()
2733 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2736 * and only retrieves a single status line from the HTTP connection.
2740 _httpUpdate(http_t *http, /* I - HTTP connection */ in _httpUpdate() argument
2741 http_status_t *status) /* O - Current HTTP status */ in _httpUpdate()
2746 int major, minor; /* HTTP version numbers */ in _httpUpdate()
2749 …DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpState… in _httpUpdate()
2755 if (!httpGets(line, sizeof(line), http)) in _httpUpdate()
2774 if (http->status == HTTP_STATUS_CONTINUE) in _httpUpdate()
2776 *status = http->status; in _httpUpdate()
2780 if (http->status < HTTP_STATUS_BAD_REQUEST) in _httpUpdate()
2781 http->digest_tries = 0; in _httpUpdate()
2784 if (http->status == HTTP_STATUS_SWITCHING_PROTOCOLS && !http->tls) in _httpUpdate()
2786 if (_httpTLSStart(http) != 0) in _httpUpdate()
2788 httpAddrClose(NULL, http->fd); in _httpUpdate()
2790 *status = http->status = HTTP_STATUS_ERROR; in _httpUpdate()
2799 if (http_set_length(http) < 0) in _httpUpdate()
2802 http->error = EINVAL; in _httpUpdate()
2803 http->status = *status = HTTP_STATUS_ERROR; in _httpUpdate()
2807 switch (http->state) in _httpUpdate()
2813 http->state ++; in _httpUpdate()
2816 httpStateString(http->state))); in _httpUpdate()
2823 http->state = HTTP_STATE_WAITING; in _httpUpdate()
2831 http_content_coding_start(http, in _httpUpdate()
2832 httpGetField(http, HTTP_FIELD_CONTENT_ENCODING)); in _httpUpdate()
2835 *status = http->status; in _httpUpdate()
2838 else if (!strncmp(line, "HTTP/", 5) && http->mode == _HTTP_MODE_CLIENT) in _httpUpdate()
2846 if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &intstatus) != 3) in _httpUpdate()
2848 *status = http->status = HTTP_STATUS_ERROR; in _httpUpdate()
2852 httpClearFields(http); in _httpUpdate()
2854 http->version = (http_version_t)(major * 100 + minor); in _httpUpdate()
2855 *status = http->status = (http_status_t)intstatus; in _httpUpdate()
2879 http->expect = (http_status_t)atoi(value); in _httpUpdate()
2887 httpSetCookie(http, value); in _httpUpdate()
2891 http_add_field(http, field, value, 1); in _httpUpdate()
2894 …httpGetSubField2(http, HTTP_FIELD_AUTHENTICATION_INFO, "nextnonce", http->nextnonce, (int)sizeof(h… in _httpUpdate()
2904 http->error = EINVAL; in _httpUpdate()
2905 http->status = *status = HTTP_STATUS_ERROR; in _httpUpdate()
2914 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2917 http_status_t /* O - HTTP status */
2918 httpUpdate(http_t *http) /* I - HTTP connection */ in httpUpdate() argument
2923 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http, httpStateString(http->state))); in httpUpdate()
2929 if (http->wused) in httpUpdate()
2933 if (httpFlushWrite(http) < 0) in httpUpdate()
2941 if (http->state == HTTP_STATE_WAITING) in httpUpdate()
2948 while (_httpUpdate(http, &status)); in httpUpdate()
2954 if (http->error == EPIPE && http->status > HTTP_STATUS_CONTINUE) in httpUpdate()
2956 DEBUG_printf(("1httpUpdate: Returning status %d...", http->status)); in httpUpdate()
2957 return (http->status); in httpUpdate()
2960 if (http->error) in httpUpdate()
2962 DEBUG_printf(("1httpUpdate: socket error %d - %s", http->error, in httpUpdate()
2963 strerror(http->error))); in httpUpdate()
2964 http->status = HTTP_STATUS_ERROR; in httpUpdate()
2981 _httpWait(http_t *http, /* I - HTTP connection */ in _httpWait() argument
2994 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http, msec, usessl)); in _httpWait()
2996 if (http->fd < 0) in _httpWait()
2998 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http->fd)); in _httpWait()
3007 if (http->tls && _httpTLSPending(http)) in _httpWait()
3019 pfd.fd = http->fd; in _httpWait()
3032 FD_SET(http->fd, &input_set); in _httpWait()
3034 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec, http->fd)); in _httpWait()
3041 nfds = select(http->fd + 1, &input_set, NULL, NULL, &timeout); in _httpWait()
3044 nfds = select(http->fd + 1, &input_set, NULL, NULL, NULL); in _httpWait()
3070 httpWait(http_t *http, /* I - HTTP connection */ in httpWait() argument
3077 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http, msec)); in httpWait()
3079 if (http == NULL) in httpWait()
3082 if (http->used) in httpWait()
3089 if (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in > 0) in httpWait()
3100 if (http->wused) in httpWait()
3104 if (httpFlushWrite(http) < 0) in httpWait()
3112 return (_httpWait(http, msec, 1)); in httpWait()
3117 * 'httpWrite()' - Write data to a HTTP connection.
3126 httpWrite(http_t *http, /* I - HTTP connection */ in httpWrite() argument
3130 return ((int)httpWrite2(http, buffer, (size_t)length)); in httpWrite()
3135 * 'httpWrite2()' - Write data to a HTTP connection.
3141 httpWrite2(http_t *http, /* I - HTTP connection */ in httpWrite2() argument
3148 …DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffe… in httpWrite2()
3154 if (!http || !buffer) in httpWrite2()
3164 http->activity = time(NULL); in httpWrite2()
3171 if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE) in httpWrite2()
3173 DEBUG_printf(("1httpWrite2: http->coding=%d", http->coding)); in httpWrite2()
3177 http_content_coding_finish(http); in httpWrite2()
3185 ((z_stream *)http->stream)->next_in = (Bytef *)buffer; in httpWrite2()
3186 ((z_stream *)http->stream)->avail_in = (uInt)length; in httpWrite2()
3188 while (deflate((z_stream *)http->stream, Z_NO_FLUSH) == Z_OK) in httpWrite2()
3190 DEBUG_printf(("1httpWrite2: avail_out=%d", ((z_stream *)http->stream)->avail_out)); in httpWrite2()
3192 if (((z_stream *)http->stream)->avail_out > 0) in httpWrite2()
3195 slen = _HTTP_MAX_SBUFFER - ((z_stream *)http->stream)->avail_out; in httpWrite2()
3199 if (slen > 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) in httpWrite2()
3200 sret = http_write_chunk(http, (char *)http->sbuffer, slen); in httpWrite2()
3202 sret = http_write(http, (char *)http->sbuffer, slen); in httpWrite2()
3212 ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; in httpWrite2()
3213 ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; in httpWrite2()
3223 if (http->wused && (length + (size_t)http->wused) > sizeof(http->wbuffer)) in httpWrite2()
3226 CUPS_LLFMT ")", http->wused, CUPS_LLCAST length)); in httpWrite2()
3228 httpFlushWrite(http); in httpWrite2()
3231 if ((length + (size_t)http->wused) <= sizeof(http->wbuffer) && length < sizeof(http->wbuffer)) in httpWrite2()
3240 memcpy(http->wbuffer + http->wused, buffer, length); in httpWrite2()
3241 http->wused += (int)length; in httpWrite2()
3253 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpWrite2()
3254 bytes = (ssize_t)http_write_chunk(http, buffer, length); in httpWrite2()
3256 bytes = (ssize_t)http_write(http, buffer, length); in httpWrite2()
3262 if (http->data_encoding == HTTP_ENCODING_LENGTH) in httpWrite2()
3263 http->data_remaining -= bytes; in httpWrite2()
3272 if ((http->data_encoding == HTTP_ENCODING_CHUNKED && length == 0) || in httpWrite2()
3273 (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0)) in httpWrite2()
3281 if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE) in httpWrite2()
3282 http_content_coding_finish(http); in httpWrite2()
3285 if (http->wused) in httpWrite2()
3287 if (httpFlushWrite(http) < 0) in httpWrite2()
3291 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpWrite2()
3297 http_write(http, "0\r\n\r\n", 5); in httpWrite2()
3303 http->data_encoding = HTTP_ENCODING_FIELDS; in httpWrite2()
3304 http->data_remaining = 0; in httpWrite2()
3307 if (http->state == HTTP_STATE_POST_RECV) in httpWrite2()
3308 http->state ++; in httpWrite2()
3309 else if (http->state == HTTP_STATE_POST_SEND || in httpWrite2()
3310 http->state == HTTP_STATE_GET_SEND) in httpWrite2()
3311 http->state = HTTP_STATE_WAITING; in httpWrite2()
3313 http->state = HTTP_STATE_STATUS; in httpWrite2()
3316 httpStateString(http->state))); in httpWrite2()
3326 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3332 httpWriteResponse(http_t *http, /* I - HTTP connection */ in httpWriteResponse() argument
3343 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http, status)); in httpWriteResponse()
3345 if (!http || status < HTTP_STATUS_CONTINUE) in httpWriteResponse()
3355 if (!http->fields[HTTP_FIELD_DATE]) in httpWriteResponse()
3356 httpSetField(http, HTTP_FIELD_DATE, httpGetDateString(time(NULL))); in httpWriteResponse()
3358 if (status >= HTTP_STATUS_BAD_REQUEST && http->keep_alive) in httpWriteResponse()
3360 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpWriteResponse()
3361 httpSetField(http, HTTP_FIELD_KEEP_ALIVE, ""); in httpWriteResponse()
3364 if (http->version == HTTP_VERSION_1_1) in httpWriteResponse()
3366 if (!http->fields[HTTP_FIELD_CONNECTION]) in httpWriteResponse()
3368 if (http->keep_alive) in httpWriteResponse()
3369 httpSetField(http, HTTP_FIELD_CONNECTION, "Keep-Alive"); in httpWriteResponse()
3371 httpSetField(http, HTTP_FIELD_CONNECTION, "close"); in httpWriteResponse()
3374 if (http->keep_alive && !http->fields[HTTP_FIELD_KEEP_ALIVE]) in httpWriteResponse()
3375 httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "timeout=10"); in httpWriteResponse()
3382 if (!http->fields[HTTP_FIELD_CONNECTION]) in httpWriteResponse()
3383 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); in httpWriteResponse()
3385 if (!http->fields[HTTP_FIELD_UPGRADE]) in httpWriteResponse()
3386 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); in httpWriteResponse()
3388 if (!http->fields[HTTP_FIELD_CONTENT_LENGTH]) in httpWriteResponse()
3389 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "0"); in httpWriteResponse()
3393 if (!http->fields[HTTP_FIELD_SERVER]) in httpWriteResponse()
3394 …httpSetField(http, HTTP_FIELD_SERVER, http->default_fields[HTTP_FIELD_SERVER] ? http->default_fiel… in httpWriteResponse()
3400 if (!http->fields[HTTP_FIELD_ACCEPT_ENCODING]) in httpWriteResponse()
3401 …httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, http->default_fields[HTTP_FIELD_ACCEPT_ENCODING] ? … in httpWriteResponse()
3412 old_encoding = http->data_encoding; in httpWriteResponse()
3413 old_remaining = http->data_remaining; in httpWriteResponse()
3414 http->data_encoding = HTTP_ENCODING_FIELDS; in httpWriteResponse()
3416 …if (httpPrintf(http, "HTTP/%d.%d %d %s\r\n", http->version / 100, http->version % 100, (int)status… in httpWriteResponse()
3418 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3433 if ((value = httpGetField(http, i)) != NULL && *value) in httpWriteResponse()
3435 if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1) in httpWriteResponse()
3437 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3443 if (http->cookie) in httpWriteResponse()
3445 if (strchr(http->cookie, ';')) in httpWriteResponse()
3447 if (httpPrintf(http, "Set-Cookie: %s\r\n", http->cookie) < 1) in httpWriteResponse()
3449 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3453 …else if (httpPrintf(http, "Set-Cookie: %s; path=/; httponly;%s\r\n", http->cookie, http->tls ? " s… in httpWriteResponse()
3455 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3464 if (httpPrintf(http, "X-Frame-Options: DENY\r\n" in httpWriteResponse()
3467 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3472 if (httpWrite2(http, "\r\n", 2) < 2) in httpWriteResponse()
3474 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3478 if (httpFlushWrite(http) < 0) in httpWriteResponse()
3480 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3491 http->data_encoding = old_encoding; in httpWriteResponse()
3492 http->data_remaining = old_remaining; in httpWriteResponse()
3495 http->_data_remaining = (int)old_remaining; in httpWriteResponse()
3497 http->_data_remaining = INT_MAX; in httpWriteResponse()
3499 else if (http->state == HTTP_STATE_OPTIONS || in httpWriteResponse()
3500 http->state == HTTP_STATE_HEAD || in httpWriteResponse()
3501 http->state == HTTP_STATE_PUT || in httpWriteResponse()
3502 http->state == HTTP_STATE_TRACE || in httpWriteResponse()
3503 http->state == HTTP_STATE_CONNECT || in httpWriteResponse()
3504 http->state == HTTP_STATE_STATUS) in httpWriteResponse()
3507 "was %s.", httpStateString(http->state))); in httpWriteResponse()
3508 http->state = HTTP_STATE_WAITING; in httpWriteResponse()
3517 http_set_length(http); in httpWriteResponse()
3519 if (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0) in httpWriteResponse()
3522 "was %s.", httpStateString(http->state))); in httpWriteResponse()
3523 http->state = HTTP_STATE_WAITING; in httpWriteResponse()
3527 if (http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_GET) in httpWriteResponse()
3528 http->state ++; in httpWriteResponse()
3536 http_content_coding_start(http, in httpWriteResponse()
3537 httpGetField(http, HTTP_FIELD_CONTENT_ENCODING)); in httpWriteResponse()
3547 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3551 http_add_field(http_t *http, /* I - HTTP connection */ in http_add_field() argument
3552 http_field_t field, /* I - HTTP field */ in http_add_field()
3601 if (!append && http->fields[field]) in http_add_field()
3603 if (http->fields[field] != http->_fields[field]) in http_add_field()
3604 free(http->fields[field]); in http_add_field()
3606 http->fields[field] = NULL; in http_add_field()
3613 http->_fields[field][0] = '\0'; in http_add_field()
3617 if (http->fields[field]) in http_add_field()
3619 fieldlen = strlen(http->fields[field]); in http_add_field()
3640 snprintf(combined, sizeof(combined), "%s, %s", http->_fields[field], value); in http_add_field()
3644 strlcpy(http->_fields[field], value, sizeof(http->_fields[field])); in http_add_field()
3645 http->fields[field] = http->_fields[field]; in http_add_field()
3655 if (http->fields[field] == http->_fields[field]) in http_add_field()
3659 http->fields[field] = combined; in http_add_field()
3660 snprintf(combined, total + 1, "%s, %s", http->_fields[field], value); in http_add_field()
3663 else if ((combined = realloc(http->fields[field], total + 1)) != NULL) in http_add_field()
3665 http->fields[field] = combined; in http_add_field()
3676 http->fields[field] = strdup(value); in http_add_field()
3680 if (field == HTTP_FIELD_CONTENT_ENCODING && http->data_encoding != HTTP_ENCODING_FIELDS) in http_add_field()
3683 http_content_coding_start(http, value); in http_add_field()
3696 http_t *http) /* I - HTTP connection */ in http_content_coding_finish() argument
3703 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http)); in http_content_coding_finish()
3704 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http->coding)); in http_content_coding_finish()
3706 switch (http->coding) in http_content_coding_finish()
3710 ((z_stream *)http->stream)->next_in = dummy; in http_content_coding_finish()
3711 ((z_stream *)http->stream)->avail_in = 0; in http_content_coding_finish()
3715 zerr = deflate((z_stream *)http->stream, Z_FINISH); in http_content_coding_finish()
3716 bytes = _HTTP_MAX_SBUFFER - ((z_stream *)http->stream)->avail_out; in http_content_coding_finish()
3722 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in http_content_coding_finish()
3723 http_write_chunk(http, (char *)http->sbuffer, bytes); in http_content_coding_finish()
3725 http_write(http, (char *)http->sbuffer, bytes); in http_content_coding_finish()
3728 ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; in http_content_coding_finish()
3729 ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; in http_content_coding_finish()
3733 deflateEnd((z_stream *)http->stream); in http_content_coding_finish()
3735 free(http->sbuffer); in http_content_coding_finish()
3736 free(http->stream); in http_content_coding_finish()
3738 http->sbuffer = NULL; in http_content_coding_finish()
3739 http->stream = NULL; in http_content_coding_finish()
3741 if (http->wused) in http_content_coding_finish()
3742 httpFlushWrite(http); in http_content_coding_finish()
3747 inflateEnd((z_stream *)http->stream); in http_content_coding_finish()
3749 free(http->sbuffer); in http_content_coding_finish()
3750 free(http->stream); in http_content_coding_finish()
3752 http->sbuffer = NULL; in http_content_coding_finish()
3753 http->stream = NULL; in http_content_coding_finish()
3760 http->coding = _HTTP_CODING_IDENTITY; in http_content_coding_finish()
3770 http_t *http, /* I - HTTP connection */ in http_content_coding_start() argument
3777 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http, value)); in http_content_coding_start()
3779 if (http->coding != _HTTP_CODING_IDENTITY) in http_content_coding_start()
3781 DEBUG_printf(("1http_content_coding_start: http->coding already %d.", in http_content_coding_start()
3782 http->coding)); in http_content_coding_start()
3787 if (http->state == HTTP_STATE_GET_SEND || in http_content_coding_start()
3788 http->state == HTTP_STATE_POST_SEND) in http_content_coding_start()
3789 coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_GZIP : in http_content_coding_start()
3791 else if (http->state == HTTP_STATE_POST_RECV || in http_content_coding_start()
3792 http->state == HTTP_STATE_PUT_RECV) in http_content_coding_start()
3793 coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_GZIP : in http_content_coding_start()
3803 if (http->state == HTTP_STATE_GET_SEND || in http_content_coding_start()
3804 http->state == HTTP_STATE_POST_SEND) in http_content_coding_start()
3805 coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_DEFLATE : in http_content_coding_start()
3807 else if (http->state == HTTP_STATE_POST_RECV || in http_content_coding_start()
3808 http->state == HTTP_STATE_PUT_RECV) in http_content_coding_start()
3809 coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_DEFLATE : in http_content_coding_start()
3827 if (http->wused) in http_content_coding_start()
3828 httpFlushWrite(http); in http_content_coding_start()
3830 if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL) in http_content_coding_start()
3832 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3833 http->error = errno; in http_content_coding_start()
3843 if ((http->stream = calloc(1, sizeof(z_stream))) == NULL) in http_content_coding_start()
3845 free(http->sbuffer); in http_content_coding_start()
3847 http->sbuffer = NULL; in http_content_coding_start()
3848 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3849 http->error = errno; in http_content_coding_start()
3853 …if ((zerr = deflateInit2((z_stream *)http->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, coding == _H… in http_content_coding_start()
3855 free(http->sbuffer); in http_content_coding_start()
3856 free(http->stream); in http_content_coding_start()
3858 http->sbuffer = NULL; in http_content_coding_start()
3859 http->stream = NULL; in http_content_coding_start()
3860 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3861 http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL; in http_content_coding_start()
3865 ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; in http_content_coding_start()
3866 ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; in http_content_coding_start()
3871 if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL) in http_content_coding_start()
3873 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3874 http->error = errno; in http_content_coding_start()
3883 if ((http->stream = calloc(1, sizeof(z_stream))) == NULL) in http_content_coding_start()
3885 free(http->sbuffer); in http_content_coding_start()
3887 http->sbuffer = NULL; in http_content_coding_start()
3888 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3889 http->error = errno; in http_content_coding_start()
3893 …if ((zerr = inflateInit2((z_stream *)http->stream, coding == _HTTP_CODING_INFLATE ? -15 : 31)) < Z… in http_content_coding_start()
3895 free(http->sbuffer); in http_content_coding_start()
3896 free(http->stream); in http_content_coding_start()
3898 http->sbuffer = NULL; in http_content_coding_start()
3899 http->stream = NULL; in http_content_coding_start()
3900 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3901 http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL; in http_content_coding_start()
3905 ((z_stream *)http->stream)->avail_in = 0; in http_content_coding_start()
3906 ((z_stream *)http->stream)->next_in = http->sbuffer; in http_content_coding_start()
3913 http->coding = coding; in http_content_coding_start()
3915 DEBUG_printf(("1http_content_coding_start: http->coding now %d.", in http_content_coding_start()
3916 http->coding)); in http_content_coding_start()
3922 * 'http_create()' - Create an unconnected HTTP connection.
3925 static http_t * /* O - HTTP connection */
3935 http_t *http; /* New HTTP connection */ in http_create() local
3969 if ((http = calloc(sizeof(http_t), 1)) == NULL) in http_create()
3977 * Initialize the HTTP data... in http_create()
3980 http->mode = mode; in http_create()
3981 http->activity = time(NULL); in http_create()
3982 http->addrlist = myaddrlist; in http_create()
3983 http->blocking = blocking; in http_create()
3984 http->fd = -1; in http_create()
3986 http->gssctx = GSS_C_NO_CONTEXT; in http_create()
3987 http->gssname = GSS_C_NO_NAME; in http_create()
3989 http->status = HTTP_STATUS_CONTINUE; in http_create()
3990 http->version = HTTP_VERSION_1_1; in http_create()
3993 strlcpy(http->hostname, host, sizeof(http->hostname)); in http_create()
3996 http->encryption = HTTP_ENCRYPTION_ALWAYS; in http_create()
3998 http->encryption = encryption; in http_create()
4000 http_set_wait(http); in http_create()
4006 return (http); in http_create()
4068 * 'http_read()' - Read a buffer from a HTTP connection.
4075 http_read(http_t *http, /* I - HTTP connection */ in http_read() argument
4082 …DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer… in http_read()
4084 if (!http->blocking || http->timeout_value > 0.0) in http_read()
4086 while (!httpWait(http, http->wait_value)) in http_read()
4088 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in http_read()
4101 if (http->tls) in http_read()
4102 bytes = _httpTLSRead(http, buffer, (int)length); in http_read()
4105 bytes = recv(http->fd, buffer, length, 0); in http_read()
4112 http->error = WSAGetLastError(); in http_read()
4117 if (!http->timeout_cb || in http_read()
4118 !(*http->timeout_cb)(http, http->timeout_data)) in http_read()
4120 http->error = WSAEWOULDBLOCK; in http_read()
4129 if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) in http_read()
4131 http->error = errno; in http_read()
4134 else if (!http->timeout_cb && errno != EAGAIN) in http_read()
4136 http->error = errno; in http_read()
4142 http->error = errno; in http_read()
4163 http->error = WSAGetLastError(); in http_read()
4165 if (errno == EINTR || (errno == EAGAIN && !http->timeout_cb)) in http_read()
4168 http->error = errno; in http_read()
4173 http->error = EPIPE; in http_read()
4182 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4184 * This function reads data from the HTTP buffer or from the socket, as needed.
4188 http_read_buffered(http_t *http, /* I - HTTP connection */ in http_read_buffered() argument
4195 …f(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT ") used=%d", (void *)http, (void *)… in http_read_buffered()
4197 if (http->used > 0) in http_read_buffered()
4199 if (length > (size_t)http->used) in http_read_buffered()
4200 bytes = (ssize_t)http->used; in http_read_buffered()
4207 memcpy(buffer, http->buffer, (size_t)bytes); in http_read_buffered()
4208 http->used -= (int)bytes; in http_read_buffered()
4210 if (http->used > 0) in http_read_buffered()
4211 memmove(http->buffer, http->buffer + bytes, (size_t)http->used); in http_read_buffered()
4214 bytes = http_read(http, buffer, length); in http_read_buffered()
4221 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4228 http_read_chunk(http_t *http, /* I - HTTP connection */ in http_read_chunk() argument
4232 …DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)… in http_read_chunk()
4234 if (http->data_remaining <= 0) in http_read_chunk()
4238 if (!httpGets(len, sizeof(len), http)) in http_read_chunk()
4247 if (!httpGets(len, sizeof(len), http)) in http_read_chunk()
4254 http->data_remaining = strtoll(len, NULL, 16); in http_read_chunk()
4256 if (http->data_remaining < 0) in http_read_chunk()
4259 CUPS_LLFMT ")", len, CUPS_LLCAST http->data_remaining)); in http_read_chunk()
4264 len, CUPS_LLCAST http->data_remaining)); in http_read_chunk()
4266 if (http->data_remaining == 0) in http_read_chunk()
4272 httpGets(len, sizeof(len), http); in http_read_chunk()
4277 CUPS_LLCAST http->data_remaining)); in http_read_chunk()
4279 if (http->data_remaining <= 0) in http_read_chunk()
4281 else if (length > (size_t)http->data_remaining) in http_read_chunk()
4282 length = (size_t)http->data_remaining; in http_read_chunk()
4284 return (http_read_buffered(http, buffer, length)); in http_read_chunk()
4293 http_send(http_t *http, /* I - HTTP connection */ in http_send() argument
4320 …DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http, codes[request], ur… in http_send()
4322 if (http == NULL || uri == NULL) in http_send()
4329 if (!http->fields[HTTP_FIELD_USER_AGENT]) in http_send()
4331 if (http->default_fields[HTTP_FIELD_USER_AGENT]) in http_send()
4332 httpSetField(http, HTTP_FIELD_USER_AGENT, http->default_fields[HTTP_FIELD_USER_AGENT]); in http_send()
4334 httpSetField(http, HTTP_FIELD_USER_AGENT, cupsUserAgent()); in http_send()
4341 if (!http->fields[HTTP_FIELD_ACCEPT_ENCODING] && http->default_fields[HTTP_FIELD_ACCEPT_ENCODING]) in http_send()
4342 … httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, http->default_fields[HTTP_FIELD_ACCEPT_ENCODING]); in http_send()
4354 if (http->fd < 0 || http->status == HTTP_STATUS_ERROR || in http_send()
4355 http->status >= HTTP_STATUS_BAD_REQUEST) in http_send()
4358 http->fd, http->status, http->tls_upgrade)); in http_send()
4360 if (httpReconnect2(http, 30000, NULL)) in http_send()
4368 if (http->wused) in http_send()
4370 if (httpFlushWrite(http) < 0) in http_send()
4371 if (httpReconnect2(http, 30000, NULL)) in http_send()
4379 http->state = request; in http_send()
4380 http->data_encoding = HTTP_ENCODING_FIELDS; in http_send()
4383 http->state ++; in http_send()
4385 http->status = HTTP_STATUS_CONTINUE; in http_send()
4388 if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls) in http_send()
4390 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); in http_send()
4391 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); in http_send()
4395 if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1) in http_send()
4397 http->status = HTTP_STATUS_ERROR; in http_send()
4402 if ((value = httpGetField(http, i)) != NULL && *value) in http_send()
4408 if (httpPrintf(http, "Host: %s:%d\r\n", value, in http_send()
4409 httpAddrPort(http->hostaddr)) < 1) in http_send()
4411 http->status = HTTP_STATUS_ERROR; in http_send()
4415 else if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1) in http_send()
4417 http->status = HTTP_STATUS_ERROR; in http_send()
4422 if (http->cookie) in http_send()
4423 if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1) in http_send()
4425 http->status = HTTP_STATUS_ERROR; in http_send()
4429 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http->expect, in http_send()
4430 http->mode, http->state)); in http_send()
4432 if (http->expect == HTTP_STATUS_CONTINUE && http->mode == _HTTP_MODE_CLIENT && in http_send()
4433 (http->state == HTTP_STATE_POST_RECV || in http_send()
4434 http->state == HTTP_STATE_PUT_RECV)) in http_send()
4435 if (httpPrintf(http, "Expect: 100-continue\r\n") < 1) in http_send()
4437 http->status = HTTP_STATUS_ERROR; in http_send()
4441 if (httpPrintf(http, "\r\n") < 1) in http_send()
4443 http->status = HTTP_STATUS_ERROR; in http_send()
4447 if (httpFlushWrite(http) < 0) in http_send()
4450 http_set_length(http); in http_send()
4451 httpClearFields(http); in http_send()
4457 if (http->fields[HTTP_FIELD_AUTHORIZATION] && http->authstring && in http_send()
4458 (!strncmp(http->authstring, "Negotiate", 9) || in http_send()
4459 !strncmp(http->authstring, "AuthRef", 7))) in http_send()
4461 http->_authstring[0] = '\0'; in http_send()
4463 if (http->authstring != http->_authstring) in http_send()
4464 free(http->authstring); in http_send()
4466 http->authstring = http->_authstring; in http_send()
4478 http_set_length(http_t *http) /* I - Connection */ in http_set_length() argument
4483 …DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http, http->mode, httpStateStri… in http_set_length()
4485 if ((remaining = httpGetLength2(http)) >= 0) in http_set_length()
4487 if (http->mode == _HTTP_MODE_SERVER && in http_set_length()
4488 http->state != HTTP_STATE_GET_SEND && in http_set_length()
4489 http->state != HTTP_STATE_PUT && in http_set_length()
4490 http->state != HTTP_STATE_POST && in http_set_length()
4491 http->state != HTTP_STATE_POST_SEND) in http_set_length()
4497 if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_TRANSFER_ENCODING), "chunked")) in http_set_length()
4501 http->data_encoding = HTTP_ENCODING_CHUNKED; in http_set_length()
4507 http->data_encoding = HTTP_ENCODING_LENGTH; in http_set_length()
4512 http->data_remaining = remaining; in http_set_length()
4515 http->_data_remaining = (int)remaining; in http_set_length()
4517 http->_data_remaining = INT_MAX; in http_set_length()
4555 http_set_wait(http_t *http) /* I - HTTP connection */ in http_set_wait() argument
4557 if (http->blocking) in http_set_wait()
4559 http->wait_value = (int)(http->timeout_value * 1000); in http_set_wait()
4561 if (http->wait_value <= 0) in http_set_wait()
4562 http->wait_value = 60000; in http_set_wait()
4565 http->wait_value = 10000; in http_set_wait()
4575 http_tls_upgrade(http_t *http) /* I - HTTP connection */ in http_tls_upgrade() argument
4578 http_t myhttp; /* Local copy of HTTP data */ in http_tls_upgrade()
4581 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http)); in http_tls_upgrade()
4588 httpFlush(http); in http_tls_upgrade()
4591 * Copy the HTTP data to a local variable so we can do the OPTIONS in http_tls_upgrade()
4595 memcpy(&myhttp, http, sizeof(myhttp)); in http_tls_upgrade()
4602 http->tls_upgrade = 1; in http_tls_upgrade()
4603 memset(http->fields, 0, sizeof(http->fields)); in http_tls_upgrade()
4604 http->expect = (http_status_t)0; in http_tls_upgrade()
4606 if (http->hostname[0] == '/') in http_tls_upgrade()
4607 httpSetField(http, HTTP_FIELD_HOST, "localhost"); in http_tls_upgrade()
4609 httpSetField(http, HTTP_FIELD_HOST, http->hostname); in http_tls_upgrade()
4611 httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade"); in http_tls_upgrade()
4612 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); in http_tls_upgrade()
4614 if ((ret = httpOptions(http, "*")) == 0) in http_tls_upgrade()
4620 while (httpUpdate(http) == HTTP_STATUS_CONTINUE); in http_tls_upgrade()
4624 * Restore the HTTP request data... in http_tls_upgrade()
4627 memcpy(http->_fields, myhttp._fields, sizeof(http->_fields)); in http_tls_upgrade()
4628 memcpy(http->fields, myhttp.fields, sizeof(http->fields)); in http_tls_upgrade()
4630 http->data_encoding = myhttp.data_encoding; in http_tls_upgrade()
4631 http->data_remaining = myhttp.data_remaining; in http_tls_upgrade()
4632 http->_data_remaining = myhttp._data_remaining; in http_tls_upgrade()
4633 http->expect = myhttp.expect; in http_tls_upgrade()
4634 http->digest_tries = myhttp.digest_tries; in http_tls_upgrade()
4635 http->tls_upgrade = 0; in http_tls_upgrade()
4641 if (!http->tls) in http_tls_upgrade()
4644 * Server does not support HTTP upgrade... in http_tls_upgrade()
4647 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!"); in http_tls_upgrade()
4650 httpAddrClose(NULL, http->fd); in http_tls_upgrade()
4652 http->fd = -1; in http_tls_upgrade()
4663 * 'http_write()' - Write a buffer to a HTTP connection.
4667 http_write(http_t *http, /* I - HTTP connection */ in http_write() argument
4675 …DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buff… in http_write()
4676 http->error = 0; in http_write()
4683 if (http->timeout_value > 0.0) in http_write()
4696 pfd.fd = http->fd; in http_write()
4699 while ((nfds = poll(&pfd, 1, http->wait_value)) < 0 && in http_write()
4707 FD_SET(http->fd, &output_set); in http_write()
4709 timeout.tv_sec = http->wait_value / 1000; in http_write()
4710 timeout.tv_usec = 1000 * (http->wait_value % 1000); in http_write()
4712 nfds = select(http->fd + 1, NULL, &output_set, NULL, &timeout); in http_write()
4724 http->error = errno; in http_write()
4727 else if (nfds == 0 && (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data))) in http_write()
4730 http->error = WSAEWOULDBLOCK; in http_write()
4732 http->error = EWOULDBLOCK; in http_write()
4741 if (http->tls) in http_write()
4742 bytes = _httpTLSWrite(http, buffer, (int)length); in http_write()
4745 bytes = send(http->fd, buffer, length, 0); in http_write()
4757 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in http_write()
4760 http->error = WSAGetLastError(); in http_write()
4762 else if (WSAGetLastError() != http->error && in http_write()
4765 http->error = WSAGetLastError(); in http_write()
4774 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in http_write()
4776 else if (!http->timeout_cb && errno == EAGAIN) in http_write()
4779 http->error = errno; in http_write()
4781 else if (errno != http->error && errno != ECONNRESET) in http_write()
4783 http->error = errno; in http_write()
4789 strerror(http->error))); in http_write()
4814 http_write_chunk(http_t *http, /* I - HTTP connection */ in http_write_chunk() argument
4822 …DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void … in http_write_chunk()
4829 if (http_write(http, header, strlen(header)) < 0) in http_write_chunk()
4835 if ((bytes = http_write(http, buffer, length)) < 0) in http_write_chunk()
4841 if (http_write(http, "\r\n", 2) < 0) in http_write_chunk()