<commit_before>#!/usr/bin/env python2
__version__ = "0.0.1"

# Standard modules
import socket
import os
import signal
import errno
import sys
import mimetypes
import datetime
import time
import shutil

# Import correct config parser
if sys.version_info > (3, 0):
    import configparser
else:
    import ConfigParser

# Community modules (optional)
try:
    import magic
except ImportError:
    pass

class HttpHandler():

    version = "1.0" # HTTP version
    supported_methods = ["GET", "HEAD", "POST"]
    responses = {
        200: "OK",
        400: "Bad Request",
        403: "Forbidden",
        404: "Not Found",
        500: "Internal Server Error",
        501: "Not Implemented",
        505: "HTTP Version Not Supported"
    }
     # Populate MIME types dictionary
    if not mimetypes.inited: mimetypes.init() # try to read system mime.types
    extensions_map = mimetypes.types_map.copy()
    extensions_map.update({
        '': 'application/octet-stream', # Default
        '.py': 'text/plain',
        '.c': 'text/plain',
        '.h': 'text/plain',
        })

    def __init__(self, conn, addr, server):
        # Not using version for now
        self.conn = conn
        self.addr = addr
        self.server = server
        self.server.headers = True
        self.server.close_connection = True
        # To Do: Introduce error handling
        #if self.server.HTTP_VERSION == 1.0:
        #    self.keep_alive = False
        if self.server.HTTP_VERSION == 1.1:
            self.server.close_connection = False

        try:
            self.handle_conn()
        except socket.error as e:
            print "error"

    # Not used
    def buffered_readLine(self,socket):
        line = ""
        while True:
            c = socket.recv(1)
            if c != "\n":
                line += c
            elif c == "\n":
                break
        print "Line:" + line
        return line.strip("\r")

    def should_keep_alive(self, line):
        try:
            field, value = [x.strip() for x in line.split(":")]
            if field.lower() == "connection":
                if value.lower() == "close":
                    self.server.close_connection = True
                    print "Connection : close"
                    return True
                elif value.lower() == "keep-alive":
                    self.server.close_connection = False
                    print "Connection : keep-alive"
                    return True
            return False
        except:
            pass
            # Should be able to handle invalid headers

    def valid_http_version(self,version):
        if version[:5] != "HTTP/":
            #self.write_code(400)
            self.code = 400
            return False
        try:
            version_number = version.split("/",1)[1].split(".")
            print version_number
            if len(version_number) != 2:
                raise ValueError
            version_number = int(version_number[0]), int(version_number[1])
            if version_number[0] < 1 or version_number[1] < 0:
                raise ValueError

        except(ValueError, IndexError):
            self.server.close_connection = True
            self.code = 400
            #self.write_code(400)
            return False
        if version_number[0] > 1: # HTTP/2+
            self.code = 505
            #self.write_code(505) # Not supported
            return False
        return True

    def valid_http_method(self,method):
        print method
        if method not in self.supported_methods:
            self.code = 400
            return False
        return True

    def parse_request(self):
        request = self.conn.recv(self.server.REQ_BUFFSIZE).split("\r\n\r\n",1)
        # Check for body
        if len(request) >= 1: #Only header
            header = [x.strip() for x in request[0].split("\r\n")] # List of header fields
            firstline =  header[0].split()
            print firstline
            # Long method
            if len(firstline) == 3: # HTTP/1.0 AND HTTP/1.1
                method, path, version = firstline
                if self.valid_http_method(method) and self.valid_http_version(version):
                    if version == "HTTP/1.1" : self.server.close_connection = False
                    if version == "HTTP/1.0" : self.server.close_connection = True
                    for line in header[1:]:
                        self.should_keep_alive(line)
                    self.method = method
                    self.version = version
                    self.path = self.handle_path(path)
            elif len(firstline) == 2: #HTTP/0.9
                method, path = firstline
                self.version = "HTTP/0.9"
                self.server.close_connection = True
                if method == "GET":
                    self.method = method
                    self.path = self.handle_path(path)
                else:
                    self.code = 400
            else:
                self.server.close_connection = True
                self.code = 400
            self.request = [header]
        if len(request) == 2 and method == "POST": #Prepare body
            body = request[1]
            self.request.append(body)
        else:
            pass # Empty request

    # Handles current connection
    def handle_conn(self):
        request = None
        self.code = None
        try:
            self.conn.settimeout(None)
            #self.rfile = self.conn.makefile('rb', -1)
            self.wfile = self.conn.makefile('wb', 0)
            #time.sleep(5)
            self.parse_request()
            #time.sleep(5)
            if self.code == 200:
                self.write_code(200)
                self.handle_request(self.method,self.path,self.version)
            else:
                self.write_code(self.code)

        except:
            raise
            #self.wfile.seek(0)
            #self.wfile.turncate()
            self.write_code(500) # Internal server error

        finally:
            # Logging
            #if self.server.LOGGING: print("{0}:{1} - - [{2}] \"{3}\" {4} -".format(self.addr[0], self.addr[1], time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()), (self.request[0][0]), self.code))
            # Finalize files<commit_msg>fixe HTTP version handling<commit_after>#!/usr/bin/env python2
__version__ = "0.0.1"

# Standard modules
import socket
import os
import signal
import errno
import sys
import mimetypes
import datetime
import time
import shutil

# Import correct config parser
if sys.version_info > (3, 0):
    import configparser
else:
    import ConfigParser

