Ticket #6325: 6325_3.patch

File 6325_3.patch, 9.2 KB (added by eddie, 9 years ago)

deleted --dsc

  • twisted/web/tap.py

     
    66"""
    77
    88import os
     9import warnings
    910
    1011# Twisted Imports
    1112from twisted.web import server, static, twcgi, script, demo, distrib, wsgi
    12 from twisted.internet import interfaces, reactor
     13from twisted.internet import interfaces, reactor, endpoints
    1314from twisted.python import usage, reflect, threadpool
    1415from twisted.spread import pb
    15 from twisted.application import internet, service, strports
     16from twisted.application import internet, service
    1617
    1718
    1819class Options(usage.Options):
     
    2122    """
    2223    synopsis = "[web options]"
    2324
    24     optParameters = [["port", "p", None, "strports description of the port to "
    25                       "start the server on."],
    26                      ["logfile", "l", None, "Path to web CLF (Combined Log Format) log file."],
    27                      ["https", None, None, "Port to listen on for Secure HTTP."],
    28                      ["certificate", "c", "server.pem", "SSL certificate to use for HTTPS. "],
    29                      ["privkey", "k", "server.pem", "SSL certificate to use for HTTPS."],
     25    optParameters = [["logfile", "l", None, "Path to web CLF (Combined Log Format) log file."],
     26                     ["https", None, None, "Port to listen on for Secure HTTP. "
     27                      "DEPRECATED: use "
     28                      "'--port ssl:port:privateKey=pkey.pem:certKey=cert.pem'"],
     29                     ["certificate", "c", "server.pem", "SSL certificate to use for HTTPS. "
     30                      "DEPRECATED: use "
     31                      "'--port ssl:port:privateKey=pkey.pem:certKey=cert.pem'"],
     32                     ["privkey", "k", "server.pem", "SSL certificate to use for HTTPS. "
     33                      "DEPRECATED: use "
     34                      "'--port ssl:port:privateKey=pkey.pem:certKey=cert.pem'"],
    3035                     ]
    3136
    3237    optFlags = [["personal", "",
     
    5257        usage.Options.__init__(self)
    5358        self['indexes'] = []
    5459        self['root'] = None
     60        self.endpoints = []
    5561
    5662
     63    def addEndpoint(self, description, privateKey=None, certificate=None):
     64        """
     65        Add an endpoint according to the description
     66        """
     67        self.endpoints.append(
     68            _toEndpoint(description, privateKey=privateKey, certificate=certificate))
     69
     70
     71    def opt_port(self, description):
     72        """
     73        Add a specified endpoint. You can add multiple endpoints by specifying
     74        multiple --port options. For backwards compatibility, a bare TCP port number
     75        can be specified, but this is deprecated.
     76        [TCP Example: tcp:port]
     77        [SSL Example: ssl:port:privateKey=mycert.pem]
     78        The default value is: '--port tcp:8080'
     79        """
     80        self.addEndpoint(description)
     81
     82    opt_p = opt_port
     83
     84
    5785    def opt_index(self, indexName):
    5886        """
    5987        Add the name of a file used to check for directory indexes.
     
    169197        If no server port was supplied, select a default appropriate for the
    170198        other options supplied.
    171199        """
    172         if self['https']:
    173             try:
    174                 from twisted.internet.ssl import DefaultOpenSSLContextFactory
    175             except ImportError:
    176                 raise usage.UsageError("SSL support not installed")
    177         if self['port'] is None:
    178             if self['personal']:
     200        if self['personal']:
     201            if not self.endpoints:
    179202                path = os.path.expanduser(
    180203                    os.path.join('~', distrib.UserDirectory.userSocketName))
    181                 self['port'] = 'unix:' + path
    182             else:
    183                 self['port'] = 'tcp:8080'
     204                self.addEndpoint('unix:' + path)
     205        else:
     206            if not self.endpoints:
     207                self.addEndpoint('tcp:8080')
     208            if self['https']:
     209                try:
     210                    from twisted.internet.ssl import DefaultOpenSSLContextFactory
     211                except ImportError:
     212                    raise usage.UsageError("SSL support not installed")
     213                self.addEndpoint(self['https'], privateKey=self['privkey'], certificate=self['certificate'])
    184214
    185215
    186216
     217def _toEndpoint(description, privateKey=None, certificate=None):
     218    """
     219    Tries to guess whether a description is a bare TCP port or a endpoint.  If a
     220    bare port is specified and a certificate file is present, returns an
     221    SSL4ServerEndpoint and otherwise returns a TCP4ServerEndpoint.
     222    """
     223    try:
     224        port = int(description)
     225    except ValueError:
     226        return endpoints.serverFromString(reactor, description)
     227    warnings.warn(
     228        "Specifying plain ports and/or a certificate is deprecated since "
     229        "Twisted 11.0; use endpoint descriptions instead.",
     230        category=DeprecationWarning, stacklevel=3)
     231    if certificate:
     232        from twisted.internet.ssl import DefaultOpenSSLContextFactory
     233        ctx = DefaultOpenSSLContextFactory(privateKey, certificate)
     234        return endpoints.SSL4ServerEndpoint(reactor, port, ctx)
     235    return endpoints.TCP4ServerEndpoint(reactor, port)
     236
     237
     238
    187239def makePersonalServerFactory(site):
    188240    """
    189241    Create and return a factory which will respond to I{distrib} requests
     
    217269    site.displayTracebacks = not config["notracebacks"]
    218270
    219271    if config['personal']:
    220         personal = strports.service(
    221             config['port'], makePersonalServerFactory(site))
    222         personal.setServiceParent(s)
     272        siteFactory = makePersonalServerFactory(site)
    223273    else:
    224         if config['https']:
    225             from twisted.internet.ssl import DefaultOpenSSLContextFactory
    226             i = internet.SSLServer(int(config['https']), site,
    227                           DefaultOpenSSLContextFactory(config['privkey'],
    228                                                        config['certificate']))
    229             i.setServiceParent(s)
    230         strports.service(config['port'], site).setServiceParent(s)
     274        siteFactory = site
     275    for endpoint in config.endpoints:
     276        svc = internet.StreamServerEndpointService(endpoint, siteFactory)
     277        svc._raiseSynchronously = True
     278        svc.setServiceParent(s)
    231279
    232280    return s
  • twisted/web/test/test_tap.py

     
    1010from twisted.python.usage import UsageError
    1111from twisted.python.filepath import FilePath
    1212from twisted.internet.interfaces import IReactorUNIX
    13 from twisted.internet import reactor
     13from twisted.internet import reactor, endpoints, defer
    1414from twisted.python.threadpool import ThreadPool
    1515from twisted.trial.unittest import TestCase
    16 from twisted.application import strports
    1716
    1817from twisted.web.server import Site
    1918from twisted.web.static import Data, File
     
    2827
    2928application = object()
    3029
     30class SpyEndpoint(object):
     31    """
     32    SpyEndpoint remembers what factory it is told to listen with.
     33    """
     34    listeningWith = None
     35    def listen(self, factory):
     36        self.listeningWith = factory
     37        return defer.succeed(None)
     38
     39
    3140class ServiceTests(TestCase):
    3241    """
    3342    Tests for the service creation APIs in L{twisted.web.tap}.
     
    110119        self.assertIdentical(serverFactory.root.site, site)
    111120
    112121
     122    def test_dscMultipleEndpoints(self):
     123        """
     124        If one or more endpoints is included in the configuration passed to
     125        L{makeService}, a service for starting a server is constructed
     126        for each of them and attached to the returned service.
     127        """
     128        cleartext = SpyEndpoint()
     129        secure = SpyEndpoint()
     130        config = Options()
     131        config.endpoints = [cleartext, secure]
     132        service = makeService(config)
     133        service.privilegedStartService()
     134        service.startService()
     135        self.addCleanup(service.stopService)
     136        self.assertIsInstance(cleartext.listeningWith, Site)
     137        self.assertIsInstance(secure.listeningWith, Site)
     138
     139
     140    def test_dscBarePort(self):
     141        """
     142        For backwards compatibility, the I{--port} option supports the deprecated
     143        bare port option like '--port 8080'.
     144        """
     145        options = Options()
     146        options.parseOptions(['--port', '8080'])
     147        self.assertEqual(options.endpoints[0]._port, 8080)
     148
     149
    113150    def test_personalServer(self):
    114151        """
    115152        The I{--personal} option to L{makeService} causes it to return a
     
    140177        options.parseOptions(['--personal'])
    141178        path = os.path.expanduser(
    142179            os.path.join('~', UserDirectory.userSocketName))
    143         self.assertEqual(
    144             strports.parse(options['port'], None)[:2],
    145             ('UNIX', (path, None)))
     180        self.assertEqual(options.endpoints[0]._address, path)
    146181
    147182    if not IReactorUNIX.providedBy(reactor):
    148183        test_defaultPersonalPath.skip = (
     
    156191        """
    157192        options = Options()
    158193        options.parseOptions([])
    159         self.assertEqual(
    160             strports.parse(options['port'], None)[:2],
    161             ('TCP', (8080, None)))
     194        self.assertEqual(options.endpoints[0]._port, 8080)
    162195
    163196
    164197    def test_wsgi(self):