예제 #1
0
def create_cookies_got(cookies):
    import traffic.lib
    cookies_got = []
    for name, morsel in cookies.items():
        cookie = traffic.lib.get_or_create_cookie('got', name, morsel.value)
        cookies_got.append(cookie)
    return cookies_got
예제 #2
0
def load_cookies():
    cookies = http.cookies.SimpleCookie()
    with open("cookie", "r") as f:
        cookies.load(f.readline())
    result = {}
    for k, v in cookies.items():
        result[k] = v.value
    return result
예제 #3
0
    def update_cookies(self, cookies):
        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                dict.__setitem__(self.cookies, name, value)
            else:
                self.cookies[name] = value
예제 #4
0
    def update_cookies(self, cookies):
        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(self.cookies, name, value)
            else:
                self.cookies[name] = value
예제 #5
0
    def update_cookies(self, cookies):
        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(self.cookies, name, value)
            else:
                self.cookies[name] = value
예제 #6
0
파일: requests.py 프로젝트: CyboLabs/XdaPy
    def parse_cookies(cookie_str):
        """Parse the useful data from Set-Cookie

        Set-Cookie includes a lot of junk which is causing the cookie
        to be broken. Parse the useful data into a dict.
        """
        cookies = http.cookies.SimpleCookie(cookie_str)
        d = {}
        for c in cookies.items():
            d[c[0]] = c[1].value
        return d
예제 #7
0
 def set_session_cookies(self, browser, session):
     # quickly create a response so the fw sets the cookies, which we copy and explicitly set on selenium.
     response = Response()
     session.set_session_key(response)
     cookies = http.cookies.BaseCookie(', '.join(
         response.headers.getall('set-cookie')))
     for name, morsel in cookies.items():
         cookie = {'name': name, 'value': morsel.value}
         cookie.update(
             dict([(key, value) for key, value in morsel.items() if value]))
         browser.create_cookie(cookie)
예제 #8
0
파일: handler.py 프로젝트: jagguli/aioweb
    def cookies(self, cookies):
        _cookie = cookies
        if isinstance(cookies, dict):
            _cookies = http.cookies.SimpleCookie()
            for name, value in cookies.items():
                _cookies[name] = value
        else:
            assert isinstance(cookies, http.cookies.SimpleCookie), \
                "Cookies have to be http.cookies.SimpleCookie or dict"

        resp = self.response
        for cookie in _cookies.output(header='').split('\n'):
            resp.add_header('Set-Cookie', cookie.strip())
예제 #9
0
    def test_release(self):
        session = Session()
        resp = unittest.mock.Mock()
        resp.message.should_close = False

        cookies = resp.cookies = http.cookies.SimpleCookie()
        cookies['c1'] = 'cookie1'
        cookies['c2'] = 'cookie2'

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        session._release(resp, 1, (tr, proto))
        self.assertEqual(session._conns[1][0], (tr, proto))
        self.assertEqual(session.cookies, dict(cookies.items()))
예제 #10
0
    def test_release(self):
        session = Session()
        resp = unittest.mock.Mock()
        resp.message.should_close = False

        cookies = resp.cookies = http.cookies.SimpleCookie()
        cookies['c1'] = 'cookie1'
        cookies['c2'] = 'cookie2'

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        session._release(resp, 1, (tr, proto))
        self.assertEqual(session._conns[1][0], (tr, proto))
        self.assertEqual(session.cookies, dict(cookies.items()))
예제 #11
0
def _prepare_client(cookies):
    client = werkzeug.test.Client(gws.base.web.web_app.application,
                                  werkzeug.wrappers.BaseResponse)

    if cookies:
        for k, v in cookies.items():
            if not v:
                client.delete_cookie('localhost', k)
            elif isinstance(v, str):
                client.set_cookie('localhost', k, v)
            else:
                client.set_cookie('localhost', k, **v)

    return client
