This source file includes following definitions.
- EncodeHeaderSet
- CookieToCrumbs
#include "net/spdy/hpack_encoder.h"
#include "base/strings/string_util.h"
#include "net/spdy/hpack_output_stream.h"
namespace net {
using base::StringPiece;
using std::string;
HpackEncoder::HpackEncoder()
: max_string_literal_size_(kDefaultMaxStringLiteralSize) {}
HpackEncoder::~HpackEncoder() {}
bool HpackEncoder::EncodeHeaderSet(const std::map<string, string>& header_set,
string* output) {
HpackOutputStream output_stream(max_string_literal_size_);
for (std::map<string, string>::const_iterator it = header_set.begin();
it != header_set.end(); ++it) {
if (LowerCaseEqualsASCII(it->first, "cookie")) {
std::vector<StringPiece> crumbs;
CookieToCrumbs(it->second, &crumbs);
for (size_t i = 0; i != crumbs.size(); i++) {
if (!output_stream.AppendLiteralHeaderNoIndexingWithName(
it->first, crumbs[i])) {
return false;
}
}
} else if (!output_stream.AppendLiteralHeaderNoIndexingWithName(
it->first, it->second)) {
return false;
}
}
output_stream.TakeString(output);
return true;
}
void HpackEncoder::CookieToCrumbs(StringPiece cookie,
std::vector<StringPiece>* out) {
out->clear();
for (size_t pos = 0;;) {
size_t end = cookie.find(';', pos);
if (end == StringPiece::npos) {
out->push_back(cookie.substr(pos));
return;
}
out->push_back(cookie.substr(pos, end - pos));
pos = end + 1;
if (pos != cookie.size() && cookie[pos] == ' ') {
pos++;
}
}
}
}