| 191 | } |
| 192 | |
| 193 | bool ReceiveResponse(HttpResponse* response) |
| 194 | { |
| 195 | std::unique_ptr<char[]> buffer(new char[m_max_response_length]); |
| 196 | char *buff = buffer.get(); |
| 197 | |
| 198 | size_t total_received = 0; |
| 199 | size_t received_length; |
| 200 | size_t buffer_length = m_max_response_length - 1; |
| 201 | char *p = NULL; |
| 202 | // handle headers first. |
| 203 | do { |
| 204 | if (!m_connector.Receive(buff + total_received, |
| 205 | buffer_length, |
| 206 | &received_length)) { |
| 207 | m_error_code = HttpClient::ERROR_FAIL_TO_GET_RESPONSE; |
| 208 | return false; |
| 209 | } else if (received_length == 0) { |
| 210 | m_error_code = HttpClient::ERROR_FAIL_TO_GET_RESPONSE; |
| 211 | VLOG(4) << "The peer reset the network connection."; |
| 212 | return false; |
| 213 | } |
| 214 | total_received += received_length; |
| 215 | buffer_length -= received_length; |
| 216 | |
| 217 | buff[total_received] = 0; |
| 218 | |
| 219 | p = strstr(buff, "\r\n\r\n"); |
| 220 | } while (p == NULL && buffer_length > 0); |
| 221 | |
| 222 | if (p == NULL) { |
| 223 | m_error_code = HttpClient::ERROR_INVALID_RESPONSE_HEADER; |
| 224 | return false; |
| 225 | } |
| 226 | p += 4; |
| 227 | |
| 228 | StringPiece piece(buff, p - buff); |
| 229 | HttpMessage::ErrorCode message_error; |
| 230 | if (!m_response.ParseHeaders(piece, &message_error)) { |
| 231 | m_error_code = HttpClient::ERROR_INVALID_RESPONSE_HEADER; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | char* current = buff + total_received; |
| 236 | char* end = current + buffer_length; // end of buffer for body |
| 237 | if (!ResponseStatusHasContent(m_response.Status())) { |
| 238 | // no content |
| 239 | } else if (m_response.HasHeader("Transfer-Encoding") |
| 240 | && m_response.GetHeader("Transfer-Encoding") != "identity") { |
| 241 | // chunked content |
| 242 | ReceiveBodyWithChunks(p, end, current); |
| 243 | } else if (m_response.HasHeader("Content-Length")) { |
| 244 | // Content-Length field is given |
| 245 | ReceiveBodyWithContentLength(p, end, current); |
| 246 | } else if (m_response.HasHeader("Content-Type") && |
| 247 | m_response.GetHeader("Content-Type") == "multipart/byteranges") { |
| 248 | // not supported yet |
| 249 | m_error_code = HttpClient::ERROR_CONTENT_TYPE_NOT_SUPPORTED; |
| 250 | return false; |
nothing calls this directly
no test coverage detected