예제 #12
0
def alert_if_sensitive(sensitive_files,
                       ip=None,
                       scheme=None,
                       host=None,
                       nginx_path=None,
                       nginx_query_str=None,
                       referrer=None,
                       code=None,
                       size=None,
                       user_agent=None,
                       cookies=None,
                       **kwargs):
    if cookies is None:
        cookies = {}
    import utils
    import myadmin.lib
    if os.path.basename(nginx_path) in sensitive_files:
        filename = os.path.basename(nginx_path)
        # Don't alert if it's just me.
        is_admin = myadmin.lib.get_admin_cookie(DummyRequest(cookies))
        if is_admin:
            logging.warning(
                'Download of {} detected, but vistor seems to be me.'.format(
                    filename))
            return
        subject = 'Visitor downloaded {}!'.format(filename)
        url = '{}://{}{}'.format(scheme, host, nginx_path)
        if nginx_query_str and nginx_query_str != '-':
            url += '?' + nginx_query_str
        cookies_list = [
            '{}:\t{}'.format(name, morsel.value)
            for name, morsel in cookies.items()
        ]
        if cookies_list:
            cookies_str = '\n  ' + '\n  '.join(cookies_list)
        else:
            cookies_str = 'None'
        if referrer and referrer != '-':
            referrer_value = referrer
        else:
            referrer_value = None
        body = (
            'A visitor from IP address {0} accessed {1} (referrer: {2!r}).\n\n'
            'User agent: {3}\n\n'
            'Cookies received: {4}\n\n'
            'Response code: {5}, bytes served: {6}\n'.format(
                ip, url, referrer_value, user_agent, cookies_str, code, size))
        logging.warning(
            'Download of {} detected. Email alert sent.'.format(filename))
        utils.email_admin(subject, body)
    def do_GET(self):
        # super().handle()
        cookies = None
        print(self.headers)
        if 'Cookie' in self.headers:
            cookies = http.cookies.BaseCookie(self.headers['Cookie'])
            print(cookies)
        print(cookies.keys())

        for k, v in cookies.items():
            print(k, ":", v)
        self.send_response(200)  # 响应行
        self.send_header('Content-type', 'text/html;charset=utf-8')  # 响应头
        self.end_headers()  # 响应头结束
        self.wfile.write('<h1>这是来自服务器的请求</h1>'.encode('utf-8'))  # 响应数据体
예제 #14
0
    def test_release(self):
        session = Session(loop=self.loop)
        session._start_cleanup_task = unittest.mock.Mock()
        req = unittest.mock.Mock()
        resp = req.response = unittest.mock.Mock()
        resp.message.should_close = False

        cookies = resp.cookies = http.cookies.SimpleCookie()
        cookies['c1'] = 'cookie1'
        cookies['c2'] = 'cookie2'

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        session._release(req, 1, (tr, proto, 0))
        self.assertEqual(session._conns[1][0], (tr, proto, 0))
        self.assertEqual(session.cookies, dict(cookies.items()))
        self.assertTrue(session._start_cleanup_task.called)
예제 #15
0
    def update_cookies(self, cookies):
        """Update request cookies header."""
        if not cookies:
            return

        c = http.cookies.SimpleCookie()
        if hdrs.COOKIE in self.headers:
            c.load(self.headers.get(hdrs.COOKIE, ''))
            del self.headers[hdrs.COOKIE]

        for name, value in cookies.items():
            if isinstance(value, http.cookies.Morsel):
                c[value.key] = value.value
            else:
                c[name] = value

        self.headers[hdrs.COOKIE] = c.output(header='', sep=';').strip()
예제 #16
0
    def update_cookies(self, cookies):
        """Update shared cookies.

        Deprecated, use ClientSession instead.
        """
        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if PY_343:
                self.cookies[name] = value
            else:
                if isinstance(value, http.cookies.Morsel):
                    # use dict method because SimpleCookie class modifies value
                    dict.__setitem__(self.cookies, name, value)
                else:
                    self.cookies[name] = value
예제 #17
0
    def update_cookies(self, cookies):
        """Update shared cookies.

        Deprecated, use ClientSession instead.
        """
        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if PY_343:
                self.cookies[name] = value
            else:
                if isinstance(value, http.cookies.Morsel):
                    # use dict method because SimpleCookie class modifies value
                    dict.__setitem__(self.cookies, name, value)
                else:
                    self.cookies[name] = value
예제 #18
0
    def update_cookies(self, cookies):
        """Update request cookies header."""
        if not cookies:
            return

        c = http.cookies.SimpleCookie()
        if hdrs.COOKIE in self.headers:
            c.load(self.headers.get(hdrs.COOKIE, ''))
            del self.headers[hdrs.COOKIE]

        for name, value in cookies.items():
            if isinstance(value, http.cookies.Morsel):
                c[value.key] = value.value
            else:
                c[name] = value

        self.headers[hdrs.COOKIE] = c.output(header='', sep=';').strip()
