コード例 #1
0
    def setUp(self):
        self.__fake_socket = FakeSocket()
        self.__fake_run_state = FakeRunState()

        parser = LineRequestParser(100, eof_as_eol=True)
        self.__request_stream = RequestStream(self.__fake_socket, parser.parse_request, max_buffer_size=100,
                                              max_request_size=100)
コード例 #2
0
 def __init__(self, sock_file, max_request_size=64 * 1024):
     self.__socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     self.__socket.connect(sock_file)
     self.__line_request = LineRequestParser(max_request_size,
                                             eof_as_eol=True)
     self.__request_stream = RequestStream(
         self.__socket, self.__line_request.parse_request, max_request_size,
         max_request_size)
     self.__headers = []
コード例 #3
0
    def setUp(self):
        super(TestRequestStream, self).setUp()
        self.__fake_socket = FakeSocket()
        self.__fake_run_state = FakeRunState()

        parser = LineRequestParser(10)
        self.__request_stream = RequestStream(self.__fake_socket,
                                              parser.parse_request,
                                              max_buffer_size=10,
                                              max_request_size=10)
コード例 #4
0
    def setUp(self):
        self.__fake_socket = FakeSocket()
        self.__fake_run_state = FakeRunState()
        self.__last_request = None

        parser = LineRequestParser(10)
        request_stream = RequestStream(self.__fake_socket, parser.parse_request, max_buffer_size=10,
                                       max_request_size=10)

        self.__fake_handler = ConnectionProcessor(request_stream, self.execute_request, self.__fake_run_state, 5.0)
        self.__fake_time = 0.0
コード例 #5
0
class TestLineRequestEOF( ScalyrTestCase ):
    def setUp(self):
        self.__fake_socket = FakeSocket()
        self.__fake_run_state = FakeRunState()

        parser = LineRequestParser(100, eof_as_eol=True)
        self.__request_stream = RequestStream(self.__fake_socket, parser.parse_request, max_buffer_size=100,
                                              max_request_size=100)
    def read_request(self):
        return self.__request_stream.read_request(run_state=self.__fake_run_state)

    def test_eof_as_eol( self ):
        self.__fake_socket.add_input('Hi there\nGoodbye')
        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals( self.read_request(), 'Goodbye' )
