mirror of
https://github.com/httpie/cli.git
synced 2025-06-30 22:30:46 +02:00
set up util for extracting expired cookies from response header
This commit is contained in:
@ -14,11 +14,11 @@ import urllib3
|
|||||||
|
|
||||||
from httpie import __version__
|
from httpie import __version__
|
||||||
from httpie.cli.dicts import RequestHeadersDict
|
from httpie.cli.dicts import RequestHeadersDict
|
||||||
|
from httpie.models import HTTPResponse
|
||||||
from httpie.plugins.registry import plugin_manager
|
from httpie.plugins.registry import plugin_manager
|
||||||
from httpie.sessions import get_httpie_session
|
from httpie.sessions import get_httpie_session
|
||||||
from httpie.ssl import AVAILABLE_SSL_VERSION_ARG_MAPPING, HTTPieHTTPSAdapter
|
from httpie.ssl import AVAILABLE_SSL_VERSION_ARG_MAPPING, HTTPieHTTPSAdapter
|
||||||
from httpie.utils import repr_dict
|
from httpie.utils import get_expired_cookies, repr_dict
|
||||||
|
|
||||||
|
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
|
|
||||||
@ -82,6 +82,7 @@ def collect_messages(
|
|||||||
if args.compress and prepared_request.body:
|
if args.compress and prepared_request.body:
|
||||||
compress_body(prepared_request, always=args.compress > 1)
|
compress_body(prepared_request, always=args.compress > 1)
|
||||||
response_count = 0
|
response_count = 0
|
||||||
|
expired_cookies = []
|
||||||
while prepared_request:
|
while prepared_request:
|
||||||
yield prepared_request
|
yield prepared_request
|
||||||
if not args.offline:
|
if not args.offline:
|
||||||
@ -95,6 +96,8 @@ def collect_messages(
|
|||||||
**send_kwargs_merged,
|
**send_kwargs_merged,
|
||||||
**send_kwargs,
|
**send_kwargs,
|
||||||
)
|
)
|
||||||
|
expired_cookies += get_expired_cookies(HTTPResponse(response))
|
||||||
|
|
||||||
response_count += 1
|
response_count += 1
|
||||||
if response.next:
|
if response.next:
|
||||||
if args.max_redirects and response_count == args.max_redirects:
|
if args.max_redirects and response_count == args.max_redirects:
|
||||||
@ -110,6 +113,8 @@ def collect_messages(
|
|||||||
if httpie_session:
|
if httpie_session:
|
||||||
if httpie_session.is_new() or not args.session_read_only:
|
if httpie_session.is_new() or not args.session_read_only:
|
||||||
httpie_session.cookies = requests_session.cookies
|
httpie_session.cookies = requests_session.cookies
|
||||||
|
print('expired_cookies -->', expired_cookies)
|
||||||
|
httpie_session.cookies.clear()
|
||||||
httpie_session.save()
|
httpie_session.save()
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
from __future__ import division
|
from __future__ import division
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import time
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from http.cookiejar import parse_ns_headers
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
|
|
||||||
import requests.auth
|
import requests.auth
|
||||||
|
|
||||||
|
from httpie.models import HTTPResponse
|
||||||
|
|
||||||
|
|
||||||
def load_json_preserve_order(s):
|
def load_json_preserve_order(s):
|
||||||
return json.loads(s, object_pairs_hook=OrderedDict)
|
return json.loads(s, object_pairs_hook=OrderedDict)
|
||||||
@ -83,3 +88,27 @@ def get_content_type(filename):
|
|||||||
if encoding:
|
if encoding:
|
||||||
content_type = '%s; charset=%s' % (mime, encoding)
|
content_type = '%s; charset=%s' % (mime, encoding)
|
||||||
return content_type
|
return content_type
|
||||||
|
|
||||||
|
|
||||||
|
def get_expired_cookies(response: HTTPResponse) -> list:
|
||||||
|
original = response._orig.raw._original_response
|
||||||
|
|
||||||
|
expired_cookies = []
|
||||||
|
cookie_headers = []
|
||||||
|
curr_timestamp = time.time()
|
||||||
|
|
||||||
|
for header in original.msg._headers:
|
||||||
|
if header[0] == 'Set-Cookie':
|
||||||
|
cookie_headers.append(header[1])
|
||||||
|
|
||||||
|
extracted_cookies = parse_ns_headers(cookie_headers)
|
||||||
|
|
||||||
|
for cookie in extracted_cookies:
|
||||||
|
cookie_name = cookie[0][0]
|
||||||
|
for cookie_key, cookie_value in cookie:
|
||||||
|
if cookie_key == 'expires':
|
||||||
|
is_expired = curr_timestamp > cookie_value
|
||||||
|
if is_expired:
|
||||||
|
expired_cookies.append(cookie_name)
|
||||||
|
|
||||||
|
return expired_cookies
|
||||||
|
Reference in New Issue
Block a user