예제 #19
0
    def test_release(self, m_time):
        m_time.time.return_value = 10

        conn = aiohttp.BaseConnector(share_cookies=True, loop=self.loop)
        conn._start_cleanup_task = unittest.mock.Mock()
        req = unittest.mock.Mock()
        resp = req.response = unittest.mock.Mock()
        resp.message.should_close = False

        cookies = resp.cookies = http.cookies.SimpleCookie()
        cookies['c1'] = 'cookie1'
        cookies['c2'] = 'cookie2'

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        conn._release(1, req, tr, proto)
        self.assertEqual(conn._conns[1][0], (tr, proto, 10))
        self.assertEqual(conn.cookies, dict(cookies.items()))
        self.assertTrue(conn._start_cleanup_task.called)
예제 #20
0
    def test_release(self, m_time):
        m_time.time.return_value = 10

        conn = aiohttp.BaseConnector(share_cookies=True, loop=self.loop)
        conn._start_cleanup_task = unittest.mock.Mock()
        req = unittest.mock.Mock()
        resp = req.response = unittest.mock.Mock()
        resp.message.should_close = False

        cookies = resp.cookies = http.cookies.SimpleCookie()
        cookies['c1'] = 'cookie1'
        cookies['c2'] = 'cookie2'

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        conn._release(1, req, tr, proto)
        self.assertEqual(conn._conns[1][0], (tr, proto, 10))
        self.assertEqual(conn.cookies, dict(cookies.items()))
        self.assertTrue(conn._start_cleanup_task.called)
예제 #21
0
    def update_cookies(self, cookies):
        """Update request cookies header."""
        c = http.cookies.SimpleCookie()
        if 'cookie' in self.headers:
            c.load(self.headers.get('cookie', ''))
            del self.headers['cookie']

        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(c, name, value)
            else:
                c[name] = value

        self.headers['cookie'] = c.output(header='', sep=';').strip()
예제 #22
0
파일: client.py 프로젝트: sah/tulip
    def update_cookies(self, cookies):
        """Update request cookies header."""
        c = http.cookies.SimpleCookie()
        if 'cookie' in self.headers:
            c.load(self.headers.get('cookie', ''))
            del self.headers['cookie']

        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(c, name, value)
            else:
                c[name] = value

        self.headers['cookie'] = c.output(header='', sep=';').strip()
예제 #23
0
    def handle_request(self, request):
        if 'Cookie' in request.headers:
            cookies = http.cookies.SimpleCookie(request.headers['Cookie'].value)
        elif 'HTTP_COOKIE' in request.headers:
            cookies = http.cookies.SimpleCookie(request.headers['HTTP_COOKIE'].value)
        else:
            request.client = client.Information(users.GUEST)
            return
        if SESSION_TOKEN_IDENTIFIER in cookies and cookies[SESSION_TOKEN_IDENTIFIER].value != session.SESSION_INVALIDATED:
            for k,v in cookies.items():
                logging.getLogger(__name__).debug('key: {}    value: {}'.format(k, v))
            db_result = session.validate_session(cookies[SESSION_TOKEN_IDENTIFIER].value)
            if db_result is not None:
                request.client = client.Information(db_result)
                return None
            new_cookie = http.cookies.SimpleCookie({SESSION_TOKEN_IDENTIFIER:session.SESSION_INVALIDATED})
            new_cookie[SESSION_TOKEN_IDENTIFIER]['expires'] = time.strftime("%a, %d-%b-%Y %Z %z", time.gmtime())
            return response.Redirect('/login?destination={}'.format(request.path)
                , code=303,  cookies=new_cookie)

        request.client = client.Information(users.GUEST)
예제 #24
0
    def update_cookies(self, cookies):
        """Update request cookies header."""
        if not cookies:
            return

        c = http.cookies.BaseCookie()
        if aiohttp.hdrs.COOKIE in self.headers:
            c.load(self.headers.get(aiohttp.hdrs.COOKIE, ''))
            del self.headers[aiohttp.hdrs.COOKIE]

        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(c, name, value)
            else:
                c[name] = value

        self.headers['Cookie'] = c.output(header='', sep=';', attrs={}).strip()
예제 #25
0
파일: asynchttp.py 프로젝트: Ouroboros/lldb
    def update_cookies(self, cookies):
        """Update request cookies header."""
        if not cookies:
            return

        c = http.cookies.BaseCookie()
        if aiohttp.hdrs.COOKIE in self.headers:
            c.load(self.headers.get(aiohttp.hdrs.COOKIE, ''))
            del self.headers[aiohttp.hdrs.COOKIE]

        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(c, name, value)
            else:
                c[name] = value

        self.headers['Cookie'] = c.output(header='', sep=';', attrs = {}).strip()
