For content data in chunks
| 287 | |
| 288 | // For content data in chunks |
| 289 | void ReceiveBodyWithChunks(char *begin, char *end, char* current) |
| 290 | { |
| 291 | std::string body; |
| 292 | body.reserve(m_max_response_length); |
| 293 | |
| 294 | int buffer_length = end - current; |
| 295 | while (begin < end) { |
| 296 | size_t received = 0; |
| 297 | |
| 298 | *current = 0; // For the following strstr. |
| 299 | char *p = strstr(begin, "\r\n"); |
| 300 | if (p != NULL) { |
| 301 | int chunk_size = 0; |
| 302 | if (sscanf(begin, "%x", &chunk_size) != 1) { // NOLINT(runtime/printf) |
| 303 | m_error_code = HttpClient::ERROR_FAIL_TO_READ_CHUNKSIZE; |
| 304 | return; |
| 305 | } |
| 306 | begin = p + 2; |
| 307 | if (chunk_size == 0) { |
| 308 | // finish |
| 309 | m_response.MutableBody()->swap(body); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | chunk_size += 2; // "\r\n" is appended to the end of chunk |
| 314 | int downloaded = current - begin; |
| 315 | chunk_size = std::min(chunk_size, buffer_length); |
| 316 | // if the downloaded content is not enough, download more. |
| 317 | if (downloaded < chunk_size) { |
| 318 | size_t length = chunk_size - downloaded; |
| 319 | if (!m_connector.ReceiveAll(current, length, &received)) { |
| 320 | m_error_code = HttpClient::ERROR_FAIL_TO_GET_RESPONSE; |
| 321 | return; |
| 322 | } |
| 323 | current += received; |
| 324 | buffer_length -= received; |
| 325 | } |
| 326 | // remove this "\r\n" |
| 327 | body.append(begin, chunk_size - 2); |
| 328 | begin += chunk_size; |
| 329 | } else { |
| 330 | // there is not enough content to get a whole CHUNK header |
| 331 | // download more data. |
| 332 | if (!m_connector.Receive(current, buffer_length, &received)) { |
| 333 | m_error_code = HttpClient::ERROR_FAIL_TO_GET_RESPONSE; |
| 334 | return; |
| 335 | } |
| 336 | current += received; |
| 337 | buffer_length -= received; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Old HTTP servers will close connection after send response package |
| 343 | void ReceiveBodyWithConnectionReset(char *begin, char *end, char* current) |
nothing calls this directly
no test coverage detected