Esempio n. 1
0
 def load_cookie(self, resp):
     cookie = http_cookies.SimpleCookie()
     try:
         cookie.load(resp.headers['Set-Cookie'])
         return cookie
     except KeyError:
         return None
Esempio n. 2
0
 def _refresh_cookie(self, c, delta):
     """
         Takes a cookie string c and a time delta in seconds, and returns
         a refreshed cookie string.
     """
     try:
         c = Cookie.SimpleCookie(str(c))
     except Cookie.CookieError:
         raise ValueError("Invalid Cookie")
     for i in c.values():
         if "expires" in i:
             d = parsedate_tz(i["expires"])
             if d:
                 d = mktime_tz(d) + delta
                 i["expires"] = formatdate(d)
             else:
                 # This can happen when the expires tag is invalid.
                 # reddit.com sends a an expires tag like this: "Thu, 31 Dec
                 # 2037 23:59:59 GMT", which is valid RFC 1123, but not
                 # strictly correct according to the cookie spec. Browsers
                 # appear to parse this tolerantly - maybe we should too.
                 # For now, we just ignore this.
                 del i["expires"]
     ret = c.output(header="").strip()
     if not ret:
         raise ValueError("Invalid Cookie")
     return ret
    def test_headers():
        webdriver = instantiate_webdriver(webdriver_class)
        # TODO: Add more cookie examples with additional fields, such as
        # expires, path, comment, max-age, secure, version, httponly
        domain = get_tld(echo_header_server) if webdriver.name == 'phantomjs' else None
        cookies = (
            {'domain': domain, 'name': 'hello', 'value': 'world'},
            {'domain': domain, 'name': 'another', 'value': 'cookie'}
        )
        # Open the server URL with the WebDriver instance initially so we can
        # set custom cookies
        webdriver.get(echo_header_server)
        for cookie in cookies:
            webdriver.add_cookie(cookie)
        response = webdriver.request('GET', echo_header_server, headers={'extra': 'header'},
                                     cookies={'extra': 'cookie'})
        sent_headers = requests.structures.CaseInsensitiveDict(json.loads(response.headers['echo']))

        # Simply assert that the User-Agent isn't requests' default one, which
        # means that it and the rest of the headers must have been overwritten
        assert sent_headers['user-agent'] != requests.utils.default_user_agent()
        # Check if the additional header was sent as well
        assert 'extra' in sent_headers and sent_headers['extra'] == 'header'
        cookies = http_cookies.SimpleCookie()
        # Python 2's Cookie module expects a string object, not Unicode
        cookies.load(sent_headers['cookie'] if six.PY3 else sent_headers['cookie'].encode('ASCII'))
        assert 'hello' in cookies and cookies['hello'].value == 'world'
        assert 'another' in cookies and cookies['another'].value == 'cookie'
        # Check if the additional cookie was sent as well
        assert 'extra' in cookies and cookies['extra'].value == 'cookie'

        webdriver.quit()
Esempio n. 4
0
def get_cookie(res, cookie_name):
    headers = res.headers.to_list()
    cookie_string = [
        h for h in headers if (h[0] == 'Set-Cookie' and cookie_name in h[1])
    ][0][1]
    cookie = http_cookies.SimpleCookie(cookie_string)
    access_granted_cookie = cookie[list(cookie.keys())[0]].value
    return access_granted_cookie
Esempio n. 5
0
 def store_cookies(self, resp):
     cookies = http_cookies.SimpleCookie()
     try:
         cookies.load(resp.headers['Set-Cookie'])
         for key in cookies:
             self.cookies[key] = cookies[key].value
     except:
         pass
Esempio n. 6
0
def parse(curl_command):
    method = "get"

    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    base_indent = " " * 4
    data_token = ''
    if parsed_args.X.lower() == 'post':
        method = 'post'
    post_data = parsed_args.data or parsed_args.data_binary
    if post_data:
        method = 'post'
        try:
            post_data_json = json.loads(post_data)
        except ValueError:
            post_data_json = None

        # If we found JSON and it is a dict, pull it apart. Otherwise, just leave as a string
        if post_data_json and isinstance(post_data_json, dict):
            post_data = dict_to_pretty_string(post_data_json)
        else:
            post_data = "'{}'".format(post_data)

        data_token = '{}data={},\n'.format(base_indent, post_data)

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()

    for curl_header in parsed_args.header:
        if curl_header.startswith(':'):
            occurrence = [m.start() for m in re.finditer(':', curl_header)]
            header_key, header_value = curl_header[:occurrence[1]], curl_header[occurrence[1] + 1:]
        else:
            header_key, header_value = curl_header.split(":", 1)

        if header_key.lower() == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    result = """requests.{method}("{url}",
{data_token}{headers_token},
{cookies_token},{security_token}
)""".format(
        method=method,
        url='https://github.com/search',
        data_token=data_token,
        headers_token="{}headers={}".format(base_indent, dict_to_pretty_string(quoted_headers)),
        cookies_token="{}cookies={}".format(base_indent, dict_to_pretty_string(cookie_dict)),
        security_token="\n%sverify=False" % base_indent if parsed_args.insecure else ""
    )
    headerNCookie={}
    headerNCookie['headers']=quoted_headers
    headerNCookie['cookies']=cookie_dict
    return headerNCookie
