def test_response_round_trip(self):
        web_page = '<html><body>This is my web page!</body></html>'

        resp0 = http.Response(
            status  =200,
            reason  ='OK',
            header  ={
                'Content-Type': 'text/html',
                'Content-Length': '46',
                },
            body=web_page.encode('UTF-8'))
        # Round trip through our API.
        msg = http.format_response(resp0)
        resp1 = http.parse_response(msg)

        self.assertEqual(resp1.status, 200)
        self.assertEqual(resp1.reason, 'OK')
        self.assertEqual(len(resp1.header), 2)
        self.assertTrue('Content-Type' in resp1.header)
        self.assertEqual(resp1.header['Content-Type'], 'text/html')
        self.assertTrue('Content-Length' in resp1.header)
        self.assertEqual(resp1.header['Content-Length'], '46')
        self.assertEqual(resp1.body, web_page.encode('UTF-8'))
Example #2
0
    def _append_packet(self, packet):
        """Appends a packet to the end of the list of received packets and processes it"""
        self.next_seq += len(packet.data)

        if self.headers is not None:
            if self.packets is None:
                self.packets = StringIO.StringIO()
            self.packets.write(packet.data)
            self.http_bytes_loaded += len(packet.data)
        else:
            self.header_data += packet.data

        # check if we have enough packets for the entire http header
        if self.is_http and self.headers is None:
            if http.has_complete_headers(self.header_data):
                resp = http.parse_response(self.header_data)
                self.header_data = None
                if self.packets is None:
                    self.packets = StringIO.StringIO()
                self.packets.write(resp['body'])
                self.headers = resp['headers']
                self.http_bytes_loaded = len(resp['body'])

                self._on_http_headers()

        # check if we have finished the request
        if self.http_content_length is not None:

            if self.http_content_length == self.http_bytes_loaded:
                self.is_finished = True
            elif self.http_content_length < self.http_bytes_loaded:
                logging.error(
                    "Received data was longer than the content length header")
                self.is_valid = False
                self.is_finished = True

        self._handle_ordered_packet(packet)
Example #3
0
    def _append_packet(self, packet):
        """Appends a packet to the end of the list of received packets and processes it"""
        self.next_seq += len(packet.data)

        if self.headers is not None:
            if self.packets is None:
                self.packets = StringIO.StringIO()
            self.packets.write(packet.data)
            self.http_bytes_loaded += len(packet.data)
        else:
            self.header_data += packet.data

        # check if we have enough packets for the entire http header
        if self.is_http and self.headers is None:
            if http.has_complete_headers(self.header_data):
                resp = http.parse_response(self.header_data)
                self.header_data = None
                if self.packets is None:
                    self.packets = StringIO.StringIO()
                self.packets.write(resp['body'])
                self.headers = resp['headers']
                self.http_bytes_loaded = len(resp['body'])

                self._on_http_headers()

        # check if we have finished the request
        if self.http_content_length is not None:

            if self.http_content_length == self.http_bytes_loaded:
                self.is_finished = True
            elif self.http_content_length < self.http_bytes_loaded:
                logging.error("Received data was longer than the content length header")
                self.is_valid = False
                self.is_finished = True

        self._handle_ordered_packet(packet)