Lines Matching full:http
2 * HTTP routines for CUPS.
14 * missing or damaged, see the license at "http://www.cups.org/".
45 static void http_add_field(http_t *http, http_field_t field, const char *value, int append);
47 static void http_content_coding_finish(http_t *http);
48 static void http_content_coding_start(http_t *http,
59 static ssize_t http_read(http_t *http, char *buffer, size_t length);
60 static ssize_t http_read_buffered(http_t *http, char *buffer, size_t length);
61 static ssize_t http_read_chunk(http_t *http, char *buffer, size_t length);
62 static int http_send(http_t *http, http_state_t request,
64 static ssize_t http_write(http_t *http, const char *buffer,
66 static ssize_t http_write_chunk(http_t *http, const char *buffer,
68 static off_t http_set_length(http_t *http);
70 static void http_set_wait(http_t *http);
73 static int http_tls_upgrade(http_t *http);
118 * 'httpAcceptConnection()' - Accept a new HTTP client connection from the
124 http_t * /* O - HTTP connection or @code NULL@ */
129 http_t *http; /* HTTP connection */ in httpAcceptConnection() local
148 if ((http = http_create(NULL, 0, &addrlist, AF_UNSPEC, in httpAcceptConnection()
159 if ((http->fd = accept(fd, (struct sockaddr *)&(http->addrlist->addr), in httpAcceptConnection()
163 httpClose(http); in httpAcceptConnection()
168 http->hostaddr = &(http->addrlist->addr); in httpAcceptConnection()
170 if (httpAddrLocalhost(http->hostaddr)) in httpAcceptConnection()
171 strlcpy(http->hostname, "localhost", sizeof(http->hostname)); in httpAcceptConnection()
173 httpAddrString(http->hostaddr, http->hostname, sizeof(http->hostname)); in httpAcceptConnection()
181 setsockopt(http->fd, SOL_SOCKET, SO_NOSIGPIPE, CUPS_SOCAST &val, sizeof(val)); in httpAcceptConnection()
192 setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, CUPS_SOCAST &val, sizeof(val)); in httpAcceptConnection()
199 fcntl(http->fd, F_SETFD, FD_CLOEXEC); in httpAcceptConnection()
202 return (http); in httpAcceptConnection()
246 httpBlocking(http_t *http, /* I - HTTP connection */ in httpBlocking() argument
249 if (http) in httpBlocking()
251 http->blocking = b; in httpBlocking()
252 http_set_wait(http); in httpBlocking()
262 httpCheck(http_t *http) /* I - HTTP connection */ in httpCheck() argument
264 return (httpWait(http, 0)); in httpCheck()
275 httpClearCookie(http_t *http) /* I - HTTP connection */ in httpClearCookie() argument
277 if (!http) in httpClearCookie()
280 if (http->cookie) in httpClearCookie()
282 free(http->cookie); in httpClearCookie()
283 http->cookie = NULL; in httpClearCookie()
289 * 'httpClearFields()' - Clear HTTP request fields.
293 httpClearFields(http_t *http) /* I - HTTP connection */ in httpClearFields() argument
295 DEBUG_printf(("httpClearFields(http=%p)", (void *)http)); in httpClearFields()
297 if (http) in httpClearFields()
299 memset(http->fields, 0, sizeof(http->fields)); in httpClearFields()
301 if (http->mode == _HTTP_MODE_CLIENT) in httpClearFields()
303 if (http->hostname[0] == '/') in httpClearFields()
304 httpSetField(http, HTTP_FIELD_HOST, "localhost"); in httpClearFields()
306 httpSetField(http, HTTP_FIELD_HOST, http->hostname); in httpClearFields()
309 if (http->field_authorization) in httpClearFields()
311 free(http->field_authorization); in httpClearFields()
312 http->field_authorization = NULL; in httpClearFields()
315 if (http->accept_encoding) in httpClearFields()
317 _cupsStrFree(http->accept_encoding); in httpClearFields()
318 http->accept_encoding = NULL; in httpClearFields()
321 if (http->allow) in httpClearFields()
323 _cupsStrFree(http->allow); in httpClearFields()
324 http->allow = NULL; in httpClearFields()
327 if (http->server) in httpClearFields()
329 _cupsStrFree(http->server); in httpClearFields()
330 http->server = NULL; in httpClearFields()
333 if (http->authentication_info) in httpClearFields()
335 _cupsStrFree(http->authentication_info); in httpClearFields()
336 http->authentication_info = NULL; in httpClearFields()
339 http->expect = (http_status_t)0; in httpClearFields()
345 * 'httpClose()' - Close an HTTP connection.
349 httpClose(http_t *http) /* I - HTTP connection */ in httpClose() argument
356 DEBUG_printf(("httpClose(http=%p)", (void *)http)); in httpClose()
362 if (!http) in httpClose()
369 _httpDisconnect(http); in httpClose()
375 httpAddrFreeList(http->addrlist); in httpClose()
377 if (http->cookie) in httpClose()
378 free(http->cookie); in httpClose()
381 if (http->gssctx != GSS_C_NO_CONTEXT) in httpClose()
382 gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); in httpClose()
384 if (http->gssname != GSS_C_NO_NAME) in httpClose()
385 gss_release_name(&minor_status, &http->gssname); in httpClose()
389 if (http->auth_ref) in httpClose()
390 AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); in httpClose()
393 httpClearFields(http); in httpClose()
395 if (http->authstring && http->authstring != http->_authstring) in httpClose()
396 free(http->authstring); in httpClose()
398 free(http); in httpClose()
427 * 'httpConnect()' - Connect to a HTTP server.
434 http_t * /* O - New HTTP connection */
444 * 'httpConnect2()' - Connect to a HTTP server.
449 http_t * /* O - New HTTP connection */
460 http_t *http; /* New HTTP connection */ in httpConnect2() local
466 * Create the HTTP structure... in httpConnect2()
469 if ((http = http_create(host, port, addrlist, family, encryption, blocking, in httpConnect2()
477 if (msec == 0 || !httpReconnect2(http, msec, cancel)) in httpConnect2()
478 return (http); in httpConnect2()
484 httpClose(http); in httpConnect2()
491 * 'httpConnectEncrypt()' - Connect to a HTTP server using encryption.
499 http_t * /* O - New HTTP connection */
518 httpDelete(http_t *http, /* I - HTTP connection */ in httpDelete() argument
521 return (http_send(http, HTTP_STATE_DELETE, uri)); in httpDelete()
526 * '_httpDisconnect()' - Disconnect a HTTP connection.
530 _httpDisconnect(http_t *http) /* I - HTTP connection */ in _httpDisconnect() argument
533 if (http->tls) in _httpDisconnect()
534 _httpTLSStop(http); in _httpDisconnect()
537 httpAddrClose(NULL, http->fd); in _httpDisconnect()
539 http->fd = -1; in _httpDisconnect()
548 httpEncryption(http_t *http, /* I - HTTP connection */ in httpEncryption() argument
551 DEBUG_printf(("httpEncryption(http=%p, e=%d)", (void *)http, e)); in httpEncryption()
554 if (!http) in httpEncryption()
557 if (http->mode == _HTTP_MODE_CLIENT) in httpEncryption()
559 http->encryption = e; in httpEncryption()
561 if ((http->encryption == HTTP_ENCRYPTION_ALWAYS && !http->tls) || in httpEncryption()
562 (http->encryption == HTTP_ENCRYPTION_NEVER && http->tls)) in httpEncryption()
563 return (httpReconnect2(http, 30000, NULL)); in httpEncryption()
564 else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls) in httpEncryption()
565 return (http_tls_upgrade(http)); in httpEncryption()
571 if (e == HTTP_ENCRYPTION_NEVER && http->tls) in httpEncryption()
574 http->encryption = e; in httpEncryption()
575 if (e != HTTP_ENCRYPTION_IF_REQUESTED && !http->tls) in httpEncryption()
576 return (_httpTLSStart(http)); in httpEncryption()
594 httpError(http_t *http) /* I - HTTP connection */ in httpError() argument
596 if (http) in httpError()
597 return (http->error); in httpError()
604 * 'httpFieldValue()' - Return the HTTP field enumeration value for a field
623 * 'httpFlush()' - Flush data read from a HTTP connection.
627 httpFlush(http_t *http) /* I - HTTP connection */ in httpFlush() argument
634 DEBUG_printf(("httpFlush(http=%p), state=%s", (void *)http, httpStateString(http->state))); in httpFlush()
640 if (http->state == HTTP_STATE_WAITING) in httpFlush()
647 blocking = http->blocking; in httpFlush()
648 http->blocking = 0; in httpFlush()
654 oldstate = http->state; in httpFlush()
655 while (httpRead2(http, buffer, sizeof(buffer)) > 0); in httpFlush()
662 http->blocking = blocking; in httpFlush()
664 if (http->state == oldstate && http->state != HTTP_STATE_WAITING && in httpFlush()
665 http->fd >= 0) in httpFlush()
672 if (http->coding) in httpFlush()
673 http_content_coding_finish(http); in httpFlush()
678 http->state = HTTP_STATE_WAITING; in httpFlush()
681 if (http->tls) in httpFlush()
682 _httpTLSStop(http); in httpFlush()
685 httpAddrClose(NULL, http->fd); in httpFlush()
687 http->fd = -1; in httpFlush()
693 * 'httpFlushWrite()' - Flush data written to a HTTP connection.
699 httpFlushWrite(http_t *http) /* I - HTTP connection */ in httpFlushWrite() argument
704 …DEBUG_printf(("httpFlushWrite(http=%p) data_encoding=%d", (void *)http, http ? http->data_encoding… in httpFlushWrite()
706 if (!http || !http->wused) in httpFlushWrite()
708 DEBUG_puts(http ? "1httpFlushWrite: Write buffer is empty." : in httpFlushWrite()
713 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpFlushWrite()
714 bytes = http_write_chunk(http, http->wbuffer, (size_t)http->wused); in httpFlushWrite()
716 bytes = http_write(http, http->wbuffer, (size_t)http->wused); in httpFlushWrite()
718 http->wused = 0; in httpFlushWrite()
755 httpGet(http_t *http, /* I - HTTP connection */ in httpGet() argument
758 return (http_send(http, HTTP_STATE_GET, uri)); in httpGet()
771 httpGetActivity(http_t *http) /* I - HTTP connection */ in httpGetActivity() argument
773 return (http ? http->activity : 0); in httpGetActivity()
789 httpGetAuthString(http_t *http) /* I - HTTP connection */ in httpGetAuthString() argument
791 if (http) in httpGetAuthString()
792 return (http->authstring); in httpGetAuthString()
805 httpGetBlocking(http_t *http) /* I - HTTP connection */ in httpGetBlocking() argument
807 return (http ? http->blocking : 0); in httpGetBlocking()
815 * This function uses the value of the Accepts-Encoding HTTP header and must be
826 httpGetContentEncoding(http_t *http) /* I - HTTP connection */ in httpGetContentEncoding() argument
829 if (http && http->accept_encoding) in httpGetContentEncoding()
845 strlcpy(temp, http->accept_encoding, sizeof(temp)); in httpGetContentEncoding()
906 httpGetCookie(http_t *http) /* I - HTTP connection */ in httpGetCookie() argument
908 return (http ? http->cookie : NULL); in httpGetCookie()
923 httpGetEncryption(http_t *http) /* I - HTTP connection */ in httpGetEncryption() argument
925 return (http ? http->encryption : HTTP_ENCRYPTION_IF_REQUESTED); in httpGetEncryption()
933 * returns the expected HTTP status code, typically @code HTTP_STATUS_CONTINUE@.
939 httpGetExpect(http_t *http) /* I - HTTP connection */ in httpGetExpect() argument
941 if (!http) in httpGetExpect()
944 return (http->expect); in httpGetExpect()
955 httpGetFd(http_t *http) /* I - HTTP connection */ in httpGetFd() argument
957 return (http ? http->fd : -1); in httpGetFd()
966 httpGetField(http_t *http, /* I - HTTP connection */ in httpGetField() argument
969 if (!http || field <= HTTP_FIELD_UNKNOWN || field >= HTTP_FIELD_MAX) in httpGetField()
975 return (http->accept_encoding); in httpGetField()
978 return (http->allow); in httpGetField()
981 return (http->server); in httpGetField()
984 return (http->authentication_info); in httpGetField()
987 if (http->field_authorization) in httpGetField()
994 return (http->field_authorization); in httpGetField()
998 return (http->fields[field]); in httpGetField()
1010 httpGetKeepAlive(http_t *http) /* I - HTTP connection */ in httpGetKeepAlive() argument
1012 return (http ? http->keep_alive : HTTP_KEEPALIVE_OFF); in httpGetKeepAlive()
1027 httpGetLength(http_t *http) /* I - HTTP connection */ in httpGetLength() argument
1033 if (http) in httpGetLength()
1035 httpGetLength2(http); in httpGetLength()
1037 return (http->_data_remaining); in httpGetLength()
1055 httpGetLength2(http_t *http) /* I - HTTP connection */ in httpGetLength2() argument
1060 DEBUG_printf(("2httpGetLength2(http=%p), state=%s", (void *)http, httpStateString(http->state))); in httpGetLength2()
1062 if (!http) in httpGetLength2()
1065 if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked")) in httpGetLength2()
1073 * The following is a hack for HTTP servers that don't send a in httpGetLength2()
1080 if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0]) in httpGetLength2()
1087 if (http->status >= HTTP_STATUS_MULTIPLE_CHOICES || in httpGetLength2()
1088 http->state == HTTP_STATE_OPTIONS || in httpGetLength2()
1089 (http->state == HTTP_STATE_GET && http->mode == _HTTP_MODE_SERVER) || in httpGetLength2()
1090 http->state == HTTP_STATE_HEAD || in httpGetLength2()
1091 (http->state == HTTP_STATE_PUT && http->mode == _HTTP_MODE_CLIENT) || in httpGetLength2()
1092 http->state == HTTP_STATE_DELETE || in httpGetLength2()
1093 http->state == HTTP_STATE_TRACE || in httpGetLength2()
1094 http->state == HTTP_STATE_CONNECT) in httpGetLength2()
1099 else if ((remaining = strtoll(http->fields[HTTP_FIELD_CONTENT_LENGTH], in httpGetLength2()
1118 httpGetPending(http_t *http) /* I - HTTP connection */ in httpGetPending() argument
1120 return (http ? (size_t)http->wused : 0); in httpGetPending()
1131 httpGetReady(http_t *http) /* I - HTTP connection */ in httpGetReady() argument
1133 if (!http) in httpGetReady()
1135 else if (http->used > 0) in httpGetReady()
1136 return ((size_t)http->used); in httpGetReady()
1138 else if (http->tls) in httpGetReady()
1139 return (_httpTLSPending(http)); in httpGetReady()
1157 httpGetRemaining(http_t *http) /* I - HTTP connection */ in httpGetRemaining() argument
1159 return (http ? (size_t)http->data_remaining : 0); in httpGetRemaining()
1164 * 'httpGets()' - Get a line of text from a HTTP connection.
1170 http_t *http) /* I - HTTP connection */ in httpGets() argument
1180 DEBUG_printf(("2httpGets(line=%p, length=%d, http=%p)", (void *)line, length, (void *)http)); in httpGets()
1182 if (!http || !line || length <= 1) in httpGets()
1189 http->error = 0; in httpGets()
1206 while (http->used == 0) in httpGets()
1212 while (!_httpWait(http, http->wait_value, 1)) in httpGets()
1214 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpGets()
1219 http->error = WSAETIMEDOUT; in httpGets()
1221 http->error = ETIMEDOUT; in httpGets()
1226 bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used)); in httpGets()
1243 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpGets()
1246 http->error = WSAGetLastError(); in httpGets()
1248 else if (WSAGetLastError() != http->error) in httpGets()
1250 http->error = WSAGetLastError(); in httpGets()
1261 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpGets()
1263 else if (!http->timeout_cb && errno == EAGAIN) in httpGets()
1266 http->error = errno; in httpGets()
1268 else if (errno != http->error) in httpGets()
1270 http->error = errno; in httpGets()
1279 http->error = EPIPE; in httpGets()
1288 http->used += (int)bytes; in httpGets()
1295 for (bufptr = http->buffer, bufend = http->buffer + http->used; in httpGets()
1310 http->used -= (int)(bufptr - http->buffer); in httpGets()
1311 if (http->used > 0) in httpGets()
1312 memmove(http->buffer, bufptr, (size_t)http->used); in httpGets()
1320 http->activity = time(NULL); in httpGets()
1337 * 'httpGetState()' - Get the current state of the HTTP request.
1340 http_state_t /* O - HTTP state */
1341 httpGetState(http_t *http) /* I - HTTP connection */ in httpGetState() argument
1343 return (http ? http->state : HTTP_STATE_ERROR); in httpGetState()
1348 * 'httpGetStatus()' - Get the status of the last HTTP request.
1353 http_status_t /* O - HTTP status */
1354 httpGetStatus(http_t *http) /* I - HTTP connection */ in httpGetStatus() argument
1356 return (http ? http->status : HTTP_STATUS_ERROR); in httpGetStatus()
1367 httpGetSubField(http_t *http, /* I - HTTP connection */ in httpGetSubField() argument
1372 return (httpGetSubField2(http, field, name, value, HTTP_MAX_VALUE)); in httpGetSubField()
1383 httpGetSubField2(http_t *http, /* I - HTTP connection */ in httpGetSubField2() argument
1394 …DEBUG_printf(("2httpGetSubField2(http=%p, field=%d, name=\"%s\", value=%p, valuelen=%d)", (void *)… in httpGetSubField2()
1396 if (!http || !name || !value || valuelen < 2 || in httpGetSubField2()
1402 for (fptr = http->fields[field]; *fptr;) in httpGetSubField2()
1508 * 'httpGetVersion()' - Get the HTTP version at the other end.
1512 httpGetVersion(http_t *http) /* I - HTTP connection */ in httpGetVersion() argument
1514 return (http ? http->version : HTTP_VERSION_1_0); in httpGetVersion()
1523 httpHead(http_t *http, /* I - HTTP connection */ in httpHead() argument
1526 DEBUG_printf(("httpHead(http=%p, uri=\"%s\")", (void *)http, uri)); in httpHead()
1527 return (http_send(http, HTTP_STATE_HEAD, uri)); in httpHead()
1532 * 'httpInitialize()' - Initialize the HTTP interface library and set the
1533 * default HTTP proxy (if any).
1595 httpIsChunked(http_t *http) /* I - HTTP connection */ in httpIsChunked() argument
1597 return (http ? http->data_encoding == HTTP_ENCODING_CHUNKED : 0); in httpIsChunked()
1610 httpIsEncrypted(http_t *http) /* I - HTTP connection */ in httpIsEncrypted() argument
1612 return (http ? http->tls != NULL : 0); in httpIsEncrypted()
1621 httpOptions(http_t *http, /* I - HTTP connection */ in httpOptions() argument
1624 return (http_send(http, HTTP_STATE_OPTIONS, uri)); in httpOptions()
1629 * 'httpPeek()' - Peek at data from a HTTP connection.
1631 * This function copies available data from the given HTTP connection, reading
1641 httpPeek(http_t *http, /* I - HTTP connection */ in httpPeek() argument
1649 …DEBUG_printf(("httpPeek(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer,… in httpPeek()
1651 if (http == NULL || buffer == NULL) in httpPeek()
1654 http->activity = time(NULL); in httpPeek()
1655 http->error = 0; in httpPeek()
1660 if (http->data_encoding == HTTP_ENCODING_CHUNKED && in httpPeek()
1661 http->data_remaining <= 0) in httpPeek()
1665 if (httpGets(len, sizeof(len), http) == NULL) in httpPeek()
1674 if (!httpGets(len, sizeof(len), http)) in httpPeek()
1681 http->data_remaining = strtoll(len, NULL, 16); in httpPeek()
1683 if (http->data_remaining < 0) in httpPeek()
1691 CUPS_LLCAST http->data_remaining)); in httpPeek()
1693 if (http->data_remaining <= 0 && http->data_encoding != HTTP_ENCODING_FIELDS) in httpPeek()
1701 if (http->coding >= _HTTP_CODING_GUNZIP) in httpPeek()
1702 http_content_coding_finish(http); in httpPeek()
1705 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpPeek()
1706 httpGets(len, sizeof(len), http); in httpPeek()
1708 if (http->state == HTTP_STATE_POST_RECV) in httpPeek()
1709 http->state ++; in httpPeek()
1711 http->state = HTTP_STATE_STATUS; in httpPeek()
1714 httpStateString(http->state))); in httpPeek()
1720 http->data_encoding = HTTP_ENCODING_FIELDS; in httpPeek()
1724 else if (length > (size_t)http->data_remaining) in httpPeek()
1725 length = (size_t)http->data_remaining; in httpPeek()
1728 if (http->used == 0 && in httpPeek()
1729 (http->coding == _HTTP_CODING_IDENTITY || in httpPeek()
1730 (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in == 0))) in httpPeek()
1732 if (http->used == 0) in httpPeek()
1741 if (!http->blocking) in httpPeek()
1743 while (!httpWait(http, http->wait_value)) in httpPeek()
1745 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in httpPeek()
1752 if ((size_t)http->data_remaining > sizeof(http->buffer)) in httpPeek()
1753 buflen = sizeof(http->buffer); in httpPeek()
1755 buflen = (ssize_t)http->data_remaining; in httpPeek()
1758 bytes = http_read(http, http->buffer, (size_t)buflen); in httpPeek()
1765 http_debug_hex("httpPeek", http->buffer, (int)bytes); in httpPeek()
1768 http->used = (int)bytes; in httpPeek()
1773 if (http->coding >= _HTTP_CODING_GUNZIP) in httpPeek()
1779 if (http->used > 0 && ((z_stream *)http->stream)->avail_in < HTTP_MAX_BUFFER) in httpPeek()
1781 size_t buflen = buflen = HTTP_MAX_BUFFER - ((z_stream *)http->stream)->avail_in; in httpPeek()
1784 if (((z_stream *)http->stream)->avail_in > 0 && in httpPeek()
1785 ((z_stream *)http->stream)->next_in > http->sbuffer) in httpPeek()
1786 … memmove(http->sbuffer, ((z_stream *)http->stream)->next_in, ((z_stream *)http->stream)->avail_in); in httpPeek()
1788 ((z_stream *)http->stream)->next_in = http->sbuffer; in httpPeek()
1790 if (buflen > (size_t)http->data_remaining) in httpPeek()
1791 buflen = (size_t)http->data_remaining; in httpPeek()
1793 if (buflen > (size_t)http->used) in httpPeek()
1794 buflen = (size_t)http->used; in httpPeek()
1799 memcpy(http->sbuffer + ((z_stream *)http->stream)->avail_in, http->buffer, buflen); in httpPeek()
1800 ((z_stream *)http->stream)->avail_in += buflen; in httpPeek()
1801 http->used -= (int)buflen; in httpPeek()
1802 http->data_remaining -= (off_t)buflen; in httpPeek()
1804 if (http->used > 0) in httpPeek()
1805 memmove(http->buffer, http->buffer + buflen, (size_t)http->used); in httpPeek()
1809 (int)((z_stream *)http->stream)->avail_in)); in httpPeek()
1811 if (inflateCopy(&stream, (z_stream *)http->stream) != Z_OK) in httpPeek()
1814 http->error = ENOMEM; in httpPeek()
1828 http_debug_hex("2httpPeek", (char *)http->sbuffer, (int)((z_stream *)http->stream)->avail_in); in httpPeek()
1831 http->error = EIO; in httpPeek()
1835 bytes = (ssize_t)(length - ((z_stream *)http->stream)->avail_out); in httpPeek()
1845 if (http->used > 0) in httpPeek()
1847 if (length > (size_t)http->used) in httpPeek()
1848 length = (size_t)http->used; in httpPeek()
1855 memcpy(buffer, http->buffer, length); in httpPeek()
1866 http->error = WSAGetLastError(); in httpPeek()
1871 http->error = errno; in httpPeek()
1876 http->error = EPIPE; in httpPeek()
1889 httpPost(http_t *http, /* I - HTTP connection */ in httpPost() argument
1892 return (http_send(http, HTTP_STATE_POST, uri)); in httpPost()
1897 * 'httpPrintf()' - Print a formatted string to a HTTP connection.
1903 httpPrintf(http_t *http, /* I - HTTP connection */ in httpPrintf() argument
1912 DEBUG_printf(("2httpPrintf(http=%p, format=\"%s\", ...)", (void *)http, format)); in httpPrintf()
1920 if (http->data_encoding == HTTP_ENCODING_FIELDS) in httpPrintf()
1921 return ((int)httpWrite2(http, buf, (size_t)bytes)); in httpPrintf()
1924 if (http->wused) in httpPrintf()
1928 if (httpFlushWrite(http) < 0) in httpPrintf()
1932 return ((int)http_write(http, buf, (size_t)bytes)); in httpPrintf()
1942 httpPut(http_t *http, /* I - HTTP connection */ in httpPut() argument
1945 DEBUG_printf(("httpPut(http=%p, uri=\"%s\")", (void *)http, uri)); in httpPut()
1946 return (http_send(http, HTTP_STATE_PUT, uri)); in httpPut()
1951 * 'httpRead()' - Read data from a HTTP connection.
1960 httpRead(http_t *http, /* I - HTTP connection */ in httpRead() argument
1964 return ((int)httpRead2(http, buffer, (size_t)length)); in httpRead()
1969 * 'httpRead2()' - Read data from a HTTP connection.
1975 httpRead2(http_t *http, /* I - HTTP connection */ in httpRead2() argument
1983 …http=%p, buffer=%p, length=" CUPS_LLFMT ") coding=%d data_encoding=%d data_remaining=" CUPS_LLFMT,… in httpRead2()
1985 …http=%p, buffer=%p, length=" CUPS_LLFMT ") data_encoding=%d data_remaining=" CUPS_LLFMT, (void *)h… in httpRead2()
1988 if (http == NULL || buffer == NULL) in httpRead2()
1991 http->activity = time(NULL); in httpRead2()
1992 http->error = 0; in httpRead2()
1998 if (http->coding >= _HTTP_CODING_GUNZIP) in httpRead2()
2002 if (((z_stream *)http->stream)->avail_in > 0) in httpRead2()
2007 (int)((z_stream *)http->stream)->avail_in, (int)length)); in httpRead2()
2009 ((z_stream *)http->stream)->next_out = (Bytef *)buffer; in httpRead2()
2010 ((z_stream *)http->stream)->avail_out = (uInt)length; in httpRead2()
2012 if ((zerr = inflate((z_stream *)http->stream, Z_SYNC_FLUSH)) < Z_OK) in httpRead2()
2016 … http_debug_hex("2httpRead2", (char *)http->sbuffer, (int)((z_stream *)http->stream)->avail_in); in httpRead2()
2019 http->error = EIO; in httpRead2()
2023 bytes = (ssize_t)(length - ((z_stream *)http->stream)->avail_out); in httpRead2()
2026 ((z_stream *)http->stream)->avail_in, ((z_stream *)http->stream)->avail_out, in httpRead2()
2034 ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)((z_stream *)http->stream)->avail_in; in httpRead2()
2039 if (((z_stream *)http->stream)->avail_in > 0 && in httpRead2()
2040 ((z_stream *)http->stream)->next_in > http->sbuffer) in httpRead2()
2041 … memmove(http->sbuffer, ((z_stream *)http->stream)->next_in, ((z_stream *)http->stream)->avail_in); in httpRead2()
2043 ((z_stream *)http->stream)->next_in = http->sbuffer; in httpRead2()
2048 if (http->data_remaining > 0) in httpRead2()
2050 if (buflen > http->data_remaining) in httpRead2()
2051 buflen = (ssize_t)http->data_remaining; in httpRead2()
2053 …bytes = http_read_buffered(http, (char *)http->sbuffer + ((z_stream *)http->stream)->avail_in, (si… in httpRead2()
2055 else if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2056 …bytes = http_read_chunk(http, (char *)http->sbuffer + ((z_stream *)http->stream)->avail_in, (size_… in httpRead2()
2068 http->data_remaining -= bytes; in httpRead2()
2069 ((z_stream *)http->stream)->avail_in += (uInt)bytes; in httpRead2()
2071 if (http->data_remaining <= 0 && in httpRead2()
2072 http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2080 httpGets(len, sizeof(len), http); in httpRead2()
2093 if (http->data_remaining == 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2095 if ((bytes = http_read_chunk(http, buffer, length)) > 0) in httpRead2()
2097 http->data_remaining -= bytes; in httpRead2()
2099 if (http->data_remaining <= 0) in httpRead2()
2107 httpGets(len, sizeof(len), http); in httpRead2()
2111 else if (http->data_remaining <= 0) in httpRead2()
2124 if (length > (size_t)http->data_remaining) in httpRead2()
2125 length = (size_t)http->data_remaining; in httpRead2()
2127 if ((bytes = http_read_buffered(http, buffer, length)) > 0) in httpRead2()
2129 http->data_remaining -= bytes; in httpRead2()
2131 if (http->data_remaining <= 0 && in httpRead2()
2132 http->data_encoding == HTTP_ENCODING_CHUNKED) in httpRead2()
2140 httpGets(len, sizeof(len), http); in httpRead2()
2147 (http->coding == _HTTP_CODING_IDENTITY || in httpRead2()
2148 (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in == 0)) && in httpRead2()
2150 ((http->data_remaining <= 0 && in httpRead2()
2151 http->data_encoding == HTTP_ENCODING_LENGTH) || in httpRead2()
2152 (http->data_encoding == HTTP_ENCODING_CHUNKED && bytes == 0))) in httpRead2()
2155 if (http->coding >= _HTTP_CODING_GUNZIP) in httpRead2()
2156 http_content_coding_finish(http); in httpRead2()
2159 if (http->state == HTTP_STATE_POST_RECV) in httpRead2()
2160 http->state ++; in httpRead2()
2161 else if (http->state == HTTP_STATE_GET_SEND || in httpRead2()
2162 http->state == HTTP_STATE_POST_SEND) in httpRead2()
2163 http->state = HTTP_STATE_WAITING; in httpRead2()
2165 http->state = HTTP_STATE_STATUS; in httpRead2()
2168 httpStateString(http->state))); in httpRead2()
2176 * 'httpReadRequest()' - Read a HTTP request from a connection.
2182 httpReadRequest(http_t *http, /* I - HTTP connection */ in httpReadRequest() argument
2186 char line[4096], /* HTTP request line */ in httpReadRequest()
2187 *req_method, /* HTTP request method */ in httpReadRequest()
2188 *req_uri, /* HTTP request URI */ in httpReadRequest()
2189 *req_version; /* HTTP request version number string */ in httpReadRequest()
2196 …DEBUG_printf(("httpReadRequest(http=%p, uri=%p, urilen=" CUPS_LLFMT ")", (void *)http, (void *)uri… in httpReadRequest()
2201 if (!http || !uri || urilen < 1) in httpReadRequest()
2206 else if (http->state != HTTP_STATE_WAITING) in httpReadRequest()
2209 httpStateString(http->state))); in httpReadRequest()
2217 httpClearFields(http); in httpReadRequest()
2219 http->activity = time(NULL); in httpReadRequest()
2220 http->data_encoding = HTTP_ENCODING_FIELDS; in httpReadRequest()
2221 http->data_remaining = 0; in httpReadRequest()
2222 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpReadRequest()
2223 http->status = HTTP_STATUS_OK; in httpReadRequest()
2224 http->version = HTTP_VERSION_1_1; in httpReadRequest()
2230 if (!httpGets(line, sizeof(line), http)) in httpReadRequest()
2288 http->state = HTTP_STATE_OPTIONS; in httpReadRequest()
2290 http->state = HTTP_STATE_GET; in httpReadRequest()
2292 http->state = HTTP_STATE_HEAD; in httpReadRequest()
2294 http->state = HTTP_STATE_POST; in httpReadRequest()
2296 http->state = HTTP_STATE_PUT; in httpReadRequest()
2298 http->state = HTTP_STATE_DELETE; in httpReadRequest()
2300 http->state = HTTP_STATE_TRACE; in httpReadRequest()
2302 http->state = HTTP_STATE_CONNECT; in httpReadRequest()
2311 httpStateString(http->state))); in httpReadRequest()
2313 if (!strcmp(req_version, "HTTP/1.0")) in httpReadRequest()
2315 http->version = HTTP_VERSION_1_0; in httpReadRequest()
2316 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpReadRequest()
2318 else if (!strcmp(req_version, "HTTP/1.1")) in httpReadRequest()
2320 http->version = HTTP_VERSION_1_1; in httpReadRequest()
2321 http->keep_alive = HTTP_KEEPALIVE_ON; in httpReadRequest()
2333 return (http->state); in httpReadRequest()
2338 * 'httpReconnect()' - Reconnect to a HTTP server.
2347 httpReconnect(http_t *http) /* I - HTTP connection */ in httpReconnect() argument
2349 DEBUG_printf(("httpReconnect(http=%p)", (void *)http)); in httpReconnect()
2351 return (httpReconnect2(http, 30000, NULL)); in httpReconnect()
2356 * 'httpReconnect2()' - Reconnect to a HTTP server with timeout and optional
2361 httpReconnect2(http_t *http, /* I - HTTP connection */ in httpReconnect2() argument
2372 DEBUG_printf(("httpReconnect2(http=%p, msec=%d, cancel=%p)", (void *)http, msec, (void *)cancel)); in httpReconnect2()
2374 if (!http) in httpReconnect2()
2381 if (http->tls) in httpReconnect2()
2384 _httpTLSStop(http); in httpReconnect2()
2392 if (http->fd >= 0) in httpReconnect2()
2394 DEBUG_printf(("2httpReconnect2: Closing socket %d...", http->fd)); in httpReconnect2()
2396 httpAddrClose(NULL, http->fd); in httpReconnect2()
2398 http->fd = -1; in httpReconnect2()
2405 http->state = HTTP_STATE_WAITING; in httpReconnect2()
2406 http->version = HTTP_VERSION_1_1; in httpReconnect2()
2407 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpReconnect2()
2408 memset(&http->_hostaddr, 0, sizeof(http->_hostaddr)); in httpReconnect2()
2409 http->data_encoding = HTTP_ENCODING_FIELDS; in httpReconnect2()
2410 http->_data_remaining = 0; in httpReconnect2()
2411 http->used = 0; in httpReconnect2()
2412 http->data_remaining = 0; in httpReconnect2()
2413 http->hostaddr = NULL; in httpReconnect2()
2414 http->wused = 0; in httpReconnect2()
2421 for (current = http->addrlist; current; current = current->next) in httpReconnect2()
2427 if ((addr = httpAddrConnect2(http->addrlist, &(http->fd), msec, cancel)) == NULL) in httpReconnect2()
2434 http->error = WSAGetLastError(); in httpReconnect2()
2436 http->error = errno; in httpReconnect2()
2438 http->status = HTTP_STATUS_ERROR; in httpReconnect2()
2441 strerror(http->error))); in httpReconnect2()
2446 DEBUG_printf(("2httpReconnect2: New socket=%d", http->fd)); in httpReconnect2()
2448 if (http->timeout_value > 0) in httpReconnect2()
2449 http_set_timeout(http->fd, http->timeout_value); in httpReconnect2()
2451 http->hostaddr = &(addr->addr); in httpReconnect2()
2452 http->error = 0; in httpReconnect2()
2455 if (http->encryption == HTTP_ENCRYPTION_ALWAYS) in httpReconnect2()
2461 if (_httpTLSStart(http) != 0) in httpReconnect2()
2463 httpAddrClose(NULL, http->fd); in httpReconnect2()
2468 else if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls_upgrade) in httpReconnect2()
2469 return (http_tls_upgrade(http)); in httpReconnect2()
2473 httpAddrString(http->hostaddr, temp, sizeof(temp)), in httpReconnect2()
2474 httpAddrPort(http->hostaddr))); in httpReconnect2()
2484 * the HTTP connection object. You must still call @link httpSetField@ to set
2485 * @code HTTP_FIELD_AUTHORIZATION@ prior to issuing a HTTP request using
2493 httpSetAuthString(http_t *http, /* I - HTTP connection */ in httpSetAuthString() argument
2501 if (!http) in httpSetAuthString()
2504 if (http->authstring && http->authstring != http->_authstring) in httpSetAuthString()
2505 free(http->authstring); in httpSetAuthString()
2507 http->authstring = http->_authstring; in httpSetAuthString()
2518 if (len > sizeof(http->_authstring)) in httpSetAuthString()
2521 len = sizeof(http->_authstring); in httpSetAuthString()
2523 http->authstring = temp; in httpSetAuthString()
2527 snprintf(http->authstring, len, "%s %s", scheme, data); in httpSetAuthString()
2529 strlcpy(http->authstring, scheme, len); in httpSetAuthString()
2537 http->_authstring[0] = '\0'; in httpSetAuthString()
2550 httpSetCredentials(http_t *http, /* I - HTTP connection */ in httpSetCredentials() argument
2553 if (!http || cupsArrayCount(credentials) < 1) in httpSetCredentials()
2557 _httpFreeCredentials(http->tls_credentials); in httpSetCredentials()
2559 http->tls_credentials = _httpCreateCredentials(credentials); in httpSetCredentials()
2562 return (http->tls_credentials ? 0 : -1); in httpSetCredentials()
2573 httpSetCookie(http_t *http, /* I - Connection */ in httpSetCookie() argument
2576 if (!http) in httpSetCookie()
2579 if (http->cookie) in httpSetCookie()
2580 free(http->cookie); in httpSetCookie()
2583 http->cookie = strdup(cookie); in httpSetCookie()
2585 http->cookie = NULL; in httpSetCookie()
2590 * 'httpSetDefaultField()' - Set the default value of an HTTP header.
2599 httpSetDefaultField(http_t *http, /* I - HTTP connection */ in httpSetDefaultField() argument
2603 …DEBUG_printf(("httpSetDefaultField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, htt… in httpSetDefaultField()
2605 if (!http) in httpSetDefaultField()
2611 if (http->default_accept_encoding) in httpSetDefaultField()
2612 _cupsStrFree(http->default_accept_encoding); in httpSetDefaultField()
2614 http->default_accept_encoding = value ? _cupsStrAlloc(value) : NULL; in httpSetDefaultField()
2618 if (http->default_server) in httpSetDefaultField()
2619 _cupsStrFree(http->default_server); in httpSetDefaultField()
2621 http->default_server = value ? _cupsStrAlloc(value) : NULL; in httpSetDefaultField()
2625 if (http->default_user_agent) in httpSetDefaultField()
2626 _cupsStrFree(http->default_user_agent); in httpSetDefaultField()
2628 http->default_user_agent = value ? _cupsStrAlloc(value) : NULL; in httpSetDefaultField()
2648 httpSetExpect(http_t *http, /* I - HTTP connection */ in httpSetExpect() argument
2649 http_status_t expect) /* I - HTTP status to expect in httpSetExpect()
2652 DEBUG_printf(("httpSetExpect(http=%p, expect=%d)", (void *)http, expect)); in httpSetExpect()
2654 if (http) in httpSetExpect()
2655 http->expect = expect; in httpSetExpect()
2660 * 'httpSetField()' - Set the value of an HTTP header.
2664 httpSetField(http_t *http, /* I - HTTP connection */ in httpSetField() argument
2668 …DEBUG_printf(("httpSetField(http=%p, field=%d(%s), value=\"%s\")", (void *)http, field, http_field… in httpSetField()
2670 if (http == NULL || in httpSetField()
2676 http_add_field(http, field, value, 0); in httpSetField()
2688 http_t *http, /* I - HTTP connection */ in httpSetKeepAlive() argument
2691 if (http) in httpSetKeepAlive()
2692 http->keep_alive = keep_alive; in httpSetKeepAlive()
2703 httpSetLength(http_t *http, /* I - HTTP connection */ in httpSetLength() argument
2706 DEBUG_printf(("httpSetLength(http=%p, length=" CUPS_LLFMT ")", (void *)http, CUPS_LLCAST length)); in httpSetLength()
2708 if (!http) in httpSetLength()
2713 strlcpy(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked", in httpSetLength()
2715 http->fields[HTTP_FIELD_CONTENT_LENGTH][0] = '\0'; in httpSetLength()
2719 http->fields[HTTP_FIELD_TRANSFER_ENCODING][0] = '\0'; in httpSetLength()
2720 snprintf(http->fields[HTTP_FIELD_CONTENT_LENGTH], HTTP_MAX_VALUE, in httpSetLength()
2729 * The optional timeout callback receives both the HTTP connection and a user
2737 http_t *http, /* I - HTTP connection */ in httpSetTimeout() argument
2743 if (!http || timeout <= 0.0) in httpSetTimeout()
2746 http->timeout_cb = cb; in httpSetTimeout()
2747 http->timeout_data = user_data; in httpSetTimeout()
2748 http->timeout_value = timeout; in httpSetTimeout()
2750 if (http->fd >= 0) in httpSetTimeout()
2751 http_set_timeout(http->fd, timeout); in httpSetTimeout()
2753 http_set_wait(http); in httpSetTimeout()
2758 * 'httpShutdown()' - Shutdown one side of an HTTP connection.
2764 httpShutdown(http_t *http) /* I - HTTP connection */ in httpShutdown() argument
2766 if (!http || http->fd < 0) in httpShutdown()
2770 if (http->tls) in httpShutdown()
2771 _httpTLSStop(http); in httpShutdown()
2775 shutdown(http->fd, SD_RECEIVE); /* Microsoft-ism... */ in httpShutdown()
2777 shutdown(http->fd, SHUT_RD); in httpShutdown()
2789 httpTrace(http_t *http, /* I - HTTP connection */ in httpTrace() argument
2792 return (http_send(http, HTTP_STATE_TRACE, uri)); in httpTrace()
2797 * '_httpUpdate()' - Update the current HTTP status for incoming data.
2800 * and only retrieves a single status line from the HTTP connection.
2804 _httpUpdate(http_t *http, /* I - HTTP connection */ in _httpUpdate() argument
2805 http_status_t *status) /* O - Current HTTP status */ in _httpUpdate()
2810 int major, minor; /* HTTP version numbers */ in _httpUpdate()
2813 …DEBUG_printf(("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpState… in _httpUpdate()
2819 if (!httpGets(line, sizeof(line), http)) in _httpUpdate()
2838 if (http->status == HTTP_STATUS_CONTINUE) in _httpUpdate()
2840 *status = http->status; in _httpUpdate()
2844 if (http->status < HTTP_STATUS_BAD_REQUEST) in _httpUpdate()
2845 http->digest_tries = 0; in _httpUpdate()
2848 if (http->status == HTTP_STATUS_SWITCHING_PROTOCOLS && !http->tls) in _httpUpdate()
2850 if (_httpTLSStart(http) != 0) in _httpUpdate()
2852 httpAddrClose(NULL, http->fd); in _httpUpdate()
2854 *status = http->status = HTTP_STATUS_ERROR; in _httpUpdate()
2863 if (http_set_length(http) < 0) in _httpUpdate()
2866 http->error = EINVAL; in _httpUpdate()
2867 http->status = *status = HTTP_STATUS_ERROR; in _httpUpdate()
2871 switch (http->state) in _httpUpdate()
2877 http->state ++; in _httpUpdate()
2880 httpStateString(http->state))); in _httpUpdate()
2887 http->state = HTTP_STATE_WAITING; in _httpUpdate()
2895 http_content_coding_start(http, in _httpUpdate()
2896 httpGetField(http, HTTP_FIELD_CONTENT_ENCODING)); in _httpUpdate()
2899 *status = http->status; in _httpUpdate()
2902 else if (!strncmp(line, "HTTP/", 5) && http->mode == _HTTP_MODE_CLIENT) in _httpUpdate()
2910 if (sscanf(line, "HTTP/%d.%d%d", &major, &minor, &intstatus) != 3) in _httpUpdate()
2912 *status = http->status = HTTP_STATUS_ERROR; in _httpUpdate()
2916 httpClearFields(http); in _httpUpdate()
2918 http->version = (http_version_t)(major * 100 + minor); in _httpUpdate()
2919 *status = http->status = (http_status_t)intstatus; in _httpUpdate()
2943 http->expect = (http_status_t)atoi(value); in _httpUpdate()
2951 httpSetCookie(http, value); in _httpUpdate()
2955 http_add_field(http, field, value, 1); in _httpUpdate()
2958 …httpGetSubField2(http, HTTP_FIELD_AUTHENTICATION_INFO, "nextnonce", http->nextnonce, (int)sizeof(h… in _httpUpdate()
2968 http->error = EINVAL; in _httpUpdate()
2969 http->status = *status = HTTP_STATUS_ERROR; in _httpUpdate()
2978 * 'httpUpdate()' - Update the current HTTP state for incoming data.
2981 http_status_t /* O - HTTP status */
2982 httpUpdate(http_t *http) /* I - HTTP connection */ in httpUpdate() argument
2987 DEBUG_printf(("httpUpdate(http=%p), state=%s", (void *)http, httpStateString(http->state))); in httpUpdate()
2993 if (http->wused) in httpUpdate()
2997 if (httpFlushWrite(http) < 0) in httpUpdate()
3005 if (http->state == HTTP_STATE_WAITING) in httpUpdate()
3012 while (_httpUpdate(http, &status)); in httpUpdate()
3018 if (http->error == EPIPE && http->status > HTTP_STATUS_CONTINUE) in httpUpdate()
3020 DEBUG_printf(("1httpUpdate: Returning status %d...", http->status)); in httpUpdate()
3021 return (http->status); in httpUpdate()
3024 if (http->error) in httpUpdate()
3026 DEBUG_printf(("1httpUpdate: socket error %d - %s", http->error, in httpUpdate()
3027 strerror(http->error))); in httpUpdate()
3028 http->status = HTTP_STATUS_ERROR; in httpUpdate()
3045 _httpWait(http_t *http, /* I - HTTP connection */ in _httpWait() argument
3058 DEBUG_printf(("4_httpWait(http=%p, msec=%d, usessl=%d)", (void *)http, msec, usessl)); in _httpWait()
3060 if (http->fd < 0) in _httpWait()
3062 DEBUG_printf(("5_httpWait: Returning 0 since fd=%d", http->fd)); in _httpWait()
3071 if (http->tls && _httpTLSPending(http)) in _httpWait()
3083 pfd.fd = http->fd; in _httpWait()
3096 FD_SET(http->fd, &input_set); in _httpWait()
3098 DEBUG_printf(("6_httpWait: msec=%d, http->fd=%d", msec, http->fd)); in _httpWait()
3105 nfds = select(http->fd + 1, &input_set, NULL, NULL, &timeout); in _httpWait()
3108 nfds = select(http->fd + 1, &input_set, NULL, NULL, NULL); in _httpWait()
3134 httpWait(http_t *http, /* I - HTTP connection */ in httpWait() argument
3141 DEBUG_printf(("2httpWait(http=%p, msec=%d)", (void *)http, msec)); in httpWait()
3143 if (http == NULL) in httpWait()
3146 if (http->used) in httpWait()
3153 if (http->coding >= _HTTP_CODING_GUNZIP && ((z_stream *)http->stream)->avail_in > 0) in httpWait()
3164 if (http->wused) in httpWait()
3168 if (httpFlushWrite(http) < 0) in httpWait()
3176 return (_httpWait(http, msec, 1)); in httpWait()
3181 * 'httpWrite()' - Write data to a HTTP connection.
3190 httpWrite(http_t *http, /* I - HTTP connection */ in httpWrite() argument
3194 return ((int)httpWrite2(http, buffer, (size_t)length)); in httpWrite()
3199 * 'httpWrite2()' - Write data to a HTTP connection.
3205 httpWrite2(http_t *http, /* I - HTTP connection */ in httpWrite2() argument
3212 …DEBUG_printf(("httpWrite2(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffe… in httpWrite2()
3218 if (!http || !buffer) in httpWrite2()
3228 http->activity = time(NULL); in httpWrite2()
3235 if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE) in httpWrite2()
3237 DEBUG_printf(("1httpWrite2: http->coding=%d", http->coding)); in httpWrite2()
3241 http_content_coding_finish(http); in httpWrite2()
3249 ((z_stream *)http->stream)->next_in = (Bytef *)buffer; in httpWrite2()
3250 ((z_stream *)http->stream)->avail_in = (uInt)length; in httpWrite2()
3252 while (deflate((z_stream *)http->stream, Z_NO_FLUSH) == Z_OK) in httpWrite2()
3254 DEBUG_printf(("1httpWrite2: avail_out=%d", ((z_stream *)http->stream)->avail_out)); in httpWrite2()
3256 if (((z_stream *)http->stream)->avail_out > 0) in httpWrite2()
3259 slen = _HTTP_MAX_SBUFFER - ((z_stream *)http->stream)->avail_out; in httpWrite2()
3263 if (slen > 0 && http->data_encoding == HTTP_ENCODING_CHUNKED) in httpWrite2()
3264 sret = http_write_chunk(http, (char *)http->sbuffer, slen); in httpWrite2()
3266 sret = http_write(http, (char *)http->sbuffer, slen); in httpWrite2()
3276 ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; in httpWrite2()
3277 ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; in httpWrite2()
3287 if (http->wused && (length + (size_t)http->wused) > sizeof(http->wbuffer)) in httpWrite2()
3290 CUPS_LLFMT ")", http->wused, CUPS_LLCAST length)); in httpWrite2()
3292 httpFlushWrite(http); in httpWrite2()
3295 if ((length + (size_t)http->wused) <= sizeof(http->wbuffer) && length < sizeof(http->wbuffer)) in httpWrite2()
3304 memcpy(http->wbuffer + http->wused, buffer, length); in httpWrite2()
3305 http->wused += (int)length; in httpWrite2()
3317 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpWrite2()
3318 bytes = (ssize_t)http_write_chunk(http, buffer, length); in httpWrite2()
3320 bytes = (ssize_t)http_write(http, buffer, length); in httpWrite2()
3326 if (http->data_encoding == HTTP_ENCODING_LENGTH) in httpWrite2()
3327 http->data_remaining -= bytes; in httpWrite2()
3336 if ((http->data_encoding == HTTP_ENCODING_CHUNKED && length == 0) || in httpWrite2()
3337 (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0)) in httpWrite2()
3345 if (http->coding == _HTTP_CODING_GZIP || http->coding == _HTTP_CODING_DEFLATE) in httpWrite2()
3346 http_content_coding_finish(http); in httpWrite2()
3349 if (http->wused) in httpWrite2()
3351 if (httpFlushWrite(http) < 0) in httpWrite2()
3355 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in httpWrite2()
3361 http_write(http, "0\r\n\r\n", 5); in httpWrite2()
3367 http->data_encoding = HTTP_ENCODING_FIELDS; in httpWrite2()
3368 http->data_remaining = 0; in httpWrite2()
3371 if (http->state == HTTP_STATE_POST_RECV) in httpWrite2()
3372 http->state ++; in httpWrite2()
3373 else if (http->state == HTTP_STATE_POST_SEND || in httpWrite2()
3374 http->state == HTTP_STATE_GET_SEND) in httpWrite2()
3375 http->state = HTTP_STATE_WAITING; in httpWrite2()
3377 http->state = HTTP_STATE_STATUS; in httpWrite2()
3380 httpStateString(http->state))); in httpWrite2()
3390 * 'httpWriteResponse()' - Write a HTTP response to a client connection.
3396 httpWriteResponse(http_t *http, /* I - HTTP connection */ in httpWriteResponse() argument
3407 DEBUG_printf(("httpWriteResponse(http=%p, status=%d)", (void *)http, status)); in httpWriteResponse()
3409 if (!http || status < HTTP_STATUS_CONTINUE) in httpWriteResponse()
3419 if (!http->fields[HTTP_FIELD_DATE][0]) in httpWriteResponse()
3420 httpSetField(http, HTTP_FIELD_DATE, httpGetDateString(time(NULL))); in httpWriteResponse()
3422 if (status >= HTTP_STATUS_BAD_REQUEST && http->keep_alive) in httpWriteResponse()
3424 http->keep_alive = HTTP_KEEPALIVE_OFF; in httpWriteResponse()
3425 httpSetField(http, HTTP_FIELD_KEEP_ALIVE, ""); in httpWriteResponse()
3428 if (http->version == HTTP_VERSION_1_1) in httpWriteResponse()
3430 if (!http->fields[HTTP_FIELD_CONNECTION][0]) in httpWriteResponse()
3432 if (http->keep_alive) in httpWriteResponse()
3433 httpSetField(http, HTTP_FIELD_CONNECTION, "Keep-Alive"); in httpWriteResponse()
3435 httpSetField(http, HTTP_FIELD_CONNECTION, "close"); in httpWriteResponse()
3438 if (http->keep_alive && !http->fields[HTTP_FIELD_KEEP_ALIVE][0]) in httpWriteResponse()
3439 httpSetField(http, HTTP_FIELD_KEEP_ALIVE, "timeout=10"); in httpWriteResponse()
3446 if (!http->fields[HTTP_FIELD_CONNECTION][0]) in httpWriteResponse()
3447 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); in httpWriteResponse()
3449 if (!http->fields[HTTP_FIELD_UPGRADE][0]) in httpWriteResponse()
3450 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); in httpWriteResponse()
3452 if (!http->fields[HTTP_FIELD_CONTENT_LENGTH][0]) in httpWriteResponse()
3453 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, "0"); in httpWriteResponse()
3457 if (!http->server) in httpWriteResponse()
3458 httpSetField(http, HTTP_FIELD_SERVER, in httpWriteResponse()
3459 http->default_server ? http->default_server : CUPS_MINIMAL); in httpWriteResponse()
3465 if (!http->accept_encoding) in httpWriteResponse()
3466 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, in httpWriteResponse()
3467 http->default_accept_encoding ? http->default_accept_encoding : in httpWriteResponse()
3478 old_encoding = http->data_encoding; in httpWriteResponse()
3479 old_remaining = http->data_remaining; in httpWriteResponse()
3480 http->data_encoding = HTTP_ENCODING_FIELDS; in httpWriteResponse()
3482 if (httpPrintf(http, "HTTP/%d.%d %d %s\r\n", http->version / 100, in httpWriteResponse()
3483 http->version % 100, (int)status, httpStatus(status)) < 0) in httpWriteResponse()
3485 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3500 if ((value = httpGetField(http, i)) != NULL && *value) in httpWriteResponse()
3502 if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1) in httpWriteResponse()
3504 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3510 if (http->cookie) in httpWriteResponse()
3512 if (strchr(http->cookie, ';')) in httpWriteResponse()
3514 if (httpPrintf(http, "Set-Cookie: %s\r\n", http->cookie) < 1) in httpWriteResponse()
3516 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3520 …else if (httpPrintf(http, "Set-Cookie: %s; path=/; httponly;%s\r\n", http->cookie, http->tls ? " s… in httpWriteResponse()
3522 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3531 if (httpPrintf(http, "X-Frame-Options: DENY\r\n" in httpWriteResponse()
3534 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3539 if (httpWrite2(http, "\r\n", 2) < 2) in httpWriteResponse()
3541 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3545 if (httpFlushWrite(http) < 0) in httpWriteResponse()
3547 http->status = HTTP_STATUS_ERROR; in httpWriteResponse()
3558 http->data_encoding = old_encoding; in httpWriteResponse()
3559 http->data_remaining = old_remaining; in httpWriteResponse()
3562 http->_data_remaining = (int)old_remaining; in httpWriteResponse()
3564 http->_data_remaining = INT_MAX; in httpWriteResponse()
3566 else if (http->state == HTTP_STATE_OPTIONS || in httpWriteResponse()
3567 http->state == HTTP_STATE_HEAD || in httpWriteResponse()
3568 http->state == HTTP_STATE_PUT || in httpWriteResponse()
3569 http->state == HTTP_STATE_TRACE || in httpWriteResponse()
3570 http->state == HTTP_STATE_CONNECT || in httpWriteResponse()
3571 http->state == HTTP_STATE_STATUS) in httpWriteResponse()
3574 "was %s.", httpStateString(http->state))); in httpWriteResponse()
3575 http->state = HTTP_STATE_WAITING; in httpWriteResponse()
3584 http_set_length(http); in httpWriteResponse()
3586 if (http->data_encoding == HTTP_ENCODING_LENGTH && http->data_remaining == 0) in httpWriteResponse()
3589 "was %s.", httpStateString(http->state))); in httpWriteResponse()
3590 http->state = HTTP_STATE_WAITING; in httpWriteResponse()
3594 if (http->state == HTTP_STATE_POST_RECV || http->state == HTTP_STATE_GET) in httpWriteResponse()
3595 http->state ++; in httpWriteResponse()
3603 http_content_coding_start(http, in httpWriteResponse()
3604 httpGetField(http, HTTP_FIELD_CONTENT_ENCODING)); in httpWriteResponse()
3614 * 'http_add_field()' - Add a value for a HTTP field, appending if needed.
3618 http_add_field(http_t *http, /* I - HTTP connection */ in http_add_field() argument
3619 http_field_t field, /* I - HTTP field */ in http_add_field()
3664 else if (append && *value && (oldvalue = httpGetField(http, field)) != NULL && *oldvalue) in http_add_field()
3677 if (http->accept_encoding) in http_add_field()
3678 _cupsStrFree(http->accept_encoding); in http_add_field()
3680 http->accept_encoding = _cupsStrAlloc(value); in http_add_field()
3684 if (http->allow) in http_add_field()
3685 _cupsStrFree(http->allow); in http_add_field()
3687 http->allow = _cupsStrAlloc(value); in http_add_field()
3691 if (http->server) in http_add_field()
3692 _cupsStrFree(http->server); in http_add_field()
3694 http->server = _cupsStrAlloc(value); in http_add_field()
3698 if (http->authentication_info) in http_add_field()
3699 _cupsStrFree(http->authentication_info); in http_add_field()
3701 http->authentication_info = _cupsStrAlloc(value); in http_add_field()
3705 strlcpy(http->fields[field], value, HTTP_MAX_VALUE); in http_add_field()
3716 if (http->field_authorization) in http_add_field()
3717 free(http->field_authorization); in http_add_field()
3719 http->field_authorization = strdup(value); in http_add_field()
3723 http->data_encoding != HTTP_ENCODING_FIELDS) in http_add_field()
3726 http_content_coding_start(http, value); in http_add_field()
3739 http_t *http) /* I - HTTP connection */ in http_content_coding_finish() argument
3746 DEBUG_printf(("http_content_coding_finish(http=%p)", (void *)http)); in http_content_coding_finish()
3747 DEBUG_printf(("1http_content_coding_finishing: http->coding=%d", http->coding)); in http_content_coding_finish()
3749 switch (http->coding) in http_content_coding_finish()
3753 ((z_stream *)http->stream)->next_in = dummy; in http_content_coding_finish()
3754 ((z_stream *)http->stream)->avail_in = 0; in http_content_coding_finish()
3758 zerr = deflate((z_stream *)http->stream, Z_FINISH); in http_content_coding_finish()
3759 bytes = _HTTP_MAX_SBUFFER - ((z_stream *)http->stream)->avail_out; in http_content_coding_finish()
3765 if (http->data_encoding == HTTP_ENCODING_CHUNKED) in http_content_coding_finish()
3766 http_write_chunk(http, (char *)http->sbuffer, bytes); in http_content_coding_finish()
3768 http_write(http, (char *)http->sbuffer, bytes); in http_content_coding_finish()
3771 ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; in http_content_coding_finish()
3772 ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; in http_content_coding_finish()
3776 deflateEnd((z_stream *)http->stream); in http_content_coding_finish()
3778 free(http->sbuffer); in http_content_coding_finish()
3779 free(http->stream); in http_content_coding_finish()
3781 http->sbuffer = NULL; in http_content_coding_finish()
3782 http->stream = NULL; in http_content_coding_finish()
3784 if (http->wused) in http_content_coding_finish()
3785 httpFlushWrite(http); in http_content_coding_finish()
3790 inflateEnd((z_stream *)http->stream); in http_content_coding_finish()
3792 free(http->sbuffer); in http_content_coding_finish()
3793 free(http->stream); in http_content_coding_finish()
3795 http->sbuffer = NULL; in http_content_coding_finish()
3796 http->stream = NULL; in http_content_coding_finish()
3803 http->coding = _HTTP_CODING_IDENTITY; in http_content_coding_finish()
3813 http_t *http, /* I - HTTP connection */ in http_content_coding_start() argument
3820 DEBUG_printf(("http_content_coding_start(http=%p, value=\"%s\")", (void *)http, value)); in http_content_coding_start()
3822 if (http->coding != _HTTP_CODING_IDENTITY) in http_content_coding_start()
3824 DEBUG_printf(("1http_content_coding_start: http->coding already %d.", in http_content_coding_start()
3825 http->coding)); in http_content_coding_start()
3830 if (http->state == HTTP_STATE_GET_SEND || in http_content_coding_start()
3831 http->state == HTTP_STATE_POST_SEND) in http_content_coding_start()
3832 coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_GZIP : in http_content_coding_start()
3834 else if (http->state == HTTP_STATE_POST_RECV || in http_content_coding_start()
3835 http->state == HTTP_STATE_PUT_RECV) in http_content_coding_start()
3836 coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_GZIP : in http_content_coding_start()
3846 if (http->state == HTTP_STATE_GET_SEND || in http_content_coding_start()
3847 http->state == HTTP_STATE_POST_SEND) in http_content_coding_start()
3848 coding = http->mode == _HTTP_MODE_SERVER ? _HTTP_CODING_DEFLATE : in http_content_coding_start()
3850 else if (http->state == HTTP_STATE_POST_RECV || in http_content_coding_start()
3851 http->state == HTTP_STATE_PUT_RECV) in http_content_coding_start()
3852 coding = http->mode == _HTTP_MODE_CLIENT ? _HTTP_CODING_DEFLATE : in http_content_coding_start()
3870 if (http->wused) in http_content_coding_start()
3871 httpFlushWrite(http); in http_content_coding_start()
3873 if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL) in http_content_coding_start()
3875 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3876 http->error = errno; in http_content_coding_start()
3886 if ((http->stream = calloc(1, sizeof(z_stream))) == NULL) in http_content_coding_start()
3888 free(http->sbuffer); in http_content_coding_start()
3890 http->sbuffer = NULL; in http_content_coding_start()
3891 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3892 http->error = errno; in http_content_coding_start()
3896 …if ((zerr = deflateInit2((z_stream *)http->stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, coding == _H… in http_content_coding_start()
3898 free(http->sbuffer); in http_content_coding_start()
3899 free(http->stream); in http_content_coding_start()
3901 http->sbuffer = NULL; in http_content_coding_start()
3902 http->stream = NULL; in http_content_coding_start()
3903 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3904 http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL; in http_content_coding_start()
3908 ((z_stream *)http->stream)->next_out = (Bytef *)http->sbuffer; in http_content_coding_start()
3909 ((z_stream *)http->stream)->avail_out = (uInt)_HTTP_MAX_SBUFFER; in http_content_coding_start()
3914 if ((http->sbuffer = malloc(_HTTP_MAX_SBUFFER)) == NULL) in http_content_coding_start()
3916 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3917 http->error = errno; in http_content_coding_start()
3926 if ((http->stream = calloc(1, sizeof(z_stream))) == NULL) in http_content_coding_start()
3928 free(http->sbuffer); in http_content_coding_start()
3930 http->sbuffer = NULL; in http_content_coding_start()
3931 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3932 http->error = errno; in http_content_coding_start()
3936 …if ((zerr = inflateInit2((z_stream *)http->stream, coding == _HTTP_CODING_INFLATE ? -15 : 31)) < Z… in http_content_coding_start()
3938 free(http->sbuffer); in http_content_coding_start()
3939 free(http->stream); in http_content_coding_start()
3941 http->sbuffer = NULL; in http_content_coding_start()
3942 http->stream = NULL; in http_content_coding_start()
3943 http->status = HTTP_STATUS_ERROR; in http_content_coding_start()
3944 http->error = zerr == Z_MEM_ERROR ? ENOMEM : EINVAL; in http_content_coding_start()
3948 ((z_stream *)http->stream)->avail_in = 0; in http_content_coding_start()
3949 ((z_stream *)http->stream)->next_in = http->sbuffer; in http_content_coding_start()
3956 http->coding = coding; in http_content_coding_start()
3958 DEBUG_printf(("1http_content_coding_start: http->coding now %d.", in http_content_coding_start()
3959 http->coding)); in http_content_coding_start()
3965 * 'http_create()' - Create an unconnected HTTP connection.
3968 static http_t * /* O - HTTP connection */
3978 http_t *http; /* New HTTP connection */ in http_create() local
4012 if ((http = calloc(sizeof(http_t), 1)) == NULL) in http_create()
4020 * Initialize the HTTP data... in http_create()
4023 http->mode = mode; in http_create()
4024 http->activity = time(NULL); in http_create()
4025 http->addrlist = myaddrlist; in http_create()
4026 http->blocking = blocking; in http_create()
4027 http->fd = -1; in http_create()
4029 http->gssctx = GSS_C_NO_CONTEXT; in http_create()
4030 http->gssname = GSS_C_NO_NAME; in http_create()
4032 http->status = HTTP_STATUS_CONTINUE; in http_create()
4033 http->version = HTTP_VERSION_1_1; in http_create()
4036 strlcpy(http->hostname, host, sizeof(http->hostname)); in http_create()
4039 http->encryption = HTTP_ENCRYPTION_ALWAYS; in http_create()
4041 http->encryption = encryption; in http_create()
4043 http_set_wait(http); in http_create()
4049 return (http); in http_create()
4111 * 'http_read()' - Read a buffer from a HTTP connection.
4118 http_read(http_t *http, /* I - HTTP connection */ in http_read() argument
4125 …DEBUG_printf(("http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer… in http_read()
4127 if (!http->blocking || http->timeout_value > 0.0) in http_read()
4129 while (!httpWait(http, http->wait_value)) in http_read()
4131 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in http_read()
4144 if (http->tls) in http_read()
4145 bytes = _httpTLSRead(http, buffer, (int)length); in http_read()
4148 bytes = recv(http->fd, buffer, length, 0); in http_read()
4155 http->error = WSAGetLastError(); in http_read()
4160 if (!http->timeout_cb || in http_read()
4161 !(*http->timeout_cb)(http, http->timeout_data)) in http_read()
4163 http->error = WSAEWOULDBLOCK; in http_read()
4172 if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) in http_read()
4174 http->error = errno; in http_read()
4177 else if (!http->timeout_cb && errno != EAGAIN) in http_read()
4179 http->error = errno; in http_read()
4185 http->error = errno; in http_read()
4206 http->error = WSAGetLastError(); in http_read()
4208 if (errno == EINTR || (errno == EAGAIN && !http->timeout_cb)) in http_read()
4211 http->error = errno; in http_read()
4216 http->error = EPIPE; in http_read()
4225 * 'http_read_buffered()' - Do a buffered read from a HTTP connection.
4227 * This function reads data from the HTTP buffer or from the socket, as needed.
4231 http_read_buffered(http_t *http, /* I - HTTP connection */ in http_read_buffered() argument
4238 …f(("http_read_buffered(http=%p, buffer=%p, length=" CUPS_LLFMT ") used=%d", (void *)http, (void *)… in http_read_buffered()
4240 if (http->used > 0) in http_read_buffered()
4242 if (length > (size_t)http->used) in http_read_buffered()
4243 bytes = (ssize_t)http->used; in http_read_buffered()
4250 memcpy(buffer, http->buffer, (size_t)bytes); in http_read_buffered()
4251 http->used -= (int)bytes; in http_read_buffered()
4253 if (http->used > 0) in http_read_buffered()
4254 memmove(http->buffer, http->buffer + bytes, (size_t)http->used); in http_read_buffered()
4257 bytes = http_read(http, buffer, length); in http_read_buffered()
4264 * 'http_read_chunk()' - Read a chunk from a HTTP connection.
4271 http_read_chunk(http_t *http, /* I - HTTP connection */ in http_read_chunk() argument
4275 …DEBUG_printf(("http_read_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)… in http_read_chunk()
4277 if (http->data_remaining <= 0) in http_read_chunk()
4281 if (!httpGets(len, sizeof(len), http)) in http_read_chunk()
4290 if (!httpGets(len, sizeof(len), http)) in http_read_chunk()
4297 http->data_remaining = strtoll(len, NULL, 16); in http_read_chunk()
4299 if (http->data_remaining < 0) in http_read_chunk()
4302 CUPS_LLFMT ")", len, CUPS_LLCAST http->data_remaining)); in http_read_chunk()
4307 len, CUPS_LLCAST http->data_remaining)); in http_read_chunk()
4309 if (http->data_remaining == 0) in http_read_chunk()
4315 httpGets(len, sizeof(len), http); in http_read_chunk()
4320 CUPS_LLCAST http->data_remaining)); in http_read_chunk()
4322 if (http->data_remaining <= 0) in http_read_chunk()
4324 else if (length > (size_t)http->data_remaining) in http_read_chunk()
4325 length = (size_t)http->data_remaining; in http_read_chunk()
4327 return (http_read_buffered(http, buffer, length)); in http_read_chunk()
4336 http_send(http_t *http, /* I - HTTP connection */ in http_send() argument
4363 …DEBUG_printf(("4http_send(http=%p, request=HTTP_%s, uri=\"%s\")", (void *)http, codes[request], ur… in http_send()
4365 if (http == NULL || uri == NULL) in http_send()
4372 if (!http->fields[HTTP_FIELD_USER_AGENT][0]) in http_send()
4374 if (http->default_user_agent) in http_send()
4375 httpSetField(http, HTTP_FIELD_USER_AGENT, http->default_user_agent); in http_send()
4377 httpSetField(http, HTTP_FIELD_USER_AGENT, cupsUserAgent()); in http_send()
4384 if (!http->accept_encoding && http->default_accept_encoding) in http_send()
4385 httpSetField(http, HTTP_FIELD_ACCEPT_ENCODING, in http_send()
4386 http->default_accept_encoding); in http_send()
4398 if (http->fd < 0 || http->status == HTTP_STATUS_ERROR || in http_send()
4399 http->status >= HTTP_STATUS_BAD_REQUEST) in http_send()
4402 http->fd, http->status, http->tls_upgrade)); in http_send()
4404 if (httpReconnect2(http, 30000, NULL)) in http_send()
4412 if (http->wused) in http_send()
4414 if (httpFlushWrite(http) < 0) in http_send()
4415 if (httpReconnect2(http, 30000, NULL)) in http_send()
4423 http->state = request; in http_send()
4424 http->data_encoding = HTTP_ENCODING_FIELDS; in http_send()
4427 http->state ++; in http_send()
4429 http->status = HTTP_STATUS_CONTINUE; in http_send()
4432 if (http->encryption == HTTP_ENCRYPTION_REQUIRED && !http->tls) in http_send()
4434 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade"); in http_send()
4435 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); in http_send()
4439 if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1) in http_send()
4441 http->status = HTTP_STATUS_ERROR; in http_send()
4446 if ((value = httpGetField(http, i)) != NULL && *value) in http_send()
4452 if (httpPrintf(http, "Host: %s:%d\r\n", value, in http_send()
4453 httpAddrPort(http->hostaddr)) < 1) in http_send()
4455 http->status = HTTP_STATUS_ERROR; in http_send()
4459 else if (httpPrintf(http, "%s: %s\r\n", http_fields[i], value) < 1) in http_send()
4461 http->status = HTTP_STATUS_ERROR; in http_send()
4466 if (http->cookie) in http_send()
4467 if (httpPrintf(http, "Cookie: $Version=0; %s\r\n", http->cookie) < 1) in http_send()
4469 http->status = HTTP_STATUS_ERROR; in http_send()
4473 DEBUG_printf(("5http_send: expect=%d, mode=%d, state=%d", http->expect, in http_send()
4474 http->mode, http->state)); in http_send()
4476 if (http->expect == HTTP_STATUS_CONTINUE && http->mode == _HTTP_MODE_CLIENT && in http_send()
4477 (http->state == HTTP_STATE_POST_RECV || in http_send()
4478 http->state == HTTP_STATE_PUT_RECV)) in http_send()
4479 if (httpPrintf(http, "Expect: 100-continue\r\n") < 1) in http_send()
4481 http->status = HTTP_STATUS_ERROR; in http_send()
4485 if (httpPrintf(http, "\r\n") < 1) in http_send()
4487 http->status = HTTP_STATUS_ERROR; in http_send()
4491 if (httpFlushWrite(http) < 0) in http_send()
4494 http_set_length(http); in http_send()
4495 httpClearFields(http); in http_send()
4501 if (http->field_authorization && http->authstring && in http_send()
4502 (!strncmp(http->authstring, "Negotiate", 9) || in http_send()
4503 !strncmp(http->authstring, "AuthRef", 7))) in http_send()
4505 http->_authstring[0] = '\0'; in http_send()
4507 if (http->authstring != http->_authstring) in http_send()
4508 free(http->authstring); in http_send()
4510 http->authstring = http->_authstring; in http_send()
4522 http_set_length(http_t *http) /* I - Connection */ in http_set_length() argument
4527 …DEBUG_printf(("http_set_length(http=%p) mode=%d state=%s", (void *)http, http->mode, httpStateStri… in http_set_length()
4529 if ((remaining = httpGetLength2(http)) >= 0) in http_set_length()
4531 if (http->mode == _HTTP_MODE_SERVER && in http_set_length()
4532 http->state != HTTP_STATE_GET_SEND && in http_set_length()
4533 http->state != HTTP_STATE_PUT && in http_set_length()
4534 http->state != HTTP_STATE_POST && in http_set_length()
4535 http->state != HTTP_STATE_POST_SEND) in http_set_length()
4541 if (!_cups_strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], in http_set_length()
4546 http->data_encoding = HTTP_ENCODING_CHUNKED; in http_set_length()
4552 http->data_encoding = HTTP_ENCODING_LENGTH; in http_set_length()
4557 http->data_remaining = remaining; in http_set_length()
4560 http->_data_remaining = (int)remaining; in http_set_length()
4562 http->_data_remaining = INT_MAX; in http_set_length()
4600 http_set_wait(http_t *http) /* I - HTTP connection */ in http_set_wait() argument
4602 if (http->blocking) in http_set_wait()
4604 http->wait_value = (int)(http->timeout_value * 1000); in http_set_wait()
4606 if (http->wait_value <= 0) in http_set_wait()
4607 http->wait_value = 60000; in http_set_wait()
4610 http->wait_value = 10000; in http_set_wait()
4620 http_tls_upgrade(http_t *http) /* I - HTTP connection */ in http_tls_upgrade() argument
4623 http_t myhttp; /* Local copy of HTTP data */ in http_tls_upgrade()
4626 DEBUG_printf(("7http_tls_upgrade(%p)", (void *)http)); in http_tls_upgrade()
4633 httpFlush(http); in http_tls_upgrade()
4636 * Copy the HTTP data to a local variable so we can do the OPTIONS in http_tls_upgrade()
4640 memcpy(&myhttp, http, sizeof(myhttp)); in http_tls_upgrade()
4647 http->tls_upgrade = 1; in http_tls_upgrade()
4648 http->field_authorization = NULL; /* Don't free the auth string */ in http_tls_upgrade()
4650 httpClearFields(http); in http_tls_upgrade()
4651 httpSetField(http, HTTP_FIELD_CONNECTION, "upgrade"); in http_tls_upgrade()
4652 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.2,TLS/1.1,TLS/1.0"); in http_tls_upgrade()
4654 if ((ret = httpOptions(http, "*")) == 0) in http_tls_upgrade()
4660 while (httpUpdate(http) == HTTP_STATUS_CONTINUE); in http_tls_upgrade()
4664 * Restore the HTTP request data... in http_tls_upgrade()
4667 memcpy(http->fields, myhttp.fields, sizeof(http->fields)); in http_tls_upgrade()
4668 http->data_encoding = myhttp.data_encoding; in http_tls_upgrade()
4669 http->data_remaining = myhttp.data_remaining; in http_tls_upgrade()
4670 http->_data_remaining = myhttp._data_remaining; in http_tls_upgrade()
4671 http->expect = myhttp.expect; in http_tls_upgrade()
4672 http->field_authorization = myhttp.field_authorization; in http_tls_upgrade()
4673 http->digest_tries = myhttp.digest_tries; in http_tls_upgrade()
4674 http->tls_upgrade = 0; in http_tls_upgrade()
4680 if (!http->tls) in http_tls_upgrade()
4683 * Server does not support HTTP upgrade... in http_tls_upgrade()
4686 DEBUG_puts("8http_tls_upgrade: Server does not support HTTP upgrade!"); in http_tls_upgrade()
4689 httpAddrClose(NULL, http->fd); in http_tls_upgrade()
4691 http->fd = -1; in http_tls_upgrade()
4702 * 'http_write()' - Write a buffer to a HTTP connection.
4706 http_write(http_t *http, /* I - HTTP connection */ in http_write() argument
4714 …DEBUG_printf(("2http_write(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buff… in http_write()
4715 http->error = 0; in http_write()
4722 if (http->timeout_value > 0.0) in http_write()
4735 pfd.fd = http->fd; in http_write()
4738 while ((nfds = poll(&pfd, 1, http->wait_value)) < 0 && in http_write()
4746 FD_SET(http->fd, &output_set); in http_write()
4748 timeout.tv_sec = http->wait_value / 1000; in http_write()
4749 timeout.tv_usec = 1000 * (http->wait_value % 1000); in http_write()
4751 nfds = select(http->fd + 1, NULL, &output_set, NULL, &timeout); in http_write()
4763 http->error = errno; in http_write()
4766 else if (nfds == 0 && (!http->timeout_cb || !(*http->timeout_cb)(http, http->timeout_data))) in http_write()
4769 http->error = WSAEWOULDBLOCK; in http_write()
4771 http->error = EWOULDBLOCK; in http_write()
4780 if (http->tls) in http_write()
4781 bytes = _httpTLSWrite(http, buffer, (int)length); in http_write()
4784 bytes = send(http->fd, buffer, length, 0); in http_write()
4796 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in http_write()
4799 http->error = WSAGetLastError(); in http_write()
4801 else if (WSAGetLastError() != http->error && in http_write()
4804 http->error = WSAGetLastError(); in http_write()
4813 if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) in http_write()
4815 else if (!http->timeout_cb && errno == EAGAIN) in http_write()
4818 http->error = errno; in http_write()
4820 else if (errno != http->error && errno != ECONNRESET) in http_write()
4822 http->error = errno; in http_write()
4828 strerror(http->error))); in http_write()
4853 http_write_chunk(http_t *http, /* I - HTTP connection */ in http_write_chunk() argument
4861 …DEBUG_printf(("7http_write_chunk(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void … in http_write_chunk()
4868 if (http_write(http, header, strlen(header)) < 0) in http_write_chunk()
4874 if ((bytes = http_write(http, buffer, length)) < 0) in http_write_chunk()
4880 if (http_write(http, "\r\n", 2) < 0) in http_write_chunk()