Esempio n. 7
0
 def handle_response(self, f):
     for i in f.response.headers.get_all("set-cookie"):
         # FIXME: We now know that Cookie.py screws up some cookies with
         # valid RFC 822/1123 datetime specifications for expiry. Sigh.
         c = http_cookies.SimpleCookie(str(i))
         for m in c.values():
             k = self.ckey(m, f)
             if self.domain_match(f.request.host, k[0]):
                 self.jar[k] = m
Esempio n. 8
0
    def set_cookie(self, name, value = '', expires = 0, path = '/', domain = '', secure = False, http_only = False):
        cook                   = http_cookies.SimpleCookie()
        cook[name]             = value
        cook[name]['expires']  = expires
        cook[name]['path']     = path
        cook[name]['domain']   = domain
        cook[name]['secure']   = secure
        cook[name]['httponly'] = http_only

        self.send_header('Set-Cookie', cook.output(header = ''))
Esempio n. 9
0
    def _cookie_replace(self, message):
        """Replace $COOKIE in a message.

        With cookie data from set-cookie in the prior request.
        """
        response_cookies = self.prior.response['set-cookie']
        cookies = http_cookies.SimpleCookie()
        cookies.load(response_cookies)
        cookie_string = cookies.output(attrs=[], header='', sep=',').strip()
        return message.replace('$COOKIE', cookie_string)
def http_parse_cookies(headers):
    if 'Cookie' not in headers:
        return {}

    try:
        cookies = Cookie.SimpleCookie()
        cookies.load(native_str(headers["Cookie"]))
        return dict((key, name.value) for key, name in six.iteritems(cookies))
    except Exception:
        return {}
Esempio n. 11
0
def parse(curl_command):
    method = "get"

    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    base_indent = " " * 4
    data_token = ''
    post_data = parsed_args.data or parsed_args.data_binary
    if post_data:
        method = 'post'
        try:
            post_data_json = json.loads(post_data)
        except ValueError:
            post_data_json = None

        # If we found JSON and it is a dict, pull it apart. Otherwise, just leave as a string
        if post_data_json and isinstance(post_data_json, dict):
            post_data = dict_to_pretty_string(post_data_json)
        else:
            post_data = "'{}'".format(post_data)

        data_token = '{}data={},\n'.format(base_indent, post_data)

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()
    for curl_header in parsed_args.header:
        header_key, header_value = curl_header.split(":", 1)

        if header_key.lower() == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    result = """requests.{method}("{url}",
{data_token}{headers_token},
{cookies_token},
)""".format(
        method=method,
        url=parsed_args.url,
        data_token=data_token,
        headers_token="{}headers={}".format(
            base_indent, dict_to_pretty_string(quoted_headers)),
        cookies_token="{}cookies={}".format(
            base_indent, dict_to_pretty_string(cookie_dict)),
    )
    # print result
    if method == "post":
        return parsed_args.url, method, ''.join(post_data).strip(
            "'"), quoted_headers, cookie_dict
    else:
        return parsed_args.url, method, None, quoted_headers, cookie_dict
Esempio n. 12
0
def get_cookie_value(cookies, name):
    '''
    Returns cookie value by its name. None if no such value.
    :param cookies: str: cookies raw data
    :param name: str: cookies key
    '''
    cookie_parser = http_cookies.SimpleCookie()
    cookie_parser.load(str(cookies))
    try:
        return cookie_parser[name].value
    except KeyError:
        return None
Esempio n. 13
0
 def _cookie_replacer(self, match):
     """Replace a regex match with the cookie of a previous response."""
     case = match.group('case')
     if case:
         referred_case = self.history[case]
     else:
         referred_case = self.prior
     response_cookies = referred_case.response['set-cookie']
     cookies = http_cookies.SimpleCookie()
     cookies.load(response_cookies)
     cookie_string = cookies.output(attrs=[], header='', sep=',').strip()
     return cookie_string