コード例 #6
0
class TestRequestStream(ScalyrTestCase):
    def setUp(self):
        super(TestRequestStream, self).setUp()
        self.__fake_socket = FakeSocket()
        self.__fake_run_state = FakeRunState()

        parser = LineRequestParser(10)
        self.__request_stream = RequestStream(self.__fake_socket,
                                              parser.parse_request,
                                              max_buffer_size=10,
                                              max_request_size=10)

    def test_basic_case(self):
        # Basic case of just a single line.
        self.__fake_socket.add_input('Hi there\n')
        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 0)

        self.assertIsNone(self.read_request())

    def test_multiple_lines(self):
        self.__fake_socket.add_input('Hi\nBye\nOk\n')
        self.assertEquals(self.read_request(), 'Hi\n')
        self.assertEquals(self.buffer_size(), 10)
        self.assertEquals(self.read_request(), 'Bye\n')
        self.assertEquals(self.read_request(), 'Ok\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 0)
        self.assertFalse(self.at_end())

        self.assertIsNone(self.read_request())

    def test_broken_lines(self):
        self.__fake_socket.add_input('Hi there')
        self.assertIsNone(self.read_request())
        self.__fake_socket.add_input('\n')
        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals(self.total_times_slept(), 2)
        self.assertEquals(self.buffer_size(), 0)
        self.assertFalse(self.at_end())

    def test_request_too_long(self):
        self.__fake_socket.add_input('0123456789')
        self.assertRaises(RequestSizeExceeded, self.read_request)
        self.assertFalse(self.at_end())

    def test_full_compaction(self):
        self.__fake_socket.add_input('012\n345678')
        self.assertEquals(self.read_request(), '012\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 10)
        self.assertFalse(self.at_end())

        self.assertIsNone(self.read_request())
        self.assertEquals(self.buffer_size(), 6)

        self.__fake_socket.add_input('\n')
        self.assertEquals(self.read_request(), '345678\n')
        self.assertEquals(self.total_times_slept(), 3)
        self.assertEquals(self.buffer_size(), 0)

    def test_close(self):
        self.__fake_socket.add_input('Hi there\n')
        self.__fake_socket.close()

        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 0)
        self.assertIsNone(self.read_request())
        self.assertTrue(self.at_end())
        self.assertEquals(self.total_times_slept(), 2)

    def read_request(self):
        return self.__request_stream.read_request(
            run_state=self.__fake_run_state)

    def total_times_slept(self):
        return self.__fake_run_state.total_times_slept

    def buffer_size(self):
        return self.__request_stream.get_buffer_size()

    def at_end(self):
        return self.__request_stream.at_end()
コード例 #7
0
class DockerRequest(object):
    def __init__(self, sock_file, max_request_size=64 * 1024):
        self.__socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        self.__socket.connect(sock_file)
        self.__line_request = LineRequestParser(max_request_size,
                                                eof_as_eol=True)
        self.__request_stream = RequestStream(
            self.__socket, self.__line_request.parse_request, max_request_size,
            max_request_size)
        self.__headers = []

    def get(self, path):
        endpoint = "GET %s HTTP/1.0\r\n\r\n" % path
        self.__socket.sendall(endpoint)
        self.__read_headers()
        return self

    def response_body(self):

        result = ""
        while not self.__request_stream.at_end():
            line = self.__request_stream.read_request()
            if line != None:
                result += line

        return result

    def readline(self):
        #Make sure the headers have been read - this might not be the case for some queries
        #even if __read_headers() has already been called
        if len(self.__headers) == 0:
            self.__read_headers()
            return None

        return self.__request_stream.read_request()

    def __read_headers(self):
        """reads HTTP headers from the request stream, leaving the stream at the first line of data"""
        self.response_code = 400
        self.response_message = "Bad request"

        #first line is response code
        line = self.__request_stream.read_request()
        if not line:
            return

        match = re.match('^(\S+) (\d+) (.*)$', line.strip())
        if not match:
            return

        if match.group(1).startswith('HTTP'):
            self.response_code = int(match.group(2))
            self.response_message = match.group(3)
            self.__headers = []
            while not self.__request_stream.at_end():
                line = self.__request_stream.read_request()
                if line == None:
                    break
                else:
                    cur_line = line.strip()
                    if len(cur_line) == 0:
                        break
                    else:
                        self.__headers.append(cur_line)
コード例 #8
0
 def __init__( self, sock_file, max_request_size=128*1024 ):
     self.__socket = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
     self.__socket.connect( sock_file )
     self.__line_request = LineRequestParser( max_request_size, eof_as_eol=True )
     self.__request_stream = RequestStream( self.__socket, self.__line_request.parse_request, max_request_size, max_request_size )
     self.__headers = []
コード例 #9
0
class DockerRequest( object ):

    def __init__( self, sock_file, max_request_size=128*1024 ):
        self.__socket = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM )
        self.__socket.connect( sock_file )
        self.__line_request = LineRequestParser( max_request_size, eof_as_eol=True )
        self.__request_stream = RequestStream( self.__socket, self.__line_request.parse_request, max_request_size, max_request_size )
        self.__headers = []

    def get( self, path ):
        endpoint = "GET %s HTTP/1.0\r\n\r\n" % path
        self.__socket.sendall( endpoint )
        self.__read_headers()
        return self

    def response_body( self ):

        result = ""
        while not self.__request_stream.at_end():
            line = self.__request_stream.read_request(0.1)
            if line != None:
                result += line

        return result

    def readline( self ):
        #Make sure the headers have been read - this might not be the case for some queries
        #even if __read_headers() has already been called
        if len( self.__headers ) == 0:
            self.__read_headers()
            return None

        return self.__request_stream.read_request(0.1)

    def __read_headers( self ):
        """reads HTTP headers from the request stream, leaving the stream at the first line of data"""
        self.response_code = 400
        self.response_message = "Bad request"

        #first line is response code
        line = self.__request_stream.read_request(0.1)
        while line == None and not self.__request_stream.at_end():
            global_log.info( 'Trying at first line' )
            line = self.__request_stream.read_request(0.1)

        global_log.info( 'line: %s' % line )
        match = re.match( '^(\S+) (\d+) (.*)$', line.strip() )
        if not match:
            global_log.info( 'Bailing at re.match' )
            return

        if match.group(1).startswith( 'HTTP' ):
            self.response_code = int( match.group(2) )
            self.response_message = match.group(3)
            self.__headers = []
            while not self.__request_stream.at_end():
                line = self.__request_stream.read_request(0.1)
                if line == None:
                    break
                else:
                    cur_line = line.strip()
                    if len( cur_line ) == 0:
                        break
                    else:
                        self.__headers.append( cur_line )
