<commit_before>from __future__ import absolute_import

import chardet
import re
import six
import socket
from timeit import default_timer

if six.PY2:
    from cookielib import CookieJar
    class ConnectionRefusedError(Exception):
        # ConnectionRefusedError doesn't exist in python 2, so we'll 
        # define a dummy class to avoid a NameError
        pass
    str = unicode
else:
    from http.cookiejar import CookieJar

from geventhttpclient.useragent import UserAgent, CompatRequest, CompatResponse, ConnectionError

from locust import events
from locust.core import Locust


# Monkey patch geventhttpclient.useragent.CompatRequest so that Cookiejar works with Python >= 3.3
# More info: https://github.com/requests/requests/pull/871
CompatRequest.unverifiable = False


absolute_http_url_regexp = re.compile(r"^https?://", re.I)


class FastHttpLocust(Locust):
    """
    Represents an HTTP "user" which is to be hatched and attack the system that is to be load tested.
    
    The behaviour of this user is defined by the task_set attribute, which should point to a 
    :py:class:`TaskSet <locust.core.TaskSet>` class.
    
    This class creates a *client* attribute on instantiation which is an HTTP client with support 
    for keeping a user session between requests.
    """
    
    client = None
    """
    Instance of HttpSession that is created upon instantiation of Locust. 
    The client support cookies, and therefore keeps the session between HTTP requests.
    """
    
    def __init__(self):
        super(FastHttpLocust, self).__init__()
        if self.host is None:
            raise LocustError("You must specify the base host. Either in the host attribute in the Locust class, or on the command line using the --host option.")
        
        self.client = FastHttpSession(base_url=self.host)


class LocustErrorResponse(object):
    content = None
    def __init__(self, error):
        self.error = error
        self.status_code = 0


class FastHttpSession(object):
    def __init__(self, base_url):
        self.base_url = base_url
        self.cookiejar = CookieJar()
        self.client = LocustUserAgent(max_retries=1, cookiejar=self.cookiejar)
    
    def _build_url(self, path):
        """ prepend url with hostname unless it's already an absolute URL """
        if absolute_http_url_regexp.match(path):<commit_msg>Check that the given FastHttpLocust.host is valid and doesn't contain a trailing slash<commit_after>from __future__ import absolute_import

import chardet
import re
import six
import socket
from timeit import default_timer

if six.PY2:
    from cookielib import CookieJar
    class ConnectionRefusedError(Exception):
        # ConnectionRefusedError doesn't exist in python 2, so we'll 
        # define a dummy class to avoid a NameError
        pass
    str = unicode
else:
    from http.cookiejar import CookieJar

from geventhttpclient.useragent import UserAgent, CompatRequest, CompatResponse, ConnectionError

from locust import events
from locust.core import Locust
from locust.exception import LocustError


# Monkey patch geventhttpclient.useragent.CompatRequest so that Cookiejar works with Python >= 3.3
# More info: https://github.com/requests/requests/pull/871
CompatRequest.unverifiable = False


absolute_http_url_regexp = re.compile(r"^https?://", re.I)


class FastHttpLocust(Locust):
    """
    Represents an HTTP "user" which is to be hatched and attack the system that is to be load tested.
    
    The behaviour of this user is defined by the task_set attribute, which should point to a 
    :py:class:`TaskSet <locust.core.TaskSet>` class.
    
    This class creates a *client* attribute on instantiation which is an HTTP client with support 
    for keeping a user session between requests.
    """
    
    client = None
    """
    Instance of HttpSession that is created upon instantiation of Locust. 
    The client support cookies, and therefore keeps the session between HTTP requests.
    """
    
    def __init__(self):
        super(FastHttpLocust, self).__init__()
        if self.host is None:
            raise LocustError("You must specify the base host. Either in the host attribute in the Locust class, or on the command line using the --host option.")
        if not re.match(r"^https?://[^/]+$", self.host, re.I):
            raise LocustError("Invalid host (`%s`). The specified host string must be a base URL without a trailing slash. E.g. http://example.org" % self.host)
        
        self.client = FastHttpSession(base_url=self.host)


class LocustErrorResponse(object):
    content = None
    def __init__(self, error):
        self.error = error
        self.status_code = 0


class FastHttpSession(object):
    def __init__(self, base_url):
        self.base_url = base_url
        self.cookiejar = CookieJar()
        self.client = LocustUserAgent(max_retries=1, cookiejar=self.cookiejar)
    
    def _build_url(self, path):
        """ prepend url with hostname unless it's already an absolute URL """
        if absolute_http_url_regexp.match(path):
{'fraction_non_alphanumeric': 0.07227254824734147, 'fraction_numerical': 0.0035447026388341868, 'mean_word_length': 3.278854254422915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '6508960', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1394, 'n_words': 520}