2012-08-18 23:03:31 +02:00
|
|
|
"""Persistent, JSON-serialized sessions.
|
|
|
|
|
|
|
|
"""
|
2012-12-01 18:16:00 +01:00
|
|
|
import re
|
2012-08-17 23:23:02 +02:00
|
|
|
import os
|
2012-08-18 23:03:31 +02:00
|
|
|
|
|
|
|
from requests.cookies import RequestsCookieJar, create_cookie
|
2012-08-17 23:23:02 +02:00
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
from httpie.compat import urlsplit
|
|
|
|
from httpie.config import BaseConfigDict, DEFAULT_CONFIG_DIR
|
2013-09-21 23:46:15 +02:00
|
|
|
from httpie.plugins import plugin_manager
|
2012-08-17 23:23:02 +02:00
|
|
|
|
|
|
|
|
2012-09-17 02:15:00 +02:00
|
|
|
SESSIONS_DIR_NAME = 'sessions'
|
2012-09-21 05:43:34 +02:00
|
|
|
DEFAULT_SESSIONS_DIR = os.path.join(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.
|
|
|
|
# http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests
|
|
|
|
SESSION_IGNORED_HEADER_PREFIXES = ['Content-', 'If-']
|
|
|
|
|
|
|
|
|
2015-02-05 15:25:00 +01:00
|
|
|
def get_response(requests_session, session_name,
|
|
|
|
config_dir, args, read_only=False):
|
2016-02-29 05:56:40 +01:00
|
|
|
"""Like `client.get_responses`, but applies permanent
|
2012-09-07 12:38:52 +02:00
|
|
|
aspects of the session to the request.
|
|
|
|
|
|
|
|
"""
|
2014-05-08 13:27:50 +02:00
|
|
|
from .client import get_requests_kwargs, dump_request
|
2013-05-13 14:47:44 +02:00
|
|
|
if os.path.sep in session_name:
|
|
|
|
path = os.path.expanduser(session_name)
|
|
|
|
else:
|
2016-02-29 05:56:40 +01:00
|
|
|
hostname = (args.headers.get('Host', None) or
|
|
|
|
urlsplit(args.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(':', '_')
|
|
|
|
path = os.path.join(config_dir,
|
|
|
|
SESSIONS_DIR_NAME,
|
|
|
|
hostname,
|
|
|
|
session_name + '.json')
|
|
|
|
|
|
|
|
session = Session(path)
|
2012-08-19 04:58:14 +02:00
|
|
|
session.load()
|
2012-08-18 23:03:31 +02:00
|
|
|
|
2015-02-05 15:25:00 +01:00
|
|
|
kwargs = get_requests_kwargs(args, base_headers=session.headers)
|
2014-05-08 13:27:50 +02:00
|
|
|
if args.debug:
|
2015-02-05 15:25:00 +01:00
|
|
|
dump_request(kwargs)
|
|
|
|
session.update_headers(kwargs['headers'])
|
2012-08-18 23:03:31 +02:00
|
|
|
|
2016-11-23 22:01:58 +01:00
|
|
|
if args.auth_plugin:
|
2013-09-21 23:46:15 +02:00
|
|
|
session.auth = {
|
2016-11-23 22:01:58 +01:00
|
|
|
'type': args.auth_plugin.auth_type,
|
|
|
|
'raw_auth': args.auth_plugin.raw_auth,
|
2013-09-21 23:46:15 +02:00
|
|
|
}
|
2012-08-18 23:03:31 +02:00
|
|
|
elif session.auth:
|
2015-02-05 15:25:00 +01:00
|
|
|
kwargs['auth'] = session.auth
|
2012-08-18 23:03:31 +02:00
|
|
|
|
2013-01-04 02:59:05 +01:00
|
|
|
requests_session.cookies = session.cookies
|
2013-01-03 13:49:41 +01:00
|
|
|
|
2012-08-17 23:23:02 +02:00
|
|
|
try:
|
2015-02-05 15:25:00 +01:00
|
|
|
response = requests_session.request(**kwargs)
|
2012-08-17 23:23:02 +02:00
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
else:
|
2012-09-07 12:48:59 +02:00
|
|
|
# Existing sessions with `read_only=True` don't get updated.
|
2014-04-27 00:07:13 +02:00
|
|
|
if session.is_new() or not read_only:
|
2013-01-03 13:49:41 +01:00
|
|
|
session.cookies = requests_session.cookies
|
2012-09-07 12:38:52 +02:00
|
|
|
session.save()
|
2012-08-17 23:23:02 +02:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2012-09-17 00:37:36 +02:00
|
|
|
class Session(BaseConfigDict):
|
2016-09-12 10:57:30 +02:00
|
|
|
helpurl = 'https://httpie.org/docs#sessions'
|
2012-12-01 18:16:00 +01:00
|
|
|
about = 'HTTPie session file'
|
|
|
|
|
2013-05-13 14:47:44 +02:00
|
|
|
def __init__(self, path, *args, **kwargs):
|
2012-08-18 23:03:31 +02:00
|
|
|
super(Session, self).__init__(*args, **kwargs)
|
2013-05-13 14:47:44 +02:00
|
|
|
self._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
|
|
|
|
2013-05-13 14:47:44 +02:00
|
|
|
def _get_path(self):
|
|
|
|
return self._path
|
2012-12-01 18:16:00 +01:00
|
|
|
|
2013-05-13 11:54:49 +02:00
|
|
|
def update_headers(self, request_headers):
|
|
|
|
"""
|
|
|
|
Update the session headers with the request ones while ignoring
|
|
|
|
certain name prefixes.
|
|
|
|
|
|
|
|
:type request_headers: dict
|
|
|
|
|
|
|
|
"""
|
|
|
|
for name, value in request_headers.items():
|
2016-02-28 12:14:10 +01:00
|
|
|
|
|
|
|
if value is None:
|
|
|
|
continue # Ignore explicitely unset headers
|
|
|
|
|
2014-04-26 17:16:11 +02:00
|
|
|
value = value.decode('utf8')
|
2013-05-13 11:54:49 +02:00
|
|
|
if name == 'User-Agent' and value.startswith('HTTPie/'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for prefix in SESSION_IGNORED_HEADER_PREFIXES:
|
|
|
|
if name.lower().startswith(prefix.lower()):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self['headers'][name] = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def headers(self):
|
|
|
|
return self['headers']
|
|
|
|
|
2012-08-18 23:03:31 +02:00
|
|
|
@property
|
|
|
|
def cookies(self):
|
|
|
|
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
|
|
|
|
def cookies(self, jar):
|
2013-05-13 11:54:49 +02:00
|
|
|
"""
|
|
|
|
:type jar: CookieJar
|
|
|
|
"""
|
2012-12-19 12:30:20 +01:00
|
|
|
# http://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
|
|
|
|
def auth(self):
|
|
|
|
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:
|
|
|
|
from httpie.input import parse_auth
|
|
|
|
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
|
2013-09-21 23:46:15 +02:00
|
|
|
def auth(self, auth):
|
2016-11-23 22:01:58 +01:00
|
|
|
assert set(['type', 'raw_auth']) == set(auth.keys())
|
2013-09-21 23:46:15 +02:00
|
|
|
self['auth'] = auth
|