| 4161 | */ |
| 4162 | |
| 4163 | static ssize_t /* O - Number of bytes read or -1 on error */ |
| 4164 | http_read(http_t *http, /* I - HTTP connection */ |
| 4165 | char *buffer, /* I - Buffer */ |
| 4166 | size_t length, /* I - Maximum bytes to read */ |
| 4167 | int timeout) /* I - Wait timeout */ |
| 4168 | { |
| 4169 | ssize_t bytes; /* Bytes read */ |
| 4170 | |
| 4171 | |
| 4172 | DEBUG_printf(("7http_read(http=%p, buffer=%p, length=" CUPS_LLFMT ")", (void *)http, (void *)buffer, CUPS_LLCAST length)); |
| 4173 | |
| 4174 | if (!http->blocking || http->timeout_value > 0.0) |
| 4175 | { |
| 4176 | while (!_httpWait(http, timeout, 1)) |
| 4177 | { |
| 4178 | if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data)) |
| 4179 | continue; |
| 4180 | |
| 4181 | DEBUG_puts("8http_read: Timeout."); |
| 4182 | return (0); |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | DEBUG_printf(("8http_read: Reading %d bytes into buffer.", (int)length)); |
| 4187 | |
| 4188 | do |
| 4189 | { |
| 4190 | #ifdef HAVE_TLS |
| 4191 | if (http->tls) |
| 4192 | bytes = _httpTLSRead(http, buffer, (int)length); |
| 4193 | else |
| 4194 | #endif /* HAVE_TLS */ |
| 4195 | bytes = recv(http->fd, buffer, length, 0); |
| 4196 | |
| 4197 | if (bytes < 0) |
| 4198 | { |
| 4199 | #ifdef _WIN32 |
| 4200 | if (WSAGetLastError() != WSAEINTR) |
| 4201 | { |
| 4202 | http->error = WSAGetLastError(); |
| 4203 | return (-1); |
| 4204 | } |
| 4205 | else if (WSAGetLastError() == WSAEWOULDBLOCK) |
| 4206 | { |
| 4207 | if (!http->timeout_cb || |
| 4208 | !(*http->timeout_cb)(http, http->timeout_data)) |
| 4209 | { |
| 4210 | http->error = WSAEWOULDBLOCK; |
| 4211 | return (-1); |
| 4212 | } |
| 4213 | } |
| 4214 | #else |
| 4215 | DEBUG_printf(("8http_read: %s", strerror(errno))); |
| 4216 | |
| 4217 | if (errno == EWOULDBLOCK || errno == EAGAIN) |
| 4218 | { |
| 4219 | if (http->timeout_cb && !(*http->timeout_cb)(http, http->timeout_data)) |
| 4220 | { |
no test coverage detected