Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Unified Diff: net/reporting/reporting_header_parser.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: Extract header parser and make static Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/reporting/reporting_header_parser.cc
diff --git a/net/reporting/reporting_header_parser.cc b/net/reporting/reporting_header_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bd9c51833260843e8a0c24eee1ae190fdfd2288f
--- /dev/null
+++ b/net/reporting/reporting_header_parser.cc
@@ -0,0 +1,81 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/reporting/reporting_header_parser.h"
+
+#include "base/json/json_reader.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "net/reporting/reporting_cache.h"
+
+namespace net {
+
+// static
+void ReportingHeaderParser::ParseHeader(ReportingCache* cache,
+ base::TimeTicks now,
+ const GURL& url,
+ const std::string& json_value) {
+ if (!url.SchemeIsCryptographic())
+ return;
+
+ std::unique_ptr<base::Value> value =
+ base::JSONReader::Read("[" + json_value + "]");
+ if (!value)
+ return;
+
+ const base::ListValue* list = nullptr;
+ bool is_list = value->GetAsList(&list);
+ DCHECK(is_list && list);
+
+ for (size_t i = 0; i < list->GetSize(); i++) {
+ const base::Value* endpoint = nullptr;
+ bool got_endpoint = list->Get(i, &endpoint);
+ DCHECK(got_endpoint && endpoint);
+ ProcessEndpoint(cache, now, url, *endpoint);
+ }
+}
+
+// static
+void ReportingHeaderParser::ProcessEndpoint(ReportingCache* cache,
+ base::TimeTicks now,
+ const GURL& url,
+ const base::Value& value) {
+ const base::DictionaryValue* dict = nullptr;
+ if (!value.GetAsDictionary(&dict))
+ return;
+ DCHECK(dict);
+
+ std::string endpoint_url_string;
+ if (!dict->GetString("url", &endpoint_url_string))
+ return;
+
+ GURL endpoint_url(endpoint_url_string);
+ if (!endpoint_url.is_valid())
+ return;
+ if (!endpoint_url.SchemeIsCryptographic())
+ return;
+
+ bool subdomains = false;
+ if (dict->HasKey("includeSubdomains") &&
+ !dict->GetBoolean("includeSubdomains", &subdomains)) {
+ return;
+ }
+
+ std::string group = "default";
+ if (dict->HasKey("group") && !dict->GetString("group", &group))
+ return;
+
+ int ttl_sec = -1;
+ if (!dict->GetInteger("max-age", &ttl_sec) || ttl_sec < 0)
+ return;
+
+ if (ttl_sec > 0) {
+ cache->SetClient(url::Origin(url), endpoint_url, subdomains, group,
+ now + base::TimeDelta::FromSeconds(ttl_sec));
+ } else {
+ cache->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url);
+ }
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698