<commit_before># vim:nowrap:textwidth=0

import os, stat, errno, mimetypes, sys, traceback, pickle

from sheared.protocol import basic
from sheared.protocol import http
from sheared.internet import shocket
from sheared.python import fdpass

class HTTPReply:
    def __init__(self, version, transport):
        self.transport = transport

        self.status = 200
        self.version = version
        
        self.headers = http.HTTPHeaders()
        self.headers.addHeader('Date', str(http.HTTPDateTime()))

        self.decapitated = 0

    def setStatusCode(self, status):
        self.status = status

    def sendHead(self):
        assert not self.decapitated
        self.decapitated = 1

        if self.version == (0,9):
            return

        self.transport.write('HTTP/%d.%d ' % self.version)
        reason = http.http_reason.get(self.status, 'Unknown Status')
        self.transport.write('%d %s\r\n' % (self.status, reason))

        for item in self.headers.items():
            self.transport.write('%s: %s\r\n' % item)

        self.transport.write('\r\n')

    def send(self, data):
        if not self.decapitated:
            self.sendHead()
        self.transport.write(data)

    def sendErrorPage(self, status):
        self.setStatusCode(status)
        self.headers.setHeader('Content-Type', 'text/plain')
        self.sendHead()
        self.send("""I am terribly sorry, but an error occured while processing your request.\n""")
        self.done()

    def done(self):
        if not self.transport.closed:
            self.transport.close()

class HTTPServerFactory(basic.ProtocolFactory):
    def __init__(self, reactor, server):
        basic.ProtocolFactory.__init__(self, reactor, server)
        
        self.hosts = { }
        self.default_host = None

    def addVirtualHost(self, name, vhost):
        self.hosts[name] = vhost

    def setDefaultHost(self, name):
        self.default_host = name

class HTTPServer(basic.LineProtocol):
    def __init__(self, reactor, transport):
        basic.LineProtocol.__init__(self, reactor, transport, '\n')
        self.where = 'request-line'
        self.collected = ''

    def handle(self, request, reply):
        try:
            try:
                if request.uri[0]:
                    reply.sendErrorPage(http.HTTP_FORBIDDEN)

                if request.headers.has_key('Host'):
                    vhost = self.factory.hosts.get(request.headers['Host'], None)
                else:
                    vhost = None
                if vhost is None and self.factory.default_host:
                    vhost = self.factory.hosts[self.factory.default_host]

                if vhost:
                    vhost.handle(request, reply, request.uri[2])
                else:
                    reply.sendErrorPage(http.HTTP_NOT_FOUND)
            except:
                if not self.reply.decapitated and not self.reply.transport.closed:
                    self.reply.sendErrorPage(http.HTTP_INTERNAL_SERVER_ERROR)
                raise
        finally:
            self.reply.done()

    def receivedLine(self, line):
        self.collected = self.collected + line

        if self.where == 'request-line':
            self.request = http.HTTPRequestLine(line.strip())
            self.collected = ''
            self.where = 'headers'
            if self.request.version == (0,9):
                self.receivedLine('\r\n')
    
        elif self.where == 'headers':
            if line == '\r\n':
                self.request.headers = http.HTTPHeaders(self.collected)
                self.reply = HTTPReply(self.request.version, self.transport)
                self.handle(self.request, self.reply)

    def lastLineReceived(self):
        pass

class HTTPSubServerAdapter:
    def __init__(self, reactor, path):
        self.reactor = reactor
        self.path = path

    def handle(self, request, reply, subpath):
        client = shocket.UNIXClient(self.reactor, self.path, None)
        transport = client.connect()
        fdpass.send(transport.fileno(), reply.transport.fileno(), pickle.dumps(reply.transport.other))
        pickle.dump((request, subpath), transport)
        transport.close()

class HTTPSubServer(HTTPServer):
    def run(self):
        for i in range(3):
            try:
                sock, addr = fdpass.recv(self.transport.fileno())
                break
            except:
                pass
        addr = pickle.loads(addr)

        data = ''
        read = None
        while not read == '':
            read = self.transport.read()
            data = data + read
        self.transport.close()

        self.request, subpath = pickle.loads(data)
        self.transport = self.reactor.createTransport(sock, addr)
        self.reply = HTTPReply(self.request.version, self.transport)
        self.handle(self.request, self.reply)

class VirtualHost:
    def __init__(self):
        self.bindings = []

    def handle(self, request, reply, subpath):
        for root, thing in self.bindings:
            if path == root or path.startswith(root + '/'):
                sub = path[ len(root) - 1 : ]
                thing.handle(request, reply, sub)
        reply.sendErrorPage(http.HTTP_NOT_FOUND)<commit_msg>Always reply with HTTP version 0.9 or 1.0.<commit_after># vim:nowrap:textwidth=0

import os, stat, errno, mimetypes, sys, traceback, pickle

