Method: Inspec::Resources::ApacheConf#read_content

Defined in:
lib/resources/apache_conf.rb

#read_contentObject



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
# File 'lib/resources/apache_conf.rb', line 64

def read_content
  @content = ''
  @params = {}

  read_file_content(conf_path)

  to_read = [conf_path]
  until to_read.empty?
    raw_conf = read_file(to_read[0])
    @content += raw_conf

    # An explaination of the below regular expression.
    # Creates two capture groups.
    # The first group captures the first group of non-whitespace character
    # surrounded whitespace characters.
    # The second group contains a conditional with a positive lookahead
    # (does the line end with one or more spaces?). If the lookahead succeeds
    # a non-greedy capture takes place, if it fails then a greedy capture takes place.
    # The regex is terminated by an expression that matches zero or more spaces.
    params = SimpleConfig.new(
      raw_conf,
      assignment_regex: /^\s*(\S+)\s+['"]*((?=.*\s+$).*?|.*?)['"]*\s*$/,
      multiple_values: true,
    ).params

    # Capture any characters between quotes that are not escaped in values
    params.values.map! do |value|
      value.map! do |sub_value|
        sub_value[/(?<=["|'])(?:\\.|[^"'\\])*(?=["|'])/] || sub_value
      end
    end

    @params.merge!(params)

    to_read = to_read.drop(1)
    to_read += include_files(params).find_all do |fp|
      not @files_contents.key? fp
    end
  end

  # fiter comments
  @content = filter_comments @content
  @content
end