예제 #26
0
    def update_cookies(self, cookies):
        """Update request cookies header."""
        if not cookies:
            return

        c = http.cookies.SimpleCookie()
        if "COOKIE" in self.headers:
            c.load(self.headers.get("COOKIE", ""))
            del self.headers["COOKIE"]

        if isinstance(cookies, dict):
            cookies = cookies.items()

        for name, value in cookies:
            if isinstance(value, http.cookies.Morsel):
                # use dict method because SimpleCookie class modifies value
                dict.__setitem__(c, name, value)
            else:
                c[name] = value

        self.headers["COOKIE"] = c.output(header="", sep=";").strip()
예제 #27
0
파일: fixtures.py 프로젝트: smohaorg/reahl
 def log_in(self,
            browser=None,
            session=None,
            system_account=None,
            stay_logged_in=False):
     """Logs the user into the current webapp without having to navigate to a login page."""
     session = session or self.party_account_fixture.session
     browser = browser or self.driver_browser
     login_session = LoginSession.for_session(session)
     login_session.set_as_logged_in(
         system_account or self.party_account_fixture.system_account,
         stay_logged_in)
     # quickly create a response so the fw sets the cookies, which we copy and explicitly set on selenium.
     response = Response()
     session.set_session_key(response)
     cookies = http.cookies.BaseCookie(', '.join(
         response.headers.getall('set-cookie')))
     for name, morsel in cookies.items():
         cookie = {'name': name, 'value': morsel.value}
         cookie.update(
             dict([(key, value) for key, value in morsel.items() if value]))
         browser.create_cookie(cookie)
예제 #28
0
 def __init__(self,
              url=None,
              type=None,
              host=None,
              uri=None,
              cookies=None,
              remote_addr=None,
              meta=None):
     if url:
         type, host, uri, query, fragment = urllib.parse.urlsplit(
             url, 'http')
         if not uri:
             uri = '/'
         if query:
             uri += '?' + query
     self.type = type or 'http'
     self.host = host
     self.uri = uri
     if isinstance(cookies, http.cookies.BaseCookie):
         cookies = dict([(k, v.value) for k, v in list(cookies.items())])
     self.cookies = cookies or {}
     self.remote_addr = remote_addr
     self.meta = meta or {}
예제 #29
0
    def __init__(self, method, url, *,
                 params=None,
                 headers=None,
                 data=None,
                 cookies=None,
                 files=None,
                 auth=None,
                 encoding='utf-8',
                 version=(1, 1),
                 compress=None,
                 chunked=None):
        self.method = method.upper()
        self.encoding = encoding

        if isinstance(version, str):
            v = [l.strip() for l in version.split('.', 1)]
            version = int(v[0]), int(v[1])
        self.version = version

        scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
        if not netloc:
            raise ValueError()

        if not path:
            path = '/'
        else:
            path = urllib.parse.unquote(path)

        # check domain idna encoding
        try:
            netloc = netloc.encode('idna').decode('utf-8')
        except UnicodeError:
            raise ValueError('URL has an invalid label.')

        if '@' in netloc:
            authinfo, netloc = netloc.split('@', 1)
            if not auth:
                auth = authinfo.split(':', 1)
                if len(auth) == 1:
                    auth.append('')

        # extract host and port
        ssl = scheme == 'https'

        if ':' in netloc:
            netloc, port_s = netloc.split(':', 1)
            port = int(port_s)
        else:
            if ssl:
                port = http.client.HTTPS_PORT
            else:
                port = http.client.HTTP_PORT

        self.host = netloc
        self.port = port
        self.ssl = ssl

        # build url query
        if isinstance(params, dict):
            params = list(params.items())

        if data and self.method in self.GET_METHODS:
            # include data to query
            if isinstance(data, dict):
                data = data.items()
            params = list(itertools.chain(params or (), data))
            data = None

        if params:
            params = urllib.parse.urlencode(params)
            if query:
                query = '%s&%s' % (query, params)
            else:
                query = params

        # build path
        path = urllib.parse.quote(path)
        self.path = urllib.parse.urlunsplit(('', '', path, query, fragment))

        # headers
        self.headers = email.message.Message()
        if headers:
            if isinstance(headers, dict):
                headers = list(headers.items())

            for key, value in headers:
                self.headers[key] = value

        for hdr, val in self.DEFAULT_HEADERS.items():
            if hdr not in self.headers:
                self.headers[hdr] = val

        # host
        if 'host' not in self.headers:
            self.headers['Host'] = self.host

        # cookies
        if cookies:
            c = http.cookies.SimpleCookie()
            if 'cookie' in self.headers:
                c.load(self.headers.get('cookie', ''))
                del self.headers['cookie']

            for name, value in cookies.items():
                if isinstance(value, http.cookies.Morsel):
                    dict.__setitem__(c, name, value)
                else:
                    c[name] = value

            self.headers['cookie'] = c.output(header='', sep=';').strip()

        # auth
        if auth:
            if isinstance(auth, (tuple, list)) and len(auth) == 2:
                # basic auth
                self.headers['Authorization'] = 'Basic %s' % (
                    base64.b64encode(
                        ('%s:%s' % (auth[0], auth[1])).encode('latin1'))
                    .strip().decode('latin1'))
            else:
                raise ValueError("Only basic auth is supported")

        self._params = (chunked, compress, files, data, encoding)
