This file is indexed.

/usr/share/pyshared/restkit/manager/base.py is in python-restkit 3.3.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# -*- coding: utf-8 -
#
# This file is part of restkit released under the MIT license. 
# See the NOTICE for more information.

from __future__ import with_statement

from collections import deque
import select
import signal
import socket
import select
import threading
import time

from restkit.sock import close

class ConnectionReaper(threading.Thread):
    """ connection reaper thread. Open a thread that will murder iddle
    connections after a delay """

    running = False

    def __init__(self, manager, delay=150):
        self.manager = manager
        self.delay = delay
        threading.Thread.__init__(self)
        self.setDaemon(True)

    def run(self):
        self.running = True
        while True:
            time.sleep(self.delay)
            self.manager.murder_connections()

    def ensure_started(self):
        if not self.running and not self.isAlive():
            self.start()

class Manager(object):
    """ Connection mager, it keeps a pool of opened connections and reap
    them after a delay if reap_connection is True. By default a thread
    is used to reap connections, but it can be replaced with signaling
    if needed. In this case a signal will be send to the manager after a
    delay. Be aware that using signaling isn't thread-safe and works
    only on UNIX or UNIX like."""

    def __init__(self, max_conn=10, timeout=150, reap_connections=True, 
            with_signaling=False):
        self.max_conn = max_conn
        self.timeout = timeout
        self.reap_connections = reap_connections
        self.with_signaling = with_signaling

        self.sockets = dict()
        self.active_sockets = dict()
        self.connections_count = dict()
        self._lock = self.get_lock()

        self._reaper = None

        if reap_connections and timeout is not None:
            self.start()

    def get_lock(self):
        return threading.Lock()

    def murder_connections(self, *args):
        self._lock.acquire()
        try:
            active_sockets = self.active_sockets.copy()
            for fno, (sock, t0, k) in active_sockets.items():
                diff = time.time() - t0
                if diff <= self.timeout:
                    continue
                close(sock)
                del self.active_sockets[fno]
                self.connections_count[k] -= 1
        finally:
            self._lock.release()

    def close_connections(self):
        self._lock.acquire()
        try:
            active_sockets = self.active_sockets.copy()

            for fno, (sock, t0, k) in active_sockets.items():
                close(sock)
                del self.active_sockets[fno]
        finally:
            self._lock.release()

    def start(self):
        if self.with_signaling:
            signal.signal(signal.SIGALRM, self.murder_connections)
            signal.alarm(self.timeout)
        else:
            self._reaper = ConnectionReaper(self, delay=self.timeout)
            self._reaper.ensure_started()

    def all_connections_count(self):
        """ return all counts per address registered. """
        return self.connections_count.items()

    def connection_count(self, addr, ssl):
        """ get connections count for an address """
        self._lock.acquire()
        try:
            return self.connections_count[(addr, ssl)]
        finally:
            self._lock.release()

        return self.connections_count[(addr, ssl)]

    def find_socket(self, addr, ssl=False):
        """ find a socket from a its address in the pool and return if
        there is one available, else, return None """

        self._lock.acquire()
        try:
            key = (addr, ssl)
            try:
                socks = self.sockets[key]
                while True:
                    fno, sck = socks.pop()
                    if fno in self.active_sockets:
                        del self.active_sockets[fno]
                        if not self.is_closed(sck):
                            break 
                self.sockets[key] = socks
                self.connections_count[key] -= 1
                return sck
            except (IndexError, KeyError,):
                return None
        finally:
            self._lock.release()

    def is_closed(self, sck):
        if sck is None:
            return True
        try:
            r, _, _ = select.select([sck], [], [], 0)
            if not r:
                return False
        except (ValueError, select.error,):
            return True
        read = sck.recv(1024)
        sck.close()
        return True

    def store_socket(self, sck, addr, ssl=False):
        """ store a socket in the pool to reuse it across threads """

        if self._reaper is not None:
            self._reaper.ensure_started()

        self._lock.acquire()
        try:
            key = (addr, ssl)
            try:
                socks = self.sockets[key]
            except KeyError:
                socks = deque()

            if len(socks) < self.max_conn:
                # add connection to the pool
                try:
                    fno = sck.fileno() 
                except (socket.error, AttributeError,):
                    # socket has been closed
                    return

                self.active_sockets[fno] = (sck, time.time(), key)

                socks.appendleft((fno, sck))
                self.sockets[key] = socks
               
                try:
                    self.connections_count[key] += 1
                except KeyError:
                    self.connections_count[key] = 1 

            else:
                # close connection if we have enough connections in the
                # pool.
                close(sck)
        finally:
            self._lock.release()