Passed
Push — develop ( f1fe9e...5c5de8 )
by
unknown
06:59 queued 03:36
created

StreamingMiddleware   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 3 1
B __call__() 0 20 5
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import fnmatch
17
18
__all__ = [
19
    'StreamingMiddleware'
20
]
21
22
23
class StreamingMiddleware(object):
24
    def __init__(self, app, path_whitelist=None):
25
        self.app = app
26
        self._path_whitelist = path_whitelist or []
27
28
    def __call__(self, environ, start_response):
29
        # Forces eventlet to respond immediately upon receiving a new chunk from endpoint rather
30
        # than buffering it until the sufficient chunk size is reached. The order for this
31
        # middleware is not important since it acts as pass-through.
32
33
        matches = False
34
        req_path = environ.get('PATH_INFO', None)
35
36
        if not self._path_whitelist:
37
            matches = True
38
        else:
39
            for path_whitelist in self._path_whitelist:
40
                if fnmatch.fnmatch(req_path, path_whitelist):
41
                    matches = True
42
                    break
43
44
        if matches:
45
            environ['eventlet.minimum_write_chunk_size'] = 0
46
47
        return self.app(environ, start_response)
48