<commit_before>#!/usr/bin/env python

__all__ = ['HttpTargetServer']

import atexit, datetime, os, re, subprocess, sys, tempfile, time, warnings

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, os.pardir, "common"))
import utils

# --

def __runServer(httpbinPort=0, httpPort=0, sslPort=0):
    
    import twisted.internet
    import twisted.internet.ssl
    import twisted.web
    import twisted.web.static
    import twisted.web.wsgi
    
    sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
    import httpbin
    
    # -- generate self signed SSL certificate
    
    privateKey = tempfile.NamedTemporaryFile(suffix='.pem')
    subprocess.check_call(['openssl', 'genrsa'], stdout=privateKey, stderr=subprocess.PIPE)
    privateKey.flush()
    
    certificate = tempfile.NamedTemporaryFile(suffix='.pem')
    certificateSettings = tempfile.NamedTemporaryFile(suffix='.pem', mode='w+')
    certificateSettings.write("US\nST=CA\nL=Mountain View\n.\n.\n.\n%s\n.\n" % datetime.datetime.now().strftime('%Y%m%d'))
    certificateSettings.seek(0)
    subprocess.check_call(['openssl', 'req', '-new', '-x509', '-key', privateKey.name, '-days', '1000'], stdin=certificateSettings, stdout=certificate, stderr=subprocess.PIPE)
    certificate.flush()
    
    def printStrtupInfo():
        sys.stdout.write('''Testing server is running
	httpbin running on:          %d http://localhost:%d
	http redirect to https:      %d http://localhost:%d/redirect
	ssl self-signed certificate: %d https://localhost:%d/quickstart.png
''' % (httpbinPort, httpbinPort, httpPort, httpPort, sslPort, sslPort))
        sys.stdout.flush()
    
    twisted.internet.reactor.callWhenRunning(printStrtupInfo) 
    
    # -- setup endpoints
    
    httpbinEndpoint = twisted.web.wsgi.WSGIResource(twisted.internet.reactor, twisted.internet.reactor.getThreadPool(), httpbin.app)
    
    class LocalContent(twisted.web.resource.Resource):
        
        def getChild(self, name, request):
            if name == '':
                return self
            else:
                return twisted.web.resource.Resource.getChild(self, name, request)
        
        def render_GET(self, request):
            request.setHeader("content-type", "text/plain")
            return "This is the message you came here to see! Woo! Hoo!"
    
    localContentInstance = LocalContent()
    
    # -- setup the listeners
    
    httpbinListner = twisted.internet.reactor.listenTCP(
        port=httpbinPort, # port
        factory=twisted.web.server.Site(httpbinEndpoint)
    )
    httpbinPort = httpbinListner.getHost().port
    
    redirectListener = twisted.internet.reactor.listenTCP(
        port=httpPort, # port
        factory=twisted.web.server.Site(localContentInstance)
    )
    httpPort = redirectListener.getHost().port
    
    sslListener = twisted.internet.reactor.listenSSL(
        port=sslPort, # port
        factory=twisted.web.server.Site(localContentInstance),
        contextFactory = twisted.internet.ssl.DefaultOpenSSLContextFactory(privateKey.name, certificate.name)
    )
    sslPort = sslListener.getHost().port
    
    # -- add the local content
    
    localContentInstance.putChild('quickstart.png', twisted.web.static.File(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'quickstart.png')))
    localContentInstance.putChild('redirect',  twisted.web.util.Redirect('https://localhost:%d' % sslPort))
    
    # -- start the server
    
    twisted.internet.reactor.run()

# --

class HttpTargetServer(object):
    
    __serverProcess = None
    __serverOutput = None
    
    httpbinPort = None
    httpPort = None
    sslPort = None
    
    def __init__(self, httpbinPort=0, httpPort=0, sslPort=0, startupTimeout=5):
        '''Start a server, using subprocess to do it out-of-process'''<commit_msg>bumping the timeout on the testing http server<commit_after>#!/usr/bin/env python

__all__ = ['HttpTargetServer']

