File: header.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (319 lines) | stat: -rw-r--r-- 8,804 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2015 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 "components/domain_reliability/header.h"

#include <stdint.h>

#include <string>
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "components/domain_reliability/config.h"
#include "content/public/common/origin_util.h"

namespace {

// Parses directives in the format ("foo; bar=value for bar; baz; quux=123")
// used by NEL.
class DirectiveHeaderValueParser {
 public:
  enum State {
    BEFORE_NAME,
    AFTER_NAME,
    BEFORE_VALUE,
    AFTER_DIRECTIVE,
    SYNTAX_ERROR
  };

  DirectiveHeaderValueParser(base::StringPiece value)
      : value_(value.data()),
        tokenizer_(value_.begin(), value_.end(), ";= "),
        stopped_with_error_(false) {
    tokenizer_.set_options(base::StringTokenizer::RETURN_DELIMS);
    tokenizer_.set_quote_chars("\"'");
  }

  // Gets the next directive, if there is one. Returns whether there was one.
  bool GetNext() {
    if (stopped_with_error_)
      return false;

    directive_name_ = base::StringPiece();
    directive_has_value_ = false;
    directive_values_.clear();

    State state = BEFORE_NAME;
    while (state != AFTER_DIRECTIVE && state != SYNTAX_ERROR
           && tokenizer_.GetNext()) {
      if (*tokenizer_.token_begin() == ' ')
        continue;

      switch (state) {
        case BEFORE_NAME:
          state = DoBeforeName();
          break;
        case AFTER_NAME:
          state = DoAfterName();
          break;
        case BEFORE_VALUE:
          state = DoBeforeValue();
          break;
        case AFTER_DIRECTIVE:
        case SYNTAX_ERROR:
          NOTREACHED();
          break;
      }
    }

    switch (state) {
      // If the parser just read the last directive, it may be in one of these
      // states, so return true to yield that directive.
      case AFTER_NAME:
      case BEFORE_VALUE:
      case AFTER_DIRECTIVE:
        return true;

      // If the parser never found a name, return false, since it doesn't have
      // a new directive for the caller.
      case BEFORE_NAME:
        return false;

      case SYNTAX_ERROR:
        stopped_with_error_ = true;
        return false;

      default:
        NOTREACHED();
        return false;
    }
  }


  base::StringPiece directive_name() const { return directive_name_; }
  bool directive_has_value() const { return directive_has_value_; }
  const std::vector<base::StringPiece>& directive_values() const {
    return directive_values_;
  }
  bool stopped_with_error() const { return stopped_with_error_; }

 private:
  State DoBeforeName() {
    if (tokenizer_.token_is_delim())
      return SYNTAX_ERROR;

    directive_name_ = tokenizer_.token_piece();
    return AFTER_NAME;
  }

  State DoAfterName() {
    if (tokenizer_.token_is_delim()) {
      char token_begin = *tokenizer_.token_begin();
      // Name can be followed by =value, ;, or just EOF.
      if (token_begin == '=') {
        directive_has_value_ = true;
        return BEFORE_VALUE;
      }
      if (token_begin == ';')
        return AFTER_DIRECTIVE;
    }
    return SYNTAX_ERROR;
  }

  State DoBeforeValue() {
    if (tokenizer_.token_is_delim()) {
      char token_begin = *tokenizer_.token_begin();
      if (token_begin == ';')
        return AFTER_DIRECTIVE;
      return SYNTAX_ERROR;
    }

    directive_values_.push_back(tokenizer_.token_piece());
    return BEFORE_VALUE;
  }

  std::string value_;
  base::StringTokenizer tokenizer_;

  base::StringPiece directive_name_;
  bool directive_has_value_;
  std::vector<base::StringPiece> directive_values_;
  bool stopped_with_error_;
};

bool Unquote(const std::string& in, std::string* out) {
  char first = in[0];
  char last = in[in.length() - 1];

  if (((first == '"') ^ (last == '"')) || ((first == '<') ^ (last == '>')))
    return false;

  if ((first == '"') || (first == '<'))
    *out = in.substr(1, in.length() - 2);
  else
    *out = in;
  return true;
}

bool ParseReportUri(const std::vector<base::StringPiece> in,
                    std::vector<std::unique_ptr<GURL>>* out) {
  if (in.size() < 1u)
    return false;

  out->clear();
  for (const auto& in_token : in) {
    std::string unquoted;
    if (!Unquote(in_token.as_string(), &unquoted))
      return false;
    GURL url(unquoted);
    if (!url.is_valid() || !content::IsOriginSecure(url))
      return false;
    out->push_back(base::MakeUnique<GURL>(url));
  }

  return true;
}

bool ParseMaxAge(const std::vector<base::StringPiece> in,
                 base::TimeDelta* out) {
  if (in.size() != 1u)
    return false;

  int64_t seconds;
  if (!base::StringToInt64(in[0], &seconds))
    return false;

  if (seconds < 0)
    return false;

  *out = base::TimeDelta::FromSeconds(seconds);
  return true;
}

}  // namespace

