<filename>ctfpwn/exploits/http.py<gh_stars>0
import requests
import random


USER_AGENTS = ['Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0',
               'Mozilla/5.0 (X11; Linux i686; rv:30.0) Gecko/20100101 Firefox/30.0',
               'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
               'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6',
               'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.39 (KHTML, like Gecko) Version/9.0 Safari/601.1.39']


class HttpClient(requests.Session):
    """
    Extends requests.Session with base url support
    and random user agents

    >>> c = HttpClient('http://www.example.com/')
    >>> c.get('/').url
    'http://www.example.com/'
    >>> c.get('/foo').url
    'http://www.example.com/foo'
    >>> c.get('bar').url
    'http://www.example.com/bar'
    >>> c.get('http://www.example.de/foo').url
    'http://www.example.de/foo'
    >>> HttpClient('http://127.0.0.1/', 'example.com')
    """
    def __init__(self, base, host=None, *args, **kwargs):
        super(HttpClient, self).__init__(*args, **kwargs)
        self.headers['User-Agent'] = random.choice(USER_AGENTS)
        if host:
            self.headers['Host'] = host
        self.base_url = base

        if not self.base_url.endswith('/'):
            self.base_url += '/'

    def request(self, method, url, *args, **kwargs):
        if '://' not in url:
            if url.startswith('/'):
                url = url[1:]
            url = self.base_url + url

        return super(HttpClient, self).request(method=method, url=url, *args, **kwargs)
