Skip to content

Fix issues #110 and #168. #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2013
Merged
Changes from all commits
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
51 changes: 49 additions & 2 deletions http/src/network/protocol/http/response/response.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
#ifndef NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206
#define NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206

#include <boost/algorithm/string/predicate.hpp>
#include <network/protocol/http/response/response.hpp>

#include <algorithm>
#include <set>
#include <sstream>

namespace network {
namespace http {
Expand Down Expand Up @@ -91,7 +95,23 @@ struct response_pimpl {
void get_headers(
std::string const& name,
std::function<void(std::string const&, std::string const&)> inserter) {
/* FIXME: Do something! */
if (removed_headers_.find(name) != removed_headers_.end())
return;

std::pair<std::multimap<std::string, std::string>::const_iterator,
std::multimap<std::string, std::string>::const_iterator>
range;
if (!headers_future_.valid()) {
range = added_headers_.equal_range(name);
} else {
std::multimap<std::string, std::string> const& headers_ =
headers_future_.get();
range = headers_.equal_range(name);
}

for (auto it = range.first; it != range.second; ++it) {
inserter(it->first, it->second);
}
}
void get_headers(
std::function<bool(std::string const&, std::string const&)> predicate,
Expand All @@ -113,7 +133,34 @@ struct response_pimpl {
if (!body_future_.valid()) {
body = "";
} else {
body = body_future_.get();
std::string partial_parsed = body_future_.get();
bool chunked = false;
auto check = [&](std::string const& key, std::string const& value) {
chunked = chunked || boost::iequals(value, "chunked");
};
get_headers(std::string("Transfer-Encoding"), check);
if (chunked) {
auto begin = partial_parsed.begin();
std::string crlf = "\r\n";
for (auto 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())) {
std::string 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);
}
}
}

Expand Down