2019-12-02 17:43:16 +01:00
|
|
|
"""
|
|
|
|
Persistent, JSON-serialized sessions.
|
2012-08-18 23:03:31 +02:00
|
|
|
|
|
|
|
"""
|
2012-08-17 23:23:02 +02:00
|
|
|
import os
|
2019-09-03 17:14:39 +02:00
|
|
|
import re
|
2020-06-18 23:17:33 +02:00
|
|
|
|
|
|
|
from http.cookies import SimpleCookie
|
2019-08-30 11:32:14 +02:00
|
|
|
from pathlib import Path
|
2020-06-15 22:28:04 +02:00
|
|
|
from typing import Iterable, Optional, Union
|
2019-08-29 08:53:56 +02:00
|
|
|
from urllib.parse import urlsplit
|
2012-08-18 23:03:31 +02:00
|
|
|
|
2019-08-30 11:32:14 +02:00
|
|
|
from requests.auth import AuthBase
|
2020-06-15 23:02:16 +02:00
|
|
|
from requests.cookies import RequestsCookieJar, create_cookie
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2019-09-01 21:15:39 +02:00
|
|
|
from httpie.cli.dicts import RequestHeadersDict
|
2014-04-28 23:33:30 +02:00
|
|
|
from httpie.config import BaseConfigDict, DEFAULT_CONFIG_DIR
|
2020-05-26 10:07:34 +02:00
|
|
|
from httpie.plugins.registry import plugin_manager
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
|
2012-09-17 02:15:00 +02:00
|
|
|
SESSIONS_DIR_NAME = 'sessions'
|
2019-08-30 11:32:14 +02:00
|
|
|
DEFAULT_SESSIONS_DIR = DEFAULT_CONFIG_DIR / SESSIONS_DIR_NAME
|
2013-05-13 14:47:44 +02:00
|
|
|
VALID_SESSION_NAME_PATTERN = re.compile('^[a-zA-Z0-9_.-]+$')
|
2013-05-13 11:54:49 +02:00
|
|
|
# Request headers starting with these prefixes won't be stored in sessions.
|
|
|
|
# They are specific to each request.
|
2019-12-03 19:09:09 +01:00
|
|
|
# <https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests>
|
2013-05-13 11:54:49 +02:00
|
|
|
SESSION_IGNORED_HEADER_PREFIXES = ['Content-', 'If-']
|
|
|
|
|
|
|
|
|
2019-09-03 17:14:39 +02:00
|
|
|
def get_httpie_session(
|
2019-08-30 11:32:14 +02:00
|
|
|
config_dir: Path,
|
2019-09-03 17:14:39 +02:00
|
|
|
session_name: str,
|
|
|
|
host: Optional[str],
|
|
|
|
url: str,
|
|
|
|
) -> 'Session':
|
2013-05-13 14:47:44 +02:00
|
|
|
if os.path.sep in session_name:
|
|
|
|
path = os.path.expanduser(session_name)
|
|
|
|
else:
|
2019-09-03 17:14:39 +02:00
|
|
|
hostname = host or urlsplit(url).netloc.split('@')[-1]
|
2015-02-05 15:25:00 +01:00
|
|
|
if not hostname:
|
|
|
|
# HACK/FIXME: httpie-unixsocket's URLs have no hostname.
|
|
|
|
hostname = 'localhost'
|
2013-05-13 14:47:44 +02:00
|
|
|
|
|
|
|
# host:port => host_port
|
|
|
|
hostname = hostname.replace(':', '_')
|
2019-08-30 11:32:14 +02:00
|
|
|
path = (
|
2019-09-03 17:14:39 +02:00
|
|
|
config_dir / SESSIONS_DIR_NAME / hostname / f'{session_name}.json'
|
2019-08-30 11:32:14 +02:00
|
|
|
)
|
2013-05-13 14:47:44 +02:00
|
|
|
session = Session(path)
|
2012-08-19 04:58:14 +02:00
|
|
|
session.load()
|
2019-09-03 17:14:39 +02:00
|
|
|
return session
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
class Session(BaseConfigDict):
|
2018-09-07 19:10:04 +02:00
|
|
|
helpurl = 'https://httpie.org/doc#sessions'
|
2012-12-01 18:16:00 +01:00
|
|
|
about = 'HTTPie session file'
|
|
|
|
|
2019-08-30 11:32:14 +02:00
|
|
|
def __init__(self, path: Union[str, Path]):
|
2019-12-02 17:43:16 +01:00
|
|
|
super().__init__(path=Path(path))
|
2012-08-19 04:58:14 +02:00
|
|
|
self['headers'] = {}
|
|
|
|
self['cookies'] = {}
|
2012-12-01 18:16:00 +01:00
|
|
|
self['auth'] = {
|
|
|
|
'type': None,
|
2012-12-11 12:54:34 +01:00
|
|
|
'username': None,
|
|
|
|
'password': None
|
2012-12-01 18:16:00 +01:00
|
|
|
}
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2019-09-01 21:15:39 +02:00
|
|
|
def update_headers(self, request_headers: RequestHeadersDict):
|
2013-05-13 11:54:49 +02:00
|
|
|
"""
|
|
|
|
Update the session headers with the request ones while ignoring
|
|
|
|
certain name prefixes.
|
|
|
|
|
|
|
|
"""
|
2019-09-01 21:15:39 +02:00
|
|
|
headers = self.headers
|
2013-05-13 11:54:49 +02:00
|
|
|
for name, value in request_headers.items():
|
2016-02-28 12:14:10 +01:00
|
|
|
|
|
|
|
if value is None:
|
2019-08-30 11:32:14 +02:00
|
|
|
continue # Ignore explicitly unset headers
|
2016-02-28 12:14:10 +01:00
|
|
|
|
2021-01-13 21:45:56 +01:00
|
|
|
if type(value) is not str:
|
|
|
|
value = value.decode('utf8')
|
|
|
|
|
2020-06-18 23:17:33 +02:00
|
|
|
if name.lower() == 'user-agent' and value.startswith('HTTPie/'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if name.lower() == 'cookie':
|
|
|
|
for cookie_name, morsel in SimpleCookie(value).items():
|
|
|
|
self['cookies'][cookie_name] = {'value': morsel.value}
|
|
|
|
del request_headers[name]
|
2013-05-13 11:54:49 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
for prefix in SESSION_IGNORED_HEADER_PREFIXES:
|
|
|
|
if name.lower().startswith(prefix.lower()):
|
|
|
|
break
|
|
|
|
else:
|
2019-09-01 21:15:39 +02:00
|
|
|
headers[name] = value
|
|
|
|
|
|
|
|
self['headers'] = dict(headers)
|
2013-05-13 11:54:49 +02:00
|
|
|
|
|
|
|
@property
|
2019-09-01 21:15:39 +02:00
|
|
|
def headers(self) -> RequestHeadersDict:
|
|
|
|
return RequestHeadersDict(self['headers'])
|
2013-05-13 11:54:49 +02:00
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
@property
|
2019-08-30 11:32:14 +02:00
|
|
|
def cookies(self) -> RequestsCookieJar:
|
2012-08-18 23:03:31 +02:00
|
|
|
jar = RequestsCookieJar()
|
|
|
|
for name, cookie_dict in self['cookies'].items():
|
2012-08-21 15:45:22 +02:00
|
|
|
jar.set_cookie(create_cookie(
|
|
|
|
name, cookie_dict.pop('value'), **cookie_dict))
|
2012-08-18 23:03:31 +02:00
|
|
|
jar.clear_expired_cookies()
|
|
|
|
return jar
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
@cookies.setter
|
2019-08-30 11:32:14 +02:00
|
|
|
def cookies(self, jar: RequestsCookieJar):
|
2019-12-03 19:09:09 +01:00
|
|
|
# <https://docs.python.org/2/library/cookielib.html#cookie-objects>
|
2013-01-22 20:03:28 +01:00
|
|
|
stored_attrs = ['value', 'path', 'secure', 'expires']
|
2012-08-18 23:03:31 +02:00
|
|
|
self['cookies'] = {}
|
2013-03-20 10:45:56 +01:00
|
|
|
for cookie in jar:
|
2017-12-28 18:15:17 +01:00
|
|
|
self['cookies'][cookie.name] = {
|
|
|
|
attname: getattr(cookie, attname)
|
2013-03-20 16:07:23 +01:00
|
|
|
for attname in stored_attrs
|
2017-12-28 18:15:17 +01:00
|
|
|
}
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
@property
|
2019-08-30 11:32:14 +02:00
|
|
|
def auth(self) -> Optional[AuthBase]:
|
2012-08-18 23:03:31 +02:00
|
|
|
auth = self.get('auth', None)
|
2012-12-01 18:16:00 +01:00
|
|
|
if not auth or not auth['type']:
|
2012-12-11 12:54:34 +01:00
|
|
|
return
|
2016-11-23 22:01:58 +01:00
|
|
|
|
|
|
|
plugin = plugin_manager.get_auth_plugin(auth['type'])()
|
|
|
|
|
|
|
|
credentials = {'username': None, 'password': None}
|
|
|
|
try:
|
|
|
|
# New style
|
|
|
|
plugin.raw_auth = auth['raw_auth']
|
|
|
|
except KeyError:
|
|
|
|
# Old style
|
|
|
|
credentials = {
|
|
|
|
'username': auth['username'],
|
|
|
|
'password': auth['password'],
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
if plugin.auth_parse:
|
2019-08-31 15:17:10 +02:00
|
|
|
from httpie.cli.argtypes import parse_auth
|
2016-11-23 22:01:58 +01:00
|
|
|
parsed = parse_auth(plugin.raw_auth)
|
|
|
|
credentials = {
|
|
|
|
'username': parsed.key,
|
|
|
|
'password': parsed.value,
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugin.get_auth(**credentials)
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
@auth.setter
|
2019-08-30 11:32:14 +02:00
|
|
|
def auth(self, auth: dict):
|
2019-08-30 09:56:50 +02:00
|
|
|
assert {'type', 'raw_auth'} == auth.keys()
|
2013-09-21 23:46:15 +02:00
|
|
|
self['auth'] = auth
|
2020-06-15 22:28:04 +02:00
|
|
|
|
|
|
|
def remove_cookies(self, names: Iterable[str]):
|
|
|
|
for name in names:
|
|
|
|
if name in self['cookies']:
|
|
|
|
del self['cookies'][name]
|