File size: 7,746 Bytes
d439dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
# MinIO Python Library for Amazon S3 Compatible Cloud Storage, (C)
# 2019 MinIO, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
minio.select.reader
~~~~~~~~~~~~~~~

This module implements the reader for SelectObject response body.

:copyright: (c) 2019 by MinIO, Inc.
:license: Apache 2.0, see LICENSE for more details.

"""

from __future__ import absolute_import

import io
import sys
from xml.etree import ElementTree

from .errors import SelectCRCValidationError, SelectMessageError
from .helpers import (ERROR, EVENT, EVENT_CONTENT_TYPE, EVENT_RECORDS,
                      EVENT_STATS, byte_int, calculate_crc, validate_crc)


def _extract_header(header_bytes):
    """
    populates the header map after reading the header in bytes
    """
    header_map = {}
    header_byte_parsed = 0
    # While loop ends when all the headers present are read
    # header contains multipe headers
    while header_byte_parsed < len(header_bytes):
        header_name_byte_length = byte_int(
            header_bytes[header_byte_parsed:header_byte_parsed+1])
        header_byte_parsed += 1
        header_name = header_bytes[
            header_byte_parsed:header_byte_parsed+header_name_byte_length
        ]
        header_byte_parsed += header_name_byte_length
        # Header Value Type is of 1 bytes and is skipped
        header_byte_parsed += 1
        value_string_byte_length = byte_int(
            header_bytes[header_byte_parsed:header_byte_parsed+2]
        )
        header_byte_parsed += 2
        header_value = header_bytes[
            header_byte_parsed:header_byte_parsed+value_string_byte_length
        ]
        header_byte_parsed += value_string_byte_length
        header_map[header_name.decode(
            "utf-8").lstrip(":")] = header_value.decode("utf-8").lstrip(":")
    return header_map


def _parse_stats(stats):
    """
    Parses stats XML and populates the stat dict.
    """
    stat = {}
    for attribute in ElementTree.fromstring(stats):
        if attribute.tag == 'BytesScanned':
            stat['BytesScanned'] = attribute.text
        elif attribute.tag == 'BytesProcessed':
            stat['BytesProcessed'] = attribute.text
        elif attribute.tag == 'BytesReturned':
            stat['BytesReturned'] = attribute.text

    return stat


class SelectObjectReader:
    """
    SelectObjectReader returns a Reader that upon read
    returns queried data, but stops when the response ends.
    LimitedRandomReader is compatible with BufferedIOBase.
    """

    def __init__(self, response):
        self.response = response
        self.remaining_bytes = bytes()
        self.stat = {}
        self.prog = {}

    def readable(self):  # pylint: disable=no-self-use
        """Return this is readable."""
        return True

    def writeable(self):  # pylint: disable=no-self-use
        """Return this is not writeable."""
        return False

    def close(self):
        """Close response."""
        self.response.close()

    def stats(self):
        """Get stats information."""
        return self.stat

    def progress(self):
        """Get progress information."""
        return self.prog

    def __extract_message(self):
        """
        Process the response sent from server.
        https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectSELECTContent.html
        """

        crc_bytes = io.BytesIO()
        total_bytes_len = self.response.read(4)
        if not total_bytes_len:
            return {}

        total_length = byte_int(total_bytes_len)
        header_bytes_len = self.response.read(4)
        if not header_bytes_len:
            return {}

        header_len = byte_int(header_bytes_len)

        crc_bytes.write(total_bytes_len)
        crc_bytes.write(header_bytes_len)

        prelude_bytes_crc = self.response.read(4)
        if not validate_crc(crc_bytes.getvalue(), prelude_bytes_crc):
            raise SelectCRCValidationError(
                {"Checksum Mismatch, PreludeCRC of " +
                 str(calculate_crc(crc_bytes.getvalue())) +
                 " does not equal expected CRC of " +
                 str(byte_int(prelude_bytes_crc))})

        crc_bytes.write(prelude_bytes_crc)

        header_bytes = self.response.read(header_len)
        if not header_bytes:
            raise SelectMessageError(
                "Premature truncation of select message header" +
                ", server is sending corrupt message?")

        crc_bytes.write(header_bytes)

        header_map = _extract_header(header_bytes)
        payload_length = total_length - header_len - int(16)
        payload_bytes = b''
        event_type = header_map["event-type"]

        if header_map["message-type"] == ERROR:
            raise SelectMessageError(
                header_map["error-code"] + ":\"" +
                header_map["error-message"] + "\"")

        if header_map["message-type"] != EVENT:
            raise SelectMessageError(
                "Unrecognized message-type {0}".format(
                    header_map["message-type"])
            )

        if event_type == EVENT_STATS:
            content_type = header_map["content-type"]
            if content_type != EVENT_CONTENT_TYPE:
                raise SelectMessageError(
                    "Unrecognized content-type {0}".format(content_type))

            payload_bytes = self.response.read(payload_length)
            self.stat = _parse_stats(payload_bytes)
        elif event_type == EVENT_RECORDS:
            payload_bytes = self.response.read(payload_length)

        crc_bytes.write(payload_bytes)

        message_crc = self.response.read(4)
        if not message_crc:
            return {}

        if not validate_crc(crc_bytes.getvalue(), message_crc):
            raise SelectCRCValidationError(
                {"Checksum Mismatch, MessageCRC of " +
                 str(calculate_crc(crc_bytes.getvalue())) +
                 " does not equal expected CRC of " +
                 str(byte_int(message_crc))})

        message = {event_type: payload_bytes}
        return message

    def stream(self, num_bytes=32*1024):
        """
        extract each record from the response body ... and buffer it.
        send only up to requested bytes such as message[:num_bytes]
        rest is buffered and added to the next iteration.

        caller should call self.close() to close the stream.
        """
        while not self.response.isclosed():
            if not self.remaining_bytes:
                message = self.__extract_message()
                if EVENT_RECORDS not in message:
                    continue

                self.remaining_bytes = message.get(EVENT_RECORDS, b'')

            result = self.remaining_bytes
            if num_bytes < len(self.remaining_bytes):
                result = self.remaining_bytes[:num_bytes]
            self.remaining_bytes = self.remaining_bytes[len(result):]

            if result == b'':
                break
            if sys.version_info.major == 3:
                yield result.decode('utf-8', errors='ignore')
            else:
                # Python 2.x needs explicit conversion.
                yield result.decode('utf-8', errors='ignore').encode('utf-8')