# Community modules (optional)
try:
    import magic
except ImportError:
    pass

class HttpHandler():

    version = "1.0" # HTTP version
    supported_methods = ["GET", "HEAD", "POST"]
    responses = {
        200: "OK",
        400: "Bad Request",
        403: "Forbidden",
        404: "Not Found",
        500: "Internal Server Error",
        501: "Not Implemented",
        505: "HTTP Version Not Supported"
    }
     # Populate MIME types dictionary
    if not mimetypes.inited: mimetypes.init() # try to read system mime.types
    extensions_map = mimetypes.types_map.copy()
    extensions_map.update({
        '': 'application/octet-stream', # Default
        '.py': 'text/plain',
        '.c': 'text/plain',
        '.h': 'text/plain',
        })

    def __init__(self, conn, addr, server):
        # Not using version for now
        self.conn = conn
        self.addr = addr
        self.server = server
        self.server.headers = True
        self.server.close_connection = True
        # To Do: Introduce error handling
        #if self.server.HTTP_VERSION == 1.0:
        #    self.keep_alive = False
        if self.server.HTTP_VERSION == 1.1:
            self.version = "HTTP/1.1"
            self.server.close_connection = False

        try:
            self.handle_conn()
        except socket.error as e:
            print "error"

    # Not used
    def buffered_readLine(self,socket):
        line = ""
        while True:
            c = socket.recv(1)
            if c != "\n":
                line += c
            elif c == "\n":
                break
        print "Line:" + line
        return line.strip("\r")

    def should_keep_alive(self, line):
        try:
            field, value = [x.strip() for x in line.split(":")]
            if field.lower() == "connection":
                if value.lower() == "close":
                    self.server.close_connection = True
                    print "Connection : close"
                    return True
                elif value.lower() == "keep-alive":
                    self.server.close_connection = False
                    print "Connection : keep-alive"
                    return True
            return False
        except:
            pass
            # Should be able to handle invalid headers

    def valid_http_version(self,version):
        if version[:5] != "HTTP/":
            #self.write_code(400)
            self.code = 400
            return False
        try:
            version_number = version.split("/",1)[1].split(".")
            print version_number
            if len(version_number) != 2:
                raise ValueError
            version_number = int(version_number[0]), int(version_number[1])
            if version_number[0] < 1 or version_number[1] < 0:
                raise ValueError

        except(ValueError, IndexError):
            self.server.close_connection = True
            self.code = 400
            #self.write_code(400)
            return False
        if version_number[0] > 1: # HTTP/2+
            self.code = 505
            #self.write_code(505) # Not supported
            return False
        if version != "HTTP/1.1" and version != "HTTP/1.0":
            self.code = 505
            self.server.close_connection = True
            return False
        return True

    def valid_http_method(self,method):
        print method
        if method not in self.supported_methods:
            self.code = 400
            return False
        return True

    def parse_request(self):
        request = self.conn.recv(self.server.REQ_BUFFSIZE).split("\r\n\r\n",1)
        print request
        # Check for body
        if len(request) >= 1: #Only header
            header = [x.strip() for x in request[0].split("\r\n")] # List of header fields
            firstline =  header[0].split()
            print firstline
            # Long method
            if len(firstline) == 3: # HTTP/1.0 AND HTTP/1.1
                method, path, version = firstline
                if self.valid_http_method(method) and self.valid_http_version(version):
                    if version == "HTTP/1.1" : self.server.close_connection = False
                    if version == "HTTP/1.0" : self.server.close_connection = True
                    for line in header[1:]:
                        self.should_keep_alive(line)
                    self.method = method
                    self.version = version
                    self.path = self.handle_path(path)
            elif len(firstline) == 2: #HTTP/0.9
                method, path = firstline
                self.version = "HTTP/0.9"
                self.server.close_connection = True
                if method == "GET":
                    self.method = method
                    self.path = self.handle_path(path)
                else:
                    self.code = 400
            else:
                self.server.close_connection = True
                self.code = 400
            self.request = [header]
        if len(request) == 2 and self.method == "POST": #Prepare body
            body = request[1]
            self.request.append(body)
        else:
            pass # Empty request

    # Handles current connection
    def handle_conn(self):
        request = None
        self.code = None
        try:
            self.conn.settimeout(None)
            #self.rfile = self.conn.makefile('rb', -1)
            self.wfile = self.conn.makefile('wb', 0)
            #time.sleep(5)
            self.parse_request()
            #time.sleep(5)
            if self.code == 200:
                self.write_code(200)
                self.handle_request(self.method,self.path,self.version)
            else:
                self.write_code(self.code)

        except:
            raise
            #self.wfile.seek(0)
            #self.wfile.turncate()
            self.write_code(500) # Internal server error

        finally:
            # Logging
            if self.server.LOGGING: print("{0}:{1} - - [{2}] \"{3}\" {4} -".format(self.addr[0], self.addr[1], time.strftime("%d/%m/%Y %H:%M:%S", time.localtime()), (self.request[0][0]), self.code))
            # Finalize files
{'fraction_non_alphanumeric': 0.10829817158931083, 'fraction_numerical': 0.019143616190029692, 'mean_word_length': 1.5536711891460495, 'pattern_counts': {'":': 19, '<': 7, '<?xml version=': 0, '>': 9, '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': '2513272', 'n_tokens_mistral': 3704, 'n_tokens_neox': 3492, 'n_words': 898}