예제 #1
0
class HttpResonse(object):
    def __init__(self, cookies=None, encoding='utf-8'):
        self.headers = CaseInsensitiveDict()
        self.content = b''
        self.encoding = encoding
        if cookies is None:
            self.cookies = DictCookie()
        else:
            self.cookies = cookies

    # def on_message_begin(self):
    # print('on_message_begin')

    def on_header(self, name, value):
        name = name.decode(self.encoding)
        value = value.decode(self.encoding)
        if name.lower() == 'set-cookie':
            self.cookies.load(value)
            if self.headers.get(name):
                self.headers[name] += ', ' + value
                return None
        self.headers[name] = value

    # def on_headers_complete(self):
    # print(self.headers)

    def on_body(self, value):
        self.content += value
예제 #2
0
def parse_headers(lines):
    headers = CaseInsensitiveDict()
    cookies = DictCookie()

    protocol, status_code, ok = lines[0].decode('utf-8').split(' ', 2)

    for line in lines[1:]:
        line = line.decode('utf-8').strip()
        if not line:
            continue

        index = line.find(': ')
        key = line[:index]
        value = line[index + 2:]

        if key.lower() == 'set-cookie':
            cookies.load(value)
            if headers.get(key):
                headers[key] += ', ' + value
        else:
            headers[key] = value

    return (protocol, status_code, ok), headers, cookies