from sheared.protocol import basic
from sheared.protocol import http
from sheared.internet import shocket
from sheared.python import fdpass

class HTTPReply:
    def __init__(self, version, transport):
        self.transport = transport

        self.status = 200

        if version[0] == 0:
            self.version = 0, 9
        else:
            self.version = 1, 0
        
        self.headers = http.HTTPHeaders()
        self.headers.addHeader('Date', str(http.HTTPDateTime()))

        self.decapitated = 0

    def setStatusCode(self, status):
        self.status = status

    def sendHead(self):
        assert not self.decapitated
        self.decapitated = 1

        if self.version == (0,9):
            return

        self.transport.write('HTTP/%d.%d ' % self.version)
        reason = http.http_reason.get(self.status, 'Unknown Status')
        self.transport.write('%d %s\r\n' % (self.status, reason))

        for item in self.headers.items():
            self.transport.write('%s: %s\r\n' % item)

        self.transport.write('\r\n')

    def send(self, data):
        if not self.decapitated:
            self.sendHead()
        self.transport.write(data)

    def sendErrorPage(self, status):
        self.setStatusCode(status)
        self.headers.setHeader('Content-Type', 'text/plain')
        self.sendHead()
        self.send("""I am terribly sorry, but an error occured while processing your request.\n""")
        self.done()

    def done(self):
        if not self.transport.closed:
            self.transport.close()

class HTTPServerFactory(basic.ProtocolFactory):
    def __init__(self, reactor, server):
        basic.ProtocolFactory.__init__(self, reactor, server)
        
        self.hosts = { }
        self.default_host = None

    def addVirtualHost(self, name, vhost):
        self.hosts[name] = vhost

    def setDefaultHost(self, name):
        self.default_host = name

class HTTPServer(basic.LineProtocol):
    def __init__(self, reactor, transport):
        basic.LineProtocol.__init__(self, reactor, transport, '\n')
        self.where = 'request-line'
        self.collected = ''

    def handle(self, request, reply):
        try:
            try:
                if request.uri[0]:
                    reply.sendErrorPage(http.HTTP_FORBIDDEN)

                if request.headers.has_key('Host'):
                    vhost = self.factory.hosts.get(request.headers['Host'], None)
                else:
                    vhost = None
                if vhost is None and self.factory.default_host:
                    vhost = self.factory.hosts[self.factory.default_host]

                if vhost:
                    vhost.handle(request, reply, request.uri[2])
                else:
                    reply.sendErrorPage(http.HTTP_NOT_FOUND)
            except:
                if not self.reply.decapitated and not self.reply.transport.closed:
                    self.reply.sendErrorPage(http.HTTP_INTERNAL_SERVER_ERROR)
                raise
        finally:
            self.reply.done()

    def receivedLine(self, line):
        self.collected = self.collected + line

        if self.where == 'request-line':
            self.request = http.HTTPRequestLine(line.strip())
            self.collected = ''
            self.where = 'headers'
            if self.request.version == (0,9):
                self.receivedLine('\r\n')
    
        elif self.where == 'headers':
            if line == '\r\n':
                self.request.headers = http.HTTPHeaders(self.collected)
                self.reply = HTTPReply(self.request.version, self.transport)
                self.handle(self.request, self.reply)

    def lastLineReceived(self):
        pass

class HTTPSubServerAdapter:
    def __init__(self, reactor, path):
        self.reactor = reactor
        self.path = path

    def handle(self, request, reply, subpath):
        client = shocket.UNIXClient(self.reactor, self.path, None)
        transport = client.connect()
        fdpass.send(transport.fileno(), reply.transport.fileno(), pickle.dumps(reply.transport.other))
        pickle.dump((request, subpath), transport)
        transport.close()

class HTTPSubServer(HTTPServer):
    def run(self):
        for i in range(3):
            try:
                sock, addr = fdpass.recv(self.transport.fileno())
                break
            except:
                pass
        else:
            raise
        addr = pickle.loads(addr)

        data = ''
        read = None
        while not read == '':
            read = self.transport.read()
            data = data + read
        self.transport.close()

        self.request, subpath = pickle.loads(data)
        self.transport = self.reactor.createTransport(sock, addr)
        self.reply = HTTPReply(self.request.version, self.transport)
        self.handle(self.request, self.reply)

class VirtualHost:
    def __init__(self):
        self.bindings = []

    def handle(self, request, reply, subpath):
        for root, thing in self.bindings:
            if path == root or path.startswith(root + '/'):
                sub = path[ len(root) - 1 : ]
                thing.handle(request, reply, sub)
        reply.sendErrorPage(http.HTTP_NOT_FOUND)
{'fraction_non_alphanumeric': 0.11061905210590785, 'fraction_numerical': 0.0035805144633939507, 'mean_word_length': 1.96232207647223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5750582', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2976, 'n_words': 658}