예제 #30
0
 def __init__(self, cookies):
     self.COOKIES = {}
     for name, morsel in cookies.items():
         self.COOKIES[name] = morsel.value
예제 #31
0
    def __init__(self,
                 method,
                 url,
                 *,
                 params=None,
                 headers=None,
                 data=None,
                 cookies=None,
                 files=None,
                 auth=None,
                 encoding='utf-8',
                 version=(1, 1),
                 compress=None,
                 chunked=None):
        self.method = method.upper()
        self.encoding = encoding

        if isinstance(version, str):
            v = [l.strip() for l in version.split('.', 1)]
            version = int(v[0]), int(v[1])
        self.version = version

        scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url)
        if not netloc:
            raise ValueError()

        if not path:
            path = '/'
        else:
            path = urllib.parse.unquote(path)

        # check domain idna encoding
        try:
            netloc = netloc.encode('idna').decode('utf-8')
        except UnicodeError:
            raise ValueError('URL has an invalid label.')

        if '@' in netloc:
            authinfo, netloc = netloc.split('@', 1)
            if not auth:
                auth = authinfo.split(':', 1)
                if len(auth) == 1:
                    auth.append('')

        # extract host and port
        ssl = scheme == 'https'

        if ':' in netloc:
            netloc, port_s = netloc.split(':', 1)
            port = int(port_s)
        else:
            if ssl:
                port = http.client.HTTPS_PORT
            else:
                port = http.client.HTTP_PORT

        self.host = netloc
        self.port = port
        self.ssl = ssl

        # build url query
        if isinstance(params, dict):
            params = list(params.items())

        if data and self.method in self.GET_METHODS:
            # include data to query
            if isinstance(data, dict):
                data = data.items()
            params = list(itertools.chain(params or (), data))
            data = None

        if params:
            params = urllib.parse.urlencode(params)
            if query:
                query = '%s&%s' % (query, params)
            else:
                query = params

        # build path
        path = urllib.parse.quote(path)
        self.path = urllib.parse.urlunsplit(('', '', path, query, fragment))

        # headers
        self.headers = email.message.Message()
        if headers:
            if isinstance(headers, dict):
                headers = list(headers.items())

            for key, value in headers:
                self.headers[key] = value

        for hdr, val in self.DEFAULT_HEADERS.items():
            if hdr not in self.headers:
                self.headers[hdr] = val

        # host
        if 'host' not in self.headers:
            self.headers['Host'] = self.host

        # cookies
        if cookies:
            c = http.cookies.SimpleCookie()
            if 'cookie' in self.headers:
                c.load(self.headers.get('cookie', ''))
                del self.headers['cookie']

            for name, value in cookies.items():
                if isinstance(value, http.cookies.Morsel):
                    dict.__setitem__(c, name, value)
                else:
                    c[name] = value

            self.headers['cookie'] = c.output(header='', sep=';').strip()

        # auth
        if auth:
            if isinstance(auth, (tuple, list)) and len(auth) == 2:
                # basic auth
                self.headers['Authorization'] = 'Basic %s' % (base64.b64encode(
                    ('%s:%s' % (auth[0], auth[1])
                     ).encode('latin1')).strip().decode('latin1'))
            else:
                raise ValueError("Only basic auth is supported")

        self._params = (chunked, compress, files, data, encoding)