import atexit, datetime, os, re, subprocess, sys, tempfile, time, warnings

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, os.pardir, "common"))
import utils

# --

def __runServer(httpbinPort=0, httpPort=0, sslPort=0):
    
    import twisted.internet
    import twisted.internet.ssl
    import twisted.web
    import twisted.web.static
    import twisted.web.wsgi
    
    sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
    import httpbin
    
    # -- generate self signed SSL certificate
    
    privateKey = tempfile.NamedTemporaryFile(suffix='.pem')
    subprocess.check_call(['openssl', 'genrsa'], stdout=privateKey, stderr=subprocess.PIPE)
    privateKey.flush()
    
    certificate = tempfile.NamedTemporaryFile(suffix='.pem')
    certificateSettings = tempfile.NamedTemporaryFile(suffix='.pem', mode='w+')
    certificateSettings.write("US\nST=CA\nL=Mountain View\n.\n.\n.\n%s\n.\n" % datetime.datetime.now().strftime('%Y%m%d'))
    certificateSettings.seek(0)
    subprocess.check_call(['openssl', 'req', '-new', '-x509', '-key', privateKey.name, '-days', '1000'], stdin=certificateSettings, stdout=certificate, stderr=subprocess.PIPE)
    certificate.flush()
    
    def printStrtupInfo():
        sys.stdout.write('''Testing server is running
	httpbin running on:          %d http://localhost:%d
	http redirect to https:      %d http://localhost:%d/redirect
	ssl self-signed certificate: %d https://localhost:%d/quickstart.png
''' % (httpbinPort, httpbinPort, httpPort, httpPort, sslPort, sslPort))
        sys.stdout.flush()
    
    twisted.internet.reactor.callWhenRunning(printStrtupInfo) 
    
    # -- setup endpoints
    
    httpbinEndpoint = twisted.web.wsgi.WSGIResource(twisted.internet.reactor, twisted.internet.reactor.getThreadPool(), httpbin.app)
    
    class LocalContent(twisted.web.resource.Resource):
        
        def getChild(self, name, request):
            if name == '':
                return self
            else:
                return twisted.web.resource.Resource.getChild(self, name, request)
        
        def render_GET(self, request):
            request.setHeader("content-type", "text/plain")
            return "This is the message you came here to see! Woo! Hoo!"
    
    localContentInstance = LocalContent()
    
    # -- setup the listeners
    
    httpbinListner = twisted.internet.reactor.listenTCP(
        port=httpbinPort, # port
        factory=twisted.web.server.Site(httpbinEndpoint)
    )
    httpbinPort = httpbinListner.getHost().port
    
    redirectListener = twisted.internet.reactor.listenTCP(
        port=httpPort, # port
        factory=twisted.web.server.Site(localContentInstance)
    )
    httpPort = redirectListener.getHost().port
    
    sslListener = twisted.internet.reactor.listenSSL(
        port=sslPort, # port
        factory=twisted.web.server.Site(localContentInstance),
        contextFactory = twisted.internet.ssl.DefaultOpenSSLContextFactory(privateKey.name, certificate.name)
    )
    sslPort = sslListener.getHost().port
    
    # -- add the local content
    
    localContentInstance.putChild('quickstart.png', twisted.web.static.File(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'quickstart.png')))
    localContentInstance.putChild('redirect',  twisted.web.util.Redirect('https://localhost:%d' % sslPort))
    
    # -- start the server
    
    twisted.internet.reactor.run()

# --

class HttpTargetServer(object):
    
    __serverProcess = None
    __serverOutput = None
    
    httpbinPort = None
    httpPort = None
    sslPort = None
    
    def __init__(self, httpbinPort=0, httpPort=0, sslPort=0, startupTimeout=20):
        '''Start a server, using subprocess to do it out-of-process'''
{'fraction_non_alphanumeric': 0.12564234326824256, 'fraction_numerical': 0.0044964028776978415, 'mean_word_length': 3.1945043103448274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2760467', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2410, 'n_words': 428}