This source file includes following definitions.
- Init
- InitFromPickle
- Persist
- MatchesRequest
- GetRequestValue
- AddField
#include "net/http/http_vary_data.h"
#include <stdlib.h>
#include "base/pickle.h"
#include "base/strings/string_util.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_request_info.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
namespace net {
HttpVaryData::HttpVaryData() : is_valid_(false) {
}
bool HttpVaryData::Init(const HttpRequestInfo& request_info,
const HttpResponseHeaders& response_headers) {
base::MD5Context ctx;
base::MD5Init(&ctx);
is_valid_ = false;
bool processed_header = false;
void* iter = NULL;
std::string name = "vary", request_header;
while (response_headers.EnumerateHeader(&iter, name, &request_header)) {
if (request_header == "*")
return false;
AddField(request_info, request_header, &ctx);
processed_header = true;
}
std::string location;
if (response_headers.IsRedirect(&location)) {
AddField(request_info, "cookie", &ctx);
processed_header = true;
}
if (!processed_header)
return false;
base::MD5Final(&request_digest_, &ctx);
return is_valid_ = true;
}
bool HttpVaryData::InitFromPickle(const Pickle& pickle, PickleIterator* iter) {
is_valid_ = false;
const char* data;
if (pickle.ReadBytes(iter, &data, sizeof(request_digest_))) {
memcpy(&request_digest_, data, sizeof(request_digest_));
return is_valid_ = true;
}
return false;
}
void HttpVaryData::Persist(Pickle* pickle) const {
DCHECK(is_valid());
pickle->WriteBytes(&request_digest_, sizeof(request_digest_));
}
bool HttpVaryData::MatchesRequest(
const HttpRequestInfo& request_info,
const HttpResponseHeaders& cached_response_headers) const {
HttpVaryData new_vary_data;
if (!new_vary_data.Init(request_info, cached_response_headers)) {
NOTREACHED();
return false;
}
return memcmp(&new_vary_data.request_digest_, &request_digest_,
sizeof(request_digest_)) == 0;
}
std::string HttpVaryData::GetRequestValue(
const HttpRequestInfo& request_info,
const std::string& request_header) {
std::string result;
if (request_info.extra_headers.GetHeader(request_header, &result))
return result;
return std::string();
}
void HttpVaryData::AddField(const HttpRequestInfo& request_info,
const std::string& request_header,
base::MD5Context* ctx) {
std::string request_value = GetRequestValue(request_info, request_header);
request_value.append(1, '\n');
base::MD5Update(ctx, request_value);
}
}