3
3
import uuid
4
4
import warnings
5
5
from collections .abc import Mapping
6
- from http .cookiejar import CookieJar
6
+ from http .cookiejar import CookieJar , Cookie
7
7
from urllib .parse import quote_plus , urlencode as _urlencode
8
8
9
9
from twisted .internet .interfaces import IProtocol
30
30
from treq .auth import add_auth
31
31
from treq import multipart
32
32
from treq .response import _Response
33
- from requests .cookies import cookiejar_from_dict , merge_cookies
33
+ from requests .cookies import merge_cookies
34
34
35
35
36
36
_NOTHING = object ()
@@ -43,6 +43,56 @@ def urlencode(query, doseq):
43
43
return s
44
44
45
45
46
+ def _scoped_cookiejar_from_dict (url_object , cookie_dict ):
47
+ """
48
+ Create a CookieJar from a dictionary whose cookies are all scoped to the
49
+ given URL's origin.
50
+
51
+ @note: This does not scope the cookies to any particular path, only the
52
+ host, port, and scheme of the given URL.
53
+ """
54
+ cookie_jar = CookieJar ()
55
+ if cookie_dict is None :
56
+ return cookie_jar
57
+ for k , v in cookie_dict .items ():
58
+ secure = url_object .scheme == 'https'
59
+ port_specified = not (
60
+ (url_object .scheme == "https" and url_object .port == 443 )
61
+ or (url_object .scheme == "http" and url_object .port == 80 )
62
+ )
63
+ port = str (url_object .port )
64
+ domain = url_object .host
65
+ netscape_domain = domain if '.' in domain else domain + '.local'
66
+
67
+ cookie_jar .set_cookie (
68
+ Cookie (
69
+ # Scoping
70
+ domain = netscape_domain ,
71
+ port = port ,
72
+ secure = secure ,
73
+ port_specified = port_specified ,
74
+
75
+ # Contents
76
+ name = k ,
77
+ value = v ,
78
+
79
+ # Constant/always-the-same stuff
80
+ version = 0 ,
81
+ path = "/" ,
82
+ expires = None ,
83
+ discard = False ,
84
+ comment = None ,
85
+ comment_url = None ,
86
+ rfc2109 = False ,
87
+ path_specified = False ,
88
+ domain_specified = False ,
89
+ domain_initial_dot = False ,
90
+ rest = [],
91
+ )
92
+ )
93
+ return cookie_jar
94
+
95
+
46
96
class _BodyBufferingProtocol (proxyForInterface (IProtocol )):
47
97
def __init__ (self , original , buffer , finished ):
48
98
self .original = original
@@ -98,7 +148,9 @@ class HTTPClient:
98
148
def __init__ (self , agent , cookiejar = None ,
99
149
data_to_body_producer = IBodyProducer ):
100
150
self ._agent = agent
101
- self ._cookiejar = cookiejar or cookiejar_from_dict ({})
151
+ if cookiejar is None :
152
+ cookiejar = CookieJar ()
153
+ self ._cookiejar = cookiejar
102
154
self ._data_to_body_producer = data_to_body_producer
103
155
104
156
def get (self , url , ** kwargs ):
@@ -195,7 +247,7 @@ def request(
195
247
headers .setRawHeaders (b'Content-Type' , [contentType ])
196
248
197
249
if not isinstance (cookies , CookieJar ):
198
- cookies = cookiejar_from_dict ( cookies )
250
+ cookies = _scoped_cookiejar_from_dict ( parsed_url , cookies )
199
251
200
252
cookies = merge_cookies (self ._cookiejar , cookies )
201
253
wrapped_agent = CookieAgent (self ._agent , cookies )
0 commit comments