Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor chunked encoding body parsing.
  • Loading branch information
skystrife committed Feb 27, 2013
commit 30be91da242368ff1c7eff89b1c4d011269c3f35
33 changes: 32 additions & 1 deletion boost/network/protocol/http/client/connection/async_normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ namespace boost { namespace network { namespace http { namespace impl {
this->part.begin()
, bytes_transferred
);
this->body_promise.set_value(body_string);
if (this->is_chunk_encoding)
this->body_promise.set_value(parse_chunk_encoding(body_string));
else
this->body_promise.set_value(body_string);
}
// TODO set the destination value somewhere!
this->destination_promise.set_value("");
Expand Down Expand Up @@ -428,6 +431,34 @@ namespace boost { namespace network { namespace http { namespace impl {
}
}
}

string_type parse_chunk_encoding( string_type & body_string ) {
string_type body;
string_type crlf = "\r\n";

typename string_type::iterator begin = body_string.begin();
for (typename string_type::iterator iter =
std::search(begin, body_string.end(), crlf.begin(), crlf.end());
iter != body_string.end();
iter = std::search(begin, body_string.end(), crlf.begin(), crlf.end())) {
string_type line(begin, iter);
if (line.empty())
break;
std::stringstream stream(line);
int len;
stream >> std::hex >> len;
std::advance(iter, 2);
if (!len)
break;
if (len <= body_string.end() - iter) {
body.insert(body.end(), iter, iter + len);
std::advance(iter, len);
}
begin = iter;
}

return body;
}

bool follow_redirect_;
resolver_type & resolver_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ namespace boost { namespace network { namespace http { namespace impl {
trim(header_pair.second);
headers.insert(header_pair);
}
// determine if the body parser will need to handle chunked encoding
typename headers_range<basic_response<Tag> >::type transfer_encoding_range =
headers.equal_range("Transfer-Encoding");
is_chunk_encoding = !empty(transfer_encoding_range)
&& boost::iequals(boost::begin(transfer_encoding_range)->second, "chunked");
headers_promise.set_value(headers);
}

Expand Down Expand Up @@ -373,6 +378,7 @@ namespace boost { namespace network { namespace http { namespace impl {
buffer_type part;
typename buffer_type::const_iterator part_begin;
string_type partial_parsed;
bool is_chunk_encoding;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable seems unnecessary. You only ever need to check this just before writing it into the message body once.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but without it I'm not sure how I'd get the header data to check if it's chunked encoding just before writing the message body. At that point, I have no reference to the response object, so I can't get the value stored in that future (and if I get a future from the promise that I do have access to, I cause issues because there can only be one future for any given promise).

I suppose I could try to look for that value as we're building up the buffer to parse the headers out with, but that felt more hacky than doing it this way, where I definitely have access to the headers that are about to be put into the promise.

Maybe I'm overlooking a better way, though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, you're right.

};


Expand Down
29 changes: 1 addition & 28 deletions boost/network/protocol/http/message/async_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,7 @@ namespace boost { namespace network { namespace http {
}

string_type const body() const {
string_type body;
string_type partial_parsed = body_.get();

typename headers_range<basic_response<Tag> >::type transfer_encoding_range = headers().equal_range("Transfer-Encoding");
if (!empty(transfer_encoding_range) && boost::iequals(boost::begin(transfer_encoding_range)->second, "chunked")) {
typename string_type::iterator begin = partial_parsed.begin();
string_type crlf = "\r\n";
for (typename string_type::iterator iter = std::search(begin, partial_parsed.end(), crlf.begin(), crlf.end());
iter != partial_parsed.end();
iter = std::search(begin, partial_parsed.end(), crlf.begin(), crlf.end())) {
string_type line(begin, iter);
if (line.empty()) break;
std::stringstream stream(line);
int len;
stream >> std::hex >> len;
iter += 2;
if (!len) break;
if (len <= partial_parsed.end() - iter) {
body.insert(body.end(), iter, iter + len);
iter += len;
}
begin = iter;
}
} else {
std::swap(body, partial_parsed);
}

return body;
return body_.get();
}

void body(boost::shared_future<string_type> const & future) const {
Expand Down