Esempio n. 14
0
def parse_cookies_and_headers(origin_headers):
    """:return: tuple. type is (OrderedDict(), OrderedDict())"""
    cookies = OrderedDict()
    headers = CaseInsensitiveDict()
    for curl_header in origin_headers:
        header_key, header_value = curl_header.split(':', 1)

        if header_key.lower() == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookies[key] = cookie[key].value
        else:
            headers[header_key] = header_value.strip()
    return cookies, headers
Esempio n. 15
0
def findCookieData(cookieName):
    " return value of cookie or None is not set, port of lib/cheapcgi.c:findCookieData "
    global cookies
    if not cookies:
        if "HTTP_COOKIE" in os.environ:
            cookies = http_cookies.SimpleCookie(os.environ["HTTP_COOKIE"])
        else:
            cookies = {}

    # unlike cheapcgi, Python does not even allow duplicate cookies, so no need to handle this case
    cookie = cookies.get(cookieName)
    if cookie:
        return cookie.value

    return None
Esempio n. 16
0
File: api.py Progetto: xorllc/uncurl
def parse(curl_command):
    method = "get"

    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    base_indent = " " * 4
    data_token = ''
    if parsed_args.X.lower() == 'post':
        method = 'post'
    post_data = parsed_args.data or parsed_args.data_binary
    if post_data:
        method = 'post'

        post_data = "'{}'".format(post_data)
        data_token = '{}data={},\n'.format(base_indent, post_data)

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()

    for curl_header in parsed_args.header:
        if curl_header.startswith(':'):
            occurrence = [m.start() for m in re.finditer(':', curl_header)]
            header_key, header_value = curl_header[:occurrence[
                1]], curl_header[occurrence[1] + 1:]
        else:
            header_key, header_value = curl_header.split(":", 1)

        if header_key.lower() == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    result = """requests.{method}("{url}",
{data_token}{headers_token},
{cookies_token},{security_token}
)""".format(method=method,
            url=parsed_args.url,
            data_token=data_token,
            headers_token="{}headers={}".format(
                base_indent, dict_to_pretty_string(quoted_headers)),
            cookies_token="{}cookies={}".format(
                base_indent, dict_to_pretty_string(cookie_dict)),
            security_token="\n%sverify=False" %
            base_indent if parsed_args.insecure else "")
    return result
Esempio n. 17
0
def parse_context(curl_command):
    method = "get"

    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    post_data = parsed_args.data or parsed_args.data_binary or parsed_args.data_urlencoded or parsed_args.form
    if post_data:
        method = 'post'

    if parsed_args.X:
        method = parsed_args.X.lower()

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()

    if len(parsed_args.data_urlencoded) > 0 :
        data_dict = OrderedDict()
        for curl_param in post_data :
            param_header, param_value = curl_param.split("=",1)
            data_dict[param_header] = param_value.strip()
        post_data = data_dict
    elif len (parsed_args.form) > 0:
        post_data = '&'.join(post_data)


    for curl_header in parsed_args.header:
        if curl_header.startswith(':'):
            occurrence = [m.start() for m in re.finditer(':', curl_header)]
            header_key, header_value = curl_header[:occurrence[1]], curl_header[occurrence[1] + 1:]
        else:
            header_key, header_value = curl_header.split(":", 1)

        if header_key.lower() == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    return ParsedContext(
        method=method,
        url=parsed_args.url,
        data=post_data,
        headers=quoted_headers,
        cookies=cookie_dict,
        verify=parsed_args.insecure
    )
Esempio n. 18
0
   def setCookies():
       """
 Gets the __ac value from the browser headers and sets it as cookie in order to keep
 the user logged during the tests. If SimpleCookie can't parse the values,
 regular expressions are used.
 """
       headers_cookie = self.browser.headers['set-cookie']
       cookie = Cookie.SimpleCookie()
       cookie.load(headers_cookie)
       if '__ac' in cookie.keys():
           ac_value = cookie['__ac'].value
       else:
           reg = '__ac=\"([A-Za-z0-9%]+)*\"'
           match = re.search(reg, headers_cookie)
           ac_value = match.group(1) if match else None
       self.browser.cookies["__ac"] = ac_value
