This file is indexed.

/usr/lib/python2.7/dist-packages/twext/internet/tcp.py is in python-twext 0.1.b2.dev15059-1.

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
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
##
# Copyright (c) 2005-2015 Apple Inc. All rights reserved.
#
# 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.
##

"""
Extentions to twisted.internet.tcp.
"""

__all__ = [
    "MaxAcceptTCPServer",
    "MaxAcceptSSLServer",
]

import socket
from OpenSSL import SSL

from twisted.application import internet
from twisted.internet import tcp, ssl
from twisted.internet.defer import succeed

from twext.python.log import Logger

log = Logger()



class MaxAcceptPortMixin(object):
    """
    Mixin for resetting maxAccepts.
    """
    def doRead(self):
        self.numberAccepts = min(
            self.factory.maxRequests - self.factory.outstandingRequests,
            self.factory.maxAccepts
        )
        tcp.Port.doRead(self)



class MaxAcceptTCPPort(MaxAcceptPortMixin, tcp.Port):
    """
    Use for non-inheriting tcp ports.
    """



class MaxAcceptSSLPort(MaxAcceptPortMixin, ssl.Port):
    """
    Use for non-inheriting SSL ports.
    """



class InheritedTCPPort(MaxAcceptTCPPort):
    """
    A tcp port which uses an inherited file descriptor.
    """

    def __init__(self, fd, factory, reactor):
        tcp.Port.__init__(self, 0, factory, reactor=reactor)
        # MOR: careful because fromfd dup()'s the socket, so we need to
        # make sure we don't leak file descriptors
        self.socket = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
        self._realPortNumber = self.port = self.socket.getsockname()[1]


    def createInternetSocket(self):
        return self.socket


    def startListening(self):
        log.info(
            "{self.factory.__class__} starting on {self._realPortNumber}",
            self=self
        )
        self.factory.doStart()
        self.connected = 1
        self.fileno = self.socket.fileno
        self.numberAccepts = self.factory.maxRequests
        self.startReading()



class InheritedSSLPort(InheritedTCPPort):
    """
    An SSL port which uses an inherited file descriptor.
    """

    _socketShutdownMethod = 'sock_shutdown'

    transport = ssl.Server


    def __init__(self, fd, factory, ctxFactory, reactor):
        InheritedTCPPort.__init__(self, fd, factory, reactor)
        self.ctxFactory = ctxFactory
        self.socket = SSL.Connection(self.ctxFactory.getContext(), self.socket)


    def _preMakeConnection(self, transport):
        transport._startTLS()
        return tcp.Port._preMakeConnection(self, transport)



def _allConnectionsClosed(protocolFactory):
    """
    Check to see if protocolFactory implements allConnectionsClosed( ) and
    if so, call it.  Otherwise, return immediately.
    This allows graceful shutdown by waiting for all requests to be completed.

    @param protocolFactory: (usually) an HTTPFactory implementing
        allConnectionsClosed which returns a Deferred which fires when all
        connections are closed.

    @return: A Deferred firing None when all connections are closed, or
        immediately if the given factory does not track its connections (e.g.
        InheritingProtocolFactory)
    """
    if hasattr(protocolFactory, "allConnectionsClosed"):
        return protocolFactory.allConnectionsClosed()
    return succeed(None)



class MaxAcceptTCPServer(internet.TCPServer):
    """
    TCP server which will uses MaxAcceptTCPPorts (and optionally,
    inherited ports)

    @ivar myPort: When running, this is set to the L{IListeningPort} being
        managed by this service.
    """

    def __init__(self, *args, **kwargs):
        internet.TCPServer.__init__(self, *args, **kwargs)
        self.protocolFactory = self.args[1]
        self.protocolFactory.myServer = self
        self.inherit = self.kwargs.get("inherit", False)
        self.backlog = self.kwargs.get("backlog", None)
        self.interface = self.kwargs.get("interface", None)


    def _getPort(self):
        from twisted.internet import reactor

        if self.inherit:
            port = InheritedTCPPort(self.args[0], self.args[1], reactor)
        else:
            port = MaxAcceptTCPPort(
                self.args[0], self.args[1],
                self.backlog, self.interface, reactor
            )

        port.startListening()
        self.myPort = port
        return port


    def stopService(self):
        """
        Wait for outstanding requests to finish

        @return: a Deferred which fires when all outstanding requests are
            complete
        """
        internet.TCPServer.stopService(self)
        return _allConnectionsClosed(self.protocolFactory)



class MaxAcceptSSLServer(internet.SSLServer):
    """
    SSL server which will uses MaxAcceptSSLPorts (and optionally,
    inherited ports)
    """

    def __init__(self, *args, **kwargs):
        internet.SSLServer.__init__(self, *args, **kwargs)
        self.protocolFactory = self.args[1]
        self.protocolFactory.myServer = self
        self.inherit = self.kwargs.get("inherit", False)
        self.backlog = self.kwargs.get("backlog", None)
        self.interface = self.kwargs.get("interface", None)


    def _getPort(self):
        from twisted.internet import reactor

        if self.inherit:
            port = InheritedSSLPort(
                self.args[0], self.args[1], self.args[2], reactor
            )
        else:
            port = MaxAcceptSSLPort(
                self.args[0], self.args[1], self.args[2],
                self.backlog, self.interface, self.reactor
            )

        port.startListening()
        self.myPort = port
        return port


    def stopService(self):
        """
        Wait for outstanding requests to finish

        @return: a Deferred which fires when all outstanding requests are
            complete.
        """
        internet.SSLServer.stopService(self)
        # TODO: check for an ICompletionWaiter interface
        return _allConnectionsClosed(self.protocolFactory)