Skip to content

Commit bd90c39

Browse files
committed
1 parent 5ac69cb commit bd90c39

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

http/src/network/protocol/http/response/response.ipp

+49-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
#ifndef NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206
88
#define NETWORK_PROTOCOL_HTTP_RESPONSE_RESPONSE_IPP_20111206
99

10+
#include <boost/algorithm/string/predicate.hpp>
1011
#include <network/protocol/http/response/response.hpp>
12+
13+
#include <algorithm>
1114
#include <set>
15+
#include <sstream>
1216

1317
namespace network {
1418
namespace http {
@@ -91,7 +95,23 @@ struct response_pimpl {
9195
void get_headers(
9296
std::string const& name,
9397
std::function<void(std::string const&, std::string const&)> inserter) {
94-
/* FIXME: Do something! */
98+
if (removed_headers_.find(name) != removed_headers_.end())
99+
return;
100+
101+
std::pair<std::multimap<std::string, std::string>::const_iterator,
102+
std::multimap<std::string, std::string>::const_iterator>
103+
range;
104+
if (!headers_future_.valid()) {
105+
range = added_headers_.equal_range(name);
106+
} else {
107+
std::multimap<std::string, std::string> const& headers_ =
108+
headers_future_.get();
109+
range = headers_.equal_range(name);
110+
}
111+
112+
for (auto it = range.first; it != range.second; ++it) {
113+
inserter(it->first, it->second);
114+
}
95115
}
96116
void get_headers(
97117
std::function<bool(std::string const&, std::string const&)> predicate,
@@ -113,7 +133,34 @@ struct response_pimpl {
113133
if (!body_future_.valid()) {
114134
body = "";
115135
} else {
116-
body = body_future_.get();
136+
std::string partial_parsed = body_future_.get();
137+
bool chunked = false;
138+
auto check = [&](std::string const& key, std::string const& value) {
139+
chunked = chunked || boost::iequals(value, "chunked");
140+
};
141+
get_headers(std::string("Transfer-Encoding"), check);
142+
if (chunked) {
143+
auto begin = partial_parsed.begin();
144+
std::string crlf = "\r\n";
145+
for (auto iter = std::search(begin,partial_parsed.end(), crlf.begin(), crlf.end());
146+
iter != partial_parsed.end();
147+
iter = std::search(begin,partial_parsed.end(), crlf.begin(), crlf.end())) {
148+
std::string line(begin, iter);
149+
if (line.empty()) break;
150+
std::stringstream stream(line);
151+
int len;
152+
stream >> std::hex >> len;
153+
iter += 2;
154+
if (!len) break;
155+
if (len <= partial_parsed.end() - iter) {
156+
body.insert(body.end(), iter, iter + len);
157+
iter += len;
158+
}
159+
begin = iter;
160+
}
161+
} else {
162+
std::swap(body, partial_parsed);
163+
}
117164
}
118165
}
119166

0 commit comments

Comments
 (0)