| 1253 | } |
| 1254 | |
| 1255 | void _read(const Future<string>& data) |
| 1256 | { |
| 1257 | deque<Response*> responses; |
| 1258 | |
| 1259 | if (!data.isReady() || data->empty()) { |
| 1260 | // Process EOF. Also send EOF to the decoder if a failure |
| 1261 | // or discard is encountered. |
| 1262 | responses = decoder.decode("", 0); |
| 1263 | } else { |
| 1264 | // We should only receive data if we're expecting a response |
| 1265 | // in the pipeline, or if a response body is still streaming. |
| 1266 | if (pipeline.empty() && !decoder.writingBody()) { |
| 1267 | disconnect("Received data when none is expected"); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | responses = decoder.decode(data->data(), data->length()); |
| 1272 | } |
| 1273 | |
| 1274 | // Process any decoded responses. |
| 1275 | while (!responses.empty()) { |
| 1276 | // We do not expect any responses when the pipeline is empty. |
| 1277 | // Note that this may occur when a 'Connection: close' header |
| 1278 | // prematurely terminates the pipeline. |
| 1279 | if (pipeline.empty()) { |
| 1280 | while (!responses.empty()) { |
| 1281 | delete responses.front(); |
| 1282 | responses.pop_front(); |
| 1283 | } |
| 1284 | |
| 1285 | disconnect("Received response without a request"); |
| 1286 | return; |
| 1287 | } |
| 1288 | |
| 1289 | Response* response = responses.front(); |
| 1290 | responses.pop_front(); |
| 1291 | |
| 1292 | tuple<bool, Promise<Response>> t = std::move(pipeline.front()); |
| 1293 | pipeline.pop(); |
| 1294 | |
| 1295 | bool streamedResponse = std::get<0>(t); |
| 1296 | Promise<Response> promise = std::move(std::get<1>(t)); |
| 1297 | |
| 1298 | if (streamedResponse) { |
| 1299 | promise.set(*response); |
| 1300 | } else { |
| 1301 | // If the response should not be streamed, we convert |
| 1302 | // the PIPE response into a BODY response. |
| 1303 | promise.associate(convert(*response)); |
| 1304 | } |
| 1305 | |
| 1306 | if (response->headers.contains("Connection") && |
| 1307 | response->headers.at("Connection") == "close") { |
| 1308 | // This is the last response the server will send! |
| 1309 | close = true; |
| 1310 | |
| 1311 | // Fail the remainder of the pipeline. |
| 1312 | while (!pipeline.empty()) { |