OLD | NEW |
(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 "base/json/json_reader.h" |
| 8 #include "base/time/time.h" |
| 9 #include "base/values.h" |
| 10 #include "net/reporting/reporting_cache.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 // static |
| 15 void ReportingHeaderParser::ParseHeader(ReportingCache* cache, |
| 16 base::TimeTicks now, |
| 17 const GURL& url, |
| 18 const std::string& json_value) { |
| 19 if (!url.SchemeIsCryptographic()) |
| 20 return; |
| 21 |
| 22 std::unique_ptr<base::Value> value = |
| 23 base::JSONReader::Read("[" + json_value + "]"); |
| 24 if (!value) |
| 25 return; |
| 26 |
| 27 const base::ListValue* list = nullptr; |
| 28 bool is_list = value->GetAsList(&list); |
| 29 DCHECK(is_list && list); |
| 30 |
| 31 for (size_t i = 0; i < list->GetSize(); i++) { |
| 32 const base::Value* endpoint = nullptr; |
| 33 bool got_endpoint = list->Get(i, &endpoint); |
| 34 DCHECK(got_endpoint && endpoint); |
| 35 ProcessEndpoint(cache, now, url, *endpoint); |
| 36 } |
| 37 } |
| 38 |
| 39 // static |
| 40 void ReportingHeaderParser::ProcessEndpoint(ReportingCache* cache, |
| 41 base::TimeTicks now, |
| 42 const GURL& url, |
| 43 const base::Value& value) { |
| 44 const base::DictionaryValue* dict = nullptr; |
| 45 if (!value.GetAsDictionary(&dict)) |
| 46 return; |
| 47 DCHECK(dict); |
| 48 |
| 49 std::string endpoint_url_string; |
| 50 if (!dict->GetString("url", &endpoint_url_string)) |
| 51 return; |
| 52 |
| 53 GURL endpoint_url(endpoint_url_string); |
| 54 if (!endpoint_url.is_valid()) |
| 55 return; |
| 56 if (!endpoint_url.SchemeIsCryptographic()) |
| 57 return; |
| 58 |
| 59 bool subdomains = false; |
| 60 if (dict->HasKey("includeSubdomains") && |
| 61 !dict->GetBoolean("includeSubdomains", &subdomains)) { |
| 62 return; |
| 63 } |
| 64 |
| 65 std::string group = "default"; |
| 66 if (dict->HasKey("group") && !dict->GetString("group", &group)) |
| 67 return; |
| 68 |
| 69 int ttl_sec = -1; |
| 70 if (!dict->GetInteger("max-age", &ttl_sec) || ttl_sec < 0) |
| 71 return; |
| 72 |
| 73 if (ttl_sec > 0) { |
| 74 cache->SetClient(url::Origin(url), endpoint_url, subdomains, group, |
| 75 now + base::TimeDelta::FromSeconds(ttl_sec)); |
| 76 } else { |
| 77 cache->RemoveClientForOriginAndEndpoint(url::Origin(url), endpoint_url); |
| 78 } |
| 79 } |
| 80 |
| 81 } // namespace net |
OLD | NEW |