Esempio n. 19
0
    def __init__(self, iterable, status, headers):
        self._text = None

        self._content = b''.join(iterable)
        if hasattr(iterable, 'close'):
            iterable.close()

        self._status = status
        self._status_code = int(status[:3])
        self._headers = CaseInsensitiveDict(headers)

        cookies = http_cookies.SimpleCookie()
        for name, value in headers:
            if name.lower() == 'set-cookie':
                cookies.load(value)

                if _PY26 or (_PY27 and _JYTHON):
                    match = re.match(r'\s*([^=;,]+)=', value)
                    assert match

                    cookie_name = match.group(1)

                    # NOTE(kgriffs): py26/Jython has a bug that causes
                    # SimpleCookie to incorrectly parse the "expires"
                    # attribute, so we have to do it ourselves. This
                    # algorithm is obviously very naive, but it should
                    # work well enough until we stop supporting
                    # 2.6, at which time we can remove this code.
                    match = re.search('expires=([^;]+)', value)
                    if match:
                        cookies[cookie_name]['expires'] = match.group(1)

                    # NOTE(kgriffs): py26/Jython's SimpleCookie won't
                    # parse the "httponly" and "secure" attributes, so
                    # we have to do it ourselves.
                    if 'httponly' in value:
                        cookies[cookie_name]['httponly'] = True

                    if 'secure' in value:
                        cookies[cookie_name]['secure'] = True

        self._cookies = dict(
            (morsel.key, Cookie(morsel))
            for morsel in cookies.values()
        )

        self._encoding = helpers.get_encoding_from_headers(self._headers)
Esempio n. 20
0
def get_cookie(res, cookie_name):
    headers = res.headers.to_list()
    set_cookie_strings = [
        h for h in headers if (h[0] == 'Set-Cookie' and cookie_name in h[1])
    ]
    try:
        cookie_string = set_cookie_strings[0][1]
    except IndexError as e:
        print(cookie_name)
        for header in headers:
            print(header)
        print(set_cookie_strings)
        raise e

    cookie = http_cookies.SimpleCookie(cookie_string)
    access_granted_cookie = cookie[list(cookie.keys())[0]].value
    return access_granted_cookie
Esempio n. 21
0
def analyze(curl_command):
    method = "get"

    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    base_indent = " " * 4
    data_token = ''
    post_data = parsed_args.data or parsed_args.data_binary
    if post_data:
        method = 'post'
        try:
            post_data_json = json.loads(post_data)
        except ValueError:
            post_data_json = None

        # If we found JSON and it is a dict, pull it apart. Otherwise, just leave as a string
        if post_data_json and isinstance(post_data_json, dict):
            post_data = dict_to_pretty_string(post_data_json)
        else:
            post_data = "'{}',\n".format(post_data)

        data_token = '{}data={}'.format(base_indent, post_data)

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()
    for curl_header in parsed_args.header:
        header_key, header_value = curl_header.split(":", 1)

        if header_key.lower() == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    results = {
        "method": method,
        "url": parsed_args.url,
        "data_token": data_token,
        "headers": quoted_headers,
        "cookies": cookie_dict,
        "base_indent": base_indent
    }
    return results
Esempio n. 22
0
def get_first_matching_cookie(cookie_names, path, resp_headers):
    if 'Set-Cookie' not in resp_headers:
        return None

    cookies = http_cookies.SimpleCookie()
    try:
        cookies.load(resp_headers['Set-Cookie'])
    except:
        return None

    if isinstance(cookie_names, six.string_types):
        cookie_names = [cookie_names]
    for cn in cookie_names:
        if cn in cookies:
            c = cookies[cn]
            if c and cookie_matches_path(c, path):
                return c
    return None
Esempio n. 23
0
    def _get_session_cookie(self):
        '''**Deprecated** Use session_id instead.

        Attempt to retrieve the session cookie from the filesystem.

        :Returns: user's session cookie
        '''
        warnings.warn(
            'session_cookie is deprecated, use session_id'
            ' instead',
            DeprecationWarning,
            stacklevel=2)
        session_id = self.session_id
        if not session_id:
            return ''
        cookie = Cookie.SimpleCookie()
        cookie[self.session_name] = session_id
        return cookie
Esempio n. 24
0
def get_all_matching_cookies(cookie_names, path, resp_headers):
    matching_cookies = None
    if 'Set-Cookie' not in resp_headers:
        return None

    cookies = http_cookies.SimpleCookie()
    try:
        cookies.load(resp_headers['Set-Cookie'])
    except Exception:
        return None

    matching_cookies = []
    for cn in cookie_names:
        if cn in cookies:
            c = cookies[cn]
            if c and cookie_matches_path(c, path):
                matching_cookies.append(c)
    return matching_cookies
