Ejemplo n.º 1
0
def set_line_number(string, location, tokens):
    # check and limit CPU usage
    context.check_and_limit_cpu_consumption()

    if len(tokens) == 1:
        line_number = lineno(location, string)
        tokens_cache[tokens[0]] = line_number
        tokens.line_number = line_number
    else:
        for item in tokens:
            tokens.line_number = tokens_cache.get(item)
Ejemplo n.º 2
0
def set_line_number(input_string, location, tokens):
    # check and limit CPU usage
    context.check_and_limit_cpu_consumption()

    if len(tokens) == 1:
        line_number = lineno(location, input_string)
        NginxConfigParser.tokens_cache[tokens[0]] = line_number
        tokens.line_number = line_number
    else:
        for item in tokens:
            tokens.line_number = NginxConfigParser.tokens_cache.get(item)
Ejemplo n.º 3
0
    def _convert(self, filename, ngx_ctx=None):
        """
        Convert a new style payload to the old 'dense' style payload
        
        :param filename: str - name of the file
        :param ngx_ctx: dict - block from block directive to convert
        :return: dict - the converted block payload
        """
        # pause for a bit if this is taking up too much cpu
        context.check_and_limit_cpu_consumption()

        if ngx_ctx is not None:
            skip_cache = True  # because we're parsing a block context
        elif filename in self._converted_cache:
            cached = self._converted_cache[filename]
            return copy.deepcopy(cached)
        elif filename in self._parsed_cache:
            ngx_ctx = self._parsed_cache[filename]
            skip_cache = False  # because this is the top context for a file
        else:
            return {}  # this file must've been empty

        block = {}

        for stmt in ngx_ctx:
            # ignore certain directives for security reasons
            if stmt['directive'] in IGNORED_DIRECTIVES:
                continue

            # get the directive's value
            if 'block' in stmt:
                value = self._convert(filename, stmt['block'])
            else:
                value = argstring(stmt)

            # ignore access/error log directives if they use nginx variables
            if stmt['directive'] in ('access_log', 'error_log'):
                if not value or ('$' in value and ' if=$' not in value):
                    continue

            # add the (file name, line number) tuple to self.index
            index = self._add_index(filename, stmt['line'])

            # add the (directive value, index index) tuple to the block
            store_directive(block, stmt, (value, index))

            if stmt['directive'] == 'include':
                value = self._abspath(value)
                if value not in self.includes:
                    self.includes.append(value)

                # add the included directives to the current block
                for fname in stmt['includes']:
                    merge_blocks(block, self._convert(fname))

            elif stmt['directive'] == 'ssl_certificate' and value:
                # skip if value uses nginx variables other than if
                if value and ('$' not in value or ' if=$' in value):
                    value = self._abspath(value)
                    if value not in self.ssl_certificates:
                        self.ssl_certificates.append(value)

        # we cache converted junk by file, so skip if we just parsed a block
        if not skip_cache:
            self._converted_cache[filename] = copy.deepcopy(block)

        return block