コード例 #10
0
class TestRequestStream(ScalyrTestCase):
    def setUp(self):
        self.__fake_socket = FakeSocket()
        self.__fake_run_state = FakeRunState()

        parser = LineRequestParser(10)
        self.__request_stream = RequestStream(self.__fake_socket, parser.parse_request, max_buffer_size=10,
                                              max_request_size=10)

    def test_basic_case(self):
        # Basic case of just a single line.
        self.__fake_socket.add_input('Hi there\n')
        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 0)

        self.assertIsNone(self.read_request())

    def test_multiple_lines(self):
        self.__fake_socket.add_input('Hi\nBye\nOk\n')
        self.assertEquals(self.read_request(), 'Hi\n')
        self.assertEquals(self.buffer_size(), 10)
        self.assertEquals(self.read_request(), 'Bye\n')
        self.assertEquals(self.read_request(), 'Ok\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 0)
        self.assertFalse(self.at_end())

        self.assertIsNone(self.read_request())

    def test_broken_lines(self):
        self.__fake_socket.add_input('Hi there')
        self.assertIsNone(self.read_request())
        self.__fake_socket.add_input('\n')
        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals(self.total_times_slept(), 2)
        self.assertEquals(self.buffer_size(), 0)
        self.assertFalse(self.at_end())

    def test_request_too_long(self):
        self.__fake_socket.add_input('0123456789')
        self.assertRaises(RequestSizeExceeded, self.read_request)
        self.assertFalse(self.at_end())
    
    def test_full_compaction(self):
        self.__fake_socket.add_input('012\n345678')
        self.assertEquals(self.read_request(), '012\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 10)
        self.assertFalse(self.at_end())

        self.assertIsNone(self.read_request())
        self.assertEquals(self.buffer_size(), 6)

        self.__fake_socket.add_input('\n')
        self.assertEquals(self.read_request(), '345678\n')
        self.assertEquals(self.total_times_slept(), 3)
        self.assertEquals(self.buffer_size(), 0)

    def test_close(self):
        self.__fake_socket.add_input('Hi there\n')
        self.__fake_socket.close()

        self.assertEquals(self.read_request(), 'Hi there\n')
        self.assertEquals(self.total_times_slept(), 1)
        self.assertEquals(self.buffer_size(), 0)
        self.assertIsNone(self.read_request())
        self.assertTrue(self.at_end())
        self.assertEquals(self.total_times_slept(), 2)

    def read_request(self):
        return self.__request_stream.read_request(run_state=self.__fake_run_state)

    def total_times_slept(self):
        return self.__fake_run_state.total_times_slept

    def buffer_size(self):
        return self.__request_stream.get_buffer_size()

    def at_end(self):
        return self.__request_stream.at_end()