summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/network/parsed_content_header_field_parameters.h
blob: 24a4b1605106c60f238df2b2cd9cf51568c11b7c (plain)
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
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_NETWORK_PARSED_CONTENT_HEADER_FIELD_PARAMETERS_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_NETWORK_PARSED_CONTENT_HEADER_FIELD_PARAMETERS_H_

#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/optional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class HeaderFieldTokenizer;

// Parses content header field parameters as specified in RFC2045 and stores
// them. It is used internally by ParsedContent* classes.
// FIXME: add support for comments.
class PLATFORM_EXPORT ParsedContentHeaderFieldParameters final {
  DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();

 public:
  struct NameValue {
    NameValue(String name, String value) : name(name), value(value) {}

    String name;
    String value;
  };

  using NameValuePairs = Vector<NameValue>;
  using const_iterator = NameValuePairs::const_iterator;
  using reverse_const_iterator = NameValuePairs::const_reverse_iterator;

  // When |Relaxed| is specified, the parser parses parameter values in a sloppy
  // manner, i.e., only ';' and '"' are treated as special characters.
  // See https://chromiumcodereview.appspot.com/23043002.
  enum class Mode {
    kNormal,
    kRelaxed,
  };

  // We use base::Optional instead of WTF::Optional which requires its content
  // type to be fully defined. They are essentially same, so uses of this class
  // can (and should) use WTF::Optional to store the returned value.
  static base::Optional<ParsedContentHeaderFieldParameters> Parse(
      HeaderFieldTokenizer,
      Mode);

  // Note that in the case of multiple values for the same name, the last value
  // is returned.
  String ParameterValueForName(const String&) const;
  size_t ParameterCount() const;
  bool HasDuplicatedNames() const;

  const_iterator begin() const { return parameters_.begin(); }
  const_iterator end() const { return parameters_.end(); }

  reverse_const_iterator rbegin() const { return parameters_.rbegin(); }
  reverse_const_iterator rend() const { return parameters_.rend(); }

 private:
  explicit ParsedContentHeaderFieldParameters(NameValuePairs parameters)
      : parameters_(std::move(parameters)) {}

  NameValuePairs parameters_;
};

}  // namespace blink

#endif