Skip to content

Commit d00836d

Browse files
committed
Take content length into account when reading body
1 parent e1aad6e commit d00836d

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

include/network/protocol/http/client/connection/async_normal.ipp

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,53 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
430430
placeholders::bytes_transferred)));
431431
} else {
432432
NETWORK_MESSAGE("no callback provided, appending to body...");
433+
bool get_more = true;
434+
buffer_type::const_iterator begin = this->part.begin();
435+
buffer_type::const_iterator end = begin;
436+
std::advance(end, bytes_transferred);
437+
// check the content length header
438+
//auto headers_future = headers_promise.get_future();
439+
auto it = headers_.find("Content-Length");
440+
if (it != headers_.end()) {
441+
try {
442+
unsigned content_length = stoi(it->second);
443+
get_more = (end - begin) < content_length;
444+
NETWORK_MESSAGE("Content-Length: " << content_length
445+
<< ", disconnect: " << !get_more);
446+
} catch(...) {
447+
}
448+
}
433449
// Here we don't have a body callback. Let's
434450
// make sure that we deal with the remainder
435451
// from the headers part in case we do have data
436452
// that's still in the buffer.
437-
this->parse_body(request_strand_.wrap(
438-
boost::bind(
439-
&this_type::handle_received_data,
440-
this_type::shared_from_this(),
441-
body,
442-
get_body,
443-
callback,
444-
placeholders::error,
445-
placeholders::bytes_transferred)),
446-
bytes_transferred);
453+
if (get_more) {
454+
this->parse_body(request_strand_.wrap(
455+
boost::bind(
456+
&this_type::handle_received_data,
457+
this_type::shared_from_this(),
458+
body,
459+
get_body,
460+
callback,
461+
placeholders::error,
462+
placeholders::bytes_transferred)),
463+
bytes_transferred);
464+
} else {
465+
std::string body_string;
466+
std::swap(body_string, this->partial_parsed);
467+
body_string.append(
468+
this->part.begin()
469+
, bytes_transferred
470+
);
471+
this->body_promise.set_value(body_string);
472+
// TODO set the destination value somewhere!
473+
this->destination_promise.set_value("");
474+
this->source_promise.set_value("");
475+
this->part.assign('\0');
476+
this->response_parser_.reset();
477+
//NETWORK_MESSAGE("forcing socket disconnect on content length");
478+
//connection_delegate_->disconnect();
479+
}
447480
}
448481
}
449482
return;
@@ -709,6 +742,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
709742
boost::trim(header_pair.second);
710743
headers.insert(header_pair);
711744
}
745+
this->headers_ = headers;
712746
headers_promise.set_value(headers);
713747
}
714748

@@ -790,6 +824,7 @@ struct http_async_connection_pimpl : boost::enable_shared_from_this<http_async_c
790824
boost::promise<boost::uint16_t> status_promise;
791825
boost::promise<std::string> status_message_promise;
792826
boost::promise<std::multimap<std::string, std::string> > headers_promise;
827+
std::multimap<std::string, std::string> headers_;
793828
boost::promise<std::string> source_promise;
794829
boost::promise<std::string> destination_promise;
795830
boost::promise<std::string> body_promise;

0 commit comments

Comments
 (0)