2014-01-08 21:33:04 +00:00
|
|
|
# Copyright (C) 2005-2014 Bastian Kleineidam
|
2005-12-18 08:18:32 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-12-18 08:18:32 +00:00
|
|
|
"""
|
2014-02-28 23:12:34 +00:00
|
|
|
Parsing of cookies.
|
2005-12-18 08:18:32 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-05-15 18:37:04 +00:00
|
|
|
from http.cookiejar import split_header_words
|
2019-09-13 19:10:25 +00:00
|
|
|
import email
|
2014-02-28 23:12:34 +00:00
|
|
|
import requests
|
2011-08-01 18:26:31 +00:00
|
|
|
|
2005-12-19 14:08:34 +00:00
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def from_file(filename):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Parse cookie data from a text file in HTTP header format.
|
2006-05-18 16:31:33 +00:00
|
|
|
|
|
|
|
|
@return: list of tuples (headers, scheme, host, path)
|
|
|
|
|
"""
|
2006-05-16 20:56:34 +00:00
|
|
|
entries = []
|
2008-04-27 11:39:21 +00:00
|
|
|
with open(filename) as fd:
|
2006-05-16 20:56:34 +00:00
|
|
|
lines = []
|
|
|
|
|
for line in fd.readlines():
|
|
|
|
|
line = line.rstrip()
|
|
|
|
|
if not line:
|
|
|
|
|
if lines:
|
2018-03-16 11:23:26 +00:00
|
|
|
entries.extend(from_headers("\r\n".join(lines)))
|
2006-05-16 20:56:34 +00:00
|
|
|
lines = []
|
|
|
|
|
else:
|
|
|
|
|
lines.append(line)
|
|
|
|
|
if lines:
|
2018-03-16 11:23:26 +00:00
|
|
|
entries.extend(from_headers("\r\n".join(lines)))
|
2006-05-16 20:56:34 +00:00
|
|
|
return entries
|
|
|
|
|
|
|
|
|
|
|
2020-05-16 19:19:42 +00:00
|
|
|
def from_headers(strheader):
|
2011-05-14 11:56:38 +00:00
|
|
|
"""Parse cookie data from a string in HTTP header (RFC 2616) format.
|
2006-05-18 16:31:33 +00:00
|
|
|
|
2014-02-28 23:12:34 +00:00
|
|
|
@return: list of cookies
|
2006-05-18 16:31:33 +00:00
|
|
|
@raises: ValueError for incomplete or invalid data
|
|
|
|
|
"""
|
2014-02-28 23:12:34 +00:00
|
|
|
res = []
|
2019-09-13 19:10:25 +00:00
|
|
|
headers = email.message_from_string(strheader)
|
2008-03-19 10:22:57 +00:00
|
|
|
if "Host" not in headers:
|
2006-05-16 20:56:34 +00:00
|
|
|
raise ValueError("Required header 'Host:' missing")
|
|
|
|
|
host = headers["Host"]
|
2018-03-16 11:23:26 +00:00
|
|
|
# XXX: our --help says we also pay attention to the Scheme: header,
|
|
|
|
|
# but we don't?!
|
|
|
|
|
path = headers.get("Path", "/")
|
2019-09-13 19:10:25 +00:00
|
|
|
for headervalue in headers.get_all("Set-Cookie"):
|
2018-01-05 16:16:35 +00:00
|
|
|
for pairs in split_header_words([headervalue]):
|
2014-02-28 23:12:34 +00:00
|
|
|
for name, value in pairs:
|
|
|
|
|
cookie = requests.cookies.create_cookie(name, value,
|
|
|
|
|
domain=host, path=path)
|
|
|
|
|
res.append(cookie)
|
|
|
|
|
return res
|