# Path: bustard/utils.py
# def to_text(st, encoding='utf-8'):
#     if isinstance(st, str):
#         return st
#     elif isinstance(st, collections.ByteString):
#         return st.decode(encoding)
#     else:
#         return str(st)
# 
# def to_bytes(bt, encoding='utf-8'):
#     if isinstance(bt, collections.ByteString):
#         return bt
#     elif isinstance(bt, str):
#         return bt.encode(encoding)
#     else:
#         return bytes(bt)


import datetime
import io
import socket
import sys
import time
import urllib
from .utils import to_text, to_bytes

    def serve_forever(self):
        while 1:
            self.handle_one_request()
            self.client_connection.close()

    def handle_one_request(self):
        self.client_connection, self.client_address = self.socket.accept()
        # self.raw_request = raw_request = self.client_connection.recv(65537)
        init_read = 65536
        self.raw_request = raw_request = self.client_connection.recv(init_read)

        self.parse_request(raw_request)
        self.parse_headers(raw_request)
        length = int(self.headers.get('Content-Length', '0'))
        while len(self.raw_request) == length:
            self.raw_request += self.client_connection.recv(init_read)

        env = self.get_environ()

        print('[%s] "%s %s %s"' % (
            datetime.datetime.now(), env['REQUEST_METHOD'],
            env['PATH_INFO'], env['SERVER_PROTOCOL'],
        ))

        result = self.application(env, self.start_response)
        self.finish_response(result)

    def parse_request(self, raw_request):
        # GET /foo?a=1&b=2 HTTP/1.1

# Path: bustard/utils.py
# def to_text(st, encoding='utf-8'):
#     if isinstance(st, str):
#         return st
#     elif isinstance(st, collections.ByteString):
#         return st.decode(encoding)
#     else:
#         return str(st)
# 
# def to_bytes(bt, encoding='utf-8'):
#     if isinstance(bt, collections.ByteString):
#         return bt
#     elif isinstance(bt, str):
#         return bt.encode(encoding)
#     else:
#         return bytes(bt)


# Path: bustard/wsgi_server.py
import datetime
import io
import socket
import sys
import time
import urllib
from .utils import to_text, to_bytes


    def serve_forever(self):
        while 1:
            self.handle_one_request()
            self.client_connection.close()

    def handle_one_request(self):
        self.client_connection, self.client_address = self.socket.accept()
        # self.raw_request = raw_request = self.client_connection.recv(65537)
        init_read = 65536
        self.raw_request = raw_request = self.client_connection.recv(init_read)

        self.parse_request(raw_request)
        self.parse_headers(raw_request)
        length = int(self.headers.get('Content-Length', '0'))
        while len(self.raw_request) == length:
            self.raw_request += self.client_connection.recv(init_read)

        env = self.get_environ()

        print('[%s] "%s %s %s"' % (
            datetime.datetime.now(), env['REQUEST_METHOD'],
            env['PATH_INFO'], env['SERVER_PROTOCOL'],
        ))

        result = self.application(env, self.start_response)
        self.finish_response(result)

    def parse_request(self, raw_request):
        # GET /foo?a=1&b=2 HTTP/1.1

        first_line = to_text(raw_request.split(b'\r\n', 1)[0].strip())