def read_headers(data, target=None):
    """
    Read HTTP headers from the supplied data string and return a dictionary
    of those headers. If bad data is supplied, a :class:`BadRequest` exception
    will be raised.

    =========  ============
    Argument   Description
    =========  ============
    data       A data string containing HTTP headers.
    target     *Optional.* A dictionary in which to place the processed headers.
    =========  ============
    """
    if target is None:
        cast = True
        target = {}
    else:
        cast = False

    data = data.rstrip(CRLF)
    key = None

    if data:
        for line in data.split(CRLF):
            if not line:
                raise BadRequest('Illegal header line: %r' % line)
            if key and line[0] in ' \t':
                val = line.strip()
                mline = True
            else:
                mline = False
                try:
                    key, val = line.split(':', 1)
                except ValueError:
                    raise BadRequest('Illegal header line: %r' % line)

                key = key.strip().lower()
                val = val.strip()

                try:
                    val = int(val)
                except ValueError:
                    pass

            if key in target:
                if mline:
                    if isinstance(target[key], list):
                        if target[key]:
                            target[key][-1] += ' ' + val
                        else:
                            target[key].append(val)
                    else:
                        target[key] += ' ' + val
                elif key in COMMA_HEADERS:
                    target[key] = '%s, %s' % (target[key], val)
                elif isinstance(target[key], list):
                    target[key].append(val)
                else:
                    target[key] = [target[key], val]
                continue
            target[key] = val

    if cast:
        target = HTTPHeaders(_store=target)

    return target
dataset/ETHPy150Open ecdavis/pants/pants/http/utils.py/read_headers/original