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

Side by Side Diff: net/reporting/reporting_header_parser.cc

Issue 2689953004: Reporting: Implement header parser. (Closed)
Patch Set: Make requested changes. Created 3 years, 8 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/reporting/reporting_header_parser.h"
6
7 #include <string>
8
9 #include "base/json/json_reader.h"
10 #include "base/logging.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "net/reporting/reporting_cache.h"
14 #include "url/gurl.h"
15 #include "url/origin.h"
16
17 namespace net {
18
19 namespace {
20
21 const char kUrlKey[] = "url";
22 const char kIncludeSubdomainsKey[] = "includeSubdomains";
23 const char kGroupKey[] = "group";
24 const char kGroupDefaultValue[] = "default";
25 const char kMaxAgeKey[] = "max-age";
26
27 } // namespace
28
29 // static
30 void ReportingHeaderParser::ParseHeader(ReportingCache* cache,
31 base::TimeTicks now,
32 const GURL& url,
33 const std::string& json_value) {
34 DCHECK(url.SchemeIsCryptographic());
35
36 std::unique_ptr<base::Value> value =
37 base::JSONReader::Read("[" + json_value + "]");
38 if (!value)
39 return;
40
41 const base::ListValue* list = nullptr;
42 bool is_list = value->GetAsList(&list);
43 DCHECK(is_list);
44
45 for (size_t i = 0; i < list->GetSize(); i++) {
46 const base::Value* endpoint = nullptr;
47 bool got_endpoint = list->Get(i, &endpoint);
48 DCHECK(got_endpoint);
49 ProcessEndpoint(cache, now, url, *endpoint);
50 }
51 }
52
53 // static
54 void ReportingHeaderParser::ProcessEndpoint(ReportingCache* cache,
55 base::TimeTicks now,
56 const GURL& url,
57 const base::Value& value) {
58 const base::DictionaryValue* dict = nullptr;
59 if (!value.GetAsDictionary(&dict))
60 return;
61 DCHECK(dict);
62
63 std::string endpoint_url_string;
64 if (!dict->GetString(kUrlKey, &endpoint_url_string))
65 return;
66
67 GURL endpoint_url(endpoint_url_string);
68 if (!endpoint_url.is_valid())
69 return;
70 if (!endpoint_url.SchemeIsCryptographic())
71 return;
72
73 ReportingClient::Subdomains subdomains = ReportingClient::Subdomains::EXCLUDE;
74 bool subdomains_bool = false;
75 if (dict->HasKey(kIncludeSubdomainsKey) &&
76 dict->GetBoolean(kIncludeSubdomainsKey, &subdomains_bool) &&
77 subdomains_bool == true) {
78 subdomains = ReportingClient::Subdomains::INCLUDE;
79 }
80
81 std::string group = kGroupDefaultValue;
82 if (dict->HasKey(kGroupKey) && !dict->GetString(kGroupKey, &group))
83 return;
84
85 int ttl_sec = -1;
86 if (!dict->GetInteger(kMaxAgeKey, &ttl_sec) || ttl_sec < 0)
jkarlin 2017/03/29 11:30:05 nit: perhaps move this up to just after parsing th
Julia Tuttle 2017/03/29 16:14:53 Done.
87 return;
88
89 if (ttl_sec > 0) {
90 cache->SetClient(url::Origin(url), endpoint_url, subdomains, group,
91 now + base::TimeDelta::FromSeconds(ttl_sec));
92 } else {
93 cache->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url);
94 }
95 }
96
97 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698