namespace domain_reliability {

DomainReliabilityHeader::~DomainReliabilityHeader() {}

// static
std::unique_ptr<DomainReliabilityHeader> DomainReliabilityHeader::Parse(
    base::StringPiece value) {
  std::vector<std::unique_ptr<GURL>> report_uri;
  base::TimeDelta max_age;
  bool include_subdomains = false;

  bool got_report_uri = false;
  bool got_max_age = false;
  bool got_include_subdomains = false;

  DirectiveHeaderValueParser parser(value);
  while (parser.GetNext()) {
    base::StringPiece name = parser.directive_name();
    if (name == "report-uri") {
      if (got_report_uri
          || !parser.directive_has_value()
          || !ParseReportUri(parser.directive_values(), &report_uri)) {
        return base::WrapUnique(new DomainReliabilityHeader(PARSE_ERROR));
      }
      got_report_uri = true;
    } else if (name == "max-age") {
      if (got_max_age
          || !parser.directive_has_value()
          || !ParseMaxAge(parser.directive_values(), &max_age)) {
        return base::WrapUnique(new DomainReliabilityHeader(PARSE_ERROR));
      }
      got_max_age = true;
    } else if (name == "includeSubdomains") {
      if (got_include_subdomains ||
          parser.directive_has_value()) {
        return base::WrapUnique(new DomainReliabilityHeader(PARSE_ERROR));
      }
      include_subdomains = true;
      got_include_subdomains = true;
    } else {
      LOG(WARNING) << "Ignoring unknown NEL header directive " << name << ".";
    }
  }

  if (parser.stopped_with_error() || !got_max_age)
    return base::WrapUnique(new DomainReliabilityHeader(PARSE_ERROR));

  if (max_age == base::TimeDelta::FromMicroseconds(0))
    return base::WrapUnique(new DomainReliabilityHeader(PARSE_CLEAR_CONFIG));

  if (!got_report_uri)
    return base::WrapUnique(new DomainReliabilityHeader(PARSE_ERROR));

  std::unique_ptr<DomainReliabilityConfig> config(
      new DomainReliabilityConfig());
  config->include_subdomains = include_subdomains;
  config->collectors.clear();
  config->collectors.swap(report_uri);
  config->success_sample_rate = 0.0;
  config->failure_sample_rate = 1.0;
  config->path_prefixes.clear();
  return base::WrapUnique(new DomainReliabilityHeader(
      PARSE_SET_CONFIG, std::move(config), max_age));
}

const DomainReliabilityConfig& DomainReliabilityHeader::config() const {
  DCHECK_EQ(PARSE_SET_CONFIG, status_);
  return *config_;
}

base::TimeDelta DomainReliabilityHeader::max_age() const {
  DCHECK_EQ(PARSE_SET_CONFIG, status_);
  return max_age_;
}

std::unique_ptr<DomainReliabilityConfig>
DomainReliabilityHeader::ReleaseConfig() {
  DCHECK_EQ(PARSE_SET_CONFIG, status_);
  status_ = PARSE_ERROR;
  return std::move(config_);
}

std::string DomainReliabilityHeader::ToString() const {
  std::string string = "";
  int64_t max_age_s = max_age_.InSeconds();

  if (config_->collectors.empty()) {
    DCHECK_EQ(0, max_age_s);
  } else {
    string += "report-uri=";
    for (const auto& uri : config_->collectors)
      string += uri->spec() + " ";
    // Remove trailing space.
    string.erase(string.length() - 1, 1);
    string += "; ";
  }

  string += "max-age=" + base::Int64ToString(max_age_s) + "; ";

  if (config_->include_subdomains)
    string += "includeSubdomains; ";

  // Remove trailing "; ".
  string.erase(string.length() - 2, 2);

  return string;
}

DomainReliabilityHeader::DomainReliabilityHeader(ParseStatus status)
    : status_(status) {
  DCHECK_NE(PARSE_SET_CONFIG, status_);
}

DomainReliabilityHeader::DomainReliabilityHeader(
    ParseStatus status,
    std::unique_ptr<DomainReliabilityConfig> config,
    base::TimeDelta max_age)
    : status_(status), config_(std::move(config)), max_age_(max_age) {
  DCHECK_EQ(PARSE_SET_CONFIG, status_);
  DCHECK(config_.get());
  DCHECK_NE(0, max_age_.InMicroseconds());
}

}  // namespace domain_reliability