<commit_before>#!/usr/bin/env python
import os
import sys
import ssl
from tempfile import mkstemp
from optparse import OptionParser

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn, ForkingMixIn


__version__ = "1.0.2"


class SecureHTTPServer(HTTPServer):
    def __init__(self, server_address, RequestHandlerClass,
                 bind_and_activate=True, keyfile=None, certfile=None,
                 ssl_version=ssl.PROTOCOL_SSLv23):

        HTTPServer.__init__(self, server_address, RequestHandlerClass,
                            bind_and_activate)
        self.keyfile = keyfile
        self.certfile = certfile
        self.ssl_version = ssl_version

    def get_request(self):
        """Get the request and client address from the socket, and wraps the
        connection in a SSL stream.

        """

        socket, addr = self.socket.accept()
        stream = ssl.wrap_socket(socket, server_side=True,
                                 keyfile=self.keyfile, certfile=self.certfile,
                                 ssl_version=self.ssl_version)
        return stream, addr


class ThreadingSecureHTTPServer(ThreadingMixIn, SecureHTTPServer):
    pass


class ForkingSecureHTTPServer(ForkingMixIn, SecureHTTPServer):
    pass


class Handler(BaseHTTPRequestHandler):
    directory = None

    def do_GET(self):
        if self.path == "/":
            # Serve a special index page if no domain is given.
            body = "<h1>dotjs</h1>\n<p>dotjs is working!</p>\n"
            content_type = "text/html"
        else:
            body = self.build_body()
            content_type = "text/javascript"

        self.send_response(200, "OK")

        # Send appropiate CORS header if Origin was specified
        origin = self.detect_origin()
        if origin:
            self.send_header("Access-Control-Allow-Origin", origin)

        # Send the response body.
        self.send_header("Content-Type", content_type)
        self.send_header("Content-Length", len(body))
        self.end_headers()
        self.wfile.write(body)

    def build_body(self):
        """Combine script files basted on the path of the request. For a
        request for ``/gist.github.com.js``, tries to load
        ``gist.github.com.js`` as well as ``github.com.js`` and ``com.js``, in
        addition to the global ``default.js``.
        Returns the combined contents of the scripts found.

        """
        # Always include default.js
        files = [os.path.join(self.directory, "default.js")]

        # Find increasingly less specific files based on the request path.
        paths = self.path.replace("/", "").split(".")
        while paths:
            files.append(os.path.join(self.directory, ".".join(paths)))
            paths = paths[1:]

        # Combine the files found, if they exist.
        body = "// dotjs is working! //\n"
        for filename in files:
            if os.path.exists(filename):
                with open(filename, "r") as fp:
                    body += fp.read() + "\n"

        return body

    def detect_origin(self):
        """Inspect the Origin header to see if it matches the path.
        """
        origin = self.headers.get("Origin")
        search = self.path.replace("/", "")
        if search.endswith(".js"):
            search = search[:-3]

        if origin and self.path and origin.endswith(search):
            return origin


cert = """-----BEGIN CERTIFICATE-----
MI<KEY>
-----END CERTIFICATE-----
"""
key = """-----<KEY>
"""


def daemonize():
    if os.fork() != 0:
        os._exit(0)

    # Make this process a session leader.
    os.setsid()
<commit_msg>generate new certificate with subjectAltName<commit_after>#!/usr/bin/env python
import os
import sys
import ssl
from tempfile import mkstemp
from optparse import OptionParser

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn, ForkingMixIn


__version__ = "1.0.2"


class SecureHTTPServer(HTTPServer):
    def __init__(self, server_address, RequestHandlerClass,
                 bind_and_activate=True, keyfile=None, certfile=None,
                 ssl_version=ssl.PROTOCOL_SSLv23):

        HTTPServer.__init__(self, server_address, RequestHandlerClass,
                            bind_and_activate)
        self.keyfile = keyfile
        self.certfile = certfile
        self.ssl_version = ssl_version

    def get_request(self):
        """Get the request and client address from the socket, and wraps the
        connection in a SSL stream.

        """

        socket, addr = self.socket.accept()
        stream = ssl.wrap_socket(socket, server_side=True,
                                 keyfile=self.keyfile, certfile=self.certfile,
                                 ssl_version=self.ssl_version)
        return stream, addr


class ThreadingSecureHTTPServer(ThreadingMixIn, SecureHTTPServer):
    pass


class ForkingSecureHTTPServer(ForkingMixIn, SecureHTTPServer):
    pass


class Handler(BaseHTTPRequestHandler):
    directory = None

    def do_GET(self):
        if self.path == "/":
            # Serve a special index page if no domain is given.
            body = "<h1>dotjs</h1>\n<p>dotjs is working!</p>\n"
            content_type = "text/html"
        else:
            body = self.build_body()
            content_type = "text/javascript"

        self.send_response(200, "OK")

        # Send appropiate CORS header if Origin was specified
        origin = self.detect_origin()
        if origin:
            self.send_header("Access-Control-Allow-Origin", origin)

        # Send the response body.
        self.send_header("Content-Type", content_type)
        self.send_header("Content-Length", len(body))
        self.end_headers()
        self.wfile.write(body)

    def build_body(self):
        """Combine script files basted on the path of the request. For a
        request for ``/gist.github.com.js``, tries to load
        ``gist.github.com.js`` as well as ``github.com.js`` and ``com.js``, in
        addition to the global ``default.js``.
        Returns the combined contents of the scripts found.

        """
        # Always include default.js
        files = [os.path.join(self.directory, "default.js")]

        # Find increasingly less specific files based on the request path.
        paths = self.path.replace("/", "").split(".")
        while paths:
            files.append(os.path.join(self.directory, ".".join(paths)))
            paths = paths[1:]

        # Combine the files found, if they exist.
        body = "// dotjs is working! //\n"
        for filename in files:
            if os.path.exists(filename):
                with open(filename, "r") as fp:
                    body += fp.read() + "\n"

        return body

    def detect_origin(self):
        """Inspect the Origin header to see if it matches the path.
        """
        origin = self.headers.get("Origin")
        search = self.path.replace("/", "")
        if search.endswith(".js"):
            search = search[:-3]

        if origin and self.path and origin.endswith(search):
            return origin


cert = """-----BEGIN CERTIFICATE-----
<KEY>
BGJ9ZF<KEY>
"""
key = """-----<KEY>
-----END RSA PRIVATE KEY-----
"""


def daemonize():
    if os.fork() != 0:
        os._exit(0)

    # Make this process a session leader.
    os.setsid()

{'fraction_non_alphanumeric': 0.11163670766319773, 'fraction_numerical': 0.00391944857413164, 'mean_word_length': 2.170522707797772, 'pattern_counts': {'":': 2, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3965038', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2080, 'n_words': 579}