Esempio n. 25
0
def parse_context(curl_command):
    method = "get"

    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    post_data = parsed_args.data or parsed_args.data_binary
    if post_data:
        method = 'post'

    if parsed_args.X:
        method = parsed_args.X.lower()

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()

    for curl_header in parsed_args.header:
        if curl_header.startswith(':'):
            occurrence = [m.start() for m in re.finditer(':', curl_header)]
            header_key, header_value = curl_header[:occurrence[
                1]], curl_header[occurrence[1] + 1:]
        else:
            header_key, header_value = curl_header.split(":", 1)

        if header_key.lower().strip("$") == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    # add auth
    user = parsed_args.user
    if parsed_args.user:
        user = tuple(user.split(':'))

    return ParsedContext(method=method,
                         url=parsed_args.url,
                         data=post_data,
                         headers=quoted_headers,
                         cookies=cookie_dict,
                         verify=parsed_args.insecure,
                         auth=user)
Esempio n. 26
0
def parse_cmd(curl_command):
    """
    解析curl的参数并映射到parsed_content的tuple中
    :param: curl_command: 以curl 开头的命令
    :return: <nametuple> 分割后的结果tuple
    """
    method = "get"
    tokens = shlex.split(curl_command)
    parsed_args = parser.parse_args(tokens)

    post_data = parsed_args.data or parsed_args.data_binary
    if post_data:
        method = 'post'
    if parsed_args.X:
        method = parsed_args.X.lower()

    cookie_dict = OrderedDict()
    quoted_headers = OrderedDict()

    # 解析parsed_args.header
    for curl_header in parsed_args.header:
        if curl_header.startswith(':'):
            occurrence = [m.start() for m in re.finditer(':', curl_header)]
            header_key, header_value = curl_header[:occurrence[
                1]], curl_header[occurrence[1] + 1:]
        else:
            header_key, header_value = curl_header.split(":", 1)

        if header_key.lower().strip("$") == 'cookie':
            cookie = Cookie.SimpleCookie(header_value)
            for key in cookie:
                cookie_dict[key] = cookie[key].value
        else:
            quoted_headers[header_key] = header_value.strip()

    # result nametuple
    return parsed_content(method=method,
                          url=parsed_args.url,
                          data=post_data,
                          headers=quoted_headers,
                          cookies=cookie_dict,
                          verify=parsed_args.insecure)
Esempio n. 27
0
def fetch_token(path, headers):
    parse = urlparse.urlparse(path, scheme='ws')
    if parse.scheme not in ('http', 'https', 'ws'):
        # From a bug in urlparse in Python < 2.7.4 we cannot support
        # special schemes (cf: http://bugs.python.org/issue9374)
        if sys.version_info < (2, 7, 4):
            raise exceptions.WebSocketError("We do not support scheme '%s' under "
                                            "Python < 2.7.4, please use http or https" % parse.scheme)

    query = parse.query
    token = urlparse.parse_qs(query).get("token", [""]).pop()
    if not token:
        hcookie = headers.getheader('cookie')
        if hcookie:
            cookie = Cookie.SimpleCookie()
            cookie.load(hcookie)
            if 'token' in cookie:
                token = cookie['token'].value

    return token
Esempio n. 28
0
    def __init__(self, iterable, status, headers):
        self._text = None

        self._content = b''.join(iterable)
        if hasattr(iterable, 'close'):
            iterable.close()

        self._status = status
        self._status_code = int(status[:3])
        self._headers = CaseInsensitiveDict(headers)

        cookies = http_cookies.SimpleCookie()
        for name, value in headers:
            if name.lower() == 'set-cookie':
                cookies.load(value)

        self._cookies = dict(
            (morsel.key, Cookie(morsel)) for morsel in cookies.values())

        self._encoding = helpers.get_encoding_from_headers(self._headers)
Esempio n. 29
0
    def parse(self, headers):
        """
        Parse HTTP header for cookie. If cookie is found, return cookie ID,
        else return None.
        """
        self.log.debug("Parsing cookie from {headers}", headers=headers)

        # see if there already is a cookie set ..
        if 'cookie' in headers:
            try:
                cookie = http_cookies.SimpleCookie()
                cookie.load(str(headers['cookie']))
            except http_cookies.CookieError:
                pass
            else:
                if self._cookie_id_field in cookie:
                    cbtid = cookie[self._cookie_id_field].value
                    if cbtid in self._cookies:
                        return cbtid
        return None
Esempio n. 30
0
def parse_cookie(cookie, final_url):
    if cookie == '':
        return {}

    if not isinstance(cookie, Cookie.BaseCookie):
        try:
            c = Cookie.SimpleCookie()
            c.load(cookie)
        except Cookie.CookieError:
            # Invalid cookie
            return {}
    else:
        c = cookie

    cookiedict = {}

    for key in c.keys():
        cook = c.get(key)
        cookiedict[key] = cook.value
    return cookiedict